From 70506f3c4559be2e7aede74125770b8032b43710 Mon Sep 17 00:00:00 2001 From: fujr Date: Sat, 5 Apr 2025 14:37:40 +0800 Subject: [PATCH] luci-app-qmodem: refactor modem_config.lua for better maintainability --- .../luasrc/model/cbi/qmodem/modem_cfg.lua | 23 +++ .../luasrc/model/cbi/qmodem/modem_config.lua | 139 +++++++++--------- 2 files changed, 89 insertions(+), 73 deletions(-) create mode 100644 luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_cfg.lua diff --git a/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_cfg.lua b/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_cfg.lua new file mode 100644 index 0000000..4a9709c --- /dev/null +++ b/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_cfg.lua @@ -0,0 +1,23 @@ +return { + manufacturers = { + quectel = "Quectel", + simcom = "Simcom", + sierra = "Sierra Wireless", + fibocom = "Fibocom" + }, + platforms = { + lte = "LTE", + lte12 = "LTE12", + qualcomm = "Qualcomm", + mediatek = "MediaTek", + unisoc = "Unisoc", + intel = "Intel" + }, + modes = { + ecm = "ECM", + mbim = "MBIM", + qmi = "QMI", + ncm = "NCM", + rndi = "RNDIS" + } +} diff --git a/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_config.lua b/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_config.lua index ffe9f63..41ceffd 100644 --- a/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_config.lua +++ b/luci/luci-app-qmodem/luasrc/model/cbi/qmodem/modem_config.lua @@ -1,120 +1,113 @@ +local modem_cfg = require "luci.model.cbi.qmodem.modem_cfg" + +-- Helper function to load slot paths +local function load_slots(path, exclude_pattern) + local slots = {} + local handle = io.popen("ls " .. path) + for line in handle:lines() do + if not exclude_pattern or not line:match(exclude_pattern) then + table.insert(slots, line) + end + end + handle:close() + return slots +end + +-- Helper function to populate options dynamically from a table +local function populate_options(option, values) + for key, value in pairs(values) do + option:value(key, value) + end +end + +-- Map and Section setup m = Map("qmodem", translate("Modem Configuration")) -m.redirect = luci.dispatcher.build_url("admin", "modem", "qmodem","settings") +m.redirect = luci.dispatcher.build_url("admin", "modem", "qmodem", "settings") s = m:section(NamedSection, arg[1], "modem-device", "") local slot_name = arg[1] -local pcie_slots = io.popen("ls /sys/bus/pci/devices/") -local pcie_slot_list = {} -for line in pcie_slots:lines() do - table.insert(pcie_slot_list, line) -end -pcie_slots:close() -local usb_slots = io.popen("ls /sys/bus/usb/devices/") -local usb_slot_list = {} -for line in usb_slots:lines() do - if not line:match("usb%d+") then - table.insert(usb_slot_list, line) - end -end -usb_slots:close() - +-- Load slot paths +local usb_slot_list = load_slots("/sys/bus/usb/devices/", "usb%d+") +local pcie_slot_list = load_slots("/sys/bus/pci/devices/") +-- Fixed Device Flag is_fixed_device = s:option(Flag, "is_fixed_device", translate("Fixed Device")) -is_fixed_device.description = translate("If the device is fixed, it will not be update when the device is connected or disconnected.") +is_fixed_device.description = translate("If the device is fixed, it will not update when the device is connected or disconnected.") is_fixed_device.default = "0" +-- Slot Path path = s:option(ListValue, "slot", translate("Slot Path")) -local usb_match_slot = {} -for i,v in ipairs(usb_slot_list) do - local uci_name = v:gsub("%.", "_"):gsub(":", "_"):gsub("-", "_") +for _, v in ipairs(usb_slot_list) do + local uci_name = v:gsub("[%.:%-]", "_") if uci_name == slot_name then - usb_match_slot[uci_name] = v - path:value("/sys/bus/usb/devices/"..v.."/",v.."[usb]") - end -end - -local pcie_match_slot = {} -for i,v in ipairs(pcie_slot_list) do - local uci_name = v:gsub("%.", "_"):gsub(":", "_"):gsub("-", "_") - if uci_name == slot_name then - pcie_match_slot[uci_name] = v - path:value("/sys/bus/pci/devices/"..v.."/",v.."[pcie]") + path:value("/sys/bus/usb/devices/" .. v .. "/", v .. "[usb]") + end +end +for _, v in ipairs(pcie_slot_list) do + local uci_name = v:gsub("[%.:%-]", "_") + if uci_name == slot_name then + path:value("/sys/bus/pci/devices/" .. v .. "/", v .. "[pcie]") end end +-- Interface Type data_interface = s:option(ListValue, "data_interface", translate("Interface Type")) data_interface:value("usb", translate("USB")) data_interface:value("pcie", translate("PCIe")) +-- Alias alias = s:option(Value, "alias", translate("Alias")) alias.description = translate("Alias for the modem, used for identification.") alias.rmempty = true alias.default = "" alias.placeholder = translate("Enter alias name") +-- Modem Model name = s:option(Value, "name", translate("Modem Model")) name.cfgvalue = function(t, n) - local name = (Value.cfgvalue(t, n) or "-") - return name + return Value.cfgvalue(t, n) or "-" end +-- PDP Context Index define_connect = s:option(Value, "define_connect", translate("PDP Context Index")) define_connect.default = "1" +-- Manufacturer (Loaded from modem_cfg.lua) manufacturer = s:option(ListValue, "manufacturer", translate("Manufacturer")) -manufacturer:value("quectel", "Quectel") -manufacturer:value("simcom", "Simcom") -manufacturer:value("sierra", "Sierra Wireless") -manufacturer:value("fibocom", "Fibocom") +populate_options(manufacturer, modem_cfg.manufacturers) -platform = s:option(Value, "platform", translate("Platform")) -platform:value("lte", "lte") -platform:value("lte12","lte12") -platform:value("qualcomm", "qualcomm") -platform:value("mediatek", "mediatek") -platform:value("unisoc", "unisoc") -platform:value("intel", "intel") +-- Platform (Loaded from modem_cfg.lua) +platform = s:option(ListValue, "platform", translate("Platform")) +populate_options(platform, modem_cfg.platforms) +-- AT Port at_port = s:option(Value, "at_port", translate("AT Port")) at_port.description = translate("AT command port for modem communication.") +-- Supported Modes (Loaded from modem_cfg.lua) modes = s:option(DynamicList, "modes", translate("Supported Modes")) -modes:value("ecm", "ECM") -modes:value("mbim", "MBIM") -modes:value("qmi", "QMI") -modes:value("ncm", "NCM") +populate_options(modes, modem_cfg.modes) +-- Enable Flag enabled = s:option(Flag, "enabled", translate("Enable")) enabled.default = "1" -wcdma_band = s:option(Value, "wcdma_band", translate("WCDMA Band")) -wcdma_band.description = translate("WCDMA band configuration, e.g., 1/2/3") -wcdma_band.placeholder = translate("Enter WCDMA band") +-- Band Configurations +local band_options = { + { name = "wcdma_band", label = "WCDMA Band", placeholder = "Enter WCDMA band" }, + { name = "lte_band", label = "LTE Band", placeholder = "Enter LTE band" }, + { name = "nsa_band", label = "NSA Band", placeholder = "Enter NSA band" }, + { name = "sa_band", label = "SA Band", placeholder = "Enter SA band" }, +} -lte_band = s:option(Value, "lte_band", translate("LTE Band")) -lte_band.description = translate("LTE band configuration, e.g., 1/2/3") -lte_band.placeholder = translate("Enter LTE band") - -nsa_band = s:option(Value, "nsa_band", translate("NSA Band")) -nsa_band.description = translate("NSA band configuration, e.g., 1/2/3") -nsa_band.placeholder = translate("Enter NSA band") - -sa_band = s:option(Value, "sa_band", translate("SA Band")) -sa_band.description = translate("SA band configuration, e.g., 1/2/3") -sa_band.placeholder = translate("Enter SA band") - -f = function(t, n) - if Value.cfgvalue(t, n) == nil then - return "null" - else - return Value.cfgvalue(t, n) +for _, band in ipairs(band_options) do + local option = s:option(Value, band.name, translate(band.label)) + option.description = translate(band.label .. " configuration, e.g., 1/2/3") + option.placeholder = translate(band.placeholder) + option.cfgvalue = function(t, n) + return Value.cfgvalue(t, n) or "null" end end -wcdma_band.cfgvalue = f -lte_band.cfgvalue = f -nsa_band.cfgvalue = f -sa_band.cfgvalue = f - return m