wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(t) (require "nwfnet"):runnet("wstaconn",t) end)
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(t) (require "nwfnet"):runnet("wstadscn",t) end)
+-- do this before wifi because for some unknown reason it clobbers the
+-- configuration state. I suspect that this is an SDK bug.
+if file.open("nwfnet.cert","r") then
+ local cert = ""
+ local chunk = file.read()
+ while chunk ~= nil do cert = cert..chunk; chunk = file.read() end
+ ok, res = pcall(net.cert.verify,cert)
+ file.close()
+ if ok then
+ print("Loaded cert from nwfnet.cert, which will now be removed.")
+ file.remove("nwfnet.cert")
+ else
+ print("Failed to load from nwfnet.cert", res)
+ end
+end
+
-- Connection configuration options
--
-- While these things could be persisted by the ESP, it's probably simpler to
if file.open("nwfnet.conf","r") then
local conf = sjson.decode(file.read() or "")
if type(conf) == "table" then
+
local essid, pw = conf["sta_essid"], conf["sta_pw"]
if essid ~= nil and pw ~= nil then
wifi.sta.config({['ssid'] = essid, ['pwd'] = pw, save=false})
else wifi.setmode(wifi.STATION)
end
+ if conf["sntp"] then
+ (require "nwfnet").sntp = conf["sntp"]
+ end
+
+ if conf["tls_verify"] == 1 then
+ pcall(net.cert.verify,true)
+ end
+
else print("nwfnet.conf malformed")
end
file.close()
end
-- must come after we've got our event callbacks registered, yeah?
wifi.sta.connect()
-
-if file.open("nwfnet.cert","r") then
- local cert = ""
- local chunk = file.read()
- while chunk ~= nil do cert = cert..chunk; chunk = file.read() end
- ok, res = pcall(net.cert.verify,cert)
- file.close()
- if ok then
- print("Loaded cert from nwfnet.cert; likely, you want to remove this file...")
- else
- print("Failed to load from nwfnet.cert", res)
- end
-end
-
-if file.open("nwfnet.conf2","r") then
- local conf = sjson.decode(file.read() or "")
- if type(conf) == "table" then
- if conf["verify"] == 1 then
- print("Enabling certificate verification")
- pcall(net.cert.verify,true)
- end
- else print("nwfnet.conf2 malformed")
- end
-end
local function dosntp(server)
- local nn = require "nwfnet"
- if not server then
- if file.open("nwfnet.conf2","r") then
- local conf = sjson.decode(file.read() or "")
- if type(conf) == "table" then
- if conf["sntp"] then server = conf["sntp"] end
- else print("nwfnet.conf2 malformed")
- end end end
- -- XXX Soon, in upstream, the NTP module will default to a ntp pool;
- -- we should just let that happen here, when that happens!
- local x, y
- if not server then x, y, server = wifi.ap.getip() end
- if not server then x, y, server = wifi.sta.getip() end
- if not server then nn:runnet("sntperr", "No sntp server?") return end
- sntp.sync(server,
- function(sec,usec,server) rtctime.set(sec,usec); nn:runnet("sntpsync",sec,usec,server) end,
- function(err) nn:runnet("sntperr",err) end
+ sntp.sync((require "nwfnet").sntp,
+ function(sec,usec,server) rtctime.set(sec,usec); (require"nwfnet"):runnet("sntpsync",sec,usec,server) end,
+ function(err) (require"nwfnet"):runnet("sntperr",err) end
)
end