gluon-core: rename iterate_radios() to foreach_radio(), pass whole radio section

Allows to remove some redundant UCI lookups.
This commit is contained in:
Matthias Schiffer 2018-02-15 14:04:37 +01:00
parent ffa5bdd716
commit d61f6a1e85
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C
4 changed files with 45 additions and 43 deletions

View File

@ -13,13 +13,18 @@ local function is_disabled(config, name)
return config.disabled(false)
end
local function configure_client(config, radio, index, suffix)
local name = 'client_' .. radio
local disabled = is_disabled(config, name)
util.foreach_radio(uci, function(radio, index, config)
local radio_name = radio['.name']
local name = 'client_' .. radio_name
local suffix = radio_name:match('^radio(%d+)$')
local ap = config.ap
local disabled = is_disabled(ap, name)
uci:delete('wireless', name)
if not config() then
if not ap() then
return
end
@ -29,22 +34,14 @@ local function configure_client(config, radio, index, suffix)
end
uci:section('wireless', 'wifi-iface', name, {
device = radio,
device = radio_name,
network = 'client',
mode = 'ap',
ssid = config.ssid(),
ssid = ap.ssid(),
macaddr = macaddr,
ifname = suffix and 'client' .. suffix,
disabled = disabled or false,
})
end
local function configure_radio(radio, index, config)
local suffix = radio:match('^radio(%d+)$')
configure_client(config.ap, radio, index, suffix)
end
util.iterate_radios(uci, configure_radio)
end)
uci:save('wireless')

View File

@ -14,7 +14,7 @@ end
local function get_channel(radio, config)
local channel
if uci:get_first('gluon-core', 'wireless', 'preserve_channels') then
channel = uci:get('wireless', radio, 'channel')
channel = radio.channel
end
return channel or config.channel()
@ -39,7 +39,8 @@ end
local function configure_ibss(config, radio, index, suffix, disabled)
local name = 'ibss_' .. radio
local radio_name = radio['.name']
local name = 'ibss_' .. radio_name
uci:delete('network', name)
uci:delete('network', name .. '_vlan')
@ -70,7 +71,7 @@ local function configure_ibss(config, radio, index, suffix, disabled)
end
uci:section('wireless', 'wifi-iface', name, {
device = radio,
device = radio_name,
network = name,
mode = 'adhoc',
ssid = config.ssid,
@ -83,7 +84,9 @@ local function configure_ibss(config, radio, index, suffix, disabled)
end
local function configure_mesh(config, radio, index, suffix, disabled)
local name = 'mesh_' .. radio
local radio_name = radio['.name']
local name = 'mesh_' .. radio_name
local macfilter = uci:get('wireless', name, 'macfilter')
local maclist = uci:get('wireless', name, 'maclist')
@ -105,7 +108,7 @@ local function configure_mesh(config, radio, index, suffix, disabled)
})
uci:section('wireless', 'wifi-iface', name, {
device = radio,
device = radio_name,
network = name,
mode = 'mesh',
mesh_id = config.id,
@ -120,7 +123,8 @@ local function configure_mesh(config, radio, index, suffix, disabled)
end
local function fixup_wan(radio, index)
local name = 'wan_' .. radio
local radio_name = radio['.name']
local name = 'wan_' .. radio_name
if not uci:get('wireless', name) then
return
@ -134,31 +138,33 @@ local function fixup_wan(radio, index)
uci:set('wireless', name, 'macaddr', macaddr)
end
local function configure_radio(radio, index, config)
util.foreach_radio(uci, function(radio, index, config)
local radio_name = radio['.name']
if not config() then
uci:set('wireless', radio, 'disabled', true)
uci:set('wireless', radio_name, 'disabled', true)
return
end
local suffix = radio:match('^radio(%d+)$')
local suffix = radio_name:match('^radio(%d+)$')
if not suffix then
return
end
local channel = get_channel(radio, config)
uci:delete('wireless', radio, 'disabled')
uci:delete('wireless', radio_name, 'disabled')
uci:set('wireless', radio, 'channel', channel)
uci:set('wireless', radio, 'htmode', 'HT20')
uci:set('wireless', radio, 'country', site.regdom())
uci:set('wireless', radio_name, 'channel', channel)
uci:set('wireless', radio_name, 'htmode', 'HT20')
uci:set('wireless', radio_name, 'country', site.regdom())
uci:set_list('wireless', radio, 'supported_rates', config.supported_rates())
uci:set_list('wireless', radio, 'basic_rate', config.basic_rate())
uci:set_list('wireless', radio_name, 'supported_rates', config.supported_rates())
uci:set_list('wireless', radio_name, 'basic_rate', config.basic_rate())
local ibss_disabled = is_disabled('ibss_' .. radio)
local mesh_disabled = is_disabled('mesh_' .. radio)
local ibss_disabled = is_disabled('ibss_' .. radio_name)
local mesh_disabled = is_disabled('mesh_' .. radio_name)
configure_ibss(config.ibss(), radio, index, suffix,
first_non_nil(
@ -176,9 +182,7 @@ local function configure_radio(radio, index, config)
)
fixup_wan(radio, index)
end
util.iterate_radios(uci, configure_radio)
end)
if uci:get('system', 'rssid_wlan0') then

View File

@ -181,7 +181,7 @@ function find_phy(config)
end
local function get_addresses(uci, radio)
local phy = find_phy(uci:get_all('wireless', radio))
local phy = find_phy(radio)
if not phy then
return function() end
end
@ -250,15 +250,15 @@ end
-- Iterate over all radios defined in UCI calling
-- f(radio, index, site.wifiX) for each radio found while passing
-- site.wifi24 for 2.4 GHz devices and site.wifi5 for 5 GHz ones.
function iterate_radios(uci, f)
function foreach_radio(uci, f)
local radios = {}
uci:foreach('wireless', 'wifi-device', function(s)
table.insert(radios, s['.name'])
uci:foreach('wireless', 'wifi-device', function(radio)
table.insert(radios, radio)
end)
for index, radio in ipairs(radios) do
local hwmode = uci:get('wireless', radio, 'hwmode')
local hwmode = radio.hwmode
if hwmode == '11g' or hwmode == '11ng' then
f(radio, index, site.wifi24)

View File

@ -27,14 +27,15 @@ key.datatype = "wpakey"
key.default = uci:get('wireless', primary_iface, "key")
function f:write()
util.iterate_radios(uci, function(radio, index)
local name = "wan_" .. radio
util.foreach_radio(uci, function(radio, index)
local radio_name = radio['.name']
local name = "wan_" .. radio_name
if enabled.data then
local macaddr = util.get_wlan_mac(uci, radio, index, 4)
uci:section('wireless', "wifi-iface", name, {
device = radio,
device = radio_name,
network = "wan",
mode = 'ap',
encryption = 'psk2',