libs/core: Fix wireless network ids
[project/luci.git] / libs / core / luasrc / model / wireless.lua
index 1f50c11..3d4b2b0 100644 (file)
@@ -17,7 +17,7 @@ limitations under the License.
 
 ]]--
 
-local pairs, i18n, uci, math = pairs, luci.i18n, luci.model.uci, math
+local pairs, type, i18n, uci, math = pairs, type, luci.i18n, luci.model.uci, math
 
 local iwi = require "iwinfo"
 local utl = require "luci.util"
@@ -86,26 +86,28 @@ function get_network(self, id)
        end
 end
 
-function shortname(self, iface)
-       if iface.wdev and iface.winfo then
-               return "%s %q" %{
-                       i18n.translate("a_s_if_iwmode_" .. iface:active_mode(), iface.winfo.mode(iface.wdev)), 
-                       iface:active_ssid() or "(hidden)"
-               }
-       else
-               return iface:name()
-       end
-end
+function add_network(self, options)
+       if type(options) == "table" and options.device and
+               ub.uci:get("wireless", options.device) == "wifi-device"
+       then
+               local s = ub.uci:section("wireless", "wifi-iface", nil, options)
+               local c = 1
+               ub.uci:foreach("wireless", "wifi-iface", function(s) c = c + 1 end)
 
-function get_i18n(self, iface)
-       if iface.wdev and iface.winfo then
-               return "%s: %s %q (%s)" %{
-                       i18n.translate("a_s_if_wifinet", "Wireless Network"),
-                       i18n.translate("a_s_if_iwmode_" .. iface:active_mode(), iface.winfo.mode(iface.wdev)),
-                       iface:active_ssid() or "(hidden)", iface.wdev
+               local id = "%s.network%d" %{ options.device, c }
+               ifs[id] = {
+                       id    = id,
+                       sid   = s,
+                       count = c
                }
-       else
-               return "%s: %q" %{ i18n.translate("a_s_if_wifinet", "Wireless Network"), iface:name() }
+
+               local wtype = iwi.type(options.device)
+               if wtype then
+                       ifs[id].winfo = iwi[wtype]
+                       ifs[id].wdev  = options.device
+               end
+
+               return network(s)
        end
 end
 
@@ -124,6 +126,29 @@ function del_network(self, id)
        end
 end
 
+function shortname(self, iface)
+       if iface.wdev and iface.winfo then
+               return "%s %q" %{
+                       i18n.translate(iface:active_mode()), 
+                       iface:active_ssid() or i18n.translate("(hidden)")
+               }
+       else
+               return iface:name()
+       end
+end
+
+function get_i18n(self, iface)
+       if iface.wdev and iface.winfo then
+               return "%s: %s %q (%s)" %{
+                       i18n.translate("Wireless Network"),
+                       i18n.translate(iface:active_mode()),
+                       iface:active_ssid() or i18n.translate("(hidden)"), iface.wdev
+               }
+       else
+               return "%s: %q" %{ i18n.translate("Wireless Network"), iface:name() }
+       end
+end
+
 function find_interfaces(self, iflist, brlist)
        local iface
        for iface, _ in pairs(ifs) do
@@ -207,23 +232,26 @@ network:property("network")
 function network._init(self, sid)
        local count = 0
 
-       ub.uci:foreach("wireless", "wifi-iface",
-               function(s)
-                       count = count + 1
-                       return s['.name'] ~= sid
-               end)
-       
+       local parent_dev = st:get("wireless", sid, "device")
+               or ub.uci:get("wireless", sid, "device")
+
        local dev = st:get("wireless", sid, "ifname")
-               or st:get("wireless", sid, "device")
+               or parent_dev
 
        if dev then
-               self.id = "%s.network%d" %{ dev, count }
-
-               local wtype = iwi.type(dev)
-               if dev and wtype then
-                       self.winfo = iwi[wtype]
-                       self.wdev  = dev
-               end
+               ub.uci:foreach("wireless", "wifi-iface",
+                       function(s)
+                               count = count + 1
+                               if s['.name'] == sid then
+                                       self.id = "%s.network%d" %{ parent_dev, count }
+
+                                       local wtype = iwi.type(dev)
+                                       if dev and wtype then
+                                               self.winfo = iwi[wtype]
+                                               self.wdev  = dev
+                                       end
+                               end
+                       end)
        end
 end
 
@@ -247,22 +275,20 @@ end
 
 function network.active_mode(self)
        local m = self.winfo and self.winfo.mode(self.wdev)
-       if m == "Master" or m == "Auto" then
-               m = "ap"
-       elseif m == "Ad-Hoc" then
-               m = "adhoc"
-       elseif m == "Client" then
-               m = "sta"
-       elseif m then
-               m = m:lower()
-       else
+       if not m then
                m = self:mode()
+               if     m == "ap"      then m = "AP"
+               elseif m == "sta"     then m = "Client"
+               elseif m == "adhoc"   then m = "Ad-Hoc"
+               elseif m == "mesh"    then m = "Mesh"
+               elseif m == "monitor" then m = "Monitor"
+               end
        end
-       return m or "ap"
+       return m or "Client"
 end
 
 function network.active_mode_i18n(self)
-       return i18n.translate("a_s_if_iwmode_" .. self:active_mode())
+       return i18n.translate(self:active_mode())
 end
 
 function network.active_ssid(self)
@@ -294,7 +320,7 @@ function network.bitrate(self)
 end
 
 function network.channel(self)
-       return self.winfo and self.winfo.channel(self.wdef)
+       return self.winfo and self.winfo.channel(self.wdev)
 end
 
 function network.signal(self)