2022-08-28 16:12:30 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2024-03-24 16:56:51 +00:00
|
|
|
local themes = require("graphics.themes")
|
|
|
|
|
2022-05-11 15:31:02 +00:00
|
|
|
local svsessions = require("supervisor.session.svsessions")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local supervisor = {}
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
local PROTOCOL = comms.PROTOCOL
|
|
|
|
local DEVICE_TYPE = comms.DEVICE_TYPE
|
2022-11-13 19:13:30 +00:00
|
|
|
local ESTABLISH_ACK = comms.ESTABLISH_ACK
|
2023-08-30 20:45:48 +00:00
|
|
|
local MGMT_TYPE = comms.MGMT_TYPE
|
2022-04-22 15:07:59 +00:00
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
---@type svr_config
|
|
|
|
local config = {}
|
|
|
|
|
|
|
|
supervisor.config = config
|
|
|
|
|
|
|
|
-- load the supervisor configuration
|
|
|
|
function supervisor.load_config()
|
|
|
|
if not settings.load("/supervisor.settings") then return false end
|
|
|
|
|
|
|
|
config.UnitCount = settings.get("UnitCount")
|
|
|
|
config.CoolingConfig = settings.get("CoolingConfig")
|
|
|
|
config.FacilityTankMode = settings.get("FacilityTankMode")
|
|
|
|
config.FacilityTankDefs = settings.get("FacilityTankDefs")
|
2024-04-12 04:13:05 +00:00
|
|
|
config.ExtChargeIdling = settings.get("ExtChargeIdling")
|
2023-12-29 18:58:28 +00:00
|
|
|
|
|
|
|
config.SVR_Channel = settings.get("SVR_Channel")
|
|
|
|
config.PLC_Channel = settings.get("PLC_Channel")
|
|
|
|
config.RTU_Channel = settings.get("RTU_Channel")
|
|
|
|
config.CRD_Channel = settings.get("CRD_Channel")
|
|
|
|
config.PKT_Channel = settings.get("PKT_Channel")
|
|
|
|
|
|
|
|
config.PLC_Timeout = settings.get("PLC_Timeout")
|
|
|
|
config.RTU_Timeout = settings.get("RTU_Timeout")
|
|
|
|
config.CRD_Timeout = settings.get("CRD_Timeout")
|
|
|
|
config.PKT_Timeout = settings.get("PKT_Timeout")
|
|
|
|
|
|
|
|
config.TrustedRange = settings.get("TrustedRange")
|
|
|
|
config.AuthKey = settings.get("AuthKey")
|
|
|
|
|
|
|
|
config.LogMode = settings.get("LogMode")
|
|
|
|
config.LogPath = settings.get("LogPath")
|
|
|
|
config.LogDebug = settings.get("LogDebug")
|
|
|
|
|
2024-03-08 00:23:46 +00:00
|
|
|
config.FrontPanelTheme = settings.get("FrontPanelTheme")
|
|
|
|
config.ColorMode = settings.get("ColorMode")
|
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
local cfv = util.new_validator()
|
|
|
|
|
|
|
|
cfv.assert_type_int(config.UnitCount)
|
|
|
|
cfv.assert_range(config.UnitCount, 1, 4)
|
|
|
|
|
|
|
|
cfv.assert_type_table(config.CoolingConfig)
|
|
|
|
cfv.assert_type_table(config.FacilityTankDefs)
|
|
|
|
cfv.assert_type_int(config.FacilityTankMode)
|
|
|
|
cfv.assert_range(config.FacilityTankMode, 0, 8)
|
|
|
|
|
2024-04-12 04:13:05 +00:00
|
|
|
cfv.assert_type_bool(config.ExtChargeIdling)
|
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
cfv.assert_channel(config.SVR_Channel)
|
|
|
|
cfv.assert_channel(config.PLC_Channel)
|
|
|
|
cfv.assert_channel(config.RTU_Channel)
|
|
|
|
cfv.assert_channel(config.CRD_Channel)
|
|
|
|
cfv.assert_channel(config.PKT_Channel)
|
|
|
|
|
|
|
|
cfv.assert_type_num(config.PLC_Timeout)
|
|
|
|
cfv.assert_min(config.PLC_Timeout, 2)
|
|
|
|
cfv.assert_type_num(config.RTU_Timeout)
|
|
|
|
cfv.assert_min(config.RTU_Timeout, 2)
|
|
|
|
cfv.assert_type_num(config.CRD_Timeout)
|
|
|
|
cfv.assert_min(config.CRD_Timeout, 2)
|
|
|
|
cfv.assert_type_num(config.PKT_Timeout)
|
|
|
|
cfv.assert_min(config.PKT_Timeout, 2)
|
|
|
|
|
|
|
|
cfv.assert_type_num(config.TrustedRange)
|
2024-02-18 20:25:30 +00:00
|
|
|
cfv.assert_min(config.TrustedRange, 0)
|
2023-12-29 18:58:28 +00:00
|
|
|
|
|
|
|
if type(config.AuthKey) == "string" then
|
|
|
|
local len = string.len(config.AuthKey)
|
2024-04-12 00:30:54 +00:00
|
|
|
cfv.assert(len == 0 or len >= 8)
|
2023-12-29 18:58:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
cfv.assert_type_int(config.LogMode)
|
2024-02-18 20:25:30 +00:00
|
|
|
cfv.assert_range(config.LogMode, 0, 1)
|
2023-12-29 18:58:28 +00:00
|
|
|
cfv.assert_type_str(config.LogPath)
|
|
|
|
cfv.assert_type_bool(config.LogDebug)
|
|
|
|
|
2024-03-08 00:23:46 +00:00
|
|
|
cfv.assert_type_int(config.FrontPanelTheme)
|
|
|
|
cfv.assert_range(config.FrontPanelTheme, 1, 2)
|
|
|
|
cfv.assert_type_int(config.ColorMode)
|
2024-03-24 16:56:51 +00:00
|
|
|
cfv.assert_range(config.ColorMode, 1, themes.COLOR_MODE.NUM_MODES)
|
2024-03-08 00:23:46 +00:00
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
return cfv.valid()
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:18:33 +00:00
|
|
|
-- supervisory controller communications
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2023-05-05 18:02:25 +00:00
|
|
|
---@param _version string supervisor version
|
2023-06-25 16:59:38 +00:00
|
|
|
---@param nic nic network interface device
|
2023-06-03 19:45:48 +00:00
|
|
|
---@param fp_ok boolean if the front panel UI is running
|
2023-04-17 23:48:03 +00:00
|
|
|
---@diagnostic disable-next-line: unused-local
|
2023-06-25 16:59:38 +00:00
|
|
|
function supervisor.comms(_version, nic, fp_ok)
|
2023-06-03 19:45:48 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println(message) if not fp_ok then util.println_ts(message) end end
|
|
|
|
|
2023-08-19 17:38:05 +00:00
|
|
|
---@class sv_cooling_conf
|
2023-12-29 18:58:28 +00:00
|
|
|
local cooling_conf = { r_cool = config.CoolingConfig, fac_tank_mode = config.FacilityTankMode, fac_tank_defs = config.FacilityTankDefs }
|
2023-06-05 05:13:22 +00:00
|
|
|
|
2022-03-25 16:18:33 +00:00
|
|
|
local self = {
|
2023-02-25 04:36:16 +00:00
|
|
|
last_est_acks = {}
|
2022-03-25 16:18:33 +00:00
|
|
|
}
|
2022-04-18 15:07:16 +00:00
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
comms.set_trusted_range(config.TrustedRange)
|
2023-02-07 22:31:22 +00:00
|
|
|
|
2022-04-18 15:07:16 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-09-23 00:42:06 +00:00
|
|
|
-- configure modem channels
|
2023-06-25 16:59:38 +00:00
|
|
|
nic.closeAll()
|
2023-12-29 18:58:28 +00:00
|
|
|
nic.open(config.SVR_Channel)
|
2022-04-23 01:44:33 +00:00
|
|
|
|
2023-06-03 19:45:48 +00:00
|
|
|
-- pass modem, status, and config data to svsessions
|
2023-12-29 18:58:28 +00:00
|
|
|
svsessions.init(nic, fp_ok, config, cooling_conf)
|
2022-04-23 16:12:33 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- send an establish request response
|
|
|
|
---@param packet scada_packet
|
|
|
|
---@param ack ESTABLISH_ACK
|
|
|
|
---@param data? any optional data
|
|
|
|
local function _send_establish(packet, ack, data)
|
2022-05-13 13:45:11 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
m_pkt.make(MGMT_TYPE.ESTABLISH, { ack, data })
|
2023-06-05 05:13:22 +00:00
|
|
|
s_pkt.make(packet.src_addr(), packet.seq_num() + 1, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
nic.transmit(packet.remote_channel(), config.SVR_Channel, s_pkt)
|
2023-06-05 05:13:22 +00:00
|
|
|
self.last_est_acks[packet.src_addr()] = ack
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
2022-04-18 15:07:16 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2023-02-25 04:36:16 +00:00
|
|
|
---@class superv_comms
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
-- parse a packet
|
2023-02-25 04:36:16 +00:00
|
|
|
---@nodiscard
|
2022-05-11 16:31:19 +00:00
|
|
|
---@param side string
|
|
|
|
---@param sender integer
|
|
|
|
---@param reply_to integer
|
|
|
|
---@param message any
|
|
|
|
---@param distance integer
|
2022-07-07 04:34:42 +00:00
|
|
|
---@return modbus_frame|rplc_frame|mgmt_frame|crdn_frame|nil packet
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.parse_packet(side, sender, reply_to, message, distance)
|
2023-06-27 22:36:16 +00:00
|
|
|
local s_pkt = nic.receive(side, sender, reply_to, message, distance)
|
2022-04-22 15:07:59 +00:00
|
|
|
local pkt = nil
|
|
|
|
|
2023-06-27 22:36:16 +00:00
|
|
|
if s_pkt then
|
2022-04-22 15:07:59 +00:00
|
|
|
-- get as MODBUS TCP packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then
|
2022-04-22 15:07:59 +00:00
|
|
|
local m_pkt = comms.modbus_packet()
|
|
|
|
if m_pkt.decode(s_pkt) then
|
|
|
|
pkt = m_pkt.get()
|
|
|
|
end
|
|
|
|
-- get as RPLC packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.RPLC then
|
2022-04-22 15:07:59 +00:00
|
|
|
local rplc_pkt = comms.rplc_packet()
|
|
|
|
if rplc_pkt.decode(s_pkt) then
|
|
|
|
pkt = rplc_pkt.get()
|
|
|
|
end
|
|
|
|
-- get as SCADA management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
|
2022-04-22 15:07:59 +00:00
|
|
|
local mgmt_pkt = comms.mgmt_packet()
|
|
|
|
if mgmt_pkt.decode(s_pkt) then
|
2022-05-02 21:43:23 +00:00
|
|
|
pkt = mgmt_pkt.get()
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
-- get as coordinator packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then
|
2022-07-07 04:34:42 +00:00
|
|
|
local crdn_pkt = comms.crdn_packet()
|
|
|
|
if crdn_pkt.decode(s_pkt) then
|
|
|
|
pkt = crdn_pkt.get()
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return pkt
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
-- handle a packet
|
2022-09-21 19:53:51 +00:00
|
|
|
---@param packet modbus_frame|rplc_frame|mgmt_frame|crdn_frame|nil
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.handle_packet(packet)
|
2022-04-22 15:07:59 +00:00
|
|
|
if packet ~= nil then
|
2023-06-05 05:13:22 +00:00
|
|
|
local l_chan = packet.scada_frame.local_channel()
|
|
|
|
local r_chan = packet.scada_frame.remote_channel()
|
2023-06-06 23:45:04 +00:00
|
|
|
local src_addr = packet.scada_frame.src_addr()
|
2022-04-22 15:07:59 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
if l_chan ~= config.SVR_Channel then
|
2023-06-05 05:13:22 +00:00
|
|
|
log.debug("received packet on unconfigured channel " .. l_chan, true)
|
2023-12-29 18:58:28 +00:00
|
|
|
elseif r_chan == config.PLC_Channel then
|
2023-06-05 05:13:22 +00:00
|
|
|
-- look for an associated session
|
2023-06-06 23:45:04 +00:00
|
|
|
local session = svsessions.find_plc_session(src_addr)
|
2022-05-13 15:38:56 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
if protocol == PROTOCOL.RPLC then
|
|
|
|
---@cast packet rplc_frame
|
2022-04-22 15:07:59 +00:00
|
|
|
-- reactor PLC packet
|
2022-05-11 16:31:19 +00:00
|
|
|
if session ~= nil then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2022-04-22 15:07:59 +00:00
|
|
|
else
|
2024-02-21 17:58:49 +00:00
|
|
|
-- any other packet should be session related, discard it
|
|
|
|
log.debug("discarding RPLC packet without a known session")
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
2023-02-25 04:36:16 +00:00
|
|
|
---@cast packet mgmt_frame
|
2022-04-22 15:07:59 +00:00
|
|
|
-- SCADA management packet
|
2022-05-11 16:31:19 +00:00
|
|
|
if session ~= nil then
|
2022-05-02 15:42:24 +00:00
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- establish a new session
|
2023-06-06 23:45:04 +00:00
|
|
|
local last_ack = self.last_est_acks[src_addr]
|
2022-05-19 14:21:04 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- validate packet and continue
|
|
|
|
if packet.length >= 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
|
2023-06-05 05:13:22 +00:00
|
|
|
local comms_v = packet.data[1]
|
2022-11-13 19:13:30 +00:00
|
|
|
local firmware_v = packet.data[2]
|
2023-06-05 05:13:22 +00:00
|
|
|
local dev_type = packet.data[3]
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
if comms_v ~= comms.version then
|
2023-06-05 05:13:22 +00:00
|
|
|
if last_ack ~= ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
log.info(util.c("dropping PLC establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
|
2023-02-25 04:36:16 +00:00
|
|
|
end
|
2022-05-13 13:45:11 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
|
2023-02-25 04:36:16 +00:00
|
|
|
elseif dev_type == DEVICE_TYPE.PLC then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- PLC linking request
|
|
|
|
if packet.length == 4 and type(packet.data[4]) == "number" then
|
|
|
|
local reactor_id = packet.data[4]
|
2023-06-06 23:45:04 +00:00
|
|
|
local plc_id = svsessions.establish_plc_session(src_addr, reactor_id, firmware_v)
|
2022-10-20 17:53:39 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
if plc_id == false then
|
|
|
|
-- reactor already has a PLC assigned
|
2023-06-05 05:13:22 +00:00
|
|
|
if last_ack ~= ESTABLISH_ACK.COLLISION then
|
2023-02-25 04:36:16 +00:00
|
|
|
log.warning(util.c("PLC_ESTABLISH: assignment collision with reactor ", reactor_id))
|
|
|
|
end
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.COLLISION)
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
-- got an ID; assigned to a reactor successfully
|
2023-06-06 23:45:04 +00:00
|
|
|
println(util.c("PLC (", firmware_v, ") [@", src_addr, "] \xbb reactor ", reactor_id, " connected"))
|
|
|
|
log.info(util.c("PLC_ESTABLISH: PLC (", firmware_v, ") [@", src_addr, "] reactor unit ", reactor_id, " PLC connected with session ID ", plc_id))
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("PLC_ESTABLISH: packet length mismatch/bad parameter type")
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2023-06-05 05:13:22 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug(util.c("illegal establish packet for device ", dev_type, " on PLC channel"))
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
|
|
|
end
|
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug("invalid establish packet (on PLC channel)")
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding PLC SCADA_MGMT packet without a known session from computer ", src_addr))
|
2023-06-05 05:13:22 +00:00
|
|
|
end
|
2023-06-05 21:24:00 +00:00
|
|
|
else
|
|
|
|
log.debug(util.c("illegal packet type ", protocol, " on PLC channel"))
|
2023-06-05 05:13:22 +00:00
|
|
|
end
|
2023-12-29 18:58:28 +00:00
|
|
|
elseif r_chan == config.RTU_Channel then
|
2023-06-05 05:13:22 +00:00
|
|
|
-- look for an associated session
|
2023-06-06 23:45:04 +00:00
|
|
|
local session = svsessions.find_rtu_session(src_addr)
|
2023-06-05 05:13:22 +00:00
|
|
|
|
|
|
|
if protocol == PROTOCOL.MODBUS_TCP then
|
|
|
|
---@cast packet modbus_frame
|
|
|
|
-- MODBUS response
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
|
|
|
log.debug("discarding MODBUS_TCP packet without a known session")
|
|
|
|
end
|
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
|
|
|
---@cast packet mgmt_frame
|
|
|
|
-- SCADA management packet
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2023-06-05 05:13:22 +00:00
|
|
|
-- establish a new session
|
2023-06-06 23:45:04 +00:00
|
|
|
local last_ack = self.last_est_acks[src_addr]
|
2023-06-05 05:13:22 +00:00
|
|
|
|
|
|
|
-- validate packet and continue
|
|
|
|
if packet.length >= 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
|
|
|
|
local comms_v = packet.data[1]
|
|
|
|
local firmware_v = packet.data[2]
|
|
|
|
local dev_type = packet.data[3]
|
|
|
|
|
|
|
|
if comms_v ~= comms.version then
|
|
|
|
if last_ack ~= ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
log.info(util.c("dropping RTU establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
|
|
|
|
end
|
|
|
|
|
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif dev_type == DEVICE_TYPE.RTU then
|
2022-11-13 19:13:30 +00:00
|
|
|
if packet.length == 4 then
|
|
|
|
-- this is an RTU advertisement for a new session
|
|
|
|
local rtu_advert = packet.data[4]
|
2023-06-06 23:45:04 +00:00
|
|
|
local s_id = svsessions.establish_rtu_session(src_addr, rtu_advert, firmware_v)
|
2023-02-25 04:36:16 +00:00
|
|
|
|
2023-06-06 23:45:04 +00:00
|
|
|
println(util.c("RTU (", firmware_v, ") [@", src_addr, "] \xbb connected"))
|
|
|
|
log.info(util.c("RTU_ESTABLISH: RTU (",firmware_v, ") [@", src_addr, "] connected with session ID ", s_id))
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
log.debug("RTU_ESTABLISH: packet length mismatch")
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug(util.c("illegal establish packet for device ", dev_type, " on RTU channel"))
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2022-05-13 13:45:11 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug("invalid establish packet (on RTU channel)")
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
2022-05-19 14:21:04 +00:00
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding RTU SCADA_MGMT packet without a known session from computer ", src_addr))
|
2022-05-02 15:42:24 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug(util.c("illegal packet type ", protocol, " on RTU channel"))
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
2023-12-29 18:58:28 +00:00
|
|
|
elseif r_chan == config.CRD_Channel then
|
2022-05-13 15:38:56 +00:00
|
|
|
-- look for an associated session
|
2023-06-06 23:45:04 +00:00
|
|
|
local session = svsessions.find_crd_session(src_addr)
|
2022-05-13 15:38:56 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if protocol == PROTOCOL.SCADA_MGMT then
|
2023-02-25 04:36:16 +00:00
|
|
|
---@cast packet mgmt_frame
|
2022-04-22 15:07:59 +00:00
|
|
|
-- SCADA management packet
|
2022-07-07 04:34:42 +00:00
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- establish a new session
|
2023-06-06 23:45:04 +00:00
|
|
|
local last_ack = self.last_est_acks[src_addr]
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
-- validate packet and continue
|
|
|
|
if packet.length >= 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
|
2023-06-05 05:13:22 +00:00
|
|
|
local comms_v = packet.data[1]
|
2022-11-13 19:13:30 +00:00
|
|
|
local firmware_v = packet.data[2]
|
2023-06-05 05:13:22 +00:00
|
|
|
local dev_type = packet.data[3]
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
if comms_v ~= comms.version then
|
2023-06-05 05:13:22 +00:00
|
|
|
if last_ack ~= ESTABLISH_ACK.BAD_VERSION then
|
2023-02-25 04:36:16 +00:00
|
|
|
log.info(util.c("dropping coordinator establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
|
|
|
|
end
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
|
2023-08-30 21:15:42 +00:00
|
|
|
elseif dev_type == DEVICE_TYPE.CRD then
|
2023-04-20 00:35:42 +00:00
|
|
|
-- this is an attempt to establish a new coordinator session
|
2023-06-06 23:45:04 +00:00
|
|
|
local s_id = svsessions.establish_crd_session(src_addr, firmware_v)
|
2023-02-25 04:36:16 +00:00
|
|
|
|
|
|
|
if s_id ~= false then
|
2023-06-06 23:45:04 +00:00
|
|
|
println(util.c("CRD (", firmware_v, ") [@", src_addr, "] \xbb connected"))
|
|
|
|
log.info(util.c("CRD_ESTABLISH: coordinator (", firmware_v, ") [@", src_addr, "] connected with session ID ", s_id))
|
2022-07-07 04:34:42 +00:00
|
|
|
|
2023-12-29 18:58:28 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW, { config.UnitCount, cooling_conf })
|
2023-02-25 04:36:16 +00:00
|
|
|
else
|
2023-06-05 05:13:22 +00:00
|
|
|
if last_ack ~= ESTABLISH_ACK.COLLISION then
|
2023-06-06 23:45:04 +00:00
|
|
|
log.info("CRD_ESTABLISH: denied new coordinator [@" .. src_addr .. "] due to already being connected to another coordinator")
|
2023-02-25 04:36:16 +00:00
|
|
|
end
|
2022-11-13 19:13:30 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.COLLISION)
|
2023-02-25 04:36:16 +00:00
|
|
|
end
|
2023-06-05 21:24:00 +00:00
|
|
|
else
|
|
|
|
log.debug(util.c("illegal establish packet for device ", dev_type, " on coordinator channel"))
|
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("CRD_ESTABLISH: establish packet length mismatch")
|
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding coordinator SCADA_MGMT packet without a known session from computer ", src_addr))
|
2023-06-05 21:24:00 +00:00
|
|
|
end
|
|
|
|
elseif protocol == PROTOCOL.SCADA_CRDN then
|
|
|
|
---@cast packet crdn_frame
|
|
|
|
-- coordinator packet
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding coordinator SCADA_CRDN packet without a known session from computer ", src_addr))
|
2023-06-05 21:24:00 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug(util.c("illegal packet type ", protocol, " on coordinator channel"))
|
|
|
|
end
|
2023-12-29 18:58:28 +00:00
|
|
|
elseif r_chan == config.PKT_Channel then
|
2023-06-05 21:24:00 +00:00
|
|
|
-- look for an associated session
|
2023-06-06 23:45:04 +00:00
|
|
|
local session = svsessions.find_pdg_session(src_addr)
|
2023-06-05 21:24:00 +00:00
|
|
|
|
|
|
|
if protocol == PROTOCOL.SCADA_MGMT then
|
|
|
|
---@cast packet mgmt_frame
|
|
|
|
-- SCADA management packet
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2023-06-05 21:24:00 +00:00
|
|
|
-- establish a new session
|
2023-06-06 23:45:04 +00:00
|
|
|
local last_ack = self.last_est_acks[src_addr]
|
2023-06-05 21:24:00 +00:00
|
|
|
|
|
|
|
-- validate packet and continue
|
|
|
|
if packet.length >= 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
|
|
|
|
local comms_v = packet.data[1]
|
|
|
|
local firmware_v = packet.data[2]
|
|
|
|
local dev_type = packet.data[3]
|
|
|
|
|
|
|
|
if comms_v ~= comms.version then
|
|
|
|
if last_ack ~= ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
log.info(util.c("dropping PDG establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
|
|
|
|
end
|
|
|
|
|
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
|
2023-04-20 00:35:42 +00:00
|
|
|
elseif dev_type == DEVICE_TYPE.PKT then
|
|
|
|
-- this is an attempt to establish a new pocket diagnostic session
|
2023-06-06 23:45:04 +00:00
|
|
|
local s_id = svsessions.establish_pdg_session(src_addr, firmware_v)
|
2023-04-20 00:35:42 +00:00
|
|
|
|
2023-06-07 18:18:13 +00:00
|
|
|
println(util.c("PKT (", firmware_v, ") [@", src_addr, "] \xbb connected"))
|
|
|
|
log.info(util.c("PDG_ESTABLISH: pocket (", firmware_v, ") [@", src_addr, "] connected with session ID ", s_id))
|
2023-04-20 00:35:42 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.ALLOW)
|
2023-04-20 00:35:42 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug(util.c("illegal establish packet for device ", dev_type, " on pocket channel"))
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-10-20 17:53:39 +00:00
|
|
|
end
|
2022-07-07 04:34:42 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug("PDG_ESTABLISH: establish packet length mismatch")
|
2023-06-05 05:13:22 +00:00
|
|
|
_send_establish(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding pocket SCADA_MGMT packet without a known session from computer ", src_addr))
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_CRDN then
|
2023-02-25 04:36:16 +00:00
|
|
|
---@cast packet crdn_frame
|
2022-11-13 19:13:30 +00:00
|
|
|
-- coordinator packet
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2022-07-07 04:34:42 +00:00
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:45:04 +00:00
|
|
|
log.debug(util.c("discarding pocket SCADA_CRDN packet without a known session from computer ", src_addr))
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
else
|
2023-06-05 21:24:00 +00:00
|
|
|
log.debug(util.c("illegal packet type ", protocol, " on pocket channel"))
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
else
|
2023-06-05 05:13:22 +00:00
|
|
|
log.debug("received packet for unknown channel " .. r_chan, true)
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:31:19 +00:00
|
|
|
return public
|
2022-03-25 16:18:33 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return supervisor
|