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