Changeset 4368

Show
Ignore:
Timestamp:
04/10/07 13:51:07 (6 years ago)
Author:
jwrdegoede
Message:

-rewrite dyn chip code, it now uses sensors_feature_get_type() to

identify sysfs entries. Reasons for this rewrite / bugs fixed:
-Don't give features like alarms / sensor type / fault flag a

compute mapping only a normal mapping

-Don't generate features for sysfs entries like uvent, modalias, etc.

instead only generate features for features known by
sensors_feature_get_type()

-Sort the list of found features logically instead of sorted in alphabet order

of the sysfs entry. So now it starts with all in entries, then all fan and
then all temp. Just like the order of most entries in lib/chips.c. Also
this means that it now contains in0 - in10 in that order and not in0, in10,
in1 - in9

Location:
lm-sensors/branches/lm-sensors-3.0.0
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/branches/lm-sensors-3.0.0/lib/access.c

    r4367 r4368  
    516516 
    517517static struct feature_type_match temp_matches[] = { 
    518         { "max", SENSORS_FEATURE_TEMP_MAX }, 
    519         { "min", SENSORS_FEATURE_TEMP_MIN }, 
    520         { "type", SENSORS_FEATURE_TEMP_SENS }, 
    521518        { "hyst", SENSORS_FEATURE_TEMP_HYST }, 
    522519        { "over", SENSORS_FEATURE_TEMP_OVER }, 
     
    525522        { "low", SENSORS_FEATURE_TEMP_LOW }, 
    526523        { "crit", SENSORS_FEATURE_TEMP_CRIT }, 
     524        { "alarm", SENSORS_FEATURE_TEMP_ALARM }, 
    527525        { "fault", SENSORS_FEATURE_TEMP_FAULT }, 
    528         { "alarm", SENSORS_FEATURE_TEMP_ALARM }, 
    529526        { "type", SENSORS_FEATURE_TEMP_SENS }, 
    530527        { 0 } 
     
    532529 
    533530static struct feature_type_match in_matches[] = { 
     531        { "min", SENSORS_FEATURE_IN_MIN }, 
    534532        { "max", SENSORS_FEATURE_IN_MAX }, 
     533        { "alarm", SENSORS_FEATURE_IN_ALARM }, 
     534        { "min_alarm", SENSORS_FEATURE_IN_MIN_ALARM }, 
    535535        { "max_alarm", SENSORS_FEATURE_IN_MAX_ALARM }, 
    536         { "min", SENSORS_FEATURE_IN_MIN }, 
    537         { "min_alarm", SENSORS_FEATURE_IN_MIN_ALARM }, 
    538         { "alarm", SENSORS_FEATURE_IN_ALARM }, 
    539536        { 0 } 
    540537}; 
     
    563560{ 
    564561        const char *name; 
    565         regex_t preg; 
    566562        regmatch_t pmatch[4]; 
    567563        int size_first, size_second, retval, i; 
    568564        struct feature_type_match *submatches; 
     565        static regex_t reg; 
     566        static regex_t *preg = NULL; 
    569567         
    570568        /* use sysname if exists */ 
     
    574572                name = feature->name; 
    575573         
    576         regcomp(&preg, GET_TYPE_REGEX, 0); 
    577          
    578         retval = regexec(&preg, name, 4, pmatch, 0); 
    579          
    580         regfree(&preg); 
     574        if (!preg) { 
     575                regcomp(&reg, GET_TYPE_REGEX, 0); 
     576                preg = ® 
     577        } 
     578         
     579        retval = regexec(preg, name, 4, pmatch, 0); 
    581580         
    582581        if (retval == -1) 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/sensors.h

    r4366 r4368  
    148148             (sensors_chip_name name, int *nr1,int *nr2); 
    149149 
     150/* This enum contains some "magic" used by sensors_read_dynamic_chip() from 
     151   lib/sysfs.c . All the sensor-types (in, fan, temp, misc) are a multiple of 
     152   0x100 apart, and sensor features which should not have a compute_mapping to 
     153   the _input feature start at 0x?10. */ 
    150154typedef enum sensors_feature_type { 
    151   SENSORS_FEATURE_TEMP = 0x0, 
    152   SENSORS_FEATURE_TEMP_ALARM, 
    153   SENSORS_FEATURE_TEMP_FAULT, 
    154   SENSORS_FEATURE_TEMP_SENS, 
     155  SENSORS_FEATURE_IN = 0x000, 
     156  SENSORS_FEATURE_IN_MIN, 
     157  SENSORS_FEATURE_IN_MAX, 
     158  SENSORS_FEATURE_IN_ALARM = 0x010, 
     159  SENSORS_FEATURE_IN_MIN_ALARM, 
     160  SENSORS_FEATURE_IN_MAX_ALARM, 
     161   
     162  SENSORS_FEATURE_FAN = 0x100, 
     163  SENSORS_FEATURE_FAN_MIN, 
     164  SENSORS_FEATURE_FAN_ALARM = 0x110, 
     165  SENSORS_FEATURE_FAN_FAULT, 
     166  SENSORS_FEATURE_FAN_DIV, 
     167   
     168  SENSORS_FEATURE_TEMP = 0x200, 
    155169  SENSORS_FEATURE_TEMP_HYST, 
    156170  SENSORS_FEATURE_TEMP_OVER, 
     
    161175  SENSORS_FEATURE_TEMP_LIM, 
    162176  SENSORS_FEATURE_TEMP_CRIT, 
    163    
    164   SENSORS_FEATURE_IN = 0x100, 
    165   SENSORS_FEATURE_IN_ALARM, 
    166   SENSORS_FEATURE_IN_MIN, 
    167   SENSORS_FEATURE_IN_MAX, 
    168   SENSORS_FEATURE_IN_MIN_ALARM, 
    169   SENSORS_FEATURE_IN_MAX_ALARM, 
    170    
    171   SENSORS_FEATURE_FAN = 0x200, 
    172   SENSORS_FEATURE_FAN_ALARM, 
    173   SENSORS_FEATURE_FAN_FAULT, 
    174   SENSORS_FEATURE_FAN_MIN, 
    175   SENSORS_FEATURE_FAN_DIV, 
     177  SENSORS_FEATURE_TEMP_ALARM = 0x210, 
     178  SENSORS_FEATURE_TEMP_FAULT, 
     179  SENSORS_FEATURE_TEMP_SENS, 
    176180   
    177181  SENSORS_FEATURE_VID = 0x300, 
    178182  SENSORS_FEATURE_VRM, 
    179183   
    180   SENSORS_FEATURE_UNKNOWN = INT_MAX 
     184  SENSORS_FEATURE_UNKNOWN = INT_MAX, 
     185   
     186  /* special the largest number of subfeatures used, iow the  
     187     highest ## from all the 0x?## above + 1*/ 
     188  SENSORS_FEATURE_MAX_SUB_FEATURES = 19 
    181189} sensors_feature_type; 
    182190 
  • lm-sensors/branches/lm-sensors-3.0.0/lib/sysfs.c

    r4367 r4368  
    2222 
    2323#include <string.h> 
     24#include <stdlib.h> 
    2425#include <limits.h> 
    2526#include <errno.h> 
    2627#include <sysfs/libsysfs.h> 
    27 #include <regex.h> 
    2828#include "data.h" 
    2929#include "error.h" 
     
    3636char sensors_sysfs_mount[NAME_MAX]; 
    3737 
    38 #define DYNAMIC_CHIP_REGEX "\\([[:alpha:]]\\{1,\\}[[:digit:]]\\{0,\\}\\)\\(\\_[[:alpha:]]\\{1,\\}\\)\\{0,1\\}" 
    39  
    40 static int sensors_read_dynamic_chip_check_mapping( 
    41         sensors_chip_feature *minor, 
    42         sensors_chip_feature *major, 
    43         regmatch_t *pmatch) 
    44 { 
    45         if (pmatch[1].rm_so == -1 || pmatch[2].rm_so == -1 ||  
    46                         strncmp(minor->data.name, major->data.name,  
    47                         pmatch[1].rm_eo - pmatch[1].rm_so)) { 
    48                 return 0; 
    49         } else { 
    50                 minor->data.mapping =  
    51                         minor->data.compute_mapping =  
    52                         major->data.number; 
    53                          
    54                 return 1; 
    55         } 
    56 } 
     38#define MAX_SENSORS_PER_TYPE 16 
    5739 
    5840static  
    5941sensors_chip_features sensors_read_dynamic_chip(struct sysfs_device *sysdir) 
    6042{ 
    61         int fnum = 1, i, last_major = -1; 
     43        int i, type, fnum = 1; 
    6244        struct sysfs_attribute *attr; 
    6345        struct dlist *attrs; 
    6446        sensors_chip_features ret = {0, 0}; 
    65         sensors_chip_feature features[256], *dyn_features; 
    66         regex_t preg; 
    67         regmatch_t pmatch[3]; 
     47        /* room for all 3  (in, fan, temp) types, with all their subfeatures 
     48           + misc features. We use a large sparse table at first to store all 
     49           found features, so that we can store them sorted at type and index 
     50           and then later create a dense sorted table */ 
     51        sensors_chip_feature features[MAX_SENSORS_PER_TYPE * 
     52                SENSORS_FEATURE_MAX_SUB_FEATURES * 3 + 
     53                SENSORS_FEATURE_MAX_SUB_FEATURES]; 
     54        sensors_chip_feature *dyn_features; 
    6855        char *name; 
    6956                 
     
    7259        if (attrs == NULL) 
    7360                return ret; 
    74          
    75         regcomp(&preg, DYNAMIC_CHIP_REGEX, 0); 
    76                  
     61                 
     62        memset(features, 0, sizeof(features)); 
     63         
    7764        dlist_for_each_data(attrs, attr, struct sysfs_attribute) { 
    78                 sensors_chip_feature feature = { { 0, }, 0, };   
     65                sensors_chip_feature feature = { { 0, }, 0, }; 
    7966                name = attr->name; 
    8067                 
     
    8269                        ret.prefix = strndup(attr->value, strlen(attr->value) - 1); 
    8370                        continue; 
    84                 } else if (regexec(&preg, name, 3, pmatch, 0) != 0) { 
    85                         continue; 
    86                 } 
    87                  
    88                 feature.data.number = fnum; 
    89                 feature.data.mode = (attr->method & (SYSFS_METHOD_SHOW|SYSFS_METHOD_STORE)) ==  
    90                                                                                         (SYSFS_METHOD_SHOW|SYSFS_METHOD_STORE) ? 
    91                                                                                         SENSORS_MODE_RW : 
    92                                                         (attr->method & SYSFS_METHOD_SHOW) ? SENSORS_MODE_R : 
    93                                                         (attr->method & SYSFS_METHOD_STORE) ? SENSORS_MODE_W : 
    94                                                         SENSORS_MODE_NO_RW; 
    95                 feature.data.mapping = SENSORS_NO_MAPPING; 
    96                 feature.data.compute_mapping = SENSORS_NO_MAPPING; 
     71                }  
     72                 
     73                /* check for _input extension and remove */ 
     74                i = strlen(name); 
     75                if (i > 6 && !strcmp(name + i - 6, "_input")) 
     76                        feature.data.name = strndup(name, i-6); 
     77                else 
     78                        feature.data.name = strdup(name); 
     79 
     80                type = sensors_feature_get_type(&feature.data); 
     81                if (type == SENSORS_FEATURE_UNKNOWN) { 
     82                        free((char *)feature.data.name); 
     83                        continue; 
     84                } 
    9785                         
    98                 if (pmatch[2].rm_so != -1) { 
    99                         if (!strcmp(name + pmatch[2].rm_so, "_input")) { 
    100                                 int last_match; 
    101                                  
    102                                 /* copy only the part before the _ */ 
    103                                 feature.data.name = strndup(name,  
    104                                         pmatch[1].rm_eo - pmatch[1].rm_so); 
    105                                  
    106                                 /* check if the features previously read are sub devices of this major 
    107                                    and update their mapping accordingly */  
    108                                 last_match = fnum - 1; 
    109                                 for(i = fnum - 2; i >= 0; i--) { 
    110                                         if (regexec(&preg, features[i].data.name, 3, pmatch, 0) == -1 || 
    111                                                         !sensors_read_dynamic_chip_check_mapping(&features[i], 
    112                                                         &feature, pmatch)) { 
    113                                                 break;                                           
    114                                         } else { 
    115                                                 last_match = i; 
    116                                         } 
    117                                 } 
    118                                  
    119                                 /* if so the major should be in the list before any minor feature, 
    120                                         so swap with the oldest read */ 
    121                                 if (last_match < fnum - 1) { 
    122                                         sensors_chip_feature tmp = features[last_match]; 
    123                                         features[last_match] = feature; 
    124                                         features[fnum - 1] = tmp; 
    125                                                          
    126                                         last_major = last_match; 
    127                                          
    128                                         goto NEXT_NUM; /* NOTE: goto used */ 
    129                                 } else { 
    130                                         last_major = fnum - 1; 
    131                                 } 
    132                                  
    133                         } else { 
    134                                 feature.data.name = strdup(name); 
     86                /* Get N as in this is the N-th in / fan / temp sensor */ 
     87                switch (type & 0xFF00) { 
     88                        case SENSORS_FEATURE_IN: 
     89                                i = strtol(name + 2, NULL, 10); 
     90                                break; 
     91                        case SENSORS_FEATURE_FAN: 
     92                                i = strtol(name + 3, NULL, 10); 
     93                                if (i) i--; 
     94                                break; 
     95                        case SENSORS_FEATURE_TEMP: 
     96                                i = strtol(name + 4, NULL, 10); 
     97                                if (i) i--; 
     98                                break; 
     99                        case SENSORS_FEATURE_VID: /* first misc feature */ 
     100                                i = 0; 
     101                                break; 
     102                } 
     103                 
     104                if (i >= MAX_SENSORS_PER_TYPE) { 
     105                        fprintf(stderr, "libsensors error, more sensors of one" 
     106                                " type then MAX_SENSORS_PER_TYPE, ignoring " 
     107                                "feature: %s\n", name); 
     108                        free((char *)feature.data.name); 
     109                        continue; 
     110                } 
     111                 
     112                /* "calculate" a place to store the feature in our sparse, 
     113                   sorted table */ 
     114                i = (type >> 8) * MAX_SENSORS_PER_TYPE * 
     115                        SENSORS_FEATURE_MAX_SUB_FEATURES + 
     116                        i * SENSORS_FEATURE_MAX_SUB_FEATURES + (type & 0xFF); 
     117                 
     118                if (features[i].data.name) {                     
     119                        fprintf(stderr, "libsensors error, trying to add dupli" 
     120                                "cate feature: %s to dynamic feature table\n", 
     121                                name); 
     122                        free((char *)feature.data.name); 
     123                        continue; 
     124                } 
     125                 
     126                /* fill in the other feature members */ 
     127                feature.data.number = i + 1; 
    135128                         
    136                                 if (last_major != -1 &&  
    137                                                 !sensors_read_dynamic_chip_check_mapping(&feature, 
    138                                                                 &features[last_major], pmatch)) { 
    139                                         last_major = -1; /* no more features for current major */ 
    140                                 } 
    141                         } 
     129                if ( (type & 0xFF00) == SENSORS_FEATURE_VID || 
     130                                (type & 0x00FF) == 0) { 
     131                        /* misc sensor or main feature */ 
     132                        feature.data.mapping = SENSORS_NO_MAPPING; 
     133                        feature.data.compute_mapping = SENSORS_NO_MAPPING; 
     134                } else if (type & 0x10) { 
     135                        /* sub feature without compute mapping */ 
     136                        feature.data.mapping = i - 
     137                                i % SENSORS_FEATURE_MAX_SUB_FEATURES + 1; 
     138                        feature.data.compute_mapping = SENSORS_NO_MAPPING; 
    142139                } else { 
    143                         feature.data.name = strdup(name); 
    144                 } 
    145                          
    146                 features[fnum - 1] = feature;            
    147 NEXT_NUM: 
     140                        feature.data.mapping = i - 
     141                                i % SENSORS_FEATURE_MAX_SUB_FEATURES + 1; 
     142                        feature.data.compute_mapping = feature.data.mapping; 
     143                } 
     144                 
     145                feature.data.mode = 
     146                        (attr->method & (SYSFS_METHOD_SHOW|SYSFS_METHOD_STORE)) 
     147                         == (SYSFS_METHOD_SHOW|SYSFS_METHOD_STORE) ? 
     148                        SENSORS_MODE_RW : (attr->method & SYSFS_METHOD_SHOW) ? 
     149                        SENSORS_MODE_R : (attr->method & SYSFS_METHOD_STORE) ? 
     150                        SENSORS_MODE_W : SENSORS_MODE_NO_RW; 
     151 
     152                features[i] = feature; 
    148153                fnum++; 
    149154        } 
    150                  
    151         regfree(&preg); 
    152          
    153         dyn_features = malloc(sizeof(sensors_chip_feature) * fnum); 
     155 
     156        dyn_features = calloc(fnum, sizeof(sensors_chip_feature)); 
    154157        if (dyn_features == NULL) { 
    155158                sensors_fatal_error(__FUNCTION__,"Out of memory"); 
    156159        } 
    157160         
    158         for(i = 0; i < fnum - 1; i++) { 
    159                 dyn_features[i] = features[i]; 
    160         } 
    161         dyn_features[fnum - 1].data.name = 0; 
     161        fnum = 0; 
     162        for(i = 0; i < sizeof(features)/sizeof(sensors_chip_feature); i++) { 
     163                if (features[i].data.name) { 
     164                        dyn_features[fnum] = features[i]; 
     165                        fnum++; 
     166                } 
     167        } 
    162168         
    163169        ret.feature = dyn_features; 
  • lm-sensors/branches/lm-sensors-3.0.0/prog/sensors/chips_generic.c

    r4366 r4368  
    111111  char *label; 
    112112  int valid, type; 
    113   const int size = SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP; 
    114   short has_features[SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP] = {0, 0, 0, 0, 0, 0, 0, 0, 0 }; 
    115   double feature_vals[SENSORS_FEATURE_TEMP_CRIT - SENSORS_FEATURE_TEMP] = {0.0, }; 
     113  const int size = SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP; 
     114  short has_features[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0, }; 
     115  double feature_vals[SENSORS_FEATURE_TEMP_SENS - SENSORS_FEATURE_TEMP] = {0.0, }; 
    116116   
    117117  if (sensors_get_label_and_valid(*name, feature->number, &label, &valid)) { 
     
    255255  const int size = SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN; 
    256256  int valid; 
    257   short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0, 0, 0, 0, 0 }; 
     257  short has_features[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0, }; 
    258258  double feature_vals[SENSORS_FEATURE_IN_MAX_ALARM - SENSORS_FEATURE_IN] = {0.0, }; 
    259259  double val, alarm_max, alarm_min;