From cdce672b22724d1c06816d480dad0a72ae65ccdc Mon Sep 17 00:00:00 2001 From: Nathaniel Wesley Filardo Date: Sat, 6 Jul 2019 21:44:39 +0100 Subject: [PATCH] Remove nwfnet.conf2 Once upon a time, it was useful because wifi.sta.config() wrote its changes to flash and we wanted to minimize wear and tear. Those days are long past. Similarly, the utility of nwfnet-sntp is decreasing with time. Let it just default as upstream does now. --- net/nwfnet-go.lua | 49 +++++++++++++++++++++++---------------------- net/nwfnet-sntp.lua | 20 +++--------------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/net/nwfnet-go.lua b/net/nwfnet-go.lua index 5f154cc..b2f87b2 100644 --- a/net/nwfnet-go.lua +++ b/net/nwfnet-go.lua @@ -8,6 +8,22 @@ wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function(_) (require "nwf 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 @@ -15,6 +31,7 @@ wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(t) (require "nwf 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}) @@ -33,33 +50,17 @@ if file.open("nwfnet.conf","r") then 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 diff --git a/net/nwfnet-sntp.lua b/net/nwfnet-sntp.lua index ee5b0c7..11ef3ee 100644 --- a/net/nwfnet-sntp.lua +++ b/net/nwfnet-sntp.lua @@ -1,21 +1,7 @@ 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 -- 2.50.1