]> git.decadent.org.uk Git - ap-utils.git/blob - lib/sysinfo.c
Imported Upstream version 1.5~pre1
[ap-utils.git] / lib / sysinfo.c
1 /*
2  *      sysinfo.c from Access Point SNMP Utils for Linux
3  *
4  * Copyright (c) 2002 Roman Festchook <roma at polesye dot net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License Version 2 from
8  * June 1991 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  */
20
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/wait.h>
27 #include "ap-utils.h"
28
29 #define SYS_DESCR _("System Description: ")
30 #define SYSINFO _("System Info")
31
32 #define OID_NUM 11
33
34 extern WINDOW *main_sub;
35
36 void atmel_sysinfo()
37 {
38     char sysDescr[] = {
39         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x01, 0x00
40     };
41     char sysDeviceInfo[] = {
42         0x2B, 0x06, 0x01, 0x04, 0x01, 0x83, 0x1A, 0x01, 0x01, 0x01, 0x05, 0x00
43     };
44     /* This one is ATMEL12350 VERNET MIB specific. */
45     char UpTime[] = {
46         0x2B, 0x06, 0x01, 0x04, 0x01, 0xE0, 0x3E, 0x01, 0x02, 0x09, 0x01, 0x00
47     };
48
49     char message[200];
50     int i;
51     varbind varbinds[3];
52     struct sysDeviceInfo_ATMEL410 str410;
53     struct sysDeviceInfo_ATMEL12350 str12350;
54     extern short ap_type, ap_vendorext;
55     extern int LINES, wait_mode;
56     extern rdprops regdom_types[];
57
58     /*
59      * These have to represent used sysDeviceInfo_{ATMEL410,ATMEL12350} members
60      */
61     uint32_t ap_sversion;
62     char *ap_macaddr;
63     uint32_t ap_regdomain;
64     uint32_t ap_prodtype;
65     char *ap_oemname;
66     uint32_t ap_oemid;
67     char *ap_prodname;
68     uint32_t ap_hwrev;
69
70     if (ap_type == ATMEL12350) {
71         sysDescr[5] = 0xE0;
72         sysDescr[6] = 0x3E;
73         sysDeviceInfo[5] = 0xE0;
74         sysDeviceInfo[6] = 0x3E;
75     }
76
77     print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, SYSINFO);
78
79     if (wait_mode == WAIT_TIMEOUT)
80         print_help(QT_HELP);
81
82     while (1) {
83         i = 0;
84
85         varbinds[0].oid = sysDescr;
86         varbinds[0].len_oid = sizeof(sysDescr);
87         varbinds[0].value = sysDescr;
88         varbinds[0].type = NULL_VALUE;
89         varbinds[0].len_val = 0;
90         i++;
91
92         varbinds[1].oid = sysDeviceInfo;
93         varbinds[1].len_oid = sizeof(sysDeviceInfo);
94         varbinds[1].value = sysDeviceInfo;
95         varbinds[1].len_val = 0;
96         varbinds[1].type = NULL_VALUE;
97         i++;
98
99         if (ap_vendorext == VERNET) {
100             varbinds[2].oid = UpTime;
101             varbinds[2].len_oid = sizeof(UpTime);
102             varbinds[2].value = UpTime;
103             varbinds[2].len_val = 0;
104             varbinds[2].type = NULL_VALUE;
105             i++;
106         }
107
108         if (wait_mode == WAIT_FOREVER)
109             print_help(WAIT_RET);
110
111         if (snmp(varbinds, i, GET) <= 0) {
112             print_helperr(ERR_RET);
113             getch();
114             goto quit;
115         }
116
117         if (wait_mode == WAIT_FOREVER)
118             print_help(QT_HELP);
119
120         mvwaddstr(main_sub, 0, 0, _("Device hardware/software/name info:"));
121         for (i = 0; i < varbinds[0].len_val && *(varbinds[0].value + i); i++)
122             mvwaddch(main_sub, 1, i + 1, *(varbinds[0].value + i));
123
124         if (ap_type == ATMEL410) {
125             memcpy(&str410, varbinds[1].value,
126                 sizeof(struct sysDeviceInfo_ATMEL410));
127             ap_sversion = str410.StructVersion;
128             ap_macaddr = str410.MacAddress;
129             ap_regdomain = swap4(str410.RegulatoryDomain);
130             ap_prodtype = str410.ProductType;
131             ap_oemname = str410.OEMName;
132             ap_oemid = str410.OEMID;
133             ap_prodname = str410.ProductName;
134             ap_hwrev = str410.HardwareRevision;
135         } else { /* ATMEL12350 */
136             memcpy(&str12350, varbinds[1].value,
137                 sizeof(struct sysDeviceInfo_ATMEL12350));
138             ap_regdomain = str12350.RegulatoryDomain;
139             ap_sversion = str12350.StructVersion;
140             ap_macaddr = str12350.MacAddress;
141             ap_prodtype = str12350.ProductType;
142             ap_oemname = str12350.OEMName;
143             ap_oemid = str12350.OEMID;
144             ap_prodname = str12350.ProductName;
145             ap_hwrev = str12350.HardwareRevision;
146         }
147
148         sprintf(message, "%s%02X%02X%02X%02X%02X%02X", MAC,
149             ap_macaddr[0] & 0xFF, ap_macaddr[1] & 0xFF,
150             ap_macaddr[2] & 0xFF, ap_macaddr[3] & 0xFF,
151             ap_macaddr[4] & 0xFF, ap_macaddr[5] & 0xFF);
152         mvwaddstr(main_sub, 2, 0, message);
153
154         mvwaddstr(main_sub, 3, 0, _("Product name:"));
155         for (i = 0; i < 32 && ap_prodname[i]; i++)
156             mvwaddch(main_sub, 3, i + 14, ap_prodname[i]);
157
158         sprintf(message, _("Product type: %u"), swap4(ap_prodtype));
159         mvwaddstr(main_sub, 4, 0, message);
160
161         mvwaddstr(main_sub, 5, 0, _("OEM name:"));
162         for (i = 0; i < 32 && ap_oemname[i]; i++)
163             mvwaddch(main_sub, 5, i + 10, ap_oemname[i]);
164
165         sprintf(message, "OEM ID: %u", swap4(ap_oemid));
166         mvwaddstr(main_sub, 6, 0, message);
167
168         sprintf(message, _("Hardware revision: %u"), swap4(ap_hwrev));
169         mvwaddstr(main_sub, 7, 0, message);
170
171         i = regdom_idx(ap_regdomain);
172         sprintf(message, "Regulatory domain: %s [%d]",
173             regdom_types[i].desc, ap_regdomain);
174         mvwaddstr(main_sub, 8, 0, message);
175
176         sprintf(message, _("Info structure version: %u"), swap4(ap_sversion));
177         mvwaddstr(main_sub, 9, 0, message);
178
179         sprintf(message, _("Manufacturer OUI: %02X %02X %02X (%s)"),
180             ap_macaddr[0] & 0xFF, ap_macaddr[1] & 0xFF, ap_macaddr[2] & 0xFF,
181             oui2manufacturer(ap_macaddr));
182         mvwaddstr(main_sub, 10, 0, message);
183
184         if (ap_vendorext == VERNET) {
185             unsigned int j = 0;
186
187             if (varbinds[2].len_val > 1) {
188                 for (i = 0; i < varbinds[2].len_val; i++)
189                     j |= (varbinds[2].value[i]
190                         << (8 * (varbinds[2].len_val - i - 1)));
191             } else {
192                 j = varbinds[2].value[0] & 0x80 ?
193                     0xff00 | varbinds[2].value[0] : varbinds[2].value[0];
194             }
195             sprintf(message,
196                 _("Uptime: %u days, %02u:%02u:%02u hours:mins:secs"),
197                 j / 60 / 60 / 24, j / 60 / 60 % 24, j / 60 % 60, j % 60);
198             mvwaddstr(main_sub, 11, 0, message);
199         }
200
201         wrefresh(main_sub);
202
203         i = wait_key();
204         if (i == -1)
205             goto quit;
206
207         switch((char) i) {
208             case 'q':
209             case 'Q':
210                 goto quit;
211             case 't':
212             case 'T':
213                 wait_mode = (wait_mode == WAIT_FOREVER ?
214                     WAIT_TIMEOUT : WAIT_FOREVER);
215                 print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
216                     SYSINFO);
217         }
218
219         /* either timeout for user input (i == 0) or invalid key => continue */
220     }
221   quit:
222     print_top(NULL, NULL);
223     clear_main(0);
224 }
225
226 int get_RegDomain()
227 {
228     char oid_dot11CurrentRegDomain[] =
229         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x02, 0x01 };
230     varbind varbinds[1];
231
232     varbinds[0].oid = oid_dot11CurrentRegDomain;
233     varbinds[0].len_oid = sizeof(oid_dot11CurrentRegDomain);
234     varbinds[0].value = oid_dot11CurrentRegDomain;
235     varbinds[0].type = NULL_VALUE;
236     varbinds[0].len_val = 0;
237
238     if (snmp(varbinds, 1, GET) <= 0)
239         return 0;
240     return *(varbinds[0].value);
241 }
242
243 void nwn_sysinfo()
244 {
245     char oid_mac[] =
246         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x01, 0x01 };
247     char oid_manufacturerID[] =
248         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x08, 0x01 };
249     char oid_productID[] =
250         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x02, 0x01, 0x01, 0x09, 0x01 };
251     char oid_dot11manufacturerOUI[] =
252         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x01,
253         0x01
254     };
255     char oid_dot11manufacturerName[] =
256         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x02,
257         0x01
258     };
259     char oid_dot11manufacturerProductName[] =
260         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x03,
261         0x01
262     };
263     char oid_dot11manufacturerProductVersion[] =
264         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x03, 0x01, 0x02, 0x01, 0x04,
265         0x01
266     };
267     char oid_dot11PHYType[] =
268         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x01, 0x01 };
269     char oid_dot11TempType[] =
270         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x01, 0x01, 0x03, 0x01 };
271     char oid_TxPowerLevel1[] =
272         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x03, 0x01, 0x02, 0x01 };
273     char oid_dot11PrivacyOptionImplemented[] =
274         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x01, 0x01, 0x01, 0x07, 0x01 };
275     char oid_dot11DiversitySupport[] =
276         { 0x2a, 0x86, 0x48, 0xce, 0x34, 0x04, 0x02, 0x01, 0x03, 0x01 };
277     char oid_sysDescr[] =
278         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x01, 0x00 };
279     char oid_sysUptime[] =
280         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x01, 0x03, 0x00 };
281     char oid_ip[] =
282         { 0x2b, 0x06, 0x01, 0x02, 0x01, 0x04, 0x14, 0x01, 0x01 };
283     char *types[3] =
284         { _("FHSS 2.4 GHz"), _("DSSS 2.4 GHz"), _("IR Baseband") };
285     char *temp[3] = { _("Commercial range 0..40 C"),
286         _("Industrial range -30..70 C"),
287         _("unknown")
288     }, *diversity[3] = {
289     _("manual"), _("notsupported"), _("dynamic")};
290
291     char message[300];
292     int i, hand, c, min, sec;
293     struct in_addr ip;
294     varbind varbinds[OID_NUM];
295     size_t time_stamp;
296     extern int wait_mode;
297
298     print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF, SYSINFO);
299
300     if (wait_mode == WAIT_TIMEOUT)
301         print_help(QT_HELP);
302
303     while (1) {
304         for (i = 0; i < OID_NUM; i++) {
305             varbinds[i].value = oid_mac;
306             varbinds[i].type = NULL_VALUE;
307             varbinds[i].len_val = 0;
308         }
309         varbinds[0].oid = oid_mac;
310         varbinds[0].len_oid = sizeof(oid_mac);
311         varbinds[1].oid = oid_manufacturerID;
312         varbinds[1].len_oid = sizeof(oid_manufacturerID);
313         varbinds[2].oid = oid_productID;
314         varbinds[2].len_oid = sizeof(oid_productID);
315         varbinds[3].oid = oid_dot11manufacturerOUI;
316         varbinds[3].len_oid = sizeof(oid_dot11manufacturerOUI);
317         varbinds[4].oid = oid_dot11manufacturerName;
318         varbinds[4].len_oid = sizeof(oid_dot11manufacturerName);
319         varbinds[5].oid = oid_dot11manufacturerProductName;
320         varbinds[5].len_oid = sizeof(oid_dot11manufacturerProductName);
321         varbinds[6].oid = oid_dot11manufacturerProductVersion;
322         varbinds[6].len_oid = sizeof(oid_dot11manufacturerProductVersion);
323         varbinds[7].oid = oid_dot11PHYType;
324         varbinds[7].len_oid = sizeof(oid_dot11PHYType);
325         varbinds[8].oid = oid_dot11TempType;
326         varbinds[8].len_oid = sizeof(oid_dot11TempType);
327         varbinds[9].oid = oid_TxPowerLevel1;
328         varbinds[9].len_oid = sizeof(oid_TxPowerLevel1);
329
330         if (wait_mode == WAIT_FOREVER)
331             print_help(WAIT_RET);
332
333         if (snmp(varbinds, OID_NUM - 1, GET) <= 0) {
334             print_helperr(ERR_RET);
335             getch();
336             goto quit;
337         }
338
339         sprintf(message, "%s%02X%02X%02X%02X%02X%02X", MAC,
340             varbinds[0].value[0] & 0xFF, varbinds[0].value[1] & 0xFF,
341             varbinds[0].value[2] & 0xFF, varbinds[0].value[3] & 0xFF,
342             varbinds[0].value[4] & 0xFF, varbinds[0].value[5] & 0xFF);
343         mvwaddstr(main_sub, 0, 0, message);
344
345         mvwaddstr(main_sub, 3, 0, _("Manufacturer:"));
346         i = 0;
347         while (varbinds[4].len_val > i)
348             mvwaddch(main_sub, 3, i + 20, *(varbinds[4].value + i++));
349
350         mvwaddstr(main_sub, 4, 0, _("Manufacturer ID:"));
351         i = 0;
352         while (varbinds[1].len_val > i)
353             mvwaddch(main_sub, 4, i + 20, *(varbinds[1].value + i++));
354
355         sprintf(message, _("Manufacturer OUI: %02X %02X %02X (%s)"),
356             varbinds[3].value[0] & 0xFF, varbinds[3].value[1] & 0xFF,
357             varbinds[3].value[2] & 0xFF,
358             oui2manufacturer(varbinds[3].value));
359         mvwaddstr(main_sub, 5, 0, message);
360
361         sprintf(message, _("Product Name:"));
362         mvwaddstr(main_sub, 6, 0, message);
363         i = 0;
364         while (varbinds[5].len_val > i)
365             mvwaddch(main_sub, 6, i + 20, *(varbinds[5].value + i++));
366
367         sprintf(message, _("Product ID:"));
368         mvwaddstr(main_sub, 7, 0, message);
369         i = 0;
370         while (varbinds[2].len_val > i)
371             mvwaddch(main_sub, 7, i + 20, *(varbinds[2].value + i++));
372
373         sprintf(message, _("Product Version:"));
374         mvwaddstr(main_sub, 8, 0, message);
375         i = 0;
376         while (varbinds[6].len_val > i)
377             mvwaddch(main_sub, 8, i + 20, *(varbinds[6].value + i++));
378
379         sprintf(message, _("PHYType: %s"), types[*(varbinds[7].value) - 1]);
380         mvwaddstr(main_sub, 9, 0, message);
381
382         sprintf(message, _("Temperature: %s"), temp[*(varbinds[8].value) - 1]);
383         mvwaddstr(main_sub, 10, 0, message);
384
385         if ((i = get_RegDomain()) == 0) {
386             print_helperr(ERR_RET);
387             getch();
388             goto quit;
389         }
390         sprintf(message, _("Regulatory Domain: %s"),
391             (i == 0x10) ? _("FCC (USA)") :
392             (i == 0x20) ? _("DOC (Canada)") :
393             (i == 0x30) ? _("ETSI (Europe)") :
394             (i == 0x31) ? _("Spain") :
395             (i == 0x32) ? _("France") :
396             (i == 0x40) ? _("MKK (Japan)") : _("unknown"));
397         mvwaddstr(main_sub, 11, 0, message);
398
399         sprintf(message, _("Transmit Power: %u mW"), *(varbinds[9].value));
400         mvwaddstr(main_sub, 12, 0, message);
401
402         varbinds[0].oid = oid_dot11PrivacyOptionImplemented;
403         varbinds[0].len_oid = sizeof(oid_dot11PrivacyOptionImplemented);
404         varbinds[0].value = oid_mac;
405         varbinds[0].type = NULL_VALUE;
406         varbinds[0].len_val = 0;
407         varbinds[1].oid = oid_dot11DiversitySupport;
408         varbinds[1].len_oid = sizeof(oid_dot11DiversitySupport);
409         varbinds[1].value = oid_mac;
410         varbinds[1].type = NULL_VALUE;
411         varbinds[1].len_val = 0;
412         varbinds[2].oid = oid_sysDescr;
413         varbinds[2].len_oid = sizeof(oid_sysDescr);
414         varbinds[2].value = oid_mac;
415         varbinds[2].type = NULL_VALUE;
416         varbinds[2].len_val = 0;
417         varbinds[3].oid = oid_sysUptime;
418         varbinds[3].len_oid = sizeof(oid_sysUptime);
419         varbinds[3].value = oid_mac;
420         varbinds[3].type = NULL_VALUE;
421         varbinds[3].len_val = 0;
422
423         if (snmp(varbinds, 4, GET) <= 0) {
424             print_helperr(ERR_RET);
425             getch();
426             goto quit;
427         }
428
429         sprintf(message, _("WEP implemented: %s"),
430             (*(varbinds[0].value) == 1) ? ON : OFF);
431         mvwaddstr(main_sub, 13, 0, message);
432         sprintf(message, _("Diversity: %s"),
433             diversity[*(varbinds[1].value) - 1]);
434         mvwaddstr(main_sub, 14, 0, message);
435         mvwaddstr(main_sub, LINES - 8, 0, SYS_DESCR);
436         i = 0;
437         while (varbinds[2].len_val > i && varbinds[2].value[i]) {
438             message[i] = varbinds[2].value[i];
439             i++;
440         }
441         message[i] = '\0';
442         mvwaddstr(main_sub, LINES - 8, strlen(SYS_DESCR) + 1, message);
443
444         time_stamp = 0;
445         i = 1;
446
447         for (c = 1; c <= varbinds[3].len_val; c++) {
448             time_stamp +=
449                 (unsigned char) varbinds[3].value[varbinds[3].len_val - c] * i;
450             i *= 256;
451         }
452
453         hand = time_stamp % 100;
454         time_stamp = time_stamp / 100;
455         sec = time_stamp % 60;
456         time_stamp = time_stamp / 60;
457         min = time_stamp % 60;
458         time_stamp = time_stamp / 60;
459
460         sprintf(message, _("Uptime: %u:%02u:%02u.%02u"), time_stamp, min, sec,
461             hand);
462         mvwaddstr(main_sub, 15, 0, message);
463
464         varbinds[0].oid = oid_ip;
465         varbinds[0].len_oid = sizeof(oid_ip);
466         varbinds[0].value = oid_mac;
467         varbinds[0].type = NULL_VALUE;
468         varbinds[0].len_val = 0;
469
470         if (snmp(varbinds, 1, GET_NEXT) <= 0) {
471             print_helperr(ERR_RET);
472             getch();
473             goto quit;
474         }
475
476         if (wait_mode == WAIT_FOREVER)
477             print_help(QT_HELP);
478
479         memcpy(&ip.s_addr, varbinds[0].value, 4);
480         sprintf(message, _("IP  Address: %s"), inet_ntoa(ip));
481         mvwaddstr(main_sub, 1, 0, message);
482         wrefresh(main_sub);
483
484         i = wait_key();
485         if (i == -1)
486             goto quit;
487
488         switch((char) i) {
489             case 'q':
490             case 'Q':
491                 goto quit;
492             case 't':
493             case 'T':
494                 wait_mode = (wait_mode == WAIT_FOREVER ?
495                     WAIT_TIMEOUT : WAIT_FOREVER);
496                 print_top(wait_mode == WAIT_TIMEOUT ? POLL_ON : POLL_OFF,
497                     SYSINFO);
498         }
499
500         /* either timeout for user input (i == 0) or invalid key => continue */
501     }
502   quit:
503     print_top(NULL, NULL);
504     clear_main(0);
505 }
506