luci-app-qmodem: refactor modem_config.lua for better maintainability

This commit is contained in:
fujr 2025-04-05 14:37:40 +08:00
parent 73b61f967a
commit 70506f3c45
2 changed files with 89 additions and 73 deletions

View File

@ -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"
}
}

View File

@ -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 = 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", "") s = m:section(NamedSection, arg[1], "modem-device", "")
local slot_name = arg[1] 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 = 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" is_fixed_device.default = "0"
-- Slot Path
path = s:option(ListValue, "slot", translate("Slot Path")) path = s:option(ListValue, "slot", translate("Slot Path"))
local usb_match_slot = {} for _, v in ipairs(usb_slot_list) do
for i,v in ipairs(usb_slot_list) do local uci_name = v:gsub("[%.:%-]", "_")
local uci_name = v:gsub("%.", "_"):gsub(":", "_"):gsub("-", "_")
if uci_name == slot_name then if uci_name == slot_name then
usb_match_slot[uci_name] = v
path:value("/sys/bus/usb/devices/" .. v .. "/", v .. "[usb]") path:value("/sys/bus/usb/devices/" .. v .. "/", v .. "[usb]")
end end
end end
for _, v in ipairs(pcie_slot_list) do
local pcie_match_slot = {} local uci_name = v:gsub("[%.:%-]", "_")
for i,v in ipairs(pcie_slot_list) do
local uci_name = v:gsub("%.", "_"):gsub(":", "_"):gsub("-", "_")
if uci_name == slot_name then 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/pci/devices/" .. v .. "/", v .. "[pcie]")
end end
end end
-- Interface Type
data_interface = s:option(ListValue, "data_interface", translate("Interface Type")) data_interface = s:option(ListValue, "data_interface", translate("Interface Type"))
data_interface:value("usb", translate("USB")) data_interface:value("usb", translate("USB"))
data_interface:value("pcie", translate("PCIe")) data_interface:value("pcie", translate("PCIe"))
-- Alias
alias = s:option(Value, "alias", translate("Alias")) alias = s:option(Value, "alias", translate("Alias"))
alias.description = translate("Alias for the modem, used for identification.") alias.description = translate("Alias for the modem, used for identification.")
alias.rmempty = true alias.rmempty = true
alias.default = "" alias.default = ""
alias.placeholder = translate("Enter alias name") alias.placeholder = translate("Enter alias name")
-- Modem Model
name = s:option(Value, "name", translate("Modem Model")) name = s:option(Value, "name", translate("Modem Model"))
name.cfgvalue = function(t, n) name.cfgvalue = function(t, n)
local name = (Value.cfgvalue(t, n) or "-") return Value.cfgvalue(t, n) or "-"
return name
end end
-- PDP Context Index
define_connect = s:option(Value, "define_connect", translate("PDP Context Index")) define_connect = s:option(Value, "define_connect", translate("PDP Context Index"))
define_connect.default = "1" define_connect.default = "1"
-- Manufacturer (Loaded from modem_cfg.lua)
manufacturer = s:option(ListValue, "manufacturer", translate("Manufacturer")) manufacturer = s:option(ListValue, "manufacturer", translate("Manufacturer"))
manufacturer:value("quectel", "Quectel") populate_options(manufacturer, modem_cfg.manufacturers)
manufacturer:value("simcom", "Simcom")
manufacturer:value("sierra", "Sierra Wireless")
manufacturer:value("fibocom", "Fibocom")
platform = s:option(Value, "platform", translate("Platform")) -- Platform (Loaded from modem_cfg.lua)
platform:value("lte", "lte") platform = s:option(ListValue, "platform", translate("Platform"))
platform:value("lte12","lte12") populate_options(platform, modem_cfg.platforms)
platform:value("qualcomm", "qualcomm")
platform:value("mediatek", "mediatek")
platform:value("unisoc", "unisoc")
platform:value("intel", "intel")
-- AT Port
at_port = s:option(Value, "at_port", translate("AT Port")) at_port = s:option(Value, "at_port", translate("AT Port"))
at_port.description = translate("AT command port for modem communication.") 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 = s:option(DynamicList, "modes", translate("Supported Modes"))
modes:value("ecm", "ECM") populate_options(modes, modem_cfg.modes)
modes:value("mbim", "MBIM")
modes:value("qmi", "QMI")
modes:value("ncm", "NCM")
-- Enable Flag
enabled = s:option(Flag, "enabled", translate("Enable")) enabled = s:option(Flag, "enabled", translate("Enable"))
enabled.default = "1" enabled.default = "1"
wcdma_band = s:option(Value, "wcdma_band", translate("WCDMA Band")) -- Band Configurations
wcdma_band.description = translate("WCDMA band configuration, e.g., 1/2/3") local band_options = {
wcdma_band.placeholder = translate("Enter WCDMA band") { 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")) for _, band in ipairs(band_options) do
lte_band.description = translate("LTE band configuration, e.g., 1/2/3") local option = s:option(Value, band.name, translate(band.label))
lte_band.placeholder = translate("Enter LTE band") option.description = translate(band.label .. " configuration, e.g., 1/2/3")
option.placeholder = translate(band.placeholder)
nsa_band = s:option(Value, "nsa_band", translate("NSA Band")) option.cfgvalue = function(t, n)
nsa_band.description = translate("NSA band configuration, e.g., 1/2/3") return Value.cfgvalue(t, n) or "null"
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)
end end
end end
wcdma_band.cfgvalue = f
lte_band.cfgvalue = f
nsa_band.cfgvalue = f
sa_band.cfgvalue = f
return m return m