From d7a280bb04fab2eddd39a774c3c82ff261340a17 Mon Sep 17 00:00:00 2001 From: Mikayla Date: Sun, 2 Nov 2025 17:10:01 +0000 Subject: [PATCH] #634 reactor PLC backplane and front panel updates --- reactor-plc/backplane.lua | 135 ++++++++++++++++++++++-------- reactor-plc/databus.lua | 21 ++--- reactor-plc/panel/front_panel.lua | 30 ++++--- reactor-plc/startup.lua | 4 +- reactor-plc/threads.lua | 59 +++---------- 5 files changed, 141 insertions(+), 108 deletions(-) diff --git a/reactor-plc/backplane.lua b/reactor-plc/backplane.lua index f5e2230..e384e50 100644 --- a/reactor-plc/backplane.lua +++ b/reactor-plc/backplane.lua @@ -7,9 +7,6 @@ local network = require("scada-common.network") local ppm = require("scada-common.ppm") local util = require("scada-common.util") -local databus = require("reactor-plc.databus") -local plc = require("reactor-plc.plc") - local println = util.println ---@class plc_backplane @@ -22,7 +19,6 @@ local _bp = { lan_iface = "", act_nic = nil, ---@type nic - wl_act = true, wd_nic = nil, ---@type nic|nil wl_nic = nil ---@type nic|nil } @@ -44,32 +40,33 @@ function backplane.init(config, __shared_memory) if _bp.smem.networked then -- init wired NIC if type(config.WiredModem) == "string" then - local modem = ppm.get_modem(_bp.lan_iface) - _bp.wd_nic = network.nic(modem) + local modem = ppm.get_modem(_bp.lan_iface) + local wd_nic = network.nic(modem) log.info("BKPLN: WIRED PHY_" .. util.trinary(modem, "UP ", "DOWN ") .. _bp.lan_iface) - plc_state.wd_modem = _bp.wd_nic.is_connected() + plc_state.wd_modem = wd_nic.is_connected() -- set this as active for now - _bp.wl_act = false - _bp.act_nic = _bp.wd_nic + _bp.act_nic = wd_nic + _bp.wd_nic = wd_nic end -- init wireless NIC(s) if config.WirelessModem then local modem, iface = ppm.get_wireless_modem() - _bp.wl_nic = network.nic(modem) + local wl_nic = network.nic(modem) log.info("BKPLN: WIRELESS PHY_" .. util.trinary(modem, "UP ", "DOWN ") .. iface) - plc_state.wl_modem = _bp.wl_nic.is_connected() + plc_state.wl_modem = wl_nic.is_connected() -- set this as active if connected or if both modems are disconnected and this is preferred if (modem and _bp.wlan_pref) or not (_bp.act_nic and _bp.act_nic.is_connected()) then - _bp.wl_act = true - _bp.act_nic = _bp.wl_nic + _bp.act_nic = wl_nic end + + _bp.wl_nic = wl_nic end -- comms modem is required if networked @@ -123,6 +120,8 @@ function backplane.active_nic() return _bp.act_nic end function backplane.attach(iface, type, device, print_no_fp) local MQ__RPS_CMD = _bp.smem.q_cmds.MQ__RPS_CMD + local wl_nic, wd_nic = _bp.wl_nic, _bp.wd_nic + local networked = _bp.smem.networked local state = _bp.smem.plc_state local dev = _bp.smem.plc_dev @@ -163,52 +162,39 @@ function backplane.attach(iface, type, device, print_no_fp) log.info(util.c("BKPLN: ", util.trinary(m_is_wl, "WIRELESS", "WIRED"), " PHY_ATTACH ", iface)) - local is_wd = _bp.wd_nic and (_bp.lan_iface == iface) - local is_wl = _bp.wl_nic and (not _bp.wl_nic.is_connected()) and m_is_wl - - if is_wd then + if wd_nic and (_bp.lan_iface == iface) then -- connect this as the wired NIC - _bp.wd_nic.connect(device) + wd_nic.connect(device) log.info("BKPLN: WIRED PHY_UP " .. iface) print_no_fp("wired comms modem reconnected") state.wd_modem = true - if _bp.act_nic == _bp.wd_nic then - -- set as active - _bp.wl_act = false - _bp.act_nic = _bp.wd_nic - elseif _bp.wl_act and not _bp.wlan_pref then + if (_bp.act_nic ~= wd_nic) and not _bp.wlan_pref then -- switch back to preferred wired - _bp.wl_act = false - _bp.act_nic = _bp.wd_nic + _bp.act_nic = wd_nic sys.plc_comms.switch_nic(_bp.act_nic) log.info("BKPLN: switched comms to wired modem (preferred)") end - elseif is_wl then + elseif wl_nic and (not wl_nic.is_connected()) and m_is_wl then -- connect this as the wireless NIC - _bp.wl_nic.connect(device) + wl_nic.connect(device) log.info("BKPLN: WIRELESS PHY_UP " .. iface) print_no_fp("wireless comms modem reconnected") state.wl_modem = true - if _bp.act_nic == _bp.wl_nic then - -- set as active - _bp.wl_act = true - _bp.act_nic = _bp.wl_nic - elseif (not _bp.wl_act) and _bp.wlan_pref then + if (_bp.act_nic ~= wl_nic) and _bp.wlan_pref then -- switch back to preferred wireless - _bp.wl_act = true - _bp.act_nic = _bp.wl_nic + _bp.act_nic = wl_nic sys.plc_comms.switch_nic(_bp.act_nic) log.info("BKPLN: switched comms to wireless modem (preferred)") end - elseif _bp.wl_nic and m_is_wl then + elseif wl_nic and m_is_wl then -- the wireless NIC already has a modem print_no_fp("standby wireless modem connected") log.info("BKPLN: standby wireless modem connected") @@ -231,6 +217,85 @@ end ---@param device table ---@param print_no_fp function function backplane.detach(iface, type, device, print_no_fp) + local MQ__RPS_CMD = _bp.smem.q_cmds.MQ__RPS_CMD + + local wl_nic, wd_nic = _bp.wl_nic, _bp.wd_nic + + local state = _bp.smem.plc_state + local dev = _bp.smem.plc_dev + local sys = _bp.smem.plc_sys + + if device == dev.reactor then + print_no_fp("reactor disconnected") + log.warning("BKPLN: reactor disconnected") + + state.no_reactor = true + state.degraded = true + elseif _bp.smem.networked and type == "modem" then + ---@cast device Modem + + local m_is_wl = device.isWireless() + + log.info(util.c("BKPLN: ", util.trinary(m_is_wl, "WIRELESS", "WIRED"), " PHY_DETACH ", iface)) + + if wd_nic and wd_nic.is_modem(device) then + wd_nic.disconnect() + log.info("BKPLN: WIRED PHY_DOWN " .. iface) + + state.wd_modem = false + elseif wl_nic and wl_nic.is_modem(device) then + wl_nic.disconnect() + log.info("BKPLN: WIRELESS PHY_DOWN " .. iface) + + state.wl_modem = false + end + + -- we only care if this is our active comms modem + if _bp.act_nic.is_modem(device) then + print_no_fp("active comms modem disconnected") + log.warning("BKPLN: active comms modem disconnected") + + -- failover and try to find a new comms modem + if _bp.act_nic == wl_nic then + -- wireless active disconnected + -- try to find another wireless modem, otherwise switch to wired + local modem, m_iface = ppm.get_wireless_modem() + if wl_nic and modem then + log.info("BKPLN: found another wireless modem, using it for comms") + + wl_nic.connect(modem) + + log.info("BKPLN: WIRELESS PHY_UP " .. m_iface) + elseif wd_nic and wd_nic.is_connected() then + _bp.act_nic = wd_nic + + sys.plc_comms.switch_nic(_bp.act_nic) + log.info("BKPLN: switched comms to wired modem") + else + -- no other wireless modems, wired unavailable + state.degraded = true + _bp.smem.q.mq_rps.push_command(MQ__RPS_CMD.DEGRADED_SCRAM) + end + elseif wl_nic and wl_nic.is_connected() then + -- wired active disconnected, wireless available + _bp.act_nic = wl_nic + + sys.plc_comms.switch_nic(_bp.act_nic) + log.info("BKPLN: switched comms to wireless modem") + else + -- wired active disconnected, wireless unavailable + state.degraded = true + _bp.smem.q.mq_rps.push_command(MQ__RPS_CMD.DEGRADED_SCRAM) + end + elseif wl_nic and m_is_wl then + -- wireless, but not active + print_no_fp("standby wireless modem disconnected") + log.info("BKPLN: standby wireless modem disconnected") + else + print_no_fp("unassigned modem disconnected") + log.warning("BKPLN: unassigned modem disconnected") + end + end end return backplane diff --git a/reactor-plc/databus.lua b/reactor-plc/databus.lua index 98c1671..32d3c9f 100644 --- a/reactor-plc/databus.lua +++ b/reactor-plc/databus.lua @@ -43,36 +43,29 @@ end -- transmit unit ID across the bus ---@param id integer unit ID -function databus.tx_id(id) - databus.ps.publish("unit_id", id) -end +function databus.tx_id(id) databus.ps.publish("unit_id", id) end -- transmit hardware status across the bus ---@param plc_state plc_state function databus.tx_hw_status(plc_state) - databus.ps.publish("reactor_dev_state", util.trinary(plc_state.no_reactor, 1, util.trinary(plc_state.reactor_formed, 3, 2))) - databus.ps.publish("has_modem", not plc_state.no_modem) databus.ps.publish("degraded", plc_state.degraded) + databus.ps.publish("reactor_dev_state", util.trinary(plc_state.no_reactor, 1, util.trinary(plc_state.reactor_formed, 3, 2))) + databus.ps.publish("has_wd_modem", plc_state.wd_modem) + databus.ps.publish("has_wl_modem", plc_state.wl_modem) end -- transmit thread (routine) statuses ---@param thread string thread name ---@param ok boolean thread state -function databus.tx_rt_status(thread, ok) - databus.ps.publish(util.c("routine__", thread), ok) -end +function databus.tx_rt_status(thread, ok) databus.ps.publish(util.c("routine__", thread), ok) end -- transmit supervisor link state across the bus ---@param state integer -function databus.tx_link_state(state) - databus.ps.publish("link_state", state) -end +function databus.tx_link_state(state) databus.ps.publish("link_state", state) end -- transmit reactor enable state across the bus ---@param active any reactor active -function databus.tx_reactor_state(active) - databus.ps.publish("reactor_active", active == true) -end +function databus.tx_reactor_state(active) databus.ps.publish("reactor_active", active == true) end -- transmit RPS data across the bus ---@param tripped boolean RPS tripped diff --git a/reactor-plc/panel/front_panel.lua b/reactor-plc/panel/front_panel.lua index dfd9d19..c3e81a7 100644 --- a/reactor-plc/panel/front_panel.lua +++ b/reactor-plc/panel/front_panel.lua @@ -35,7 +35,8 @@ local ind_red = style.ind_red -- create new front panel view ---@param panel DisplayBox main displaybox -local function init(panel) +---@param config plc_config configuraiton +local function init(panel, config) local s_hi_box = style.theme.highlight_box local disabled_fg = style.fp.disabled_fg @@ -59,7 +60,21 @@ local function init(panel) heartbeat.register(databus.ps, "heartbeat", heartbeat.update) local reactor = LEDPair{parent=system,label="REACTOR",off=colors.red,c1=colors.yellow,c2=colors.green} - local modem = LED{parent=system,label="MODEM",colors=ind_grn} + reactor.register(databus.ps, "reactor_dev_state", reactor.update) + + if config.Networked then + if config.WirelessModem and config.WiredModem then + local wl_modem = LED{parent=system,label="WD MODEM",colors=ind_grn} + local wd_modem = LED{parent=system,label="WL MODEM",colors=ind_grn} + wd_modem.register(databus.ps, "wd_modem", wd_modem.update) + wl_modem.register(databus.ps, "wl_modem", wl_modem.update) + else + local modem = LED{parent=system,label="MODEM",colors=ind_grn} + modem.register(databus.ps, util.trinary(config.WirelessModem, "wl_modem", "wd_modem"), modem.update) + end + else + local _ = LED{parent=system,label="MODEM",colors=ind_grn} + end if not style.colorblind then local network = RGBLED{parent=system,label="NETWORK",colors={colors.green,colors.red,colors.yellow,colors.orange,style.ind_bkg}} @@ -99,9 +114,6 @@ local function init(panel) system.line_break() - reactor.register(databus.ps, "reactor_dev_state", reactor.update) - modem.register(databus.ps, "has_modem", modem.update) - local rt_main = LED{parent=system,label="RT MAIN",colors=ind_grn} local rt_rps = LED{parent=system,label="RT RPS",colors=ind_grn} local rt_cmtx = LED{parent=system,label="RT COMMS TX",colors=ind_grn} @@ -150,12 +162,10 @@ local function init(panel) -- about footer -- - local about = Div{parent=panel,width=15,height=2,y=term_h-1,fg_bg=disabled_fg} - local fw_v = TextBox{parent=about,text="FW: v00.00.00"} - local comms_v = TextBox{parent=about,text="NT: v00.00.00"} + local info_text = util.sprintf("FW: %s | NT: v%s", databus.ps.get("version"), databus.ps.get("comms_version")) - fw_v.register(databus.ps, "version", function (version) fw_v.set_value(util.c("FW: ", version)) end) - comms_v.register(databus.ps, "comms_version", function (version) comms_v.set_value(util.c("NT: v", version)) end) + local about = Div{parent=panel,height=1,y=term_h,fg_bg=disabled_fg} + TextBox{parent=about,y=1,text=info_text} -- -- rps list diff --git a/reactor-plc/startup.lua b/reactor-plc/startup.lua index 1fc5137..393b89e 100644 --- a/reactor-plc/startup.lua +++ b/reactor-plc/startup.lua @@ -114,7 +114,6 @@ local function main() ---@class plc_sys plc_sys = { rps = nil, ---@type rps - nic = nil, ---@type nic plc_comms = nil, ---@type plc_comms conn_watchdog = nil ---@type watchdog }, @@ -189,8 +188,7 @@ local function main() log.debug("startup> conn watchdog started") -- create network interface then setup comms - smem_sys.nic = backplane.active_nic() - smem_sys.plc_comms = plc.comms(R_PLC_VERSION, smem_sys.nic, smem_dev.reactor, smem_sys.rps, smem_sys.conn_watchdog) + smem_sys.plc_comms = plc.comms(R_PLC_VERSION, backplane.active_nic(), smem_dev.reactor, smem_sys.rps, smem_sys.conn_watchdog) log.debug("startup> comms init") else _println_no_fp("startup> starting in non-networked mode") diff --git a/reactor-plc/threads.lua b/reactor-plc/threads.lua index 281ad16..cc0e631 100644 --- a/reactor-plc/threads.lua +++ b/reactor-plc/threads.lua @@ -41,24 +41,21 @@ function threads.thread__main(smem) local loop_clock = util.new_clock(MAIN_CLOCK) -- load in from shared memory - local networked = smem.networked - local plc_state = smem.plc_state - local plc_dev = smem.plc_dev + local networked = smem.networked + local plc_state = smem.plc_state - local MQ__RPS_CMD = smem.q_cmds.MQ__RPS_CMD - local MQ__COMM_CMD = smem.q_cmds.MQ__COMM_CMD + local rps = smem.plc_sys.rps + local plc_comms = smem.plc_sys.plc_comms + local conn_watchdog = smem.plc_sys.conn_watchdog + + local MQ__RPS_CMD = smem.q_cmds.MQ__RPS_CMD + local MQ__COMM_CMD = smem.q_cmds.MQ__COMM_CMD -- start clock loop_clock.start() -- event loop while true do - -- get plc_sys fields (may have been set late due to degraded boot) - local rps = smem.plc_sys.rps - local nic = smem.plc_sys.nic - local plc_comms = smem.plc_sys.plc_comms - local conn_watchdog = smem.plc_sys.conn_watchdog - local event, param1, param2, param3, param4, param5 = util.pull_event() -- handle event @@ -70,10 +67,10 @@ function threads.thread__main(smem) loop_clock.start() -- send updated data - if networked and nic.is_connected() then + if networked then if plc_comms.is_linked() then smem.q.mq_comms_tx.push_command(MQ__COMM_CMD.SEND_STATUS) - else + elseif backplane.active_nic().is_connected() then if ticks_to_update == 0 then plc_comms.send_link_req() ticks_to_update = LINK_TICKS @@ -95,7 +92,7 @@ function threads.thread__main(smem) smem.q.mq_rps.push_command(MQ__RPS_CMD.SCRAM) -- determine if we are still in a degraded state - if (not networked) or nic.is_connected() then + if (not networked) or backplane.active_nic().is_connected() then plc_state.degraded = false end @@ -113,7 +110,7 @@ function threads.thread__main(smem) -- update indicators databus.tx_hw_status(plc_state) - elseif event == "modem_message" and networked and nic.is_connected() then + elseif event == "modem_message" and networked then -- got a packet local packet = plc_comms.parse_packet(param1, param2, param3, param4, param5) if packet ~= nil then @@ -130,38 +127,8 @@ function threads.thread__main(smem) elseif event == "peripheral_detach" then -- peripheral disconnect local type, device = ppm.handle_unmount(param1) - if type ~= nil and device ~= nil then - if device == plc_dev.reactor then - println_ts("reactor disconnected!") - log.error("reactor logic adapter disconnected") - - plc_state.no_reactor = true - plc_state.degraded = true - elseif networked and type == "modem" then - ---@cast device Modem - -- we only care if this is our comms modem - if nic.is_modem(device) then - nic.disconnect() - - println_ts("comms modem disconnected!") - log.warning("comms modem disconnected") - - local other_modem = ppm.get_wireless_modem() - 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 - plc_state.no_modem = true - plc_state.degraded = true - - -- try to scram reactor if it is still connected - smem.q.mq_rps.push_command(MQ__RPS_CMD.DEGRADED_SCRAM) - end - else - log.warning("non-comms modem disconnected") - end - end + backplane.detach(param1, type, device, println_ts) end -- update indicators