iwinfo 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #!/usr/bin/ucode
  2. 'use strict';
  3. import { find_phy } from 'wifi.utils';
  4. import * as uci from 'uci';
  5. import * as iwinfo from 'iwinfo';
  6. function print_assoclist(stations) {
  7. for (let mac, station in stations) {
  8. printf(`${station.mac} ${station.signal} dBm / ${station.noise} dBm (SNR ${station.snr}) ${station.inactive_time} ms ago\n`);
  9. for (let k in [ 'rx', 'tx' ]) {
  10. let bitrate = station[k];
  11. let flags = join(', ', bitrate.flags);
  12. printf(`\t${uc(k)}: ${bitrate.bitrate == 'unknown' ? 'unknown' : bitrate.bitrate + ' MBit/s'}`);
  13. if (length(bitrate.flags))
  14. printf(', %s', flags);
  15. printf('%10d Pkts.\n', bitrate.packets);
  16. }
  17. printf(`\texpected throughput: ${station.expected_throughput}\n\n`);
  18. }
  19. }
  20. function print_countrylist(list) {
  21. for (let k, v in list.countries)
  22. printf(`${k == list.active ? '*' : ' '} ${k} "${v}"\n`);
  23. }
  24. function print_freqlist(channels) {
  25. for (let channel in channels) {
  26. printf(`${channel.active ? '*' : ' '} ${channel.freq} GHz (Band: ${channel.band} GHz, Channel ${channel.channel})`);
  27. if (length(channel.flags))
  28. printf(` [${join(', ', channel.flags)}]`);
  29. printf('\n');
  30. }
  31. }
  32. function print_htmodelist(htmode) {
  33. printf('%s\n', join(' ', htmode));
  34. }
  35. function print_info(list) {
  36. let padding = ' ';
  37. for (let bss in list) {
  38. printf(`${bss.iface} ESSID: "${bss.ssid}"\n`);
  39. printf(`${padding}Access Point: ${bss.mac}\n`);
  40. printf(`${padding}Mode: ${bss.mode} Channel: ${bss.channel} (${bss.freq} GHz) HT Mode: ${bss.htmode}\n`);
  41. printf(`${padding}Center Channel 1: ${bss.center_freq1} 2: ${bss.center_freq2}\n`);
  42. printf(`${padding}Tx-Power: ${bss.txpower} dBm Link Quality: ${bss.quality}/70\n`);
  43. printf(`${padding}Signal: ${bss.signal} dBm Noise: ${bss.noise} dBm\n`);
  44. printf(`${padding}Bit Rate: ${bss.bitrate == 'unknown' ? 'unknown' : bss.bitrate + ' MBit/s'}\n`);
  45. printf(`${padding}Encryption: ${bss.encryption}\n`);
  46. printf(`${padding}Type: nl80211 HW Mode(s): 802.11${bss.hwmode}\n`);
  47. printf(`${padding}Hardware: ${bss.hw_type} [${bss.hw_id}]\n`);
  48. printf(`${padding}TX power offset: ${bss.power_offset}\n`);
  49. printf(`${padding}Channel offset: ${bss.channel_offset}\n`);
  50. printf(`${padding}Supports VAPs: ${bss.vaps} PHY name: ${bss.phy}\n`);
  51. if (bss.owe_transition_ifname)
  52. printf(`${padding}OWE partner: ${bss.owe_transition_ifname}\n`);
  53. printf('\n');
  54. }
  55. return 0;
  56. }
  57. function print_scan(cells) {
  58. let idx = 1;
  59. for (let cell in cells) {
  60. printf('Cell %02d - Address: %s\n', idx++, cell.bssid);
  61. printf('\t Mode: %s Frequency: %s GHz Band: %s GHz Channel: %d\n', cell.mode, cell.frequency, cell.band, cell.channel);
  62. printf('\t Signal: %d dBm Quality: %2d/70\n', cell.dbm, cell.quality);
  63. if (!length(cell.crypto.key_mgmt))
  64. printf('\t Encryption: NONE\n');
  65. else
  66. printf('\t Encryption: %s (%s)\n', join(' / ', cell.crypto.key_mgmt), join(' / ', cell.crypto.pair));
  67. if (cell.ht) {
  68. printf('\t HT Operation:\n');
  69. printf('\t\tPrimary Channel: %d\n', cell.ht.primary_channel);
  70. printf('\t\tSecondary Channel Offset: %s\n', cell.ht.secondary_chan_off);
  71. printf('\t\tChannel Width: %s\n', cell.ht.chan_width);
  72. }
  73. if (cell.vht) {
  74. printf('\t VHT Operation:\n');
  75. printf('\t\tCenter Frequency 1: %d\n', cell.vht.center_chan_1);
  76. printf('\t\tCenter Frequency 2: %s\n', cell.vht.center_chan_2);
  77. printf('\t\tChannel Width: %s\n', cell.vht.chan_width);
  78. }
  79. printf('\n');
  80. }
  81. }
  82. function print_txpowerlist(list) {
  83. for (let power in list)
  84. printf('%s %2d dbm (%4d mW)\n', power.active ? '*' : ' ', power.dbm, power.mw);
  85. }
  86. let pretty = true;
  87. if (ARGV[0] == '-j') {
  88. pretty = false;
  89. shift(ARGV);
  90. }
  91. if (!length(ARGV)) {
  92. let info = iwinfo.info();
  93. if (pretty)
  94. print_info(info);
  95. else
  96. printf('%.J\n', info);
  97. return 0;
  98. }
  99. const commands = {
  100. assoclist: [ iwinfo.assoclist, print_assoclist ],
  101. countrylist: [ iwinfo.countrylist, print_countrylist ],
  102. freqlist: [ iwinfo.freqlist, print_freqlist ],
  103. htmodelist: [ iwinfo.htmodelist, print_htmodelist ],
  104. info: [ iwinfo.info, print_info ],
  105. scan: [ iwinfo.scan, print_scan ],
  106. txpowerlist: [ iwinfo.txpowerlist, print_txpowerlist ],
  107. };
  108. if (ARGV[0] == 'nl80211' && ARGV[1] == 'phyname') {
  109. let sec_name = ARGV[2];
  110. let sec = uci.cursor(null, null, null, { strict: false }).get_all('wireless', sec_name);
  111. if (!sec || sec['.type'] != 'wifi-device') {
  112. warn(`Config section ${sec_name} not found\n`);
  113. return 1;
  114. }
  115. let name = find_phy(sec);
  116. if (!name) {
  117. warn('Phy not found\n');
  118. return 1;
  119. }
  120. print(name + '\n');
  121. return 0;
  122. }
  123. if (length(ARGV) == 2 && iwinfo.ifaces[ARGV[0]])
  124. for (let cmd, cb in commands)
  125. if (substr(cmd, 0, length(ARGV[1])) == ARGV[1]) {
  126. let ret = cb[0](ARGV[0]);
  127. if (pretty)
  128. cb[1](ret);
  129. else
  130. printf('%.J\n', ret);
  131. return 0;
  132. }
  133. switch(ARGV[0]) {
  134. case 'phy':
  135. printf('%.J\n', iwinfo.phys);
  136. return 0;
  137. case 'iface':
  138. printf('%.J\n', iwinfo.ifaces);
  139. return 0;
  140. }
  141. warn('Usage:\n' +
  142. '\tiwinfo <device> info\n' +
  143. '\tiwinfo <device> scan\n' +
  144. '\tiwinfo <device> txpowerlist\n' +
  145. '\tiwinfo <device> freqlist\n' +
  146. '\tiwinfo <device> assoclist\n' +
  147. '\tiwinfo <device> countrylist\n' +
  148. '\tiwinfo <device> htmodelist\n' +
  149. '\tiwinfo nl80211 phyname <section>\n');
  150. return 1;