37819bd1235b4eb4b67ea004fcb3291b9b9cf627
[oweals/luci.git] / libs / core / luasrc / model / network / wireless.lua
1 --[[
2 LuCI - Network model - Wireless extension
3
4 Copyright 2009 Jo-Philipp Wich <xm@subsignal.org>
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 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 ]]--
19
20 local pairs, i18n, uci = pairs, luci.i18n, luci.model.uci
21
22 local iwi = require "iwinfo"
23 local utl = require "luci.util"
24 local uct = require "luci.model.uci.bind"
25
26 module "luci.model.network.wireless"
27
28 local ub = uct.bind("wireless")
29 local st, ifs
30
31 function init(self, cursor)
32         cursor:unload("wireless")
33         cursor:load("wireless")
34         ub:init(cursor)
35
36         st = uci.cursor_state()
37         ifs = { }
38
39         local count = 0
40                 
41         ub.uci:foreach("wireless", "wifi-iface",
42                 function(s)
43                         count = count + 1
44
45                         local device = s.device or "wlan0"
46                         local state = st:get_all("wireless", s['.name'])
47                         local name = device .. ".network" .. count
48                         
49                         ifs[name] = {
50                                 idx      = count,
51                                 name     = name,
52                                 rawname  = state and state.ifname or name,
53                                 flags    = { },
54                                 ipaddrs  = { },
55                                 ip6addrs = { },
56
57                                 type     = "wifi",
58                                 network  = s.network,
59                                 handler  = self,
60                                 wifi     = state or s,
61                                 sid      = s['.name']
62                         }
63                 end)
64 end
65
66 local function _mode(m)
67         if     m == "ap"      then m = "AP"
68         elseif m == "sta"     then m = "Client"
69         elseif m == "adhoc"   then m = "Ad-Hoc"
70         elseif m == "mesh"    then m = "Mesh"
71         elseif m == "monitor" then m = "Monitor"
72         end
73
74         return m or "Client"
75 end
76
77 function shortname(self, iface)
78         if iface.dev and iface.dev.wifi then
79                 return "%s %q" %{
80                         i18n.translate(_mode(iface.dev.wifi.mode)),
81                         iface.dev.wifi.ssid or iface.dev.wifi.bssid
82                                 or i18n.translate("(hidden)")
83                 }
84         else
85                 return iface:name()
86         end
87 end
88
89 function get_i18n(self, iface)
90         if iface.dev and iface.dev.wifi then
91                 return "%s: %s %q" %{
92                         i18n.translate("Wireless Network"),
93                         i18n.translate(iface.dev.wifi.mode or "Client"),
94                         iface.dev.wifi.ssid or iface.dev.wifi.bssid
95                                 or i18n.translate("(hidden)")
96                 }
97         else
98                 return "%s: %q" %{ i18n.translate("Wireless Network"), iface:name() }
99         end
100 end
101
102 function rename_network(self, old, new)
103         local i
104         for i, _ in pairs(ifs) do
105                 if ifs[i].network == old then
106                         ifs[i].network = new
107                 end
108         end
109
110         ub.uci:foreach("wireless", "wifi-iface",
111                 function(s)
112                         if s.network == old then
113                                 if new then 
114                                         ub.uci:set("wireless", s['.name'], "network", new)
115                                 else
116                                         ub.uci:delete("wireless", s['.name'], "network")
117                                 end
118                         end
119                 end)
120 end
121
122 function del_network(self, old)
123         return self:rename_network(old, nil)
124 end
125
126 function find_interfaces(self, iflist, brlist)
127         local iface
128         for iface, _ in pairs(ifs) do
129                 iflist[iface] = ifs[iface]
130         end
131 end
132
133 function ignore_interface(self, iface)
134         if ifs and ifs[iface] then
135                 return false
136         else
137                 return iwi.type(iface) and true or false
138         end
139 end
140
141 function add_interface(self, net, iface)
142         if ifs and ifs[iface] and ifs[iface].sid then
143                 ub.uci:set("wireless", ifs[iface].sid, "network", net:name())
144                 ifs[iface].network = net:name()
145                 return true
146         end
147
148         return false
149 end
150
151 function del_interface(self, net, iface)
152         if ifs and ifs[iface] and ifs[iface].sid then
153                 ub.uci:delete("wireless", ifs[iface].sid, "network")
154                 --return true
155         end
156
157         return false
158 end
159
160 return _M
161