1 -- Copyright 2018 Jo-Philipp Wich <jo@mein.io>
2 -- Licensed to the public under the Apache License 2.0.
4 local fs = require "nixio.fs"
5 local utl = require "luci.util"
6 local tpl = require "luci.template"
7 local ntm = require "luci.model.network"
9 local has_iwinfo = pcall(require, "iwinfo")
11 function guess_wifi_hw(dev)
13 local ifname = dev:name()
14 local name, idx = ifname:match("^([a-z]+)(%d+)")
18 local bl = dev.iwinfo.hwmodelist
19 if bl and next(bl) then
20 if bl.a then bands = bands .. "a" end
21 if bl.b then bands = bands .. "b" end
22 if bl.g then bands = bands .. "g" end
23 if bl.n then bands = bands .. "n" end
24 if bl.ac then bands = bands .. "ac" end
27 local hw = dev.iwinfo.hardware_name
29 return "%s 802.11%s" %{ hw, bands }
35 local name = translatef("Broadcom 802.11%s Wireless Controller", bands)
38 local fd = nixio.open("/proc/bus/pci/devices", "r")
41 for ln in fd:linesource() do
42 if ln:match("wl$") then
44 local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
46 "Broadcom BCM%04x 802.11 Wireless Controller",
63 return translatef("Generic 802.11%s Wireless Controller", bands)
67 local tpl_radio = tpl.Template(nil, [[
68 <div class="cbi-section-node">
70 <!-- physical device -->
72 <div class="td col-2 center">
73 <span class="ifacebadge"><img src="<%=resource%>/icons/wifi_toggled.png" id="<%=dev:name()%>-iw-upstate" /> <%=dev:name()%></span>
75 <div class="td col-7 left">
76 <big><strong><%=hw%></strong></big><br />
77 <span id="<%=dev:name()%>-iw-devinfo"></span>
79 <div class="td cbi-section-actions">
80 <input type="button" class="cbi-button cbi-button-neutral" title="<%:Restart radio interface%>" value="<%:Restart%>" onclick="wifi_restart(event)" data-radio="<%=dev:name()%>" />
82 <form action="<%=url('admin/network/wireless_join')%>" method="post" class="inline">
83 <input type="hidden" name="device" value="<%=dev:name()%>" />
84 <input type="hidden" name="token" value="<%=token%>" />
85 <input type="submit" class="cbi-button cbi-button-action important" title="<%:Find and join network%>" value="<%:Scan%>" />
87 <form action="<%=url('admin/network/wireless_add')%>" method="post" class="inline">
88 <input type="hidden" name="device" value="<%=dev:name()%>" />
89 <input type="hidden" name="token" value="<%=token%>" />
90 <input type="submit" class="cbi-button cbi-button-add" title="<%:Provide new network%>" value="<%:Add%>" />
94 <!-- /physical device -->
97 <% if #wnets > 0 then %>
98 <% for i, net in ipairs(wnets) do local disabled = (dev:get("disabled") == "1" or net:get("disabled") == "1") %>
99 <div class="tr cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
100 <div class="td col-2 center" id="<%=net:id()%>-iw-signal">
101 <span class="ifacebadge" title="<%:Not associated%>"><img src="<%=resource%>/icons/signal-<%= disabled and "none" or "0" %>.png" /> 0%</span>
103 <div class="td col-7 left" id="<%=net:id()%>-iw-status" data-network="<%=net:id()%>" data-disabled="<%= disabled and "true" or "false" %>">
104 <em><%= disabled and translate("Wireless is disabled") or translate("Collecting data...") %></em>
106 <div class="td cbi-section-actions">
107 <% if disabled then %>
108 <input name="cbid.wireless.<%=net:name()%>.__disable__" type="hidden" value="1" />
109 <input name="cbi.apply" type="submit" class="cbi-button cbi-button-neutral" title="<%:Enable this network%>" value="<%:Enable%>" onclick="this.previousElementSibling.value='0'" />
111 <input name="cbid.wireless.<%=net:name()%>.__disable__" type="hidden" value="0" />
112 <input name="cbi.apply" type="submit" class="cbi-button cbi-button-neutral" title="<%:Disable this network%>" value="<%:Disable%>" onclick="this.previousElementSibling.value='1'" />
115 <input type="button" class="cbi-button cbi-button-action important" onclick="location.href='<%=net:adminlink()%>'" title="<%:Edit this network%>" value="<%:Edit%>" />
117 <input name="cbid.wireless.<%=net:name()%>.__delete__" type="hidden" value="" />
118 <input name="cbi.apply" type="submit" class="cbi-button cbi-button-negative" title="<%:Delete this network%>" value="<%:Remove%>" onclick="wifi_delete(event)" />
123 <div class="tr cbi-rowstyle-2">
124 <div class="td left">
125 <em><%:No network configured on this device%></em>
129 <!-- /network list -->
135 m = Map("wireless", translate("Wireless Overview"))
139 if not has_iwinfo then
140 s = m:section(NamedSection, "__warning__")
142 function s.render(self)
144 <div class="alert-message warning">
145 <h4><%:Package libiwinfo required!%></h4>
146 <p><%_The <em>libiwinfo-lua</em> package is not installed. You must install this component for working wireless configuration!%></p>
153 for _, dev in ipairs(ntm:get_wifidevs()) do
154 s = m:section(TypedSection)
155 s.wnets = dev:get_wifinets()
157 function s.render(self, sid)
159 hw = guess_wifi_hw(dev),
165 function s.cfgsections(self)
166 local _, net, sl = nil, nil, { }
167 for _, net in ipairs(self.wnets) do
168 sl[#sl+1] = net:name()
169 self.wnets[net:name()] = net
174 o = s:option(Value, "__disable__")
176 function o.cfgvalue(self, sid)
177 local wnet = self.section.wnets[sid]
178 local wdev = wnet:get_device()
180 return ((wnet and wnet:get("disabled") == "1") or
181 (wdev and wdev:get("disabled") == "1")) and "1" or "0"
184 function o.write(self, sid, value)
185 local wnet = self.section.wnets[sid]
186 local wdev = wnet:get_device()
189 wnet:set("disabled", nil)
190 wdev:set("disabled", nil)
192 wnet:set("disabled", "1")
199 o = s:option(Value, "__delete__")
201 function o.write(self, sid, value)
202 local wnet = self.section.wnets[sid]
203 local nets = wnet:get_networks()
205 ntm:del_wifinet(wnet:id())
208 for _, net in ipairs(nets) do
209 if net:is_empty() then
210 ntm:del_network(net:name())
216 s = m:section(NamedSection, "__script__")
217 s.template = "admin_network/wifi_overview_status"
219 s = m:section(NamedSection, "__assoclist__")
221 function s.render(self, sid)
223 <h2><%:Associated Stations%></h2>
224 <%+admin_network/wifi_assoclist%>