wifi_add.lua 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. -- Copyright 2017 Dirk Brenken (dev@brenken.org)
  2. -- This is free software, licensed under the Apache License, Version 2.0
  3. local fs = require("nixio.fs")
  4. local uci = require("luci.model.uci").cursor()
  5. local http = require("luci.http")
  6. local trmiface = uci.get("travelmate", "global", "trm_iface") or "trm_wwan"
  7. m = SimpleForm("add", translate("Add Wireless Uplink Configuration"))
  8. m.submit = translate("Save")
  9. m.cancel = translate("Back to overview")
  10. m.reset = false
  11. function m.on_cancel()
  12. http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
  13. end
  14. m.hidden = {
  15. device = http.formvalue("device"),
  16. ssid = http.formvalue("ssid"),
  17. wep = http.formvalue("wep"),
  18. wpa_suites = http.formvalue("wpa_suites"),
  19. wpa_version = http.formvalue("wpa_version")
  20. }
  21. if m.hidden.ssid ~= "" then
  22. wssid = m:field(Value, "ssid", translate("SSID"))
  23. wssid.default = m.hidden.ssid
  24. else
  25. wssid = m:field(Value, "ssid", translate("SSID (hidden)"))
  26. end
  27. if (tonumber(m.hidden.wep) or 0) == 1 then
  28. wkey = m:field(Value, "key", translate("WEP passphrase"),
  29. translate("Specify the secret encryption key here."))
  30. wkey.password = true
  31. wkey.datatype = "wepkey"
  32. elseif (tonumber(m.hidden.wpa_version) or 0) > 0 and
  33. (m.hidden.wpa_suites == "PSK" or m.hidden.wpa_suites == "PSK2")
  34. then
  35. wkey = m:field(Value, "key", translate("WPA passphrase"),
  36. translate("Specify the secret encryption key here."))
  37. wkey.password = true
  38. wkey.datatype = "wpakey"
  39. end
  40. function wssid.write(self, section, value)
  41. newsection = uci:section("wireless", "wifi-iface", nil, {
  42. mode = "sta",
  43. network = trmiface,
  44. device = m.hidden.device,
  45. ssid = wssid:formvalue(section),
  46. disabled = "1"
  47. })
  48. if (tonumber(m.hidden.wep) or 0) == 1 then
  49. uci:set("wireless", newsection, "encryption", "wep-open")
  50. uci:set("wireless", newsection, "key", "1")
  51. uci:set("wireless", newsection, "key1", wkey:formvalue(section))
  52. elseif (tonumber(m.hidden.wpa_version) or 0) > 0 then
  53. uci:set("wireless", newsection, "encryption", "psk2")
  54. uci:set("wireless", newsection, "key", wkey:formvalue(section))
  55. else
  56. uci:set("wireless", newsection, "encryption", "none")
  57. end
  58. uci:save("wireless")
  59. uci:commit("wireless")
  60. http.redirect(luci.dispatcher.build_url("admin/services/travelmate/stations"))
  61. end
  62. return m