summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-app-travelmate/luasrc/model/cbi/travelmate/wifi_add.lua
blob: 979307e0013b7f613575b194021f10134aeb921f (plain)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
-- Copyright 2017 Dirk Brenken (dev@brenken.org)
-- This is free software, licensed under the Apache License, Version 2.0

local fs = require("nixio.fs")
local uci = require("luci.model.uci").cursor()
local http = require("luci.http")
local trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"

m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
m.cancel = translate("Back to overview")
m.reset = false

function m.on_cancel()
	http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
end

m.hidden = {
	device      = http.formvalue("device"),
	ssid        = http.formvalue("ssid"),
	wep         = http.formvalue("wep"),
	wpa_suites	= http.formvalue("wpa_suites"),
	wpa_version = http.formvalue("wpa_version")
}

if m.hidden.ssid ~= "" then
	wssid = m:field(Value, "ssid", translate("SSID"))
	wssid.default = m.hidden.ssid
else
	wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
end

if (tonumber(m.hidden.wep) or 0) == 1 then
	wkey = m:field(Value, "key", translate("WEP passphrase"),
		translate("Specify the secret encryption key here."))
	wkey.password = true
	wkey.datatype = "wepkey"
elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
	(m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
then
	wkey = m:field(Value, "key", translate("WPA passphrase"),
		translate("Specify the secret encryption key here."))
	wkey.password = true
	wkey.datatype = "wpakey"
end

function wssid.write(self, section, value)
	newsection = uci:section("wireless", "wifi-iface", nil, {
		mode       = "sta",
		network    = trmiface,
		device     = m.hidden.device,
		ssid       = wssid:formvalue(section),
		disabled   = "1"
	})
	if (tonumber(m.hidden.wep) or 0) == 1 then
		uci:set("wireless", newsection, "encryption", "wep-open")
		uci:set("wireless", newsection, "key", "1")
		uci:set("wireless", newsection, "key1", wkey:formvalue(section))
	elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
		uci:set("wireless", newsection, "encryption", "psk2")
		uci:set("wireless", newsection, "key", wkey:formvalue(section))
	else
		uci:set("wireless", newsection, "encryption", "none")
	end
	uci:save("wireless")
	uci:commit("wireless")
	http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
end

return m