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