nodemcu-firmware/lua_examples/luaOTA/check.lua

70 lines
2.4 KiB
Lua
Raw Normal View History

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, 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 = {}}
-- luacheck: globals DEBUG
2019-02-17 19:26:29 +01:00
self.log = (DEBUG == true) and print or function() end
self.modname = ...
self.timer = tmr.create()
2017-10-28 23:22:34 +02:00
_G["stopOTA"] = function()
self.timer:stop()
end
2017-10-28 23:22:34 +02:00
--------------------------------------------------------------------------------------
-- Utility Functions
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
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
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
function self.startApp(arg) --upval: gc, self, wifi
2017-10-28 23:22:34 +02:00
gc();gc()
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