cli: fix printing the scan channel width
[project/iwinfo.git] / 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 #include <glob.h>
21
22 #include "iwinfo.h"
23
24
25 static char * format_bssid(unsigned char *mac)
26 {
27 static char buf[18];
28
29 snprintf(buf, sizeof(buf), "%02X:%02X:%02X:%02X:%02X:%02X",
30 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
31
32 return buf;
33 }
34
35 static char * format_ssid(char *ssid)
36 {
37 static char buf[IWINFO_ESSID_MAX_SIZE+3];
38
39 if (ssid && ssid[0])
40 snprintf(buf, sizeof(buf), "\"%s\"", ssid);
41 else
42 snprintf(buf, sizeof(buf), "unknown");
43
44 return buf;
45 }
46
47 static char * format_channel(int ch)
48 {
49 static char buf[16];
50
51 if (ch <= 0)
52 snprintf(buf, sizeof(buf), "unknown");
53 else
54 snprintf(buf, sizeof(buf), "%d", ch);
55
56 return buf;
57 }
58
59 static char * format_frequency(int freq)
60 {
61 static char buf[11];
62
63 if (freq <= 0)
64 snprintf(buf, sizeof(buf), "unknown");
65 else
66 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)freq / 1000.0));
67
68 return buf;
69 }
70
71 static char * format_txpower(int pwr)
72 {
73 static char buf[16];
74
75 if (pwr < 0)
76 snprintf(buf, sizeof(buf), "unknown");
77 else
78 snprintf(buf, sizeof(buf), "%d dBm", pwr);
79
80 return buf;
81 }
82
83 static char * format_quality(int qual)
84 {
85 static char buf[16];
86
87 if (qual < 0)
88 snprintf(buf, sizeof(buf), "unknown");
89 else
90 snprintf(buf, sizeof(buf), "%d", qual);
91
92 return buf;
93 }
94
95 static char * format_quality_max(int qmax)
96 {
97 static char buf[16];
98
99 if (qmax < 0)
100 snprintf(buf, sizeof(buf), "unknown");
101 else
102 snprintf(buf, sizeof(buf), "%d", qmax);
103
104 return buf;
105 }
106
107 static char * format_signal(int sig)
108 {
109 static char buf[10];
110
111 if (!sig)
112 snprintf(buf, sizeof(buf), "unknown");
113 else
114 snprintf(buf, sizeof(buf), "%d dBm", sig);
115
116 return buf;
117 }
118
119 static char * format_noise(int noise)
120 {
121 static char buf[10];
122
123 if (!noise)
124 snprintf(buf, sizeof(buf), "unknown");
125 else
126 snprintf(buf, sizeof(buf), "%d dBm", noise);
127
128 return buf;
129 }
130
131 static char * format_rate(int rate)
132 {
133 static char buf[18];
134
135 if (rate <= 0)
136 snprintf(buf, sizeof(buf), "unknown");
137 else
138 snprintf(buf, sizeof(buf), "%d.%d MBit/s",
139 rate / 1000, (rate % 1000) / 100);
140
141 return buf;
142 }
143
144 static char * format_enc_ciphers(int ciphers)
145 {
146 static char str[128] = { 0 };
147 char *pos = str;
148
149 if (ciphers & IWINFO_CIPHER_WEP40)
150 pos += sprintf(pos, "WEP-40, ");
151
152 if (ciphers & IWINFO_CIPHER_WEP104)
153 pos += sprintf(pos, "WEP-104, ");
154
155 if (ciphers & IWINFO_CIPHER_TKIP)
156 pos += sprintf(pos, "TKIP, ");
157
158 if (ciphers & IWINFO_CIPHER_CCMP)
159 pos += sprintf(pos, "CCMP, ");
160
161 if (ciphers & IWINFO_CIPHER_CCMP256)
162 pos += sprintf(pos, "CCMP-256, ");
163
164 if (ciphers & IWINFO_CIPHER_GCMP)
165 pos += sprintf(pos, "GCMP, ");
166
167 if (ciphers & IWINFO_CIPHER_GCMP256)
168 pos += sprintf(pos, "GCMP-256, ");
169
170 if (ciphers & IWINFO_CIPHER_WRAP)
171 pos += sprintf(pos, "WRAP, ");
172
173 if (ciphers & IWINFO_CIPHER_AESOCB)
174 pos += sprintf(pos, "AES-OCB, ");
175
176 if (ciphers & IWINFO_CIPHER_CKIP)
177 pos += sprintf(pos, "CKIP, ");
178
179 if (!ciphers || (ciphers & IWINFO_CIPHER_NONE))
180 pos += sprintf(pos, "NONE, ");
181
182 *(pos - 2) = 0;
183
184 return str;
185 }
186
187 static char * format_enc_suites(int suites)
188 {
189 static char str[64] = { 0 };
190 char *pos = str;
191
192 if (suites & IWINFO_KMGMT_PSK)
193 pos += sprintf(pos, "PSK/");
194
195 if (suites & IWINFO_KMGMT_8021x)
196 pos += sprintf(pos, "802.1X/");
197
198 if (suites & IWINFO_KMGMT_SAE)
199 pos += sprintf(pos, "SAE/");
200
201 if (suites & IWINFO_KMGMT_OWE)
202 pos += sprintf(pos, "OWE/");
203
204 if (!suites || (suites & IWINFO_KMGMT_NONE))
205 pos += sprintf(pos, "NONE/");
206
207 *(pos - 1) = 0;
208
209 return str;
210 }
211
212 static char * format_encryption(struct iwinfo_crypto_entry *c)
213 {
214 static char buf[512];
215 char *pos = buf;
216 int i, n;
217
218 if (!c)
219 {
220 snprintf(buf, sizeof(buf), "unknown");
221 }
222 else if (c->enabled)
223 {
224 /* WEP */
225 if (c->auth_algs && !c->wpa_version)
226 {
227 if ((c->auth_algs & IWINFO_AUTH_OPEN) &&
228 (c->auth_algs & IWINFO_AUTH_SHARED))
229 {
230 snprintf(buf, sizeof(buf), "WEP Open/Shared (%s)",
231 format_enc_ciphers(c->pair_ciphers));
232 }
233 else if (c->auth_algs & IWINFO_AUTH_OPEN)
234 {
235 snprintf(buf, sizeof(buf), "WEP Open System (%s)",
236 format_enc_ciphers(c->pair_ciphers));
237 }
238 else if (c->auth_algs & IWINFO_AUTH_SHARED)
239 {
240 snprintf(buf, sizeof(buf), "WEP Shared Auth (%s)",
241 format_enc_ciphers(c->pair_ciphers));
242 }
243 }
244
245 /* WPA */
246 else if (c->wpa_version)
247 {
248 for (i = 0, n = 0; i < 3; i++)
249 if (c->wpa_version & (1 << i))
250 n++;
251
252 if (n > 1)
253 pos += sprintf(pos, "mixed ");
254
255 for (i = 0; i < 3; i++)
256 if (c->wpa_version & (1 << i))
257 {
258 if (i)
259 pos += sprintf(pos, "WPA%d/", i + 1);
260 else
261 pos += sprintf(pos, "WPA/");
262 }
263
264 pos--;
265
266 sprintf(pos, " %s (%s)",
267 format_enc_suites(c->auth_suites),
268 format_enc_ciphers(c->pair_ciphers | c->group_ciphers));
269 }
270 else
271 {
272 snprintf(buf, sizeof(buf), "none");
273 }
274 }
275 else
276 {
277 snprintf(buf, sizeof(buf), "none");
278 }
279
280 return buf;
281 }
282
283 static char * format_hwmodes(int modes)
284 {
285 static char buf[32] = "802.11";
286
287 if (iwinfo_format_hwmodes(modes, buf + 6, sizeof(buf) - 6) < 1)
288 snprintf(buf, sizeof(buf), "unknown");
289
290 return buf;
291 }
292
293 static char * format_assocrate(struct iwinfo_rate_entry *r)
294 {
295 static char buf[80];
296 char *p = buf;
297 int l = sizeof(buf);
298
299 if (r->rate <= 0)
300 {
301 snprintf(buf, sizeof(buf), "unknown");
302 }
303 else
304 {
305 p += snprintf(p, l, "%s", format_rate(r->rate));
306 l = sizeof(buf) - (p - buf);
307
308 if (r->is_ht)
309 {
310 p += snprintf(p, l, ", MCS %d, %dMHz", r->mcs, r->mhz);
311 l = sizeof(buf) - (p - buf);
312 }
313 else if (r->is_vht)
314 {
315 p += snprintf(p, l, ", VHT-MCS %d, %dMHz", r->mcs, r->mhz);
316 l = sizeof(buf) - (p - buf);
317
318 if (r->nss)
319 {
320 p += snprintf(p, l, ", VHT-NSS %d", r->nss);
321 l = sizeof(buf) - (p - buf);
322 }
323 }
324 else if (r->is_he)
325 {
326 p += snprintf(p, l, ", HE-MCS %d, %dMHz", r->mcs, r->mhz);
327 l = sizeof(buf) - (p - buf);
328
329 p += snprintf(p, l, ", HE-NSS %d", r->nss);
330 l = sizeof(buf) - (p - buf);
331
332 p += snprintf(p, l, ", HE-GI %d", r->he_gi);
333 l = sizeof(buf) - (p - buf);
334
335 p += snprintf(p, l, ", HE-DCM %d", r->he_dcm);
336 l = sizeof(buf) - (p - buf);
337 }
338 }
339
340 return buf;
341 }
342
343 static const char* format_chan_width(bool vht, uint8_t width)
344 {
345 if (!vht && width < ARRAY_SIZE(ht_chan_width))
346 switch (ht_chan_width[width]) {
347 case 20: return "20 MHz";
348 case 2040: return "40 MHz or higher";
349 }
350
351 if (vht && width < ARRAY_SIZE(vht_chan_width))
352 switch (vht_chan_width[width]) {
353 case 40: return "20 or 40 MHz";
354 case 80: return "80 MHz";
355 case 8080: return "80+80 MHz";
356 case 160: return "160 MHz";
357 }
358
359 return "unknown";
360 }
361
362
363 static const char * print_type(const struct iwinfo_ops *iw, const char *ifname)
364 {
365 const char *type = iwinfo_type(ifname);
366 return type ? type : "unknown";
367 }
368
369 static char * print_hardware_id(const struct iwinfo_ops *iw, const char *ifname)
370 {
371 static char buf[20];
372 struct iwinfo_hardware_id ids;
373
374 if (!iw->hardware_id(ifname, (char *)&ids))
375 {
376 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
377 ids.vendor_id, ids.device_id,
378 ids.subsystem_vendor_id, ids.subsystem_device_id);
379 }
380 else
381 {
382 snprintf(buf, sizeof(buf), "unknown");
383 }
384
385 return buf;
386 }
387
388 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
389 {
390 static char buf[128];
391
392 if (iw->hardware_name(ifname, buf))
393 snprintf(buf, sizeof(buf), "unknown");
394
395 return buf;
396 }
397
398 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
399 {
400 int off;
401 static char buf[12];
402
403 if (iw->txpower_offset(ifname, &off))
404 snprintf(buf, sizeof(buf), "unknown");
405 else if (off != 0)
406 snprintf(buf, sizeof(buf), "%d dB", off);
407 else
408 snprintf(buf, sizeof(buf), "none");
409
410 return buf;
411 }
412
413 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
414 {
415 int off;
416 static char buf[12];
417
418 if (iw->frequency_offset(ifname, &off))
419 snprintf(buf, sizeof(buf), "unknown");
420 else if (off != 0)
421 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
422 else
423 snprintf(buf, sizeof(buf), "none");
424
425 return buf;
426 }
427
428 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
429 {
430 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
431
432 if (iw->ssid(ifname, buf))
433 memset(buf, 0, sizeof(buf));
434
435 return format_ssid(buf);
436 }
437
438 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
439 {
440 static char buf[18] = { 0 };
441
442 if (iw->bssid(ifname, buf))
443 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
444
445 return buf;
446 }
447
448 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
449 {
450 int mode;
451 static char buf[128];
452
453 if (iw->mode(ifname, &mode))
454 mode = IWINFO_OPMODE_UNKNOWN;
455
456 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
457
458 return buf;
459 }
460
461 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
462 {
463 int ch;
464 if (iw->channel(ifname, &ch))
465 ch = -1;
466
467 return format_channel(ch);
468 }
469
470 static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
471 {
472 int ch;
473 if (iw->center_chan1(ifname, &ch))
474 ch = -1;
475
476 return format_channel(ch);
477 }
478
479 static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
480 {
481 int ch;
482 if (iw->center_chan2(ifname, &ch))
483 ch = -1;
484
485 return format_channel(ch);
486 }
487
488 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
489 {
490 int freq;
491 if (iw->frequency(ifname, &freq))
492 freq = -1;
493
494 return format_frequency(freq);
495 }
496
497 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
498 {
499 int pwr, off;
500 if (iw->txpower_offset(ifname, &off))
501 off = 0;
502
503 if (iw->txpower(ifname, &pwr))
504 pwr = -1;
505 else
506 pwr += off;
507
508 return format_txpower(pwr);
509 }
510
511 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
512 {
513 int qual;
514 if (iw->quality(ifname, &qual))
515 qual = -1;
516
517 return format_quality(qual);
518 }
519
520 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
521 {
522 int qmax;
523 if (iw->quality_max(ifname, &qmax))
524 qmax = -1;
525
526 return format_quality_max(qmax);
527 }
528
529 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
530 {
531 int sig;
532 if (iw->signal(ifname, &sig))
533 sig = 0;
534
535 return format_signal(sig);
536 }
537
538 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
539 {
540 int noise;
541 if (iw->noise(ifname, &noise))
542 noise = 0;
543
544 return format_noise(noise);
545 }
546
547 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
548 {
549 int rate;
550 if (iw->bitrate(ifname, &rate))
551 rate = -1;
552
553 return format_rate(rate);
554 }
555
556 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
557 {
558 struct iwinfo_crypto_entry c = { 0 };
559 if (iw->encryption(ifname, (char *)&c))
560 return format_encryption(NULL);
561
562 return format_encryption(&c);
563 }
564
565 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
566 {
567 int modes;
568 if (iw->hwmodelist(ifname, &modes))
569 modes = -1;
570
571 return format_hwmodes(modes);
572 }
573
574 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
575 {
576 int supp;
577 static char buf[4];
578
579 if (iw->mbssid_support(ifname, &supp))
580 snprintf(buf, sizeof(buf), "no");
581 else
582 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
583
584 return buf;
585 }
586
587 static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
588 {
589 static char buf[32];
590
591 if (!iw->phyname(ifname, buf))
592 return buf;
593
594 return "?";
595 }
596
597
598 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
599 {
600 printf("%-9s ESSID: %s\n",
601 ifname,
602 print_ssid(iw, ifname));
603 printf(" Access Point: %s\n",
604 print_bssid(iw, ifname));
605 printf(" Mode: %s Channel: %s (%s)\n",
606 print_mode(iw, ifname),
607 print_channel(iw, ifname),
608 print_frequency(iw, ifname));
609 if (iw->center_chan1 != NULL) {
610 printf(" Center Channel 1: %s",
611 print_center_chan1(iw, ifname));
612 printf(" 2: %s\n", print_center_chan2(iw, ifname));
613 }
614 printf(" Tx-Power: %s Link Quality: %s/%s\n",
615 print_txpower(iw, ifname),
616 print_quality(iw, ifname),
617 print_quality_max(iw, ifname));
618 printf(" Signal: %s Noise: %s\n",
619 print_signal(iw, ifname),
620 print_noise(iw, ifname));
621 printf(" Bit Rate: %s\n",
622 print_rate(iw, ifname));
623 printf(" Encryption: %s\n",
624 print_encryption(iw, ifname));
625 printf(" Type: %s HW Mode(s): %s\n",
626 print_type(iw, ifname),
627 print_hwmodes(iw, ifname));
628 printf(" Hardware: %s [%s]\n",
629 print_hardware_id(iw, ifname),
630 print_hardware_name(iw, ifname));
631 printf(" TX power offset: %s\n",
632 print_txpower_offset(iw, ifname));
633 printf(" Frequency offset: %s\n",
634 print_frequency_offset(iw, ifname));
635 printf(" Supports VAPs: %s PHY name: %s\n",
636 print_mbssid_supp(iw, ifname),
637 print_phyname(iw, ifname));
638 }
639
640
641 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
642 {
643 int i, x, len;
644 char buf[IWINFO_BUFSIZE];
645 struct iwinfo_scanlist_entry *e;
646
647 if (iw->scanlist(ifname, buf, &len))
648 {
649 printf("Scanning not possible\n\n");
650 return;
651 }
652 else if (len <= 0)
653 {
654 printf("No scan results\n\n");
655 return;
656 }
657
658 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
659 {
660 e = (struct iwinfo_scanlist_entry *) &buf[i];
661
662 printf("Cell %02d - Address: %s\n",
663 x,
664 format_bssid(e->mac));
665 printf(" ESSID: %s\n",
666 format_ssid(e->ssid));
667 printf(" Mode: %s Channel: %s\n",
668 IWINFO_OPMODE_NAMES[e->mode],
669 format_channel(e->channel));
670 printf(" Signal: %s Quality: %s/%s\n",
671 format_signal(e->signal - 0x100),
672 format_quality(e->quality),
673 format_quality_max(e->quality_max));
674 printf(" Encryption: %s\n",
675 format_encryption(&e->crypto));
676 printf(" HT Operation:\n");
677 printf(" Primary Channel: %d\n",
678 e->ht_chan_info.primary_chan);
679 printf(" Secondary Channel Offset: %s\n",
680 ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
681 printf(" Channel Width: %s\n",
682 format_chan_width(false, e->ht_chan_info.chan_width));
683
684 if (e->vht_chan_info.center_chan_1) {
685 printf(" VHT Operation:\n");
686 printf(" Center Frequency 1: %d\n",
687 e->vht_chan_info.center_chan_1);
688 printf(" Center Frequency 2: %d\n",
689 e->vht_chan_info.center_chan_2);
690 printf(" Channel Width: %s\n",
691 format_chan_width(true, e->vht_chan_info.chan_width));
692 }
693
694 printf("\n");
695 }
696 }
697
698
699 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
700 {
701 int len, pwr, off, i;
702 char buf[IWINFO_BUFSIZE];
703 struct iwinfo_txpwrlist_entry *e;
704
705 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
706 {
707 printf("No TX power information available\n");
708 return;
709 }
710
711 if (iw->txpower(ifname, &pwr))
712 pwr = -1;
713
714 if (iw->txpower_offset(ifname, &off))
715 off = 0;
716
717 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
718 {
719 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
720
721 printf("%s%3d dBm (%4d mW)\n",
722 (pwr == e->dbm) ? "*" : " ",
723 e->dbm + off,
724 iwinfo_dbm2mw(e->dbm + off));
725 }
726 }
727
728
729 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
730 {
731 int i, len, freq;
732 char buf[IWINFO_BUFSIZE];
733 struct iwinfo_freqlist_entry *e;
734
735 if (iw->freqlist(ifname, buf, &len) || len <= 0)
736 {
737 printf("No frequency information available\n");
738 return;
739 }
740
741 if (iw->frequency(ifname, &freq))
742 freq = -1;
743
744 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
745 {
746 e = (struct iwinfo_freqlist_entry *) &buf[i];
747
748 printf("%s %s (Channel %s)%s\n",
749 (freq == e->mhz) ? "*" : " ",
750 format_frequency(e->mhz),
751 format_channel(e->channel),
752 e->restricted ? " [restricted]" : "");
753 }
754 }
755
756
757 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
758 {
759 int i, len;
760 char buf[IWINFO_BUFSIZE];
761 struct iwinfo_assoclist_entry *e;
762
763 if (iw->assoclist(ifname, buf, &len))
764 {
765 printf("No information available\n");
766 return;
767 }
768 else if (len <= 0)
769 {
770 printf("No station connected\n");
771 return;
772 }
773
774 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
775 {
776 e = (struct iwinfo_assoclist_entry *) &buf[i];
777
778 printf("%s %s / %s (SNR %d) %d ms ago\n",
779 format_bssid(e->mac),
780 format_signal(e->signal),
781 format_noise(e->noise),
782 (e->signal - e->noise),
783 e->inactive);
784
785 printf(" RX: %-38s %8d Pkts.\n",
786 format_assocrate(&e->rx_rate),
787 e->rx_packets
788 );
789
790 printf(" TX: %-38s %8d Pkts.\n",
791 format_assocrate(&e->tx_rate),
792 e->tx_packets
793 );
794
795 printf(" expected throughput: %s\n\n",
796 format_rate(e->thr));
797 }
798 }
799
800
801 static char * lookup_country(char *buf, int len, int iso3166)
802 {
803 int i;
804 struct iwinfo_country_entry *c;
805
806 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
807 {
808 c = (struct iwinfo_country_entry *) &buf[i];
809
810 if (c->iso3166 == iso3166)
811 return c->ccode;
812 }
813
814 return NULL;
815 }
816
817 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
818 {
819 int len;
820 char buf[IWINFO_BUFSIZE];
821 char *ccode;
822 char curcode[3];
823 const struct iwinfo_iso3166_label *l;
824
825 if (iw->countrylist(ifname, buf, &len))
826 {
827 printf("No country code information available\n");
828 return;
829 }
830
831 if (iw->country(ifname, curcode))
832 memset(curcode, 0, sizeof(curcode));
833
834 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
835 {
836 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
837 {
838 printf("%s %4s %c%c\n",
839 strncmp(ccode, curcode, 2) ? " " : "*",
840 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
841 }
842 }
843 }
844
845 static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
846 {
847 int i, htmodes = 0;
848
849 if (iw->htmodelist(ifname, &htmodes))
850 {
851 printf("No HT mode information available\n");
852 return;
853 }
854
855 for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
856 if (htmodes & (1 << i))
857 printf("%s ", IWINFO_HTMODE_NAMES[i]);
858
859 printf("\n");
860 }
861
862 static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
863 {
864 char buf[IWINFO_BUFSIZE];
865
866 if (!iw->lookup_phy)
867 {
868 fprintf(stderr, "Not supported\n");
869 return;
870 }
871
872 if (iw->lookup_phy(section, buf))
873 {
874 fprintf(stderr, "Phy not found\n");
875 return;
876 }
877
878 printf("%s\n", buf);
879 }
880
881
882 static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
883 {
884 const char *path;
885
886 if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
887 return;
888
889 printf("%s\n", path);
890 }
891
892 int main(int argc, char **argv)
893 {
894 int i, rv = 0;
895 char *p;
896 const struct iwinfo_ops *iw;
897 glob_t globbuf;
898
899 if (argc > 1 && argc < 3)
900 {
901 fprintf(stderr,
902 "Usage:\n"
903 " iwinfo <device> info\n"
904 " iwinfo <device> scan\n"
905 " iwinfo <device> txpowerlist\n"
906 " iwinfo <device> freqlist\n"
907 " iwinfo <device> assoclist\n"
908 " iwinfo <device> countrylist\n"
909 " iwinfo <device> htmodelist\n"
910 " iwinfo <backend> phyname <section>\n"
911 );
912
913 return 1;
914 }
915
916 if (argc == 1)
917 {
918 glob("/sys/class/net/*", 0, NULL, &globbuf);
919
920 for (i = 0; i < globbuf.gl_pathc; i++)
921 {
922 p = strrchr(globbuf.gl_pathv[i], '/');
923
924 if (!p)
925 continue;
926
927 iw = iwinfo_backend(++p);
928
929 if (!iw)
930 continue;
931
932 print_info(iw, p);
933 printf("\n");
934 }
935
936 globfree(&globbuf);
937 return 0;
938 }
939
940 if (argc > 3)
941 {
942 iw = iwinfo_backend_by_name(argv[1]);
943
944 if (!iw)
945 {
946 fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
947 rv = 1;
948 }
949 else
950 {
951 if (!strcmp(argv[2], "path")) {
952 lookup_path(iw, argv[3]);
953 return 0;
954 }
955 switch (argv[2][0])
956 {
957 case 'p':
958 lookup_phy(iw, argv[3]);
959 break;
960
961 default:
962 fprintf(stderr, "Unknown command: %s\n", argv[2]);
963 rv = 1;
964 }
965 }
966 }
967 else
968 {
969 iw = iwinfo_backend(argv[1]);
970
971 if (!iw)
972 {
973 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
974 rv = 1;
975 }
976 else
977 {
978 for (i = 2; i < argc; i++)
979 {
980 switch(argv[i][0])
981 {
982 case 'i':
983 print_info(iw, argv[1]);
984 break;
985
986 case 's':
987 print_scanlist(iw, argv[1]);
988 break;
989
990 case 't':
991 print_txpwrlist(iw, argv[1]);
992 break;
993
994 case 'f':
995 print_freqlist(iw, argv[1]);
996 break;
997
998 case 'a':
999 print_assoclist(iw, argv[1]);
1000 break;
1001
1002 case 'c':
1003 print_countrylist(iw, argv[1]);
1004 break;
1005
1006 case 'h':
1007 print_htmodelist(iw, argv[1]);
1008 break;
1009
1010 default:
1011 fprintf(stderr, "Unknown command: %s\n", argv[i]);
1012 rv = 1;
1013 }
1014 }
1015 }
1016 }
1017
1018 iwinfo_finish();
1019
1020 return rv;
1021 }