#635 comms optimizations

This commit is contained in:
Mikayla
2025-11-10 23:00:52 +00:00
parent fb102502dd
commit 2e01b478f1
15 changed files with 90 additions and 134 deletions

View File

@@ -327,10 +327,10 @@ function facility.receive_sv(side, sender, reply_to, message, distance)
local s_pkt = self.nic.receive(side, sender, reply_to, message, distance) local s_pkt = self.nic.receive(side, sender, reply_to, message, distance)
if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() local pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then if pkt then
tcd.abort(handle_timeout) tcd.abort(handle_timeout)
handle_packet(mgmt_pkt.get()) handle_packet(pkt)
end end
end end
end end

View File

@@ -374,18 +374,10 @@ function coordinator.comms(version, nic, wl_nic, sv_watchdog)
local pkt = nil local pkt = nil
if s_pkt then if s_pkt then
-- get as SCADA management packet
if s_pkt.protocol() == PROTOCOL.SCADA_MGMT then if s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then
pkt = mgmt_pkt.get()
end
-- get as coordinator packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then
local crdn_pkt = comms.crdn_packet() pkt = comms.crdn_packet().decode(s_pkt)
if crdn_pkt.decode(s_pkt) then
pkt = crdn_pkt.get()
end
else else
log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true) log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
end end

View File

@@ -20,7 +20,7 @@ local renderer = require("coordinator.renderer")
local sounder = require("coordinator.sounder") local sounder = require("coordinator.sounder")
local threads = require("coordinator.threads") local threads = require("coordinator.threads")
local COORDINATOR_VERSION = "v1.7.0" local COORDINATOR_VERSION = "v1.7.1"
local CHUNK_LOAD_DELAY_S = 30.0 local CHUNK_LOAD_DELAY_S = 30.0

View File

@@ -626,21 +626,13 @@ function pocket.comms(version, nic, sv_watchdog, api_watchdog, nav)
---@return mgmt_frame|crdn_frame|nil packet ---@return mgmt_frame|crdn_frame|nil packet
function public.parse_packet(side, sender, reply_to, message, distance) function public.parse_packet(side, sender, reply_to, message, distance)
local s_pkt = nic.receive(side, sender, reply_to, message, distance) local s_pkt = nic.receive(side, sender, reply_to, message, distance)
local pkt = nil
local pkt = nil
if s_pkt then if s_pkt then
-- get as SCADA management packet
if s_pkt.protocol() == PROTOCOL.SCADA_MGMT then if s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then
pkt = mgmt_pkt.get()
end
-- get as coordinator packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then
local crdn_pkt = comms.crdn_packet() pkt = comms.crdn_packet().decode(s_pkt)
if crdn_pkt.decode(s_pkt) then
pkt = crdn_pkt.get()
end
else else
log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true) log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
end end

View File

@@ -22,7 +22,7 @@ local pocket = require("pocket.pocket")
local renderer = require("pocket.renderer") local renderer = require("pocket.renderer")
local threads = require("pocket.threads") local threads = require("pocket.threads")
local POCKET_VERSION = "v1.0.4" local POCKET_VERSION = "v1.0.5"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts

View File

@@ -249,10 +249,10 @@ function check.receive_sv(side, sender, reply_to, message, distance)
local s_pkt = self.nic.receive(side, sender, reply_to, message, distance) local s_pkt = self.nic.receive(side, sender, reply_to, message, distance)
if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() local pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then if pkt then
tcd.abort(handle_timeout) tcd.abort(handle_timeout)
handle_packet(mgmt_pkt.get()) handle_packet(pkt)
end end
end end
end end

View File

@@ -923,14 +923,10 @@ function plc.comms(version, nic, reactor, rps, conn_watchdog)
local pkt = nil local pkt = nil
if s_pkt then if s_pkt then
-- get as RPLC packet
if s_pkt.protocol() == PROTOCOL.RPLC then if s_pkt.protocol() == PROTOCOL.RPLC then
local rplc_pkt = comms.rplc_packet() pkt = comms.rplc_packet().decode(s_pkt)
if rplc_pkt.decode(s_pkt) then pkt = rplc_pkt.get() end
-- get as SCADA management packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then pkt = mgmt_pkt.get() end
else else
log.debug("unsupported packet type " .. s_pkt.protocol(), true) log.debug("unsupported packet type " .. s_pkt.protocol(), true)
end end

