From 416255f41a8a48ad2cf2fa6e6004294a5f895c42 Mon Sep 17 00:00:00 2001 From: Mikayla Fischler Date: Sat, 23 Apr 2022 21:10:25 -0400 Subject: [PATCH] PLC check sequence numbers, corrected trip time to ms --- reactor-plc/plc.lua | 14 +++++++++++++- supervisor/session/plc.lua | 4 ++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/reactor-plc/plc.lua b/reactor-plc/plc.lua index f0f00b1..64adb88 100644 --- a/reactor-plc/plc.lua +++ b/reactor-plc/plc.lua @@ -202,6 +202,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) local self = { id = id, seq_num = 0, + r_seq_num = nil, modem = modem, s_port = server_port, l_port = local_port, @@ -363,6 +364,17 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) -- handle an RPLC packet local handle_packet = function (packet, plc_state) if packet ~= nil then + -- check sequence number + if self.r_seq_num == nil then + self.r_seq_num = packet.scada_frame.seq_num() + elseif self.r_seq_num >= packet.scada_frame.seq_num() then + log._warning("sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. packet.scada_frame.seq_num()) + return + else + self.r_seq_num = packet.scada_frame.seq_num() + end + + -- handle packet if packet.scada_frame.protocol() == PROTOCOLS.RPLC then if self.linked then if packet.type == RPLC_TYPES.KEEP_ALIVE then @@ -372,7 +384,7 @@ function comms_init(id, modem, local_port, server_port, reactor, iss) if trip_time < 0 then log._warning("PLC KEEP_ALIVE trip time less than 0 (" .. trip_time .. ")") - elseif trip_time > 1 then + elseif trip_time > 1000 then log._warning("PLC KEEP_ALIVE trip time > 1s (" .. trip_time .. ")") end diff --git a/supervisor/session/plc.lua b/supervisor/session/plc.lua index 7694ef4..9fbed9b 100644 --- a/supervisor/session/plc.lua +++ b/supervisor/session/plc.lua @@ -190,11 +190,11 @@ function new_session(id, for_reactor, in_queue, out_queue) if self.last_rtt < 0 then log._warning(log_header .. "PLC KEEP_ALIVE round trip time less than 0 (" .. trip_time .. ")") - elseif trip_time > 1 then + elseif trip_time > 1000 then log._warning(log_header .. "PLC KEEP_ALIVE round trip time > 1s (" .. trip_time .. ")") end - log._debug(log_header .. "RPLC RTT = ".. trip_time) + log._debug(log_header .. "RPLC RTT = ".. trip_time .. "ms") else log._debug(log_header .. "RPLC keep alive packet length mismatch") end