~apw/ubuntu/oneiric/lm-sensors-3/kernel-versions

« back to all changes in this revision

Viewing changes to prog/sensord/sense.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2009-10-14 00:20:17 UTC
  • mfrom: (1.1.2 upstream) (0.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091014002017-36cfephnx9saq42i
Tags: 1:3.1.1-4
* Bumped Standards-version to 3.8.3 (no changes).
* Drop libsensors4 postinst script and the dependency on makedev | 
  udev. This part has been moved to i2c-tools.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <string.h>
27
27
#include <syslog.h>
28
28
 
 
29
#include "args.h"
29
30
#include "sensord.h"
30
31
#include "lib/error.h"
31
32
 
34
35
#define DO_SET 2
35
36
#define DO_RRD 3
36
37
 
37
 
static const char *
38
 
chipName
39
 
(const sensors_chip_name *chip) {
40
 
  static char buffer[256];
41
 
  if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
42
 
    return NULL;
43
 
  return buffer;
44
 
}
45
 
 
46
 
static int
47
 
idChip
48
 
(const sensors_chip_name *chip) {
49
 
  const char *adapter;
50
 
 
51
 
  sensorLog (LOG_INFO, "Chip: %s", chipName (chip));
52
 
  adapter = sensors_get_adapter_name (&chip->bus);
53
 
  if (adapter)
54
 
    sensorLog (LOG_INFO, "Adapter: %s", adapter);
55
 
  
56
 
  return 0;
57
 
}
58
 
 
59
 
static int
60
 
doKnownChip
61
 
(const sensors_chip_name *chip, const ChipDescriptor *descriptor, int action) {
62
 
  const FeatureDescriptor *features = descriptor->features;
63
 
  int index0, subindex;
64
 
  int ret = 0;
65
 
  double tmp;
66
 
 
67
 
  if (action == DO_READ)
68
 
    ret = idChip (chip);
69
 
  for (index0 = 0; (ret == 0) && features[index0].format; ++ index0) {
70
 
    const FeatureDescriptor *feature = features + index0;
71
 
    int alarm, beep;
72
 
    char *label = NULL;
73
 
 
74
 
    if (!(label = sensors_get_label (chip, feature->feature))) {
75
 
      sensorLog (LOG_ERR, "Error getting sensor label: %s/%s", chip->prefix, feature->feature->name);
76
 
      ret = 22;
77
 
    } else {
78
 
      double values[MAX_DATA];
79
 
 
80
 
      alarm = 0;
81
 
      if (!ret && feature->alarmNumber != -1) {
82
 
        if ((ret = sensors_get_value (chip, feature->alarmNumber, &tmp))) {
83
 
          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->alarmNumber, sensors_strerror (ret));
84
 
          ret = 20;
85
 
        } else {
86
 
          alarm = (int) (tmp + 0.5);
87
 
        }
88
 
      }
89
 
      if ((action == DO_SCAN) && !alarm)
90
 
        continue;
91
 
 
92
 
      beep = 0;
93
 
      if (!ret && feature->beepNumber != -1) {
94
 
        if ((ret = sensors_get_value (chip, feature->beepNumber, &tmp))) {
95
 
          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->beepNumber, sensors_strerror (ret));
96
 
          ret = 21;
97
 
        } else {
98
 
          beep = (int) (tmp + 0.5);
99
 
        }
100
 
      }
101
 
 
102
 
      for (subindex = 0; !ret && (feature->dataNumbers[subindex] >= 0); ++ subindex) {
103
 
        if ((ret = sensors_get_value (chip, feature->dataNumbers[subindex], values + subindex))) {
104
 
          sensorLog (LOG_ERR, "Error getting sensor data: %s/#%d: %s", chip->prefix, feature->dataNumbers[subindex], sensors_strerror (ret));
105
 
          ret = 23;
106
 
        }
107
 
      }
108
 
      if (ret == 0) {
109
 
        if (action == DO_RRD) { // arse = "N:"
110
 
          if (feature->rrd) {
111
 
            const char *rrded = feature->rrd (values);
112
 
            strcat (strcat (rrdBuff, ":"), rrded ? rrded : "U");
113
 
          }
114
 
        } else {
115
 
          const char *formatted = feature->format (values, alarm, beep);
116
 
          if (formatted) {
117
 
            if (action == DO_READ) {
118
 
              sensorLog (LOG_INFO, "  %s: %s", label, formatted);
119
 
            } else {
120
 
              sensorLog (LOG_ALERT, "Sensor alarm: Chip %s: %s: %s", chipName (chip), label, formatted);
121
 
            }
122
 
          }
123
 
        }
124
 
      }
125
 
    }
126
 
    if (label)
127
 
      free (label);
128
 
  }
