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