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