summaryrefslogtreecommitdiffhomepage
path: root/contrib/package/iwinfo/src/iwinfo.lua
blob: 76b9e4ad1f7df1eb3e192d9fbb77eb3028a1f454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/lua

require "iwinfo"

function printf(fmt, ...)
	print(string.format(fmt, ...))
end

function s(x)
	if x == nil then
		return "?"
	else
		return tostring(x)
	end
end

function n(x)
	if x == nil then
		return 0
	else
		return tonumber(x)
	end
end

function print_info(api, dev)
	local iw = iwinfo[api]
	local enc = iw.encryption(dev)

	printf("%-9s Type: %s  ESSID: \"%s\"",
		dev, api, s(iw.ssid(dev)))

	printf("          Access Point: %s",
		s(iw.bssid(dev)))

	printf("          Mode: %s  Channel: %d (%.3f GHz)",
		iw.mode(dev), n(iw.channel(dev)), n(iw.frequency(dev)) / 1000)

	printf("          Tx-Power: %s dBm  Link Quality: %s/%s",
		s(iw.txpower(dev)), s(iw.quality(dev)), s(iw.quality_max(dev)))

	printf("          Signal: %s dBm  Noise: %s dBm",
		s(iw.signal(dev)), s(iw.noise(dev)))

	printf("          Bit Rate: %.1f MBit/s",
		n(iw.bitrate(dev)) / 1000)

	printf("          Encryption: %s",
		s(enc and enc.description))

	print("")
end

function print_scan(api, dev)
	local iw = iwinfo[api]
	local sr = iw.scanlist(dev)
	local si, se

	if sr and #sr > 0 then
		for si, se in ipairs(sr) do
			printf("Cell %02d - Address: %s", si, se.bssid)
			printf("          ESSID: \"%s\"",
				s(se.ssid))

			printf("          Mode: %s  Channel: %d",
				s(se.mode), n(se.channel))

			printf("          Signal: %s dBm  Quality: %d/%d",
				s(se.signal), n(se.quality), n(se.quality_max))

			printf("          Encryption: %s",
				s(se.encryption.description))

			print("")
		end
	else
		print("No scan results or scanning not possible")
		print("")
	end
end

function print_txpwrlist(api, dev)
	local iw = iwinfo[api]
	local pl = iw.txpwrlist(dev)
	local cp = n(iw.txpower(dev))
	local pe

	if pl and #pl > 0 then
		for _, pe in ipairs(pl) do
			printf("%s%3d dBm (%4d mW)",
				(cp == pe.dbm) and "*" or " ",
				n(pe.dbm), n(pe.mw))
		end
	else
		print("No TX power information available")
	end

	print("")
end

function print_freqlist(api, dev)
	local iw = iwinfo[api]
	local fl = iw.freqlist(dev)
	local cc = n(iw.channel(dev))
	local fe

	if fl and #fl > 0 then
		for _, fe in ipairs(fl) do
			printf("%s %.3f GHz (Channel %d)%s",
				(cc == fe.channel) and "*" or " ",
				n(fe.mhz) / 1000, n(fe.channel),
				fe.restricted and " [restricted]" or "")
		end
	else
		print("No frequency information available")
	end

	print("")
end

function print_assoclist(api, dev)
	local iw = iwinfo[api]
	local al = iw.assoclist(dev)
	local ai, ae

	if al and next(al) then
		for ai, ae in pairs(al) do
			printf("%s  %s dBm", ai, s(ae.signal))
		end
	else
		print("No client connected or no information available")
	end

	print("")
end

function print_countrylist(api, dev)
	local iw = iwinfo[api]
	local cl = iw.countrylist(dev)
	local cc = iw.country(dev)
	local ce

	if cl and #cl > 0 then
		for _, ce in ipairs(cl) do
			printf("%s %4s	%s",
				(cc == ce.alpha2) and "*" or " ",
				ce.ccode, ce.name)
		end
	else
		print("No country code information available")
	end

	print("")
end


if #arg ~= 2 then
	print("Usage:")
	print("	iwinfo <device> info")
	print("	iwinfo <device> scan")
	print("	iwinfo <device> txpowerlist")
	print("	iwinfo <device> freqlist")
	print("	iwinfo <device> assoclist")
	print("	iwinfo <device> countrylist")
	os.exit(1)
end

local dev = arg[1]
local api = iwinfo.type(dev)
if not api then
	print("No such wireless device: " .. dev)
	os.exit(1)
end


if arg[2]:match("^i") then
	print_info(api, dev)

elseif arg[2]:match("^s") then
	print_scan(api, dev)

elseif arg[2]:match("^t") then
	print_txpwrlist(api, dev)

elseif arg[2]:match("^f") then
	print_freqlist(api, dev)

elseif arg[2]:match("^a") then
	print_assoclist(api, dev)

elseif arg[2]:match("^c") then
	print_countrylist(api, dev)

else
	print("Unknown command: " .. arg[2])
end