2022-12-06 16:40:13 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local util = require("scada-common.util")
|
2023-07-10 03:31:56 +00:00
|
|
|
local types = require("scada-common.types")
|
2022-06-25 17:39:47 +00:00
|
|
|
|
2022-09-07 02:38:27 +00:00
|
|
|
local iocontrol = require("coordinator.iocontrol")
|
2023-01-26 23:26:26 +00:00
|
|
|
local process = require("coordinator.process")
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2023-04-18 17:55:18 +00:00
|
|
|
local apisessions = require("coordinator.session.apisessions")
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
local PROTOCOL = comms.PROTOCOL
|
|
|
|
local DEVICE_TYPE = comms.DEVICE_TYPE
|
2022-11-13 19:13:30 +00:00
|
|
|
local ESTABLISH_ACK = comms.ESTABLISH_ACK
|
2023-08-30 20:45:48 +00:00
|
|
|
local MGMT_TYPE = comms.MGMT_TYPE
|
|
|
|
local CRDN_TYPE = comms.CRDN_TYPE
|
2023-02-21 16:05:57 +00:00
|
|
|
local UNIT_COMMAND = comms.UNIT_COMMAND
|
|
|
|
local FAC_COMMAND = comms.FAC_COMMAND
|
2022-05-10 21:09:02 +00:00
|
|
|
|
2023-07-11 17:32:26 +00:00
|
|
|
local LINK_TIMEOUT = 60.0
|
|
|
|
|
2022-12-06 16:40:13 +00:00
|
|
|
local coordinator = {}
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
---@type crd_config
|
|
|
|
local config = {}
|
|
|
|
|
|
|
|
coordinator.config = config
|
|
|
|
|
|
|
|
-- load the coordinator configuration<br>
|
|
|
|
-- status of 0 is OK, 1 is bad config, 2 is bad monitor config
|
|
|
|
---@return 0|1|2 status, nil|monitors_struct|string monitors
|
|
|
|
function coordinator.load_config()
|
|
|
|
if not settings.load("/coordinator.settings") then return 1 end
|
|
|
|
|
|
|
|
config.UnitCount = settings.get("UnitCount")
|
|
|
|
config.SpeakerVolume = settings.get("SpeakerVolume")
|
|
|
|
config.Time24Hour = settings.get("Time24Hour")
|
|
|
|
|
|
|
|
config.DisableFlowView = settings.get("DisableFlowView")
|
|
|
|
config.MainDisplay = settings.get("MainDisplay")
|
|
|
|
config.FlowDisplay = settings.get("FlowDisplay")
|
|
|
|
config.UnitDisplays = settings.get("UnitDisplays")
|
|
|
|
|
|
|
|
config.SVR_Channel = settings.get("SVR_Channel")
|
|
|
|
config.CRD_Channel = settings.get("CRD_Channel")
|
|
|
|
config.PKT_Channel = settings.get("PKT_Channel")
|
|
|
|
config.SVR_Timeout = settings.get("SVR_Timeout")
|
|
|
|
config.API_Timeout = settings.get("API_Timeout")
|
|
|
|
config.TrustedRange = settings.get("TrustedRange")
|
|
|
|
config.AuthKey = settings.get("AuthKey")
|
|
|
|
|
|
|
|
config.LogMode = settings.get("LogMode")
|
|
|
|
config.LogPath = settings.get("LogPath")
|
|
|
|
config.LogDebug = settings.get("LogDebug")
|
|
|
|
|
|
|
|
local cfv = util.new_validator()
|
|
|
|
|
|
|
|
cfv.assert_type_int(config.UnitCount)
|
|
|
|
cfv.assert_range(config.UnitCount, 1, 4)
|
|
|
|
cfv.assert_type_bool(config.Time24Hour)
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
cfv.assert_type_bool(config.DisableFlowView)
|
|
|
|
cfv.assert_type_table(config.UnitDisplays)
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
cfv.assert_type_num(config.SpeakerVolume)
|
|
|
|
cfv.assert_min(config.SpeakerVolume, 0.0)
|
|
|
|
cfv.assert_max(config.SpeakerVolume, 3.0)
|
|
|
|
|
|
|
|
cfv.assert_channel(config.SVR_Channel)
|
|
|
|
cfv.assert_channel(config.CRD_Channel)
|
|
|
|
cfv.assert_channel(config.PKT_Channel)
|
|
|
|
|
|
|
|
cfv.assert_type_num(config.SVR_Timeout)
|
|
|
|
cfv.assert_min(config.SVR_Timeout, 2)
|
|
|
|
cfv.assert_type_num(config.API_Timeout)
|
|
|
|
cfv.assert_min(config.API_Timeout, 2)
|
|
|
|
|
|
|
|
cfv.assert_type_num(config.TrustedRange)
|
|
|
|
cfv.assert_min(config.TrustedRange, 0)
|
|
|
|
cfv.assert_type_str(config.AuthKey)
|
|
|
|
|
|
|
|
if type(config.AuthKey) == "string" then
|
|
|
|
local len = string.len(config.AuthKey)
|
|
|
|
cfv.assert_eq(len == 0 or len >= 8, true)
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
cfv.assert_type_int(config.LogMode)
|
|
|
|
cfv.assert_range(config.LogMode, 0, 1)
|
|
|
|
cfv.assert_type_str(config.LogPath)
|
|
|
|
cfv.assert_type_bool(config.LogDebug)
|
|
|
|
|
|
|
|
-- Monitor Setup
|
2022-05-29 18:34:09 +00:00
|
|
|
|
|
|
|
---@class monitors_struct
|
|
|
|
local monitors = {
|
2023-09-03 21:54:39 +00:00
|
|
|
primary = nil, ---@type table|nil
|
2022-07-20 17:28:58 +00:00
|
|
|
primary_name = "",
|
2023-09-03 21:54:39 +00:00
|
|
|
flow = nil, ---@type table|nil
|
2023-08-10 03:26:06 +00:00
|
|
|
flow_name = "",
|
2022-07-20 17:28:58 +00:00
|
|
|
unit_displays = {},
|
|
|
|
unit_name_map = {}
|
2022-05-29 18:34:09 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
local mon_cfv = util.new_validator()
|
2022-05-29 18:34:09 +00:00
|
|
|
|
|
|
|
-- get all interface names
|
2024-02-18 20:21:00 +00:00
|
|
|
local names = {}
|
|
|
|
for iface, _ in pairs(ppm.get_monitor_list()) do table.insert(names, iface) end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
local function setup_monitors()
|
|
|
|
mon_cfv.assert_type_str(config.MainDisplay)
|
|
|
|
if not config.DisableFlowView then mon_cfv.assert_type_str(config.FlowDisplay) end
|
|
|
|
mon_cfv.assert_eq(#config.UnitDisplays, config.UnitCount)
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if mon_cfv.valid() then
|
2024-02-18 20:30:18 +00:00
|
|
|
local w, h, _
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
mon_cfv.assert(util.table_contains(names, config.MainDisplay))
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Main monitor is not connected." end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
monitors.primary = ppm.get_periph(config.MainDisplay)
|
|
|
|
monitors.primary_name = config.MainDisplay
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:30:18 +00:00
|
|
|
w, _ = ppm.monitor_block_size(monitors.primary.getSize())
|
2024-02-18 20:21:00 +00:00
|
|
|
mon_cfv.assert(w == 8)
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Main monitor width is incorrect." end
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not config.DisableFlowView then
|
|
|
|
mon_cfv.assert(util.table_contains(names, config.FlowDisplay))
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Flow monitor is not connected." end
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
monitors.flow = ppm.get_periph(config.FlowDisplay)
|
|
|
|
monitors.flow_name = config.FlowDisplay
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:30:18 +00:00
|
|
|
w, _ = ppm.monitor_block_size(monitors.flow.getSize())
|
2024-02-18 20:21:00 +00:00
|
|
|
mon_cfv.assert(w == 8)
|
2023-08-10 03:26:06 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Flow monitor width is incorrect." end
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
for i = 1, config.UnitCount do
|
|
|
|
local display = config.UnitDisplays[i]
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
mon_cfv.assert_type_str(display)
|
|
|
|
mon_cfv.assert(util.table_contains(names, display))
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Unit " .. i .. " monitor is not connected." end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
monitors.unit_displays[i] = ppm.get_periph(display)
|
|
|
|
monitors.unit_name_map[i] = display
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
w, h = ppm.monitor_block_size(monitors.unit_displays[i].getSize())
|
|
|
|
mon_cfv.assert(w == 4 and h == 4)
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if not mon_cfv.valid() then return 2, "Unit " .. i .. " monitor size is incorrect." end
|
|
|
|
end
|
|
|
|
else return 2, "Monitor configuration invalid." end
|
2023-01-24 01:47:45 +00:00
|
|
|
end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if cfv.valid() then
|
|
|
|
local ok, result, message = pcall(setup_monitors)
|
|
|
|
assert(ok, util.c("fatal error while trying to verify monitors: ", result))
|
|
|
|
if result == 2 then return 2, message end
|
|
|
|
else return 1 end
|
2022-05-29 18:34:09 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
return 0, monitors
|
2022-05-29 18:34:09 +00:00
|
|
|
end
|
|
|
|
|
2022-07-05 15:18:26 +00:00
|
|
|
-- dmesg print wrapper
|
|
|
|
---@param message string message
|
|
|
|
---@param dmesg_tag string tag
|
2022-07-06 03:48:01 +00:00
|
|
|
---@param working? boolean to use dmesg_working
|
|
|
|
---@return function? update, function? done
|
|
|
|
local function log_dmesg(message, dmesg_tag, working)
|
2022-07-05 15:18:26 +00:00
|
|
|
local colors = {
|
|
|
|
GRAPHICS = colors.green,
|
|
|
|
SYSTEM = colors.cyan,
|
|
|
|
BOOT = colors.blue,
|
2023-06-25 18:00:18 +00:00
|
|
|
COMMS = colors.purple,
|
|
|
|
CRYPTO = colors.yellow
|
2022-07-05 15:18:26 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 03:48:01 +00:00
|
|
|
if working then
|
|
|
|
return log.dmesg_working(message, dmesg_tag, colors[dmesg_tag])
|
|
|
|
else
|
|
|
|
log.dmesg(message, dmesg_tag, colors[dmesg_tag])
|
|
|
|
end
|
2022-07-05 15:18:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function coordinator.log_graphics(message) log_dmesg(message, "GRAPHICS") end
|
|
|
|
function coordinator.log_sys(message) log_dmesg(message, "SYSTEM") end
|
|
|
|
function coordinator.log_boot(message) log_dmesg(message, "BOOT") end
|
|
|
|
function coordinator.log_comms(message) log_dmesg(message, "COMMS") end
|
2023-06-25 18:00:18 +00:00
|
|
|
function coordinator.log_crypto(message) log_dmesg(message, "CRYPTO") end
|
2022-07-05 15:18:26 +00:00
|
|
|
|
2023-02-23 04:09:47 +00:00
|
|
|
-- log a message for communications connecting, providing access to progress indication control functions
|
|
|
|
---@nodiscard
|
2022-07-06 03:48:01 +00:00
|
|
|
---@param message string
|
|
|
|
---@return function update, function done
|
2022-09-21 19:53:51 +00:00
|
|
|
function coordinator.log_comms_connecting(message)
|
2023-02-23 04:09:47 +00:00
|
|
|
local update, done = log_dmesg(message, "COMMS", true)
|
|
|
|
---@cast update function
|
|
|
|
---@cast done function
|
|
|
|
return update, done
|
2022-09-21 19:53:51 +00:00
|
|
|
end
|
2022-07-06 03:48:01 +00:00
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- coordinator communications
|
2023-02-23 04:09:47 +00:00
|
|
|
---@nodiscard
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param version string coordinator version
|
2023-06-21 23:04:39 +00:00
|
|
|
---@param nic nic network interface device
|
2022-06-25 17:39:47 +00:00
|
|
|
---@param sv_watchdog watchdog
|
2024-02-18 20:21:00 +00:00
|
|
|
function coordinator.comms(version, nic, sv_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
2022-07-06 03:48:01 +00:00
|
|
|
sv_linked = false,
|
2023-06-06 23:41:09 +00:00
|
|
|
sv_addr = comms.BROADCAST,
|
2022-06-25 17:39:47 +00:00
|
|
|
sv_seq_num = 0,
|
|
|
|
sv_r_seq_num = nil,
|
2023-03-03 03:29:50 +00:00
|
|
|
sv_config_err = false,
|
2023-04-18 17:55:18 +00:00
|
|
|
last_est_ack = ESTABLISH_ACK.ALLOW,
|
2023-07-11 17:32:26 +00:00
|
|
|
last_api_est_acks = {},
|
|
|
|
est_start = 0,
|
|
|
|
est_last = 0,
|
|
|
|
est_tick_waiting = nil,
|
|
|
|
est_task_done = nil
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
2022-06-15 19:35:34 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
comms.set_trusted_range(config.TrustedRange)
|
2023-02-07 22:31:22 +00:00
|
|
|
|
2023-06-21 23:04:39 +00:00
|
|
|
-- configure network channels
|
|
|
|
nic.closeAll()
|
2024-02-18 20:21:00 +00:00
|
|
|
nic.open(config.CRD_Channel)
|
2022-06-15 19:35:34 +00:00
|
|
|
|
2023-06-21 23:04:39 +00:00
|
|
|
-- link nic to apisessions
|
|
|
|
apisessions.init(nic)
|
2023-04-20 00:35:42 +00:00
|
|
|
|
2024-01-31 19:10:03 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-06-25 17:39:47 +00:00
|
|
|
-- send a packet to the supervisor
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param msg_type MGMT_TYPE|CRDN_TYPE
|
2022-06-25 17:39:47 +00:00
|
|
|
---@param msg table
|
|
|
|
local function _send_sv(protocol, msg_type, msg)
|
2022-06-15 19:35:34 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
2023-04-12 20:02:29 +00:00
|
|
|
local pkt ---@type mgmt_packet|crdn_packet
|
2022-06-25 17:39:47 +00:00
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if protocol == PROTOCOL.SCADA_MGMT then
|
2022-06-25 17:39:47 +00:00
|
|
|
pkt = comms.mgmt_packet()
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_CRDN then
|
2022-07-07 04:34:42 +00:00
|
|
|
pkt = comms.crdn_packet()
|
2022-06-25 17:39:47 +00:00
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
2022-06-15 19:35:34 +00:00
|
|
|
|
2022-06-25 17:39:47 +00:00
|
|
|
pkt.make(msg_type, msg)
|
2023-06-06 23:41:09 +00:00
|
|
|
s_pkt.make(self.sv_addr, self.sv_seq_num, protocol, pkt.raw_sendable())
|
2022-06-15 19:35:34 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
nic.transmit(config.SVR_Channel, config.CRD_Channel, s_pkt)
|
2022-06-25 17:39:47 +00:00
|
|
|
self.sv_seq_num = self.sv_seq_num + 1
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
|
|
|
|
2023-04-18 17:55:18 +00:00
|
|
|
-- send an API establish request response
|
2023-06-06 23:41:09 +00:00
|
|
|
---@param packet scada_packet
|
|
|
|
---@param ack ESTABLISH_ACK
|
|
|
|
local function _send_api_establish_ack(packet, ack)
|
2023-04-18 17:55:18 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
m_pkt.make(MGMT_TYPE.ESTABLISH, { ack })
|
2023-06-06 23:41:09 +00:00
|
|
|
s_pkt.make(packet.src_addr(), packet.seq_num() + 1, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2023-04-18 17:55:18 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
nic.transmit(config.PKT_Channel, config.CRD_Channel, s_pkt)
|
2023-06-06 23:41:09 +00:00
|
|
|
self.last_api_est_acks[packet.src_addr()] = ack
|
2023-04-18 17:55:18 +00:00
|
|
|
end
|
|
|
|
|
2022-07-07 04:34:42 +00:00
|
|
|
-- attempt connection establishment
|
|
|
|
local function _send_establish()
|
2023-08-30 21:15:42 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_MGMT, MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.CRD })
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- keep alive ack
|
|
|
|
---@param srv_time integer
|
|
|
|
local function _send_keep_alive_ack(srv_time)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_MGMT, MGMT_TYPE.KEEP_ALIVE, { srv_time, util.time() })
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
|
|
|
|
2022-06-15 19:35:34 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2023-02-23 04:09:47 +00:00
|
|
|
---@class coord_comms
|
|
|
|
local public = {}
|
|
|
|
|
2023-07-11 17:32:26 +00:00
|
|
|
-- try to connect to the supervisor if not already linked
|
|
|
|
---@param abort boolean? true to print out cancel info if not linked (use on program terminate)
|
|
|
|
---@return boolean ok, boolean start_ui
|
|
|
|
function public.try_connect(abort)
|
|
|
|
local ok = true
|
|
|
|
local start_ui = false
|
|
|
|
|
|
|
|
if not self.sv_linked then
|
|
|
|
if self.est_tick_waiting == nil then
|
|
|
|
self.est_start = util.time_s()
|
|
|
|
self.est_last = self.est_start
|
|
|
|
|
|
|
|
self.est_tick_waiting, self.est_task_done =
|
2024-02-18 20:21:00 +00:00
|
|
|
coordinator.log_comms_connecting("attempting to connect to configured supervisor on channel " .. config.SVR_Channel)
|
2023-07-11 17:32:26 +00:00
|
|
|
|
|
|
|
_send_establish()
|
|
|
|
else
|
|
|
|
self.est_tick_waiting(math.max(0, LINK_TIMEOUT - (util.time_s() - self.est_start)))
|
|
|
|
end
|
|
|
|
|
|
|
|
if abort or (util.time_s() - self.est_start) >= LINK_TIMEOUT then
|
|
|
|
self.est_task_done(false)
|
|
|
|
|
|
|
|
if abort then
|
|
|
|
coordinator.log_comms("supervisor connection attempt cancelled by user")
|
|
|
|
elseif self.sv_config_err then
|
2024-02-18 20:21:00 +00:00
|
|
|
coordinator.log_comms("supervisor unit count does not match coordinator unit count, check configs")
|
2023-07-11 17:32:26 +00:00
|
|
|
elseif not self.sv_linked then
|
|
|
|
if self.last_est_ack == ESTABLISH_ACK.DENY then
|
|
|
|
coordinator.log_comms("supervisor connection attempt denied")
|
|
|
|
elseif self.last_est_ack == ESTABLISH_ACK.COLLISION then
|
|
|
|
coordinator.log_comms("supervisor connection failed due to collision")
|
|
|
|
elseif self.last_est_ack == ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
coordinator.log_comms("supervisor connection failed due to version mismatch")
|
|
|
|
else
|
|
|
|
coordinator.log_comms("supervisor connection failed with no valid response")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ok = false
|
|
|
|
elseif self.sv_config_err then
|
2024-02-18 20:21:00 +00:00
|
|
|
coordinator.log_comms("supervisor unit count does not match coordinator unit count, check configs")
|
2023-07-11 17:32:26 +00:00
|
|
|
ok = false
|
|
|
|
elseif (util.time_s() - self.est_last) > 1.0 then
|
|
|
|
_send_establish()
|
|
|
|
self.est_last = util.time_s()
|
|
|
|
end
|
|
|
|
elseif self.est_tick_waiting ~= nil then
|
|
|
|
self.est_task_done(true)
|
|
|
|
self.est_tick_waiting = nil
|
|
|
|
self.est_task_done = nil
|
|
|
|
start_ui = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return ok, start_ui
|
|
|
|
end
|
|
|
|
|
2022-07-20 17:28:58 +00:00
|
|
|
-- close the connection to the server
|
|
|
|
function public.close()
|
|
|
|
sv_watchdog.cancel()
|
2023-06-06 23:41:09 +00:00
|
|
|
self.sv_addr = comms.BROADCAST
|
2022-09-13 20:07:21 +00:00
|
|
|
self.sv_linked = false
|
2023-06-06 23:41:09 +00:00
|
|
|
self.sv_r_seq_num = nil
|
2023-07-10 03:31:56 +00:00
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.DISCONNECTED)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_MGMT, MGMT_TYPE.CLOSE, {})
|
2022-07-20 17:28:58 +00:00
|
|
|
end
|
|
|
|
|
2023-01-26 23:26:26 +00:00
|
|
|
-- send a facility command
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param cmd FAC_COMMAND command
|
2023-07-08 20:57:13 +00:00
|
|
|
---@param option any? optional option options for the optional options (like waste mode)
|
|
|
|
function public.send_fac_command(cmd, option)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.FAC_CMD, { cmd, option })
|
2023-01-26 23:26:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- send the auto process control configuration with a start command
|
|
|
|
---@param config coord_auto_config configuration
|
|
|
|
function public.send_auto_start(config)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.FAC_CMD, {
|
2023-02-21 16:05:57 +00:00
|
|
|
FAC_COMMAND.START, config.mode, config.burn_target, config.charge_target, config.gen_target, config.limits
|
2023-01-26 23:26:26 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-09-21 19:53:51 +00:00
|
|
|
-- send a unit command
|
2023-02-21 16:05:57 +00:00
|
|
|
---@param cmd UNIT_COMMAND command
|
2022-10-20 16:22:03 +00:00
|
|
|
---@param unit integer unit ID
|
2023-07-08 20:57:13 +00:00
|
|
|
---@param option any? optional option options for the optional options (like burn rate)
|
2023-01-26 23:26:26 +00:00
|
|
|
function public.send_unit_command(cmd, unit, option)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.UNIT_CMD, { cmd, unit, option })
|
2022-09-21 19:53:51 +00:00
|
|
|
end
|
|
|
|
|
2022-06-15 19:35:34 +00:00
|
|
|
-- parse a packet
|
|
|
|
---@param side string
|
|
|
|
---@param sender integer
|
|
|
|
---@param reply_to integer
|
|
|
|
---@param message any
|
|
|
|
---@param distance integer
|
2023-08-30 20:45:48 +00:00
|
|
|
---@return mgmt_frame|crdn_frame|nil packet
|
2022-06-15 19:35:34 +00:00
|
|
|
function public.parse_packet(side, sender, reply_to, message, distance)
|
2023-06-23 18:12:41 +00:00
|
|
|
local s_pkt = nic.receive(side, sender, reply_to, message, distance)
|
2022-06-15 19:35:34 +00:00
|
|
|
local pkt = nil
|
|
|
|
|
2023-06-23 18:12:41 +00:00
|
|
|
if s_pkt then
|
2022-06-15 19:35:34 +00:00
|
|
|
-- get as SCADA management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
|
2022-06-15 19:35:34 +00:00
|
|
|
local mgmt_pkt = comms.mgmt_packet()
|
|
|
|
if mgmt_pkt.decode(s_pkt) then
|
|
|
|
pkt = mgmt_pkt.get()
|
|
|
|
end
|
|
|
|
-- get as coordinator packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.SCADA_CRDN then
|
2022-07-07 04:34:42 +00:00
|
|
|
local crdn_pkt = comms.crdn_packet()
|
|
|
|
if crdn_pkt.decode(s_pkt) then
|
|
|
|
pkt = crdn_pkt.get()
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("attempted parse of illegal packet type " .. s_pkt.protocol(), true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return pkt
|
|
|
|
end
|
|
|
|
|
|
|
|
-- handle a packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param packet mgmt_frame|crdn_frame|nil
|
2023-07-11 17:32:26 +00:00
|
|
|
---@return boolean close_ui
|
2022-06-15 19:35:34 +00:00
|
|
|
function public.handle_packet(packet)
|
2023-07-11 17:32:26 +00:00
|
|
|
local was_linked = self.sv_linked
|
|
|
|
|
2022-06-15 19:35:34 +00:00
|
|
|
if packet ~= nil then
|
2023-06-06 23:41:09 +00:00
|
|
|
local l_chan = packet.scada_frame.local_channel()
|
|
|
|
local r_chan = packet.scada_frame.remote_channel()
|
|
|
|
local src_addr = packet.scada_frame.src_addr()
|
2023-04-18 17:55:18 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
2022-06-15 19:35:34 +00:00
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if l_chan ~= config.CRD_Channel then
|
2023-06-06 23:41:09 +00:00
|
|
|
log.debug("received packet on unconfigured channel " .. l_chan, true)
|
2024-02-18 20:21:00 +00:00
|
|
|
elseif r_chan == config.PKT_Channel then
|
2023-07-11 17:32:26 +00:00
|
|
|
if not self.sv_linked then
|
|
|
|
log.debug("discarding pocket API packet before linked to supervisor")
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_CRDN then
|
|
|
|
---@cast packet crdn_frame
|
2023-04-18 17:55:18 +00:00
|
|
|
-- look for an associated session
|
2023-06-06 23:41:09 +00:00
|
|
|
local session = apisessions.find_session(src_addr)
|
2023-04-18 17:55:18 +00:00
|
|
|
|
2023-08-30 20:45:48 +00:00
|
|
|
-- coordinator packet
|
2023-04-18 17:55:18 +00:00
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-08-30 20:45:48 +00:00
|
|
|
log.debug("discarding SCADA_CRDN packet without a known session")
|
2023-04-18 17:55:18 +00:00
|
|
|
end
|
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
|
|
|
---@cast packet mgmt_frame
|
|
|
|
-- look for an associated session
|
2023-06-06 23:41:09 +00:00
|
|
|
local session = apisessions.find_session(src_addr)
|
2023-04-18 17:55:18 +00:00
|
|
|
|
|
|
|
-- SCADA management packet
|
|
|
|
if session ~= nil then
|
|
|
|
-- pass the packet onto the session handler
|
|
|
|
session.in_queue.push_packet(packet)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2023-04-18 17:55:18 +00:00
|
|
|
-- establish a new session
|
|
|
|
-- validate packet and continue
|
|
|
|
if packet.length == 3 and type(packet.data[1]) == "string" and type(packet.data[2]) == "string" then
|
|
|
|
local comms_v = packet.data[1]
|
|
|
|
local firmware_v = packet.data[2]
|
|
|
|
local dev_type = packet.data[3]
|
|
|
|
|
|
|
|
if comms_v ~= comms.version then
|
2023-06-06 23:41:09 +00:00
|
|
|
if self.last_api_est_acks[src_addr] ~= ESTABLISH_ACK.BAD_VERSION then
|
2023-04-18 17:55:18 +00:00
|
|
|
log.info(util.c("dropping API establish packet with incorrect comms version v", comms_v, " (expected v", comms.version, ")"))
|
|
|
|
end
|
|
|
|
|
2023-06-06 23:41:09 +00:00
|
|
|
_send_api_establish_ack(packet.scada_frame, ESTABLISH_ACK.BAD_VERSION)
|
2023-04-18 17:55:18 +00:00
|
|
|
elseif dev_type == DEVICE_TYPE.PKT then
|
|
|
|
-- pocket linking request
|
2023-06-06 23:41:09 +00:00
|
|
|
local id = apisessions.establish_session(src_addr, firmware_v)
|
|
|
|
coordinator.log_comms(util.c("API_ESTABLISH: pocket (", firmware_v, ") [@", src_addr, "] connected with session ID ", id))
|
2023-04-18 17:55:18 +00:00
|
|
|
|
2023-06-06 23:41:09 +00:00
|
|
|
_send_api_establish_ack(packet.scada_frame, ESTABLISH_ACK.ALLOW)
|
2023-04-18 17:55:18 +00:00
|
|
|
else
|
2023-06-06 23:41:09 +00:00
|
|
|
log.debug(util.c("API_ESTABLISH: illegal establish packet for device ", dev_type, " on pocket channel"))
|
|
|
|
_send_api_establish_ack(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2023-04-18 17:55:18 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("invalid establish packet (on API listening channel)")
|
2023-06-06 23:41:09 +00:00
|
|
|
_send_api_establish_ack(packet.scada_frame, ESTABLISH_ACK.DENY)
|
2023-04-18 17:55:18 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- any other packet should be session related, discard it
|
2023-06-06 23:41:09 +00:00
|
|
|
log.debug(util.c("discarding pocket SCADA_MGMT packet without a known session from computer ", src_addr))
|
2023-04-18 17:55:18 +00:00
|
|
|
end
|
2022-09-23 00:42:06 +00:00
|
|
|
else
|
2023-06-06 23:41:09 +00:00
|
|
|
log.debug("illegal packet type " .. protocol .. " on pocket channel", true)
|
2022-09-23 00:42:06 +00:00
|
|
|
end
|
2024-02-18 20:21:00 +00:00
|
|
|
elseif r_chan == config.SVR_Channel then
|
2022-06-25 17:39:47 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.sv_r_seq_num == nil then
|
|
|
|
self.sv_r_seq_num = packet.scada_frame.seq_num()
|
2023-07-11 17:32:26 +00:00
|
|
|
elseif self.sv_linked and ((self.sv_r_seq_num + 1) ~= packet.scada_frame.seq_num()) then
|
2022-06-25 17:39:47 +00:00
|
|
|
log.warning("sequence out-of-order: last = " .. self.sv_r_seq_num .. ", new = " .. packet.scada_frame.seq_num())
|
2023-07-11 17:32:26 +00:00
|
|
|
return false
|
2023-06-06 23:41:09 +00:00
|
|
|
elseif self.sv_linked and src_addr ~= self.sv_addr then
|
|
|
|
log.debug("received packet from unknown computer " .. src_addr .. " while linked; channel in use by another system?")
|
2023-07-11 17:32:26 +00:00
|
|
|
return false
|
2022-06-15 19:35:34 +00:00
|
|
|
else
|
2022-06-25 17:39:47 +00:00
|
|
|
self.sv_r_seq_num = packet.scada_frame.seq_num()
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
2022-06-25 17:39:47 +00:00
|
|
|
|
|
|
|
-- feed watchdog on valid sequence number
|
|
|
|
sv_watchdog.feed()
|
|
|
|
|
|
|
|
-- handle packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if protocol == PROTOCOL.SCADA_CRDN then
|
2023-02-23 04:09:47 +00:00
|
|
|
---@cast packet crdn_frame
|
2022-11-13 19:13:30 +00:00
|
|
|
if self.sv_linked then
|
2023-08-30 20:45:48 +00:00
|
|
|
if packet.type == CRDN_TYPE.INITIAL_BUILDS then
|
2023-02-20 19:50:20 +00:00
|
|
|
if packet.length == 2 then
|
|
|
|
-- record builds
|
|
|
|
local fac_builds = iocontrol.record_facility_builds(packet.data[1])
|
|
|
|
local unit_builds = iocontrol.record_unit_builds(packet.data[2])
|
|
|
|
|
|
|
|
if fac_builds and unit_builds then
|
|
|
|
-- acknowledge receipt of builds
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.INITIAL_BUILDS, {})
|
2023-02-20 19:50:20 +00:00
|
|
|
else
|
2023-02-23 04:09:47 +00:00
|
|
|
log.debug("received invalid INITIAL_BUILDS packet")
|
2023-02-20 19:50:20 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("INITIAL_BUILDS packet length mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.FAC_BUILDS then
|
2022-12-10 20:44:11 +00:00
|
|
|
if packet.length == 1 then
|
|
|
|
-- record facility builds
|
|
|
|
if iocontrol.record_facility_builds(packet.data[1]) then
|
|
|
|
-- acknowledge receipt of builds
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.FAC_BUILDS, {})
|
2022-12-10 20:44:11 +00:00
|
|
|
else
|
2023-02-23 04:09:47 +00:00
|
|
|
log.debug("received invalid FAC_BUILDS packet")
|
2022-12-10 20:44:11 +00:00
|
|
|
end
|
2022-12-10 18:58:17 +00:00
|
|
|
else
|
2022-12-10 20:44:11 +00:00
|
|
|
log.debug("FAC_BUILDS packet length mismatch")
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.FAC_STATUS then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- update facility status
|
|
|
|
if not iocontrol.update_facility_status(packet.data) then
|
2023-02-23 04:09:47 +00:00
|
|
|
log.debug("received invalid FAC_STATUS packet")
|
2022-12-10 18:58:17 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.FAC_CMD then
|
2022-12-10 18:58:17 +00:00
|
|
|
-- facility command acknowledgement
|
2023-01-26 23:26:26 +00:00
|
|
|
if packet.length >= 2 then
|
|
|
|
local cmd = packet.data[1]
|
|
|
|
local ack = packet.data[2] == true
|
|
|
|
|
2023-02-21 16:05:57 +00:00
|
|
|
if cmd == FAC_COMMAND.SCRAM_ALL then
|
2023-01-26 23:26:26 +00:00
|
|
|
iocontrol.get_db().facility.scram_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == FAC_COMMAND.STOP then
|
2023-01-26 23:26:26 +00:00
|
|
|
iocontrol.get_db().facility.stop_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == FAC_COMMAND.START then
|
2023-01-26 23:26:26 +00:00
|
|
|
if packet.length == 7 then
|
|
|
|
process.start_ack_handle({ table.unpack(packet.data, 2) })
|
|
|
|
else
|
|
|
|
log.debug("SCADA_CRDN process start (with configuration) ack echo packet length mismatch")
|
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == FAC_COMMAND.ACK_ALL_ALARMS then
|
2023-02-07 23:44:34 +00:00
|
|
|
iocontrol.get_db().facility.ack_alarms_ack(ack)
|
2023-07-08 20:57:13 +00:00
|
|
|
elseif cmd == FAC_COMMAND.SET_WASTE_MODE then
|
|
|
|
process.waste_ack_handle(packet.data[2])
|
|
|
|
elseif cmd == FAC_COMMAND.SET_PU_FB then
|
|
|
|
process.pu_fb_ack_handle(packet.data[2])
|
2023-01-26 23:26:26 +00:00
|
|
|
else
|
|
|
|
log.debug(util.c("received facility command ack with unknown command ", cmd))
|
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("SCADA_CRDN facility command ack packet length mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.UNIT_BUILDS then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- record builds
|
2023-02-20 05:49:37 +00:00
|
|
|
if packet.length == 1 then
|
|
|
|
if iocontrol.record_unit_builds(packet.data[1]) then
|
|
|
|
-- acknowledge receipt of builds
|
2023-08-30 20:45:48 +00:00
|
|
|
_send_sv(PROTOCOL.SCADA_CRDN, CRDN_TYPE.UNIT_BUILDS, {})
|
2023-02-20 05:49:37 +00:00
|
|
|
else
|
2023-02-23 04:09:47 +00:00
|
|
|
log.debug("received invalid UNIT_BUILDS packet")
|
2023-02-20 05:49:37 +00:00
|
|
|
end
|
2022-07-06 03:48:01 +00:00
|
|
|
else
|
2023-02-20 05:49:37 +00:00
|
|
|
log.debug("UNIT_BUILDS packet length mismatch")
|
2022-07-06 03:48:01 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.UNIT_STATUSES then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- update statuses
|
2022-12-10 18:58:17 +00:00
|
|
|
if not iocontrol.update_unit_statuses(packet.data) then
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug("received invalid UNIT_STATUSES packet")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == CRDN_TYPE.UNIT_CMD then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- unit command acknowledgement
|
|
|
|
if packet.length == 3 then
|
|
|
|
local cmd = packet.data[1]
|
|
|
|
local unit_id = packet.data[2]
|
2023-01-23 20:10:41 +00:00
|
|
|
local ack = packet.data[3] == true
|
2022-11-13 19:13:30 +00:00
|
|
|
|
2023-01-23 20:10:41 +00:00
|
|
|
local unit = iocontrol.get_db().units[unit_id] ---@type ioctl_unit
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
if unit ~= nil then
|
2023-02-21 16:05:57 +00:00
|
|
|
if cmd == UNIT_COMMAND.SCRAM then
|
2022-11-13 19:13:30 +00:00
|
|
|
unit.scram_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.START then
|
2022-11-13 19:13:30 +00:00
|
|
|
unit.start_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.RESET_RPS then
|
2022-11-13 19:13:30 +00:00
|
|
|
unit.reset_rps_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.SET_BURN then
|
2022-11-13 19:13:30 +00:00
|
|
|
unit.set_burn_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.SET_WASTE then
|
2022-11-13 19:13:30 +00:00
|
|
|
unit.set_waste_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.ACK_ALL_ALARMS then
|
2022-11-26 21:18:31 +00:00
|
|
|
unit.ack_alarms_ack(ack)
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif cmd == UNIT_COMMAND.SET_GROUP then
|
2023-02-23 04:09:47 +00:00
|
|
|
-- UI will be updated to display current group if changed successfully
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
2023-01-23 20:10:41 +00:00
|
|
|
log.debug(util.c("received unit command ack with unknown command ", cmd))
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2022-11-06 23:41:52 +00:00
|
|
|
else
|
2023-01-23 20:10:41 +00:00
|
|
|
log.debug(util.c("received unit command ack with unknown unit ", unit_id))
|
2022-11-06 23:41:52 +00:00
|
|
|
end
|
2022-10-20 16:22:03 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
log.debug("SCADA_CRDN unit command ack packet length mismatch")
|
2022-10-20 16:22:03 +00:00
|
|
|
end
|
|
|
|
else
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug("received unknown SCADA_CRDN packet type " .. packet.type)
|
2022-10-20 16:22:03 +00:00
|
|
|
end
|
2022-06-25 17:39:47 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
log.debug("discarding SCADA_CRDN packet before linked")
|
2022-06-25 17:39:47 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
2023-02-23 04:09:47 +00:00
|
|
|
---@cast packet mgmt_frame
|
2023-07-11 17:32:26 +00:00
|
|
|
if self.sv_linked then
|
2023-08-30 20:45:48 +00:00
|
|
|
if packet.type == MGMT_TYPE.KEEP_ALIVE then
|
2023-07-11 17:32:26 +00:00
|
|
|
-- keep alive request received, echo back
|
|
|
|
if packet.length == 1 then
|
|
|
|
local timestamp = packet.data[1]
|
|
|
|
local trip_time = util.time() - timestamp
|
|
|
|
|
|
|
|
if trip_time > 750 then
|
|
|
|
log.warning("coordinator KEEP_ALIVE trip time > 750ms (" .. trip_time .. "ms)")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- log.debug("coordinator RTT = " .. trip_time .. "ms")
|
|
|
|
|
|
|
|
iocontrol.get_db().facility.ps.publish("sv_ping", trip_time)
|
|
|
|
|
|
|
|
_send_keep_alive_ack(timestamp)
|
|
|
|
else
|
|
|
|
log.debug("SCADA keep alive packet length mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.CLOSE then
|
2023-07-11 17:32:26 +00:00
|
|
|
-- handle session close
|
|
|
|
sv_watchdog.cancel()
|
|
|
|
self.sv_addr = comms.BROADCAST
|
|
|
|
self.sv_linked = false
|
|
|
|
self.sv_r_seq_num = nil
|
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.DISCONNECTED)
|
|
|
|
log.info("server connection closed by remote host")
|
|
|
|
else
|
|
|
|
log.debug("received unknown SCADA_MGMT packet type " .. packet.type)
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- connection with supervisor established
|
|
|
|
if packet.length == 2 then
|
|
|
|
local est_ack = packet.data[1]
|
2024-02-18 20:21:00 +00:00
|
|
|
local sv_config = packet.data[2]
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
if est_ack == ESTABLISH_ACK.ALLOW then
|
2023-07-10 03:31:56 +00:00
|
|
|
-- reset to disconnected before validating
|
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.DISCONNECTED)
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if type(sv_config) == "table" and #sv_config == 2 then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- get configuration
|
|
|
|
|
|
|
|
---@class facility_conf
|
|
|
|
local conf = {
|
2024-02-18 20:21:00 +00:00
|
|
|
num_units = sv_config[1], ---@type integer
|
|
|
|
cooling = sv_config[2] ---@type sv_cooling_conf
|
2022-11-13 19:13:30 +00:00
|
|
|
}
|
|
|
|
|
2024-02-18 20:21:00 +00:00
|
|
|
if conf.num_units == config.UnitCount then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- init io controller
|
|
|
|
iocontrol.init(conf, public)
|
|
|
|
|
2023-06-06 23:41:09 +00:00
|
|
|
self.sv_addr = src_addr
|
2022-11-13 19:13:30 +00:00
|
|
|
self.sv_linked = true
|
2023-07-11 17:32:26 +00:00
|
|
|
self.sv_r_seq_num = nil
|
2023-03-03 03:29:50 +00:00
|
|
|
self.sv_config_err = false
|
2023-07-10 03:31:56 +00:00
|
|
|
|
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.LINKED)
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
2023-03-03 03:29:50 +00:00
|
|
|
self.sv_config_err = true
|
2023-08-19 17:38:05 +00:00
|
|
|
log.warning("supervisor config's number of units don't match coordinator's config, establish failed")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
else
|
2023-02-23 04:09:47 +00:00
|
|
|
log.debug("invalid supervisor configuration table received, establish failed")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
else
|
2023-02-16 00:59:58 +00:00
|
|
|
log.debug("SCADA_MGMT establish packet reply (len = 2) unsupported")
|
2022-07-07 04:34:42 +00:00
|
|
|
end
|
2023-02-16 00:59:58 +00:00
|
|
|
|
|
|
|
self.last_est_ack = est_ack
|
|
|
|
elseif packet.length == 1 then
|
|
|
|
local est_ack = packet.data[1]
|
|
|
|
|
|
|
|
if est_ack == ESTABLISH_ACK.DENY then
|
|
|
|
if self.last_est_ack ~= est_ack then
|
2023-07-10 03:31:56 +00:00
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.DENIED)
|
2023-02-23 04:09:47 +00:00
|
|
|
log.info("supervisor connection denied")
|
2023-02-16 00:59:58 +00:00
|
|
|
end
|
|
|
|
elseif est_ack == ESTABLISH_ACK.COLLISION then
|
|
|
|
if self.last_est_ack ~= est_ack then
|
2023-07-10 03:31:56 +00:00
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.COLLISION)
|
2023-04-19 13:30:17 +00:00
|
|
|
log.warning("supervisor connection denied due to collision")
|
2023-02-16 00:59:58 +00:00
|
|
|
end
|
|
|
|
elseif est_ack == ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
if self.last_est_ack ~= est_ack then
|
2023-07-10 03:31:56 +00:00
|
|
|
iocontrol.fp_link_state(types.PANEL_LINK_STATE.BAD_VERSION)
|
2023-04-19 13:30:17 +00:00
|
|
|
log.warning("supervisor comms version mismatch")
|
2023-02-16 00:59:58 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT establish packet reply (len = 1) unsupported")
|
|
|
|
end
|
|
|
|
|
|
|
|
self.last_est_ack = est_ack
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT establish packet length mismatch")
|
|
|
|
end
|
2022-06-25 17:39:47 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
log.debug("discarding non-link SCADA_MGMT packet before linked")
|
2022-06-25 17:39:47 +00:00
|
|
|
end
|
2022-06-15 19:35:34 +00:00
|
|
|
else
|
2022-09-23 00:42:06 +00:00
|
|
|
log.debug("illegal packet type " .. protocol .. " on supervisor listening channel", true)
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
2022-09-23 00:42:06 +00:00
|
|
|
else
|
2023-06-06 23:41:09 +00:00
|
|
|
log.debug("received packet for unknown channel " .. r_chan, true)
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
|
|
|
end
|
2023-07-11 17:32:26 +00:00
|
|
|
|
|
|
|
return was_linked and not self.sv_linked
|
2022-06-15 19:35:34 +00:00
|
|
|
end
|
|
|
|
|
2022-09-03 15:51:27 +00:00
|
|
|
-- check if the coordinator is still linked to the supervisor
|
2023-02-23 04:09:47 +00:00
|
|
|
---@nodiscard
|
2022-09-03 15:51:27 +00:00
|
|
|
function public.is_linked() return self.sv_linked end
|
|
|
|
|
2022-06-15 19:35:34 +00:00
|
|
|
return public
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-05-10 21:09:02 +00:00
|
|
|
|
|
|
|
return coordinator
|