From 2c63f84862403968b114f8d53175ed68178ab03d Mon Sep 17 00:00:00 2001 From: meandthemachine <52816483+meandthemachine@users.noreply.github.com> Date: Mon, 24 Nov 2025 15:51:10 +0100 Subject: [PATCH 1/2] Determine start index of nearby networks more robustly Resolves #2003: Unless we examine its source code, we don't exactly know if `wpa_cli ... list_networks` always assigns network ids in the order the known networks are listed in the configuration file. Therefore, we should not assume it to be this way and instead, we should make the code capable of handling all scenarios (in-synch as well as async) in a robust way. --- src/RaspAP/Networking/Hotspot/WiFiManager.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/RaspAP/Networking/Hotspot/WiFiManager.php b/src/RaspAP/Networking/Hotspot/WiFiManager.php index 76fdd0b0..5a7d8e9e 100644 --- a/src/RaspAP/Networking/Hotspot/WiFiManager.php +++ b/src/RaspAP/Networking/Hotspot/WiFiManager.php @@ -93,13 +93,16 @@ class WiFiManager exec('sed -rn "s/ssid=(.*)\s*$/\1/p" ' . escapeshellarg(RASPI_HOSTAPD_CONFIG), $ap_ssid); $ap_ssid = $ap_ssid[0] ?? ''; - $index = 0; + // determine the next index that follows the indexes of the known networks + $index = -1; if (!empty($networks)) { - $lastnet = end($networks); - if (isset($lastnet['index'])) { - $index = $lastnet['index'] + 1; + foreach ($networks as $network) { + if (isset($network['index']) && ($network['index'] > $index)) { + $index = $network['index']; + } } } + $index++; $current = []; $commitCurrent = function () use (&$current, &$networks, &$index, $ap_ssid) { From 0938c3d9e9cb31a306a83553d43fb0fd018f7e21 Mon Sep 17 00:00:00 2001 From: meandthemachine <52816483+meandthemachine@users.noreply.github.com> Date: Mon, 24 Nov 2025 16:03:34 +0100 Subject: [PATCH 2/2] Remove unused local variable The assigned/incremented value is/was never used anywhere else in the loop. --- src/RaspAP/Networking/Hotspot/WiFiManager.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/RaspAP/Networking/Hotspot/WiFiManager.php b/src/RaspAP/Networking/Hotspot/WiFiManager.php index 5a7d8e9e..8fb580d9 100644 --- a/src/RaspAP/Networking/Hotspot/WiFiManager.php +++ b/src/RaspAP/Networking/Hotspot/WiFiManager.php @@ -22,11 +22,9 @@ class WiFiManager { // find currently configured networks exec(' sudo cat ' . RASPI_WPA_SUPPLICANT_CONFIG, $known_return); - $index = 0; foreach ($known_return as $line) { if (preg_match('/network\s*=/', $line)) { $network = array('visible' => false, 'configured' => true, 'connected' => false, 'index' => null); - ++$index; } elseif (isset($network) && $network !== null) { if (preg_match('/^\s*}\s*$/', $line)) { $networks[$ssid] = $network; @@ -38,8 +36,7 @@ class WiFiManager $ssid = trim($lineArr[1], '"'); $ssid = str_replace('P"','',$ssid); $network['ssid'] = $ssid; - $index = $this->getNetworkIdBySSID($ssid); - $network['index'] = $index; + $network['index'] = $this->getNetworkIdBySSID($ssid); break; case 'psk': $network['passkey'] = trim($lineArr[1]);