Developing lightweight computation at the DSG edge

wizard.lua 8.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
--[[
    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
--]]

local sys = require "luci.sys"
local http = require "luci.http"
local ip = require "luci.ip"
local util = require "luci.util"
local uci  = require "luci.model.uci"
local uciout = uci.cursor()

package.path = package.path .. ";/etc/qmp/?.lua"
qmpinfo = require "qmpinfo"

m = SimpleForm("qmp_tmp", translate("qMp Wizard"))

local roaming_help
roaming_help = m:field(DummyValue,"roaming_help")
roaming_help:depends("_netmode","roaming")
roaming_help.rawhtml = true
roaming_help.default = "Roaming for quick deployments. Connected devices cannot see each other among different Mesh stations. \
39
    However the devices can change between Access Points without loosing connectivity"
40
41
42
43
44

local community_help
community_help = m:field(DummyValue,"community_help")
community_help:depends("_netmode","community")
community_help.rawhtml = true
45
community_help.default = "Community for static deployments (such as community networks). Connected devices have a static IP range and are able to see each others.\
46
47
48
49
50
   However there is no roaming between stations."

netmode = m:field(ListValue, "_netmode",translate("Network mode"),translate("Roaming is used for quick deployments.<br/>Community for network communities"))
netmode:value("community","community")
netmode:value("roaming","roaming")
51
52

local networkmode
53
if uciout:get("qmp","roaming","ignore") == "1" then
54
55
56
57
58
59
60
61
62
63
64
65
	local ipv4 = uciout:get("qmp","networks","bmx6_ipv4_address")
	local ipv4mask = string.find(ipv4,"/")
	if ipv4mask ~= nil then
		ipv4 = string.sub(ipv4,1,ipv4mask-1)
	end
	if ipv4 == uciout:get("qmp","networks","lan_address") then
		networkmode="community"
	end
else
	networkmode="roaming"
end
netmode.default=networkmode
66

67
68
nodeip_roaming =  m:field(Value, "_nodeip_roaming", translate("IP address"),
translate("Main IPv4 address. This IP must be unique in the Mesh network. <br/>Leave blank for randomize."))
69
70
nodeip_roaming:depends("_netmode","roaming")

71
72
73
74
75
76
local rip = uciout:get("qmp","networks","bmx6_ipv4_address")
if rip == nil or #rip < 7 then
	rip = uciout:get("bmx6","general","tun4Address")
	if rip == nil or #rip < 7 then 
		rip = ""
	end
77
78
end 

79
nodeip_roaming.default=rip
80

81
82
83
nodename = m:field(Value, "_nodename", translate("Node name"),
translate("The name of this node. Two random hex-digits will be added at the end."))

84
85
nodename:depends("_netmode","community")
nodename.default="qMp"
86
87
88
if uciout:get("qmp","node","community_id") ~= nil then
	nodename.default=uciout:get("qmp","node","community_id")
end
89

90
91
92
nodeip = m:field(Value, "_nodeip", translate("IP address"),
translate("Main IPv4 address. This IP must be unique in the Mesh network. If will be used for LAN end users."))

93
94
95
nodeip:depends("_netmode","community")
nodeip.default = "10.30."..util.trim(util.exec("echo $((($(date +%M)*$(date +%S)%254)+1))"))..".1"

96
97
nodemask = m:field(Value, "_nodemask",translate("Network mask"),
translate("Netmask to use with the IPv4 address specified before. This mask will be used for LAN end users."))
98
99
100
nodemask:depends("_netmode","community")
nodemask.default = "255.255.255.0"

101
102
103
104
105
if networkmode == "community" then
	nodeip.default=uciout:get("qmp","networks","lan_address")
	nodemask.default=uciout:get("qmp","networks","lan_netmask")
end

106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- Get list of devices {{ethernet}{wireless}}
devices = qmpinfo.get_devices()

-- Ethernet devices
nodedevs_eth = {}

local function is_a(dev, what)
	local x
	for x in util.imatch(uciout:get("qmp", "interfaces", what)) do
        	if dev == x then
        		return true
        	end
        end
	return false
end

122
for i,v in ipairs(devices.eth) do
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
        tmp = m:field(ListValue, "_" .. v, v)
	tmp:value("Mesh")
	tmp:value("Lan")
	tmp:value("Wan")

	if is_a(v, "lan_devices") then
		tmp.default = "Lan"
	elseif is_a(v, "wan_devices") then
		tmp.default = "Wan"
	elseif is_a(v, "mesh_devices") then
		tmp.default = "Mesh"
	end

	nodedevs_eth[i] = {v,tmp}
end

-- Wireless devices
nodedevs_wifi = {}

142
for i,v in ipairs(devices.wifi) do
143
144
145
	tmp = m:field(ListValue, "_" .. v, v)
	tmp:value("Mesh")
	tmp:value("AP")
146
	tmp.default = ""
147
148

	if is_a(v,"lan_devices") then
149
		-- If the device is in lan_devices, it is AP mode
150
151
		tmp.default = "AP"
	else
152
153
154
155
156
157
158
159
160
161
162
163
		-- Check if the device is adhoc_ap mode, then Mode=AP MeshAll=1
		uciout:foreach("qmp","wireless", function (s)
			if s.device == v then
				if s.mode == "adhoc_ap" then
					tmp.default = "AP"
				end
			end

		end)

		-- If it is not adhoc_ap it is only mesh
		if tmp.default == "" then tmp.default = "Mesh" end
164
165
166
167
168
169
170
171
172
173
174
175
176
	end

	nodedevs_wifi[i] = {v,tmp}
end

meshall = m:field(Flag, "_meshall", translate("Use mesh in all devices"),translate("If this option is enabled all existing network devices will be used for meshing (recomended)"))
meshall.default = "1"

function netmode.write(self, section, value)
	local name = nodename:formvalue(section)
	local mode = netmode:formvalue(section)
	local nodeip = nodeip:formvalue(section)
	local nodemask = nodemask:formvalue(section)
177
178
	local nodeip_roaming = nodeip_roaming:formvalue(section)
	
179
	if mode == "community" then
180
		uciout:set("qmp","roaming","ignore","1")
181
182
183
184
185
186
187
		uciout:set("qmp","networks","publish_lan","1")
		uciout:set("qmp","networks","lan_address",nodeip)
		uciout:set("qmp","networks","bmx6_ipv4_address",ip.IPv4(nodeip,nodemask):string())
		uciout:set("qmp","networks","lan_netmask",nodemask)
		uciout:set("qmp","node","community_id",name)

	else
188
		uciout:set("qmp","roaming","ignore","0")
189
190
191
		uciout:set("qmp","networks","publish_lan","0")
		uciout:set("qmp","networks","lan_address","172.30.22.1")
		uciout:set("qmp","networks","lan_netmask","255.255.0.0")
192
		uciout:set("qmp","networks","bmx6_ipv4_prefix24","10.202.0")
193
194
		uciout:set("qmp","networks","olsr6_ipv4_address","")
		uciout:set("qmp","networks","olsr6_ipv4_prefix24","10.201")
195
196
197
198
199
		if nodeip_roaming == nil then
			uciout:set("qmp","networks","bmx6_ipv4_address","")
		else
			uciout:set("qmp","networks","bmx6_ipv4_address",nodeip_roaming)
		end
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

	end

	local i,v,devmode,devname
	local lan_devices = ""
	local wan_devices = ""
	local mesh_devices = ""
	local meshall = meshall:formvalue(section)

	for i,v in ipairs(nodedevs_eth) do
		devmode = v[2]:formvalue(section)
		devname = v[1]

		if devmode == "Lan" then
			lan_devices = lan_devices..devname.." "
		elseif devmode == "Wan" then
			wan_devices = wan_devices..devname.." "
		end
		if devmode == "Mesh" or meshall == "1" then
			mesh_devices = mesh_devices..devname.." "
		end
	end

	for i,v in ipairs(nodedevs_wifi) do
		devmode = v[2]:formvalue(section)
		devname = v[1]

227
		if (devmode == "AP" and meshall == "1") or devmode == "Mesh" then
228
			mesh_devices = mesh_devices..devname.." "
229
230
		elseif devmode == "AP" and meshall ~= "1" then
			lan_devices = lan_devices..devname.." "
231
232
233
234
		end

		function setmode(s)
			if s.device == devname then
235
236
237
238
239
240
				if devmode == "AP" and meshall == "1" then 
					uciout:set("qmp",s['.name'],"mode","adhoc_ap")
				elseif devmode == "AP" then
					uciout:set("qmp",s['.name'],"mode","ap")
				else
					uciout:set("qmp",s['.name'],"mode","adhoc") end
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
			end
		end
		uciout:foreach("qmp","wireless",setmode)
	end

	uciout:set("qmp","interfaces","lan_devices",lan_devices)
	uciout:set("qmp","interfaces","wan_devices",wan_devices)
	uciout:set("qmp","interfaces","mesh_devices",mesh_devices)

	uciout:commit("qmp")
	apply()
end

function apply(self)
	http.redirect("/luci-static/resources/qmp/wait_long.html")
256
        luci.sys.call('(qmpcontrol configure_wifi ; qmpcontrol configure_network) &')
257
258
259
260
end


return m