b43c52e4f83efe4c905e527a04594e41306b7779
[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 snprintf(buf, sizeof(buf), "%04X:%04X %04X:%04X",
339 ids.vendor_id, ids.device_id,
340 ids.subsystem_vendor_id, ids.subsystem_device_id);
341 }
342 else
343 {
344 snprintf(buf, sizeof(buf), "unknown");
345 }
346
347 return buf;
348 }
349
350 static char * print_hardware_name(const struct iwinfo_ops *iw, const char *ifname)
351 {
352 static char buf[128];
353
354 if (iw->hardware_name(ifname, buf))
355 snprintf(buf, sizeof(buf), "unknown");
356
357 return buf;
358 }
359
360 static char * print_txpower_offset(const struct iwinfo_ops *iw, const char *ifname)
361 {
362 int off;
363 static char buf[12];
364
365 if (iw->txpower_offset(ifname, &off))
366 snprintf(buf, sizeof(buf), "unknown");
367 else if (off != 0)
368 snprintf(buf, sizeof(buf), "%d dB", off);
369 else
370 snprintf(buf, sizeof(buf), "none");
371
372 return buf;
373 }
374
375 static char * print_frequency_offset(const struct iwinfo_ops *iw, const char *ifname)
376 {
377 int off;
378 static char buf[12];
379
380 if (iw->frequency_offset(ifname, &off))
381 snprintf(buf, sizeof(buf), "unknown");
382 else if (off != 0)
383 snprintf(buf, sizeof(buf), "%.3f GHz", ((float)off / 1000.0));
384 else
385 snprintf(buf, sizeof(buf), "none");
386
387 return buf;
388 }
389
390 static char * print_ssid(const struct iwinfo_ops *iw, const char *ifname)
391 {
392 char buf[IWINFO_ESSID_MAX_SIZE+1] = { 0 };
393
394 if (iw->ssid(ifname, buf))
395 memset(buf, 0, sizeof(buf));
396
397 return format_ssid(buf);
398 }
399
400 static char * print_bssid(const struct iwinfo_ops *iw, const char *ifname)
401 {
402 static char buf[18] = { 0 };
403
404 if (iw->bssid(ifname, buf))
405 snprintf(buf, sizeof(buf), "00:00:00:00:00:00");
406
407 return buf;
408 }
409
410 static char * print_mode(const struct iwinfo_ops *iw, const char *ifname)
411 {
412 int mode;
413 static char buf[128];
414
415 if (iw->mode(ifname, &mode))
416 mode = IWINFO_OPMODE_UNKNOWN;
417
418 snprintf(buf, sizeof(buf), "%s", IWINFO_OPMODE_NAMES[mode]);
419
420 return buf;
421 }
422
423 static char * print_channel(const struct iwinfo_ops *iw, const char *ifname)
424 {
425 int ch;
426 if (iw->channel(ifname, &ch))
427 ch = -1;
428
429 return format_channel(ch);
430 }
431
432 static char * print_center_chan1(const struct iwinfo_ops *iw, const char *ifname)
433 {
434 int ch;
435 if (iw->center_chan1(ifname, &ch))
436 ch = -1;
437
438 return format_channel(ch);
439 }
440
441 static char * print_center_chan2(const struct iwinfo_ops *iw, const char *ifname)
442 {
443 int ch;
444 if (iw->center_chan2(ifname, &ch))
445 ch = -1;
446
447 return format_channel(ch);
448 }
449
450 static char * print_frequency(const struct iwinfo_ops *iw, const char *ifname)
451 {
452 int freq;
453 if (iw->frequency(ifname, &freq))
454 freq = -1;
455
456 return format_frequency(freq);
457 }
458
459 static char * print_txpower(const struct iwinfo_ops *iw, const char *ifname)
460 {
461 int pwr, off;
462 if (iw->txpower_offset(ifname, &off))
463 off = 0;
464
465 if (iw->txpower(ifname, &pwr))
466 pwr = -1;
467 else
468 pwr += off;
469
470 return format_txpower(pwr);
471 }
472
473 static char * print_quality(const struct iwinfo_ops *iw, const char *ifname)
474 {
475 int qual;
476 if (iw->quality(ifname, &qual))
477 qual = -1;
478
479 return format_quality(qual);
480 }
481
482 static char * print_quality_max(const struct iwinfo_ops *iw, const char *ifname)
483 {
484 int qmax;
485 if (iw->quality_max(ifname, &qmax))
486 qmax = -1;
487
488 return format_quality_max(qmax);
489 }
490
491 static char * print_signal(const struct iwinfo_ops *iw, const char *ifname)
492 {
493 int sig;
494 if (iw->signal(ifname, &sig))
495 sig = 0;
496
497 return format_signal(sig);
498 }
499
500 static char * print_noise(const struct iwinfo_ops *iw, const char *ifname)
501 {
502 int noise;
503 if (iw->noise(ifname, &noise))
504 noise = 0;
505
506 return format_noise(noise);
507 }
508
509 static char * print_rate(const struct iwinfo_ops *iw, const char *ifname)
510 {
511 int rate;
512 if (iw->bitrate(ifname, &rate))
513 rate = -1;
514
515 return format_rate(rate);
516 }
517
518 static char * print_encryption(const struct iwinfo_ops *iw, const char *ifname)
519 {
520 struct iwinfo_crypto_entry c = { 0 };
521 if (iw->encryption(ifname, (char *)&c))
522 return format_encryption(NULL);
523
524 return format_encryption(&c);
525 }
526
527 static char * print_hwmodes(const struct iwinfo_ops *iw, const char *ifname)
528 {
529 int modes;
530 if (iw->hwmodelist(ifname, &modes))
531 modes = -1;
532
533 return format_hwmodes(modes);
534 }
535
536 static char * print_mbssid_supp(const struct iwinfo_ops *iw, const char *ifname)
537 {
538 int supp;
539 static char buf[4];
540
541 if (iw->mbssid_support(ifname, &supp))
542 snprintf(buf, sizeof(buf), "no");
543 else
544 snprintf(buf, sizeof(buf), "%s", supp ? "yes" : "no");
545
546 return buf;
547 }
548
549 static char * print_phyname(const struct iwinfo_ops *iw, const char *ifname)
550 {
551 static char buf[32];
552
553 if (!iw->phyname(ifname, buf))
554 return buf;
555
556 return "?";
557 }
558
559
560 static void print_info(const struct iwinfo_ops *iw, const char *ifname)
561 {
562 printf("%-9s ESSID: %s\n",
563 ifname,
564 print_ssid(iw, ifname));
565 printf(" Access Point: %s\n",
566 print_bssid(iw, ifname));
567 printf(" Mode: %s Channel: %s (%s)\n",
568 print_mode(iw, ifname),
569 print_channel(iw, ifname),
570 print_frequency(iw, ifname));
571 if (iw->center_chan1 != NULL) {
572 printf(" Center Channel 1: %s",
573 print_center_chan1(iw, ifname));
574 printf(" 2: %s\n", print_center_chan2(iw, ifname));
575 }
576 printf(" Tx-Power: %s Link Quality: %s/%s\n",
577 print_txpower(iw, ifname),
578 print_quality(iw, ifname),
579 print_quality_max(iw, ifname));
580 printf(" Signal: %s Noise: %s\n",
581 print_signal(iw, ifname),
582 print_noise(iw, ifname));
583 printf(" Bit Rate: %s\n",
584 print_rate(iw, ifname));
585 printf(" Encryption: %s\n",
586 print_encryption(iw, ifname));
587 printf(" Type: %s HW Mode(s): %s\n",
588 print_type(iw, ifname),
589 print_hwmodes(iw, ifname));
590 printf(" Hardware: %s [%s]\n",
591 print_hardware_id(iw, ifname),
592 print_hardware_name(iw, ifname));
593 printf(" TX power offset: %s\n",
594 print_txpower_offset(iw, ifname));
595 printf(" Frequency offset: %s\n",
596 print_frequency_offset(iw, ifname));
597 printf(" Supports VAPs: %s PHY name: %s\n",
598 print_mbssid_supp(iw, ifname),
599 print_phyname(iw, ifname));
600 }
601
602
603 static void print_scanlist(const struct iwinfo_ops *iw, const char *ifname)
604 {
605 int i, x, len;
606 char buf[IWINFO_BUFSIZE];
607 struct iwinfo_scanlist_entry *e;
608
609 if (iw->scanlist(ifname, buf, &len))
610 {
611 printf("Scanning not possible\n\n");
612 return;
613 }
614 else if (len <= 0)
615 {
616 printf("No scan results\n\n");
617 return;
618 }
619
620 for (i = 0, x = 1; i < len; i += sizeof(struct iwinfo_scanlist_entry), x++)
621 {
622 e = (struct iwinfo_scanlist_entry *) &buf[i];
623
624 printf("Cell %02d - Address: %s\n",
625 x,
626 format_bssid(e->mac));
627 printf(" ESSID: %s\n",
628 format_ssid(e->ssid));
629 printf(" Mode: %s Channel: %s\n",
630 IWINFO_OPMODE_NAMES[e->mode],
631 format_channel(e->channel));
632 printf(" Signal: %s Quality: %s/%s\n",
633 format_signal(e->signal - 0x100),
634 format_quality(e->quality),
635 format_quality_max(e->quality_max));
636 printf(" Encryption: %s\n",
637 format_encryption(&e->crypto));
638 printf(" HT Operation:\n");
639 printf(" Primary Channel: %d\n",
640 e->ht_chan_info.primary_chan);
641 printf(" Secondary Channel Offset: %s\n",
642 ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
643 printf(" Channel Width: %s\n",
644 format_chan_width(false, e->ht_chan_info.chan_width));
645
646 if (e->vht_chan_info.center_chan_1) {
647 printf(" VHT Operation:\n");
648 printf(" Center Frequency 1: %d\n",
649 e->vht_chan_info.center_chan_1);
650 printf(" Center Frequency 2: %d\n",
651 e->vht_chan_info.center_chan_2);
652 printf(" Channel Width: %s\n",
653 format_chan_width(true, e->vht_chan_info.chan_width));
654 }
655
656 printf("\n");
657 }
658 }
659
660
661 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
662 {
663 int len, pwr, off, i;
664 char buf[IWINFO_BUFSIZE];
665 struct iwinfo_txpwrlist_entry *e;
666
667 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
668 {
669 printf("No TX power information available\n");
670 return;
671 }
672
673 if (iw->txpower(ifname, &pwr))
674 pwr = -1;
675
676 if (iw->txpower_offset(ifname, &off))
677 off = 0;
678
679 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
680 {
681 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
682
683 printf("%s%3d dBm (%4d mW)\n",
684 (pwr == e->dbm) ? "*" : " ",
685 e->dbm + off,
686 iwinfo_dbm2mw(e->dbm + off));
687 }
688 }
689
690
691 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
692 {
693 int i, len, freq;
694 char buf[IWINFO_BUFSIZE];
695 struct iwinfo_freqlist_entry *e;
696
697 if (iw->freqlist(ifname, buf, &len) || len <= 0)
698 {
699 printf("No frequency information available\n");
700 return;
701 }
702
703 if (iw->frequency(ifname, &freq))
704 freq = -1;
705
706 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
707 {
708 e = (struct iwinfo_freqlist_entry *) &buf[i];
709
710 printf("%s %s (Channel %s)%s\n",
711 (freq == e->mhz) ? "*" : " ",
712 format_frequency(e->mhz),
713 format_channel(e->channel),
714 e->restricted ? " [restricted]" : "");
715 }
716 }
717
718
719 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
720 {
721 int i, len;
722 char buf[IWINFO_BUFSIZE];
723 struct iwinfo_assoclist_entry *e;
724
725 if (iw->assoclist(ifname, buf, &len))
726 {
727 printf("No information available\n");
728 return;
729 }
730 else if (len <= 0)
731 {
732 printf("No station connected\n");
733 return;
734 }
735
736 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
737 {
738 e = (struct iwinfo_assoclist_entry *) &buf[i];
739
740 printf("%s %s / %s (SNR %d) %d ms ago\n",
741 format_bssid(e->mac),
742 format_signal(e->signal),
743 format_noise(e->noise),
744 (e->signal - e->noise),
745 e->inactive);
746
747 printf(" RX: %-38s %8d Pkts.\n",
748 format_assocrate(&e->rx_rate),
749 e->rx_packets
750 );
751
752 printf(" TX: %-38s %8d Pkts.\n",
753 format_assocrate(&e->tx_rate),
754 e->tx_packets
755 );
756
757 printf(" expected throughput: %s\n\n",
758 format_rate(e->thr));
759 }
760 }
761
762
763 static char * lookup_country(char *buf, int len, int iso3166)
764 {
765 int i;
766 struct iwinfo_country_entry *c;
767
768 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
769 {
770 c = (struct iwinfo_country_entry *) &buf[i];
771
772 if (c->iso3166 == iso3166)
773 return c->ccode;
774 }
775
776 return NULL;
777 }
778
779 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
780 {
781 int len;
782 char buf[IWINFO_BUFSIZE];
783 char *ccode;
784 char curcode[3];
785 const struct iwinfo_iso3166_label *l;
786
787 if (iw->countrylist(ifname, buf, &len))
788 {
789 printf("No country code information available\n");
790 return;
791 }
792
793 if (iw->country(ifname, curcode))
794 memset(curcode, 0, sizeof(curcode));
795
796 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
797 {
798 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
799 {
800 printf("%s %4s %c%c\n",
801 strncmp(ccode, curcode, 2) ? " " : "*",
802 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
803 }
804 }
805 }
806
807 static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
808 {
809 int i, htmodes = 0;
810
811 if (iw->htmodelist(ifname, &htmodes))
812 {
813 printf("No HT mode information available\n");
814 return;
815 }
816
817 for (i = 0; i < ARRAY_SIZE(IWINFO_HTMODE_NAMES); i++)
818 if (htmodes & (1 << i))
819 printf("%s ", IWINFO_HTMODE_NAMES[i]);
820
821 printf("\n");
822 }
823
824 static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
825 {
826 char buf[IWINFO_BUFSIZE];
827
828 if (!iw->lookup_phy)
829 {
830 fprintf(stderr, "Not supported\n");
831 return;
832 }
833
834 if (iw->lookup_phy(section, buf))
835 {
836 fprintf(stderr, "Phy not found\n");
837 return;
838 }
839
840 printf("%s\n", buf);
841 }
842
843
844 static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
845 {
846 const char *path;
847
848 if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
849 return;
850
851 printf("%s\n", path);
852 }
853
854 int main(int argc, char **argv)
855 {
856 int i, rv = 0;
857 char *p;
858 const struct iwinfo_ops *iw;
859 glob_t globbuf;
860
861 if (argc > 1 && argc < 3)
862 {
863 fprintf(stderr,
864 "Usage:\n"
865 " iwinfo <device> info\n"
866 " iwinfo <device> scan\n"
867 " iwinfo <device> txpowerlist\n"
868 " iwinfo <device> freqlist\n"
869 " iwinfo <device> assoclist\n"
870 " iwinfo <device> countrylist\n"
871 " iwinfo <device> htmodelist\n"
872 " iwinfo <backend> phyname <section>\n"
873 );
874
875 return 1;
876 }
877
878 if (argc == 1)
879 {
880 glob("/sys/class/net/*", 0, NULL, &globbuf);
881
882 for (i = 0; i < globbuf.gl_pathc; i++)
883 {
884 p = strrchr(globbuf.gl_pathv[i], '/');
885
886 if (!p)
887 continue;
888
889 iw = iwinfo_backend(++p);
890
891 if (!iw)
892 continue;
893
894 print_info(iw, p);
895 printf("\n");
896 }
897
898 globfree(&globbuf);
899 return 0;
900 }
901
902 if (argc > 3)
903 {
904 iw = iwinfo_backend_by_name(argv[1]);
905
906 if (!iw)
907 {
908 fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
909 rv = 1;
910 }
911 else
912 {
913 if (!strcmp(argv[2], "path")) {
914 lookup_path(iw, argv[3]);
915 return 0;
916 }
917 switch (argv[2][0])
918 {
919 case 'p':
920 lookup_phy(iw, argv[3]);
921 break;
922
923 default:
924 fprintf(stderr, "Unknown command: %s\n", argv[2]);
925 rv = 1;
926 }
927 }
928 }
929 else
930 {
931 iw = iwinfo_backend(argv[1]);
932
933 if (!iw)
934 {
935 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
936 rv = 1;
937 }
938 else
939 {
940 for (i = 2; i < argc; i++)
941 {
942 switch(argv[i][0])
943 {
944 case 'i':
945 print_info(iw, argv[1]);
946 break;
947
948 case 's':
949 print_scanlist(iw, argv[1]);
950 break;
951
952 case 't':
953 print_txpwrlist(iw, argv[1]);
954 break;
955
956 case 'f':
957 print_freqlist(iw, argv[1]);
958 break;
959
960 case 'a':
961 print_assoclist(iw, argv[1]);
962 break;
963
964 case 'c':
965 print_countrylist(iw, argv[1]);
966 break;
967
968 case 'h':
969 print_htmodelist(iw, argv[1]);
970 break;
971
972 default:
973 fprintf(stderr, "Unknown command: %s\n", argv[i]);
974 rv = 1;
975 }
976 }
977 }
978 }
979
980 iwinfo_finish();
981
982 return rv;
983 }