2022-05-04 17:37:01 +00:00
|
|
|
--
|
|
|
|
-- Communications
|
|
|
|
--
|
|
|
|
|
2023-04-16 23:50:16 +00:00
|
|
|
local log = require("scada-common.log")
|
2022-05-09 13:34:26 +00:00
|
|
|
|
2022-05-07 17:39:12 +00:00
|
|
|
local insert = table.insert
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
---@type integer computer ID
|
2023-06-05 05:13:22 +00:00
|
|
|
---@diagnostic disable-next-line: undefined-field
|
2023-08-30 20:45:48 +00:00
|
|
|
local COMPUTER_ID = os.getComputerID()
|
2023-06-05 05:13:22 +00:00
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
---@type number|nil maximum acceptable transmission distance
|
|
|
|
local max_distance = nil
|
2023-06-05 05:13:22 +00:00
|
|
|
|
|
|
|
---@class comms
|
|
|
|
local comms = {}
|
2023-02-07 22:31:22 +00:00
|
|
|
|
2024-04-13 00:41:47 +00:00
|
|
|
-- protocol/data versions (protocol/data independent changes tracked by util.lua version)
|
2024-04-27 20:27:01 +00:00
|
|
|
comms.version = "2.5.1"
|
2024-04-13 00:41:47 +00:00
|
|
|
comms.api_version = "0.0.1"
|
2022-11-13 19:13:30 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
---@enum PROTOCOL
|
|
|
|
local PROTOCOL = {
|
2023-08-30 21:15:42 +00:00
|
|
|
MODBUS_TCP = 0, -- the "MODBUS TCP"-esque protocol
|
2023-07-29 22:16:59 +00:00
|
|
|
RPLC = 1, -- reactor PLC protocol
|
|
|
|
SCADA_MGMT = 2, -- SCADA supervisor management, device advertisements, etc
|
2023-08-30 20:45:48 +00:00
|
|
|
SCADA_CRDN = 3 -- data/control packets for coordinators to/from supervisory controllers
|
2022-01-14 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
---@enum RPLC_TYPE
|
|
|
|
local RPLC_TYPE = {
|
2023-07-29 22:16:59 +00:00
|
|
|
STATUS = 0, -- reactor/system status
|
|
|
|
MEK_STRUCT = 1, -- mekanism build structure
|
|
|
|
MEK_BURN_RATE = 2, -- set burn rate
|
|
|
|
RPS_ENABLE = 3, -- enable reactor
|
2023-09-19 20:37:15 +00:00
|
|
|
RPS_DISABLE = 4, -- disable the reactor
|
|
|
|
RPS_SCRAM = 5, -- SCRAM reactor (manual request)
|
|
|
|
RPS_ASCRAM = 6, -- SCRAM reactor (automatic request)
|
|
|
|
RPS_STATUS = 7, -- RPS status
|
|
|
|
RPS_ALARM = 8, -- RPS alarm broadcast
|
|
|
|
RPS_RESET = 9, -- clear RPS trip (if in bad state, will trip immediately)
|
|
|
|
RPS_AUTO_RESET = 10, -- clear RPS trip if it is just a timeout or auto scram
|
|
|
|
AUTO_BURN_RATE = 11 -- set an automatic burn rate, PLC will respond with status, enable toggle speed limited
|
2022-03-15 16:02:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
---@enum MGMT_TYPE
|
|
|
|
local MGMT_TYPE = {
|
2023-07-29 22:16:59 +00:00
|
|
|
ESTABLISH = 0, -- establish new connection
|
|
|
|
KEEP_ALIVE = 1, -- keep alive packet w/ RTT
|
|
|
|
CLOSE = 2, -- close a connection
|
|
|
|
RTU_ADVERT = 3, -- RTU capability advertisement
|
|
|
|
RTU_DEV_REMOUNT = 4, -- RTU multiblock possbily changed (formed, unformed) due to PPM remount
|
|
|
|
RTU_TONE_ALARM = 5, -- instruct RTUs to play specified alarm tones
|
|
|
|
DIAG_TONE_GET = 6, -- diagnostic: get alarm tones
|
|
|
|
DIAG_TONE_SET = 7, -- diagnostic: set alarm tones
|
|
|
|
DIAG_ALARM_SET = 8 -- diagnostic: set alarm to simulate audio for
|
2022-03-15 16:02:31 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
---@enum CRDN_TYPE
|
|
|
|
local CRDN_TYPE = {
|
2023-07-29 22:16:59 +00:00
|
|
|
INITIAL_BUILDS = 0, -- initial, complete builds packet to the coordinator
|
|
|
|
FAC_BUILDS = 1, -- facility RTU builds
|
|
|
|
FAC_STATUS = 2, -- state of facility and facility devices
|
|
|
|
FAC_CMD = 3, -- faility command
|
|
|
|
UNIT_BUILDS = 4, -- build of each reactor unit (reactor + RTUs)
|
|
|
|
UNIT_STATUSES = 5, -- state of each of the reactor units
|
2024-03-07 17:27:25 +00:00
|
|
|
UNIT_CMD = 6, -- command a reactor unit
|
|
|
|
API_GET_FAC = 7, -- API: get all the facility data
|
|
|
|
API_GET_UNITS = 8 -- API: get all the reactor unit data
|
2022-06-15 19:35:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
---@enum ESTABLISH_ACK
|
2023-01-26 23:26:26 +00:00
|
|
|
local ESTABLISH_ACK = {
|
2023-07-29 22:16:59 +00:00
|
|
|
ALLOW = 0, -- link approved
|
|
|
|
DENY = 1, -- link denied
|
|
|
|
COLLISION = 2, -- link denied due to existing active link
|
2024-04-13 00:41:47 +00:00
|
|
|
BAD_VERSION = 3, -- link denied due to comms version mismatch
|
|
|
|
BAD_API_VERSION = 4 -- link denied due to api version mismatch
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 21:15:42 +00:00
|
|
|
---@enum DEVICE_TYPE device types for establish messages
|
|
|
|
local DEVICE_TYPE = { PLC = 0, RTU = 1, SVR = 2, CRD = 3, PKT = 4 }
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
---@enum PLC_AUTO_ACK
|
2023-01-26 23:26:26 +00:00
|
|
|
local PLC_AUTO_ACK = {
|
2023-07-29 22:16:59 +00:00
|
|
|
FAIL = 0, -- failed to set burn rate/burn rate invalid
|
|
|
|
DIRECT_SET_OK = 1, -- successfully set burn rate
|
|
|
|
RAMP_SET_OK = 2, -- successfully started burn rate ramping
|
|
|
|
ZERO_DIS_OK = 3 -- successfully disabled reactor with < 0.01 burn rate
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
---@enum FAC_COMMAND
|
|
|
|
local FAC_COMMAND = {
|
2023-07-29 22:16:59 +00:00
|
|
|
SCRAM_ALL = 0, -- SCRAM all reactors
|
|
|
|
STOP = 1, -- stop automatic process control
|
|
|
|
START = 2, -- start automatic process control
|
|
|
|
ACK_ALL_ALARMS = 3, -- acknowledge all alarms on all units
|
|
|
|
SET_WASTE_MODE = 4, -- set automatic waste processing mode
|
|
|
|
SET_PU_FB = 5 -- set plutonium fallback mode
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
---@enum UNIT_COMMAND
|
|
|
|
local UNIT_COMMAND = {
|
2023-07-29 22:16:59 +00:00
|
|
|
SCRAM = 0, -- SCRAM the reactor
|
|
|
|
START = 1, -- start the reactor
|
|
|
|
RESET_RPS = 2, -- reset the RPS
|
|
|
|
SET_BURN = 3, -- set the burn rate
|
|
|
|
SET_WASTE = 4, -- set the waste processing mode
|
|
|
|
ACK_ALL_ALARMS = 5, -- ack all active alarms
|
|
|
|
ACK_ALARM = 6, -- ack a particular alarm
|
|
|
|
RESET_ALARM = 7, -- reset a particular alarm
|
|
|
|
SET_GROUP = 8 -- assign this unit to a group
|
2023-01-26 23:26:26 +00:00
|
|
|
}
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
comms.PROTOCOL = PROTOCOL
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
comms.RPLC_TYPE = RPLC_TYPE
|
2023-08-30 20:45:48 +00:00
|
|
|
comms.MGMT_TYPE = MGMT_TYPE
|
|
|
|
comms.CRDN_TYPE = CRDN_TYPE
|
2023-01-26 23:26:26 +00:00
|
|
|
|
|
|
|
comms.ESTABLISH_ACK = ESTABLISH_ACK
|
2023-02-21 16:05:57 +00:00
|
|
|
comms.DEVICE_TYPE = DEVICE_TYPE
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
comms.PLC_AUTO_ACK = PLC_AUTO_ACK
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
comms.UNIT_COMMAND = UNIT_COMMAND
|
|
|
|
comms.FAC_COMMAND = FAC_COMMAND
|
2023-01-26 23:26:26 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- destination broadcast address (to all devices)
|
|
|
|
comms.BROADCAST = -1
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
---@alias packet scada_packet|modbus_packet|rplc_packet|mgmt_packet|crdn_packet
|
|
|
|
---@alias frame modbus_frame|rplc_frame|mgmt_frame|crdn_frame
|
2022-09-21 19:53:51 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
-- configure the maximum allowable message receive distance<br>
|
2023-02-07 22:31:22 +00:00
|
|
|
-- packets received with distances greater than this will be silently discarded
|
2023-06-07 18:17:10 +00:00
|
|
|
---@param distance integer max modem message distance (0 disables the limit)
|
2023-02-07 22:31:22 +00:00
|
|
|
function comms.set_trusted_range(distance)
|
2023-06-07 18:17:10 +00:00
|
|
|
if distance == 0 then max_distance = nil else max_distance = distance end
|
2023-02-07 22:31:22 +00:00
|
|
|
end
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
-- generic SCADA packet
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function comms.scada_packet()
|
2022-01-14 21:32:20 +00:00
|
|
|
local self = {
|
2023-06-05 05:13:22 +00:00
|
|
|
modem_msg_in = nil, ---@type modem_message|nil
|
2022-01-22 19:26:25 +00:00
|
|
|
valid = false,
|
2023-07-29 22:16:59 +00:00
|
|
|
authenticated = false,
|
2023-06-05 05:13:22 +00:00
|
|
|
raw = {},
|
|
|
|
src_addr = comms.BROADCAST,
|
|
|
|
dest_addr = comms.BROADCAST,
|
2023-02-04 18:47:00 +00:00
|
|
|
seq_num = -1,
|
2023-03-04 06:37:15 +00:00
|
|
|
protocol = PROTOCOL.SCADA_MGMT,
|
2023-02-04 18:47:00 +00:00
|
|
|
length = 0,
|
|
|
|
payload = {}
|
2022-01-14 21:32:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class scada_packet
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- make a SCADA packet
|
2023-06-05 05:13:22 +00:00
|
|
|
---@param dest_addr integer destination computer address (ID)
|
|
|
|
---@param seq_num integer sequence number
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param protocol PROTOCOL
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param payload table
|
2023-06-05 05:13:22 +00:00
|
|
|
function public.make(dest_addr, seq_num, protocol, payload)
|
2022-01-22 19:26:25 +00:00
|
|
|
self.valid = true
|
2023-06-11 18:26:55 +00:00
|
|
|
self.src_addr = COMPUTER_ID
|
2023-06-05 05:13:22 +00:00
|
|
|
self.dest_addr = dest_addr
|
2022-03-14 18:19:14 +00:00
|
|
|
self.seq_num = seq_num
|
2022-01-22 19:26:25 +00:00
|
|
|
self.protocol = protocol
|
|
|
|
self.length = #payload
|
2022-04-22 19:43:25 +00:00
|
|
|
self.payload = payload
|
2023-06-27 22:36:16 +00:00
|
|
|
self.raw = { self.src_addr, self.dest_addr, self.seq_num, self.protocol, self.payload }
|
2022-01-22 19:26:25 +00:00
|
|
|
end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- parse in a modem message as a SCADA packet
|
2023-02-21 15:31:05 +00:00
|
|
|
---@param side string modem side
|
2023-06-05 05:13:22 +00:00
|
|
|
---@param sender integer sender channel
|
|
|
|
---@param reply_to integer reply channel
|
2023-02-21 15:31:05 +00:00
|
|
|
---@param message any message body
|
|
|
|
---@param distance integer transmission distance
|
|
|
|
---@return boolean valid valid message received
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.receive(side, sender, reply_to, message, distance)
|
2023-06-05 05:13:22 +00:00
|
|
|
---@class modem_message
|
2022-01-14 21:32:20 +00:00
|
|
|
self.modem_msg_in = {
|
|
|
|
iface = side,
|
2023-06-05 05:13:22 +00:00
|
|
|
s_channel = sender,
|
|
|
|
r_channel = reply_to,
|
2022-01-14 21:32:20 +00:00
|
|
|
msg = message,
|
|
|
|
dist = distance
|
|
|
|
}
|
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
self.valid = false
|
2022-01-14 21:32:20 +00:00
|
|
|
self.raw = self.modem_msg_in.msg
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
if (type(max_distance) == "number") and (type(distance) == "number") and (distance > max_distance) then
|
2023-02-07 22:31:22 +00:00
|
|
|
-- outside of maximum allowable transmission distance
|
2023-08-30 20:45:48 +00:00
|
|
|
-- log.debug("comms.scada_packet.receive(): discarding packet with distance " .. distance .. " (outside trusted range)")
|
2023-02-07 22:31:22 +00:00
|
|
|
else
|
2023-02-07 23:44:34 +00:00
|
|
|
if type(self.raw) == "table" then
|
2023-06-27 22:36:16 +00:00
|
|
|
if #self.raw == 5 then
|
2023-06-05 05:13:22 +00:00
|
|
|
self.src_addr = self.raw[1]
|
|
|
|
self.dest_addr = self.raw[2]
|
|
|
|
self.seq_num = self.raw[3]
|
|
|
|
self.protocol = self.raw[4]
|
|
|
|
|
2023-06-27 22:36:16 +00:00
|
|
|
-- element 5 must be a table
|
|
|
|
if type(self.raw[5]) == "table" then
|
|
|
|
self.length = #self.raw[5]
|
|
|
|
self.payload = self.raw[5]
|
2023-02-07 23:44:34 +00:00
|
|
|
end
|
2023-06-05 05:13:22 +00:00
|
|
|
else
|
|
|
|
self.src_addr = nil
|
|
|
|
self.dest_addr = nil
|
|
|
|
self.seq_num = nil
|
|
|
|
self.protocol = nil
|
|
|
|
self.length = 0
|
|
|
|
self.payload = {}
|
2022-09-23 01:31:07 +00:00
|
|
|
end
|
2022-06-05 17:21:02 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
-- check if this packet is destined for this device
|
2023-06-11 18:26:55 +00:00
|
|
|
local is_destination = (self.dest_addr == comms.BROADCAST) or (self.dest_addr == COMPUTER_ID)
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
self.valid = is_destination and type(self.src_addr) == "number" and type(self.dest_addr) == "number" and
|
|
|
|
type(self.seq_num) == "number" and type(self.protocol) == "number" and type(self.payload) == "table"
|
2023-02-07 22:31:22 +00:00
|
|
|
end
|
2022-01-14 21:32:20 +00:00
|
|
|
end
|
2022-04-22 19:43:25 +00:00
|
|
|
|
|
|
|
return self.valid
|
2022-01-14 21:32:20 +00:00
|
|
|
end
|
|
|
|
|
2023-07-29 22:16:59 +00:00
|
|
|
-- report that this packet has been authenticated (was received with a valid HMAC)
|
|
|
|
function public.stamp_authenticated() self.authenticated = true end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- public accessors --
|
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.modem_event() return self.modem_msg_in end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.raw_sendable() return self.raw end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2023-06-05 05:13:22 +00:00
|
|
|
function public.local_channel() return self.modem_msg_in.s_channel end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2023-06-05 05:13:22 +00:00
|
|
|
function public.remote_channel() return self.modem_msg_in.r_channel end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.is_valid() return self.valid end
|
2023-07-29 22:16:59 +00:00
|
|
|
---@nodiscard
|
|
|
|
function public.is_authenticated() return self.authenticated end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2023-06-05 05:13:22 +00:00
|
|
|
---@nodiscard
|
|
|
|
function public.src_addr() return self.src_addr end
|
|
|
|
---@nodiscard
|
|
|
|
function public.dest_addr() return self.dest_addr end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.seq_num() return self.seq_num end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.protocol() return self.protocol end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.length() return self.length end
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.data() return self.payload end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
|
|
|
return public
|
2022-01-14 21:32:20 +00:00
|
|
|
end
|
2022-04-02 12:28:43 +00:00
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
-- authenticated SCADA packet
|
2023-06-27 22:36:16 +00:00
|
|
|
---@nodiscard
|
|
|
|
function comms.authd_packet()
|
|
|
|
local self = {
|
|
|
|
modem_msg_in = nil, ---@type modem_message|nil
|
|
|
|
valid = false,
|
|
|
|
raw = {},
|
|
|
|
src_addr = comms.BROADCAST,
|
|
|
|
dest_addr = comms.BROADCAST,
|
|
|
|
mac = "",
|
|
|
|
payload = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
---@class authd_packet
|
|
|
|
local public = {}
|
|
|
|
|
|
|
|
-- make an authenticated SCADA packet
|
|
|
|
---@param s_packet scada_packet scada packet to authenticate
|
|
|
|
---@param mac function message authentication function
|
|
|
|
function public.make(s_packet, mac)
|
|
|
|
self.valid = true
|
|
|
|
self.src_addr = s_packet.src_addr()
|
|
|
|
self.dest_addr = s_packet.dest_addr()
|
|
|
|
self.payload = textutils.serialize(s_packet.raw_sendable(), { allow_repetitions = true, compact = true })
|
|
|
|
self.mac = mac(self.payload)
|
|
|
|
self.raw = { self.src_addr, self.dest_addr, self.mac, self.payload }
|
|
|
|
end
|
|
|
|
|
|
|
|
-- parse in a modem message as an authenticated SCADA packet
|
|
|
|
---@param side string modem side
|
|
|
|
---@param sender integer sender channel
|
|
|
|
---@param reply_to integer reply channel
|
|
|
|
---@param message any message body
|
|
|
|
---@param distance integer transmission distance
|
|
|
|
---@return boolean valid valid message received
|
|
|
|
function public.receive(side, sender, reply_to, message, distance)
|
|
|
|
---@class modem_message
|
|
|
|
self.modem_msg_in = {
|
|
|
|
iface = side,
|
|
|
|
s_channel = sender,
|
|
|
|
r_channel = reply_to,
|
|
|
|
msg = message,
|
|
|
|
dist = distance
|
|
|
|
}
|
|
|
|
|
|
|
|
self.valid = false
|
|
|
|
self.raw = self.modem_msg_in.msg
|
|
|
|
|
2023-10-14 16:17:25 +00:00
|
|
|
if (type(max_distance) == "number") and ((type(distance) ~= "number") or (distance > max_distance)) then
|
2023-06-27 22:36:16 +00:00
|
|
|
-- outside of maximum allowable transmission distance
|
2023-08-30 20:45:48 +00:00
|
|
|
-- log.debug("comms.authd_packet.receive(): discarding packet with distance " .. distance .. " (outside trusted range)")
|
2023-06-27 22:36:16 +00:00
|
|
|
else
|
|
|
|
if type(self.raw) == "table" then
|
|
|
|
if #self.raw == 4 then
|
|
|
|
self.src_addr = self.raw[1]
|
|
|
|
self.dest_addr = self.raw[2]
|
|
|
|
self.mac = self.raw[3]
|
|
|
|
self.payload = self.raw[4]
|
|
|
|
else
|
|
|
|
self.src_addr = nil
|
|
|
|
self.dest_addr = nil
|
|
|
|
self.mac = ""
|
|
|
|
self.payload = ""
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check if this packet is destined for this device
|
|
|
|
local is_destination = (self.dest_addr == comms.BROADCAST) or (self.dest_addr == COMPUTER_ID)
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
self.valid = is_destination and type(self.src_addr) == "number" and type(self.dest_addr) == "number" and
|
|
|
|
type(self.mac) == "string" and type(self.payload) == "string"
|
2023-06-27 22:36:16 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self.valid
|
|
|
|
end
|
|
|
|
|
|
|
|
-- public accessors --
|
|
|
|
|
|
|
|
---@nodiscard
|
|
|
|
function public.modem_event() return self.modem_msg_in end
|
|
|
|
---@nodiscard
|
|
|
|
function public.raw_sendable() return self.raw end
|
|
|
|
|
|
|
|
---@nodiscard
|
|
|
|
function public.local_channel() return self.modem_msg_in.s_channel end
|
|
|
|
---@nodiscard
|
|
|
|
function public.remote_channel() return self.modem_msg_in.r_channel end
|
|
|
|
|
|
|
|
---@nodiscard
|
|
|
|
function public.is_valid() return self.valid end
|
|
|
|
|
|
|
|
---@nodiscard
|
|
|
|
function public.src_addr() return self.src_addr end
|
|
|
|
---@nodiscard
|
|
|
|
function public.dest_addr() return self.dest_addr end
|
|
|
|
---@nodiscard
|
|
|
|
function public.mac() return self.mac end
|
|
|
|
---@nodiscard
|
|
|
|
function public.data() return self.payload end
|
|
|
|
|
|
|
|
return public
|
|
|
|
end
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
-- MODBUS packet, modeled after MODBUS TCP
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function comms.modbus_packet()
|
2022-04-21 14:26:02 +00:00
|
|
|
local self = {
|
|
|
|
frame = nil,
|
2023-02-07 05:32:50 +00:00
|
|
|
raw = {},
|
|
|
|
txn_id = -1,
|
|
|
|
length = 0,
|
|
|
|
unit_id = -1,
|
2023-02-22 04:50:43 +00:00
|
|
|
func_code = 0x80,
|
2023-02-07 05:32:50 +00:00
|
|
|
data = {}
|
2022-04-21 14:26:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class modbus_packet
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-21 14:26:02 +00:00
|
|
|
-- make a MODBUS packet
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param txn_id integer
|
|
|
|
---@param unit_id integer
|
|
|
|
---@param func_code MODBUS_FCODE
|
|
|
|
---@param data table
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.make(txn_id, unit_id, func_code, data)
|
2022-06-05 17:21:02 +00:00
|
|
|
if type(data) == "table" then
|
|
|
|
self.txn_id = txn_id
|
|
|
|
self.length = #data
|
|
|
|
self.unit_id = unit_id
|
|
|
|
self.func_code = func_code
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
-- populate raw array
|
|
|
|
self.raw = { self.txn_id, self.unit_id, self.func_code }
|
2023-09-19 20:37:15 +00:00
|
|
|
for i = 1, self.length do insert(self.raw, data[i]) end
|
2022-06-05 17:21:02 +00:00
|
|
|
else
|
|
|
|
log.error("comms.modbus_packet.make(): data not table")
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- decode a MODBUS packet from a SCADA frame
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param frame scada_packet
|
|
|
|
---@return boolean success
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.decode(frame)
|
2022-04-21 14:26:02 +00:00
|
|
|
if frame then
|
|
|
|
self.frame = frame
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if frame.protocol() == PROTOCOL.MODBUS_TCP then
|
2022-04-23 00:21:28 +00:00
|
|
|
local size_ok = frame.length() >= 3
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
if size_ok then
|
|
|
|
local data = frame.data()
|
2022-05-10 21:06:27 +00:00
|
|
|
public.make(data[1], data[2], data[3], { table.unpack(data, 4, #data) })
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
local valid = type(self.txn_id) == "number" and type(self.unit_id) == "number" and type(self.func_code) == "number"
|
2022-06-05 17:21:02 +00:00
|
|
|
|
|
|
|
return size_ok and valid
|
2022-04-22 19:43:25 +00:00
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("attempted MODBUS_TCP parse of incorrect protocol " .. frame.protocol(), true)
|
2022-04-22 19:43:25 +00:00
|
|
|
return false
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("nil frame encountered", true)
|
2022-04-21 14:26:02 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- get raw to send
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.raw_sendable() return self.raw end
|
2022-04-22 19:43:25 +00:00
|
|
|
|
2022-05-26 23:37:19 +00:00
|
|
|
-- get this packet as a frame with an immutable relation to this object
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.get()
|
2022-05-11 01:51:04 +00:00
|
|
|
---@class modbus_frame
|
|
|
|
local frame = {
|
2022-04-21 14:26:02 +00:00
|
|
|
scada_frame = self.frame,
|
|
|
|
txn_id = self.txn_id,
|
|
|
|
length = self.length,
|
|
|
|
unit_id = self.unit_id,
|
|
|
|
func_code = self.func_code,
|
|
|
|
data = self.data
|
|
|
|
}
|
2022-05-11 01:51:04 +00:00
|
|
|
|
|
|
|
return frame
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
return public
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- reactor PLC packet
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function comms.rplc_packet()
|
2022-04-21 14:26:02 +00:00
|
|
|
local self = {
|
|
|
|
frame = nil,
|
2023-02-07 05:32:50 +00:00
|
|
|
raw = {},
|
|
|
|
id = 0,
|
2023-02-21 21:57:33 +00:00
|
|
|
type = 0, ---@type RPLC_TYPE
|
2023-02-07 05:32:50 +00:00
|
|
|
length = 0,
|
|
|
|
data = {}
|
2022-04-21 14:26:02 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class rplc_packet
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-21 14:26:02 +00:00
|
|
|
-- make an RPLC packet
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param id integer
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param packet_type RPLC_TYPE
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param data table
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.make(id, packet_type, data)
|
2022-06-05 17:21:02 +00:00
|
|
|
if type(data) == "table" then
|
|
|
|
-- packet accessor properties
|
|
|
|
self.id = id
|
|
|
|
self.type = packet_type
|
|
|
|
self.length = #data
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
-- populate raw array
|
|
|
|
self.raw = { self.id, self.type }
|
2023-09-19 20:37:15 +00:00
|
|
|
for i = 1, #data do insert(self.raw, data[i]) end
|
2022-06-05 17:21:02 +00:00
|
|
|
else
|
|
|
|
log.error("comms.rplc_packet.make(): data not table")
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- decode an RPLC packet from a SCADA frame
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param frame scada_packet
|
|
|
|
---@return boolean success
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.decode(frame)
|
2022-04-21 14:26:02 +00:00
|
|
|
if frame then
|
|
|
|
self.frame = frame
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if frame.protocol() == PROTOCOL.RPLC then
|
2022-04-22 19:43:25 +00:00
|
|
|
local ok = frame.length() >= 2
|
2022-04-21 14:26:02 +00:00
|
|
|
|
|
|
|
if ok then
|
2022-04-22 19:43:25 +00:00
|
|
|
local data = frame.data()
|
2022-05-10 21:06:27 +00:00
|
|
|
public.make(data[1], data[2], { table.unpack(data, 3, #data) })
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
2022-06-05 17:21:02 +00:00
|
|
|
ok = ok and type(self.id) == "number"
|
|
|
|
|
2022-04-21 14:26:02 +00:00
|
|
|
return ok
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("attempted RPLC parse of incorrect protocol " .. frame.protocol(), true)
|
2022-04-21 14:26:02 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("nil frame encountered", true)
|
2022-04-21 14:26:02 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- get raw to send
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.raw_sendable() return self.raw end
|
2022-04-22 19:43:25 +00:00
|
|
|
|
2022-05-26 23:37:19 +00:00
|
|
|
-- get this packet as a frame with an immutable relation to this object
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.get()
|
2022-05-11 01:51:04 +00:00
|
|
|
---@class rplc_frame
|
|
|
|
local frame = {
|
2022-04-21 14:26:02 +00:00
|
|
|
scada_frame = self.frame,
|
|
|
|
id = self.id,
|
|
|
|
type = self.type,
|
|
|
|
length = self.length,
|
|
|
|
data = self.data
|
|
|
|
}
|
2022-05-11 01:51:04 +00:00
|
|
|
|
|
|
|
return frame
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
return public
|
2022-04-21 14:26:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- SCADA management packet
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function comms.mgmt_packet()
|
2022-04-02 12:28:43 +00:00
|
|
|
local self = {
|
|
|
|
frame = nil,
|
2023-02-07 05:32:50 +00:00
|
|
|
raw = {},
|
2023-08-30 20:45:48 +00:00
|
|
|
type = 0, ---@type MGMT_TYPE
|
2023-02-07 05:32:50 +00:00
|
|
|
length = 0,
|
|
|
|
data = {}
|
2022-04-02 12:28:43 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
---@class mgmt_packet
|
|
|
|
local public = {}
|
|
|
|
|
2022-04-02 12:28:43 +00:00
|
|
|
-- make a SCADA management packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param packet_type MGMT_TYPE
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param data table
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.make(packet_type, data)
|
2022-06-05 17:21:02 +00:00
|
|
|
if type(data) == "table" then
|
|
|
|
-- packet accessor properties
|
|
|
|
self.type = packet_type
|
|
|
|
self.length = #data
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
-- populate raw array
|
|
|
|
self.raw = { self.type }
|
2023-09-19 20:37:15 +00:00
|
|
|
for i = 1, #data do insert(self.raw, data[i]) end
|
2022-06-05 17:21:02 +00:00
|
|
|
else
|
|
|
|
log.error("comms.mgmt_packet.make(): data not table")
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- decode a SCADA management packet from a SCADA frame
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param frame scada_packet
|
|
|
|
---@return boolean success
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.decode(frame)
|
2022-04-02 12:28:43 +00:00
|
|
|
if frame then
|
|
|
|
self.frame = frame
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if frame.protocol() == PROTOCOL.SCADA_MGMT then
|
2022-05-02 21:34:24 +00:00
|
|
|
local ok = frame.length() >= 1
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2022-04-02 12:28:43 +00:00
|
|
|
if ok then
|
2022-04-22 19:43:25 +00:00
|
|
|
local data = frame.data()
|
2022-05-10 21:06:27 +00:00
|
|
|
public.make(data[1], { table.unpack(data, 2, #data) })
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
2022-05-10 21:06:27 +00:00
|
|
|
|
2022-04-02 12:28:43 +00:00
|
|
|
return ok
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("attempted SCADA_MGMT parse of incorrect protocol " .. frame.protocol(), true)
|
2022-06-05 17:21:02 +00:00
|
|
|
return false
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("nil frame encountered", true)
|
2022-04-02 12:28:43 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- get raw to send
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.raw_sendable() return self.raw end
|
2022-04-22 19:43:25 +00:00
|
|
|
|
2022-05-26 23:37:19 +00:00
|
|
|
-- get this packet as a frame with an immutable relation to this object
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.get()
|
2022-05-11 01:51:04 +00:00
|
|
|
---@class mgmt_frame
|
|
|
|
local frame = {
|
2022-04-02 12:28:43 +00:00
|
|
|
scada_frame = self.frame,
|
|
|
|
type = self.type,
|
|
|
|
length = self.length,
|
|
|
|
data = self.data
|
|
|
|
}
|
2022-05-11 01:51:04 +00:00
|
|
|
|
|
|
|
return frame
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
return public
|
2022-04-02 12:28:43 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
|
|
|
|
-- SCADA coordinator packet
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-07-07 04:34:42 +00:00
|
|
|
function comms.crdn_packet()
|
2022-04-22 15:07:59 +00:00
|
|
|
local self = {
|
|
|
|
frame = nil,
|
2023-02-07 05:32:50 +00:00
|
|
|
raw = {},
|
2023-08-30 20:45:48 +00:00
|
|
|
type = 0, ---@type CRDN_TYPE
|
2023-02-07 05:32:50 +00:00
|
|
|
length = 0,
|
|
|
|
data = {}
|
2022-04-22 15:07:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 04:34:42 +00:00
|
|
|
---@class crdn_packet
|
2022-05-10 21:06:27 +00:00
|
|
|
local public = {}
|
|
|
|
|
2022-04-22 15:07:59 +00:00
|
|
|
-- make a coordinator packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param packet_type CRDN_TYPE
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param data table
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.make(packet_type, data)
|
2022-06-05 17:21:02 +00:00
|
|
|
if type(data) == "table" then
|
|
|
|
-- packet accessor properties
|
|
|
|
self.type = packet_type
|
|
|
|
self.length = #data
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
-- populate raw array
|
|
|
|
self.raw = { self.type }
|
2023-09-19 20:37:15 +00:00
|
|
|
for i = 1, #data do insert(self.raw, data[i]) end
|
2022-06-05 17:21:02 +00:00
|
|
|
else
|
2022-07-07 04:34:42 +00:00
|
|
|
log.error("comms.crdn_packet.make(): data not table")
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- decode a coordinator packet from a SCADA frame
|
2022-05-10 21:06:27 +00:00
|
|
|
---@param frame scada_packet
|
|
|
|
---@return boolean success
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.decode(frame)
|
2022-04-22 15:07:59 +00:00
|
|
|
if frame then
|
|
|
|
self.frame = frame
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if frame.protocol() == PROTOCOL.SCADA_CRDN then
|
2022-05-02 21:34:24 +00:00
|
|
|
local ok = frame.length() >= 1
|
2022-04-22 15:07:59 +00:00
|
|
|
|
|
|
|
if ok then
|
2022-04-22 19:43:25 +00:00
|
|
|
local data = frame.data()
|
2022-05-10 21:06:27 +00:00
|
|
|
public.make(data[1], { table.unpack(data, 2, #data) })
|
2022-04-22 15:07:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ok
|
|
|
|
else
|
2022-07-07 04:34:42 +00:00
|
|
|
log.debug("attempted SCADA_CRDN parse of incorrect protocol " .. frame.protocol(), true)
|
2022-04-22 15:07:59 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.debug("nil frame encountered", true)
|
2022-04-22 15:07:59 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-04-22 19:43:25 +00:00
|
|
|
-- get raw to send
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.raw_sendable() return self.raw end
|
2022-04-22 19:43:25 +00:00
|
|
|
|
2022-05-26 23:37:19 +00:00
|
|
|
-- get this packet as a frame with an immutable relation to this object
|
2023-02-21 15:31:05 +00:00
|
|
|
---@nodiscard
|
2022-05-31 20:09:06 +00:00
|
|
|
function public.get()
|
2022-07-07 04:34:42 +00:00
|
|
|
---@class crdn_frame
|
2022-05-11 01:51:04 +00:00
|
|
|
local frame = {
|
2022-04-22 19:43:25 +00:00
|
|
|
scada_frame = self.frame,
|
|
|
|
type = self.type,
|
|
|
|
length = self.length,
|
|
|
|
data = self.data
|
|
|
|
}
|
2022-05-11 01:51:04 +00:00
|
|
|
|
|
|
|
return frame
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
|
|
|
|
2022-05-10 21:06:27 +00:00
|
|
|
return public
|
2022-04-22 19:43:25 +00:00
|
|
|
end
|
|
|
|
|
2022-05-04 17:37:01 +00:00
|
|
|
return comms
|