[package] iwinfo: add initial hardware detection capabilities
[openwrt.git] / package / iwinfo / src / iwinfo_cli.c
1 /*
2  * iwinfo - Wireless Information Library - Command line frontend
3  *
4  *   Copyright (C) 2011 Jo-Philipp Wich <xm@subsignal.org>
5  *
6  * The iwinfo library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License version 2
8  * as published by the Free Software Foundation.
9  *
10  * The iwinfo library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with the iwinfo library. If not, see http://www.gnu.org/licenses/.
17  */
18
19 #include <stdio.h>
20
21 #include "iwinfo.h"
22
23
24 static char * format_bssid(unsigned char *mac)
25 {
26         static char buf[18];
27
28         snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
29                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
30
31         return buf;
32 }
33
34 static char * format_ssid(char *ssid)
35 {
36         static char buf[IWINFO_ESSID_MAX_SIZE+3];
37
38         if (ssid && ssid[0])
39                 snprintf(buf, sizeof(buf), "\"%s\"", ssid);
40         else
41                 snprintf(buf, sizeof(buf), "unknown");
42
43         return buf;
44 }
45
46 static char * format_channel(int ch)
47 {
48         static char buf[8];
49
50         if (ch <= 0)
51                 snprintf(buf, sizeof(buf), "unknown");
52         else
53                 snprintf(buf, sizeof(buf), "%d", ch);
54
55         return buf;
56 }
57
58 static char * format_frequency(int freq)
59 {
60         static char buf[10];
61
62         if (freq <= 0)
63                 snprintf(buf, sizeof(buf), "unknown");
64         else
65                 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
66
67         return buf;
68 }
69
70 static char * format_txpower(int pwr)
71 {
72         static char buf[10];
73
74         if (pwr < 0)
75                 snprintf(buf, sizeof(buf), "unknown");
76         else
77                 snprintf(buf, sizeof(buf), "%d dBm", pwr);
78
79         return buf;
80 }
81
82 static char * format_quality(int qual)
83 {
84         static char buf[8];
85
86         if (qual < 0)
87                 snprintf(buf, sizeof(buf), "unknown");
88         else
89                 snprintf(buf, sizeof(buf), "%d", qual);
90
91         return buf;
92 }
93
94 static char * format_quality_max(int qmax)
95 {
96         static char buf[8];
97
98         if (qmax < 0)
99                 snprintf(buf, sizeof(buf), "unknown");
100         else
101                 snprintf(buf, sizeof(buf), "%d", qmax);
102
103         return buf;
104 }
105
106 static char * format_signal(int sig)
107 {
108         static char buf[10];
109
110         if (!sig)
111                 snprintf(buf, sizeof(buf), "unknown");
112         else
113                 snprintf(buf, sizeof(buf), "%d dBm", sig);
114
115         return buf;
116 }
117
118 static char * format_noise(int noise)
119 {
120         static char buf[10];
121
122         if (!noise)
123                 snprintf(buf, sizeof(buf), "unknown");
124         else
125                 snprintf(buf, sizeof(buf), "%d dBm", noise);
126
127         return buf;
128 }
129
130 static char * format_rate(int rate)
131 {
132         static char buf[14];
133
134         if (rate <= 0)
135                 snprintf(buf, sizeof(buf), "unknown");
136         else
137                 snprintf(buf, sizeof(buf), "%.1f MBit/s", ((float)rate / 1000.0));
138
139         return buf;
140 }
141
142 static char * format_enc_ciphers(int ciphers)
143 {
144         static char str[128] = { 0 };
145         char *pos = str;
146
147         if (ciphers & IWINFO_CIPHER_WEP40)
148                 pos += sprintf(pos, "WEP-40, ");
149
150         if (ciphers & IWINFO_CIPHER_WEP104)
151                 pos += sprintf(pos, "WEP-104, ");
152
153         if (ciphers & IWINFO_CIPHER_TKIP)
154                 pos += sprintf(pos, "TKIP, ");
155
156         if (ciphers & IWINFO_CIPHER_CCMP)
157                 pos += sprintf(pos, "CCMP, ");
158
159         if (ciphers & IWINFO_CIPHER_WRAP)
160                 pos += sprintf(pos, "WRAP, ");
161
162         if (ciphers & IWINFO_CIPHER_AESOCB)
163                 pos += sprintf(pos, "AES-OCB, ");
164
165         if (ciphers & IWINFO_CIPHER_CKIP)
166                 pos += sprintf(pos, "CKIP, ");
167
168         if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
169                 pos += sprintf(pos, "NONE, ");
170
171         *(pos - 2) = 0;
172
173         return str;
174 }
175
176 static char * format_enc_suites(int suites)
177 {
178         static char str[64] = { 0 };
179         char *pos = str;
180
181         if (suites & IWINFO_KMGMT_PSK)
182                 pos += sprintf(pos, "PSK/");
183
184         if (suites & IWINFO_KMGMT_8021x)
185                 pos += sprintf(pos, "802.1X/");
186
187         if (!suites || (suites & IWINFO_KMGMT_NONE))
188                 pos += sprintf(pos, "NONE/");
189
190         *(pos - 1) = 0;
191
192         return str;
193 }
194
195 static char * format_encryption(struct iwinfo_crypto_entry *c)
196 {
197         static char buf[512];
198
199         if (!c)
200         {
201                 snprintf(buf, sizeof(buf), "unknown");
202         }
203         else if (c->enabled)
204         {
205                 /* WEP */
206                 if (c->auth_algs && !c->wpa_version)
207                 {
208                         if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
209                                 (c->auth_algs & IWINFO_AUTH_SHARED))
210                         {
211                                 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
212                                         format_enc_ciphers(c->pair_ciphers));
213                         }
214                         else if (c->auth_algs & IWINFO_AUTH_OPEN)
215                         {
216                                 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
217                                         format_enc_ciphers(c->pair_ciphers));
218                         }
219                         else if (c->auth_algs & IWINFO_AUTH_SHARED)
220                         {
221                                 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
222                                         format_enc_ciphers(c->pair_ciphers));
223                         }
224                 }
225
226                 /* WPA */
227                 else if (c->wpa_version)
228                 {
229                         switch (c->wpa_version) {
230                                 case 3:
231                                         snprintf(buf, sizeof(buf), "mixed WPA/WPA2 %s (%s)",
232                                                 format_enc_suites(c->auth_suites),
233                                                 format_enc_ciphers(c->pair_ciphers & c->group_ciphers));
234                                         break;
235
236                                 case 2:
237                                         snprintf(buf, sizeof(buf), "WPA2 %s (%s)",
238                                                 format_enc_suites(c->auth_suites),
239                                                 format_enc_ciphers(c->pair_ciphers & c->group_ciphers));
240                                         break;
241
242                                 case 1:
243                                         snprintf(buf, sizeof(buf), "WPA %s (%s)",
244                                                 format_enc_suites(c->auth_suites),
245                                                 format_enc_ciphers(c->pair_ciphers & c->group_ciphers));
246                                         break;
247                         }
248                 }
249                 else
250                 {
251                         snprintf(buf, sizeof(buf), "none");
252                 }
253         }
254         else
255         {
256                 snprintf(buf, sizeof(buf), "none");
257         }
258
259         return buf;
260 }
261
262 static char * format_hwmodes(int modes)
263 {
264         static char buf[12];
265
266         if (modes <= 0)
267                 snprintf(buf, sizeof(buf), "unknown");
268         else
269                 snprintf(buf, sizeof(buf), "802.11%s%s%s%s",
270                         (modes & IWINFO_80211_A) ? "a" : "",
271                         (modes & IWINFO_80211_B) ? "b" : "",
272                         (modes & IWINFO_80211_G) ? "g" : "",
273                         (modes & IWINFO_80211_N) ? "n" : "");
274
275         return buf;
276 }
277
278
279 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
280 {
281         const char *type = iwinfo_type(ifname);
282         return type ? type : "unknown";
283 }
284
285 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
286 {
287         static char buf[20];
288         struct iwinfo_hardware_id ids;
289
290         if (!iw->hardware_id(ifname, (char *)&ids))
291         {
292                 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
293                         ids.vendor_id, ids.device_id,
294                         ids.subsystem_vendor_id, ids.subsystem_device_id);
295         }
296         else
297         {
298                 snprintf(buf, sizeof(buf), "unknown");
299         }
300
301         return buf;
302 }
303
304 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
305 {
306         static char buf[128];
307
308         if (iw->hardware_name(ifname, buf))
309                 snprintf(buf, sizeof(buf), "unknown");
310
311         return buf;
312 }
313
314 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
315 {
316         char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
317
318         if (iw->ssid(ifname, buf))
319                 memset(buf, 0, sizeof(buf));
320
321         return format_ssid(buf);
322 }
323
324 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
325 {
326         static char buf[18] = { 0 };
327
328         if (iw->bssid(ifname, buf))
329                 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
330
331         return buf;
332 }
333
334 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
335 {
336         static char buf[128];
337
338         if (iw->mode(ifname, buf))
339                 snprintf(buf, sizeof(buf), "unknown");
340
341         return buf;
342 }
343
344 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
345 {
346         int ch;
347         if (iw->channel(ifname, &ch))
348                 ch = -1;
349
350         return format_channel(ch);
351 }
352
353 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
354 {
355         int freq;
356         if (iw->frequency(ifname, &freq))
357                 freq = -1;
358
359         return format_frequency(freq);
360 }
361
362 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
363 {
364         int pwr;
365         if (iw->txpower(ifname, &pwr))
366                 pwr = -1;
367
368         return format_txpower(pwr);
369 }
370
371 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
372 {
373         int qual;
374         if (iw->quality(ifname, &qual))
375                 qual = -1;
376
377         return format_quality(qual);
378 }
379
380 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
381 {
382         int qmax;
383         if (iw->quality_max(ifname, &qmax))
384                 qmax = -1;
385
386         return format_quality_max(qmax);
387 }
388
389 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
390 {
391         int sig;
392         if (iw->signal(ifname, &sig))
393                 sig = 0;
394
395         return format_signal(sig);
396 }
397
398 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
399 {
400         int noise;
401         if (iw->noise(ifname, &noise))
402                 noise = 0;
403
404         return format_noise(noise);
405 }
406
407 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
408 {
409         int rate;
410         if (iw->bitrate(ifname, &rate))
411                 rate = -1;
412
413         return format_rate(rate);
414 }
415
416 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
417 {
418         struct iwinfo_crypto_entry c = { 0 };
419         if (iw->encryption(ifname, (char *)&c))
420                 return format_encryption(NULL);
421
422         return format_encryption(&c);
423 }
424
425 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
426 {
427         int modes;
428         if (iw->hwmodelist(ifname, &modes))
429                 modes = -1;
430
431         return format_hwmodes(modes);
432 }
433
434 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
435 {
436         int supp;
437         static char buf[4];
438
439         if (iw->mbssid_support(ifname, &supp))
440                 snprintf(buf, sizeof(buf), "no");
441         else
442                 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
443
444         return buf;
445 }
446
447
448 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
449 {
450         printf("%-9s ESSID: %s\n",
451                 ifname,
452                 print_ssid(iw, ifname));
453         printf("          Access Point: %s\n",
454                 print_bssid(iw, ifname));
455         printf("          Mode: %s  Channel: %s (%s)\n",
456                 print_mode(iw, ifname),
457                 print_channel(iw, ifname),
458                 print_frequency(iw, ifname));
459         printf("          Tx-Power: %s  Link Quality: %s/%s\n",
460                 print_txpower(iw, ifname),
461                 print_quality(iw, ifname),
462                 print_quality_max(iw, ifname));
463         printf("          Signal: %s  Noise: %s\n",
464                 print_signal(iw, ifname),
465                 print_noise(iw, ifname));
466         printf("          Bit Rate: %s\n",
467                 print_rate(iw, ifname));
468         printf("          Encryption: %s\n",
469                 print_encryption(iw, ifname));
470         printf("          Type: %s  HW Mode(s): %s\n",
471                 print_type(iw, ifname),
472                 print_hwmodes(iw, ifname));
473         printf("          Hardware: %s [%s]\n",
474                 print_hardware_id(iw, ifname),
475                 print_hardware_name(iw, ifname));
476         printf("          Supports VAPs: %s\n",
477                 print_mbssid_supp(iw, ifname));
478 }
479
480
481 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
482 {
483         int i, x, len;
484         char buf[IWINFO_BUFSIZE];
485         struct iwinfo_scanlist_entry *e;
486
487         if (iw->scanlist(ifname, buf, &len))
488         {
489                 printf("Scanning not possible\n\n");
490                 return;
491         }
492         else if (len <= 0)
493         {
494                 printf("No scan results\n\n");
495                 return;
496         }
497
498         for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
499         {
500                 e = (struct iwinfo_scanlist_entry *) &buf[i];
501
502                 printf("Cell %02d - Address: %s\n",
503                         x,
504                         format_bssid(e->mac));
505                 printf("          ESSID: %s\n",
506                         format_ssid(e->ssid));
507                 printf("          Mode: %s  Channel: %s\n",
508                         e->mode ? (char *)e->mode : "unknown",
509                         format_channel(e->channel));
510                 printf("          Signal: %s  Quality: %s/%s\n",
511                         format_signal(e->signal - 0x100),
512                         format_quality(e->quality),
513                         format_quality_max(e->quality_max));
514                 printf("          Encryption: %s\n\n",
515                         format_encryption(&e->crypto));
516         }
517 }
518
519
520 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
521 {
522         int len, pwr, i;
523         char buf[IWINFO_BUFSIZE];
524         struct iwinfo_txpwrlist_entry *e;
525
526         if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
527         {
528                 printf("No TX power information available\n");
529                 return;
530         }
531
532         if (iw->txpower(ifname, &pwr))
533                 pwr = -1;
534
535         for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
536         {
537                 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
538
539                 printf("%s%3d dBm (%4d mW)\n",
540                         (pwr == e->dbm) ? "*" : " ",
541                         e->dbm,
542                         e->mw);
543         }
544 }
545
546
547 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
548 {
549         int i, len, ch;
550         char buf[IWINFO_BUFSIZE];
551         struct iwinfo_freqlist_entry *e;
552
553         if (iw->freqlist(ifname, buf, &len) || len <= 0)
554         {
555                 printf("No frequency information available\n");
556                 return;
557         }
558
559         if (iw->channel(ifname, &ch))
560                 ch = -1;
561
562         for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
563         {
564                 e = (struct iwinfo_freqlist_entry *) &buf[i];
565
566                 printf("%s %s (Channel %s)%s\n",
567                         (ch == e->channel) ? "*" : " ",
568                         format_frequency(e->mhz),
569                         format_channel(e->channel),
570                         e->restricted ? " [restricted]" : "");
571         }
572 }
573
574
575 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
576 {
577         int i, len;
578         char buf[IWINFO_BUFSIZE];
579         struct iwinfo_assoclist_entry *e;
580
581         if (iw->assoclist(ifname, buf, &len))
582         {
583                 printf("No information available\n");
584                 return;
585         }
586         else if (len <= 0)
587         {
588                 printf("No station connected\n");
589                 return;
590         }
591
592         for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
593         {
594                 e = (struct iwinfo_assoclist_entry *) &buf[i];
595
596                 printf("%s  %s / %s (SNR %d)\n",
597                         format_bssid(e->mac),
598                         format_signal(e->signal),
599                         format_noise(e->noise),
600                         (e->signal - e->noise));
601         }
602 }
603
604
605 static char * lookup_country(char *buf, int len, int iso3166)
606 {
607         int i;
608         struct iwinfo_country_entry *c;
609
610         for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
611         {
612                 c = (struct iwinfo_country_entry *) &buf[i];
613
614                 if (c->iso3166 == iso3166)
615                         return c->ccode;
616         }
617
618         return NULL;
619 }
620
621 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
622 {
623         int len;
624         char buf[IWINFO_BUFSIZE];
625         char *ccode;
626         char curcode[3];
627         const struct iwinfo_iso3166_label *l;
628
629         if (iw->countrylist(ifname, buf, &len))
630         {
631                 printf("No country code information available\n");
632                 return;
633         }
634
635         if (iw->country(ifname, curcode))
636                 memset(curcode, 0, sizeof(curcode));
637
638         for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
639         {
640                 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
641                 {
642                         printf("%s %4s  %c%c\n",
643                                 strncmp(ccode, curcode, 2) ? " " : "*",
644                                 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
645                 }
646         }
647 }
648
649
650 int main(int argc, char **argv)
651 {
652         int i;
653         const struct iwinfo_ops *iw;
654
655         if (argc < 3)
656         {
657                 fprintf(stderr,
658                         "Usage:\n"
659                         "       iwinfo <device> info\n"
660                         "       iwinfo <device> scan\n"
661                         "       iwinfo <device> txpowerlist\n"
662                         "       iwinfo <device> freqlist\n"
663                         "       iwinfo <device> assoclist\n"
664                         "       iwinfo <device> countrylist\n"
665                 );
666
667                 return 1;
668         }
669
670         iw = iwinfo_backend(argv[1]);
671
672         if (!iw)
673         {
674                 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
675                 return 1;
676         }
677
678         for (i = 2; i < argc; i++)
679         {
680                 switch(argv[i][0])
681                 {
682                 case 'i':
683                         print_info(iw, argv[1]);
684                         break;
685
686                 case 's':
687                         print_scanlist(iw, argv[1]);
688                         break;
689
690                 case 't':
691                         print_txpwrlist(iw, argv[1]);
692                         break;
693
694                 case 'f':
695                         print_freqlist(iw, argv[1]);
696                         break;
697
698                 case 'a':
699                         print_assoclist(iw, argv[1]);
700                         break;
701
702                 case 'c':
703                         print_countrylist(iw, argv[1]);
704                         break;
705
706                 default:
707                         fprintf(stderr, "Unknown command: %s\n", argv[i]);
708                         return 1;
709                 }
710         }
711
712         iwinfo_finish();
713
714         return 0;
715 }