summaryrefslogtreecommitdiff
path: root/agent/mibgroup/hardware/sensors/picld_sensors.c
blob: a4617b28398ca551c2096dde3e36f8ba4893db54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/hardware/sensors.h>

#include <time.h>

#include <picl.h>

void netsnmp_sensor_arch_init( void ) {
    DEBUGMSGTL(("sensors:arch", "Initialise PICLd Sensors module\n"));
    picl_initialize();
}


/*
 * Handle a numeric-valued sensor
 */
static int
read_num_sensor( picl_nodehdl_t childh, const char *propval, float *value )
{
    picl_nodehdl_t  sensorh;
    picl_propinfo_t sensor_info;
    picl_errno_t    error_code;

    union valu {
        char buf[PICL_PROPSIZE_MAX];
        uint32_t us4;
        uint16_t us2;
        int32_t is4;
        int16_t is2;
        float f;
    } val;

    /*
     *  Retrieve the specified sensor information and value
     */
    error_code = picl_get_propinfo_by_name(childh, propval, &sensor_info, &sensorh);
    if ( error_code != PICL_SUCCESS ) {
        DEBUGMSGTL(("sensors:arch:detail", "sensor info lookup failed (%d)\n",
                                            error_code));
        return( error_code );
    }

    error_code = picl_get_propval(sensorh, &val.buf, sensor_info.size);
    if ( error_code != PICL_SUCCESS ) {
        DEBUGMSGTL(("sensors:arch:detail", "sensor value lookup failed (%d)\n",
                                            error_code));
        return( error_code );
    }

    /*
     *  Check the validity (type and size) of this value
     */
    if ( sensor_info.type == PICL_PTYPE_FLOAT ) {
        *value = val.f;
    } else if ( sensor_info.type == PICL_PTYPE_UNSIGNED_INT ) {
        /* 16-bit or 32-bit unsigned integers */
        if ( sensor_info.size == 2 ) {
            *value = val.us2;
        } else if ( sensor_info.size == 4 ) { 
            *value = val.us4;
        } else {
            DEBUGMSGTL(("sensors:arch:detail", "unsigned integer (%d bit)\n",
                                                sensor_info.size * 8));
            return PICL_FAILURE;
        }
    } else if ( sensor_info.type == PICL_PTYPE_INT ) {
        /* 16-bit or 32-bit signed integers */
        if ( sensor_info.size == 2 ) {
            *value = val.is2;
        } else if ( sensor_info.size == 4 ) { 
            *value = val.is4;
        } else {
            DEBUGMSGTL(("sensors:arch:detail", "signed integer (%d bit)\n",
                                                sensor_info.size * 8));
            return PICL_FAILURE;
        }
    } else {
        DEBUGMSGTL(("sensors:arch:detail", "unrecognised type (%d)\n",
                                            sensor_info.type));
        return PICL_FAILURE;
    }

    return error_code;
}

static int
process_num_sensor( picl_nodehdl_t childh, const char *propname, const char *propval, int typ )
{
    netsnmp_sensor_info        *sp;
    float                       value;
    picl_errno_t    error_code;

    sp = sensor_by_name( propname, typ );
    if ( !sp ) {
         return -1;
    }

    error_code = read_num_sensor( childh, propval, &value );
    if ( error_code == PICL_SUCCESS ) {
        sp->value = value;
        sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE;
    } else {
        DEBUGMSGTL(("sensors:arch:detail", "Failed to read %s sensor value (%d)\n",
                                            propname, error_code));
        return -1;
    }
    return 0;
}



/*
 *    Handle an enumeration-valued sensor
 */
const char *switch_settings[] = { "OFF","ON","NORMAL","LOCKED",
				  "UNKNOWN","DIAG","SECURE",
                                  NULL };
const char *led_settings[]    = { "OFF","ON","BLINK",
                                  NULL };
const char *i2c_settings[]    = { "OK",
                                  NULL };

