mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 15:19:08 +00:00
Add dbus Test
This commit is contained in:
parent
a737ac4d9d
commit
f01ea3fdb7
7 changed files with 106 additions and 56 deletions
48
internal/networkd/dbus/link.go
Normal file
48
internal/networkd/dbus/link.go
Normal file
|
@ -0,0 +1,48 @@
|
|||
package dbus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/godbus/dbus/v5"
|
||||
"golang.org/x/exp/slog"
|
||||
)
|
||||
|
||||
type Link struct {
|
||||
Name string `json:"name"`
|
||||
CarrierState string `json:"carrier_state"`
|
||||
OperationalState string `json:"operational_state"`
|
||||
}
|
||||
|
||||
func GetLinks(dbusConn dbus.Conn) ([]Link, error) {
|
||||
managerObj := dbusConn.Object("org.freedesktop.network1", dbus.ObjectPath("/org/freedesktop/network1"))
|
||||
|
||||
var links [][]any
|
||||
err := managerObj.Call("org.freedesktop.network1.Manager.ListLinks", 0).Store(&links)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Calling ListLinks %w", err)
|
||||
}
|
||||
slog.Info("Dbus Result", "links", links)
|
||||
|
||||
result := []Link{}
|
||||
|
||||
for _, link := range links {
|
||||
name := link[1].(string)
|
||||
path := link[2].(string)
|
||||
linkObj := dbusConn.Object("org.freedesktop.network1", dbus.ObjectPath(path))
|
||||
carrierState, err := linkObj.GetProperty("CarrierState")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetProperty CarrierState %w", err)
|
||||
}
|
||||
operationalState, err := linkObj.GetProperty("OperationalState")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("GetProperty OperationalState %w", err)
|
||||
}
|
||||
result = append(result, Link{
|
||||
Name: name,
|
||||
CarrierState: carrierState.String(),
|
||||
OperationalState: operationalState.String(),
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue