48fb9060089a5686ffba22dd0cf5799efdee1e0c
[project/luci.git] / applications / luci-meshwizard / luasrc / model / cbi / freifunk / meshwizard.lua
1 -- wizard rewrite wip
2
3 local uci = require "luci.model.uci".cursor()
4 local sys = require "luci.sys"
5 local util = require "luci.util"
6 local ip = require "luci.ip"
7
8 local community = "profile_" .. (uci:get("freifunk", "community", "name") or "Freifunk")
9 mesh_network = ip.IPv4(uci:get_first(community, "community", "mesh_network") or "10.0.0.0/8")
10
11 m = Map("meshwizard", translate("Wizard"), translate("This wizard will assist you in setting up your router for Freifunk " ..
12 "or another similar wireless community network."))
13 --m:chain("meshwizard")
14
15 n = m:section(TypedSection, "netconfig", translate("Interfaces"))
16 n.anonymous = true
17
18 -- common functions
19
20 function cbi_configure(device)
21 local configure = n:taboption(device, Flag, device .. "_config", translate("Configure this interface"))
22 end
23
24 function cbi_ip4addr(device)
25 local ip4addr = n:taboption(device, Value, device .. "_ip4addr", translate("Mesh IP address"),
26 translate("This is a unique address in the mesh (e.g. 10.1.1.1) and has to be registered at your local community."))
27 ip4addr:depends(device .. "_config", 1)
28 ip4addr.datatype = "ip4addr"
29 function ip4addr.validate(self, value)
30 local x = ip.IPv4(value)
31 if mesh_network:contains(x) then
32 return value
33 else
34 return nil, translate("The given IP address is not inside the mesh network range ") ..
35 "(" .. mesh_network:string() .. ")."
36 end
37 end
38 end
39
40 function cbi_dhcp(device)
41 local dhcp = n:taboption(device, Flag, device .. "_dhcp", translate("Enable DHCP"),
42 translate("DHCP will automatically assign ip addresses to clients"))
43 dhcp:depends(device .. "_config", 1)
44 dhcp.rmempty = true
45 end
46
47 function cbi_dhcprange(device)
48 local dhcprange = n:taboption(device, Value, device .. "_dhcprange", translate("DHCP IP range"),
49 translate("The IP range from which clients are assigned ip addresses (e.g. 10.1.2.1/28). " ..
50 "If this is a range inside your mesh network range, then it will be announced as HNA. Any other range will use NAT. " ..
51 "If left empty then the defaults from the community profile will be used."))
52 dhcprange:depends(device .. "_dhcp", "1")
53 dhcprange.rmempty = true
54 dhcprange.datatype = "ip4addr"
55 end
56 -- create tabs and config for wireless
57 local nets={}
58 uci:foreach("wireless", "wifi-device", function(section)
59 local device = section[".name"]
60 table.insert(nets, device)
61 end)
62
63 local wired_nets = {}
64 uci:foreach("network", "interface", function(section)
65 local device = section[".name"]
66 if not util.contains(nets, device) and device ~= "loopback" then
67 table.insert(nets, device)
68 table.insert(wired_nets, device)
69 end
70 end)
71
72 for _, net in util.spairs(nets, function(a,b) return (nets[a] < nets[b]) end) do
73 n:tab(net, net)
74 end
75
76 -- create cbi config for wireless
77 uci:foreach("wireless", "wifi-device", function(section)
78 local device = section[".name"]
79 local hwtype = section.type
80 local syscc = section.country or uci:get(community, "wifi_device", "country") or
81 uci:get("freifunk", "wifi_device", "country")
82
83 cbi_configure(device)
84
85 -- Channel selection
86
87 if hwtype == "atheros" then
88 local cc = util.trim(sys.exec("grep -i '" .. syscc .. "' /lib/wifi/cc_translate.txt |cut -d ' ' -f 2")) or 0
89 sys.exec('"echo " .. cc .. " > /proc/sys/dev/" .. device .. "/countrycode"')
90 elseif hwtype == "mac80211" then
91 sys.exec("iw reg set " .. syscc)
92 elseif hwtype == "broadcom" then
93 sys.exec ("wlc country " .. syscc)
94 end
95
96 local chan = n:taboption(device, ListValue, device .. "_channel", translate("Channel"),
97 translate("Your device and neighbouring nodes have to use the same channel."))
98 chan:depends(device .. "_config", 1)
99 chan:value('default')
100
101 for _, f in ipairs(sys.wifi.channels(device)) do
102 if not f.restricted then
103 chan:value(f.channel)
104 end
105 end
106 -- IPv4 address
107 cbi_ip4addr(device)
108
109 -- DHCP enable
110 cbi_dhcp(device)
111
112 -- DHCP range
113 cbi_dhcprange(device)
114
115 -- Enable VAP
116 if hwtype == "atheros" then
117 local vap = n:taboption(device, Flag, device .. "_vap", translate("Virtual Access Point (VAP)"),
118 translate("This will setup a new virtual wireless interface in Access Point mode."))
119 vap:depends(device .. "_dhcp", "1")
120 vap.rmempty = true
121 end
122 end)
123
124 for _, device in pairs(wired_nets) do
125 cbi_configure(device)
126 cbi_ip4addr(device)
127 cbi_dhcp(device)
128 cbi_dhcprange(device)
129 end
130
131 g = m:section(TypedSection, "general", translate("General Settings"))
132 g.anonymous = true
133
134 local cleanup = g:option(Flag, "cleanup", translate("Cleanup config"),
135 translate("If this is selected then config is cleaned before setting new config options."))
136 cleanup.default = "1"
137
138 local restrict = g:option(Flag, "local_restrict", translate("Protect LAN"),
139 translate("Check this to protect your LAN from other nodes or clients") .. " (" .. translate("recommended") .. ").")
140
141 local share = g:option(Flag, "sharenet", translate("Share your internet connection"),
142 translate("Select this to allow others to use your connection to access the internet."))
143 share.rmempty = true
144
145 --function m.on_after_commit (self)
146 -- sys.call("/usr/bin/mesh-wizard/wizard.sh >/dev/null")
147 --end
148
149 return m