nl80211: fix some comments
[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 Frequency: %s Band: %s Channel: %s\n",
663 IWINFO_OPMODE_NAMES[e->mode],
664 format_frequency(e->mhz),
665 format_band(e->band),
666 format_channel(e->channel));
667 printf(" Signal: %s Quality: %s/%s\n",
668 format_signal(e->signal - 0x100),
669 format_quality(e->quality),
670 format_quality_max(e->quality_max));
671 printf(" Encryption: %s\n",
672 format_encryption(&e->crypto));
673 printf(" HT Operation:\n");
674 printf(" Primary Channel: %d\n",
675 e->ht_chan_info.primary_chan);
676 printf(" Secondary Channel Offset: %s\n",
677 ht_secondary_offset[e->ht_chan_info.secondary_chan_off]);
678 printf(" Channel Width: %s\n",
679 format_chan_width(false, e->ht_chan_info.chan_width));
680
681 if (e->vht_chan_info.center_chan_1) {
682 printf(" VHT Operation:\n");
683 printf(" Center Frequency 1: %d\n",
684 e->vht_chan_info.center_chan_1);
685 printf(" Center Frequency 2: %d\n",
686 e->vht_chan_info.center_chan_2);
687 printf(" Channel Width: %s\n",
688 format_chan_width(true, e->vht_chan_info.chan_width));
689 }
690
691 printf("\n");
692 }
693 }
694
695
696 static void print_txpwrlist(const struct iwinfo_ops *iw, const char *ifname)
697 {
698 int len, pwr, off, i;
699 char buf[IWINFO_BUFSIZE];
700 struct iwinfo_txpwrlist_entry *e;
701
702 if (iw->txpwrlist(ifname, buf, &len) || len <= 0)
703 {
704 printf("No TX power information available\n");
705 return;
706 }
707
708 if (iw->txpower(ifname, &pwr))
709 pwr = -1;
710
711 if (iw->txpower_offset(ifname, &off))
712 off = 0;
713
714 for (i = 0; i < len; i += sizeof(struct iwinfo_txpwrlist_entry))
715 {
716 e = (struct iwinfo_txpwrlist_entry *) &buf[i];
717
718 printf("%s%3d dBm (%4d mW)\n",
719 (pwr == e->dbm) ? "*" : " ",
720 e->dbm + off,
721 iwinfo_dbm2mw(e->dbm + off));
722 }
723 }
724
725
726 static void print_freqlist(const struct iwinfo_ops *iw, const char *ifname)
727 {
728 int i, len, freq;
729 char buf[IWINFO_BUFSIZE];
730 struct iwinfo_freqlist_entry *e;
731
732 if (iw->freqlist(ifname, buf, &len) || len <= 0)
733 {
734 printf("No frequency information available\n");
735 return;
736 }
737
738 if (iw->frequency(ifname, &freq))
739 freq = -1;
740
741 for (i = 0; i < len; i += sizeof(struct iwinfo_freqlist_entry))
742 {
743 e = (struct iwinfo_freqlist_entry *) &buf[i];
744
745 printf("%s %s (Band: %s, Channel %s)%s\n",
746 (freq == e->mhz) ? "*" : " ",
747 format_frequency(e->mhz),
748 format_band(e->band),
749 format_channel(e->channel),
750 e->restricted ? " [restricted]" : "");
751 }
752 }
753
754
755 static void print_assoclist(const struct iwinfo_ops *iw, const char *ifname)
756 {
757 int i, len;
758 char buf[IWINFO_BUFSIZE];
759 struct iwinfo_assoclist_entry *e;
760
761 if (iw->assoclist(ifname, buf, &len))
762 {
763 printf("No information available\n");
764 return;
765 }
766 else if (len <= 0)
767 {
768 printf("No station connected\n");
769 return;
770 }
771
772 for (i = 0; i < len; i += sizeof(struct iwinfo_assoclist_entry))
773 {
774 e = (struct iwinfo_assoclist_entry *) &buf[i];
775
776 printf("%s %s / %s (SNR %d) %d ms ago\n",
777 format_bssid(e->mac),
778 format_signal(e->signal),
779 format_noise(e->noise),
780 (e->signal - e->noise),
781 e->inactive);
782
783 printf(" RX: %-38s %8d Pkts.\n",
784 format_assocrate(&e->rx_rate),
785 e->rx_packets
786 );
787
788 printf(" TX: %-38s %8d Pkts.\n",
789 format_assocrate(&e->tx_rate),
790 e->tx_packets
791 );
792
793 printf(" expected throughput: %s\n\n",
794 format_rate(e->thr));
795 }
796 }
797
798
799 static char * lookup_country(char *buf, int len, int iso3166)
800 {
801 int i;
802 struct iwinfo_country_entry *c;
803
804 for (i = 0; i < len; i += sizeof(struct iwinfo_country_entry))
805 {
806 c = (struct iwinfo_country_entry *) &buf[i];
807
808 if (c->iso3166 == iso3166)
809 return c->ccode;
810 }
811
812 return NULL;
813 }
814
815 static void print_countrylist(const struct iwinfo_ops *iw, const char *ifname)
816 {
817 int len;
818 char buf[IWINFO_BUFSIZE];
819 char *ccode;
820 char curcode[3];
821 const struct iwinfo_iso3166_label *l;
822
823 if (iw->countrylist(ifname, buf, &len))
824 {
825 printf("No country code information available\n");
826 return;
827 }
828
829 if (iw->country(ifname, curcode))
830 memset(curcode, 0, sizeof(curcode));
831
832 for (l = IWINFO_ISO3166_NAMES; l->iso3166; l++)
833 {
834 if ((ccode = lookup_country(buf, len, l->iso3166)) != NULL)
835 {
836 printf("%s %4s %c%c\n",
837 strncmp(ccode, curcode, 2) ? " " : "*",
838 ccode, (l->iso3166 / 256), (l->iso3166 % 256));
839 }
840 }
841 }
842
843 static void print_htmodelist(const struct iwinfo_ops *iw, const char *ifname)
844 {
845 int i, htmodes = 0;
846
847 if (iw->htmodelist(ifname, &htmodes))
848 {
849 printf("No HT mode information available\n");
850 return;
851 }
852
853 for (i = 0; i < IWINFO_HTMODE_COUNT; i++)
854 if (htmodes & (1 << i))
855 printf("%s ", IWINFO_HTMODE_NAMES[i]);
856
857 printf("\n");
858 }
859
860 static void lookup_phy(const struct iwinfo_ops *iw, const char *section)
861 {
862 char buf[IWINFO_BUFSIZE];
863
864 if (!iw->lookup_phy)
865 {
866 fprintf(stderr, "Not supported\n");
867 return;
868 }
869
870 if (iw->lookup_phy(section, buf))
871 {
872 fprintf(stderr, "Phy not found\n");
873 return;
874 }
875
876 printf("%s\n", buf);
877 }
878
879
880 static void lookup_path(const struct iwinfo_ops *iw, const char *phy)
881 {
882 const char *path;
883
884 if (!iw->phy_path || iw->phy_path(phy, &path) || !path)
885 return;
886
887 printf("%s\n", path);
888 }
889
890 int main(int argc, char **argv)
891 {
892 int i, rv = 0;
893 char *p;
894 const struct iwinfo_ops *iw;
895 glob_t globbuf;
896
897 if (argc > 1 && argc < 3)
898 {
899 fprintf(stderr,
900 "Usage:\n"
901 " iwinfo <device> info\n"
902 " iwinfo <device> scan\n"
903 " iwinfo <device> txpowerlist\n"
904 " iwinfo <device> freqlist\n"
905 " iwinfo <device> assoclist\n"
906 " iwinfo <device> countrylist\n"
907 " iwinfo <device> htmodelist\n"
908 " iwinfo <backend> phyname <section>\n"
909 );
910
911 return 1;
912 }
913
914 if (argc == 1)
915 {
916 glob("/sys/class/net/*", 0, NULL, &globbuf);
917
918 for (i = 0; i < globbuf.gl_pathc; i++)
919 {
920 p = strrchr(globbuf.gl_pathv[i], '/');
921
922 if (!p)
923 continue;
924
925 iw = iwinfo_backend(++p);
926
927 if (!iw)
928 continue;
929
930 print_info(iw, p);
931 printf("\n");
932 }
933
934 globfree(&globbuf);
935 return 0;
936 }
937
938 if (argc > 3)
939 {
940 iw = iwinfo_backend_by_name(argv[1]);
941
942 if (!iw)
943 {
944 fprintf(stderr, "No such wireless backend: %s\n", argv[1]);
945 rv = 1;
946 }
947 else
948 {
949 if (!strcmp(argv[2], "path")) {
950 lookup_path(iw, argv[3]);
951 return 0;
952 }
953 switch (argv[2][0])
954 {
955 case 'p':
956 lookup_phy(iw, argv[3]);
957 break;
958
959 default:
960 fprintf(stderr, "Unknown command: %s\n", argv[2]);
961 rv = 1;
962 }
963 }
964 }
965 else
966 {
967 iw = iwinfo_backend(argv[1]);
968
969 if (!iw)
970 {
971 fprintf(stderr, "No such wireless device: %s\n", argv[1]);
972 rv = 1;
973 }
974 else
975 {
976 for (i = 2; i < argc; i++)
977 {
978 switch(argv[i][0])
979 {
980 case 'i':
981 print_info(iw, argv[1]);
982 break;
983
984 case 's':
985 print_scanlist(iw, argv[1]);
986 break;
987
988 case 't':
989 print_txpwrlist(iw, argv[1]);
990 break;
991
992 case 'f':
993 print_freqlist(iw, argv[1]);
994 break;
995
996 case 'a':
997 print_assoclist(iw, argv[1]);
998 break;
999
1000 case 'c':
1001 print_countrylist(iw, argv[1]);
1002 break;
1003
1004 case 'h':
1005 print_htmodelist(iw, argv[1]);
1006 break;
1007
1008 default:
1009 fprintf(stderr, "Unknown command: %s\n", argv[i]);
1010 rv = 1;
1011 }
1012 }
1013 }
1014 }
1015
1016 iwinfo_finish();
1017
1018 return rv;
1019 }