wpa_supplicant: Use log package and tolerate GetNetworks failures

Switch warning prints to the log package for consistent output, and
fall back to AddNetwork when listing existing networks fails instead
of aborting Connect entirely.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
nemunaire 2026-05-01 22:14:38 +08:00
commit 0797f7dd50

View file

@ -2,6 +2,7 @@ package wpasupplicant
import (
"fmt"
"log"
"time"
"github.com/godbus/dbus/v5"
@ -152,10 +153,14 @@ func (b *WPABackend) IsScanning() (bool, error) {
// Connect connects to a WiFi network
func (b *WPABackend) Connect(ssid, password string) error {
// Look up existing network with the same SSID to avoid creating duplicates
// Best-effort lookup of existing networks, used both to avoid creating
// duplicate entries for the same SSID and to re-enable other entries
// after SelectNetwork's "disable all others" side-effect. If listing
// fails, fall back to plain AddNetwork — saving still works.
existingNetworks, err := b.iface.GetNetworks()
if err != nil {
return fmt.Errorf("failed to list configured networks: %v", err)
log.Printf("wpa_supplicant: failed to list configured networks: %v", err)
existingNetworks = nil
}
var networkPath dbus.ObjectPath
@ -196,8 +201,7 @@ func (b *WPABackend) Connect(ssid, password string) error {
// Select (connect to) the network. Note: SelectNetwork disables every
// other configured network as a side-effect.
err = b.iface.SelectNetwork(networkPath)
if err != nil {
if err := b.iface.SelectNetwork(networkPath); err != nil {
if createdNew {
b.iface.RemoveNetwork(networkPath)
}
@ -211,14 +215,17 @@ func (b *WPABackend) Connect(ssid, password string) error {
continue
}
if err := b.iface.EnableNetwork(p); err != nil {
fmt.Printf("Warning: failed to re-enable network %s: %v\n", p, err)
log.Printf("wpa_supplicant: failed to re-enable network %s: %v", p, err)
}
}
// Save the configuration to persist it across reboots
// Save the configuration to persist it across reboots. Requires
// update_config=1 in the wpa_supplicant.conf wpa_supplicant was started
// with, and that file being writable by the wpa_supplicant process.
if err := b.iface.SaveConfig(); err != nil {
// Log warning but don't fail - connection still works
fmt.Printf("Warning: failed to save config: %v\n", err)
log.Printf("wpa_supplicant: SaveConfig failed: %v", err)
} else {
log.Printf("wpa_supplicant: configuration saved for SSID %q", ssid)
}
return nil