3f3f2348a7d214dc2d9ebe8c88ee154ebef117cf
[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
9 m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
10 m.cancel = translate("Back to overview")
11 m.reset = false
12
13 function m.on_cancel()
14         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
15 end
16
17 m.hidden = {
18         device      = http.formvalue("device"),
19         ssid        = http.formvalue("ssid"),
20         wep         = http.formvalue("wep"),
21         wpa_suites  = http.formvalue("wpa_suites"),
22         wpa_version = http.formvalue("wpa_version")
23 }
24
25 if m.hidden.ssid ~= "" then
26         wssid = m:field(Value, "ssid", translate("SSID"))
27         wssid.default = m.hidden.ssid
28 else
29         wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
30 end
31
32 if (tonumber(m.hidden.wep) or 0) == 1 then
33         wkey = m:field(Value, "key", translate("WEP passphrase"),
34                 translate("Specify the secret encryption key here."))
35         wkey.password = true
36         wkey.datatype = "wepkey"
37 elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
38         (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
39 then
40         wkey = m:field(Value, "key", translate("WPA passphrase"),
41                 translate("Specify the secret encryption key here."))
42         wkey.password = true
43         wkey.datatype = "wpakey"
44 end
45
46 function wssid.write(self, section, value)
47         newsection = uci:section("wireless", "wifi-iface", nil, {
48                 mode     = "sta",
49                 network  = trmiface,
50                 device   = m.hidden.device,
51                 ssid     = wssid:formvalue(section),
52                 disabled = "1"
53         })
54         if (tonumber(m.hidden.wep) or 0) == 1 then
55                 uci:set("wireless", newsection, "encryption", "wep-open")
56                 uci:set("wireless", newsection, "key", "1")
57                 uci:set("wireless", newsection, "key1", wkey:formvalue(section))
58         elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
59                 uci:set("wireless", newsection, "encryption", "psk2")
60                 uci:set("wireless", newsection, "key", wkey:formvalue(section))
61         else
62                 uci:set("wireless", newsection, "encryption", "none")
63         end
64         uci:save("wireless")
65         uci:commit("wireless")
66         http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
67 end
68
69 return m