luci-app-travelmate: sync with travelmate 0.9.5
[project/luci.git] / applications / luci-app-travelmate / luasrc / model / cbi / travelmate / wifi_add.lua
1 -- Copyright 2017 Dirk Brenken (dev@brenken.org)
2 -- This is free software, licensed under the Apache License, Version 2.0
3
4 local fs = require("nixio.fs")
5 local uci = require("luci.model.uci").cursor()
6 local http = require("luci.http")
7 local trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"
8 local val = ""
9
10 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
11 m.submit = translate("Save")
12 m.cancel = translate("Back to overview")
13 m.reset = false
14
15 function m.on_cancel()
16 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
17 end
18
19 m.hidden = {
20 device = http.formvalue("device"),
21 ssid = http.formvalue("ssid"),
22 bssid = http.formvalue("bssid"),
23 wep = http.formvalue("wep"),
24 wpa_suites = http.formvalue("wpa_suites"),
25 wpa_version = http.formvalue("wpa_version")
26 }
27
28 if m.hidden.ssid ~= "" then
29 wssid = m:field(Value, "ssid", translate("SSID"))
30 wssid.datatype = "rangelength(1,32)"
31 wssid.default = m.hidden.ssid or ""
32 else
33 wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
34 end
35
36 bssid = m:field(Value, "bssid", translate("BSSID"))
37 bssid.datatype = "macaddr"
38 bssid.default = m.hidden.bssid or ""
39
40 if (tonumber(m.hidden.wep) or 0) == 1 then
41 wkey = m:field(Value, "key", translate("WEP passphrase"),
42 translate("Specify the secret encryption key here."))
43 wkey.password = true
44 wkey.datatype = "wepkey"
45 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
46 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
47 wkey = m:field(Value, "key", translate("WPA passphrase"),
48 translate("Specify the secret encryption key here."))
49 wkey.password = true
50 wkey.datatype = "wpakey"
51 elseif m.hidden.wpa_suites == "802.1X" then
52 eaptype = m:field(ListValue, "eap_type", translate("EAP-Method"))
53 eaptype:value("TLS")
54 eaptype:value("TTLS")
55 eaptype:value("PEAP")
56 eaptype.default = "PEAP"
57
58 authentication = m:field(ListValue, "auth", translate("Authentication"))
59 authentication:value("PAP")
60 authentication:value("CHAP")
61 authentication:value("MSCHAP")
62 authentication:value("MSCHAPV2")
63 authentication.default = "MSCHAPV2"
64
65 ident = m:field(Value, "identity", translate("Identity"))
66
67 pass = m:field(Value, "password", translate("Password"))
68 pass.datatype = "wpakey"
69 pass.password = true
70 end
71 end
72
73 function wssid.write(self, section, value)
74 newsection = uci:section("wireless", "wifi-iface", nil, {
75 mode = "sta",
76 network = trmiface,
77 device = m.hidden.device,
78 ssid = wssid:formvalue(section),
79 bssid = bssid:formvalue(section),
80 disabled = "1"
81 })
82 if wkey ~= nil then
83 val = wkey:formvalue(section)
84 if val == "" then
85 val = "changeme"
86 end
87 end
88 if (tonumber(m.hidden.wep) or 0) == 1 then
89 uci:set("wireless", newsection, "encryption", "wep-open")
90 uci:set("wireless", newsection, "key", "1")
91 uci:set("wireless", newsection, "key1", val)
92 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
93 if m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2" then
94 uci:set("wireless", newsection, "encryption", "psk2")
95 uci:set("wireless", newsection, "key", val)
96 elseif m.hidden.wpa_suites == "802.1X" then
97 uci:set("wireless", newsection, "encryption", "wpa2")
98 uci:set("wireless", newsection, "eap_type", eaptype:formvalue(section))
99 uci:set("wireless", newsection, "auth", authentication:formvalue(section))
100 val = ident:formvalue(section)
101 if val == "" then
102 val = "changeme"
103 end
104 uci:set("wireless", newsection, "identity", val)
105 val = pass:formvalue(section)
106 if val == "" then
107 val = "changeme"
108 end
109 uci:set("wireless", newsection, "password", val)
110 end
111 else
112 uci:set("wireless", newsection, "encryption", "none")
113 end
114 uci:save("wireless")
115 uci:commit("wireless")
116 http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
117 end
118
119 return m