#71 validate frame data types

This commit is contained in:
Mikayla Fischler
2022-06-05 13:21:02 -04:00
parent f0c97e8b70
commit 81345f5325
4 changed files with 77 additions and 48 deletions

View File

@@ -13,7 +13,7 @@ local config = require("reactor-plc.config")
local plc = require("reactor-plc.plc")
local threads = require("reactor-plc.threads")
local R_PLC_VERSION = "beta-v0.7.5"
local R_PLC_VERSION = "beta-v0.7.6"
local print = util.print
local println = util.println

View File

@@ -25,7 +25,7 @@ local imatrix_rtu = require("rtu.dev.imatrix_rtu")
local turbine_rtu = require("rtu.dev.turbine_rtu")
local turbinev_rtu = require("rtu.dev.turbinev_rtu")
local RTU_VERSION = "beta-v0.7.5"
local RTU_VERSION = "beta-v0.7.6"
local rtu_t = types.rtu_t

View File

@@ -115,12 +115,15 @@ function comms.scada_packet()
if type(self.raw) == "table" then
if #self.raw >= 3 then
self.valid = true
self.seq_num = self.raw[1]
self.protocol = self.raw[2]
self.length = #self.raw[3]
self.payload = self.raw[3]
end
self.valid = type(self.seq_num) == "number" and
type(self.protocol) == "number" and
type(self.payload) == "table"
end
return self.valid
@@ -166,6 +169,7 @@ function comms.modbus_packet()
---@param func_code MODBUS_FCODE
---@param data table
function public.make(txn_id, unit_id, func_code, data)
if type(data) == "table" then
self.txn_id = txn_id
self.length = #data
self.unit_id = unit_id
@@ -177,6 +181,9 @@ function comms.modbus_packet()
for i = 1, self.length do
insert(self.raw, data[i])
end
else
log.error("comms.modbus_packet.make(): data not table")
end
end
-- decode a MODBUS packet from a SCADA frame
@@ -194,7 +201,11 @@ function comms.modbus_packet()
public.make(data[1], data[2], data[3], { table.unpack(data, 4, #data) })
end
return size_ok
local valid = type(self.txn_id) == "number" and
type(self.unit_id) == "number" and
type(self.func_code) == "number"
return size_ok and valid
else
log.debug("attempted MODBUS_TCP parse of incorrect protocol " .. frame.protocol(), true)
return false
@@ -258,6 +269,7 @@ function comms.rplc_packet()
---@param packet_type RPLC_TYPES
---@param data table
function public.make(id, packet_type, data)
if type(data) == "table" then
-- packet accessor properties
self.id = id
self.type = packet_type
@@ -269,6 +281,9 @@ function comms.rplc_packet()
for i = 1, #data do
insert(self.raw, data[i])
end
else
log.error("comms.rplc_packet.make(): data not table")
end
end
-- decode an RPLC packet from a SCADA frame
@@ -287,6 +302,8 @@ function comms.rplc_packet()
ok = _rplc_type_valid()
end
ok = ok and type(self.id) == "number"
return ok
else
log.debug("attempted RPLC parse of incorrect protocol " .. frame.protocol(), true)
@@ -343,6 +360,7 @@ function comms.mgmt_packet()
---@param packet_type SCADA_MGMT_TYPES
---@param data table
function public.make(packet_type, data)
if type(data) == "table" then
-- packet accessor properties
self.type = packet_type
self.length = #data
@@ -353,6 +371,9 @@ function comms.mgmt_packet()
for i = 1, #data do
insert(self.raw, data[i])
end
else
log.error("comms.mgmt_packet.make(): data not table")
end
end
-- decode a SCADA management packet from a SCADA frame
@@ -424,6 +445,7 @@ function comms.coord_packet()
---@param packet_type any
---@param data table
function public.make(packet_type, data)
if type(data) == "table" then
-- packet accessor properties
self.type = packet_type
self.length = #data
@@ -434,6 +456,9 @@ function comms.coord_packet()
for i = 1, #data do
insert(self.raw, data[i])
end
else
log.error("comms.coord_packet.make(): data not table")
end
end
-- decode a coordinator packet from a SCADA frame
@@ -505,6 +530,7 @@ function comms.capi_packet()
---@param packet_type any
---@param data table
function public.make(packet_type, data)
if type(data) == "table" then
-- packet accessor properties
self.type = packet_type
self.length = #data
@@ -515,6 +541,9 @@ function comms.capi_packet()
for i = 1, #data do
insert(self.raw, data[i])
end
else
log.error("comms.capi_packet.make(): data not table")
end
end
-- decode a coordinator API packet from a SCADA frame

View File

@@ -13,7 +13,7 @@ local svsessions = require("supervisor.session.svsessions")
local config = require("supervisor.config")
local supervisor = require("supervisor.supervisor")
local SUPERVISOR_VERSION = "beta-v0.4.11"
local SUPERVISOR_VERSION = "beta-v0.4.12"
local print = util.print
local println = util.println