some comms cleanup and added wrapper for generic packet

This commit is contained in:
Mikayla Fischler 2022-01-14 16:32:20 -05:00
parent 00a81ab4f0
commit 018b228976
2 changed files with 103 additions and 23 deletions

View File

@ -1,16 +1,86 @@
PROTOCOLS = {
MODBUS_TCP = 0, -- our "modbus tcp"-esque protocol
RPLC = 1, -- reactor plc protocol
SCADA_MGMT = 2, -- SCADA supervisor intercommunication
COORD_DATA = 3 -- data packets for coordinators to/from supervisory controller
}
-- generic SCADA packet object
function scada_packet()
local self = {
modem_msg_in = nil,
raw = nil
seq_id = nil,
protocol = nil,
length = nil
}
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 #self.raw < 3 then
-- malformed
return false
else
self.seq_id = self.raw[0]
self.protocol = self.raw[1]
self.length = self.raw[2]
end
end
local seq_id = function (packet)
return self.seq_id
end
local protocol = function (packet)
return self.protocol
end
local length = function (packet)
return self.length
end
local raw = function (packet)
return self.raw
end
local modem_event = function (packet)
return self.modem_msg_in
end
return {
receive = receive,
seq_id = seq_id,
protocol = protocol,
length = length,
raw = raw,
modem_event = modem_event
}
end
-- coordinator communications
function coord_comms() function coord_comms()
local self = { local self = {
reactor_struct_cache = nil reactor_struct_cache = nil
} }
end end
-- supervisory controller communications
function superv_comms() function superv_comms()
local self = { local self = {
reactor_struct_cache = nil reactor_struct_cache = nil
} }
end end
-- reactor PLC communications
function rplc_comms(id, modem, local_port, server_port, reactor) function rplc_comms(id, modem, local_port, server_port, reactor)
local self = { local self = {
_id = id, _id = id,

View File

@ -1,3 +1,5 @@
-- #REQUIRES comms.lua
-- modbus function codes -- modbus function codes
local MODBUS_FCODE = { local MODBUS_FCODE = {
READ_COILS = 0x01, READ_COILS = 0x01,
@ -175,9 +177,8 @@ function modbus_init(rtu_dev)
} }
end end
-- create new modbus packet function modbus_packet()
function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data) local self = {
return {
txn_id = txn_id, txn_id = txn_id,
protocol = protocol, protocol = protocol,
length = length, length = length,
@ -185,25 +186,34 @@ function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data)
func_code = func_code, func_code = func_code,
data = data data = data
} }
end
-- parse raw table data as a modbus packet local receive = function (raw)
function parse_modbus_packet(raw) local size_ok = #raw ~= 6
if #raw ~= 6 then
return nil if size_ok then
else set(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6])
return new_modbus_packet(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6]) end
return size_ok and self.protocol == comms.PROTOCOLS.MODBUS_TCP
end
local set = function (txn_id, protocol, length, unit_id, func_code, data)
self.txn_id = txn_id
self.protocol = protocol
self.length = length
self.unit_id = unit_id
self.func_code = func_code
self.data = data
end
local get = function ()
return {
txn_id = self.txn_id,
protocol = self.protocol,
length = self.length,
unit_id = self.unit_id,
func_code = self.func_code,
data = self.data
}
end end
end end
-- create raw table data from a modbus packet
function modbus_to_raw(packet)
return {
packet.txn_id,
packet.protocol,
packet.length,
packet.unit_id,
packet.func_code,
packet.data
}
end