Debian Codegraph @

obvious/lib/wlan.lua

raw symbols SCIP
1 --------------------------------------------
2 -- Author: Gregor Best --
3 -- Copyright 2009, 2010, 2011 Gregor Best --
4 --------------------------------------------
5
6 local tonumber = tonumber
7 local pcall = pcall
8 local io = {
9 open = io.open,
10 popen = io.popen
11 }
12 local math = {
13 floor = math.floor
14 }
15
16 local os = "unknown"
17
18 local first_wlan = nil
19
20 local function determine_os ()
21 if os ~= "unknown" then
22 return
23 end
24 local fh = io.popen("uname")
25 local ok = nil
26 ok, os = pcall(function () return fh:read():sub(1, -1) end)
27 fh:close()
28 if not ok then
29 os = "unknown"
30 end
31 return os
32 end
33
34 local function get_info_openbsd(device)
35 local repl = {
36 ["^\tieee80211: "] = "802.11:\t",
37 ["^\tinet(.?) "] = "inet%1:\t",
38 ["^\tmedia: "] = "media:\t"
39 }
40 local fh = io.popen("ifconfig " .. device)
41 local rv = ""
42
43 function string:multimatch(patterns)
44 for _, v in ipairs(patterns) do
45 if self:match(v) then
46 return true
47 end
48 end
49 return false
50 end
51
52 for line in fh:lines() do
53 for k, v in pairs(repl) do
54 line = line:gsub(k, v)
55 end
56 if line:multimatch({ "^media", "^802%.11", "^inet" }) then
57 rv = rv .. "\n" .. line
58 end
59 end
60 fh:close()
61
62 return rv:gsub("^\n", "")
63 end
64
65 local function get_data_openbsd(device)
66 local link = 0
67 local fd = io.popen("/sbin/ifconfig " .. device)
68 if not fd then return 0 end
69
70 for line in fd:lines() do
71 if line:match("ieee80211: ") then
72 link = tonumber(line:match("(%d?%d?%d)dB"))
73 break
74 end
75 end
76 fd:close()
77
78 if not link then
79 return 0
80 end
81 return link
82 end
83
84 local function find_first_wlan_openbsd()
85 local last_device = nil
86 local wlan_device = nil
87
88 local fd = io.popen("/sbin/ifconfig")
89 if not fd then
90 return
91 end
92
93 for line in fd:lines() do
94 local m = line:match("^(%w+): ")
95 if m then
96 last_device = m
97 else
98 m = line:match("%s+ieee80211: ")
99 if m and last_device then
100 wlan_device = last_device
101 break
102 end
103 end
104 end
105
106 fd:close()
107
108 return wlan_device
109 end
110
111 local function get_data_linux(device)
112 local link = 0
113 local fd = io.open("/proc/net/wireless")
114 if not fd then return end
115
116 for line in fd:lines() do
117 if line:match("^%s*"..device) then
118 link = tonumber(line:match(" (%d?%d?%d)"))
119 break
120 end
121 end
122 fd:close()
123
124 fd = io.popen("/sbin/iwconfig " .. device)
125 if fd then
126 local scale = 100
127 for line in fd:lines() do
128 if line:match("Link Quality=") then
129 scale = tonumber(line:match("=%d+/(%d+)"))
130 end
131 end
132 link = math.floor((link / scale) * 100)
133 end
134 return link
135 end
136
137 local function find_first_wlan_linux()
138 local device = nil
139 local fd = io.open("/proc/net/wireless")
140 if not fd then return end
141
142 for line in fd:lines() do
143 local m = line:match("^%s*(%w+): ")
144 if m then
145 device = m
146 break
147 end
148 end
149 fd:close()
150
151 return device
152 end
153
154 local function find_first_wlan()
155 if first_wlan then
156 return first_wlan
157 end
158
159 determine_os()
160 if os == "OpenBSD" then
161 first_wlan = find_first_wlan_openbsd()
162 else
163 first_wlan = find_first_wlan_linux()
164 end
165
166 return first_wlan
167 end
168
169 local function get_data(device)
170 determine_os()
171 if os == "OpenBSD" then
172 return get_data_openbsd(device)
173 end
174 return get_data_linux(device)
175 end
176
177 local function get_info(device)
178 determine_os()
179 if os == "OpenBSD" then
180 return get_info_openbsd(device)
181 end
182 return ""
183 end
184
185 return setmetatable({
186 find_first_wlan = first_wlan,
187 }, { __call = function (_, ...) return get_data(...) end })
188
189 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et