From 454d166ac93ac030d3480e37077621875e1db104 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Mon, 5 May 2025 17:54:47 +0000 Subject: [PATCH] #580 reactor PLC changes for wired comms modems --- reactor-plc/configure.lua | 4 +++- reactor-plc/plc.lua | 6 +++++- reactor-plc/startup.lua | 17 ++++++++++++----- reactor-plc/threads.lua | 18 ++++++++++-------- scada-common/ppm.lua | 13 +++++++++++++ 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/reactor-plc/configure.lua b/reactor-plc/configure.lua index b2baa6d..651b57e 100644 --- a/reactor-plc/configure.lua +++ b/reactor-plc/configure.lua @@ -79,8 +79,9 @@ local tmp_cfg = { SVR_Channel = nil, ---@type integer PLC_Channel = nil, ---@type integer ConnTimeout = nil, ---@type number + WiredModem = false, ---@type string|false TrustedRange = nil, ---@type number - AuthKey = nil, ---@type string|nil + AuthKey = "", ---@type string LogMode = 0, ---@type LOG_MODE LogPath = "", LogDebug = false, @@ -103,6 +104,7 @@ local fields = { { "SVR_Channel", "SVR Channel", 16240 }, { "PLC_Channel", "PLC Channel", 16241 }, { "ConnTimeout", "Connection Timeout", 5 }, + { "WiredModem", "Wired Modem", false }, { "TrustedRange", "Trusted Range", 0 }, { "AuthKey", "Facility Auth Key" , ""}, { "LogMode", "Log Mode", log.MODE.APPEND }, diff --git a/reactor-plc/plc.lua b/reactor-plc/plc.lua index 73b95ce..c9af63a 100644 --- a/reactor-plc/plc.lua +++ b/reactor-plc/plc.lua @@ -47,6 +47,7 @@ function plc.load_config() config.SVR_Channel = settings.get("SVR_Channel") config.PLC_Channel = settings.get("PLC_Channel") config.ConnTimeout = settings.get("ConnTimeout") + config.WiredModem = settings.get("WiredModem") config.TrustedRange = settings.get("TrustedRange") config.AuthKey = settings.get("AuthKey") @@ -74,6 +75,7 @@ function plc.validate_config(cfg) cfv.assert_channel(cfg.PLC_Channel) cfv.assert_type_num(cfg.ConnTimeout) cfv.assert_min(cfg.ConnTimeout, 2) + cfv.assert((cfg.WiredModem == false) or (type(cfg.WiredModem) == "string")) cfv.assert_type_num(cfg.TrustedRange) cfv.assert_min(cfg.TrustedRange, 0) cfv.assert_type_str(cfg.AuthKey) @@ -542,7 +544,9 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog) max_burn_rate = nil } - comms.set_trusted_range(config.TrustedRange) + if nic.isWireless() then + comms.set_trusted_range(config.TrustedRange) + end -- PRIVATE FUNCTIONS -- diff --git a/reactor-plc/startup.lua b/reactor-plc/startup.lua index c085e95..1ce7682 100644 --- a/reactor-plc/startup.lua +++ b/reactor-plc/startup.lua @@ -18,7 +18,7 @@ local plc = require("reactor-plc.plc") local renderer = require("reactor-plc.renderer") local threads = require("reactor-plc.threads") -local R_PLC_VERSION = "v1.8.20" +local R_PLC_VERSION = "v1.9.0" local println = util.println local println_ts = util.println_ts @@ -106,7 +106,9 @@ local function main() -- core PLC devices plc_dev = { reactor = ppm.get_fission_reactor(), - modem = ppm.get_wireless_modem() + modem_wired = type(config.WiredModem) == "string", + modem_iface = config.WiredModem, + modem = nil }, -- system objects @@ -130,6 +132,11 @@ local function main() local plc_state = __shared_memory.plc_state + -- get the configured modem + if smem_dev.modem_wired then + smem_dev.modem = ppm.get_wired_modem(smem_dev.modem_iface) + else smem_dev.modem = ppm.get_wireless_modem() end + -- initial state evaluation plc_state.no_reactor = smem_dev.reactor == nil plc_state.no_modem = smem_dev.modem == nil @@ -149,10 +156,10 @@ local function main() plc_state.reactor_formed = false end - -- modem is required if networked + -- comms modem is required if networked if __shared_memory.networked and plc_state.no_modem then - println("init> wireless modem not found") - log.warning("init> no wireless modem on startup") + println("init> comms modem not found") + log.warning("init> no comms modem on startup") -- scram reactor if present and enabled if (smem_dev.reactor ~= nil) and plc_state.reactor_formed and smem_dev.reactor.getStatus() then diff --git a/reactor-plc/threads.lua b/reactor-plc/threads.lua index b56ccc7..dadb80d 100644 --- a/reactor-plc/threads.lua +++ b/reactor-plc/threads.lua @@ -145,7 +145,7 @@ function threads.thread__main(smem, init) plc_state.degraded = true elseif networked and type == "modem" then ---@cast device Modem - -- we only care if this is our wireless modem + -- we only care if this is our comms modem -- note, check init_ok first since nic will be nil if it is false if plc_state.init_ok and nic.is_modem(device) then nic.disconnect() @@ -154,7 +154,7 @@ function threads.thread__main(smem, init) log.warning("comms modem disconnected") local other_modem = ppm.get_wireless_modem() - if other_modem then + if other_modem and not plc_dev.modem_wired then log.info("found another wireless modem, using it for comms") nic.connect(other_modem) else @@ -167,7 +167,7 @@ function threads.thread__main(smem, init) end end else - log.warning("a modem was disconnected") + log.warning("a non-comms modem was disconnected") end end end @@ -210,15 +210,17 @@ function threads.thread__main(smem, init) end elseif networked and type == "modem" then ---@cast device Modem - -- note, check init_ok first since nic will be nil if it is false - if device.isWireless() and not (plc_state.init_ok and nic.is_connected()) then + local is_comms_modem = util.trinary(plc_dev.modem_wired, plc_dev.modem_iface == param1, device.isWireless()) + + -- note, check init_ok since nic will be nil if it is false + if is_comms_modem and not (plc_state.init_ok and nic.is_connected()) then -- reconnected modem plc_dev.modem = device plc_state.no_modem = false if plc_state.init_ok then nic.connect(device) end - println_ts("wireless modem reconnected.") + println_ts("comms modem reconnected.") log.info("comms modem reconnected") -- determine if we are still in a degraded state @@ -226,9 +228,9 @@ function threads.thread__main(smem, init) plc_state.degraded = false end elseif device.isWireless() then - log.info("unused wireless modem reconnected") + log.info("unused wireless modem connected") else - log.info("wired modem reconnected") + log.info("non-comms wired modem connected") end end end diff --git a/scada-common/ppm.lua b/scada-common/ppm.lua index ea310a3..894e643 100644 --- a/scada-common/ppm.lua +++ b/scada-common/ppm.lua @@ -447,6 +447,19 @@ end ---@return table|nil reactor function table function ppm.get_fission_reactor() return ppm.get_device("fissionReactorLogicAdapter") end +-- get the named wired modem +---@nodiscard +---@param iface string CC peripheral interface +---@return Modem|nil modem function table +function ppm.get_wired_modem(iface) + local modem = nil + local device = ppm_sys.mounts[iface] + + if device.type == "modem" then modem = device.dev end + + return modem +end + -- get the wireless modem (if multiple, returns the first)
-- if this is in a CraftOS emulated environment, wired modems will be used instead ---@nodiscard