#580 supervisor wired comms networking logic

This commit is contained in:
Mikayla
2025-06-28 17:17:31 +00:00
parent 4a7fc6200e
commit 391b68d357
9 changed files with 177 additions and 143 deletions

View File

@@ -11,65 +11,95 @@ local databus = require("supervisor.databus")
local pcie_bus = {}
local bus = {
c_wired = false, ---@type string|false wired comms modem
c_nic = nil, ---@type nic core nic
p_nic = nil ---@type nic|nil pocket nic
wired_modem = false, ---@type string|false wired comms modem name
wl_nic = nil, ---@type nic|nil wireless nic
wd_nic = nil ---@type nic|nil wired nic
}
-- network cards
---@class _svr_pcie_nic
---@field core nic the core comms NIC
---@field pocket nic the pocket NIC
---@field wl nic|nil the wireless comms NIC
---@field wd nic|nil the wired comms NIC
pcie_bus.nic = {
-- close all channels then open a specified one on all nics
---@param channel integer
reset_open = function (channel)
bus.c_nic.closeAll()
bus.c_nic.open(channel)
-- close all channels and then open the configured channels on the appropriate nic(s)
---@param config svr_config
reset_open = function (config)
if bus.wl_nic then
bus.wl_nic.closeAll()
if bus.p_nic then
bus.p_nic.closeAll()
bus.p_nic.open(channel)
if config.PLC_Listen % 2 == 0 then bus.wl_nic.open(config.PLC_Channel) end
if config.RTU_Listen % 2 == 0 then bus.wl_nic.open(config.RTU_Channel) end
if config.CRD_Listen % 2 == 0 then bus.wl_nic.open(config.CRD_Channel) end
if config.PocketEnabled then bus.wl_nic.open(config.PKT_Channel) end
end
end
if bus.wd_nic then
bus.wd_nic.closeAll()
if config.PLC_Listen > 0 then bus.wd_nic.open(config.PLC_Channel) end
if config.RTU_Listen > 0 then bus.wd_nic.open(config.RTU_Channel) end
if config.CRD_Listen > 0 then bus.wd_nic.open(config.CRD_Channel) end
end
end,
-- get the requested nic by interface
---@param iface string
---@return nic|nil
get = function(iface)
local dev = ppm.get_device(iface)
if dev then
if bus.wl_nic and bus.wl_nic.is_modem(dev) then return bus.wl_nic end
if bus.wd_nic and bus.wd_nic.is_modem(dev) then return bus.wd_nic end
end
return nil
end,
-- cards by interface
---@type { string: nic }
cards = {}
}
-- initialize peripherals
---@param config svr_config
---@param println function
---@return boolean success
function pcie_bus.init(config, println)
-- setup networking peripheral(s)
local core_modem, core_iface = ppm.get_wireless_modem()
if type(config.WiredModem) == "string" then
bus.c_wired = config.WiredModem
core_modem = ppm.get_wired_modem(config.WiredModem)
end
bus.wired_modem = config.WiredModem
if not (core_modem and core_iface) then
println("startup> core comms modem not found")
log.fatal("no core comms modem on startup")
return
end
local wired_modem = ppm.get_wired_modem(bus.wired_modem)
bus.c_nic = network.nic(core_iface, core_modem)
if config.WirelessModem and config.WiredModem then
local pocket_modem, pocket_iface = ppm.get_wireless_modem()
if not (pocket_modem and pocket_iface) then
println("startup> pocket wireless modem not found")
log.fatal("no pocket wireless modem on startup")
return
if not (wired_modem and bus.wired_modem) then
println("startup> wired comms modem not found")
log.fatal("no wired comms modem on startup")
return false
end
bus.p_nic = network.nic(pocket_iface, pocket_modem)
bus.wd_nic = network.nic(bus.wired_modem, wired_modem)
pcie_bus.nic.cards[bus.wired_modem] = bus.wd_nic
end
pcie_bus.nic.core = bus.c_nic
pcie_bus.nic.pocket = bus.p_nic or bus.c_nic
if config.WirelessModem then
local wireless_modem, wireless_iface = ppm.get_wireless_modem()
databus.tx_hw_c_modem(true)
databus.tx_hw_p_modem(config.WirelessModem)
if not (wireless_modem and wireless_iface) then
println("startup> wireless comms modem not found")
log.fatal("no wireless comms modem on startup")
return false
end
bus.wl_nic = network.nic(wireless_iface, wireless_modem)
pcie_bus.nic.cards[wireless_iface] = bus.wl_nic
end
pcie_bus.nic.wl = bus.wl_nic
pcie_bus.nic.wd = bus.wd_nic
databus.tx_hw_wl_modem(true)
databus.tx_hw_wd_modem(config.WirelessModem)
return true
end
-- handle the connecting of a device
@@ -81,33 +111,27 @@ function pcie_bus.connect(iface, type, device, println)
if type == "modem" then
---@cast device Modem
if device.isWireless() then
if not (bus.c_wired or bus.c_nic.is_connected()) then
-- reconnected comms modem
bus.c_nic.connect(device)
if bus.wl_nic and not bus.wl_nic.is_connected() then
-- reconnected wireless comms modem
bus.wl_nic.connect(device)
pcie_bus.nic.cards[iface] = bus.wl_nic
println("core comms modem reconnected")
log.info("core comms modem reconnected")
println("wireless comms modem reconnected")
log.info("wireless comms modem reconnected")
databus.tx_hw_c_modem(true)
elseif bus.p_nic and not bus.p_nic.is_connected() then
-- reconnected pocket modem
bus.p_nic.connect(device)
println("pocket modem reconnected")
log.info("pocket modem reconnected")
databus.tx_hw_p_modem(true)
databus.tx_hw_wl_modem(true)
else
log.info("unused wireless modem reconnected")
end
elseif iface == bus.c_wired then
elseif bus.wd_nic and (iface == bus.wired_modem) then
-- reconnected wired comms modem
bus.c_nic.connect(device)
bus.wd_nic.connect(device)
pcie_bus.nic.cards[iface] = bus.wd_nic
println("core comms modem reconnected")
log.info("core comms modem reconnected")
println("wired comms modem reconnected")
log.info("wired comms modem reconnected")
databus.tx_hw_c_modem(true)
databus.tx_hw_wl_modem(true)
else
log.info("wired modem reconnected")
end
@@ -115,45 +139,39 @@ function pcie_bus.connect(iface, type, device, println)
end
-- handle the removal of a device
---@param iface string
---@param type string
---@param device table
---@param println function
function pcie_bus.remove(type, device, println)
function pcie_bus.remove(iface, type, device, println)
if type == "modem" then
pcie_bus.nic.cards[iface] = nil
---@cast device Modem
if bus.c_nic.is_modem(device) then
bus.c_nic.disconnect()
if bus.wl_nic and bus.wl_nic.is_modem(device) then
bus.wl_nic.disconnect()
println("core comms modem disconnected")
log.warning("core comms modem disconnected")
local other_modem = ppm.get_wireless_modem()
if other_modem and not bus.c_wired then
log.info("found another wireless modem, using it for comms")
bus.c_nic.connect(other_modem)
else
databus.tx_hw_c_modem(false)
end
elseif bus.p_nic and bus.p_nic.is_modem(device) then
bus.p_nic.disconnect()
println("pocket modem disconnected")
log.warning("pocket modem disconnected")
println("wireless comms modem disconnected")
log.warning("wireless comms modem disconnected")
local other_modem = ppm.get_wireless_modem()
if other_modem then
log.info("found another wireless modem, using it for pocket comms")
bus.p_nic.connect(other_modem)
log.info("found another wireless modem, using it for comms")
bus.wl_nic.connect(other_modem)
else
databus.tx_hw_p_modem(false)
databus.tx_hw_wl_modem(false)
end
elseif bus.wd_nic and bus.wd_nic.is_modem(device) then
bus.wd_nic.disconnect()
println("wired modem disconnected")
log.warning("wired modem disconnected")
databus.tx_hw_wd_modem(false)
else
log.warning("non-comms modem disconnected")
end
end
end
-- check if a dedicated pocket nic is in use
function pcie_bus.has_pocket_nic() return bus.p_nic ~= nil end
return pcie_bus