View File

@@ -19,7 +19,7 @@ local plc = require("reactor-plc.plc")
local renderer = require("reactor-plc.renderer") local renderer = require("reactor-plc.renderer")
local threads = require("reactor-plc.threads") local threads = require("reactor-plc.threads")
local R_PLC_VERSION = "v1.10.0" local R_PLC_VERSION = "v1.10.1"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts

View File

@@ -326,10 +326,10 @@ function check.receive_sv(side, sender, reply_to, message, distance)
local s_pkt = self.nic.receive(side, sender, reply_to, message, distance) local s_pkt = self.nic.receive(side, sender, reply_to, message, distance)
if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then if s_pkt and s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() local pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then if pkt then
tcd.abort(handle_timeout) tcd.abort(handle_timeout)
handle_packet(mgmt_pkt.get()) handle_packet(pkt)
end end
end end
end end

View File

@@ -425,18 +425,10 @@ function rtu.comms(version, nic, conn_watchdog)
local pkt = nil local pkt = nil
if s_pkt then if s_pkt then
-- get as MODBUS TCP packet
if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then
local m_pkt = comms.modbus_packet() pkt = comms.modbus_packet().decode(s_pkt)
if m_pkt.decode(s_pkt) then
pkt = m_pkt.get()
end
-- get as SCADA management packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then
pkt = mgmt_pkt.get()
end
else else
log.debug("illegal packet type " .. s_pkt.protocol(), true) log.debug("illegal packet type " .. s_pkt.protocol(), true)
end end

View File

@@ -21,7 +21,7 @@ local rtu = require("rtu.rtu")
local threads = require("rtu.threads") local threads = require("rtu.threads")
local uinit = require("rtu.uinit") local uinit = require("rtu.uinit")
local RTU_VERSION = "v1.13.0" local RTU_VERSION = "v1.13.1"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts

View File

