2017-10-28 23:22:34 +02:00
|
|
|
--SAFETRIM
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- LuaOTA provisioning system for ESPs using NodeMCU Lua
|
|
|
|
-- LICENCE: http://opensource.org/licenses/MIT
|
|
|
|
-- TerryE 15 Jul 2017
|
|
|
|
--
|
|
|
|
-- See luaOTA.md for description and implementation notes
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- upvals
|
2019-12-31 00:53:54 +01:00
|
|
|
local crypto, json, node, wifi =
|
|
|
|
crypto, sjson, node, wifi
|
|
|
|
local error = error
|
2017-10-28 23:22:34 +02:00
|
|
|
local loadfile, gc = loadfile, collectgarbage
|
|
|
|
|
|
|
|
local self = {post = node.task.post, prefix = "luaOTA/", conf = {}}
|
2019-12-31 00:53:54 +01:00
|
|
|
-- luacheck: globals DEBUG
|
2019-02-17 19:26:29 +01:00
|
|
|
self.log = (DEBUG == true) and print or function() end
|
|
|
|
self.modname = ...
|
2019-05-17 14:13:49 +02:00
|
|
|
self.timer = tmr.create()
|
2017-10-28 23:22:34 +02:00
|
|
|
|
2019-05-17 14:13:49 +02:00
|
|
|
_G["stopOTA"] = function()
|
|
|
|
self.timer:stop()
|
|
|
|
end
|
2017-10-28 23:22:34 +02:00
|
|
|
--------------------------------------------------------------------------------------
|
|
|
|
-- Utility Functions
|
|
|
|
|
2019-12-31 00:53:54 +01:00
|
|
|
setmetatable( self, {__index=function(obj, func) --upval: loadfile
|
2019-02-17 19:26:29 +01:00
|
|
|
-- The only __index calls in in LuaOTA are dynamically loaded functions.
|
|
|
|
-- The convention is that functions starting with "_" are treated as
|
|
|
|
-- call-once / ephemeral; the rest are registered in self
|
2019-12-31 00:53:54 +01:00
|
|
|
func = obj.prefix .. func
|
2017-10-28 23:22:34 +02:00
|
|
|
local f,msg = loadfile( func..".lc")
|
|
|
|
if msg then f, msg = loadfile(func..".lua") end
|
|
|
|
if msg then error (msg,2) end
|
2019-12-31 00:53:54 +01:00
|
|
|
if func:sub(8,8) ~= "_" then obj[func] = f end
|
2017-10-28 23:22:34 +02:00
|
|
|
return f
|
|
|
|
end} )
|
|
|
|
|
|
|
|
function self.sign(arg) --upval: crypto, json, self
|
|
|
|
arg = json.encode(arg)
|
|
|
|
return arg .. crypto.toHex(crypto.hmac("MD5", arg, self.secret):sub(-3)) .. '\n'
|
|
|
|
end
|
|
|
|
|
2019-05-17 14:13:49 +02:00
|
|
|
function self.startApp(arg) --upval: gc, self, wifi
|
2017-10-28 23:22:34 +02:00
|
|
|
gc();gc()
|
2019-05-17 14:13:49 +02:00
|
|
|
self.timer.unregister()
|
2017-10-28 23:22:34 +02:00
|
|
|
self.socket = nil
|
|
|
|
if not self.config.leave then wifi.setmode(wifi.NULLMODE,false) end
|
|
|
|
local appMod = self.config.app or "luaOTA.default"
|
|
|
|
local appMethod = self.config.entry or "entry"
|
|
|
|
if not arg then arg = "General timeout on provisioning" end
|
2019-02-17 19:26:29 +01:00
|
|
|
self.post(function() --upval: appMod, appMethod, arg
|
2017-10-28 23:22:34 +02:00
|
|
|
require(appMod)[appMethod](arg)
|
|
|
|
end)
|
2019-02-17 19:26:29 +01:00
|
|
|
end
|
2017-10-28 23:22:34 +02:00
|
|
|
|
|
|
|
function self.socket_send(socket, rec, opt_buffer)
|
|
|
|
return socket:send(self.sign(rec) .. (opt_buffer or ''))
|
|
|
|
end
|
|
|
|
|
|
|
|
self.post(function() -- upval: self
|
2019-02-17 19:26:29 +01:00
|
|
|
-- This config check is to prevent a double execution if the
|
|
|
|
-- user invokes with "require 'luaOTA/check':_init( etc>)" form
|
|
|
|
if not rawget(self, "config") then self:_init() end
|
2017-10-28 23:22:34 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
return self
|