From d61f6a1e857e0d5b3181cfdf56f2048c7c05c95a Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 15 Feb 2018 14:04:37 +0100 Subject: [PATCH] gluon-core: rename iterate_radios() to foreach_radio(), pass whole radio section Allows to remove some redundant UCI lookups. --- .../upgrade/320-gluon-client-bridge-wireless | 27 +++++------- .../luasrc/lib/gluon/upgrade/200-wireless | 44 ++++++++++--------- .../luasrc/usr/lib/lua/gluon/util.lua | 10 ++--- .../lib/gluon/web/model/admin/privatewifi.lua | 7 +-- 4 files changed, 45 insertions(+), 43 deletions(-) diff --git a/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/320-gluon-client-bridge-wireless b/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/320-gluon-client-bridge-wireless index a0c47760..d9ac83f3 100755 --- a/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/320-gluon-client-bridge-wireless +++ b/package/gluon-client-bridge/luasrc/lib/gluon/upgrade/320-gluon-client-bridge-wireless @@ -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') diff --git a/package/gluon-core/luasrc/lib/gluon/upgrade/200-wireless b/package/gluon-core/luasrc/lib/gluon/upgrade/200-wireless index a0e0ba09..d8b7e1ff 100755 --- a/package/gluon-core/luasrc/lib/gluon/upgrade/200-wireless +++ b/package/gluon-core/luasrc/lib/gluon/upgrade/200-wireless @@ -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 diff --git a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua index a1da1768..1a05a7e3 100644 --- a/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua +++ b/package/gluon-core/luasrc/usr/lib/lua/gluon/util.lua @@ -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) diff --git a/package/gluon-web-private-wifi/luasrc/lib/gluon/web/model/admin/privatewifi.lua b/package/gluon-web-private-wifi/luasrc/lib/gluon/web/model/admin/privatewifi.lua index ed5d55c7..9f25bd12 100644 --- a/package/gluon-web-private-wifi/luasrc/lib/gluon/web/model/admin/privatewifi.lua +++ b/package/gluon-web-private-wifi/luasrc/lib/gluon/web/model/admin/privatewifi.lua @@ -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',