71 lines
2 KiB
Go
71 lines
2 KiB
Go
package iwd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/godbus/dbus/v5"
|
|
)
|
|
|
|
// Manager handles iwd object discovery via ObjectManager
|
|
type Manager struct {
|
|
conn *dbus.Conn
|
|
obj dbus.BusObject
|
|
}
|
|
|
|
// NewManager creates a new Manager instance
|
|
func NewManager(conn *dbus.Conn) *Manager {
|
|
return &Manager{
|
|
conn: conn,
|
|
obj: conn.Object(Service, dbus.ObjectPath(ManagerPath)),
|
|
}
|
|
}
|
|
|
|
// GetManagedObjects returns all iwd managed objects
|
|
func (m *Manager) GetManagedObjects() (map[dbus.ObjectPath]map[string]map[string]dbus.Variant, error) {
|
|
var objects map[dbus.ObjectPath]map[string]map[string]dbus.Variant
|
|
err := m.obj.Call("org.freedesktop.DBus.ObjectManager.GetManagedObjects", 0).Store(&objects)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to get managed objects: %v", err)
|
|
}
|
|
return objects, nil
|
|
}
|
|
|
|
// FindStation finds the Station object for the given interface name
|
|
func (m *Manager) FindStation(interfaceName string) (*Station, error) {
|
|
objects, err := m.GetManagedObjects()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// First, find the device with matching interface name
|
|
var devicePath dbus.ObjectPath
|
|
for path, interfaces := range objects {
|
|
if deviceProps, ok := interfaces[DeviceInterface]; ok {
|
|
if nameVariant, ok := deviceProps["Name"]; ok {
|
|
if name, ok := nameVariant.Value().(string); ok && name == interfaceName {
|
|
devicePath = path
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if devicePath == "" {
|
|
return nil, fmt.Errorf("device with interface '%s' not found", interfaceName)
|
|
}
|
|
|
|
// Now find the station object under this device
|
|
// Station path is typically the same as device path or a child of it
|
|
for path, interfaces := range objects {
|
|
if _, ok := interfaces[StationInterface]; ok {
|
|
// Check if this station belongs to our device
|
|
// Station path should be the device path or start with it
|
|
if path == devicePath || strings.HasPrefix(string(path), string(devicePath)+"/") {
|
|
return NewStation(m.conn, path), nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("station for device '%s' not found", interfaceName)
|
|
}
|