2023-07-27 00:48:44 +00:00
|
|
|
local audio = require("scada-common.audio")
|
2023-04-21 00:40:28 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local types = require("scada-common.types")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2024-03-24 16:56:51 +00:00
|
|
|
local themes = require("graphics.themes")
|
|
|
|
|
2023-04-21 00:40:28 +00:00
|
|
|
local databus = require("rtu.databus")
|
|
|
|
local modbus = require("rtu.modbus")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local rtu = {}
|
2022-03-25 16:17:46 +00:00
|
|
|
|
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
|
2023-02-21 17:27:16 +00:00
|
|
|
local RTU_UNIT_TYPE = types.RTU_UNIT_TYPE
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
---@type rtu_config
|
|
|
|
local config = {}
|
|
|
|
|
|
|
|
rtu.config = config
|
|
|
|
|
|
|
|
-- load the RTU configuration
|
|
|
|
function rtu.load_config()
|
|
|
|
if not settings.load("/rtu.settings") then return false end
|
|
|
|
|
|
|
|
config.Peripherals = settings.get("Peripherals")
|
|
|
|
config.Redstone = settings.get("Redstone")
|
|
|
|
|
|
|
|
config.SpeakerVolume = settings.get("SpeakerVolume")
|
2024-03-08 00:23:46 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
config.SVR_Channel = settings.get("SVR_Channel")
|
|
|
|
config.RTU_Channel = settings.get("RTU_Channel")
|
|
|
|
config.ConnTimeout = settings.get("ConnTimeout")
|
|
|
|
config.TrustedRange = settings.get("TrustedRange")
|
|
|
|
config.AuthKey = settings.get("AuthKey")
|
2024-03-08 00:23:46 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
config.LogMode = settings.get("LogMode")
|
|
|
|
config.LogPath = settings.get("LogPath")
|
|
|
|
config.LogDebug = settings.get("LogDebug")
|
|
|
|
|
2024-03-08 00:23:46 +00:00
|
|
|
config.FrontPanelTheme = settings.get("FrontPanelTheme")
|
|
|
|
config.ColorMode = settings.get("ColorMode")
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
local cfv = util.new_validator()
|
|
|
|
|
|
|
|
cfv.assert_type_num(config.SpeakerVolume)
|
2024-02-18 21:49:39 +00:00
|
|
|
cfv.assert_range(config.SpeakerVolume, 0, 3)
|
2024-02-18 20:25:30 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
cfv.assert_channel(config.SVR_Channel)
|
|
|
|
cfv.assert_channel(config.RTU_Channel)
|
2023-12-31 01:25:57 +00:00
|
|
|
cfv.assert_type_num(config.ConnTimeout)
|
2023-11-06 14:25:44 +00:00
|
|
|
cfv.assert_min(config.ConnTimeout, 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)
|
2024-04-12 00:30:54 +00:00
|
|
|
cfv.assert(len == 0 or len >= 8)
|
2023-11-06 14:25:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
cfv.assert_type_int(config.LogMode)
|
2024-02-18 20:25:30 +00:00
|
|
|
cfv.assert_range(config.LogMode, 0, 1)
|
2023-11-06 14:25:44 +00:00
|
|
|
cfv.assert_type_str(config.LogPath)
|
|
|
|
cfv.assert_type_bool(config.LogDebug)
|
|
|
|
|
2024-03-08 00:23:46 +00:00
|
|
|
cfv.assert_type_int(config.FrontPanelTheme)
|
|
|
|
cfv.assert_range(config.FrontPanelTheme, 1, 2)
|
|
|
|
cfv.assert_type_int(config.ColorMode)
|
2024-03-24 16:56:51 +00:00
|
|
|
cfv.assert_range(config.ColorMode, 1, themes.COLOR_MODE.NUM_MODES)
|
2024-03-08 00:23:46 +00:00
|
|
|
|
2023-11-16 00:28:32 +00:00
|
|
|
cfv.assert_type_table(config.Peripherals)
|
|
|
|
cfv.assert_type_table(config.Redstone)
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
return cfv.valid()
|
|
|
|
end
|
|
|
|
|
2024-03-09 18:24:06 +00:00
|
|
|
-- create a new RTU unit<br>
|
|
|
|
-- if this is for a PPM peripheral, auto fault clearing MUST stay enabled once access begins
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2024-03-09 18:24:06 +00:00
|
|
|
---@param device table|nil peripheral device, if applicable
|
|
|
|
function rtu.init_unit(device)
|
2022-01-13 15:12:44 +00:00
|
|
|
local self = {
|
|
|
|
discrete_inputs = {},
|
|
|
|
coils = {},
|
|
|
|
input_regs = {},
|
2022-01-14 17:42:11 +00:00
|
|
|
holding_regs = {},
|
|
|
|
io_count_cache = { 0, 0, 0, 0 }
|
2022-01-13 15:12:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
local insert = table.insert
|
|
|
|
|
2024-03-23 04:26:58 +00:00
|
|
|
local stub = function () log.warning("tried to call an RTU function stub") end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
---@class rtu_device
|
2022-05-11 16:03:15 +00:00
|
|
|
local public = {}
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
---@class rtu
|
|
|
|
local protected = {}
|
2022-05-07 17:39:12 +00:00
|
|
|
|
2024-03-09 18:24:06 +00:00
|
|
|
-- function to check if the peripheral (if exists) is faulted
|
|
|
|
local function _is_faulted() return false end
|
|
|
|
if device then _is_faulted = device.__p_is_faulted end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- refresh IO count
|
2022-05-31 18:54:55 +00:00
|
|
|
local function _count_io()
|
2022-01-14 17:42:11 +00:00
|
|
|
self.io_count_cache = { #self.discrete_inputs, #self.coils, #self.input_regs, #self.holding_regs }
|
|
|
|
end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- return IO count
|
2022-05-11 16:03:15 +00:00
|
|
|
---@return integer discrete_inputs, integer coils, integer input_regs, integer holding_regs
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.io_count()
|
2022-05-15 00:07:26 +00:00
|
|
|
return self.io_count_cache[1], self.io_count_cache[2], self.io_count_cache[3], self.io_count_cache[4]
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
2024-03-23 04:26:58 +00:00
|
|
|
-- pass a function through or generate one to call a function by name from the device
|
|
|
|
---@param f function|string function or device function name
|
|
|
|
local function _as_func(f)
|
|
|
|
if type(f) == "string" then
|
|
|
|
local name = f
|
|
|
|
if device then
|
|
|
|
f = function (...) return device[name](...) end
|
|
|
|
else f = stub end
|
|
|
|
end
|
|
|
|
|
|
|
|
return f
|
|
|
|
end
|
|
|
|
|
2022-01-13 15:12:44 +00:00
|
|
|
-- discrete inputs: single bit read-only
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- connect discrete input
|
2024-03-23 04:26:58 +00:00
|
|
|
---@param f function|string function or function name
|
2022-05-11 16:03:15 +00:00
|
|
|
---@return integer count count of discrete inputs
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_di(f)
|
2024-03-23 04:26:58 +00:00
|
|
|
insert(self.discrete_inputs, { read = _as_func(f) })
|
2022-04-18 01:12:25 +00:00
|
|
|
_count_io()
|
2022-01-13 15:12:44 +00:00
|
|
|
return #self.discrete_inputs
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- read discrete input
|
|
|
|
---@param di_addr integer
|
|
|
|
---@return any value, boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.read_di(di_addr)
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.discrete_inputs[di_addr].read()
|
2024-03-09 18:24:06 +00:00
|
|
|
return value, _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- coils: single bit read-write
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- connect coil
|
2024-03-23 04:26:58 +00:00
|
|
|
---@param f_read function|string function or function name
|
|
|
|
---@param f_write function|string function or function name
|
2022-05-11 16:03:15 +00:00
|
|
|
---@return integer count count of coils
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_coil(f_read, f_write)
|
2024-03-23 04:26:58 +00:00
|
|
|
insert(self.coils, { read = _as_func(f_read), write = _as_func(f_write) })
|
2022-04-18 01:12:25 +00:00
|
|
|
_count_io()
|
2022-01-13 15:12:44 +00:00
|
|
|
return #self.coils
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- read coil
|
|
|
|
---@param coil_addr integer
|
|
|
|
---@return any value, boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.read_coil(coil_addr)
|
2022-04-18 01:12:25 +00:00
|
|
|
local value = self.coils[coil_addr].read()
|
2024-03-09 18:24:06 +00:00
|
|
|
return value, _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- write coil
|
|
|
|
---@param coil_addr integer
|
|
|
|
---@param value any
|
|
|
|
---@return boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.write_coil(coil_addr, value)
|
2022-01-13 15:12:44 +00:00
|
|
|
self.coils[coil_addr].write(value)
|
2024-03-09 18:24:06 +00:00
|
|
|
return _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- input registers: multi-bit read-only
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- connect input register
|
2024-03-23 04:26:58 +00:00
|
|
|
---@param f function|string function or function name
|
2022-05-11 16:03:15 +00:00
|
|
|
---@return integer count count of input registers
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_input_reg(f)
|
2024-03-23 04:26:58 +00:00
|
|
|
insert(self.input_regs, { read = _as_func(f) })
|
2022-04-18 01:12:25 +00:00
|
|
|
_count_io()
|
2022-01-13 15:12:44 +00:00
|
|
|
return #self.input_regs
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- read input register
|
|
|
|
---@param reg_addr integer
|
|
|
|
---@return any value, boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.read_input_reg(reg_addr)
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.input_regs[reg_addr].read()
|
2024-03-09 18:24:06 +00:00
|
|
|
return value, _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- holding registers: multi-bit read-write
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- connect holding register
|
2024-03-23 04:26:58 +00:00
|
|
|
---@param f_read function|string function or function name
|
|
|
|
---@param f_write function|string function or function name
|
2022-05-11 16:03:15 +00:00
|
|
|
---@return integer count count of holding registers
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_holding_reg(f_read, f_write)
|
2024-03-23 04:26:58 +00:00
|
|
|
insert(self.holding_regs, { read = _as_func(f_read), write = _as_func(f_write) })
|
2022-04-18 01:12:25 +00:00
|
|
|
_count_io()
|
2022-01-13 15:12:44 +00:00
|
|
|
return #self.holding_regs
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- read holding register
|
|
|
|
---@param reg_addr integer
|
|
|
|
---@return any value, boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.read_holding_reg(reg_addr)
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.holding_regs[reg_addr].read()
|
2024-03-09 18:24:06 +00:00
|
|
|
return value, _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- write holding register
|
|
|
|
---@param reg_addr integer
|
|
|
|
---@param value any
|
|
|
|
---@return boolean access_fault
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.write_holding_reg(reg_addr, value)
|
2022-05-27 22:10:06 +00:00
|
|
|
self.holding_regs[reg_addr].write(value)
|
2024-03-09 18:24:06 +00:00
|
|
|
return _is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
2022-05-12 19:36:27 +00:00
|
|
|
-- public RTU device access
|
|
|
|
|
|
|
|
-- get the public interface to this RTU
|
2023-02-22 04:50:43 +00:00
|
|
|
function protected.interface() return public end
|
2022-05-12 19:36:27 +00:00
|
|
|
|
|
|
|
return protected
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2023-07-27 00:48:44 +00:00
|
|
|
-- create an alarm speaker sounder
|
|
|
|
---@param speaker table device peripheral
|
|
|
|
function rtu.init_sounder(speaker)
|
|
|
|
---@class rtu_speaker_sounder
|
|
|
|
local spkr_ctl = {
|
|
|
|
speaker = speaker,
|
|
|
|
name = ppm.get_iface(speaker),
|
|
|
|
playing = false,
|
|
|
|
stream = audio.new_stream(),
|
|
|
|
play = function () end,
|
|
|
|
stop = function () end,
|
|
|
|
continue = function () end
|
|
|
|
}
|
|
|
|
|
|
|
|
-- continue audio stream if playing
|
|
|
|
function spkr_ctl.continue()
|
|
|
|
if spkr_ctl.playing then
|
|
|
|
if spkr_ctl.speaker ~= nil and spkr_ctl.stream.has_next_block() then
|
2023-11-06 14:25:44 +00:00
|
|
|
local success = spkr_ctl.speaker.playAudio(spkr_ctl.stream.get_next_block(), config.SpeakerVolume)
|
2023-07-27 00:48:44 +00:00
|
|
|
if not success then log.error(util.c("rtu_sounder(", spkr_ctl.name, "): error playing audio")) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- start audio stream playback
|
|
|
|
function spkr_ctl.play()
|
|
|
|
if not spkr_ctl.playing then
|
|
|
|
spkr_ctl.playing = true
|
|
|
|
return spkr_ctl.continue()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stop audio stream playback
|
|
|
|
function spkr_ctl.stop()
|
|
|
|
spkr_ctl.playing = false
|
|
|
|
spkr_ctl.speaker.stop()
|
|
|
|
spkr_ctl.stream.stop()
|
|
|
|
end
|
|
|
|
|
|
|
|
return spkr_ctl
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- RTU Communications
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param version string RTU version
|
2023-06-25 16:59:38 +00:00
|
|
|
---@param nic nic network interface device
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param conn_watchdog watchdog watchdog reference
|
2023-11-06 14:25:44 +00:00
|
|
|
function rtu.comms(version, nic, conn_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
2023-06-05 05:13:22 +00:00
|
|
|
sv_addr = comms.BROADCAST,
|
2024-06-29 02:27:55 +00:00
|
|
|
seq_num = util.time_ms() * 10, -- unique per peer, restarting will not re-use seq nums due to message rate
|
2022-03-25 16:17:46 +00:00
|
|
|
txn_id = 0,
|
2023-02-16 00:59:58 +00:00
|
|
|
last_est_ack = ESTABLISH_ACK.ALLOW
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:31:22 +00:00
|
|
|
local insert = table.insert
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
comms.set_trusted_range(config.TrustedRange)
|
2023-02-07 22:31:22 +00:00
|
|
|
|
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-09-23 00:42:06 +00:00
|
|
|
-- configure modem channels
|
2023-06-25 16:59:38 +00:00
|
|
|
nic.closeAll()
|
2023-11-06 14:25:44 +00:00
|
|
|
nic.open(config.RTU_Channel)
|
2022-09-23 00:42:06 +00:00
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- send a scada management packet
|
2023-08-30 20:45:48 +00:00
|
|
|
---@param msg_type MGMT_TYPE
|
2022-05-11 16:09:04 +00:00
|
|
|
---@param msg table
|
2022-05-31 18:54:55 +00:00
|
|
|
local function _send(msg_type, msg)
|
2022-04-23 00:23:40 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
|
|
|
local m_pkt = comms.mgmt_packet()
|
|
|
|
|
|
|
|
m_pkt.make(msg_type, msg)
|
2023-06-05 05:13:22 +00:00
|
|
|
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.SCADA_MGMT, m_pkt.raw_sendable())
|
2022-04-23 00:23:40 +00:00
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
nic.transmit(config.SVR_Channel, config.RTU_Channel, s_pkt)
|
2022-04-23 00:23:40 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- keep alive ack
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param srv_time integer
|
2022-05-31 18:54:55 +00:00
|
|
|
local function _send_keep_alive_ack(srv_time)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send(MGMT_TYPE.KEEP_ALIVE, { srv_time, util.time() })
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- generate device advertisement table
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2022-11-13 19:13:30 +00:00
|
|
|
---@param units table
|
|
|
|
---@return table advertisement
|
|
|
|
local function _generate_advertisement(units)
|
|
|
|
local advertisement = {}
|
|
|
|
|
|
|
|
for i = 1, #units do
|
2023-02-21 17:27:16 +00:00
|
|
|
local unit = units[i] ---@type rtu_unit_registry_entry
|
2022-11-13 19:13:30 +00:00
|
|
|
|
2023-02-25 04:59:39 +00:00
|
|
|
if unit.type ~= nil then
|
2023-02-22 04:50:43 +00:00
|
|
|
local advert = { unit.type, unit.index, unit.reactor }
|
2022-11-13 19:13:30 +00:00
|
|
|
|
2023-02-25 04:59:39 +00:00
|
|
|
if unit.type == RTU_UNIT_TYPE.REDSTONE then
|
2022-11-13 19:13:30 +00:00
|
|
|
insert(advert, unit.device)
|
|
|
|
end
|
|
|
|
|
|
|
|
insert(advertisement, advert)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return advertisement
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2023-02-22 04:50:43 +00:00
|
|
|
---@class rtu_comms
|
|
|
|
local public = {}
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- send a MODBUS TCP packet
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param m_pkt modbus_packet
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.send_modbus(m_pkt)
|
2022-05-09 19:00:16 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
2023-06-05 05:13:22 +00:00
|
|
|
s_pkt.make(self.sv_addr, self.seq_num, PROTOCOL.MODBUS_TCP, m_pkt.raw_sendable())
|
2023-11-06 14:25:44 +00:00
|
|
|
nic.transmit(config.SVR_Channel, config.RTU_Channel, s_pkt)
|
2022-05-09 19:00:16 +00:00
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- unlink from the server
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param rtu_state rtu_state
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.unlink(rtu_state)
|
2022-05-09 19:00:16 +00:00
|
|
|
rtu_state.linked = false
|
2023-06-05 05:13:22 +00:00
|
|
|
self.sv_addr = comms.BROADCAST
|
2023-06-05 21:47:43 +00:00
|
|
|
databus.tx_link_state(types.PANEL_LINK_STATE.DISCONNECTED)
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection to the server
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param rtu_state rtu_state
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.close(rtu_state)
|
2023-02-22 04:50:43 +00:00
|
|
|
conn_watchdog.cancel()
|
2022-05-11 16:03:15 +00:00
|
|
|
public.unlink(rtu_state)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send(MGMT_TYPE.CLOSE, {})
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- send establish request (includes advertisement)
|
|
|
|
---@param units table
|
|
|
|
function public.send_establish(units)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send(MGMT_TYPE.ESTABLISH, { comms.version, version, DEVICE_TYPE.RTU, _generate_advertisement(units) })
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- send capability advertisement
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param units table
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.send_advertisement(units)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send(MGMT_TYPE.RTU_ADVERT, _generate_advertisement(units))
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
|
2022-11-11 19:59:53 +00:00
|
|
|
-- notify that a peripheral was remounted
|
|
|
|
---@param unit_index integer RTU unit ID
|
|
|
|
function public.send_remounted(unit_index)
|
2023-08-30 20:45:48 +00:00
|
|
|
_send(MGMT_TYPE.RTU_DEV_REMOUNT, { unit_index })
|
2022-11-11 19:59:53 +00:00
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- parse a MODBUS/SCADA packet
|
2023-02-22 04:50:43 +00:00
|
|
|
---@nodiscard
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param side string
|
|
|
|
---@param sender integer
|
|
|
|
---@param reply_to integer
|
|
|
|
---@param message any
|
|
|
|
---@param distance integer
|
|
|
|
---@return modbus_frame|mgmt_frame|nil packet
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.parse_packet(side, sender, reply_to, message, distance)
|
2023-06-25 16:59:38 +00:00
|
|
|
local s_pkt = nic.receive(side, sender, reply_to, message, distance)
|
2022-03-25 16:17:46 +00:00
|
|
|
local pkt = nil
|
|
|
|
|
2023-06-25 16:59:38 +00:00
|
|
|
if s_pkt then
|
2022-03-25 16:17:46 +00:00
|
|
|
-- get as MODBUS TCP packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if s_pkt.protocol() == PROTOCOL.MODBUS_TCP then
|
2022-04-21 14:26:02 +00:00
|
|
|
local m_pkt = comms.modbus_packet()
|
2022-04-02 12:28:43 +00:00
|
|
|
if m_pkt.decode(s_pkt) then
|
|
|
|
pkt = m_pkt.get()
|
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
-- get as SCADA management packet
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif s_pkt.protocol() == PROTOCOL.SCADA_MGMT then
|
2022-04-18 13:35:08 +00:00
|
|
|
local mgmt_pkt = comms.mgmt_packet()
|
2022-04-02 12:28:43 +00:00
|
|
|
if mgmt_pkt.decode(s_pkt) then
|
2022-05-02 21:34:57 +00:00
|
|
|
pkt = mgmt_pkt.get()
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
else
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug("illegal packet type " .. s_pkt.protocol(), true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return pkt
|
|
|
|
end
|
|
|
|
|
2022-04-23 00:23:40 +00:00
|
|
|
-- handle a MODBUS/SCADA packet
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param packet modbus_frame|mgmt_frame
|
2023-02-22 04:50:43 +00:00
|
|
|
---@param units table RTU units
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param rtu_state rtu_state
|
2023-07-27 00:48:44 +00:00
|
|
|
---@param sounders table speaker alarm sounders
|
|
|
|
function public.handle_packet(packet, units, rtu_state, sounders)
|
2023-04-21 00:40:28 +00:00
|
|
|
-- print a log message to the terminal as long as the UI isn't running
|
|
|
|
local function println_ts(message) if not rtu_state.fp_ok then util.println_ts(message) end end
|
|
|
|
|
2023-06-05 23:12:43 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
|
|
|
local l_chan = packet.scada_frame.local_channel()
|
|
|
|
local src_addr = packet.scada_frame.src_addr()
|
|
|
|
|
2023-11-06 14:25:44 +00:00
|
|
|
if l_chan == config.RTU_Channel then
|
2022-04-29 14:19:05 +00:00
|
|
|
-- check sequence number
|
2024-06-29 02:27:55 +00:00
|
|
|
if self.seq_num ~= packet.scada_frame.seq_num() then
|
|
|
|
log.warning("sequence out-of-order: last = " .. self.seq_num .. ", new = " .. packet.scada_frame.seq_num())
|
2022-04-29 14:19:05 +00:00
|
|
|
return
|
2023-06-07 16:48:43 +00:00
|
|
|
elseif rtu_state.linked and (src_addr ~= self.sv_addr) then
|
|
|
|
log.debug("received packet from unknown computer " .. src_addr .. " while linked (expected " .. self.sv_addr ..
|
|
|
|
"); channel in use by another system?")
|
2023-06-05 23:12:43 +00:00
|
|
|
return
|
2022-04-29 14:19:05 +00:00
|
|
|
else
|
2024-06-29 02:27:55 +00:00
|
|
|
self.seq_num = packet.scada_frame.seq_num() + 1
|
2022-04-29 14:19:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- feed watchdog on valid sequence number
|
2023-02-22 04:50:43 +00:00
|
|
|
conn_watchdog.feed()
|
2022-04-29 14:19:05 +00:00
|
|
|
|
2023-06-05 23:12:43 +00:00
|
|
|
-- handle packet
|
2023-02-21 16:05:57 +00:00
|
|
|
if protocol == PROTOCOL.MODBUS_TCP then
|
2023-02-22 04:50:43 +00:00
|
|
|
---@cast packet modbus_frame
|
2023-06-07 16:48:43 +00:00
|
|
|
if rtu_state.linked then
|
2023-04-12 20:02:29 +00:00
|
|
|
local return_code ---@type boolean
|
|
|
|
local reply ---@type modbus_packet
|
2022-04-23 00:23:40 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- handle MODBUS instruction
|
|
|
|
if packet.unit_id <= #units then
|
|
|
|
local unit = units[packet.unit_id] ---@type rtu_unit_registry_entry
|
|
|
|
local unit_dbg_tag = " (unit " .. packet.unit_id .. ")"
|
2022-05-17 14:35:55 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
if unit.name == "redstone_io" then
|
|
|
|
-- immediately execute redstone RTU requests
|
|
|
|
return_code, reply = unit.modbus_io.handle_packet(packet)
|
|
|
|
if not return_code then
|
|
|
|
log.warning("requested MODBUS operation failed" .. unit_dbg_tag)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- check validity then pass off to unit comms thread
|
|
|
|
return_code, reply = unit.modbus_io.check_request(packet)
|
|
|
|
if return_code then
|
|
|
|
-- check if there are more than 3 active transactions
|
|
|
|
-- still queue the packet, but this may indicate a problem
|
|
|
|
if unit.pkt_queue.length() > 3 then
|
|
|
|
reply = modbus.reply__srv_device_busy(packet)
|
|
|
|
log.debug("queueing new request with " .. unit.pkt_queue.length() ..
|
|
|
|
" transactions already in the queue" .. unit_dbg_tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- always queue the command even if busy
|
|
|
|
unit.pkt_queue.push_packet(packet)
|
|
|
|
else
|
|
|
|
log.warning("cannot perform requested MODBUS operation" .. unit_dbg_tag)
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
end
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
-- unit ID out of range?
|
|
|
|
reply = modbus.reply__gw_unavailable(packet)
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug("received MODBUS packet for non-existent unit")
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-11-13 19:13:30 +00:00
|
|
|
|
|
|
|
public.send_modbus(reply)
|
2023-06-07 16:35:17 +00:00
|
|
|
else
|
2023-06-07 16:48:43 +00:00
|
|
|
log.debug("discarding MODBUS packet before linked")
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2023-02-21 16:05:57 +00:00
|
|
|
elseif protocol == PROTOCOL.SCADA_MGMT then
|
2023-02-22 04:50:43 +00:00
|
|
|
---@cast packet mgmt_frame
|
2022-03-25 16:17:46 +00:00
|
|
|
-- SCADA management packet
|
2023-07-29 21:57:51 +00:00
|
|
|
if rtu_state.linked then
|
2023-08-30 20:45:48 +00:00
|
|
|
if packet.type == MGMT_TYPE.KEEP_ALIVE then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- keep alive request received, echo back
|
|
|
|
if packet.length == 1 and type(packet.data[1]) == "number" then
|
|
|
|
local timestamp = packet.data[1]
|
|
|
|
local trip_time = util.time() - timestamp
|
|
|
|
|
2023-02-26 19:49:16 +00:00
|
|
|
if trip_time > 750 then
|
|
|
|
log.warning("RTU KEEP_ALIVE trip time > 750ms (" .. trip_time .. "ms)")
|
2022-11-13 19:13:30 +00:00
|
|
|
end
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- log.debug("RTU RTT = " .. trip_time .. "ms")
|
2022-05-09 19:00:16 +00:00
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
_send_keep_alive_ack(timestamp)
|
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT keep alive packet length/type mismatch")
|
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.CLOSE then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- close connection
|
2023-02-22 04:50:43 +00:00
|
|
|
conn_watchdog.cancel()
|
2022-11-13 19:13:30 +00:00
|
|
|
public.unlink(rtu_state)
|
|
|
|
println_ts("server connection closed by remote host")
|
|
|
|
log.warning("server connection closed by remote host")
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.RTU_ADVERT then
|
2022-11-13 19:13:30 +00:00
|
|
|
-- request for capabilities again
|
2023-02-22 04:50:43 +00:00
|
|
|
public.send_advertisement(units)
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.RTU_TONE_ALARM then
|
2023-07-27 00:48:44 +00:00
|
|
|
-- alarm tone update from supervisor
|
|
|
|
if (packet.length == 1) and type(packet.data[1] == "table") and (#packet.data[1] == 8) then
|
|
|
|
local states = packet.data[1]
|
|
|
|
|
|
|
|
for i = 1, #sounders do
|
|
|
|
local s = sounders[i] ---@type rtu_speaker_sounder
|
|
|
|
|
|
|
|
-- set tone states
|
2023-07-30 04:13:26 +00:00
|
|
|
for id = 1, #states do s.stream.set_active(id, states[id] == true) end
|
2023-07-27 00:48:44 +00:00
|
|
|
end
|
|
|
|
end
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
-- not supported
|
2023-04-19 13:30:17 +00:00
|
|
|
log.debug("received unsupported SCADA_MGMT message type " .. packet.type)
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
2023-08-30 20:45:48 +00:00
|
|
|
elseif packet.type == MGMT_TYPE.ESTABLISH then
|
2023-07-29 21:57:51 +00:00
|
|
|
if packet.length == 1 then
|
|
|
|
local est_ack = packet.data[1]
|
|
|
|
|
|
|
|
if est_ack == ESTABLISH_ACK.ALLOW then
|
|
|
|
-- establish allowed
|
|
|
|
rtu_state.linked = true
|
|
|
|
self.sv_addr = packet.scada_frame.src_addr()
|
|
|
|
println_ts("supervisor connection established")
|
|
|
|
log.info("supervisor connection established")
|
|
|
|
else
|
|
|
|
-- establish denied
|
|
|
|
if est_ack ~= self.last_est_ack then
|
|
|
|
if est_ack == ESTABLISH_ACK.BAD_VERSION then
|
|
|
|
-- version mismatch
|
|
|
|
println_ts("supervisor comms version mismatch (try updating), retrying...")
|
|
|
|
log.warning("supervisor connection denied due to comms version mismatch, retrying")
|
|
|
|
else
|
|
|
|
println_ts("supervisor connection denied, retrying...")
|
|
|
|
log.warning("supervisor connection denied, retrying")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
public.unlink(rtu_state)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.last_est_ack = est_ack
|
|
|
|
|
|
|
|
-- report link state
|
|
|
|
databus.tx_link_state(est_ack + 1)
|
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT establish packet length mismatch")
|
|
|
|
end
|
2023-06-07 16:35:17 +00:00
|
|
|
else
|
2023-06-07 16:48:43 +00:00
|
|
|
log.debug("discarding non-link SCADA_MGMT packet before linked")
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- should be unreachable assuming packet is from parse_packet()
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("illegal packet type " .. protocol, true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2023-06-05 23:12:43 +00:00
|
|
|
else
|
|
|
|
log.debug("received packet on unconfigured channel " .. l_chan, true)
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
return public
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return rtu
|