freifunk-watchdog: copy with API change in latest uci
[project/luci.git] / contrib / package / iwinfo / src / iwinfo.lua
1 #!/usr/bin/lua
2
3 require "iwinfo"
4
5 function printf(fmt, ...)
6         print(string.format(fmt, ...))
7 end
8
9 function s(x)
10         if x == nil then
11                 return "?"
12         else
13                 return tostring(x)
14         end
15 end
16
17 function n(x)
18         if x == nil then
19                 return 0
20         else
21                 return tonumber(x)
22         end
23 end
24
25 function print_info(api, dev)
26         local iw = iwinfo[api]
27         local enc = iw.encryption(dev)
28
29         printf("%-9s Type: %s  ESSID: \"%s\"",
30                 dev, api, s(iw.ssid(dev)))
31
32         printf("          Access Point: %s",
33                 s(iw.bssid(dev)))
34
35         printf("          Mode: %s  Channel: %d (%.3f GHz)",
36                 iw.mode(dev), n(iw.channel(dev)), n(iw.frequency(dev)) / 1000)
37
38         printf("          Tx-Power: %s dBm  Link Quality: %s/%s",
39                 s(iw.txpower(dev)), s(iw.quality(dev)), s(iw.quality_max(dev)))
40
41         printf("          Signal: %s dBm  Noise: %s dBm",
42                 s(iw.signal(dev)), s(iw.noise(dev)))
43
44         printf("          Bit Rate: %.1f MBit/s",
45                 n(iw.bitrate(dev)) / 1000)
46
47         printf("          Encryption: %s",
48                 s(enc and enc.description))
49
50         print("")
51 end
52
53 function print_scan(api, dev)
54         local iw = iwinfo[api]
55         local sr = iw.scanlist(dev)
56         local si, se
57
58         if sr and #sr > 0 then
59                 for si, se in ipairs(sr) do
60                         printf("Cell %02d - Address: %s", si, se.bssid)
61                         printf("          ESSID: \"%s\"",
62                                 s(se.ssid))
63
64                         printf("          Mode: %s  Channel: %d",
65                                 s(se.mode), n(se.channel))
66
67                         printf("          Signal: %s dBm  Quality: %d/%d",
68                                 s(se.signal), n(se.quality), n(se.quality_max))
69
70                         printf("          Encryption: %s",
71                                 s(se.encryption.description))
72
73                         print("")
74                 end
75         else
76                 print("No scan results or scanning not possible")
77                 print("")
78         end
79 end
80
81 function print_txpwrlist(api, dev)
82         local iw = iwinfo[api]
83         local pl = iw.txpwrlist(dev)
84         local cp = n(iw.txpower(dev))
85         local pe
86
87         if pl and #pl > 0 then
88                 for _, pe in ipairs(pl) do
89                         printf("%s%3d dBm (%4d mW)",
90                                 (cp == pe.dbm) and "*" or " ",
91                                 n(pe.dbm), n(pe.mw))
92                 end
93         else
94                 print("No TX power information available")
95         end
96
97         print("")
98 end
99
100 function print_freqlist(api, dev)
101         local iw = iwinfo[api]
102         local fl = iw.freqlist(dev)
103         local cc = n(iw.channel(dev))
104         local fe
105
106         if fl and #fl > 0 then
107                 for _, fe in ipairs(fl) do
108                         printf("%s %.3f GHz (Channel %d)",
109                                 (cc == fe.channel) and "*" or " ",
110                                 n(fe.mhz) / 1000, n(fe.channel))
111                 end
112         else
113                 print("No frequency information available")
114         end
115
116         print("")
117 end
118
119 function print_assoclist(api, dev)
120         local iw = iwinfo[api]
121         local al = iw.assoclist(dev)
122         local ai, ae
123
124         if al and next(al) then
125                 for ai, ae in pairs(al) do
126                         printf("%s  %s dBm", ai, s(ae.signal))
127                 end
128         else
129                 print("No client connected or no information available")
130         end
131
132         print("")
133 end
134
135 function print_countrylist(api, dev)
136         local iw = iwinfo[api]
137         local cl = iw.countrylist(dev)
138         local cc = iw.country(dev)
139         local ce
140
141         if cl and #cl > 0 then
142                 for _, ce in ipairs(cl) do
143                         printf("%s %4s  %s",
144                                 (cc == ce.alpha2) and "*" or " ",
145                                 ce.ccode, ce.name)
146                 end
147         else
148                 print("No country code information available")
149         end
150
151         print("")
152 end
153
154
155 if #arg ~= 2 then
156         print("Usage:")
157         print(" iwinfo <device> info")
158         print(" iwinfo <device> scan")
159         print(" iwinfo <device> txpowerlist")
160         print(" iwinfo <device> freqlist")
161         print(" iwinfo <device> assoclist")
162         print(" iwinfo <device> countrylist")
163         os.exit(1)
164 end
165
166 local dev = arg[1]
167 local api = iwinfo.type(dev)
168 if not api then
169         print("No such wireless device: " .. dev)
170         os.exit(1)
171 end
172
173
174 if arg[2]:match("^i") then
175         print_info(api, dev)
176
177 elseif arg[2]:match("^s") then
178         print_scan(api, dev)
179
180 elseif arg[2]:match("^t") then
181         print_txpwrlist(api, dev)
182
183 elseif arg[2]:match("^f") then
184         print_freqlist(api, dev)
185
186 elseif arg[2]:match("^a") then
187         print_assoclist(api, dev)
188
189 elseif arg[2]:match("^c") then
190         print_countrylist(api, dev)
191
192 else
193         print("Unknown command: " .. arg[2])
194 end