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