Procházet zdrojové kódy

Fix nmcli key-mgmt error: use explicit connection profiles

'nmcli dev wifi connect' fails on Pi Trixie with 'key-mgmt: property
is missing' for WPA networks. Switch to explicit profile creation
(nmcli con add + nmcli con up) with wifi-sec.key-mgmt wpa-psk set
explicitly. Applied to both dw CLI and backend Python module.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
tuanchris před 4 měsíci
rodič
revize
10825dc5ee
2 změnil soubory, kde provedl 56 přidání a 8 odebrání
  1. 21 2
      dw
  2. 35 6
      modules/wifi/manager.py

+ 21 - 2
dw

@@ -420,10 +420,29 @@ cmd_wifi() {
             fi
             read -sp "Enter password (leave empty for open networks): " password
             echo ""
+
+            # Delete any stale connection profile for this SSID
+            sudo nmcli con delete "$ssid" 2>/dev/null || true
+
             if [[ -n "$password" ]]; then
-                sudo nmcli dev wifi connect "$ssid" password "$password" ifname wlan0
+                # Create connection with explicit security settings
+                # (nmcli dev wifi connect can fail on Trixie without key-mgmt)
+                sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
+                    ssid "$ssid" \
+                    wifi-sec.key-mgmt wpa-psk \
+                    wifi-sec.psk "$password"
+            else
+                sudo nmcli con add type wifi ifname wlan0 con-name "$ssid" \
+                    ssid "$ssid"
+            fi
+
+            echo -e "${BLUE}Connecting to '$ssid'...${NC}"
+            if sudo nmcli con up "$ssid"; then
+                echo -e "${GREEN}Connected!${NC}"
             else
-                sudo nmcli dev wifi connect "$ssid" ifname wlan0
+                echo -e "${RED}Failed to connect. Check password and try again.${NC}"
+                sudo nmcli con delete "$ssid" 2>/dev/null || true
+                exit 1
             fi
             ;;
         hotspot)

+ 35 - 6
modules/wifi/manager.py

@@ -209,25 +209,54 @@ def get_saved_connections() -> list[dict]:
 
 
 async def connect_to_network(ssid: str, password: str) -> dict:
-    """Connect to a WiFi network and schedule reboot."""
+    """Connect to a WiFi network and schedule reboot.
+
+    Uses explicit connection profile creation (nmcli con add) instead of
+    'nmcli dev wifi connect' because the latter fails on Pi Trixie with
+    'key-mgmt: property is missing' for WPA networks.
+    """
     try:
-        # Try to connect using nmcli
+        # Delete any stale connection profile for this SSID
+        run_host_command("nmcli", "con", "delete", ssid, timeout=10)
+
+        # Create connection profile with explicit security settings
         if password:
             result = run_host_command(
-                "nmcli", "dev", "wifi", "connect", ssid, "password", password,
+                "nmcli", "con", "add",
+                "type", "wifi",
                 "ifname", "wlan0",
-                timeout=30,
+                "con-name", ssid,
+                "ssid", ssid,
+                "wifi-sec.key-mgmt", "wpa-psk",
+                "wifi-sec.psk", password,
+                timeout=15,
             )
         else:
             result = run_host_command(
-                "nmcli", "dev", "wifi", "connect", ssid,
+                "nmcli", "con", "add",
+                "type", "wifi",
                 "ifname", "wlan0",
-                timeout=30,
+                "con-name", ssid,
+                "ssid", ssid,
+                timeout=15,
             )
 
+        if result.returncode != 0:
+            error_msg = result.stderr.strip() or "Failed to create connection"
+            logger.error(f"WiFi connection add failed: {error_msg}")
+            return {"success": False, "message": error_msg}
+
+        # Activate the connection
+        result = run_host_command(
+            "nmcli", "con", "up", ssid,
+            timeout=30,
+        )
+
         if result.returncode != 0:
             error_msg = result.stderr.strip() or "Failed to connect"
             logger.error(f"WiFi connect failed: {error_msg}")
+            # Clean up the failed connection profile
+            run_host_command("nmcli", "con", "delete", ssid, timeout=10)
             return {"success": False, "message": error_msg}
 
         logger.info(f"WiFi connection to '{ssid}' successful, scheduling reboot...")