libs/sys: implement luci.sys.wifi.channels()
authorJo-Philipp Wich <jow@openwrt.org>
Wed, 13 May 2009 18:42:53 +0000 (18:42 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Wed, 13 May 2009 18:42:53 +0000 (18:42 +0000)
modules/admin-full: only display available channels

libs/sys/luasrc/sys.lua
modules/admin-full/luasrc/model/cbi/admin_network/wifi.lua

index 1ecd5d2..3412b32 100644 (file)
@@ -681,6 +681,36 @@ function wifi.iwscan(iface)
        return iface and (iws[iface] or {}) or iws
 end
 
+--- Get available channels from given wireless iface.
+-- @param iface        Wireless interface (optional)
+-- @return             Table of available channels
+function wifi.channels(iface)
+       local cmd = "iwlist " .. ( iface or "" ) .. " freq 2>/dev/null"
+       local cns = { }
+
+       local fd = io.popen(cmd)
+       if fd then
+               local ln, c, f
+               repeat
+                       ln = fd:read("*l") or ""
+                       c, f = ln:match("Channel (%d+) : (%d+%.%d+) GHz")
+                       if c and f then
+                               cns[tonumber(c)] = tonumber(f)
+                       end
+               until not ( #ln > 0 )
+               fd:close()
+       end
+
+       if #cns == 0 then
+               cms = {
+                       2.412, 2.417, 2.422, 2.427, 2.432, 2.437,
+                       2.442, 2.447, 2.452, 2.457, 2.462
+               }
+       end
+
+       return cns
+end
+
 
 --- LuCI system utilities / init related functions.
 -- @class      module
index 6783866..43bff5d 100644 (file)
@@ -39,19 +39,9 @@ local hwtype = m:get(arg[1], "type")
 local nsantenna = m:get(arg[1], "antenna")
 
 ch = s:option(Value, "channel", translate("a_w_channel"))
-for i=1, 14 do
-       ch:value(i, i .. " (2.4 GHz)")
+for c, f in luci.util.kspairs(luci.sys.wifi.channels()) do
+       ch:value(c, "%i (%.3f GHz)" %{ c, f })
 end
-for i=36, 64, 4 do
-       ch:value(i, i .. " (5 GHz)")
-end
-for i=100, 140, 4 do
-       ch:value(i, i .. " (5 GHz)")
-end
-ch:value(147, 147 .. " (5 GHz)")
-ch:value(151, 151 .. " (5 GHz)")
-ch:value(155, 155 .. " (5 GHz)")
-ch:value(167, 167 .. " (5 GHz)")
 
 s:option(Value, "txpower", translate("a_w_txpwr"), "dBm").rmempty = true