From 50be7f9ca2975e6328d645c9e291a95f11f0cef2 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Thu, 22 Sep 2022 20:42:06 -0400 Subject: [PATCH] #97 fixed issue where traffic on other channels gets processed if channels are left open --- coordinator/coordinator.lua | 35 ++++++++++++++++++----------------- coordinator/startup.lua | 2 +- reactor-plc/plc.lua | 21 ++++++++++----------- reactor-plc/startup.lua | 2 +- rtu/rtu.lua | 21 ++++++++++----------- rtu/startup.lua | 2 +- supervisor/startup.lua | 2 +- supervisor/supervisor.lua | 24 ++++++++---------------- 8 files changed, 50 insertions(+), 59 deletions(-) diff --git a/coordinator/coordinator.lua b/coordinator/coordinator.lua index 3414753..22e79d9 100644 --- a/coordinator/coordinator.lua +++ b/coordinator/coordinator.lua @@ -202,19 +202,14 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa -- PRIVATE FUNCTIONS -- - -- open all channels - local function _open_channels() - if not self.modem.isOpen(sv_listen) then - self.modem.open(sv_listen) - end - - if not self.modem.isOpen(api_listen) then - self.modem.open(api_listen) - end + -- configure modem channels + local function _conf_channels() + self.modem.closeAll() + self.modem.open(sv_listen) + self.modem.open(api_listen) end - -- open at construct time - _open_channels() + _conf_channels() -- send a packet to the supervisor ---@param msg_type SCADA_MGMT_TYPES|SCADA_CRDN_TYPES @@ -256,7 +251,7 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa ---@diagnostic disable-next-line: redefined-local function public.reconnect_modem(modem) self.modem = modem - _open_channels() + _conf_channels() end -- close the connection to the server @@ -364,11 +359,16 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa function public.handle_packet(packet) if packet ~= nil then local protocol = packet.scada_frame.protocol() + local l_port = packet.scada_frame.local_port() - if protocol == PROTOCOLS.COORD_API then + if l_port == api_listen then + if protocol == PROTOCOLS.COORD_API then ---@diagnostic disable-next-line: param-type-mismatch - apisessions.handle_packet(packet) - else + apisessions.handle_packet(packet) + else + log.debug("illegal packet type " .. protocol .. " on api listening channel", true) + end + elseif l_port == sv_listen then -- check sequence number if self.sv_r_seq_num == nil then self.sv_r_seq_num = packet.scada_frame.seq_num() @@ -456,9 +456,10 @@ function coordinator.comms(version, modem, sv_port, sv_listen, api_listen, sv_wa log.warning("received unknown SCADA_MGMT packet type " .. packet.type) end else - -- should be unreachable assuming packet is from parse_packet() - log.error("illegal packet type " .. protocol, true) + log.debug("illegal packet type " .. protocol .. " on supervisor listening channel", true) end + else + log.debug("received packet on unconfigured channel " .. l_port, true) end end end diff --git a/coordinator/startup.lua b/coordinator/startup.lua index 10c0ede..3590a39 100644 --- a/coordinator/startup.lua +++ b/coordinator/startup.lua @@ -16,7 +16,7 @@ local config = require("coordinator.config") local coordinator = require("coordinator.coordinator") local renderer = require("coordinator.renderer") -local COORDINATOR_VERSION = "alpha-v0.4.13" +local COORDINATOR_VERSION = "alpha-v0.4.14" local print = util.print local println = util.println diff --git a/reactor-plc/plc.lua b/reactor-plc/plc.lua index db84d0b..b56fd4c 100644 --- a/reactor-plc/plc.lua +++ b/reactor-plc/plc.lua @@ -303,14 +303,17 @@ function plc.comms(id, version, modem, local_port, server_port, reactor, rps, co max_burn_rate = nil } - ---@class plc_comms - local public = {} - - -- open modem - if not self.modem.isOpen(self.l_port) then + -- configure modem channels + local function _conf_channels() + self.modem.closeAll() self.modem.open(self.l_port) end + _conf_channels() + + ---@class plc_comms + local public = {} + -- PRIVATE FUNCTIONS -- -- send an RPLC packet @@ -484,11 +487,7 @@ function plc.comms(id, version, modem, local_port, server_port, reactor, rps, co ---@diagnostic disable-next-line: redefined-local function public.reconnect_modem(modem) self.modem = modem - - -- open modem - if not self.modem.isOpen(self.l_port) then - self.modem.open(self.l_port) - end + _conf_channels() end -- reconnect a newly connected reactor @@ -605,7 +604,7 @@ function plc.comms(id, version, modem, local_port, server_port, reactor, rps, co ---@param plc_state plc_state ---@param setpoints setpoints function public.handle_packet(packet, plc_state, setpoints) - if packet ~= nil then + if packet ~= nil and packet.scada_frame.local_port() == self.l_port then -- check sequence number if self.r_seq_num == nil then self.r_seq_num = packet.scada_frame.seq_num() diff --git a/reactor-plc/startup.lua b/reactor-plc/startup.lua index f04f0d3..4cc2658 100644 --- a/reactor-plc/startup.lua +++ b/reactor-plc/startup.lua @@ -13,7 +13,7 @@ local config = require("reactor-plc.config") local plc = require("reactor-plc.plc") local threads = require("reactor-plc.threads") -local R_PLC_VERSION = "beta-v0.8.3" +local R_PLC_VERSION = "beta-v0.8.4" local print = util.print local println = util.println diff --git a/rtu/rtu.lua b/rtu/rtu.lua index 4380e84..c427f54 100644 --- a/rtu/rtu.lua +++ b/rtu/rtu.lua @@ -175,16 +175,19 @@ function rtu.comms(version, modem, local_port, server_port, conn_watchdog) conn_watchdog = conn_watchdog } + -- configure modem channels + local function _conf_channels() + self.modem.closeAll() + self.modem.open(self.l_port) + end + + _conf_channels() + ---@class rtu_comms local public = {} local insert = table.insert - -- open modem - if not self.modem.isOpen(self.l_port) then - self.modem.open(self.l_port) - end - -- PRIVATE FUNCTIONS -- -- send a scada management packet @@ -223,11 +226,7 @@ function rtu.comms(version, modem, local_port, server_port, conn_watchdog) ---@diagnostic disable-next-line: redefined-local function public.reconnect_modem(modem) self.modem = modem - - -- open modem - if not self.modem.isOpen(self.l_port) then - self.modem.open(self.l_port) - end + _conf_channels() end -- unlink from the server @@ -312,7 +311,7 @@ function rtu.comms(version, modem, local_port, server_port, conn_watchdog) ---@param units table ---@param rtu_state rtu_state function public.handle_packet(packet, units, rtu_state) - if packet ~= nil then + if packet ~= nil and packet.scada_frame.local_port() == self.l_port then -- check sequence number if self.r_seq_num == nil then self.r_seq_num = packet.scada_frame.seq_num() diff --git a/rtu/startup.lua b/rtu/startup.lua index 342cb19..422ee5e 100644 --- a/rtu/startup.lua +++ b/rtu/startup.lua @@ -24,7 +24,7 @@ local sna_rtu = require("rtu.dev.sna_rtu") local sps_rtu = require("rtu.dev.sps_rtu") local turbinev_rtu = require("rtu.dev.turbinev_rtu") -local RTU_VERSION = "beta-v0.8.0" +local RTU_VERSION = "beta-v0.8.1" local rtu_t = types.rtu_t diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 5378f83..0acef53 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -13,7 +13,7 @@ local svsessions = require("supervisor.session.svsessions") local config = require("supervisor.config") local supervisor = require("supervisor.supervisor") -local SUPERVISOR_VERSION = "beta-v0.5.12" +local SUPERVISOR_VERSION = "beta-v0.5.13" local print = util.print local println = util.println diff --git a/supervisor/supervisor.lua b/supervisor/supervisor.lua index 50ead6d..873c3e1 100644 --- a/supervisor/supervisor.lua +++ b/supervisor/supervisor.lua @@ -9,12 +9,9 @@ local supervisor = {} local PROTOCOLS = comms.PROTOCOLS local RPLC_TYPES = comms.RPLC_TYPES local RPLC_LINKING = comms.RPLC_LINKING -local RTU_UNIT_TYPES = comms.RTU_UNIT_TYPES local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES local SCADA_CRDN_TYPES = comms.SCADA_CRDN_TYPES -local SESSION_TYPE = svsessions.SESSION_TYPE - local print = util.print local println = util.println local print_ts = util.print_ts @@ -42,19 +39,14 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen -- PRIVATE FUNCTIONS -- - -- open all channels - local function _open_channels() - if not self.modem.isOpen(self.dev_listen) then - self.modem.open(self.dev_listen) - end - - if not self.modem.isOpen(self.coord_listen) then - self.modem.open(self.coord_listen) - end + -- configure modem channels + local function _conf_channels() + self.modem.closeAll() + self.modem.open(self.dev_listen) + self.modem.open(self.coord_listen) end - -- open at construct time - _open_channels() + _conf_channels() -- link modem to svsessions svsessions.init(self.modem, num_reactors, cooling_conf) @@ -113,7 +105,7 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen function public.reconnect_modem(modem) self.modem = modem svsessions.relink_modem(self.modem) - _open_channels() + _conf_channels() end -- parse a packet @@ -292,7 +284,7 @@ function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen log.debug("illegal packet type " .. protocol .. " on coordinator listening channel") end else - log.warning("received packet on unused channel " .. l_port) + log.warning("received packet on unconfigured channel " .. l_port) end end end