modules/admin-full: initial work on site survey and network join
[project/luci.git] / modules / admin-full / luasrc / view / admin_network / wifi_overview.htm
1 <%#
2 LuCI - Lua Configuration Interface
3 Copyright 2008-2009 Steven Barth <steven@midlink.org>
4 Copyright 2008-2009 Jo-Philipp Wich <xm@leipzig.freifunk.net>
5
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
9
10         http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13
14 -%>
15
16 <%-
17
18         local sys = require "luci.sys"
19         local utl = require "luci.util"
20         local uci = require "luci.model.uci".cursor_state()
21
22         function guess_wifi_hw(ifname)
23                 local name, idx = ifname:match("^([a-z]+)(%d+)")
24                 idx = tonumber(idx)
25
26                 -- wl.o
27                 if name == "wl" then
28                         local name = "Broadcom 802.11 Wireless Controller"
29                         local nm   = 0
30
31                         local fd = nixio.open("/proc/bus/pci/devices", "r")
32                         if fd then
33                                 local ln
34                                 for ln in fd:linesource() do
35                                         if ln:match("wl$") then
36                                                 if nm == idx then
37                                                         local version = ln:match("^%S+%s+%S%S%S%S([0-9a-f]+)")
38                                                         name = string.format(
39                                                                 "Broadcom BCM%04x 802.11 Wireless Controller",
40                                                                 tonumber(version, 16)
41                                                         )
42
43                                                         break
44                                                 else
45                                                         nm = nm + 1
46                                                 end
47                                         end
48                                 end
49                                 fd:close()
50                         end
51
52                         return name
53
54                 -- madwifi
55                 elseif name == "ath" or name == "wifi" then
56                         return "Atheros 802.11 Wireless Controller"
57
58                 -- ralink
59                 elseif name == "ra" then
60                         return "RaLink 802.11 Wireless Controller"
61
62                 -- prism?
63                 elseif name == "eth" then
64                         return "Prism 802.11 Wireless Controller"
65
66                 -- dunno yet
67                 else
68                         return "Unkown 802.11 Wireless Controller"
69                 end
70         end
71
72         function guess_wifi_signal(info)
73                 local snr = -1 * ((info.noise or 0) - (info.signal or 0))
74                 local scale = math.floor(snr / 5)
75                 local icon
76
77                 if not info.bssid or info.bssid == "00:00:00:00:00:00" then
78                         icon = resource .. "/icons/signal-none.png"
79                 elseif scale < 1 then
80                         icon = resource .. "/icons/signal-0.png"
81                 elseif scale < 2 then
82                         icon = resource .. "/icons/signal-0-25.png"
83                 elseif scale < 3 then
84                         icon = resource .. "/icons/signal-25-50.png"
85                 elseif scale < 4 then
86                         icon = resource .. "/icons/signal-50-75.png"
87                 else
88                         icon = resource .. "/icons/signal-75-100.png"
89                 end
90
91                 return icon
92         end
93
94         function percent_wifi_signal(info)
95                 local qc = info.quality or 0
96                 local qm = info.quality_max or 0
97
98                 if info.bssid and qc > 0 and qm > 0 then
99                         return math.floor((100 / qm) * qc)
100                 else
101                         return 0
102                 end
103         end
104
105         function find_wifi_devices()
106                 local devs = { }
107                 uci:foreach("wireless", "wifi-device",
108                         function(s)
109                                 local dev = s['.name']
110                                 local act = 0
111                                 devs[dev] = { active = 0, networks = { } }
112
113                                 uci:foreach("wireless", "wifi-iface",
114                                         function(s)
115                                                 if s.device == dev then
116                                                         if s.up == "1" then act = act + 1 end
117                                                         devs[dev].networks[#devs[dev].networks+1] = {
118                                                                 active = (s.up == "1"),
119                                                                 ifname = s.ifname,
120                                                                 info   = sys.wifi.getiwinfo(s.ifname or s.device)
121                                                         }
122                                                 end
123                                         end)
124
125                                 devs[dev].hwname = guess_wifi_hw(dev)
126                                 devs[dev].active = (act > 0)
127                         end)
128
129                 return devs
130         end
131
132         function find_wifi_frequency(state)
133                 if state.active then
134                         return string.format("%d (%.03f GHz)",
135                                 state.networks[1].info.channel,
136                                 state.networks[1].info.frequency / 1000);
137                 else
138                         return "n/a"
139                 end
140         end
141
142
143         local devices  = find_wifi_devices()
144         local arpcache = { }
145         sys.net.arptable(function(e) arpcache[e["HW address"]] = e["IP address"] end)
146 -%>
147
148 <%+header%>
149
150 <h2><a id="content" name="content"><%:a_s_iw_overview Wireless Overview%></a></h2>
151
152 <div class="cbi-map">
153
154         <% for dev, state in utl.kspairs(devices) do %>
155         <!-- device <%=dev%> -->
156         <fieldset class="cbi-section">
157                 <table class="cbi-section-table" style="margin:10px; empty-cells:hide">
158                         <!-- physical device -->
159                         <tr>
160                                 <td style="width:34px"><img src="<%=resource%>/icons/wifi<%=state.active and "" or "_disabled"%>.png" style="float:left; margin-right:10px" /></td>
161                                 <td colspan="2" style="text-align:left">
162                                         <big><strong><%=state.hwname%> (<%=dev%>)</strong></big><br />
163                                         <strong>Channel:</strong> <%=find_wifi_frequency(state)%> |
164                                         <strong>Bitrate:</strong> <%=state.active and (state.networks[1].info.bitrate / 1000) .. " Mb/s" or "n/a"%>
165                                 </td>
166                                 <td style="width:40px">
167                                         <a href="<%=luci.dispatcher.build_url("admin/network/wireless_join?device="..dev)%>"><img style="border:none" src="<%=resource%>/cbi/find.gif" alt="Find and join network" title="Find and join network" /></a>
168                                         <a href="#"><img style="border:none" src="<%=resource%>/cbi/add.gif" alt="Provide new network" title="Provide new network" /></a>
169                                 </td>
170                         </tr>
171                         <!-- /physical device -->
172
173                         <!-- network list -->
174                         <% if #state.networks > 0 then %>
175                                 <% for i, net in ipairs(state.networks) do %>
176                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
177                                         <td></td>
178                                         <td class="cbi-value-field" style="width:16px; padding:3px">
179                                                 <img src="<%=guess_wifi_signal(net.info)%>" title="Signal: <%=net.info.signal%> dBm / Noise: <%=net.info.noise%> dBm" /><br />
180                                                 <small><%=percent_wifi_signal(net.info)%>%</small>
181                                         </td>
182                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
183                                                 <strong>SSID:</strong> <%=utl.pcdata(net.info.ssid)%> |
184                                                 <strong>Mode:</strong> <%=net.info.mode%><br />
185                                                 <strong>BSSID:</strong> <%=net.info.bssid%> |
186                                                 <strong>Encryption:</strong> <%=net.info.enctype%>
187                                         </td>
188                                         <td class="cbi-value-field" style="width:40px">
189                                                 <a href="<%=REQUEST_URI%>/<%=dev%>"><img style="border:none" src="<%=resource%>/cbi/edit.gif" alt="Edit this network" title="Edit this network" /></a>
190                                                 <a href="#"><img style="border:none" src="<%=resource%>/cbi/remove.gif" alt="Delete this network" title="Delete this network" /></a>
191                                         </td>
192                                 </tr>
193                                 <% end %>
194                         <% else %>
195                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
196                                         <td></td>
197                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
198                                                 <em>(No network configured on this device)</em>
199                                         </td>
200                                 </tr>
201                         <% end %>
202                         <!-- /network list -->
203                 </table>
204         </fieldset>
205         <!-- /device <%=dev%> -->
206         <% end %>
207
208
209
210
211         <h2><a id="content" name="content"><%:a_s_iw_overview2 Associated Stations%></a></h2>
212
213         <fieldset class="cbi-section">
214                 <table class="cbi-section-table" style="margin:10px; width:50%">
215                         <tr class="cbi-section-table-titles">
216                                 <th class="cbi-section-table-cell"></th>
217                                 <th class="cbi-section-table-cell">SSID</th>
218                                 <th class="cbi-section-table-cell">MAC</th>
219                                 <th class="cbi-section-table-cell">Address</th>
220                                 <th class="cbi-section-table-cell">Signal</th>
221                                 <th class="cbi-section-table-cell">Noise</th>
222                         </tr>
223
224                         <% local count = -1 %>
225                         <% for dev, state in utl.kspairs(devices) do %>
226                                 <% for _, net in ipairs(state.networks) do %>
227                                         <% for mac, info in utl.kspairs(net.info.assoclist) do info.bssid = mac; count = count + 1 %>
228                                         <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + (count % 2)%>">
229                                                 <td class="cbi-value-field"><img src="<%=guess_wifi_signal(info)%>" title="Signal: <%=info.signal%> dBm / Noise: <%=info.noise%> dBm" /></td>
230                                                 <td class="cbi-value-field"><%=net.info.ssid%></td>
231                                                 <td class="cbi-value-field"><%=mac%></td>
232                                                 <td class="cbi-value-field"><%=arpcache[mac] or "n/a"%></td>
233                                                 <td class="cbi-value-field"><%=info.signal%> dBm</td>
234                                                 <td class="cbi-value-field"><%=info.noise%> dBm</td>
235                                         </tr>
236                                         <% end %>
237                                 <% end %>
238                         <% end %>
239                         <% if count <= 0 then %>
240                         <tr class="cbi-section-table-row cbi-rowstyle-2">
241                                 <td class="cbi-value-field" colspan="6">
242                                         <em>No information available</em>
243                                 </td>
244                         </tr>
245                         <% end %>
246                 </table>
247         </fieldset>
248 </div>
249
250 <%+footer%>