#7 work on PLC session comms, bugfixes with comms, general supervisor bugfixes

This commit is contained in:
Mikayla Fischler
2022-04-25 21:00:50 -04:00
parent 19a4b3c0ef
commit f7f723829c
9 changed files with 158 additions and 134 deletions

View File

@@ -23,11 +23,11 @@ function new()
end
local push_packet = function (message)
push(TYPE.PACKET, message)
_push(TYPE.PACKET, message)
end
local push_command = function (message)
push(TYPE.COMMAND, message)
_push(TYPE.COMMAND, message)
end
local pop = function ()

View File

@@ -70,6 +70,7 @@ function get_reactor_session(reactor)
end
function establish_plc_session(local_port, remote_port, for_reactor)
util.println(remote_port)
if get_reactor_session(for_reactor) == nil then
local plc_s = {
open = true,
@@ -81,9 +82,12 @@ function establish_plc_session(local_port, remote_port, for_reactor)
instance = nil
}
plc_s.instance = plc.new_session(next_plc_id, plc_s.in_queue, plc_s.out_queue)
plc_s.instance = plc.new_session(self.next_plc_id, for_reactor, plc_s.in_queue, plc_s.out_queue)
table.insert(self.plc_sessions, plc_s)
next_plc_id = next_plc_id + 1
log._debug("established new PLC session to " .. remote_port .. " with ID " .. self.next_plc_id)
self.next_plc_id = self.next_plc_id + 1
-- success
return plc_s.instance.get_id()
@@ -129,7 +133,7 @@ local function _iterate(sessions)
while not session.out_queue.empty() do
local msg = session.out_queue.pop()
if msg.qtype == mqueue.TYPE.PACKET then
self.modem.transmit(self.r_port, self.l_port, msg.message.raw_sendable())
self.modem.transmit(session.r_port, session.l_port, msg.message.raw_sendable())
end
end
else
@@ -163,7 +167,7 @@ local function _free_closed(sessions)
end
move_to = move_to + 1
else
log._debug("free'ing closing session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
log._debug("free'ing closed session " .. session.instance.get_id() .. " on remote port " .. session.r_port)
sessions[i] = nil
end
end

View File

@@ -18,7 +18,7 @@ os.loadAPI("session/svsessions.lua")
os.loadAPI("supervisor.lua")
local SUPERVISOR_VERSION = "alpha-v0.1.4"
local SUPERVISOR_VERSION = "alpha-v0.1.5"
local print = util.print
local println = util.println
@@ -93,7 +93,7 @@ while true do
svsessions.check_all_watchdogs(param1)
elseif event == "modem_message" then
-- got a packet
local packet = superv_comms.parse_packet(p1, p2, p3, p4, p5)
local packet = superv_comms.parse_packet(param1, param2, param3, param4, param5)
superv_comms.handle_packet(packet)
end

View File

@@ -5,6 +5,7 @@
local PROTOCOLS = comms.PROTOCOLS
local RPLC_TYPES = comms.RPLC_TYPES
local RPLC_LINKING = comms.RPLC_LINKING
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
local RTU_ADVERT_TYPES = comms.RTU_ADVERT_TYPES
@@ -64,7 +65,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
-- parse a packet
local parse_packet = function(side, sender, reply_to, message, distance)
local pkt = nil
local s_pkt = scada_packet()
local s_pkt = comms.scada_packet()
-- parse packet as generic SCADA packet
s_pkt.receive(side, sender, reply_to, message, distance)
@@ -104,21 +105,22 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
local handle_packet = function(packet)
if packet ~= nil then
local sender = packet.scada_frame.sender()
local receiver = packet.scada_frame.receiver()
local l_port = packet.scada_frame.local_port()
local r_port = packet.scada_frame.remote_port()
local protocol = packet.scada_frame.protocol()
-- device (RTU/PLC) listening channel
if receiver == self.dev_listen then
if l_port == self.dev_listen then
if protocol == PROTOCOLS.MODBUS_TCP then
-- MODBUS response
elseif protocol == PROTOCOLS.RPLC then
-- reactor PLC packet
local session = svsessions.find_session(SESSION_TYPE.PLC_SESSION, sender)
local session = svsessions.find_session(SESSION_TYPE.PLC_SESSION, r_port)
if session then
if packet.type == RPLC_TYPES.LINK_REQ then
-- new device on this port? that's a collision
_send_plc_linking(sender, { RPLC_LINKING.COLLISION })
log._debug("PLC_LNK: request from existing connection received on " .. r_port .. ", responding with collision")
_send_plc_linking(r_port, { RPLC_LINKING.COLLISION })
else
-- pass the packet onto the session handler
session.in_queue.push_packet(packet)
@@ -126,18 +128,25 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
else
-- unknown session, is this a linking request?
if packet.type == RPLC_TYPES.LINK_REQ then
-- this is a linking request
local plc_id = svsessions.establish_plc_session(sender)
if plc_id == false then
-- reactor already has a PLC assigned
_send_plc_linking(sender, { RPLC_LINKING.COLLISION })
if packet.length == 1 then
-- this is a linking request
local plc_id = svsessions.establish_plc_session(l_port, r_port, packet.data[1])
if plc_id == false then
-- reactor already has a PLC assigned
log._debug("PLC_LNK: assignment collision with reactor " .. packet.data[1])
_send_plc_linking(r_port, { RPLC_LINKING.COLLISION })
else
-- got an ID; assigned to a reactor successfully
log._debug("PLC_LNK: allowed for device at " .. r_port)
_send_plc_linking(r_port, { RPLC_LINKING.ALLOW })
end
else
-- got an ID; assigned to a reactor successfully
_send_plc_linking(sender, { RPLC_LINKING.ALLOW })
log._debug("PLC_LNK: new linking packet length mismatch")
end
else
-- force a re-link
_send_plc_linking(sender, { RPLC_LINKING.DENY })
log._debug("PLC_LNK: no session but not a link, force relink")
_send_plc_linking(r_port, { RPLC_LINKING.DENY })
end
end
elseif protocol == PROTOCOLS.SCADA_MGMT then
@@ -146,7 +155,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
log._debug("illegal packet type " .. protocol .. " on device listening channel")
end
-- coordinator listening channel
elseif receiver == self.coord_listen then
elseif l_port == self.coord_listen then
if protocol == PROTOCOLS.SCADA_MGMT then
-- SCADA management packet
elseif protocol == PROTOCOLS.COORD_DATA then
@@ -155,7 +164,7 @@ function superv_comms(num_reactors, modem, dev_listen, coord_listen)
log._debug("illegal packet type " .. protocol .. " on coordinator listening channel")
end
else
log._error("received packet on unused channel " .. receiver, true)
log._error("received packet on unused channel " .. l_port, true)
end
end
end