7ac2fff99a496bca34eab0dc9e8872458fdccd5c
[project/luci.git] / modules / niu / luasrc / model / cbi / niu / wireless / bridge.lua
1 local uci = require "luci.model.uci"
2 local cursor = uci.cursor()
3
4 if not cursor:get("wireless", "bridge") then
5         cursor:section("wireless", "wifi-iface", "bridge",
6                 {device = "_", doth = "1", _niu = "1", mode = "sta", wds = "1"})
7         cursor:save("wireless")
8 end
9
10 local function deviceroute(self)
11         cursor:unload("wireless")
12         local d = cursor:get("wireless", "bridge", "device")
13         if d ~= "none" then
14                 local nc = uci.cursor(nil, "")
15                 cursor:delete_all("wireless", "wifi-iface", function(s)
16                         return s.device == d and s._niu ~= "1"
17                 end)
18                 if nc:get("wireless", "bridge", "network")
19                  ~= cursor:get("wireless", "bridge", "network") then
20                         cursor:delete("wireless", "bridge", "network")
21                 end
22                 cursor:set("wireless", d, "disabled", 0)
23                 cursor:foreach("dhcp", "dhcp", function(s)
24                         if s.interface == "lan" and s.ignore ~= "1" then 
25                                 cursor:set("dhcp", s[".name"], "ignore", "1")
26                         end
27                 end)
28                 self:set_route("scan", "bridge", "bridgelan")
29         else
30                 if cursor:get("wireless", "bridge", "network") then
31                         cursor:delete("wireless", "bridge", "network")
32                         cursor:foreach("dhcp", "dhcp", function(s)
33                                 if s.interface == "lan" and s.ignore == "1" then 
34                                         cursor:set("dhcp", s[".name"], "ignore", "0")
35                                 end
36                         end)
37                         self:set_route("lan")
38                 end
39         end
40         cursor:save("dhcp")
41         cursor:save("wireless")
42 end
43
44
45 local d = Delegator()
46 d.allow_finish = true
47 d.allow_back = true
48 d.allow_cancel = true
49
50 d:add("device", "niu/wireless/brdevice")
51 d:add("deviceroute", deviceroute)
52 d:set("scan", "niu/network/wlanwanscan")
53 d:set("bridge", "niu/network/wlanwan")
54 d:set("bridgelan", "niu/network/lan1")
55 d:set("lan", "niu/network/lan1")
56
57 function d.on_cancel()
58         cursor:revert("network")
59         cursor:revert("wireless")
60         cursor:revert("dhcp")
61 end
62
63 function d.on_done()
64         if uci.inst_state:get("network", "lan", "ipaddr") ~= cursor:get("network", "lan", "ipaddr") then
65                 local cs = uci.cursor_state()
66                 cs:set("network", "lan", "_ipchanged", "1")
67                 cs:save("network")
68         end
69         
70         if cursor:get("network", "lan", "proto") == "dhcp" then
71                 local emergv4 = cursor:get("network", "lan", "_emergv4")
72                 if emergv4 then
73                         if cursor:get("network", "lan_ea") then
74                                 cursor:set("network", "lan_ea", "ipaddr", emergv4)
75                         else
76                                 cursor:section("network", "alias", "lan_ea", {
77                                         ipaddr = emergv4,
78                                         netmask = "255.255.255.0",
79                                         network = "lan"
80                                 })
81                         end
82                 else
83                         cursor:delete("network", "lan_ea")
84                 end
85         end
86         cursor:commit("network")
87         cursor:commit("wireless")
88         cursor:commit("dhcp")
89 end
90
91 return d