4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
20 local pairs, i18n, uci, math = pairs, luci.i18n, luci.model.uci, math
22 local iwi = require "iwinfo"
23 local utl = require "luci.util"
24 local uct = require "luci.model.uci.bind"
26 module "luci.model.wireless"
28 local ub = uct.bind("wireless")
32 cursor:unload("wireless")
33 cursor:load("wireless")
36 st = uci.cursor_state()
41 ub.uci:foreach("wireless", "wifi-iface",
45 local id = "%s.network%d" %{ s.device, count }
53 local dev = st:get("wireless", s['.name'], "ifname")
54 or st:get("wireless", s['.name'], "device")
56 local wtype = dev and iwi.type(dev)
59 ifs[id].winfo = iwi[wtype]
65 function get_device(self, dev)
69 function get_network(self, id)
71 return network(ifs[id].sid)
74 for n, _ in pairs(ifs) do
75 if ifs[n].sid == id then
82 function shortname(self, iface)
83 if iface.wdev and iface.winfo then
85 i18n.translate("a_s_if_iwmode_" .. iface:active_mode(), iface.winfo.mode(iface.wdev)),
86 iface:active_ssid() or "(hidden)"
93 function get_i18n(self, iface)
94 if iface.wdev and iface.winfo then
95 return "%s: %s %q (%s)" %{
96 i18n.translate("a_s_if_wifinet", "Wireless Network"),
97 i18n.translate("a_s_if_iwmode_" .. iface:active_mode(), iface.winfo.mode(iface.wdev)),
98 iface:active_ssid() or "(hidden)", iface.wdev
101 return "%s: %q" %{ i18n.translate("a_s_if_wifinet", "Wireless Network"), iface:name() }
105 function rename_network(self, old, new)
107 for i, _ in pairs(ifs) do
108 if ifs[i].network == old then
113 ub.uci:foreach("wireless", "wifi-iface",
115 if s.network == old then
117 ub.uci:set("wireless", s['.name'], "network", new)
119 ub.uci:delete("wireless", s['.name'], "network")
125 function del_network(self, old)
126 return self:rename_network(old, nil)
129 function find_interfaces(self, iflist, brlist)
131 for iface, _ in pairs(ifs) do
132 iflist[iface] = ifs[iface]
136 function ignore_interface(self, iface)
137 if ifs and ifs[iface] then
140 return iwi.type(iface) and true or false
144 function add_interface(self, net, iface)
145 if ifs and ifs[iface] and ifs[iface].sid then
146 ub.uci:set("wireless", ifs[iface].sid, "network", net:name())
147 ifs[iface].network = net:name()
154 function del_interface(self, net, iface)
155 if ifs and ifs[iface] and ifs[iface].sid then
156 ub.uci:delete("wireless", ifs[iface].sid, "network")
164 device = ub:section("wifi-device")
165 device:property("type")
166 device:property("channel")
167 device:property("disabled")
169 function device.name(self)
173 function device.get_networks(self)
176 ub.uci:foreach("wireless", "wifi-iface",
178 if s.device == self:name() then
179 nets[#nets+1] = network(s['.name'])
187 network = ub:section("wifi-iface")
188 network:property("mode")
189 network:property("ssid")
190 network:property("bssid")
191 network:property("network")
193 function network._init(self, sid)
196 ub.uci:foreach("wireless", "wifi-iface",
199 return s['.name'] ~= sid
202 local dev = st:get("wireless", sid, "ifname")
203 or st:get("wireless", sid, "device")
206 self.id = "%s.network%d" %{ dev, count }
208 local wtype = iwi.type(dev)
209 if dev and wtype then
210 self.winfo = iwi[wtype]
216 function network.name(self)
220 function network.ifname(self)
224 function network.get_device(self)
226 return device(self.device)
230 function network.active_mode(self)
231 local m = self.winfo and self.winfo.mode(self.wdev)
232 if m == "Master" or m == "Auto" then
234 elseif m == "Ad-Hoc" then
236 elseif m == "Client" then
246 function network.active_mode_i18n(self)
247 return i18n.translate("a_s_if_iwmode_" .. self:active_mode())
250 function network.active_ssid(self)
251 return self.winfo and self.winfo.ssid(self.wdev) or
255 function network.active_bssid(self)
256 return self.winfo and self.winfo.bssid(self.wdev) or
257 self:bssid() or "00:00:00:00:00:00"
260 function network.signal(self)
261 return self.winfo and self.winfo.signal(self.wdev) or 0
264 function network.noise(self)
265 return self.winfo and self.winfo.noise(self.wdev) or 0
268 function network.signal_level(self)
269 if self:active_bssid() ~= "00:00:00:00:00:00" then
270 local signal = self:signal()
271 local noise = self:noise()
273 if signal > 0 and noise > 0 then
274 local snr = -1 * (noise - signal)
275 return math.floor(snr / 5)
284 function network.signal_percent(self)
285 local qc = self.winfo and
286 self.winfo.quality(self.wdev) or 0
288 local qm = self.winfo and
289 self.winfo.quality_max(self.wdev) or 0
291 if qc > 0 and qm > 0 then
292 return math.floor((100 / qm) * qc)