129
 
  return ret;
130
 
}
131
 
 
132
 
static int
133
 
setChip
134
 
(const sensors_chip_name *chip) {
135
 
  int ret = 0;
136
 
  if ((ret = idChip (chip))) {
137
 
    sensorLog (LOG_ERR, "Error identifying chip: %s", chip->prefix);
138
 
  } else if ((ret = sensors_do_chip_sets (chip))) {
139
 
    sensorLog (LOG_ERR, "Error performing chip sets: %s: %s", chip->prefix, sensors_strerror (ret));
140
 
    ret = 50;
141
 
  } else {
142
 
    sensorLog (LOG_INFO, "Set.");
143
 
  }
144
 
  return ret;
145
 
}
146
 
 
147
 
static int
148
 
doChip
149
 
(const sensors_chip_name *chip, int action) {
150
 
  int ret = 0;
151
 
  if (action == DO_SET) {
152
 
    ret = setChip (chip);
153
 
  } else {
154
 
    int index0, chipindex = -1;
155
 
    for (index0 = 0; knownChips[index0].features; ++ index0)
156
 
      /* Trick: we compare addresses here. We know it works because both
157
 
         pointers were returned by sensors_get_detected_chips(), so they
158
 
         refer to libsensors internal structures, which do not move. */
159
 
      if (knownChips[index0].name == chip) {
160
 
        chipindex = index0;
161
 
        break;
162
 
      }
163
 
    if (chipindex >= 0)
164
 
      ret = doKnownChip (chip, &knownChips[chipindex], action);
165
 
  }
166
 
  return ret;
167
 
}
168
 
 
169
 
static int
170
 
doChips
171
 
(int action) {
172
 
  const sensors_chip_name *chip;
173
 
  int i, j, ret = 0;
174
 
 
175
 
  for (j = 0; (ret == 0) && (j < numChipNames); ++ j) {
176
 
    i = 0;
177
 
    while ((ret == 0) && ((chip = sensors_get_detected_chips (&chipNames[j], &i)) != NULL)) {
178
 
      ret = doChip (chip, action);
179
 
    }
180
 
  }
181
 
 
182
 
  return ret;
183
 
}
184
 
 
185
 
int
186
 
readChips
187
 
(void) {
188
 
  int ret = 0;
189
 
 
190
 
  sensorLog (LOG_DEBUG, "sensor read started");
191
 
  ret = doChips (DO_READ);
192
 
  sensorLog (LOG_DEBUG, "sensor read finished");
193
 
 
194
 
  return ret;
195
 
}
196
 
 
197
 
int
198
 
scanChips
199
 
(void) {
200
 
  int ret = 0;
201
 
 
202
 
  sensorLog (LOG_DEBUG, "sensor sweep started"); /* only logged in debug mode */
203
 
  ret = doChips (DO_SCAN);
204
 
  sensorLog (LOG_DEBUG, "sensor sweep finished");
205
 
 
206
 
  return ret;
207
 
}
208
 
 
209
 
int
210
 
setChips
211
 
(void) {
212
 
  int ret = 0;
213
 
 
214
 
  sensorLog (LOG_DEBUG, "sensor set started");
215
 
  ret = doChips (DO_SET);
216
 
  sensorLog (LOG_DEBUG, "sensor set finished");
217
 
 
218
 
  return ret;
 
38
static const char *chipName(const sensors_chip_name *chip)
 
39
{
 
40
        static char buffer[256];
 
41
        if (sensors_snprintf_chip_name(buffer, 256, chip) < 0)
 
42
                return NULL;
 
43
        return buffer;
 
44
}
 
45
 
 
46
static int idChip(const sensors_chip_name *chip)
 
47
{
 
48
        const char *adapter;
 
49
 
 
50
        sensorLog(LOG_INFO, "Chip: %s", chipName (chip));
 
51
        adapter = sensors_get_adapter_name(&chip->bus);
 
52
        if (adapter)
 
53
                sensorLog(LOG_INFO, "Adapter: %s", adapter);
 
54
 
 
55
        return 0;
 
56
}
 
57
 
 
58
static int doKnownChip(const sensors_chip_name *chip,
 
59
                       const ChipDescriptor *descriptor, int action)
 
60
{
 
61
        const FeatureDescriptor *features = descriptor->features;
 
62
        int index0, subindex;
 
63
        int ret = 0;
 
64
        double tmp;
 
65
 
 
66
        if (action == DO_READ)
 
67
                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
        }
 
144
        return ret;
 
145
}
 
