diff --git a/supervisor/session/rtu.lua b/supervisor/session/rtu.lua index 9468255..ca4e467 100644 --- a/supervisor/session/rtu.lua +++ b/supervisor/session/rtu.lua @@ -102,8 +102,29 @@ function rtu.new_session(id, in_queue, out_queue, advertisement) local u_type = unit_advert.type - -- create unit by type + -- validate unit advertisement + + local advert_validator = util.new_validator() + advert_validator.assert_type_int(unit_advert.index) + advert_validator.assert_type_int(unit_advert.reactor) + if u_type == RTU_UNIT_TYPES.REDSTONE then + advert_validator.assert_type_table(unit_advert.rsio) + end + + if advert_validator.valid() then + advert_validator.assert_min(unit_advert.index, 1) + advert_validator.assert_min(unit_advert.reactor, 1) + if not advert_validator.valid() then u_type = false end + else + u_type = false + end + + -- create unit by type + + if u_type == false then + -- validation fail + elseif u_type == RTU_UNIT_TYPES.REDSTONE then -- redstone unit, rs_in_q = svrs_redstone.new(self.id, i, unit_advert, self.out_q) elseif u_type == RTU_UNIT_TYPES.BOILER then diff --git a/supervisor/session/rtu/redstone.lua b/supervisor/session/rtu/redstone.lua index 6e6f7b5..2447094 100644 --- a/supervisor/session/rtu/redstone.lua +++ b/supervisor/session/rtu/redstone.lua @@ -90,25 +90,31 @@ function redstone.new(session_id, unit_id, advert, out_queue) -- setup I/O for i = 1, #advert.rsio do local channel = advert.rsio[i] - local mode = rsio.get_io_mode(channel) - if mode == IO_MODE.DIGITAL_IN then - self.has_di = true - table.insert(self.io_list.digital_in, channel) - elseif mode == IO_MODE.DIGITAL_OUT then - table.insert(self.io_list.digital_out, channel) - elseif mode == IO_MODE.ANALOG_IN then - self.has_ai = true - table.insert(self.io_list.analog_in, channel) - elseif mode == IO_MODE.ANALOG_OUT then - table.insert(self.io_list.analog_out, channel) + if rsio.is_valid_channel(channel) then + local mode = rsio.get_io_mode(channel) + + if mode == IO_MODE.DIGITAL_IN then + self.has_di = true + table.insert(self.io_list.digital_in, channel) + elseif mode == IO_MODE.DIGITAL_OUT then + table.insert(self.io_list.digital_out, channel) + elseif mode == IO_MODE.ANALOG_IN then + self.has_ai = true + table.insert(self.io_list.analog_in, channel) + elseif mode == IO_MODE.ANALOG_OUT then + table.insert(self.io_list.analog_out, channel) + else + -- should be unreachable code, we already validated channels + log.error(util.c(log_tag, "failed to identify advertisement channel IO mode (", channel, ")"), true) + return nil + end + + self.db[channel] = IO_LVL.LOW else - -- should be unreachable code, we already validated channels - log.error(util.c(log_tag, "failed to identify advertisement channel IO mode (", channel, ")"), true) + log.error(util.c(log_tag, "invalid advertisement channel (", channel, ")"), true) return nil end - - self.db[channel] = IO_LVL.LOW end -- PRIVATE FUNCTIONS -- diff --git a/supervisor/startup.lua b/supervisor/startup.lua index 46ad6b4..54f4f54 100644 --- a/supervisor/startup.lua +++ b/supervisor/startup.lua @@ -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.13" +local SUPERVISOR_VERSION = "beta-v0.4.14" local print = util.print local println = util.println