static int
read_enum_sensor( picl_nodehdl_t childh, float *value, const char **options )
{
    picl_nodehdl_t  sensorh;
    picl_propinfo_t sensor_info;
    picl_errno_t    error_code;
    char            state[PICL_PROPSIZE_MAX];
    int             i;

    /*
     *  Retrieve the specified sensor information and value
     */
    error_code = picl_get_propinfo_by_name(childh, "State", &sensor_info, &sensorh);
    if ( error_code != PICL_SUCCESS ) {
        DEBUGMSGTL(("sensors:arch:detail", "sensor info lookup failed (%d)\n",
                                            error_code));
        return( error_code );
    }

    error_code = picl_get_propval(sensorh, state, sensor_info.size);
    if ( error_code != PICL_SUCCESS ) {
        DEBUGMSGTL(("sensors:arch:detail", "sensor value lookup failed (%d)\n",
                                            error_code));
        return( error_code );
    }

    /*
     * Try to find a matching entry in the list of options.
     * Note that some platforms may use upper or lower case
     *   versions of these enumeration values
     *  (so the checks are case insensitive)
     */
    *value = 99;    /* Dummy value */
    for ( i=0;  options[i] != NULL; i++ ) {
        if (strncasecmp(state, options[i], strlen(options[i])) == 0) {
            *value = i;
            return 0;
        }
    }
    
    DEBUGMSGTL(("sensors:arch:detail", "Enumeration state %s not matched\n",
                                        state));
    return 0;  /* Or an error ? */
}

static int
process_enum_sensor( picl_nodehdl_t childh, const char *propname, int typ, const char **options )
{
    netsnmp_sensor_info        *sp;
    float                       value;
    picl_errno_t    error_code;

    sp = sensor_by_name( propname, typ );
    if ( !sp ) {
         return -1;
    }

    error_code = read_enum_sensor( childh, &value, options );
    if ( error_code == PICL_SUCCESS ) {
        sp->value = value;
        sp->flags|= NETSNMP_SENSOR_FLAG_ACTIVE;
    } else {
        DEBUGMSGTL(("sensors:arch:detail", "Failed to read %s sensor value (%d)\n",
                                            propname, error_code));
        return -1;
    }
    return 0;
}



/*
 *  Recursively walk through the tree of sensors
 */