146
 
 
147
static int setChip(const sensors_chip_name *chip)
 
148
{
 
149
        int ret = 0;
 
150
        if ((ret = idChip(chip))) {
 
151
                sensorLog(LOG_ERR, "Error identifying chip: %s",
 
152
                          chip->prefix);
 
153
        } else if ((ret = sensors_do_chip_sets(chip))) {
 
154
                sensorLog(LOG_ERR, "Error performing chip sets: %s: %s",
 
155
                          chip->prefix, sensors_strerror(ret));
 
156
                ret = 50;
 
157
        } else {
 
158
                sensorLog(LOG_INFO, "Set.");
 
159
        }
 
160
        return ret;
 
161
}
 
162
 
 
163
static int doChip(const sensors_chip_name *chip, int action)
 
164
{
 
165
        int ret = 0;
 
166
        if (action == DO_SET) {
 
167
                ret = setChip(chip);
 
168
        } else {
 
169
                int index0, chipindex = -1;
 
170
                for (index0 = 0; knownChips[index0].features; ++ index0)
 
171
                        /*
 
172
                         * Trick: we compare addresses here. We know it works
 
173
                         * because both pointers were returned by
 
174
                         * sensors_get_detected_chips(), so they refer to
 
175
                         * libsensors internal structures, which do not move.
 
176
                         */
 
177
                        if (knownChips[index0].name == chip) {
 
178
                                chipindex = index0;
 
179
                                break;
 
180
                        }
 
181
                if (chipindex >= 0)
 
182
                        ret = doKnownChip(chip, &knownChips[chipindex],
 
183
                                          action);
 
184
        }
 
185
        return ret;
 
186
}
 
187
 
 
188
static int doChips(int action)
 
189
{
 
190
        const sensors_chip_name *chip;
 
191
        int i, j, ret = 0;
 
192
 
 
193
        for (j = 0; (ret == 0) && (j < sensord_args.numChipNames); ++ j) {
 
194
                i = 0;
 
195
                while ((ret == 0) &&
 
196
                       ((chip = sensors_get_detected_chips(&sensord_args.chipNames[j], &i)) != NULL)) {
 
197
                        ret = doChip(chip, action);
 
198
                }
 
199
        }
 
200
 
 
201
        return ret;
 
202
}
 
203
 
 
204
int readChips(void)
 
205
{
 
206
        int ret = 0;
 
207
 
 
208
        sensorLog(LOG_DEBUG, "sensor read started");
 
209
        ret = doChips(DO_READ);
 
210
        sensorLog(LOG_DEBUG, "sensor read finished");
 
211
 
 
212
        return ret;
 
213
}
 
214
 
 
215
int scanChips(void)
 
216
{
 
217
        int ret = 0;
 
218
 
 
219
        sensorLog(LOG_DEBUG, "sensor sweep started");
 
220
        ret = doChips(DO_SCAN);
 
221
        sensorLog(LOG_DEBUG, "sensor sweep finished");
 
222
 
 
223
        return ret;
 
224
}
 
225
 
 
226
int setChips(void)
 
227
{
 
228
        int ret = 0;
 
229
 
 
230
        sensorLog(LOG_DEBUG, "sensor set started");
 
231
        ret = doChips(DO_SET);
 
232
        sensorLog(LOG_DEBUG, "sensor set finished");
 
233
 
 
234
        return ret;
219
235
}
220
236
 
221
237
/* TODO: loadavg entry */
222
238
 
223
 
int
224
 
rrdChips
225
 
(void) {
226
 
  int ret = 0;
227
 
 
228
 
  strcpy (rrdBuff, "N");
229
 
 
230
 
  sensorLog (LOG_DEBUG, "sensor rrd started"); 
231
 
  ret = doChips (DO_RRD);
232
 
  sensorLog (LOG_DEBUG, "sensor rrd finished");
233
 
 
234
 
  return ret;
 
239
int rrdChips(void)
 
240
{
 
241
        int ret = 0;
 
242
 
 
243
        strcpy(rrdBuff, "N");
 
244
 
 
245
        sensorLog(LOG_DEBUG, "sensor rrd started");
 
246
        ret = doChips(DO_RRD);
 
247
        sensorLog(LOG_DEBUG, "sensor rrd finished");
 
248
 
 
249
        return ret;
235
250
}