72fe39ea3ee833baced7732f466a6c9a6068efa4
[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 "Generic 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 and state.active and state.networks[1] and state.networks[1].info 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                                         <% if state.networks[1] and state.networks[1].info then %>
164                                                 <strong>Channel:</strong> <%=find_wifi_frequency(state)%> |
165                                                 <strong>Bitrate:</strong> <%=state.networks[1].info.bitrate and (state.networks[1].info.bitrate / 1000) .. " Mb/s" or "n/a"%>
166                                         <% end %>
167                                 </td>
168                                 <td style="width:40px">
169                                         <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>
170                                         <a href="#"><img style="border:none" src="<%=resource%>/cbi/add.gif" alt="Provide new network" title="Provide new network" /></a>
171                                 </td>
172                         </tr>
173                         <!-- /physical device -->
174
175                         <!-- network list -->
176                         <% if #state.networks > 0 then %>
177                                 <% for i, net in ipairs(state.networks) do %>
178                                 <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + ((i-1) % 2)%>">
179                                         <td></td>
180                                         <td class="cbi-value-field" style="width:16px; padding:3px">
181                                                 <img src="<%=guess_wifi_signal(net.info)%>" title="Signal: <%=net.info.signal or 0%> dBm / Noise: <%=net.info.noise or 0%> dBm" /><br />
182                                                 <small><%=percent_wifi_signal(net.info)%>%</small>
183                                         </td>
184                                         <td class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
185                                                 <strong>SSID:</strong> <%=utl.pcdata(net.info.ssid)%> |
186                                                 <strong>Mode:</strong> <%=net.info.mode%><br />
187                                                 <strong>BSSID:</strong> <%=net.info.bssid%> |
188                                                 <strong>Encryption:</strong> <%=net.info.enctype or "None"%>
189                                         </td>
190                                         <td class="cbi-value-field" style="width:40px">
191                                                 <a href="<%=REQUEST_URI%>/<%=dev%>"><img style="border:none" src="<%=resource%>/cbi/edit.gif" alt="Edit this network" title="Edit this network" /></a>
192                                                 <a href="#"><img style="border:none" src="<%=resource%>/cbi/remove.gif" alt="Delete this network" title="Delete this network" /></a>
193                                         </td>
194                                 </tr>
195                                 <% end %>
196                         <% else %>
197                                 <tr class="cbi-section-table-row cbi-rowstyle-2">
198                                         <td></td>
199                                         <td colspan="3" class="cbi-value-field" style="vertical-align:middle; text-align:left; padding:3px">
200                                                 <em>(No network configured on this device)</em>
201                                         </td>
202                                 </tr>
203                         <% end %>
204                         <!-- /network list -->
205                 </table>
206         </fieldset>
207         <!-- /device <%=dev%> -->
208         <% end %>
209
210
211
212
213         <h2><a id="content" name="content"><%:a_s_iw_overview2 Associated Stations%></a></h2>
214
215         <fieldset class="cbi-section">
216                 <table class="cbi-section-table" style="margin:10px; width:50%">
217                         <tr class="cbi-section-table-titles">
218                                 <th class="cbi-section-table-cell"></th>
219                                 <th class="cbi-section-table-cell">SSID</th>
220                                 <th class="cbi-section-table-cell">MAC</th>
221                                 <th class="cbi-section-table-cell">Address</th>
222                                 <th class="cbi-section-table-cell">Signal</th>
223                                 <th class="cbi-section-table-cell">Noise</th>
224                         </tr>
225
226                         <% local count = -1 %>
227                         <% for dev, state in utl.kspairs(devices) do %>
228                                 <% for _, net in ipairs(state.networks) do %>
229                                         <% for mac, info in utl.kspairs(net.info.assoclist) do info.bssid = mac; count = count + 1 %>
230                                         <tr class="cbi-section-table-row cbi-rowstyle-<%=1 + (count % 2)%>">
231                                                 <td class="cbi-value-field"><img src="<%=guess_wifi_signal(info)%>" title="Signal: <%=info.signal%> dBm / Noise: <%=info.noise%> dBm" /></td>
232                                                 <td class="cbi-value-field"><%=net.info.ssid%></td>
233                                                 <td class="cbi-value-field"><%=mac%></td>
234                                                 <td class="cbi-value-field"><%=arpcache[mac] or "n/a"%></td>
235                                                 <td class="cbi-value-field"><%=info.signal%> dBm</td>
236                                                 <td class="cbi-value-field"><%=info.noise%> dBm</td>
237                                         </tr>
238                                         <% end %>
239                                 <% end %>
240                         <% end %>
241                         <% if count <= 0 then %>
242                         <tr class="cbi-section-table-row cbi-rowstyle-2">
243                                 <td class="cbi-value-field" colspan="6">
244                                         <em>No information available</em>
245                                 </td>
246                         </tr>
247                         <% end %>
248                 </table>
249         </fieldset>
250 </div>
251
252 <%+footer%>