diff --git a/coordinator/config.lua b/coordinator/config.lua index b3eab5f..8e12c53 100644 --- a/coordinator/config.lua +++ b/coordinator/config.lua @@ -3,7 +3,7 @@ local config = {} -- port of the SCADA supervisor config.SCADA_SV_PORT = 16100 -- port to listen to incoming packets from supervisor -config.SCADA_SV_LISTEN = 16101 +config.SCADA_SV_CTL_LISTEN = 16101 -- listen port for SCADA coordinator API access config.SCADA_API_LISTEN = 16200 -- max trusted modem message distance (0 to disable check) diff --git a/coordinator/coordinator.lua b/coordinator/coordinator.lua index c1777a9..ef36971 100644 --- a/coordinator/coordinator.lua +++ b/coordinator/coordinator.lua @@ -242,6 +242,9 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, range _conf_channels() + -- link modem to apisessions + apisessions.init(modem) + -- send a packet to the supervisor ---@param msg_type SCADA_MGMT_TYPE|SCADA_CRDN_TYPE ---@param msg table diff --git a/coordinator/session/api.lua b/coordinator/session/api.lua index e062295..69df834 100644 --- a/coordinator/session/api.lua +++ b/coordinator/session/api.lua @@ -46,6 +46,7 @@ function api.new_session(id, in_queue, out_queue, timeout) last_rtt = 0, -- periodic messages periodics = { + last_update = 0, keep_alive = 0 }, -- when to next retry one of these requests @@ -110,6 +111,9 @@ function api.new_session(id, in_queue, out_queue, timeout) self.r_seq_num = pkt.scada_frame.seq_num() end + -- feed watchdog + self.conn_watchdog.feed() + -- process packet if pkt.scada_frame.protocol() == PROTOCOL.COORD_API then ---@cast pkt capi_frame diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 71f6f07..7ce1485 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -12,14 +12,15 @@ local util = require("scada-common.util") local core = require("graphics.core") -local apisessions = require("coordinator.apisessions") local config = require("coordinator.config") local coordinator = require("coordinator.coordinator") local iocontrol = require("coordinator.iocontrol") local renderer = require("coordinator.renderer") local sounder = require("coordinator.sounder") -local COORDINATOR_VERSION = "v0.13.0" +local apisessions = require("coordinator.session.apisessions") + +local COORDINATOR_VERSION = "v0.13.1" local println = util.println local println_ts = util.println_ts @@ -37,7 +38,7 @@ local log_comms_connecting = coordinator.log_comms_connecting local cfv = util.new_validator() cfv.assert_port(config.SCADA_SV_PORT) -cfv.assert_port(config.SCADA_SV_LISTEN) +cfv.assert_port(config.SCADA_SV_CTL_LISTEN) cfv.assert_port(config.SCADA_API_LISTEN) cfv.assert_type_int(config.TRUSTED_RANGE) cfv.assert_type_num(config.SV_TIMEOUT) @@ -147,7 +148,7 @@ local function main() log.debug("startup> conn watchdog created") -- start comms, open all channels - local coord_comms = coordinator.comms(COORDINATOR_VERSION, modem, config.SCADA_SV_PORT, config.SCADA_SV_LISTEN, + local coord_comms = coordinator.comms(COORDINATOR_VERSION, modem, config.SCADA_SV_PORT, config.SCADA_SV_CTL_LISTEN, config.SCADA_API_LISTEN, config.TRUSTED_RANGE, conn_watchdog) log.debug("startup> comms init") log_comms("comms initialized") @@ -300,6 +301,9 @@ local function main() if loop_clock.is_clock(param1) then -- main loop tick + -- iterate sessions + apisessions.iterate_all() + -- free any closed sessions apisessions.free_all_closed() @@ -326,7 +330,7 @@ local function main() else -- a non-clock/main watchdog timer event - --check API watchdogs + -- check API watchdogs apisessions.check_all_watchdogs(param1) -- notify timer callback dispatcher diff --git a/pocket/pocket.lua b/pocket/pocket.lua index 04feca3..be0c622 100644 --- a/pocket/pocket.lua +++ b/pocket/pocket.lua @@ -152,6 +152,8 @@ function pocket.comms(version, modem, local_port, sv_port, api_port, range, sv_w -- attempt to re-link if any of the dependent links aren't active function public.link_update() if not self.sv.linked then + coreio.report_link_state(util.trinary(self.api.linked, LINK_STATE.API_LINK_ONLY, LINK_STATE.UNLINKED)) + if self.establish_delay_counter <= 0 then _send_sv_establish() self.establish_delay_counter = 4 @@ -159,6 +161,8 @@ function pocket.comms(version, modem, local_port, sv_port, api_port, range, sv_w self.establish_delay_counter = self.establish_delay_counter - 1 end elseif not self.api.linked then + coreio.report_link_state(LINK_STATE.SV_LINK_ONLY) + if self.establish_delay_counter <= 0 then _send_api_establish() self.establish_delay_counter = 4 @@ -167,6 +171,7 @@ function pocket.comms(version, modem, local_port, sv_port, api_port, range, sv_w end else -- linked, all good! + coreio.report_link_state(LINK_STATE.LINKED) end end diff --git a/pocket/startup.lua b/pocket/startup.lua index 3ef9b8e..b57756c 100644 --- a/pocket/startup.lua +++ b/pocket/startup.lua @@ -17,7 +17,7 @@ local coreio = require("pocket.coreio") local pocket = require("pocket.pocket") local renderer = require("pocket.renderer") -local POCKET_VERSION = "alpha-v0.2.1" +local POCKET_VERSION = "alpha-v0.2.2" local println = util.println local println_ts = util.println_ts @@ -89,8 +89,8 @@ local function main() log.debug("startup> conn watchdogs created") -- start comms, open all channels - local pocket_comms = pocket.comms(POCKET_VERSION, modem, config.SCADA_SV_PORT, config.SCADA_API_PORT, - config.LISTEN_PORT, config.TRUSTED_RANGE, conn_wd.sv, conn_wd.api) + local pocket_comms = pocket.comms(POCKET_VERSION, modem, config.LISTEN_PORT, config.SCADA_SV_PORT, + config.SCADA_API_PORT, config.TRUSTED_RANGE, conn_wd.sv, conn_wd.api) log.debug("startup> comms init") -- base loop clock (2Hz, 10 ticks) diff --git a/pocket/ui/components/conn_waiting.lua b/pocket/ui/components/conn_waiting.lua index bca56e9..cd08652 100644 --- a/pocket/ui/components/conn_waiting.lua +++ b/pocket/ui/components/conn_waiting.lua @@ -19,17 +19,20 @@ local cpair = core.graphics.cpair ---@param parent graphics_element parent ---@param y integer y offset local function init(parent, y, is_api) + -- root div + local root = Div{parent=parent,x=1,y=1} + -- bounding box div - local root = Div{parent=parent,x=1,y=y,height=5} + local box = Div{parent=root,x=1,y=y,height=5} local waiting_x = math.floor(parent.width() / 2) - 1 if is_api then - WaitingAnim{parent=root,x=waiting_x,y=1,fg_bg=cpair(colors.blue,style.root.bkg)} - TextBox{parent=root,text="Connecting to API",alignment=TEXT_ALIGN.CENTER,y=5,height=1,fg_bg=cpair(colors.white,style.root.bkg)} + WaitingAnim{parent=box,x=waiting_x,y=1,fg_bg=cpair(colors.blue,style.root.bkg)} + TextBox{parent=box,text="Connecting to API",alignment=TEXT_ALIGN.CENTER,y=5,height=1,fg_bg=cpair(colors.white,style.root.bkg)} else - WaitingAnim{parent=root,x=waiting_x,y=1,fg_bg=cpair(colors.green,style.root.bkg)} - TextBox{parent=root,text="Connecting to Supervisor",alignment=TEXT_ALIGN.CENTER,y=5,height=1,fg_bg=cpair(colors.white,style.root.bkg)} + WaitingAnim{parent=box,x=waiting_x,y=1,fg_bg=cpair(colors.green,style.root.bkg)} + TextBox{parent=box,text="Connecting to Supervisor",alignment=TEXT_ALIGN.CENTER,y=5,height=1,fg_bg=cpair(colors.white,style.root.bkg)} end return root diff --git a/pocket/ui/main.lua b/pocket/ui/main.lua index 630991a..820867c 100644 --- a/pocket/ui/main.lua +++ b/pocket/ui/main.lua @@ -69,7 +69,7 @@ local function init(monitor) -- main page panel panes & sidebar -- - local page_div = Div{parent=main_pane,x=4,y=2} + local page_div = Div{parent=main_pane,x=4,y=1} local sidebar_tabs = { { @@ -103,7 +103,7 @@ local function init(monitor) local page_pane = MultiPane{parent=page_div,x=1,y=1,panes=panes} - Sidebar{parent=main_pane,x=1,y=2,tabs=sidebar_tabs,fg_bg=cpair(colors.white,colors.gray),callback=page_pane.set_value} + Sidebar{parent=main_pane,x=1,y=1,tabs=sidebar_tabs,fg_bg=cpair(colors.white,colors.gray),callback=page_pane.set_value} return main end diff --git a/supervisor/config.lua b/supervisor/config.lua index 9fa3290..0aa26d8 100644 --- a/supervisor/config.lua +++ b/supervisor/config.lua @@ -2,8 +2,8 @@ local config = {} -- scada network listen for PLC's and RTU's config.SCADA_DEV_LISTEN = 16000 --- listen port for SCADA supervisor access by coordinators -config.SCADA_SV_LISTEN = 16100 +-- listen port for SCADA supervisor access +config.SCADA_SV_CTL_LISTEN = 16100 -- max trusted modem message distance (0 to disable check) config.TRUSTED_RANGE = 0 -- time in seconds (>= 2) before assuming a remote device is no longer active diff --git a/supervisor/session/pocket.lua b/supervisor/session/pocket.lua index 54017f5..e760512 100644 --- a/supervisor/session/pocket.lua +++ b/supervisor/session/pocket.lua @@ -45,6 +45,7 @@ function pocket.new_session(id, in_queue, out_queue, timeout) last_rtt = 0, -- periodic messages periodics = { + last_update = 0, keep_alive = 0 }, -- when to next retry one of these requests @@ -95,6 +96,9 @@ function pocket.new_session(id, in_queue, out_queue, timeout) self.r_seq_num = pkt.scada_frame.seq_num() end + -- feed watchdog + self.conn_watchdog.feed() + -- process packet if pkt.scada_frame.protocol() == PROTOCOL.SCADA_MGMT then ---@cast pkt mgmt_frame diff --git a/supervisor/session/svsessions.lua b/supervisor/session/svsessions.lua index 1bf2d59..8ad3366 100644 --- a/supervisor/session/svsessions.lua +++ b/supervisor/session/svsessions.lua @@ -216,7 +216,7 @@ end function svsessions.find_rtu_session(remote_port) -- check RTU sessions local session = _find_session(self.sessions.rtu, remote_port) - ---@cast session rtu_session_struct + ---@cast session rtu_session_struct|nil return session end @@ -227,7 +227,7 @@ end function svsessions.find_plc_session(remote_port) -- check PLC sessions local session = _find_session(self.sessions.plc, remote_port) - ---@cast session plc_session_struct + ---@cast session plc_session_struct|nil return session end @@ -246,15 +246,18 @@ function svsessions.find_device_session(remote_port) return session end --- find a coordinator session by the remote port
--- only one coordinator is allowed, but this is kept to be consistent with all other session tables +-- find a coordinator or diagnostic access session by the remote port ---@nodiscard ---@param remote_port integer ----@return coord_session_struct|nil -function svsessions.find_coord_session(remote_port) +---@return coord_session_struct|diag_session_struct|nil +function svsessions.find_svctl_session(remote_port) -- check coordinator sessions local session = _find_session(self.sessions.coord, remote_port) - ---@cast session coord_session_struct + + -- check diagnostic sessions + if session == nil then session = _find_session(self.sessions.diag, remote_port) end + ---@cast session coord_session_struct|diag_session_struct|nil + return session end diff --git a/supervisor/startup.lua b/supervisor/startup.lua index fd80180..bc11f4f 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -14,7 +14,7 @@ local supervisor = require("supervisor.supervisor") local svsessions = require("supervisor.session.svsessions") -local SUPERVISOR_VERSION = "v0.15.0" +local SUPERVISOR_VERSION = "v0.15.1" local println = util.println local println_ts = util.println_ts @@ -26,7 +26,7 @@ local println_ts = util.println_ts local cfv = util.new_validator() cfv.assert_port(config.SCADA_DEV_LISTEN) -cfv.assert_port(config.SCADA_SV_LISTEN) +cfv.assert_port(config.SCADA_SV_CTL_LISTEN) cfv.assert_type_int(config.TRUSTED_RANGE) cfv.assert_type_num(config.PLC_TIMEOUT) cfv.assert_min(config.PLC_TIMEOUT, 2) @@ -91,7 +91,7 @@ local function main() -- start comms, open all channels local superv_comms = supervisor.comms(SUPERVISOR_VERSION, config.NUM_REACTORS, config.REACTOR_COOLING, modem, - config.SCADA_DEV_LISTEN, config.SCADA_SV_LISTEN, config.TRUSTED_RANGE) + config.SCADA_DEV_LISTEN, config.SCADA_SV_CTL_LISTEN, config.TRUSTED_RANGE) -- base loop clock (6.67Hz, 3 ticks) local MAIN_CLOCK = 0.15 diff --git a/supervisor/supervisor.lua b/supervisor/supervisor.lua index 99226fb..817275c 100644 --- a/supervisor/supervisor.lua +++ b/supervisor/supervisor.lua @@ -20,10 +20,10 @@ local println = util.println ---@param cooling_conf table cooling configuration table ---@param modem table modem device ---@param dev_listen integer listening port for PLC/RTU devices ----@param coord_listen integer listening port for coordinator +---@param svctl_listen integer listening port for supervisor access ---@param range integer trusted device connection range ---@diagnostic disable-next-line: unused-local -function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen, coord_listen, range) +function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen, svctl_listen, range) local self = { last_est_acks = {} } @@ -36,7 +36,7 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen local function _conf_channels() modem.closeAll() modem.open(dev_listen) - modem.open(coord_listen) + modem.open(svctl_listen) end _conf_channels() @@ -57,18 +57,18 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen modem.transmit(dest, dev_listen, s_pkt.raw_sendable()) end - -- send coordinator connection establish response + -- send supervisor control access connection establish response ---@param seq_id integer ---@param dest integer ---@param msg table - local function _send_crdn_establish(seq_id, dest, msg) + local function _send_svctl_establish(seq_id, dest, msg) local s_pkt = comms.scada_packet() local c_pkt = comms.mgmt_packet() c_pkt.make(SCADA_MGMT_TYPE.ESTABLISH, msg) s_pkt.make(seq_id, PROTOCOL.SCADA_MGMT, c_pkt.raw_sendable()) - modem.transmit(dest, coord_listen, s_pkt.raw_sendable()) + modem.transmit(dest, svctl_listen, s_pkt.raw_sendable()) end -- PUBLIC FUNCTIONS -- @@ -251,9 +251,9 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen log.debug("illegal packet type " .. protocol .. " on device listening channel") end -- coordinator listening channel - elseif l_port == coord_listen then + elseif l_port == svctl_listen then -- look for an associated session - local session = svsessions.find_coord_session(r_port) + local session = svsessions.find_svctl_session(r_port) if protocol == PROTOCOL.SCADA_MGMT then ---@cast packet mgmt_frame @@ -277,12 +277,9 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen self.last_est_acks[r_port] = ESTABLISH_ACK.BAD_VERSION end - _send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.BAD_VERSION }) - elseif dev_type ~= DEVICE_TYPE.CRDN then - log.debug(util.c("illegal establish packet for device ", dev_type, " on CRDN listening channel")) - _send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY }) - else - -- this is an attempt to establish a new session + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.BAD_VERSION }) + elseif dev_type == DEVICE_TYPE.CRDN then + -- this is an attempt to establish a new coordinator session local s_id = svsessions.establish_coord_session(l_port, r_port, firmware_v) if s_id ~= false then @@ -292,23 +289,35 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen table.insert(config, cooling_conf[i].TURBINES) end - println(util.c("CRD (",firmware_v, ") [:", r_port, "] \xbb connected")) - log.info(util.c("CRDN_ESTABLISH: coordinator (",firmware_v, ") [:", r_port, "] connected with session ID ", s_id)) + println(util.c("CRD (", firmware_v, ") [:", r_port, "] \xbb connected")) + log.info(util.c("SVCTL_ESTABLISH: coordinator (", firmware_v, ") [:", r_port, "] connected with session ID ", s_id)) - _send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW, config }) + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW, config }) self.last_est_acks[r_port] = ESTABLISH_ACK.ALLOW else if self.last_est_acks[r_port] ~= ESTABLISH_ACK.COLLISION then - log.info("CRDN_ESTABLISH: denied new coordinator due to already being connected to another coordinator") + log.info("SVCTL_ESTABLISH: denied new coordinator due to already being connected to another coordinator") self.last_est_acks[r_port] = ESTABLISH_ACK.COLLISION end - _send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.COLLISION }) + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.COLLISION }) end + elseif dev_type == DEVICE_TYPE.PKT then + -- this is an attempt to establish a new pocket diagnostic session + local s_id = svsessions.establish_diag_session(l_port, r_port, firmware_v) + + println(util.c("PKT (", firmware_v, ") [:", r_port, "] \xbb connected")) + log.info(util.c("SVCTL_ESTABLISH: pocket (", firmware_v, ") [:", r_port, "] connected with session ID ", s_id)) + + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW }) + self.last_est_acks[r_port] = ESTABLISH_ACK.ALLOW + else + log.debug(util.c("illegal establish packet for device ", dev_type, " on SVCTL listening channel")) + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY }) end else - log.debug("CRDN_ESTABLISH: establish packet length mismatch") - _send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY }) + log.debug("SVCTL_ESTABLISH: establish packet length mismatch") + _send_svctl_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY }) end else -- any other packet should be session related, discard it