Debian Codegraph @

agent/mibgroup/hardware/sensors/picld_sensors.c

raw
1 #include <net-snmp/net-snmp-config.h>
2 #include <net-snmp/net-snmp-includes.h>
3 #include <net-snmp/agent/net-snmp-agent-includes.h>
4 #include <net-snmp/agent/hardware/sensors.h>
5 #include "hw_sensors_private.h"
6
7 #include <time.h>
8
9 #include <picl.h>
10
11 void netsnmp_sensor_arch_init( void ) {
12 DEBUGMSGTL(("sensors:arch", "Initialise PICLd Sensors module\n"));
13 picl_initialize();
14 }
15
16
17 /*
18 * Handle a numeric-valued sensor
19 */
20 static int
21 read_num_sensor( picl_nodehdl_t childh, const char *propval, float *value )
22 {
23 picl_nodehdl_t sensorh;
24 picl_propinfo_t sensor_info;
25 picl_errno_t error_code;
26
27 union valu {
28 char buf[PICL_PROPSIZE_MAX];
29 uint32_t us4;
30 uint16_t us2;
31 int32_t is4;
32 int16_t is2;
33 float f;
34 } val;
35
36 /*
37 * Retrieve the specified sensor information and value
38 */
39 error_code = picl_get_propinfo_by_name(childh, propval, &sensor_info, &sensorh);
40 if ( error_code != PICL_SUCCESS ) {
41 DEBUGMSGTL(("sensors:arch:detail", "sensor info lookup failed (%d)\n",
42 error_code));
43 return( error_code );
44 }
45
46 error_code = picl_get_propval(sensorh, &val.buf, sensor_info.size);
47 if ( error_code != PICL_SUCCESS ) {
48 DEBUGMSGTL(("sensors:arch:detail", "sensor value lookup failed (%d)\n",
49 error_code));
50 return( error_code );
51 }
52
53 /*
54 * Check the validity (type and size) of this value
55 */
56 if ( sensor_info.type == PICL_PTYPE_FLOAT ) {
57 *value = val.f;
58 } else if ( sensor_info.type == PICL_PTYPE_UNSIGNED_INT ) {
59 /* 16-bit or 32-bit unsigned integers */
60 if ( sensor_info.size == 2 ) {
61 *value = val.us2;
62 } else if ( sensor_info.size == 4 ) {
63 *value = val.us4;
64 } else {
65 DEBUGMSGTL(("sensors:arch:detail", "unsigned integer (%d bit)\n",
66 sensor_info.size * 8));
67 return PICL_FAILURE;
68 }
69 } else if ( sensor_info.type == PICL_PTYPE_INT ) {
70 /* 16-bit or 32-bit signed integers */
71 if ( sensor_info.size == 2 ) {
72 *value = val.is2;
73 } else if ( sensor_info.size == 4 ) {
74 *value = val.is4;
75 } else {
76 DEBUGMSGTL(("sensors:arch:detail", "signed integer (%d bit)\n",
77 sensor_info.size * 8));
78 return PICL_FAILURE;
79 }
80 } else {
81 DEBUGMSGTL(("sensors:arch:detail", "unrecognised type (%d)\n",
82 sensor_info.type));
83 return PICL_FAILURE;
84 }
85
86 return error_code;
87 }
88
89 static int
90 process_num_sensor( picl_nodehdl_t childh, const char *propname, const char *propval, int typ )
91 {
92 netsnmp_sensor_info *sp;
93 float value;
94 picl_errno_t error_code;
95
96 sp = sensor_by_name( propname, typ );
97 if ( !sp ) {
98 return -1;
99 }
100
101 error_code = read_num_sensor( childh, propval, &value );
102 if ( error_code == PICL_SUCCESS ) {
103 sp->value = value;
104 sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE;
105 } else {
106 DEBUGMSGTL(("sensors:arch:detail", "Failed to read %s sensor value (%d)\n",
107 propname, error_code));
108 return -1;
109 }
110 return 0;
111 }
112
113
114
115 /*
116 * Handle an enumeration-valued sensor
117 */
118 const char *switch_settings[] = { "OFF","ON","NORMAL","LOCKED",
119 "UNKNOWN","DIAG","SECURE",
120 NULL };
121 const char *led_settings[] = { "OFF","ON","BLINK",
122 NULL };
123 const char *i2c_settings[] = { "OK",
124 NULL };
125
126 static int
127 read_enum_sensor( picl_nodehdl_t childh, float *value, const char **options )
128 {
129 picl_nodehdl_t sensorh;
130 picl_propinfo_t sensor_info;
131 picl_errno_t error_code;
132 char state[PICL_PROPSIZE_MAX];
133 int i;
134
135 /*
136 * Retrieve the specified sensor information and value
137 */
138 error_code = picl_get_propinfo_by_name(childh, "State", &sensor_info, &sensorh);
139 if ( error_code != PICL_SUCCESS ) {
140 DEBUGMSGTL(("sensors:arch:detail", "sensor info lookup failed (%d)\n",
141 error_code));
142 return( error_code );
143 }
144
145 error_code = picl_get_propval(sensorh, state, sensor_info.size);
146 if ( error_code != PICL_SUCCESS ) {
147 DEBUGMSGTL(("sensors:arch:detail", "sensor value lookup failed (%d)\n",
148 error_code));
149 return( error_code );
150 }
151
152 /*
153 * Try to find a matching entry in the list of options.
154 * Note that some platforms may use upper or lower case
155 * versions of these enumeration values
156 * (so the checks are case insensitive)
157 */
158 *value = 99; /* Dummy value */
159 for ( i=0; options[i] != NULL; i++ ) {
160 if (strncasecmp(state, options[i], strlen(options[i])) == 0) {
161 *value = i;
162 return 0;
163 }
164 }
165
166 DEBUGMSGTL(("sensors:arch:detail", "Enumeration state %s not matched\n",
167 state));
168 return 0; /* Or an error ? */
169 }
170
171 static int
172 process_enum_sensor( picl_nodehdl_t childh, const char *propname, int typ, const char **options )
173 {
174 netsnmp_sensor_info *sp;
175 float value;
176 picl_errno_t error_code;
177
178 sp = sensor_by_name( propname, typ );
179 if ( !sp ) {
180 return -1;
181 }
182
183 error_code = read_enum_sensor( childh, &value, options );
184 if ( error_code == PICL_SUCCESS ) {
185 sp->value = value;
186 sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE;
187 } else {
188 DEBUGMSGTL(("sensors:arch:detail", "Failed to read %s sensor value (%d)\n",
189 propname, error_code));
190 return -1;
191 }
192 return 0;
193 }
194
195
196
197 /*
198 * Recursively walk through the tree of sensors
199 */
200 static int
201 process_sensors( int level, picl_nodehdl_t nodeh ) {
202 picl_nodehdl_t childh, nexth;
203 char propname[ PICL_PROPNAMELEN_MAX ];
204 char propclass[ PICL_CLASSNAMELEN_MAX ];
205 picl_errno_t error_code;
206
207 level++;
208 DEBUGMSGTL(("sensors:arch:detail", "process_sensors - level %d\n", level));
209
210 /* Look up the first child node at this level */
211 error_code = picl_get_propval_by_name( nodeh, PICL_PROP_CHILD,
212 &childh, sizeof(childh));
213 if ( error_code != PICL_SUCCESS ) {
214 DEBUGMSGTL(("sensors:arch:detail", "Failed to get first child node (%d)\n",
215 error_code));
216 return( error_code );
217 }
218
219 /* Step through the child nodes, retrieving the name and class of each one */
220 while ( error_code == PICL_SUCCESS ) {
221 error_code = picl_get_propval_by_name( childh, PICL_PROP_NAME,
222 propname, sizeof(propname)-1);
223 if ( error_code != PICL_SUCCESS ) {
224 /* The Node With No Name */
225 DEBUGMSGTL(("sensors:arch:detail", "get property name failed (%d)\n",
226 error_code));
227 return( error_code );
228 }
229
230 error_code = picl_get_propval_by_name( childh, PICL_PROP_CLASSNAME,
231 propclass, sizeof(propclass)-1);
232 if ( error_code != PICL_SUCCESS ) {
233 /* The Classless Society */
234 DEBUGMSGTL(("sensors:arch:detail", "get property class failed (%d)\n",
235 error_code));
236 return( error_code );
237 }
238
239 DEBUGMSGTL(("sensors:arch:detail", "Name: %s, Class %s\n",
240 propname, propclass ));
241
242
243 /*
244 * Three classes represent further groups of sensors, etc.
245 * Call 'process_sensors' recursively to handle this next level
246 */
247 if (( strstr( propclass, "picl" )) ||
248 ( strstr( propclass, "frutree" )) ||
249 ( strstr( propclass, "obp" ))) {
250 process_sensors( level, childh );
251 }
252 /*
253 * Otherwise retrieve the value appropriately based on the
254 * class of the sensor.
255 *
256 * We need to specify the name of the PICL property to retrieve
257 * for this class of sensor, and the Net-SNMP sensor type.
258 */
259 else if ( strstr( propclass, "fan-tachometer" )) {
260 process_num_sensor( childh, propname, "AtoDSensorValue",
261 NETSNMP_SENSOR_TYPE_RPM );
262 } else if ( strstr( propclass, "fan" )) {
263 process_num_sensor( childh, propname, "Speed",
264 NETSNMP_SENSOR_TYPE_RPM );
265 } else if ( strstr( propclass, "temperature-sensor" )) {
266 process_num_sensor( childh, propname, "Temperature",
267 NETSNMP_SENSOR_TYPE_TEMPERATURE );
268 } else if ( strstr( propclass, "voltage-sensor" )) {
269 process_num_sensor( childh, propname, "Voltage",
270 /* ?? */ NETSNMP_SENSOR_TYPE_VOLTAGE_DC );
271 } else if ( strstr( propclass, "digital-sensor" )) {
272 process_num_sensor( childh, propname, "AtoDSensorValue",
273 /* ?? */ NETSNMP_SENSOR_TYPE_VOLTAGE_DC );
274 /*
275 * Enumeration-valued sensors use a fixed PICL property ("State"),
276 * but take a list of the values appropriate for that sensor,
277 * as well as the Net-SNMP sensor type.
278 */
279 } else if ( strstr( propclass, "switch" )) {
280 process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_OTHER,
281 switch_settings );
282 } else if ( strstr( propclass, "led" )) {
283 process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_OTHER,
284 led_settings );
285 } else if ( strstr( propclass, "i2c" )) {
286 process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_BOOLEAN, /* ?? */
287 i2c_settings );
288 } else {
289 /* Skip other classes of sensor */
290 DEBUGMSGTL(("sensors:arch:detail", "Skipping class %s\n", propclass ));
291 }
292
293 /*
294 * Move on to the next child node at the current level (if any)
295 */
296 error_code = picl_get_propval_by_name( childh, PICL_PROP_PEER,
297 &nexth, sizeof(nexth));
298 if ( error_code != PICL_SUCCESS ) {
299 /* That's All Folks! */
300 return (( error_code == PICL_PROPNOTFOUND )
301 ? PICL_SUCCESS : error_code );
302 }
303 childh = nexth;
304 }
305
306 return error_code;
307 }
308
309
310 int
311 netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) {
312 int error_code;
313 picl_nodehdl_t rooth;
314
315 DEBUGMSGTL(("sensors:arch", "Reload PICLd Sensors module\n"));
316
317 error_code = picl_get_root(&rooth);
318 if ( error_code != PICL_SUCCESS) {
319 DEBUGMSGTL(("sensors:arch", "Couldn't get root node (error %d)\n", error_code));
320 return 1;
321 }
322
323 error_code = process_sensors(0, rooth);
324 if ( error_code != 255 )
325 if ( error_code != 7 ) /* ignore PICL_PROPNOTFOUND error */
326 DEBUGMSGTL(("sensors:arch", "Internal PICLd problem (error %d)\n", error_code));
327
328 return 0;
329 }
330
331 void netsnmp_sensor_arch_shutdown( void ) {
332 DEBUGMSGTL(("sensors:arch", "Shutdown PicLD Sensors module\n"));
333 picl_shutdown();
334 }
335