Changeset 5794

Show
Ignore:
Timestamp:
11/04/09 20:08:32 (4 years ago)
Author:
andy
Message:

sensord: Refactoring of file prog/sensord/sense.c

This patch does some refactoring of function doKnownChip().

* doKnownChip() is a huge function with deep indentation
levels. Splitting this funcion up into smaller ones makes code much
more readable.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/prog/sensord/sense.c

    r5719 r5794  
    4848        const char *adapter; 
    4949 
    50         sensorLog(LOG_INFO, "Chip: %s", chipName (chip)); 
     50        sensorLog(LOG_INFO, "Chip: %s", chipName(chip)); 
    5151        adapter = sensors_get_adapter_name(&chip->bus); 
    5252        if (adapter) 
     
    5656} 
    5757 
     58static int get_flag(const sensors_chip_name *chip, int num) 
     59{ 
     60        double val; 
     61        int ret; 
     62 
     63        if (num == -1) 
     64                return 0; 
     65 
     66        ret = sensors_get_value(chip, num, &val); 
     67        if (ret) { 
     68                sensorLog(LOG_ERR, "Error getting sensor data: %s/#%d: %s", 
     69                          chip->prefix, num, sensors_strerror(ret)); 
     70                return -1; 
     71        } 
     72 
     73        return (int) (val + 0.5); 
     74} 
     75 
     76static int get_features(const sensors_chip_name *chip, 
     77                        const FeatureDescriptor *feature, int action, 
     78                        char *label, int alarm, int beep) 
     79{ 
     80        int i, ret; 
     81        double val[MAX_DATA]; 
     82 
     83        for (i = 0; feature->dataNumbers[i] >= 0; i++) { 
     84                ret = sensors_get_value(chip, feature->dataNumbers[i], 
     85                                        val + i); 
     86                if (ret) { 
     87                        sensorLog(LOG_ERR, 
     88                                  "Error getting sensor data: %s/#%d: %s", 
     89                                  chip->prefix, feature->dataNumbers[i], 
     90                                  sensors_strerror(ret)); 
     91                        return -1; 
     92                } 
     93        } 
     94 
     95        if (action == DO_RRD) { 
     96                if (feature->rrd) { 
     97                        const char *rrded = feature->rrd(val); 
     98 
     99                        /* FIXME: Jean's review comment: 
     100                         * sprintf would me more efficient. 
     101                         */ 
     102                        strcat(strcat (rrdBuff, ":"), rrded ? rrded : "U"); 
     103                } 
     104        } else { 
     105                const char *formatted = feature->format(val, alarm, beep); 
     106 
     107                if (!formatted) { 
     108                        sensorLog(LOG_ERR, "Error formatting sensor data"); 
     109                        return -1; 
     110                } 
     111 
     112                if (action == DO_READ) { 
     113                        sensorLog(LOG_INFO, "  %s: %s", label, formatted); 
     114                } else { 
     115                        sensorLog(LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", 
     116                                  chipName(chip), label, formatted); 
     117                } 
     118        } 
     119        return 0; 
     120} 
     121 
     122static int do_features(const sensors_chip_name *chip, 
     123                       const FeatureDescriptor *feature, int action) 
     124{ 
     125        char *label; 
     126        int alarm, beep; 
     127 
     128        label = sensors_get_label(chip, feature->feature); 
     129        if (!label) { 
     130                sensorLog(LOG_ERR, "Error getting sensor label: %s/%s", 
     131                          chip->prefix, feature->feature->name); 
     132                return -1; 
     133        } 
     134 
     135        alarm = get_flag(chip, feature->alarmNumber); 
     136        if (alarm == -1) 
     137                return -1; 
     138        else if (action == DO_SCAN && !alarm) 
     139                return 0; 
     140 
     141        beep = get_flag(chip, feature->beepNumber); 
     142        if (beep == -1) 
     143                return -1; 
     144 
     145        return get_features(chip, feature, action, label, alarm, beep); 
     146} 
     147 
    58148static int doKnownChip(const sensors_chip_name *chip, 
    59149                       const ChipDescriptor *descriptor, int action) 
    60150{ 
    61151        const FeatureDescriptor *features = descriptor->features; 
    62         int index0, subindex; 
    63         int ret = 0; 
    64         double tmp; 
     152        int i, ret = 0; 
    65153 
    66154        if (action == DO_READ) 
    67155                ret = idChip(chip); 
    68         for (index0 = 0; (ret == 0) && features[index0].format; ++ index0) { 
    69                 const FeatureDescriptor *feature = features + index0; 
    70                 int alarm, beep; 
    71                 char *label = NULL; 
    72  
    73                 if (!(label = sensors_get_label(chip, feature->feature))) { 
    74                         sensorLog(LOG_ERR, 
    75                                   "Error getting sensor label: %s/%s", 
    76                                   chip->prefix, feature->feature->name); 
    77                         ret = 22; 
    78                 } else { 
    79                         double values[MAX_DATA]; 
    80  
    81                         alarm = 0; 
    82                         if (!ret && feature->alarmNumber != -1) { 
    83                                 if ((ret = sensors_get_value(chip, 
    84                                                              feature->alarmNumber, 
    85                                                              &tmp))) { 
    86                                         sensorLog(LOG_ERR, 
    87                                                   "Error getting sensor data: %s/#%d: %s", 
    88                                                   chip->prefix, 
    89                                                   feature->alarmNumber, 
    90                                                   sensors_strerror(ret)); 
    91                                         ret = 20; 
    92                                 } else { 
    93                                         alarm = (int) (tmp + 0.5); 
    94                                 } 
    95                         } 
    96                         if ((action == DO_SCAN) && !alarm) 
    97                                 continue; 
    98  
    99                         beep = 0; 
    100                         if (!ret && feature->beepNumber != -1) { 
    101                                 if ((ret = sensors_get_value(chip, 
    102                                                              feature->beepNumber, 
    103                                                              &tmp))) { 
    104                                         sensorLog(LOG_ERR, 
    105                                                   "Error getting sensor data: %s/#%d: %s", 
    106                                                   chip->prefix, 
    107                                                   feature->beepNumber, 
    108                                                   sensors_strerror(ret)); 
    109                                         ret = 21; 
    110                                 } else { 
    111                                         beep = (int) (tmp + 0.5); 
    112                                 } 
    113                         } 
    114  
    115                         for (subindex = 0; !ret && 
    116                                      (feature->dataNumbers[subindex] >= 0); ++ subindex) { 
    117                                 if ((ret = sensors_get_value(chip, feature->dataNumbers[subindex], values + subindex))) { 
    118                                         sensorLog(LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[subindex], sensors_strerror(ret)); 
    119                                         ret = 23; 
    120                                 } 
    121                         } 
    122                         if (ret == 0) { 
    123                                 if (action == DO_RRD) { // arse = "N:" 
    124                                         if (feature->rrd) { 
    125                                                 const char *rrded = feature->rrd (values); 
    126                                                 strcat(strcat (rrdBuff, ":"), 
    127                                                        rrded ? rrded : "U"); 
    128                                         } 
    129                                 } else { 
    130                                         const char *formatted = feature->format (values, alarm, beep); 
    131                                         if (formatted) { 
    132                                                 if (action == DO_READ) { 
    133                                                         sensorLog(LOG_INFO, "  %s: %s", label, formatted); 
    134                                                 } else { 
    135                                                         sensorLog(LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName(chip), label, formatted); 
    136                                                 } 
    137                                         } 
    138                                 } 
    139                         } 
    140                 } 
    141                 if (label) 
    142                         free(label); 
    143         } 
     156 
     157        for (i = 0; features[i].format; i++) { 
     158                ret = do_features(chip, features + i, action); 
     159                if (ret == -1) 
     160                        break; 
     161        } 
     162 
    144163        return ret; 
    145164} 
     
    168187        } else { 
    169188                int index0, chipindex = -1; 
    170                 for (index0 = 0; knownChips[index0].features; ++ index0) 
     189                for (index0 = 0; knownChips[index0].features; ++index0) { 
    171190                        /* 
    172191                         * Trick: we compare addresses here. We know it works 
     
    179198                                break; 
    180199                        } 
    181                 if (chipindex >= 0) 
     200                } 
     201 
     202                if (chipindex >= 0) { 
    182203                        ret = doKnownChip(chip, &knownChips[chipindex], 
    183204                                          action); 
     205                } 
    184206        } 
    185207        return ret;