diff --git a/scada-common/comms.lua b/scada-common/comms.lua index 34de9cb..3007cf9 100644 --- a/scada-common/comms.lua +++ b/scada-common/comms.lua @@ -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() local self = { reactor_struct_cache = nil } end +-- supervisory controller communications function superv_comms() local self = { reactor_struct_cache = nil } end +-- reactor PLC communications function rplc_comms(id, modem, local_port, server_port, reactor) local self = { _id = id, @@ -150,4 +220,4 @@ function rplc_comms(id, modem, local_port, server_port, reactor) send_keep_alive = send_keep_alive, handle_link = handle_link } -end \ No newline at end of file +end diff --git a/scada-common/modbus.lua b/scada-common/modbus.lua index e6d5eba..6721c44 100644 --- a/scada-common/modbus.lua +++ b/scada-common/modbus.lua @@ -1,3 +1,5 @@ +-- #REQUIRES comms.lua + -- modbus function codes local MODBUS_FCODE = { READ_COILS = 0x01, @@ -175,9 +177,8 @@ function modbus_init(rtu_dev) } end --- create new modbus packet -function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data) - return { +function modbus_packet() + local self = { txn_id = txn_id, protocol = protocol, length = length, @@ -185,25 +186,34 @@ function new_modbus_packet(txn_id, protocol, length, unit_id, func_code, data) func_code = func_code, data = data } -end --- parse raw table data as a modbus packet -function parse_modbus_packet(raw) - if #raw ~= 6 then - return nil - else - return new_modbus_packet(raw[1], raw[2], raw[3], raw[4], raw[5], raw[6]) + local receive = function (raw) + local size_ok = #raw ~= 6 + + if size_ok then + set(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 - --- 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