cc-mek-scada/scada-common/comms.lua

548 lines
14 KiB
Lua
Raw Normal View History

--
-- Communications
--
local comms = {}
2022-05-07 17:39:12 +00:00
local insert = table.insert
local PROTOCOLS = {
2022-03-15 16:02:31 +00:00
MODBUS_TCP = 0, -- our "MODBUS TCP"-esque protocol
RPLC = 1, -- reactor PLC protocol
SCADA_MGMT = 2, -- SCADA supervisor management, device advertisements, etc
2022-04-22 19:43:25 +00:00
COORD_DATA = 3, -- data/control packets for coordinators to/from supervisory controllers
COORD_API = 4 -- data/control packets for pocket computers to/from coordinators
}
local RPLC_TYPES = {
2022-01-22 19:26:25 +00:00
KEEP_ALIVE = 0, -- keep alive packets
LINK_REQ = 1, -- linking requests
STATUS = 2, -- reactor/system status
MEK_STRUCT = 3, -- mekanism build structure
MEK_BURN_RATE = 4, -- set burn rate
RPS_ENABLE = 5, -- enable reactor
RPS_SCRAM = 6, -- SCRAM reactor
2022-05-05 15:55:04 +00:00
RPS_STATUS = 7, -- RPS status
RPS_ALARM = 8, -- RPS alarm broadcast
RPS_RESET = 9 -- clear RPS trip (if in bad state, will trip immediately)
2022-01-22 19:26:25 +00:00
}
local RPLC_LINKING = {
2022-03-15 16:02:31 +00:00
ALLOW = 0, -- link approved
DENY = 1, -- link denied
COLLISION = 2 -- link denied due to existing active link
}
local SCADA_MGMT_TYPES = {
2022-03-15 16:02:31 +00:00
PING = 0, -- generic ping
2022-05-02 15:42:24 +00:00
CLOSE = 1, -- close a connection
REMOTE_LINKED = 2, -- remote device linked
RTU_ADVERT = 3, -- RTU capability advertisement
2022-05-02 15:42:24 +00:00
RTU_HEARTBEAT = 4 -- RTU heartbeat
2022-03-15 16:02:31 +00:00
}
local RTU_ADVERT_TYPES = {
2022-03-15 16:02:31 +00:00
BOILER = 0, -- boiler
TURBINE = 1, -- turbine
2022-03-23 19:41:08 +00:00
IMATRIX = 2, -- induction matrix
REDSTONE = 3 -- redstone I/O
2022-01-22 19:26:25 +00:00
}
comms.PROTOCOLS = PROTOCOLS
comms.RPLC_TYPES = RPLC_TYPES
comms.RPLC_LINKING = RPLC_LINKING
comms.SCADA_MGMT_TYPES = SCADA_MGMT_TYPES
comms.RTU_ADVERT_TYPES = RTU_ADVERT_TYPES
-- generic SCADA packet object
comms.scada_packet = function ()
local self = {
modem_msg_in = nil,
2022-01-22 19:26:25 +00:00
valid = false,
2022-04-22 19:43:25 +00:00
raw = nil,
2022-03-14 18:19:14 +00:00
seq_num = nil,
protocol = nil,
2022-01-22 19:26:25 +00:00
length = nil,
2022-04-22 19:43:25 +00:00
payload = nil
}
2022-04-22 19:43:25 +00:00
-- make a SCADA packet
2022-03-14 18:19:14 +00:00
local make = function (seq_num, protocol, payload)
2022-01-22 19:26:25 +00:00
self.valid = true
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
self.raw = { 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
local receive = function (side, sender, reply_to, message, distance)
self.modem_msg_in = {
iface = side,
s_port = sender,
r_port = reply_to,
msg = message,
dist = distance
}
self.raw = self.modem_msg_in.msg
if type(self.raw) == "table" then
if #self.raw >= 3 then
self.valid = true
self.seq_num = self.raw[1]
self.protocol = self.raw[2]
self.length = #self.raw[3]
self.payload = self.raw[3]
end
end
2022-04-22 19:43:25 +00:00
return self.valid
end
2022-04-22 19:43:25 +00:00
-- public accessors --
2022-03-15 16:02:31 +00:00
local modem_event = function () return self.modem_msg_in end
2022-04-23 00:21:28 +00:00
local raw_sendable = function () return self.raw end
2022-03-15 16:02:31 +00:00
local local_port = function () return self.modem_msg_in.s_port end
local remote_port = function () return self.modem_msg_in.r_port end
2022-03-15 16:02:31 +00:00
local is_valid = function () return self.valid end
local seq_num = function () return self.seq_num end
2022-03-15 16:02:31 +00:00
local protocol = function () return self.protocol end
local length = function () return self.length end
2022-04-22 19:43:25 +00:00
local data = function () return self.payload end
2022-01-22 19:26:25 +00:00
return {
2022-04-22 19:43:25 +00:00
-- construct
2022-01-22 19:26:25 +00:00
make = make,
receive = receive,
2022-04-22 19:43:25 +00:00
-- raw access
2022-03-14 18:19:14 +00:00
modem_event = modem_event,
2022-04-23 00:21:28 +00:00
raw_sendable = raw_sendable,
-- ports
local_port = local_port,
remote_port = remote_port,
2022-04-22 19:43:25 +00:00
-- well-formed
2022-03-15 16:02:31 +00:00
is_valid = is_valid,
2022-04-22 19:43:25 +00:00
-- packet properties
2022-03-14 18:19:14 +00:00
seq_num = seq_num,
protocol = protocol,
length = length,
2022-03-14 18:19:14 +00:00
data = data
}
end
2022-04-22 19:43:25 +00:00
-- MODBUS packet
2022-04-23 00:21:28 +00:00
-- modeled after MODBUS TCP packet
comms.modbus_packet = function ()
local self = {
frame = nil,
2022-04-22 19:43:25 +00:00
raw = nil,
txn_id = txn_id,
length = length,
unit_id = unit_id,
func_code = func_code,
data = data
}
-- make a MODBUS packet
2022-04-23 00:21:28 +00:00
local make = function (txn_id, unit_id, func_code, data)
self.txn_id = txn_id
2022-04-22 19:43:25 +00:00
self.length = #data
self.unit_id = unit_id
self.func_code = func_code
self.data = data
2022-04-22 19:43:25 +00:00
-- populate raw array
2022-04-23 00:21:28 +00:00
self.raw = { self.txn_id, self.unit_id, self.func_code }
2022-04-22 19:43:25 +00:00
for i = 1, self.length do
2022-05-07 17:39:12 +00:00
insert(self.raw, data[i])
2022-04-22 19:43:25 +00:00
end
end
-- decode a MODBUS packet from a SCADA frame
local decode = function (frame)
if frame then
self.frame = frame
2022-04-22 19:43:25 +00:00
if frame.protocol() == PROTOCOLS.MODBUS_TCP then
2022-04-23 00:21:28 +00:00
local size_ok = frame.length() >= 3
2022-04-22 19:43:25 +00:00
if size_ok then
local data = frame.data()
2022-04-23 00:21:28 +00:00
make(data[1], data[2], data[3], { table.unpack(data, 4, #data) })
2022-04-22 19:43:25 +00:00
end
return size_ok
else
log.debug("attempted MODBUS_TCP parse of incorrect protocol " .. frame.protocol(), true)
2022-04-22 19:43:25 +00:00
return false
end
else
log.debug("nil frame encountered", true)
return false
end
end
2022-04-22 19:43:25 +00:00
-- get raw to send
local raw_sendable = function () return self.raw end
-- get this packet
local get = function ()
return {
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
}
end
return {
2022-04-22 19:43:25 +00:00
-- construct
make = make,
decode = decode,
2022-04-22 19:43:25 +00:00
-- raw access
raw_sendable = raw_sendable,
-- formatted access
get = get
}
end
-- reactor PLC packet
comms.rplc_packet = function ()
local self = {
frame = nil,
2022-04-22 19:43:25 +00:00
raw = nil,
id = nil,
type = nil,
length = nil,
body = nil
}
2022-04-22 19:43:25 +00:00
-- check that type is known
local _rplc_type_valid = function ()
return self.type == RPLC_TYPES.KEEP_ALIVE or
self.type == RPLC_TYPES.LINK_REQ or
self.type == RPLC_TYPES.STATUS or
self.type == RPLC_TYPES.MEK_STRUCT or
self.type == RPLC_TYPES.MEK_BURN_RATE or
self.type == RPLC_TYPES.RPS_ENABLE or
self.type == RPLC_TYPES.RPS_SCRAM or
2022-05-05 15:55:04 +00:00
self.type == RPLC_TYPES.RPS_ALARM or
self.type == RPLC_TYPES.RPS_STATUS or
self.type == RPLC_TYPES.RPS_RESET
end
-- make an RPLC packet
2022-04-22 19:43:25 +00:00
local make = function (id, packet_type, data)
-- packet accessor properties
self.id = id
self.type = packet_type
2022-04-22 19:43:25 +00:00
self.length = #data
self.data = data
2022-04-22 19:43:25 +00:00
-- populate raw array
self.raw = { self.id, self.type }
for i = 1, #data do
2022-05-07 17:39:12 +00:00
insert(self.raw, data[i])
2022-04-22 19:43:25 +00:00
end
end
-- decode an RPLC packet from a SCADA frame
local decode = function (frame)
if frame then
self.frame = frame
2022-04-22 19:43:25 +00:00
if frame.protocol() == PROTOCOLS.RPLC then
local ok = frame.length() >= 2
if ok then
2022-04-22 19:43:25 +00:00
local data = frame.data()
make(data[1], data[2], { table.unpack(data, 3, #data) })
ok = _rplc_type_valid()
end
return ok
else
log.debug("attempted RPLC parse of incorrect protocol " .. frame.protocol(), true)
return false
end
else
log.debug("nil frame encountered", true)
return false
end
end
2022-04-22 19:43:25 +00:00
-- get raw to send
local raw_sendable = function () return self.raw end
-- get this packet
local get = function ()
return {
scada_frame = self.frame,
id = self.id,
type = self.type,
length = self.length,
data = self.data
}
end
return {
2022-04-22 19:43:25 +00:00
-- construct
make = make,
decode = decode,
2022-04-22 19:43:25 +00:00
-- raw access
raw_sendable = raw_sendable,
-- formatted access
get = get
}
end
-- SCADA management packet
comms.mgmt_packet = function ()
local self = {
frame = nil,
2022-04-22 19:43:25 +00:00
raw = nil,
type = nil,
length = nil,
data = nil
}
2022-04-22 19:43:25 +00:00
-- check that type is known
local _scada_type_valid = function ()
return self.type == SCADA_MGMT_TYPES.PING or
self.type == SCADA_MGMT_TYPES.CLOSE or
self.type == SCADA_MGMT_TYPES.REMOTE_LINKED or
self.type == SCADA_MGMT_TYPES.RTU_ADVERT or
self.type == SCADA_MGMT_TYPES.RTU_HEARTBEAT
end
-- make a SCADA management packet
2022-04-22 19:43:25 +00:00
local make = function (packet_type, data)
-- packet accessor properties
self.type = packet_type
2022-04-22 19:43:25 +00:00
self.length = #data
self.data = data
2022-04-22 19:43:25 +00:00
-- populate raw array
self.raw = { self.type }
for i = 1, #data do
2022-05-07 17:39:12 +00:00
insert(self.raw, data[i])
2022-04-22 19:43:25 +00:00
end
end
-- decode a SCADA management packet from a SCADA frame
local decode = function (frame)
if frame then
self.frame = frame
2022-04-22 19:43:25 +00:00
if frame.protocol() == PROTOCOLS.SCADA_MGMT then
local ok = frame.length() >= 1
if ok then
2022-04-22 19:43:25 +00:00
local data = frame.data()
make(data[1], { table.unpack(data, 2, #data) })
ok = _scada_type_valid()
end
return ok
else
log.debug("attempted SCADA_MGMT parse of incorrect protocol " .. frame.protocol(), true)
return false
end
else
log.debug("nil frame encountered", true)
return false
end
end
2022-04-22 19:43:25 +00:00
-- get raw to send
local raw_sendable = function () return self.raw end
-- get this packet
local get = function ()
return {
scada_frame = self.frame,
type = self.type,
length = self.length,
data = self.data
}
end
return {
2022-04-22 19:43:25 +00:00
-- construct
make = make,
decode = decode,
2022-04-22 19:43:25 +00:00
-- raw access
raw_sendable = raw_sendable,
-- formatted access
get = get
}
end
2022-04-22 15:07:59 +00:00
-- SCADA coordinator packet
-- @todo
comms.coord_packet = function ()
2022-04-22 15:07:59 +00:00
local self = {
frame = nil,
2022-04-22 19:43:25 +00:00
raw = nil,
2022-04-22 15:07:59 +00:00
type = nil,
length = nil,
data = nil
}
local _coord_type_valid = function ()
-- @todo
return false
end
-- make a coordinator packet
2022-04-22 19:43:25 +00:00
local make = function (packet_type, data)
-- packet accessor properties
2022-04-22 15:07:59 +00:00
self.type = packet_type
2022-04-22 19:43:25 +00:00
self.length = #data
2022-04-22 15:07:59 +00:00
self.data = data
2022-04-22 19:43:25 +00:00
-- populate raw array
self.raw = { self.type }
for i = 1, #data do
2022-05-07 17:39:12 +00:00
insert(self.raw, data[i])
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
local decode = function (frame)
if frame then
self.frame = frame
2022-04-22 19:43:25 +00:00
if frame.protocol() == PROTOCOLS.COORD_DATA then
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()
make(data[1], { table.unpack(data, 2, #data) })
2022-04-22 15:07:59 +00:00
ok = _coord_type_valid()
end
return ok
else
log.debug("attempted COORD_DATA parse of incorrect protocol " .. frame.protocol(), true)
2022-04-22 15:07:59 +00:00
return false
end
else
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
local raw_sendable = function () return self.raw end
-- get this packet
local get = function ()
return {
scada_frame = self.frame,
type = self.type,
length = self.length,
data = self.data
}
end
return {
-- construct
make = make,
decode = decode,
-- raw access
raw_sendable = raw_sendable,
-- formatted access
get = get
}
end
-- coordinator API (CAPI) packet
-- @todo
comms.capi_packet = function ()
2022-04-22 19:43:25 +00:00
local self = {
frame = nil,
raw = nil,
type = nil,
length = nil,
data = nil
}
local _coord_type_valid = function ()
-- @todo
return false
end
-- make a coordinator packet
local make = function (packet_type, data)
-- packet accessor properties
self.type = packet_type
self.length = #data
self.data = data
-- populate raw array
self.raw = { self.type }
for i = 1, #data do
2022-05-07 17:39:12 +00:00
insert(self.raw, data[i])
2022-04-22 19:43:25 +00:00
end
end
-- decode a coordinator packet from a SCADA frame
local decode = function (frame)
if frame then
self.frame = frame
if frame.protocol() == PROTOCOLS.COORD_API then
local ok = frame.length() >= 1
2022-04-22 19:43:25 +00:00
if ok then
local data = frame.data()
make(data[1], { table.unpack(data, 2, #data) })
ok = _coord_type_valid()
end
return ok
else
log.debug("attempted COORD_API parse of incorrect protocol " .. frame.protocol(), true)
2022-04-22 19:43:25 +00:00
return false
end
else
log.debug("nil frame encountered", true)
2022-04-22 19:43:25 +00:00
return false
end
end
-- get raw to send
local raw_sendable = function () return self.raw end
-- get this packet
2022-04-22 15:07:59 +00:00
local get = function ()
return {
scada_frame = self.frame,
type = self.type,
length = self.length,
data = self.data
}
end
return {
2022-04-22 19:43:25 +00:00
-- construct
2022-04-22 15:07:59 +00:00
make = make,
decode = decode,
2022-04-22 19:43:25 +00:00
-- raw access
raw_sendable = raw_sendable,
-- formatted access
2022-04-22 15:07:59 +00:00
get = get
}
end
return comms