2022-05-31 19:36:17 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local log = require("scada-common.log")
|
2022-05-09 13:35:39 +00:00
|
|
|
local mqueue = require("scada-common.mqueue")
|
2022-05-31 19:36:17 +00:00
|
|
|
local rsio = require("scada-common.rsio")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-09 13:35:39 +00:00
|
|
|
|
2022-05-11 17:02:21 +00:00
|
|
|
-- supervisor rtu sessions (svrs)
|
2022-05-31 19:36:17 +00:00
|
|
|
local svrs_boiler = require("supervisor.session.rtu.boiler")
|
2022-06-04 21:59:24 +00:00
|
|
|
local svrs_boilerv = require("supervisor.session.rtu.boilerv")
|
2022-05-13 13:45:11 +00:00
|
|
|
local svrs_emachine = require("supervisor.session.rtu.emachine")
|
2022-06-04 15:11:35 +00:00
|
|
|
local svrs_envd = require("supervisor.session.rtu.envd")
|
2022-06-04 21:45:52 +00:00
|
|
|
local svrs_imatrix = require("supervisor.session.rtu.imatrix")
|
2022-05-15 00:27:06 +00:00
|
|
|
local svrs_redstone = require("supervisor.session.rtu.redstone")
|
2022-06-04 20:25:23 +00:00
|
|
|
local svrs_sna = require("supervisor.session.rtu.sna")
|
2022-06-04 19:33:04 +00:00
|
|
|
local svrs_sps = require("supervisor.session.rtu.sps")
|
2022-05-31 19:36:17 +00:00
|
|
|
local svrs_turbine = require("supervisor.session.rtu.turbine")
|
2022-05-11 17:02:21 +00:00
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
local rtu = {}
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
local PROTOCOLS = comms.PROTOCOLS
|
|
|
|
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
|
2022-05-13 14:27:57 +00:00
|
|
|
local RTU_UNIT_TYPES = comms.RTU_UNIT_TYPES
|
2022-05-09 13:35:39 +00:00
|
|
|
|
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
|
|
|
local print_ts = util.print_ts
|
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
local RTU_S_CMDS = {
|
|
|
|
}
|
|
|
|
|
|
|
|
local RTU_S_DATA = {
|
|
|
|
RS_COMMAND = 1,
|
|
|
|
UNIT_COMMAND = 2
|
|
|
|
}
|
|
|
|
|
|
|
|
rtu.RTU_S_CMDS = RTU_S_CMDS
|
|
|
|
rtu.RTU_S_DATA = RTU_S_DATA
|
|
|
|
|
2022-05-10 16:01:56 +00:00
|
|
|
local PERIODICS = {
|
|
|
|
KEEP_ALIVE = 2.0
|
|
|
|
}
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
---@class rs_session_command
|
|
|
|
---@field reactor integer
|
|
|
|
---@field channel RS_IO
|
2022-05-16 15:54:34 +00:00
|
|
|
---@field value integer|boolean
|
2022-05-15 00:27:06 +00:00
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
-- create a new RTU session
|
|
|
|
---@param id integer
|
|
|
|
---@param in_queue mqueue
|
|
|
|
---@param out_queue mqueue
|
|
|
|
---@param advertisement table
|
2022-05-31 19:36:17 +00:00
|
|
|
function rtu.new_session(id, in_queue, out_queue, advertisement)
|
2022-05-09 13:35:39 +00:00
|
|
|
local log_header = "rtu_session(" .. id .. "): "
|
|
|
|
|
|
|
|
local self = {
|
|
|
|
id = id,
|
|
|
|
in_q = in_queue,
|
|
|
|
out_q = out_queue,
|
2022-05-13 13:45:11 +00:00
|
|
|
advert = advertisement,
|
2022-05-09 13:35:39 +00:00
|
|
|
-- connection properties
|
|
|
|
seq_num = 0,
|
|
|
|
r_seq_num = nil,
|
|
|
|
connected = true,
|
|
|
|
rtu_conn_watchdog = util.new_watchdog(3),
|
2022-05-13 13:45:11 +00:00
|
|
|
last_rtt = 0,
|
2022-05-16 15:54:34 +00:00
|
|
|
rs_io_q = {},
|
2022-05-13 13:45:11 +00:00
|
|
|
units = {}
|
2022-05-09 13:35:39 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
---@class rtu_session
|
|
|
|
local public = {}
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
-- parse the recorded advertisement and create unit sub-sessions
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _handle_advertisement()
|
2022-05-13 13:45:11 +00:00
|
|
|
self.units = {}
|
2022-05-16 15:54:34 +00:00
|
|
|
self.rs_io_q = {}
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
for i = 1, #self.advert do
|
2022-05-15 00:27:06 +00:00
|
|
|
local unit = nil ---@type unit_session|nil
|
2022-05-16 15:54:34 +00:00
|
|
|
local rs_in_q = nil ---@type mqueue|nil
|
2022-05-13 13:45:11 +00:00
|
|
|
|
|
|
|
---@type rtu_advertisement
|
|
|
|
local unit_advert = {
|
2022-05-15 00:27:06 +00:00
|
|
|
type = self.advert[i][1],
|
|
|
|
index = self.advert[i][2],
|
|
|
|
reactor = self.advert[i][3],
|
|
|
|
rsio = self.advert[i][4]
|
2022-05-13 13:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local u_type = unit_advert.type
|
|
|
|
|
|
|
|
-- create unit by type
|
2022-05-13 14:27:57 +00:00
|
|
|
if u_type == RTU_UNIT_TYPES.REDSTONE then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- redstone
|
2022-05-18 17:49:04 +00:00
|
|
|
unit, rs_in_q = svrs_redstone.new(self.id, i, unit_advert, self.out_q)
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.BOILER then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- boiler
|
2022-05-18 17:49:04 +00:00
|
|
|
unit = svrs_boiler.new(self.id, i, unit_advert, self.out_q)
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.BOILER_VALVE then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- boiler (Mekanism 10.1+)
|
2022-06-04 21:59:24 +00:00
|
|
|
unit = svrs_boilerv.new(self.id, 1, unit_advert, self.out_q)
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.TURBINE then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- turbine
|
2022-05-18 17:49:04 +00:00
|
|
|
unit = svrs_turbine.new(self.id, i, unit_advert, self.out_q)
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.TURBINE_VALVE then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- turbine (Mekanism 10.1+)
|
2022-05-13 13:45:11 +00:00
|
|
|
-- @todo Mekanism 10.1+
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.EMACHINE then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- mekanism [energy] machine
|
2022-05-18 17:49:04 +00:00
|
|
|
unit = svrs_emachine.new(self.id, i, unit_advert, self.out_q)
|
2022-05-13 14:27:57 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.IMATRIX then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- induction matrix
|
|
|
|
unit = svrs_imatrix.new(self.id, i, unit_advert, self.out_q)
|
2022-06-04 14:49:36 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.SPS then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- super-critical phase shifter
|
2022-06-04 19:33:04 +00:00
|
|
|
unit = svrs_sps.new(self.id, i, unit_advert, self.out_q)
|
2022-06-04 14:49:36 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.SNA then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- solar neutron activator
|
2022-06-04 20:25:23 +00:00
|
|
|
unit = svrs_sna.new(self.id, i, unit_advert, self.out_q)
|
2022-06-04 14:49:36 +00:00
|
|
|
elseif u_type == RTU_UNIT_TYPES.ENV_DETECTOR then
|
2022-06-04 21:45:52 +00:00
|
|
|
-- environment detector
|
2022-06-04 15:11:35 +00:00
|
|
|
unit = svrs_envd.new(self.id, i, unit_advert, self.out_q)
|
2022-05-15 00:27:06 +00:00
|
|
|
else
|
|
|
|
log.error(log_header .. "bad advertisement: encountered unsupported RTU type")
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if unit ~= nil then
|
|
|
|
table.insert(self.units, unit)
|
2022-05-16 15:54:34 +00:00
|
|
|
|
|
|
|
if self.rs_io_q[unit_advert.reactor] == nil then
|
|
|
|
self.rs_io_q[unit_advert.reactor] = rs_in_q
|
|
|
|
else
|
|
|
|
self.units = {}
|
|
|
|
self.rs_io_q = {}
|
|
|
|
log.error(log_header .. "bad advertisement: duplicate redstone RTU for reactor " .. unit_advert.reactor)
|
|
|
|
break
|
|
|
|
end
|
2022-05-13 13:45:11 +00:00
|
|
|
else
|
|
|
|
self.units = {}
|
2022-05-16 15:54:34 +00:00
|
|
|
self.rs_io_q = {}
|
|
|
|
|
|
|
|
local type_string = comms.advert_type_to_rtu_t(u_type)
|
|
|
|
if type_string == nil then type_string = "unknown" end
|
|
|
|
|
|
|
|
log.error(log_header .. "bad advertisement: error occured while creating a unit (type is " .. type_string .. ")")
|
2022-05-13 13:45:11 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-16 21:13:54 +00:00
|
|
|
-- mark this RTU session as closed, stop watchdog
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _close()
|
2022-05-16 21:13:54 +00:00
|
|
|
self.rtu_conn_watchdog.cancel()
|
|
|
|
self.connected = false
|
|
|
|
|
|
|
|
-- mark all RTU unit sessions as closed so the reactor unit knows
|
|
|
|
for i = 1, #self.units do
|
|
|
|
self.units[i].close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
-- send a SCADA management packet
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param msg_type SCADA_MGMT_TYPES
|
|
|
|
---@param msg table
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _send_mgmt(msg_type, msg)
|
2022-05-09 13:35:39 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
|
|
|
m_pkt.make(msg_type, msg)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.SCADA_MGMT, m_pkt.raw_sendable())
|
|
|
|
|
|
|
|
self.out_q.push_packet(s_pkt)
|
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle a packet
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param pkt modbus_frame|mgmt_frame
|
2022-05-31 19:36:17 +00:00
|
|
|
local function _handle_packet(pkt)
|
2022-05-09 13:35:39 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.r_seq_num == nil then
|
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
|
|
|
elseif self.r_seq_num >= pkt.scada_frame.seq_num() then
|
|
|
|
log.warning(log_header .. "sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. pkt.scada_frame.seq_num())
|
|
|
|
return
|
|
|
|
else
|
|
|
|
self.r_seq_num = pkt.scada_frame.seq_num()
|
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- feed watchdog
|
|
|
|
self.rtu_conn_watchdog.feed()
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
-- process packet
|
|
|
|
if pkt.scada_frame.protocol() == PROTOCOLS.MODBUS_TCP then
|
2022-05-13 13:45:11 +00:00
|
|
|
if self.units[pkt.unit_id] ~= nil then
|
2022-05-13 13:49:24 +00:00
|
|
|
local unit = self.units[pkt.unit_id] ---@type unit_session
|
2022-05-13 13:45:11 +00:00
|
|
|
unit.handle_packet(pkt)
|
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
elseif pkt.scada_frame.protocol() == PROTOCOLS.SCADA_MGMT then
|
2022-05-15 00:27:06 +00:00
|
|
|
-- handle management packet
|
2022-05-09 19:00:16 +00:00
|
|
|
if pkt.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
|
|
|
-- keep alive reply
|
|
|
|
if pkt.length == 2 then
|
|
|
|
local srv_start = pkt.data[1]
|
|
|
|
local rtu_send = pkt.data[2]
|
|
|
|
local srv_now = util.time()
|
|
|
|
self.last_rtt = srv_now - srv_start
|
|
|
|
|
|
|
|
if self.last_rtt > 500 then
|
|
|
|
log.warning(log_header .. "RTU KEEP_ALIVE round trip time > 500ms (" .. self.last_rtt .. "ms)")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- log.debug(log_header .. "RTU RTT = ".. self.last_rtt .. "ms")
|
|
|
|
-- log.debug(log_header .. "RTU TT = ".. (srv_now - rtu_send) .. "ms")
|
|
|
|
else
|
|
|
|
log.debug(log_header .. "SCADA keep alive packet length mismatch")
|
|
|
|
end
|
|
|
|
elseif pkt.type == SCADA_MGMT_TYPES.CLOSE then
|
2022-05-09 13:35:39 +00:00
|
|
|
-- close the session
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2022-05-09 13:35:39 +00:00
|
|
|
elseif pkt.type == SCADA_MGMT_TYPES.RTU_ADVERT then
|
|
|
|
-- RTU unit advertisement
|
2022-05-15 00:27:06 +00:00
|
|
|
-- handle advertisement; this will re-create all unit sub-sessions
|
2022-05-13 13:45:11 +00:00
|
|
|
self.advert = pkt.data
|
2022-05-15 00:27:06 +00:00
|
|
|
_handle_advertisement()
|
2022-05-09 13:35:39 +00:00
|
|
|
else
|
|
|
|
log.debug(log_header .. "handler received unsupported SCADA_MGMT packet type " .. pkt.type)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
|
|
|
-- get the session ID
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.get_id() return self.id end
|
2022-05-09 13:35:39 +00:00
|
|
|
|
|
|
|
-- check if a timer matches this session's watchdog
|
2022-05-13 13:45:11 +00:00
|
|
|
---@param timer number
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.check_wd(timer)
|
2022-05-16 21:13:54 +00:00
|
|
|
return self.rtu_conn_watchdog.is_timer(timer) and self.connected
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.close()
|
2022-05-16 21:13:54 +00:00
|
|
|
_close()
|
2022-05-09 13:35:39 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.CLOSE, {})
|
|
|
|
println(log_header .. "connection to RTU closed by server")
|
|
|
|
log.info(log_header .. "session closed by server")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- iterate the session
|
2022-05-13 13:45:11 +00:00
|
|
|
---@return boolean connected
|
2022-05-31 19:36:17 +00:00
|
|
|
function public.iterate()
|
2022-05-09 13:35:39 +00:00
|
|
|
if self.connected then
|
|
|
|
------------------
|
|
|
|
-- handle queue --
|
|
|
|
------------------
|
|
|
|
|
|
|
|
local handle_start = util.time()
|
|
|
|
|
|
|
|
while self.in_q.ready() and self.connected do
|
|
|
|
-- get a new message to process
|
2022-05-13 13:45:11 +00:00
|
|
|
local msg = self.in_q.pop()
|
|
|
|
|
|
|
|
if msg ~= nil then
|
|
|
|
if msg.qtype == mqueue.TYPE.PACKET then
|
|
|
|
-- handle a packet
|
|
|
|
_handle_packet(msg.message)
|
|
|
|
elseif msg.qtype == mqueue.TYPE.COMMAND then
|
|
|
|
-- handle instruction
|
|
|
|
elseif msg.qtype == mqueue.TYPE.DATA then
|
|
|
|
-- instruction with body
|
2022-05-15 00:27:06 +00:00
|
|
|
local cmd = msg.message ---@type queue_data
|
|
|
|
|
|
|
|
if cmd.key == RTU_S_DATA.RS_COMMAND then
|
|
|
|
local rs_cmd = cmd.val ---@type rs_session_command
|
2022-05-16 15:54:34 +00:00
|
|
|
|
|
|
|
if rsio.is_valid_channel(rs_cmd.channel) then
|
|
|
|
cmd.key = svrs_redstone.RS_RTU_S_DATA.RS_COMMAND
|
|
|
|
if rs_cmd.reactor == nil then
|
|
|
|
-- for all reactors (facility)
|
|
|
|
for i = 1, #self.rs_io_q do
|
|
|
|
local q = self.rs_io.q[i] ---@type mqueue
|
|
|
|
q.push_data(msg)
|
|
|
|
end
|
|
|
|
elseif self.rs_io_q[rs_cmd.reactor] ~= nil then
|
|
|
|
-- for just one reactor
|
|
|
|
local q = self.rs_io.q[rs_cmd.reactor] ---@type mqueue
|
|
|
|
q.push_data(msg)
|
|
|
|
end
|
|
|
|
end
|
2022-05-15 00:27:06 +00:00
|
|
|
end
|
2022-05-13 13:45:11 +00:00
|
|
|
end
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- max 100ms spent processing queue
|
|
|
|
if util.time() - handle_start > 100 then
|
|
|
|
log.warning(log_header .. "exceeded 100ms queue process limit")
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- exit if connection was closed
|
|
|
|
if not self.connected then
|
|
|
|
println(log_header .. "connection to RTU closed by remote host")
|
|
|
|
log.info(log_header .. "session closed by remote host")
|
|
|
|
return self.connected
|
|
|
|
end
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
------------------
|
|
|
|
-- update units --
|
|
|
|
------------------
|
|
|
|
|
2022-05-16 15:54:34 +00:00
|
|
|
local time_now = util.time()
|
|
|
|
|
2022-05-15 00:27:06 +00:00
|
|
|
for i = 1, #self.units do
|
2022-05-16 15:54:34 +00:00
|
|
|
self.units[i].update(time_now)
|
2022-05-15 00:27:06 +00:00
|
|
|
end
|
|
|
|
|
2022-05-09 13:35:39 +00:00
|
|
|
----------------------
|
|
|
|
-- update periodics --
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
local elapsed = util.time() - self.periodics.last_update
|
|
|
|
|
|
|
|
local periodics = self.periodics
|
|
|
|
|
|
|
|
-- keep alive
|
|
|
|
|
|
|
|
periodics.keep_alive = periodics.keep_alive + elapsed
|
|
|
|
if periodics.keep_alive >= PERIODICS.KEEP_ALIVE then
|
2022-05-09 19:00:16 +00:00
|
|
|
_send_mgmt(SCADA_MGMT_TYPES.KEEP_ALIVE, { util.time() })
|
2022-05-09 13:35:39 +00:00
|
|
|
periodics.keep_alive = 0
|
|
|
|
end
|
|
|
|
|
|
|
|
self.periodics.last_update = util.time()
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.connected
|
|
|
|
end
|
|
|
|
|
2022-05-13 13:45:11 +00:00
|
|
|
return public
|
2022-05-09 13:35:39 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return rtu
|