@@ -414,6 +414,7 @@ function comms.modbus_packet()
---@param unit_id integer ---@param unit_id integer
---@param func_code MODBUS_FCODE ---@param func_code MODBUS_FCODE
---@param data table ---@param data table
---@return boolean success
function public.make(txn_id, unit_id, func_code, data) function public.make(txn_id, unit_id, func_code, data)
if type(data) == "table" then if type(data) == "table" then
self.txn_id = txn_id self.txn_id = txn_id
@@ -425,37 +426,33 @@ function comms.modbus_packet()
-- populate raw array -- populate raw array
self.raw = { self.txn_id, self.unit_id, self.func_code } self.raw = { self.txn_id, self.unit_id, self.func_code }
for i = 1, self.length do insert(self.raw, data[i]) end for i = 1, self.length do insert(self.raw, data[i]) end
else
log.error("COMMS: modbus_packet.make(): data not a table") return true
end end
log.error("COMMS: modbus_packet.make(): data not a table")
return false
end end
-- decode a MODBUS packet from a SCADA frame -- decode a MODBUS packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return modbus_frame|nil frame the decoded frame, if valid
function public.decode(frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
if frame.protocol() == PROTOCOL.MODBUS_TCP then if frame.protocol() == PROTOCOL.MODBUS_TCP then
local size_ok = frame.length() >= 3 if frame.length() >= 3 then
if size_ok then
local data = frame.data() local data = frame.data()
public.make(data[1], data[2], data[3], { table.unpack(data, 4, #data) })
if public.make(data[1], data[2], data[3], { table.unpack(data, 4, #data) }) then
return public.get()
end
end end
else log.debug("COMMS: attempted MODBUS_TCP parse of incorrect protocol " .. frame.protocol(), true) end
else log.debug("COMMS: nil frame encountered", true) end
local valid = type(self.txn_id) == "number" and type(self.unit_id) == "number" and type(self.func_code) == "number" return nil
return size_ok and valid
else
log.debug("COMMS: attempted MODBUS_TCP parse of incorrect protocol " .. frame.protocol(), true)
return false
end
else
log.debug("COMMS: nil frame encountered", true)
return false
end
end end
-- get raw to send -- get raw to send
@@ -500,6 +497,7 @@ function comms.rplc_packet()
---@param id integer ---@param id integer
---@param packet_type RPLC_TYPE ---@param packet_type RPLC_TYPE
---@param data table ---@param data table
---@return boolean success
function public.make(id, packet_type, data) function public.make(id, packet_type, data)
if type(data) == "table" then if type(data) == "table" then
-- packet accessor properties -- packet accessor properties
@@ -511,37 +509,33 @@ function comms.rplc_packet()
-- populate raw array -- populate raw array
self.raw = { self.id, self.type } self.raw = { self.id, self.type }
for i = 1, #data do insert(self.raw, data[i]) end for i = 1, #data do insert(self.raw, data[i]) end
else
log.error("COMMS: rplc_packet.make(): data not a table") return true
end end
log.error("COMMS: rplc_packet.make(): data not a table")
return false
end end
-- decode an RPLC packet from a SCADA frame -- decode an RPLC packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return rplc_frame|nil frame the decoded frame, if valid
function public.decode(frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
if frame.protocol() == PROTOCOL.RPLC then if frame.protocol() == PROTOCOL.RPLC then
local ok = frame.length() >= 2 if frame.length() >= 2 then
if ok then
local data = frame.data() local data = frame.data()
public.make(data[1], data[2], { table.unpack(data, 3, #data) })
if public.make(data[1], data[2], { table.unpack(data, 3, #data) }) and (type(self.id) == "number") then
return public.get()
end
end end
else log.debug("COMMS: attempted RPLC parse of incorrect protocol " .. frame.protocol(), true) end
else log.debug("COMMS: nil frame encountered", true) end
ok = ok and type(self.id) == "number" return nil
return ok
else
log.debug("COMMS: attempted RPLC parse of incorrect protocol " .. frame.protocol(), true)
return false
end
else
log.debug("COMMS: nil frame encountered", true)
return false
end
end end
-- get raw to send -- get raw to send
@@ -583,6 +577,7 @@ function comms.mgmt_packet()
-- make a SCADA management packet -- make a SCADA management packet
---@param packet_type MGMT_TYPE ---@param packet_type MGMT_TYPE
---@param data table ---@param data table
---@return boolean success
function public.make(packet_type, data) function public.make(packet_type, data)
if type(data) == "table" then if type(data) == "table" then
-- packet accessor properties -- packet accessor properties
@@ -593,35 +588,33 @@ function comms.mgmt_packet()
-- populate raw array -- populate raw array
self.raw = { self.type } self.raw = { self.type }
for i = 1, #data do insert(self.raw, data[i]) end for i = 1, #data do insert(self.raw, data[i]) end
else
log.error("COMMS: mgmt_packet.make(): data not a table") return true
end end
log.error("COMMS: mgmt_packet.make(): data not a table")
return false
end end
-- decode a SCADA management packet from a SCADA frame -- decode a SCADA management packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return mgmt_frame|nil frame the decoded frame, if valid
function public.decode(frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
if frame.protocol() == PROTOCOL.SCADA_MGMT then if frame.protocol() == PROTOCOL.SCADA_MGMT then
local ok = frame.length() >= 1 if frame.length() >= 1 then
if ok then
local data = frame.data() local data = frame.data()
public.make(data[1], { table.unpack(data, 2, #data) })
end
return ok if public.make(data[1], { table.unpack(data, 2, #data) }) then
else return public.get()
log.debug("COMMS: attempted SCADA_MGMT parse of incorrect protocol " .. frame.protocol(), true) end
return false end
end else log.debug("COMMS: attempted SCADA_MGMT parse of incorrect protocol " .. frame.protocol(), true) end
else else log.debug("COMMS: nil frame encountered", true) end
log.debug("COMMS: nil frame encountered", true)
return false return nil
end
end end
-- get raw to send -- get raw to send
@@ -662,6 +655,7 @@ function comms.crdn_packet()
-- make a coordinator packet -- make a coordinator packet
---@param packet_type CRDN_TYPE ---@param packet_type CRDN_TYPE
---@param data table ---@param data table
---@return boolean success
function public.make(packet_type, data) function public.make(packet_type, data)
if type(data) == "table" then if type(data) == "table" then
-- packet accessor properties -- packet accessor properties
@@ -672,35 +666,33 @@ function comms.crdn_packet()
-- populate raw array -- populate raw array
self.raw = { self.type } self.raw = { self.type }
for i = 1, #data do insert(self.raw, data[i]) end for i = 1, #data do insert(self.raw, data[i]) end
else
log.error("COMMS: crdn_packet.make(): data not a table") return true
end end
log.error("COMMS: crdn_packet.make(): data not a table")
return false
end end
-- decode a coordinator packet from a SCADA frame -- decode a coordinator packet from a SCADA frame
---@param frame scada_packet ---@param frame scada_packet
---@return boolean success ---@return crdn_frame|nil frame the decoded frame, if valid
function public.decode(frame) function public.decode(frame)
if frame then if frame then
self.frame = frame self.frame = frame
if frame.protocol() == PROTOCOL.SCADA_CRDN then if frame.protocol() == PROTOCOL.SCADA_CRDN then
local ok = frame.length() >= 1 if frame.length() >= 1 then
if ok then
local data = frame.data() local data = frame.data()
public.make(data[1], { table.unpack(data, 2, #data) })
end
return ok if public.make(data[1], { table.unpack(data, 2, #data) }) then
else return public.get()
log.debug("COMMS: attempted SCADA_CRDN parse of incorrect protocol " .. frame.protocol(), true) end
return false end
end else log.debug("COMMS: attempted SCADA_CRDN parse of incorrect protocol " .. frame.protocol(), true) end
else else log.debug("COMMS: nil frame encountered", true) end
log.debug("COMMS: nil frame encountered", true)
return false return nil
end
end end
-- get raw to send -- get raw to send

View File

@@ -24,7 +24,7 @@ local t_pack = table.pack
local util = {} local util = {}
-- scada-common version -- scada-common version
util.version = "1.6.0" util.version = "1.6.1"
util.TICK_TIME_S = 0.05 util.TICK_TIME_S = 0.05
util.TICK_TIME_MS = 50 util.TICK_TIME_MS = 50

View File

@@ -24,7 +24,7 @@ local supervisor = require("supervisor.supervisor")
local svsessions = require("supervisor.session.svsessions") local svsessions = require("supervisor.session.svsessions")
local SUPERVISOR_VERSION = "v1.8.0" local SUPERVISOR_VERSION = "v1.8.1"
local println = util.println local println = util.println
local println_ts = util.println_ts local println_ts = util.println_ts

View File

@@ -413,22 +413,14 @@ function supervisor.comms(_version, fp_ok, facility)
end end
if s_pkt then if s_pkt then
-- get as MODBUS TCP packet
if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then
local m_pkt = comms.modbus_packet() pkt = comms.modbus_packet().decode(s_pkt)
if m_pkt.decode(s_pkt) then pkt = m_pkt.get() end
-- get as RPLC packet
elseif s_pkt.protocol() == PROTOCOL.RPLC then elseif s_pkt.protocol() == PROTOCOL.RPLC then
local rplc_pkt = comms.rplc_packet() pkt = comms.rplc_packet().decode(s_pkt)
if rplc_pkt.decode(s_pkt) then pkt = rplc_pkt.get() end
-- get as SCADA management packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
local mgmt_pkt = comms.mgmt_packet() pkt = comms.mgmt_packet().decode(s_pkt)
if mgmt_pkt.decode(s_pkt) then pkt = mgmt_pkt.get() end
-- get as coordinator packet
elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then
local crdn_pkt = comms.crdn_packet() pkt = comms.crdn_packet().decode(s_pkt)
if crdn_pkt.decode(s_pkt) then pkt = crdn_pkt.get() end
else else
log.debug("parse_packet(" .. side .. "): attempted parse of illegal packet type " .. s_pkt.protocol(), true) log.debug("parse_packet(" .. side .. "): attempted parse of illegal packet type " .. s_pkt.protocol(), true)
end end