static int
process_sensors( int level, picl_nodehdl_t nodeh ) {
    picl_nodehdl_t childh, nexth;
    char           propname[  PICL_PROPNAMELEN_MAX  ];
    char           propclass[ PICL_CLASSNAMELEN_MAX ];
    picl_errno_t   error_code;

    level++;
    DEBUGMSGTL(("sensors:arch:detail", "process_sensors - level %d\n", level));

    /* Look up the first child node at this level */
    error_code = picl_get_propval_by_name( nodeh, PICL_PROP_CHILD,
                                           &childh, sizeof(childh));
    if ( error_code != PICL_SUCCESS ) {
        DEBUGMSGTL(("sensors:arch:detail", "Failed to get first child node (%d)\n",
                                            error_code));
        return( error_code );
    }

    /* Step through the child nodes, retrieving the name and class of each one */
    while ( error_code == PICL_SUCCESS ) {
        error_code = picl_get_propval_by_name( childh, PICL_PROP_NAME,
                                               propname, sizeof(propname)-1);
        if ( error_code != PICL_SUCCESS ) {
            /* The Node With No Name */
            DEBUGMSGTL(("sensors:arch:detail", "get property name failed (%d)\n",
                                                error_code));
            return( error_code );
        }

        error_code = picl_get_propval_by_name( childh, PICL_PROP_CLASSNAME,
                                               propclass, sizeof(propclass)-1);
        if ( error_code != PICL_SUCCESS ) {
            /* The Classless Society */
            DEBUGMSGTL(("sensors:arch:detail", "get property class failed (%d)\n",
                                                error_code));
            return( error_code );
        }

        DEBUGMSGTL(("sensors:arch:detail", "Name: %s, Class %s\n",
                                            propname, propclass ));


        /*
         *  Three classes represent further groups of sensors, etc.
         *  Call 'process_sensors' recursively to handle this next level
         */
        if (( strstr( propclass, "picl"    )) ||
            ( strstr( propclass, "frutree" )) ||
            ( strstr( propclass, "obp"     ))) {
            process_sensors( level, childh );
        }
        /*
         *  Otherwise retrieve the value appropriately based on the
         *     class of the sensor.
         *
         *  We need to specify the name of the PICL property to retrieve
         *     for this class of sensor, and the Net-SNMP sensor type.
         */
        else if ( strstr( propclass, "fan-tachometer" )) {
            process_num_sensor( childh, propname, "AtoDSensorValue",
                                                   NETSNMP_SENSOR_TYPE_RPM );
        } else if ( strstr( propclass, "fan" )) {
            process_num_sensor( childh, propname, "Speed",
                                                   NETSNMP_SENSOR_TYPE_RPM );
        } else if ( strstr( propclass, "temperature-sensor" )) {
            process_num_sensor( childh, propname, "Temperature",
                                                   NETSNMP_SENSOR_TYPE_TEMPERATURE );
        } else if ( strstr( propclass, "voltage-sensor" )) {
            process_num_sensor( childh, propname, "Voltage",
                                          /* ?? */ NETSNMP_SENSOR_TYPE_VOLTAGE_DC );
        } else if ( strstr( propclass, "digital-sensor" )) {
            process_num_sensor( childh, propname, "AtoDSensorValue",
                                          /* ?? */ NETSNMP_SENSOR_TYPE_VOLTAGE_DC );
            /*
             * Enumeration-valued sensors use a fixed PICL property ("State"),
             *   but take a list of the values appropriate for that sensor,
             *   as well as the Net-SNMP sensor type.
             */
        } else if ( strstr( propclass, "switch" )) {
            process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_OTHER,
                                                   switch_settings );
        } else if ( strstr( propclass, "led" )) {
            process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_OTHER,
                                                   led_settings );
        } else if ( strstr( propclass, "i2c" )) {
            process_enum_sensor( childh, propname, NETSNMP_SENSOR_TYPE_BOOLEAN, /* ?? */
                                                   i2c_settings );
        } else {
            /* Skip other classes of sensor */
            DEBUGMSGTL(("sensors:arch:detail", "Skipping class %s\n", propclass ));
        }

        /*
         *  Move on to the next child node at the current level (if any)
         */
        error_code = picl_get_propval_by_name( childh, PICL_PROP_PEER,
                                               &nexth, sizeof(nexth));
        if ( error_code != PICL_SUCCESS ) {
            /* That's All Folks! */
            return (( error_code == PICL_PROPNOTFOUND )
                          ? PICL_SUCCESS : error_code );
        }
        childh = nexth;
    }
    
    return error_code;
}


int
netsnmp_sensor_arch_load(netsnmp_cache *cache, void *vp) {
    int               error_code;
    picl_nodehdl_t    rooth;

    DEBUGMSGTL(("sensors:arch", "Reload PICLd Sensors module\n"));

    error_code = picl_get_root(&rooth);
    if ( error_code != PICL_SUCCESS) {
        DEBUGMSGTL(("sensors:arch", "Couldn't get root node (error %d)\n", error_code));
        return 1;
    }

    error_code = process_sensors(0, rooth);
    if ( error_code != 255 )
        if ( error_code != 7 )  /* ignore PICL_PROPNOTFOUND error */
            DEBUGMSGTL(("sensors:arch", "Internal PICLd problem (error %d)\n", error_code));

    return 0;
}

void netsnmp_sensor_arch_shutdown( void ) {
    DEBUGMSGTL(("sensors:arch", "Shutdown PicLD Sensors module\n"));
    picl_shutdown();
}