diff --git a/scada-common/log.lua b/scada-common/log.lua index 8ebc987..5a839fd 100644 --- a/scada-common/log.lua +++ b/scada-common/log.lua @@ -20,7 +20,7 @@ local MODE = { APPEND = 0, NEW = 1 } log.MODE = MODE -local logger = { +local _log = { not_ready = true, path = "/log.txt", mode = MODE.APPEND, @@ -42,36 +42,36 @@ local free_space = fs.getFreeSpace ---@param err_msg string|nil error message ---@return boolean out_of_space local function check_out_of_space(err_msg) - return (free_space(logger.path) < MIN_SPACE) or ((err_msg ~= nil) and (string.find(err_msg, OUT_OF_SPACE) ~= nil)) + return (free_space(_log.path) < MIN_SPACE) or ((err_msg ~= nil) and (string.find(err_msg, OUT_OF_SPACE) ~= nil)) end -- private log write function ---@param msg_bits any[] -local function _log(msg_bits) - if logger.not_ready then return end +local function write_log(msg_bits) + if _log.not_ready then return end local time_stamp = os.date(TIME_FMT) local stamped = util.c(time_stamp, table.unpack(msg_bits)) -- attempt to write log local status, result = pcall(function () - logger.file.writeLine(stamped) - logger.file.flush() + _log.file.writeLine(stamped) + _log.file.flush() end) -- if we don't have space, we need to create a new log file if check_out_of_space() then -- delete the old log file before opening a new one - logger.file.close() - fs.delete(logger.path) + _log.file.close() + fs.delete(_log.path) -- re-init logger and pass dmesg_out so that it doesn't change - log.init(logger.path, logger.mode, logger.debug, logger.dmesg_out) + log.init(_log.path, _log.mode, _log.debug, _log.dmesg_out) -- log the message and recycle warning - logger.file.writeLine(time_stamp .. WRN_TAG .. "recycled log file") - logger.file.writeLine(stamped) - logger.file.flush() + _log.file.writeLine(time_stamp .. WRN_TAG .. "recycled log file") + _log.file.writeLine(stamped) + _log.file.flush() elseif (not status) and (result ~= nil) then util.println("unexpected error writing to the log file: " .. result) end @@ -89,45 +89,45 @@ end function log.init(path, write_mode, include_debug, dmesg_redirect) local err_msg - logger.path = path - logger.mode = write_mode - logger.debug = include_debug - logger.file, err_msg = fs.open(path, util.trinary(logger.mode == MODE.APPEND, "a", "w")) + _log.path = path + _log.mode = write_mode + _log.debug = include_debug + _log.file, err_msg = fs.open(path, util.trinary(_log.mode == MODE.APPEND, "a", "w")) if dmesg_redirect then - logger.dmesg_out = dmesg_redirect + _log.dmesg_out = dmesg_redirect else - logger.dmesg_out = term.current() + _log.dmesg_out = term.current() end -- check for space issues local out_of_space = check_out_of_space(err_msg) -- try to handle problems - if logger.file == nil or out_of_space then + if _log.file == nil or out_of_space then if out_of_space then - if fs.exists(logger.path) then - fs.delete(logger.path) + if fs.exists(_log.path) then + fs.delete(_log.path) - logger.file, err_msg = fs.open(path, util.trinary(logger.mode == MODE.APPEND, "a", "w")) + _log.file, err_msg = fs.open(path, util.trinary(_log.mode == MODE.APPEND, "a", "w")) - if logger.file then - logger.file.writeLine(os.date(TIME_FMT) .. WRN_TAG .. "init recycled log file") - logger.file.flush() + if _log.file then + _log.file.writeLine(os.date(TIME_FMT) .. WRN_TAG .. "init recycled log file") + _log.file.flush() else error("failed to setup the log file: " .. err_msg) end else error("failed to make space for the log file, please delete unused files") end else error("unexpected error setting up the log file: " .. err_msg) end end - logger.not_ready = false + _log.not_ready = false end -- close the log file handle -function log.close() logger.file.close() end +function log.close() _log.file.close() end -- direct dmesg output to a monitor/window ---@param window Window window or terminal reference -function log.direct_dmesg(window) logger.dmesg_out = window end +function log.direct_dmesg(window) _log.dmesg_out = window end -- dmesg style logging for boot because I like linux-y things ---@param msg any message @@ -142,7 +142,7 @@ function log.dmesg(msg, tag, tag_color) tag = util.strval(tag or "") local t_stamp = string.format("%12.2f", os.clock()) - local out = logger.dmesg_out + local out = _log.dmesg_out if out ~= nil then local out_w, out_h = out.getSize() @@ -180,7 +180,7 @@ function log.dmesg(msg, tag, tag_color) if cur_y == out_h then out.scroll(1) out.setCursorPos(1, cur_y) - logger.dmesg_scroll_count = logger.dmesg_scroll_count + 1 + _log.dmesg_scroll_count = _log.dmesg_scroll_count + 1 else out.setCursorPos(1, cur_y + 1) end @@ -216,7 +216,7 @@ function log.dmesg(msg, tag, tag_color) if cur_y == out_h then out.scroll(1) out.setCursorPos(1, cur_y) - logger.dmesg_scroll_count = logger.dmesg_scroll_count + 1 + _log.dmesg_scroll_count = _log.dmesg_scroll_count + 1 else out.setCursorPos(1, cur_y + 1) end @@ -225,9 +225,9 @@ function log.dmesg(msg, tag, tag_color) out.write(lines[i]) end - logger.dmesg_restore_coord = { out.getCursorPos() } + _log.dmesg_restore_coord = { out.getCursorPos() } - _log{"[", t_stamp, "] [", tag, "] ", msg} + write_log{"[", t_stamp, "] [", tag, "] ", msg} end return ts_coord @@ -241,9 +241,9 @@ end ---@return function update, function done function log.dmesg_working(msg, tag, tag_color) local ts_coord = log.dmesg(msg, tag, tag_color) - local initial_scroll = logger.dmesg_scroll_count + local initial_scroll = _log.dmesg_scroll_count - local out = logger.dmesg_out + local out = _log.dmesg_out local width = (ts_coord.x2 - ts_coord.x1) + 1 if out ~= nil then @@ -252,7 +252,7 @@ function log.dmesg_working(msg, tag, tag_color) local counter = 0 local function update(sec_remaining) - local new_y = ts_coord.y - (logger.dmesg_scroll_count - initial_scroll) + local new_y = ts_coord.y - (_log.dmesg_scroll_count - initial_scroll) if new_y < 1 then return end local time = util.sprintf("%ds", sec_remaining) @@ -280,11 +280,11 @@ function log.dmesg_working(msg, tag, tag_color) counter = counter + 1 - out.setCursorPos(table.unpack(logger.dmesg_restore_coord)) + out.setCursorPos(table.unpack(_log.dmesg_restore_coord)) end local function done(ok) - local new_y = ts_coord.y - (logger.dmesg_scroll_count - initial_scroll) + local new_y = ts_coord.y - (_log.dmesg_scroll_count - initial_scroll) if new_y < 1 then return end out.setCursorPos(ts_coord.x1, new_y) @@ -299,7 +299,7 @@ function log.dmesg_working(msg, tag, tag_color) out.setTextColor(initial_color) - out.setCursorPos(table.unpack(logger.dmesg_restore_coord)) + out.setCursorPos(table.unpack(_log.dmesg_restore_coord)) end return update, done @@ -312,28 +312,28 @@ end ---@param msg any message ---@param trace? boolean include file trace function log.debug(msg, trace) - if logger.debug then + if _log.debug then if trace then local info = debug.getinfo(2) if info.name ~= nil then - _log{DBG_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg} + write_log{DBG_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg} else - _log{DBG_TAG, info.short_src, COLON, info.currentline, ARROW, msg} + write_log{DBG_TAG, info.short_src, COLON, info.currentline, ARROW, msg} end else - _log{DBG_TAG, msg} + write_log{DBG_TAG, msg} end end end -- log info messages ---@param msg any message -function log.info(msg) _log{INF_TAG, msg} end +function log.info(msg) write_log{INF_TAG, msg} end -- log warning messages ---@param msg any message -function log.warning(msg) _log{WRN_TAG, msg} end +function log.warning(msg) write_log{WRN_TAG, msg} end -- log error messages ---@param msg any message @@ -343,17 +343,17 @@ function log.error(msg, trace) local info = debug.getinfo(2) if info.name ~= nil then - _log{ERR_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg} + write_log{ERR_TAG, info.short_src, COLON, info.name, FUNC, info.currentline, ARROW, msg} else - _log{ERR_TAG, info.short_src, COLON, info.currentline, ARROW, msg} + write_log{ERR_TAG, info.short_src, COLON, info.currentline, ARROW, msg} end else - _log{ERR_TAG, msg} + write_log{ERR_TAG, msg} end end -- log fatal errors ---@param msg any message -function log.fatal(msg) _log{FTL_TAG, msg} end +function log.fatal(msg) write_log{FTL_TAG, msg} end return log diff --git a/scada-common/network.lua b/scada-common/network.lua index 1891f5e..317d517 100644 --- a/scada-common/network.lua +++ b/scada-common/network.lua @@ -1,5 +1,5 @@ -- --- Network Communications +-- Network Communications and Message Authentication -- local comms = require("scada-common.comms") @@ -18,7 +18,7 @@ local array = require("lockbox.util.array") local network = {} -- cryptography engine -local c_eng = { +local _crypt = { key = nil, hmac = nil } @@ -40,13 +40,13 @@ function network.init_mac(passkey) key_deriv.setPassword(passkey) key_deriv.finish() - c_eng.key = array.fromHex(key_deriv.asHex()) + _crypt.key = array.fromHex(key_deriv.asHex()) -- initialize HMAC - c_eng.hmac = hmac() - c_eng.hmac.setBlockSize(64) - c_eng.hmac.setDigest(md5) - c_eng.hmac.setKey(c_eng.key) + _crypt.hmac = hmac() + _crypt.hmac.setBlockSize(64) + _crypt.hmac.setDigest(md5) + _crypt.hmac.setKey(_crypt.key) local init_time = util.time_ms() - start log.info("NET: network.init_mac completed in " .. init_time .. "ms") @@ -56,7 +56,7 @@ end -- de-initialize message authentication system function network.deinit_mac() - c_eng.key, c_eng.hmac = nil, nil + _crypt.key, _crypt.hmac = nil, nil end -- generate HMAC of message @@ -65,11 +65,11 @@ end local function compute_hmac(message) -- local start = util.time_ms() - c_eng.hmac.init() - c_eng.hmac.update(stream.fromString(message)) - c_eng.hmac.finish() + _crypt.hmac.init() + _crypt.hmac.update(stream.fromString(message)) + _crypt.hmac.finish() - local hash = c_eng.hmac.asHex() + local hash = _crypt.hmac.asHex() -- log.debug("NET: compute_hmac(): hmac-md5 = " .. util.strval(hash) .. " (took " .. (util.time_ms() - start) .. "ms)") @@ -112,7 +112,7 @@ function network.nic(modem) self.iface = ppm.get_iface(modem) self.name = util.c(util.trinary(modem.isWireless(), "WLAN_PHY", "ETH_PHY"), "{", self.iface, "}") self.connected = true - self.use_hash = c_eng.hmac and modem.isWireless() + self.use_hash = _crypt.hmac and modem.isWireless() -- open only previously opened channels modem.closeAll() diff --git a/scada-common/ppm.lua b/scada-common/ppm.lua index ea310a3..faf74a6 100644 --- a/scada-common/ppm.lua +++ b/scada-common/ppm.lua @@ -22,7 +22,7 @@ ppm.VIRTUAL_DEVICE_TYPE = VIRTUAL_DEVICE_TYPE local REPORT_FREQUENCY = 20 -- log every 20 faults per function -local ppm_sys = { +local _ppm = { mounts = {}, ---@type { [string]: ppm_entry } next_vid = 0, auto_cf = false, @@ -66,7 +66,7 @@ local function peri_init(iface) if status then -- auto fault clear if self.auto_cf then self.faulted = false end - if ppm_sys.auto_cf then ppm_sys.faulted = false end + if _ppm.auto_cf then _ppm.faulted = false end self.fault_counts[key] = 0 @@ -78,10 +78,10 @@ local function peri_init(iface) self.faulted = true self.last_fault = result - ppm_sys.faulted = true - ppm_sys.last_fault = result + _ppm.faulted = true + _ppm.last_fault = result - if not ppm_sys.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then + if not _ppm.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then local count_str = "" if self.fault_counts[key] > 0 then count_str = " [" .. self.fault_counts[key] .. " total faults]" @@ -92,7 +92,7 @@ local function peri_init(iface) self.fault_counts[key] = self.fault_counts[key] + 1 - if result == "Terminated" then ppm_sys.terminate = true end + if result == "Terminated" then _ppm.terminate = true end return ACCESS_FAULT, result end @@ -159,10 +159,10 @@ local function peri_init(iface) self.faulted = true self.last_fault = UNDEFINED_FIELD - ppm_sys.faulted = true - ppm_sys.last_fault = UNDEFINED_FIELD + _ppm.faulted = true + _ppm.last_fault = UNDEFINED_FIELD - if not ppm_sys.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then + if not _ppm.mute and (self.fault_counts[key] % REPORT_FREQUENCY == 0) then local count_str = "" if self.fault_counts[key] > 0 then count_str = " [" .. self.fault_counts[key] .. " total calls]" @@ -193,35 +193,35 @@ end -- REPORTING -- -- silence error prints -function ppm.disable_reporting() ppm_sys.mute = true end +function ppm.disable_reporting() _ppm.mute = true end -- allow error prints -function ppm.enable_reporting() ppm_sys.mute = false end +function ppm.enable_reporting() _ppm.mute = false end -- FAULT MEMORY -- -- enable automatically clearing fault flag -function ppm.enable_afc() ppm_sys.auto_cf = true end +function ppm.enable_afc() _ppm.auto_cf = true end -- disable automatically clearing fault flag -function ppm.disable_afc() ppm_sys.auto_cf = false end +function ppm.disable_afc() _ppm.auto_cf = false end -- clear fault flag -function ppm.clear_fault() ppm_sys.faulted = false end +function ppm.clear_fault() _ppm.faulted = false end -- check fault flag ---@nodiscard -function ppm.is_faulted() return ppm_sys.faulted end +function ppm.is_faulted() return _ppm.faulted end -- get the last fault message ---@nodiscard -function ppm.get_last_fault() return ppm_sys.last_fault end +function ppm.get_last_fault() return _ppm.last_fault end -- TERMINATION -- -- if a caught error was a termination request ---@nodiscard -function ppm.should_terminate() return ppm_sys.terminate end +function ppm.should_terminate() return _ppm.terminate end -- MOUNTING -- @@ -229,12 +229,12 @@ function ppm.should_terminate() return ppm_sys.terminate end function ppm.mount_all() local ifaces = peripheral.getNames() - ppm_sys.mounts = {} + _ppm.mounts = {} for i = 1, #ifaces do - ppm_sys.mounts[ifaces[i]] = peri_init(ifaces[i]) + _ppm.mounts[ifaces[i]] = peri_init(ifaces[i]) - log.info(util.c("PPM: found a ", ppm_sys.mounts[ifaces[i]].type, " (", ifaces[i], ")")) + log.info(util.c("PPM: found a ", _ppm.mounts[ifaces[i]].type, " (", ifaces[i], ")")) end if #ifaces == 0 then @@ -253,10 +253,10 @@ function ppm.mount(iface) for i = 1, #ifaces do if iface == ifaces[i] then - ppm_sys.mounts[iface] = peri_init(iface) + _ppm.mounts[iface] = peri_init(iface) - pm_type = ppm_sys.mounts[iface].type - pm_dev = ppm_sys.mounts[iface].dev + pm_type = _ppm.mounts[iface].type + pm_dev = _ppm.mounts[iface].dev log.info(util.c("PPM: mount(", iface, ") -> found a ", pm_type)) break @@ -278,12 +278,12 @@ function ppm.remount(iface) for i = 1, #ifaces do if iface == ifaces[i] then log.info(util.c("PPM: remount(", iface, ") -> is a ", pm_type)) - ppm.unmount(ppm_sys.mounts[iface].dev) + ppm.unmount(_ppm.mounts[iface].dev) - ppm_sys.mounts[iface] = peri_init(iface) + _ppm.mounts[iface] = peri_init(iface) - pm_type = ppm_sys.mounts[iface].type - pm_dev = ppm_sys.mounts[iface].dev + pm_type = _ppm.mounts[iface].type + pm_dev = _ppm.mounts[iface].dev log.info(util.c("PPM: remount(", iface, ") -> remounted a ", pm_type)) break @@ -297,24 +297,24 @@ end ---@nodiscard ---@return string type, table device function ppm.mount_virtual() - local iface = "ppm_vdev_" .. ppm_sys.next_vid + local iface = "ppm_vdev_" .. _ppm.next_vid - ppm_sys.mounts[iface] = peri_init("__virtual__") - ppm_sys.next_vid = ppm_sys.next_vid + 1 + _ppm.mounts[iface] = peri_init("__virtual__") + _ppm.next_vid = _ppm.next_vid + 1 log.info(util.c("PPM: mount_virtual() -> allocated new virtual device ", iface)) - return ppm_sys.mounts[iface].type, ppm_sys.mounts[iface].dev + return _ppm.mounts[iface].type, _ppm.mounts[iface].dev end -- manually unmount a peripheral from the PPM ---@param device table device table function ppm.unmount(device) if device then - for iface, data in pairs(ppm_sys.mounts) do + for iface, data in pairs(_ppm.mounts) do if data.dev == device then log.warning(util.c("PPM: manually unmounted ", data.type, " mounted to ", iface)) - ppm_sys.mounts[iface] = nil + _ppm.mounts[iface] = nil break end end @@ -330,7 +330,7 @@ function ppm.handle_unmount(iface) local pm_type = nil -- what got disconnected? - local lost_dev = ppm_sys.mounts[iface] + local lost_dev = _ppm.mounts[iface] if lost_dev then pm_type = lost_dev.type @@ -341,18 +341,18 @@ function ppm.handle_unmount(iface) log.error(util.c("PPM: lost device unknown to the PPM mounted to ", iface)) end - ppm_sys.mounts[iface] = nil + _ppm.mounts[iface] = nil return pm_type, pm_dev end -- log all mounts, to be used if `ppm.mount_all` is called before logging is ready function ppm.log_mounts() - for iface, mount in pairs(ppm_sys.mounts) do + for iface, mount in pairs(_ppm.mounts) do log.info(util.c("PPM: had found a ", mount.type, " (", iface, ")")) end - if util.table_len(ppm_sys.mounts) == 0 then + if util.table_len(_ppm.mounts) == 0 then log.warning("PPM: no devices had been found") end end @@ -369,7 +369,7 @@ function ppm.list_avail() return peripheral.getNames() end ---@return { [string]: ppm_entry } mounts function ppm.list_mounts() local list = {} - for k, v in pairs(ppm_sys.mounts) do list[k] = v end + for k, v in pairs(_ppm.mounts) do list[k] = v end return list end @@ -379,7 +379,7 @@ end ---@return string|nil iface CC peripheral interface function ppm.get_iface(device) if device then - for iface, data in pairs(ppm_sys.mounts) do + for iface, data in pairs(_ppm.mounts) do if data.dev == device then return iface end end end @@ -392,8 +392,8 @@ end ---@param iface string CC peripheral interface ---@return { [string]: function }|nil device function table function ppm.get_periph(iface) - if ppm_sys.mounts[iface] then - return ppm_sys.mounts[iface].dev + if _ppm.mounts[iface] then + return _ppm.mounts[iface].dev else return nil end end @@ -402,8 +402,8 @@ end ---@param iface string CC peripheral interface ---@return string|nil type function ppm.get_type(iface) - if ppm_sys.mounts[iface] then - return ppm_sys.mounts[iface].type + if _ppm.mounts[iface] then + return _ppm.mounts[iface].type else return nil end end @@ -414,7 +414,7 @@ end function ppm.get_all_devices(name) local devices = {} - for _, data in pairs(ppm_sys.mounts) do + for _, data in pairs(_ppm.mounts) do if data.type == name then table.insert(devices, data.dev) end @@ -430,7 +430,7 @@ end function ppm.get_device(name) local device = nil - for _, data in pairs(ppm_sys.mounts) do + for _, data in pairs(_ppm.mounts) do if data.type == name then device = data.dev break @@ -455,7 +455,7 @@ function ppm.get_wireless_modem() local w_modem = nil local emulated_env = periphemu ~= nil - for _, device in pairs(ppm_sys.mounts) do + for _, device in pairs(_ppm.mounts) do if device.type == "modem" and (emulated_env or device.dev.isWireless()) then w_modem = device.dev break @@ -471,7 +471,7 @@ end function ppm.get_monitor_list() local list = {} - for iface, device in pairs(ppm_sys.mounts) do + for iface, device in pairs(_ppm.mounts) do if device.type == "monitor" then list[iface] = device end end