repeater/internal/wifi/iwd/network.go

64 lines
1.5 KiB
Go

package iwd
import (
"fmt"
"github.com/godbus/dbus/v5"
)
// Network represents an iwd Network interface
type Network struct {
path dbus.ObjectPath
conn *dbus.Conn
obj dbus.BusObject
}
// NewNetwork creates a new Network instance
func NewNetwork(conn *dbus.Conn, path dbus.ObjectPath) *Network {
return &Network{
path: path,
conn: conn,
obj: conn.Object(Service, path),
}
}
// GetProperties retrieves all network properties
func (n *Network) GetProperties() (*NetworkProperties, error) {
var props map[string]dbus.Variant
err := n.obj.Call("org.freedesktop.DBus.Properties.GetAll", 0, NetworkInterface).Store(&props)
if err != nil {
return nil, fmt.Errorf("failed to get network properties: %v", err)
}
netProps := &NetworkProperties{}
if nameVariant, ok := props["Name"]; ok {
if name, ok := nameVariant.Value().(string); ok {
netProps.Name = name
}
}
if typeVariant, ok := props["Type"]; ok {
if netType, ok := typeVariant.Value().(string); ok {
netProps.Type = netType
}
}
if connectedVariant, ok := props["Connected"]; ok {
if connected, ok := connectedVariant.Value().(bool); ok {
netProps.Connected = connected
}
}
return netProps, nil
}
// Connect initiates a connection to this network
// Credentials are provided via the registered agent's RequestPassphrase callback
func (n *Network) Connect() error {
err := n.obj.Call(NetworkInterface+".Connect", 0).Err
if err != nil {
return fmt.Errorf("connect failed: %v", err)
}
return nil
}