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