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
|
|
|
|
local crypto, file, json, net, node, table, tmr, wifi =
|
|
|
|
crypto, file, sjson, net, node, table, tmr, wifi
|
2019-02-17 19:26:29 +01:00
|
|
|
local error, pcall = error, pcall
|
2017-10-28 23:22:34 +02:00
|
|
|
local loadfile, gc = loadfile, collectgarbage
|
2019-02-17 19:26:29 +01:00
|
|
|
local concat, unpack = table.concat, unpack or table.unpack
|
2017-10-28 23:22:34 +02:00
|
|
|
|
|
|
|
local self = {post = node.task.post, prefix = "luaOTA/", conf = {}}
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
self.log = (DEBUG == true) and print or function() end
|
|
|
|
self.modname = ...
|
2017-10-28 23:22:34 +02:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------------
|
|
|
|
-- Utility Functions
|
|
|
|
|
|
|
|
setmetatable( self, {__index=function(self, 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
|
2017-10-28 23:22:34 +02:00
|
|
|
func = self.prefix .. func
|
|
|
|
local f,msg = loadfile( func..".lc")
|
|
|
|
if msg then f, msg = loadfile(func..".lua") end
|
|
|
|
if msg then error (msg,2) end
|
|
|
|
if func:sub(8,8) ~= "_" then self[func] = f end
|
|
|
|
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
|
|
|
|
|
|
|
|
function self.startApp(arg) --upval: gc, self, tmr, wifi
|
|
|
|
gc();gc()
|
|
|
|
tmr.unregister(0)
|
|
|
|
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
|