Add configuration system and ARP-based device discovery

Implement comprehensive configuration management with CLI flags for WiFi interface, device discovery method, and file paths. Add ARP table parsing as an alternative to DHCP leases for more reliable device detection. Improve WiFi scanning to handle concurrent scan requests gracefully.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
nemunaire 2025-12-28 18:51:15 +07:00
commit 2b3a5b89f8
8 changed files with 156 additions and 28 deletions

View file

@ -12,8 +12,7 @@ import (
)
const (
WLAN_INTERFACE = "wlan0"
WPA_CONF = "/etc/wpa_supplicant/wpa_supplicant.conf"
WPA_CONF = "/etc/wpa_supplicant/wpa_supplicant.conf"
// D-Bus constants for wpa_supplicant
WPA_SUPPLICANT_SERVICE = "fi.w1.wpa_supplicant1"
@ -25,12 +24,14 @@ const (
)
var (
wlanInterface string
dbusConn *dbus.Conn
wpaSupplicant dbus.BusObject
)
// Initialize initializes the WiFi service with D-Bus connection
func Initialize() error {
func Initialize(interfaceName string) error {
wlanInterface = interfaceName
var err error
dbusConn, err = dbus.SystemBus()
if err != nil {
@ -55,15 +56,29 @@ func ScanNetworks() ([]models.WiFiNetwork, error) {
return nil, fmt.Errorf("impossible d'obtenir l'interface WiFi: %v", err)
}
// Trigger a scan
wifiInterface := dbusConn.Object(WPA_SUPPLICANT_SERVICE, interfacePath)
call := wifiInterface.Call(WPA_INTERFACE_IFACE+".Scan", 0, map[string]dbus.Variant{"Type": dbus.MakeVariant("active")})
if call.Err != nil {
return nil, fmt.Errorf("erreur lors du scan: %v", call.Err)
}
// Wait for scan to complete
time.Sleep(2 * time.Second)
// Check current scanning state
scanning, err := wifiInterface.GetProperty(WPA_INTERFACE_IFACE + ".Scanning")
if err == nil && scanning.Value().(bool) {
// Scan already in progress, wait for it to complete
time.Sleep(3 * time.Second)
} else {
// Trigger a scan
call := wifiInterface.Call(WPA_INTERFACE_IFACE+".Scan", 0, map[string]dbus.Variant{"Type": dbus.MakeVariant("active")})
if call.Err != nil {
// If scan is rejected, it might be too soon after a previous scan
// Try to use cached results instead
if strings.Contains(call.Err.Error(), "rejected") {
// Continue to retrieve existing BSS list
} else {
return nil, fmt.Errorf("erreur lors du scan: %v", call.Err)
}
} else {
// Wait for scan to complete
time.Sleep(2 * time.Second)
}
}
// Retrieve BSS list
bssePaths, err := wifiInterface.GetProperty(WPA_INTERFACE_IFACE + ".BSSs")
@ -224,7 +239,7 @@ func IsConnected() bool {
// IsConnectedLegacy checks if WiFi is connected using iwconfig (fallback)
func IsConnectedLegacy() bool {
cmd := exec.Command("iwconfig", WLAN_INTERFACE)
cmd := exec.Command("iwconfig", wlanInterface)
output, err := cmd.Output()
if err != nil {
return false
@ -236,7 +251,7 @@ func IsConnectedLegacy() bool {
// getWiFiInterfacePath retrieves the D-Bus path for the WiFi interface
func getWiFiInterfacePath() (dbus.ObjectPath, error) {
var interfacePath dbus.ObjectPath
err := wpaSupplicant.Call(WPA_SUPPLICANT_IFACE+".GetInterface", 0, WLAN_INTERFACE).Store(&interfacePath)
err := wpaSupplicant.Call(WPA_SUPPLICANT_IFACE+".GetInterface", 0, wlanInterface).Store(&interfacePath)
if err != nil {
return "", fmt.Errorf("erreur lors de la récupération des interfaces: %v", err)
}