2022-09-21 19:53:51 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local ppm = require("scada-common.ppm")
|
|
|
|
local log = require("scada-common.log")
|
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
2022-05-11 15:31:02 +00:00
|
|
|
local modbus = require("rtu.modbus")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local rtu = {}
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-04-18 13:35:08 +00:00
|
|
|
local PROTOCOLS = comms.PROTOCOLS
|
2022-11-13 19:13:30 +00:00
|
|
|
local DEVICE_TYPES = comms.DEVICE_TYPES
|
|
|
|
local ESTABLISH_ACK = comms.ESTABLISH_ACK
|
2022-04-18 13:35:08 +00:00
|
|
|
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
|
2022-05-13 14:27:57 +00:00
|
|
|
local RTU_UNIT_TYPES = comms.RTU_UNIT_TYPES
|
2022-04-18 13:35:08 +00:00
|
|
|
|
2022-05-10 16:01:56 +00:00
|
|
|
local print = util.print
|
|
|
|
local println = util.println
|
|
|
|
local print_ts = util.print_ts
|
|
|
|
local println_ts = util.println_ts
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- create a new RTU
|
2022-05-31 18:54:55 +00:00
|
|
|
function rtu.init_unit()
|
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
|
|
|
|
|
|
|
|
---@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
|
|
|
|
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
|
|
|
|
|
|
|
|
-- discrete inputs: single bit read-only
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- connect discrete input
|
|
|
|
---@param f function
|
|
|
|
---@return integer count count of discrete inputs
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_di(f)
|
2022-05-27 22:10:06 +00:00
|
|
|
insert(self.discrete_inputs, { read = 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-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.discrete_inputs[di_addr].read()
|
2022-04-18 01:12:25 +00:00
|
|
|
return value, ppm.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
|
|
|
|
---@param f_read function
|
|
|
|
---@param f_write function
|
|
|
|
---@return integer count count of coils
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_coil(f_read, f_write)
|
2022-05-07 17:39:12 +00:00
|
|
|
insert(self.coils, { read = f_read, write = 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
|
|
|
ppm.clear_fault()
|
|
|
|
local value = self.coils[coil_addr].read()
|
|
|
|
return value, ppm.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-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-01-13 15:12:44 +00:00
|
|
|
self.coils[coil_addr].write(value)
|
2022-04-18 01:12:25 +00:00
|
|
|
return ppm.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
|
|
|
|
---@param f function
|
|
|
|
---@return integer count count of input registers
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_input_reg(f)
|
2022-05-27 22:10:06 +00:00
|
|
|
insert(self.input_regs, { read = 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-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.input_regs[reg_addr].read()
|
2022-04-18 01:12:25 +00:00
|
|
|
return value, ppm.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
|
|
|
|
---@param f_read function
|
|
|
|
---@param f_write function
|
|
|
|
---@return integer count count of holding registers
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.connect_holding_reg(f_read, f_write)
|
2022-05-07 17:39:12 +00:00
|
|
|
insert(self.holding_regs, { read = f_read, write = 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-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-05-27 22:10:06 +00:00
|
|
|
local value = self.holding_regs[reg_addr].read()
|
2022-04-18 01:12:25 +00:00
|
|
|
return value, ppm.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-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-05-27 22:10:06 +00:00
|
|
|
self.holding_regs[reg_addr].write(value)
|
2022-04-18 01:12:25 +00:00
|
|
|
return ppm.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
|
2022-05-31 18:54:55 +00:00
|
|
|
function protected.interface()
|
2022-05-12 19:36:27 +00:00
|
|
|
return public
|
|
|
|
end
|
|
|
|
|
|
|
|
return protected
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- RTU Communications
|
2023-02-07 22:31:22 +00:00
|
|
|
---@param version string RTU version
|
|
|
|
---@param modem table modem device
|
|
|
|
---@param local_port integer local listening port
|
|
|
|
---@param server_port integer remote server port
|
|
|
|
---@param range integer trusted device connection range
|
|
|
|
---@param conn_watchdog watchdog watchdog reference
|
|
|
|
function rtu.comms(version, modem, local_port, server_port, range, conn_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
2022-05-19 14:21:04 +00:00
|
|
|
version = version,
|
2022-03-25 16:17:46 +00:00
|
|
|
seq_num = 0,
|
2022-04-29 14:19:05 +00:00
|
|
|
r_seq_num = nil,
|
2022-03-25 16:17:46 +00:00
|
|
|
txn_id = 0,
|
|
|
|
modem = modem,
|
|
|
|
s_port = server_port,
|
2022-05-09 19:00:16 +00:00
|
|
|
l_port = local_port,
|
2023-02-16 00:59:58 +00:00
|
|
|
conn_watchdog = conn_watchdog,
|
|
|
|
last_est_ack = ESTABLISH_ACK.ALLOW
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 22:31:22 +00:00
|
|
|
---@class rtu_comms
|
|
|
|
local public = {}
|
|
|
|
|
|
|
|
local insert = table.insert
|
|
|
|
|
|
|
|
comms.set_trusted_range(range)
|
|
|
|
|
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-09-23 00:42:06 +00:00
|
|
|
-- configure modem channels
|
|
|
|
local function _conf_channels()
|
|
|
|
self.modem.closeAll()
|
|
|
|
self.modem.open(self.l_port)
|
|
|
|
end
|
|
|
|
|
|
|
|
_conf_channels()
|
|
|
|
|
2022-05-11 16:03:15 +00:00
|
|
|
-- send a scada management packet
|
|
|
|
---@param msg_type SCADA_MGMT_TYPES
|
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)
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.SCADA_MGMT, m_pkt.raw_sendable())
|
|
|
|
|
|
|
|
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
|
|
|
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)
|
2022-05-09 19:00:16 +00:00
|
|
|
_send(SCADA_MGMT_TYPES.KEEP_ALIVE, { srv_time, util.time() })
|
|
|
|
end
|
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- generate device advertisement table
|
|
|
|
---@param units table
|
|
|
|
---@return table advertisement
|
|
|
|
local function _generate_advertisement(units)
|
|
|
|
local advertisement = {}
|
|
|
|
|
|
|
|
for i = 1, #units do
|
|
|
|
local unit = units[i] --@type rtu_unit_registry_entry
|
|
|
|
local type = comms.rtu_t_to_unit_type(unit.type)
|
|
|
|
|
|
|
|
if type ~= nil then
|
|
|
|
local advert = {
|
|
|
|
type,
|
|
|
|
unit.index,
|
|
|
|
unit.reactor
|
|
|
|
}
|
|
|
|
|
|
|
|
if type == RTU_UNIT_TYPES.REDSTONE then
|
|
|
|
insert(advert, unit.device)
|
|
|
|
end
|
|
|
|
|
|
|
|
insert(advertisement, advert)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return advertisement
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
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()
|
|
|
|
s_pkt.make(self.seq_num, PROTOCOLS.MODBUS_TCP, m_pkt.raw_sendable())
|
|
|
|
self.modem.transmit(self.s_port, self.l_port, s_pkt.raw_sendable())
|
|
|
|
self.seq_num = self.seq_num + 1
|
|
|
|
end
|
|
|
|
|
2022-04-29 13:27:05 +00:00
|
|
|
-- reconnect a newly connected modem
|
2022-05-11 16:03:15 +00:00
|
|
|
---@param modem table
|
|
|
|
---@diagnostic disable-next-line: redefined-local
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.reconnect_modem(modem)
|
2022-04-29 13:27:05 +00:00
|
|
|
self.modem = modem
|
2022-09-23 00:42:06 +00:00
|
|
|
_conf_channels()
|
2022-04-29 13:27:05 +00:00
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- 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
|
|
|
|
self.r_seq_num = nil
|
|
|
|
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)
|
2022-05-09 19:00:16 +00:00
|
|
|
self.conn_watchdog.cancel()
|
2022-05-11 16:03:15 +00:00
|
|
|
public.unlink(rtu_state)
|
2022-05-09 19:00:16 +00:00
|
|
|
_send(SCADA_MGMT_TYPES.CLOSE, {})
|
|
|
|
end
|
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
-- send establish request (includes advertisement)
|
|
|
|
---@param units table
|
|
|
|
function public.send_establish(units)
|
|
|
|
_send(SCADA_MGMT_TYPES.ESTABLISH, { comms.version, self.version, DEVICE_TYPES.RTU, _generate_advertisement(units) })
|
|
|
|
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)
|
2022-11-13 19:13:30 +00:00
|
|
|
_send(SCADA_MGMT_TYPES.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)
|
|
|
|
_send(SCADA_MGMT_TYPES.RTU_DEV_REMOUNT, { unit_index })
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- parse a MODBUS/SCADA packet
|
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)
|
2022-03-25 16:17:46 +00:00
|
|
|
local pkt = nil
|
2022-04-18 13:35:08 +00:00
|
|
|
local s_pkt = comms.scada_packet()
|
2022-03-25 16:17:46 +00:00
|
|
|
|
|
|
|
-- parse packet as generic SCADA packet
|
2022-04-25 19:49:04 +00:00
|
|
|
s_pkt.receive(side, sender, reply_to, message, distance)
|
2022-03-25 16:17:46 +00:00
|
|
|
|
|
|
|
if s_pkt.is_valid() then
|
|
|
|
-- get as MODBUS TCP packet
|
|
|
|
if s_pkt.protocol() == PROTOCOLS.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
|
|
|
|
elseif s_pkt.protocol() == PROTOCOLS.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
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("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
|
|
|
|
---@param units table
|
|
|
|
---@param rtu_state rtu_state
|
2022-05-31 18:54:55 +00:00
|
|
|
function public.handle_packet(packet, units, rtu_state)
|
2022-09-23 00:42:06 +00:00
|
|
|
if packet ~= nil and packet.scada_frame.local_port() == self.l_port then
|
2022-04-29 14:19:05 +00:00
|
|
|
-- check sequence number
|
|
|
|
if self.r_seq_num == nil then
|
|
|
|
self.r_seq_num = packet.scada_frame.seq_num()
|
|
|
|
elseif rtu_state.linked and self.r_seq_num >= packet.scada_frame.seq_num() then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("sequence out-of-order: last = " .. self.r_seq_num .. ", new = " .. packet.scada_frame.seq_num())
|
2022-04-29 14:19:05 +00:00
|
|
|
return
|
|
|
|
else
|
|
|
|
self.r_seq_num = packet.scada_frame.seq_num()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- feed watchdog on valid sequence number
|
2022-05-09 19:00:16 +00:00
|
|
|
self.conn_watchdog.feed()
|
2022-04-29 14:19:05 +00:00
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
local protocol = packet.scada_frame.protocol()
|
|
|
|
|
|
|
|
if protocol == PROTOCOLS.MODBUS_TCP then
|
2022-11-13 19:13:30 +00:00
|
|
|
if rtu_state.linked then
|
|
|
|
local return_code = false
|
2022-09-21 19:53:51 +00:00
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
2022-11-13 19:13:30 +00:00
|
|
|
local reply = modbus.reply__neg_ack(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
|
2022-09-21 19:53:51 +00:00
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
2022-11-13 19:13:30 +00:00
|
|
|
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
|
2022-09-21 19:53:51 +00:00
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
2022-11-13 19:13:30 +00:00
|
|
|
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
|
2022-09-21 19:53:51 +00:00
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
2022-11-13 19:13:30 +00:00
|
|
|
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?
|
|
|
|
---@diagnostic disable-next-line: param-type-mismatch
|
|
|
|
reply = modbus.reply__gw_unavailable(packet)
|
|
|
|
log.error("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)
|
2022-03-25 16:17:46 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
log.debug("discarding MODBUS packet before linked")
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
elseif protocol == PROTOCOLS.SCADA_MGMT then
|
|
|
|
-- SCADA management packet
|
2022-11-13 19:13:30 +00:00
|
|
|
if packet.type == SCADA_MGMT_TYPES.ESTABLISH then
|
2022-05-09 19:00:16 +00:00
|
|
|
if packet.length == 1 then
|
2022-11-13 19:13:30 +00:00
|
|
|
local est_ack = packet.data[1]
|
|
|
|
|
|
|
|
if est_ack == ESTABLISH_ACK.ALLOW then
|
|
|
|
-- establish allowed
|
|
|
|
rtu_state.linked = true
|
|
|
|
self.r_seq_num = nil
|
|
|
|
println_ts("supervisor connection established")
|
|
|
|
log.info("supervisor connection established")
|
|
|
|
else
|
|
|
|
-- establish denied
|
2023-02-16 00:59:58 +00:00
|
|
|
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")
|
|
|
|
else
|
|
|
|
println_ts("supervisor connection denied, retrying...")
|
|
|
|
log.warning("supervisor connection denied")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-13 19:13:30 +00:00
|
|
|
public.unlink(rtu_state)
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
2023-02-16 00:59:58 +00:00
|
|
|
|
|
|
|
self.last_est_ack = est_ack
|
2022-11-13 19:13:30 +00:00
|
|
|
else
|
|
|
|
log.debug("SCADA_MGMT establish packet length mismatch")
|
|
|
|
end
|
|
|
|
elseif rtu_state.linked then
|
|
|
|
if packet.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
|
|
|
-- 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
|
|
|
|
|
|
|
|
if trip_time > 500 then
|
|
|
|
log.warning("RTU KEEP_ALIVE trip time > 500ms (" .. trip_time .. "ms)")
|
|
|
|
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
|
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.CLOSE then
|
|
|
|
-- close connection
|
|
|
|
self.conn_watchdog.cancel()
|
|
|
|
public.unlink(rtu_state)
|
|
|
|
println_ts("server connection closed by remote host")
|
|
|
|
log.warning("server connection closed by remote host")
|
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.RTU_ADVERT then
|
|
|
|
-- request for capabilities again
|
|
|
|
public.send_advertisement(units)
|
2022-05-09 19:00:16 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +00:00
|
|
|
-- not supported
|
|
|
|
log.warning("received unsupported SCADA_MGMT message type " .. packet.type)
|
2022-05-09 19:00:16 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
else
|
2022-11-13 19:13:30 +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
|
|
|
|
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
|