cc-mek-scada/supervisor/supervisor.lua

327 lines
15 KiB
Lua
Raw Normal View History

local comms = require("scada-common.comms")
local log = require("scada-common.log")
local util = require("scada-common.util")
2022-05-11 15:31:02 +00:00
local svsessions = require("supervisor.session.svsessions")
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
local ESTABLISH_ACK = comms.ESTABLISH_ACK
2023-02-21 16:05:57 +00:00
local SCADA_MGMT_TYPE = comms.SCADA_MGMT_TYPE
2022-04-22 15:07:59 +00:00
local print = util.print
local println = util.println
local print_ts = util.print_ts
local println_ts = util.println_ts
2022-03-25 16:18:33 +00:00
-- supervisory controller communications
---@param version string supervisor version
---@param num_reactors integer number of reactors
---@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 range integer trusted device connection range
function supervisor.comms(version, num_reactors, cooling_conf, modem, dev_listen, coord_listen, range)
2022-03-25 16:18:33 +00:00
local self = {
2022-05-19 14:21:04 +00:00
version = version,
2022-03-25 16:18:33 +00:00
num_reactors = num_reactors,
modem = modem,
dev_listen = dev_listen,
2022-04-22 15:07:59 +00:00
coord_listen = coord_listen,
2022-03-25 16:18:33 +00:00
reactor_struct_cache = nil
}
2022-04-18 15:07:16 +00:00
2022-05-11 16:31:19 +00:00
---@class superv_comms
local public = {}
comms.set_trusted_range(range)
2022-04-18 15:07:16 +00:00
-- PRIVATE FUNCTIONS --
-- configure modem channels
local function _conf_channels()
self.modem.closeAll()
self.modem.open(self.dev_listen)
self.modem.open(self.coord_listen)
2022-04-18 15:07:16 +00:00
end
_conf_channels()
2022-04-23 16:12:33 +00:00
-- link modem to svsessions
svsessions.init(self.modem, num_reactors, cooling_conf)
2022-04-23 16:12:33 +00:00
-- send an establish request response to a PLC/RTU
2022-05-11 16:31:19 +00:00
---@param dest integer
---@param msg table
local function _send_dev_establish(seq_id, dest, msg)
local s_pkt = comms.scada_packet()
local m_pkt = comms.mgmt_packet()
2023-02-21 16:05:57 +00:00
m_pkt.make(SCADA_MGMT_TYPE.ESTABLISH, msg)
s_pkt.make(seq_id, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
self.modem.transmit(dest, self.dev_listen, s_pkt.raw_sendable())
2022-04-22 15:07:59 +00:00
end
-- send coordinator connection establish response
---@param seq_id integer
---@param dest integer
---@param msg table
local function _send_crdn_establish(seq_id, dest, msg)
local s_pkt = comms.scada_packet()
local c_pkt = comms.mgmt_packet()
2023-02-21 16:05:57 +00:00
c_pkt.make(SCADA_MGMT_TYPE.ESTABLISH, msg)
s_pkt.make(seq_id, PROTOCOL.SCADA_MGMT, c_pkt.raw_sendable())
self.modem.transmit(dest, self.coord_listen, s_pkt.raw_sendable())
end
2022-04-18 15:07:16 +00:00
-- PUBLIC FUNCTIONS --
-- reconnect a newly connected modem
2022-05-11 16:31:19 +00:00
---@param modem table
---@diagnostic disable-next-line: redefined-local
2022-05-31 19:36:17 +00:00
function public.reconnect_modem(modem)
2022-04-18 15:07:16 +00:00
self.modem = modem
svsessions.relink_modem(self.modem)
_conf_channels()
2022-04-18 15:07:16 +00:00
end
2022-04-22 15:07:59 +00:00
-- parse a packet
2022-05-11 16:31:19 +00:00
---@param side string
---@param sender integer
---@param reply_to integer
---@param message any
---@param distance integer
---@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)
2022-04-22 15:07:59 +00:00
local pkt = nil
local s_pkt = comms.scada_packet()
2022-04-22 15:07:59 +00:00
-- parse packet as generic SCADA packet
s_pkt.receive(side, sender, reply_to, message, distance)
2022-04-22 15:07:59 +00:00
if s_pkt.is_valid() then
-- 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
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
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
---@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
local l_port = packet.scada_frame.local_port()
local r_port = packet.scada_frame.remote_port()
2022-04-22 15:07:59 +00:00
local protocol = packet.scada_frame.protocol()
-- device (RTU/PLC) listening channel
if l_port == self.dev_listen then
2023-02-21 16:05:57 +00:00
if protocol == PROTOCOL.MODBUS_TCP then
2022-05-13 15:38:56 +00:00
-- look for an associated session
local session = svsessions.find_rtu_session(r_port)
2022-04-22 15:07:59 +00:00
-- 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
2023-02-21 16:05:57 +00:00
elseif protocol == PROTOCOL.RPLC then
2022-05-13 15:38:56 +00:00
-- look for an associated session
local session = svsessions.find_plc_session(r_port)
2022-04-22 15:07:59 +00:00
-- reactor PLC packet
2022-05-11 16:31:19 +00:00
if session ~= nil then
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
2022-04-22 15:07:59 +00:00
else
-- unknown session, force a re-link
log.debug("PLC_ESTABLISH: no session but not an establish, forcing relink")
_send_dev_establish(packet.scada_frame.seq_num() + 1, r_port, { ESTABLISH_ACK.DENY })
2022-04-22 15:07:59 +00:00
end
2023-02-21 16:05:57 +00:00
elseif protocol == PROTOCOL.SCADA_MGMT then
2022-05-13 15:38:56 +00:00
-- look for an associated session
local session = svsessions.find_device_session(r_port)
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-02-21 16:05:57 +00:00
elseif packet.type == SCADA_MGMT_TYPE.ESTABLISH then
-- establish a new session
local next_seq_id = packet.scada_frame.seq_num() + 1
2022-05-19 14:21:04 +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
log.debug(util.c("dropping establish packet with incorrect comms version v", comms_v,
" (expected v", comms.version, ")"))
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.BAD_VERSION })
return
end
2023-02-21 16:05:57 +00:00
if dev_type == DEVICE_TYPE.PLC then
-- PLC linking request
if packet.length == 4 and type(packet.data[4]) == "number" then
local reactor_id = packet.data[4]
local plc_id = svsessions.establish_plc_session(l_port, r_port, reactor_id, firmware_v)
2022-10-20 17:53:39 +00:00
if plc_id == false then
-- reactor already has a PLC assigned
log.warning(util.c("PLC_ESTABLISH: assignment collision with reactor ", reactor_id))
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.COLLISION })
else
-- got an ID; assigned to a reactor successfully
println(util.c("PLC (", firmware_v, ") [:", r_port, "] \xbb reactor ", reactor_id, " connected"))
log.info(util.c("PLC_ESTABLISH: PLC (", firmware_v, ") [:", r_port, "] reactor unit ", reactor_id, " PLC connected with session ID ", plc_id))
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW })
end
else
log.debug("PLC_ESTABLISH: packet length mismatch/bad parameter type")
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY })
end
2023-02-21 16:05:57 +00:00
elseif dev_type == DEVICE_TYPE.RTU then
if packet.length == 4 then
-- this is an RTU advertisement for a new session
local rtu_advert = packet.data[4]
local s_id = svsessions.establish_rtu_session(l_port, r_port, rtu_advert, firmware_v)
println(util.c("RTU (", firmware_v, ") [:", r_port, "] \xbb connected"))
log.info(util.c("RTU_ESTABLISH: RTU (",firmware_v, ") [:", r_port, "] connected with session ID ", s_id))
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW })
else
log.debug("RTU_ESTABLISH: packet length mismatch")
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY })
end
else
log.debug(util.c("illegal establish packet for device ", dev_type, " on PLC/RTU listening channel"))
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY })
end
else
log.debug("invalid establish packet (on PLC/RTU listening channel)")
_send_dev_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY })
end
2022-05-19 14:21:04 +00:00
else
-- any other packet should be session related, discard it
log.debug(util.c(r_port, "->", l_port, ": discarding SCADA_MGMT packet without a known session"))
2022-05-02 15:42:24 +00:00
end
2022-04-22 15:07:59 +00:00
else
log.debug("illegal packet type " .. protocol .. " on device listening channel")
2022-04-22 15:07:59 +00:00
end
-- coordinator listening channel
elseif l_port == self.coord_listen then
2022-05-13 15:38:56 +00:00
-- look for an associated session
local session = svsessions.find_coord_session(r_port)
2023-02-21 16:05:57 +00:00
if protocol == PROTOCOL.SCADA_MGMT then
2022-04-22 15:07:59 +00:00
-- SCADA management packet
if session ~= nil then
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
2023-02-21 16:05:57 +00:00
elseif packet.type == SCADA_MGMT_TYPE.ESTABLISH then
-- establish a new session
local next_seq_id = packet.scada_frame.seq_num() + 1
-- 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
log.debug(util.c("dropping establish packet with incorrect comms version v", comms_v,
" (expected v", comms.version, ")"))
_send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.BAD_VERSION })
return
2023-02-21 16:05:57 +00:00
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 })
return
end
-- this is an attempt to establish a new session
local s_id = svsessions.establish_coord_session(l_port, r_port, firmware_v)
2022-10-20 17:53:39 +00:00
if s_id ~= false then
local config = { self.num_reactors }
for i = 1, #cooling_conf do
table.insert(config, cooling_conf[i].BOILERS)
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))
_send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.ALLOW, config })
2022-10-20 17:53:39 +00:00
else
log.debug("CRDN_ESTABLISH: denied new coordinator due to already being connected to another coordinator")
_send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.COLLISION })
2022-10-20 17:53:39 +00:00
end
else
log.debug("CRDN_ESTABLISH: establish packet length mismatch")
_send_crdn_establish(next_seq_id, r_port, { ESTABLISH_ACK.DENY })
end
else
-- any other packet should be session related, discard it
log.debug(r_port .. "->" .. l_port .. ": discarding SCADA_MGMT packet without a known session")
end
2023-02-21 16:05:57 +00:00
elseif protocol == PROTOCOL.SCADA_CRDN then
-- 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
2022-10-20 17:53:39 +00:00
log.debug(r_port .. "->" .. l_port .. ": discarding SCADA_CRDN packet without a known session")
end
2022-04-22 15:07:59 +00:00
else
log.debug("illegal packet type " .. protocol .. " on coordinator listening channel")
2022-04-22 15:07:59 +00:00
end
else
log.warning("received packet on unconfigured channel " .. l_port)
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
return supervisor