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.network.wireless"
28 local ub = uct.bind("wireless")
31 function init(self, cursor)
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" %{ self.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.dev and iface.dev.wifi then
85 i18n.translate("a_s_if_iwmode_" .. (iface.dev.wifi.mode or "ap")),
86 iface.dev.wifi.ssid or iface.dev.wifi.bssid or "(hidden)"
93 function get_i18n(self, iface)
94 if iface.dev and iface.dev.wifi then
96 i18n.translate("a_s_if_wifinet", "Wireless Network"),
97 i18n.translate("a_s_if_iwmode_" .. (iface.dev.wifi.mode or "ap"), iface.dev.wifi.mode or "AP"),
98 iface.dev.wifi.ssid or iface.dev.wifi.bssid or "(hidden)"
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 self.id = "%s.network%d" %{ self.device, count }
204 local dev = st:get("wireless", sid, "ifname")
205 or st:get("wireless", sid, "device")
207 local wtype = dev and iwi.type(dev)
209 if dev and wtype then
210 self.winfo = iwi[wtype]
215 function network.name(self)
219 function network.ifname(self)
223 function network.get_device(self)
225 return device(self.device)
229 function network.active_mode(self)
230 local m = self.winfo and self.winfo.mode(self.wdev)
231 if m == "Master" or m == "Auto" then
233 elseif m == "Ad-Hoc" then
235 elseif m == "Client" then
245 function network.active_mode_i18n(self)
246 return i18n.translate("a_s_if_iwmode_" .. self:active_mode())
249 function network.active_ssid(self)
250 return self.winfo and self.winfo.ssid(self.wdev) or
254 function network.active_bssid(self)
255 return self.winfo and self.winfo.bssid(self.wdev) or
256 self:bssid() or "00:00:00:00:00:00"
259 function network.signal(self)
260 return self.winfo and self.winfo.signal(self.wdev) or 0
263 function network.noise(self)
264 return self.winfo and self.winfo.noise(self.wdev) or 0
267 function network.signal_level(self)
268 if self:active_bssid() ~= "00:00:00:00:00:00" then
269 local signal = self:signal()
270 local noise = self:noise()
272 if signal > 0 and noise > 0 then
273 local snr = -1 * (noise - signal)
274 return math.floor(snr / 5)
283 function network.signal_percent(self)
284 local qc = self.winfo and
285 self.winfo.quality(self.wdev) or 0
287 local qm = self.winfo and
288 self.winfo.quality_max(self.wdev) or 0
290 if qc > 0 and qm > 0 then
291 return math.floor((100 / qm) * qc)