2022-05-04 17:37:01 +00:00
|
|
|
local comms = require("scada-common.comms")
|
|
|
|
local ppm = require("scada-common.ppm")
|
2022-05-10 16:01:56 +00:00
|
|
|
local log = require("scada-common.log")
|
2022-05-09 13:34:26 +00:00
|
|
|
local types = require("scada-common.types")
|
2022-05-10 16:01:56 +00:00
|
|
|
local util = require("scada-common.util")
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
local modbus = require("modbus")
|
|
|
|
|
|
|
|
local rtu = {}
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-05-09 13:34:26 +00:00
|
|
|
local rtu_t = types.rtu_t
|
|
|
|
|
2022-04-18 13:35:08 +00:00
|
|
|
local PROTOCOLS = comms.PROTOCOLS
|
|
|
|
local SCADA_MGMT_TYPES = comms.SCADA_MGMT_TYPES
|
|
|
|
local RTU_ADVERT_TYPES = comms.RTU_ADVERT_TYPES
|
|
|
|
|
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-04 17:37:01 +00:00
|
|
|
rtu.init_unit = function ()
|
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-07 17:39:12 +00:00
|
|
|
local insert = table.insert
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
local _count_io = function ()
|
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-04-18 01:12:25 +00:00
|
|
|
-- return : IO count table
|
2022-01-14 17:42:11 +00:00
|
|
|
local io_count = function ()
|
|
|
|
return self.io_count_cache[0], self.io_count_cache[1], self.io_count_cache[2], self.io_count_cache[3]
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- discrete inputs: single bit read-only
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- return : count of discrete inputs
|
2022-01-13 15:12:44 +00:00
|
|
|
local connect_di = function (f)
|
2022-05-07 17:39:12 +00:00
|
|
|
insert(self.discrete_inputs, 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-04-18 01:12:25 +00:00
|
|
|
-- return : value, access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local read_di = function (di_addr)
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
|
|
|
local value = self.discrete_inputs[di_addr]()
|
|
|
|
return value, ppm.is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- coils: single bit read-write
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- return : count of coils
|
2022-01-13 15:12:44 +00:00
|
|
|
local connect_coil = function (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-04-18 01:12:25 +00:00
|
|
|
-- return : value, access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local read_coil = function (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-04-18 01:12:25 +00:00
|
|
|
-- return : access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local write_coil = function (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-04-18 01:12:25 +00:00
|
|
|
-- return : count of input registers
|
2022-01-13 15:12:44 +00:00
|
|
|
local connect_input_reg = function (f)
|
2022-05-07 17:39:12 +00:00
|
|
|
insert(self.input_regs, 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-04-18 01:12:25 +00:00
|
|
|
-- return : value, access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local read_input_reg = function (reg_addr)
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
|
|
|
local value = self.coils[reg_addr]()
|
|
|
|
return value, ppm.is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- holding registers: multi-bit read-write
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- return : count of holding registers
|
2022-01-13 15:12:44 +00:00
|
|
|
local connect_holding_reg = function (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-04-18 01:12:25 +00:00
|
|
|
-- return : value, access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local read_holding_reg = function (reg_addr)
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
|
|
|
local value = self.coils[reg_addr].read()
|
|
|
|
return value, ppm.is_faulted()
|
2022-01-13 15:12:44 +00:00
|
|
|
end
|
|
|
|
|
2022-04-18 01:12:25 +00:00
|
|
|
-- return : access fault
|
2022-01-13 15:12:44 +00:00
|
|
|
local write_holding_reg = function (reg_addr, value)
|
2022-04-18 01:12:25 +00:00
|
|
|
ppm.clear_fault()
|
2022-01-13 15:12:44 +00:00
|
|
|
self.coils[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
|
|
|
|
|
|
|
|
return {
|
2022-01-14 17:42:11 +00:00
|
|
|
io_count = io_count,
|
2022-01-13 15:12:44 +00:00
|
|
|
connect_di = connect_di,
|
|
|
|
read_di = read_di,
|
|
|
|
connect_coil = connect_coil,
|
|
|
|
read_coil = read_coil,
|
|
|
|
write_coil = write_coil,
|
|
|
|
connect_input_reg = connect_input_reg,
|
|
|
|
read_input_reg = read_input_reg,
|
|
|
|
connect_holding_reg = connect_holding_reg,
|
|
|
|
read_holding_reg = read_holding_reg,
|
|
|
|
write_holding_reg = write_holding_reg
|
|
|
|
}
|
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
rtu.comms = function (modem, local_port, server_port, conn_watchdog)
|
2022-03-25 16:17:46 +00:00
|
|
|
local self = {
|
|
|
|
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,
|
|
|
|
conn_watchdog = conn_watchdog
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 16:01:56 +00:00
|
|
|
local insert = table.insert
|
|
|
|
|
2022-04-11 15:08:46 +00:00
|
|
|
-- open modem
|
|
|
|
if not self.modem.isOpen(self.l_port) then
|
|
|
|
self.modem.open(self.l_port)
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PRIVATE FUNCTIONS --
|
|
|
|
|
2022-04-23 00:23:40 +00:00
|
|
|
local _send = function (msg_type, msg)
|
|
|
|
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
|
|
|
|
local _send_keep_alive_ack = function (srv_time)
|
|
|
|
_send(SCADA_MGMT_TYPES.KEEP_ALIVE, { srv_time, util.time() })
|
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- PUBLIC FUNCTIONS --
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- send a MODBUS TCP packet
|
|
|
|
local send_modbus = function (m_pkt)
|
|
|
|
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
|
|
|
|
local reconnect_modem = function (modem)
|
|
|
|
self.modem = modem
|
|
|
|
|
|
|
|
-- open modem
|
|
|
|
if not self.modem.isOpen(self.l_port) then
|
|
|
|
self.modem.open(self.l_port)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-09 19:00:16 +00:00
|
|
|
-- unlink from the server
|
|
|
|
local unlink = function (rtu_state)
|
|
|
|
rtu_state.linked = false
|
|
|
|
self.r_seq_num = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
-- close the connection to the server
|
|
|
|
local close = function (rtu_state)
|
|
|
|
self.conn_watchdog.cancel()
|
|
|
|
unlink(rtu_state)
|
|
|
|
_send(SCADA_MGMT_TYPES.CLOSE, {})
|
|
|
|
end
|
|
|
|
|
|
|
|
-- send capability advertisement
|
|
|
|
local send_advertisement = function (units)
|
|
|
|
local advertisement = {}
|
|
|
|
|
|
|
|
for i = 1, #units do
|
|
|
|
local unit = units[i]
|
|
|
|
local type = comms.rtu_t_to_advert_type(unit.type)
|
|
|
|
|
|
|
|
if type ~= nil then
|
|
|
|
if type == RTU_ADVERT_TYPES.REDSTONE then
|
|
|
|
insert(advertisement, {
|
|
|
|
type = type,
|
|
|
|
index = unit.index,
|
|
|
|
reactor = unit.for_reactor,
|
|
|
|
rsio = unit.device
|
|
|
|
})
|
|
|
|
else
|
|
|
|
insert(advertisement, {
|
|
|
|
type = type,
|
|
|
|
index = unit.index,
|
|
|
|
reactor = unit.for_reactor,
|
|
|
|
rsio = nil
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
_send(SCADA_MGMT_TYPES.RTU_ADVERT, advertisement)
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
|
2022-03-25 16:17:46 +00:00
|
|
|
-- parse a MODBUS/SCADA packet
|
|
|
|
local parse_packet = function(side, sender, reply_to, message, distance)
|
|
|
|
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-09 19:00:16 +00:00
|
|
|
local handle_packet = function(packet, units, rtu_state)
|
2022-03-25 16:17:46 +00:00
|
|
|
if packet ~= nil then
|
2022-04-29 14:19:05 +00:00
|
|
|
local seq_ok = true
|
|
|
|
|
|
|
|
-- 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-04-23 00:23:40 +00:00
|
|
|
local reply = modbus.reply__neg_ack(packet)
|
|
|
|
|
2022-04-29 17:19:01 +00:00
|
|
|
-- handle MODBUS instruction
|
2022-04-02 12:28:43 +00:00
|
|
|
if packet.unit_id <= #units then
|
|
|
|
local unit = units[packet.unit_id]
|
2022-04-29 17:19:01 +00:00
|
|
|
if unit.name == "redstone_io" then
|
|
|
|
-- immediately execute redstone RTU requests
|
|
|
|
local return_code, reply = unit.modbus_io.handle_packet(packet)
|
|
|
|
if not return_code then
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("requested MODBUS operation failed")
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- check validity then pass off to unit comms thread
|
|
|
|
local return_code, reply = unit.modbus_io.check_request(packet)
|
|
|
|
if return_code then
|
|
|
|
-- check if an operation is already in progress for this unit
|
|
|
|
if unit.modbus_busy then
|
|
|
|
reply = unit.modbus_io.reply__srv_device_busy(packet)
|
|
|
|
else
|
|
|
|
unit.pkt_queue.push(packet)
|
|
|
|
end
|
|
|
|
else
|
2022-05-04 17:37:01 +00:00
|
|
|
log.warning("cannot perform requested MODBUS operation")
|
2022-04-29 17:19:01 +00:00
|
|
|
end
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
-- unit ID out of range?
|
2022-04-23 00:23:40 +00:00
|
|
|
reply = modbus.reply__gw_unavailable(packet)
|
2022-05-04 17:37:01 +00:00
|
|
|
log.error("MODBUS packet requesting non-existent unit")
|
2022-03-25 16:17:46 +00:00
|
|
|
end
|
2022-04-23 00:23:40 +00:00
|
|
|
|
2022-04-29 17:19:01 +00:00
|
|
|
send_modbus(reply)
|
2022-03-25 16:17:46 +00:00
|
|
|
elseif protocol == PROTOCOLS.SCADA_MGMT then
|
|
|
|
-- SCADA management packet
|
2022-05-09 19:00:16 +00:00
|
|
|
if packet.type == SCADA_MGMT_TYPES.KEEP_ALIVE then
|
|
|
|
-- 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 > 500 then
|
|
|
|
log.warning("RTU KEEP_ALIVE trip time > 500ms (" .. trip_time .. "ms)")
|
|
|
|
end
|
|
|
|
|
|
|
|
-- log.debug("RTU RTT = ".. trip_time .. "ms")
|
|
|
|
|
|
|
|
_send_keep_alive_ack(timestamp)
|
|
|
|
else
|
|
|
|
log.debug("SCADA keep alive packet length mismatch")
|
|
|
|
end
|
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.CLOSE then
|
2022-05-02 17:15:08 +00:00
|
|
|
-- close connection
|
2022-05-09 19:00:16 +00:00
|
|
|
self.conn_watchdog.cancel()
|
2022-05-02 17:15:08 +00:00
|
|
|
unlink(rtu_state)
|
2022-05-09 19:00:16 +00:00
|
|
|
println_ts("server connection closed by remote host")
|
|
|
|
log.warning("server connection closed by remote host")
|
2022-05-04 17:37:01 +00:00
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.REMOTE_LINKED then
|
2022-03-25 16:17:46 +00:00
|
|
|
-- acknowledgement
|
2022-04-27 16:46:04 +00:00
|
|
|
rtu_state.linked = true
|
2022-04-29 14:19:05 +00:00
|
|
|
self.r_seq_num = nil
|
2022-03-25 16:17:46 +00:00
|
|
|
elseif packet.type == SCADA_MGMT_TYPES.RTU_ADVERT then
|
|
|
|
-- request for capabilities again
|
|
|
|
send_advertisement(units)
|
|
|
|
else
|
|
|
|
-- not supported
|
2022-05-10 16:01:56 +00:00
|
|
|
log.warning("RTU got unexpected SCADA message type " .. packet.type)
|
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
|
|
|
|
|
|
|
|
return {
|
2022-04-29 17:19:01 +00:00
|
|
|
send_modbus = send_modbus,
|
2022-04-29 13:27:05 +00:00
|
|
|
reconnect_modem = reconnect_modem,
|
2022-03-25 16:17:46 +00:00
|
|
|
parse_packet = parse_packet,
|
|
|
|
handle_packet = handle_packet,
|
|
|
|
send_advertisement = send_advertisement,
|
2022-05-02 17:15:08 +00:00
|
|
|
unlink = unlink,
|
|
|
|
close = close
|
2022-03-25 16:17:46 +00:00
|
|
|
}
|
|
|
|
end
|
2022-05-04 17:37:01 +00:00
|
|
|
|
|
|
|
return rtu
|