From ed94ee9a92e0b498552f64417e689d526c528ad3 Mon Sep 17 00:00:00 2001 From: Wesley Ray Date: Sat, 7 Jun 2025 07:38:19 -0400 Subject: [PATCH] Modified set_connect_wifi to build all uci configuration actions into a single string that is passed to os.execute() to run in the background. This allows the function to return a JSON response to the caller (based on the validty of the POST data) instead of timing out while the network configuration settles. --- src/skyweave.lua | 141 +++++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 61 deletions(-) diff --git a/src/skyweave.lua b/src/skyweave.lua index e4c3739..5c755a2 100755 --- a/src/skyweave.lua +++ b/src/skyweave.lua @@ -461,82 +461,101 @@ function handlers.get_system_log_parsed(params) -- Renamed to distinguish end function handlers.set_connect_wifi(params) + -- =================================================================== + -- 1. Parameter Validation + -- Validate all incoming parameters from the request payload. + -- =================================================================== local ssid = params.ssid local bssid = params.bssid - local encryption_type = params.encryption -- maps to UCI 'encryption' option + local encryption_type = params.encryption or "none" -- Default to 'none' if nil local key = params.key - local sta_device = params.device or "radio0" -- The physical radio for the STA interface (e.g., radio0) + local sta_device = params.device or "radio0" - local sta_uci_section = "wireless.wwan" -- The UCI section for the Wi-Fi STA interface is typically 'wwan' - local sta_ifname = "vif-sta0" -- The ifname for this STA section, often 'vif-sta0' or derived. + -- Define UCI configuration sections and interface names + local sta_uci_section = "wireless.wwan" + local sta_ifname = "vif-sta0" + -- Validate SSID: must not be nil or an empty string. if not ssid or ssid == "" then - return { errCode = -30, errMsg = "SSID is required for Wi-Fi connection.", configDone = false } + return { errCode = -30, errMsg = "SSID is required.", configDone = false } end - -- 1. Configure the Wireless STA Interface (e.g., wireless.wwan) - uci_run("uci set " .. sta_uci_section .. ".disabled=0") -- Ensure the interface is enabled - uci_run("uci set " .. sta_uci_section .. ".mode=sta") - uci_run("uci set " .. sta_uci_section .. ".ifname=" .. sta_ifname) -- Set kernel interface name - uci_run("uci set " .. sta_uci_section .. ".device='" .. sta_device .. "'") - uci_run("uci set " .. sta_uci_section .. ".network=wan") -- Associate with the 'wan' firewall zone/network - uci_run("uci set " .. sta_uci_section .. ".ssid='" .. ssid .. "'") + -- Validate Encryption Type: must be one of the supported values. + local supported_encryptions = { + ["none"] = true, ["psk"] = true, ["psk2"] = true, + ["wep"] = true, ["wep-open"] = true, ["wep-shared"] = true + } + if not supported_encryptions[encryption_type] then + return { errCode = -31, errMsg = "Unsupported encryption type: " .. tostring(encryption_type), configDone = false } + end + + -- If validation passes, proceed to building the command sequence. + + -- =================================================================== + -- 2. Asynchronous Command Execution + -- The necessary shell commands are built and then executed in the background. + -- =================================================================== + local commands = {} + local function add_cmd(cmd) + table.insert(commands, cmd) + end + + -- A) Configure the Wireless STA Interface (e.g., wireless.wwan) + add_cmd("uci set " .. sta_uci_section .. ".disabled=0") + add_cmd("uci set " .. sta_uci_section .. ".mode=sta") + add_cmd("uci set " .. sta_uci_section .. ".ifname='" .. sta_ifname .. "'") + add_cmd("uci set " .. sta_uci_section .. ".device='" .. sta_device .. "'") + add_cmd("uci set " .. sta_uci_section .. ".network=wan") + add_cmd("uci set " .. sta_uci_section .. ".ssid='" .. ssid .. "'") + if bssid and bssid ~= "" then add_cmd("uci set " .. sta_uci_section .. ".bssid='" .. bssid .. "'") + else add_cmd("uci delete " .. sta_uci_section .. ".bssid") end + + -- B) Configure Wireless Encryption + if encryption_type == "none" then + add_cmd("uci set " .. sta_uci_section .. ".encryption=none") + add_cmd("uci delete " .. sta_uci_section .. ".key") + elseif encryption_type == "psk2" or encryption_type == "psk" then + add_cmd("uci set " .. sta_uci_section .. ".encryption=" .. encryption_type) + add_cmd("uci set " .. sta_uci_section .. ".key='" .. (key or "") .. "'") + else -- WEP variants + local uci_wep_type = (encryption_type == "wep") and "wep-open" or encryption_type + add_cmd("uci set " .. sta_uci_section .. ".encryption=" .. uci_wep_type) + add_cmd("uci set " .. sta_uci_section .. ".key='" .. (key or "") .. "'") + end + + -- C) Configure the Network WAN Interface + add_cmd("uci set network.wan.ifname='" .. sta_ifname .. "'") + add_cmd("uci set network.wan.proto=dhcp") + add_cmd("uci delete network.wan.ipaddr; uci delete network.wan.netmask; uci delete network.wan.gateway; uci delete network.wan.dns") + add_cmd("uci delete network.wan.ip6addr; uci delete network.wan.ip6gw; uci delete network.wan.ip6prefix") + + -- D) Deconflict with LTE Mode (emulates original binary behavior) + add_cmd("rm -f /etc/lte_mode") + + -- E) Commit UCI changes and restart the network + add_cmd("uci commit wireless") + add_cmd("uci commit network") + add_cmd("/etc/init.d/network restartall") + + local command_string = table.concat(commands, "; ") + local background_command = "(sleep 2; " .. command_string .. ") >/dev/null 2>&1 &" + os.execute(background_command) + + -- =================================================================== + -- 3. Immediate HTTP Response (MODIFIED) + -- Conditionally construct the response message to include the BSSID if provided. + -- =================================================================== + local response_msg = "OK (Configuration accepted. Device will restart network services to connect to SSID: " .. ssid if bssid and bssid ~= "" then - uci_run("uci set " .. sta_uci_section .. ".bssid='" .. bssid .. "'") + response_msg = response_msg .. " | BSSID: " .. bssid .. ")" else - uci_run("uci delete " .. sta_uci_section .. ".bssid") -- Remove BSSID if not specified or empty + response_msg = response_msg .. ")" end - -- Handle Encryption Settings - if encryption_type == "none" or encryption_type == "" or encryption_type == nil then - uci_run("uci set " .. sta_uci_section .. ".encryption=none") - uci_run("uci delete " .. sta_uci_section .. ".key") - elseif encryption_type == "psk2" then -- WPA2 PSK - uci_run("uci set " .. sta_uci_section .. ".encryption=psk2") - uci_run("uci set " .. sta_uci_section .. ".key='" .. (key or "") .. "'") - elseif encryption_type == "psk" then -- WPA PSK - uci_run("uci set " .. sta_uci_section .. ".encryption=psk") - uci_run("uci set " .. sta_uci_section .. ".key='" .. (key or "") .. "'") - -- Add other encryption types as needed, following the pattern above. - -- The C code analysis did not show enterprise (WPA2/WPA) logic, so those have been omitted for clarity. - elseif encryption_type == "wep-open" or encryption_type == "wep-shared" or encryption_type == "wep" then - uci_run("uci set " .. sta_uci_section .. ".encryption=" .. (encryption_type == "wep" and "wep-open" or encryption_type)) -- Default WEP to wep-open - uci_run("uci set " .. sta_uci_section .. ".key='" .. (key or "") .. "'") - else - return { errCode = -31, errMsg = "Unsupported encryption type: " .. tostring(encryption_type) } - end - - -- 2. Configure the Network WAN Interface - uci_run("uci set network.wan.ifname='" .. sta_ifname .. "'") -- Ensure WAN uses the Wi-Fi STA interface - uci_run("uci set network.wan.proto=dhcp") -- Set to DHCP client mode - -- Clean up any potentially conflicting static IP settings on the WAN interface - uci_run("uci delete network.wan.ipaddr") - uci_run("uci delete network.wan.netmask") - uci_run("uci delete network.wan.gateway") - uci_run("uci delete network.wan.dns") - uci_run("uci delete network.wan.ip6addr") -- Also clear potential static IPv6 - uci_run("uci delete network.wan.ip6gw") - uci_run("uci delete network.wan.ip6prefix") - - -- *** REVISED/ADDED SECTION *** - -- 3. Deconflict with LTE Mode (emulates C binary behavior) - os.execute("rm -f /etc/lte_mode") -- - - -- 4. Commit UCI Changes - uci_run("uci commit wireless") - uci_run("uci commit network") - - -- 5. Apply Changes by Performing a Full Network Restart (emulates C binary's use of apply.sh) - -- This is the most critical change. A full restart is more robust than separate reloads. - uci_run("/etc/init.d/network restartall") - - -- *** END REVISED SECTION *** - - -- 6. Return success return { errCode = 0, - errMsg = "OK (Wi-Fi connection process initiated with full network restart for SSID: " .. ssid .. ")" + errMsg = response_msg } end