Skip to content

Commit

Permalink
Add tools/dbusclient
Browse files Browse the repository at this point in the history
Signed-off-by: hwipl <33433250+hwipl@users.noreply.github.com>
  • Loading branch information
hwipl committed May 12, 2023
1 parent a00b61f commit debc7a9
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions tools/dbusclient/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"

"github.com/T-Systems-MMS/oc-daemon/internal/dbusapi"
"github.com/godbus/dbus/v5"
log "github.com/sirupsen/logrus"
)

func main() {
// connect to session bus
conn, err := dbus.ConnectSystemBus()
if err != nil {
log.Fatal(err)
}
defer func() { _ = conn.Close() }()

// subscribe to properties changed signals
if err = conn.AddMatchSignal(
dbus.WithMatchSender(dbusapi.Interface),
dbus.WithMatchInterface("org.freedesktop.DBus.Properties"),
dbus.WithMatchMember("PropertiesChanged"),
dbus.WithMatchPathNamespace(dbusapi.Path),
); err != nil {
log.Fatal(err)
}

// get initial values of properties
trustedNetwork := dbusapi.TrustedNetworkUnknown
connectionState := dbusapi.ConnectionStateUnknown
ip := dbusapi.IPInvalid
device := dbusapi.DeviceInvalid
connectedAt := dbusapi.ConnectedAtInvalid
servers := dbusapi.ServersInvalid

getProperty := func(name string, val any) {
err = conn.Object(dbusapi.Interface, dbusapi.Path).
StoreProperty(dbusapi.Interface+"."+name, val)
if err != nil {
log.Fatal(err)
}
}
getProperty(dbusapi.PropertyTrustedNetwork, &trustedNetwork)
getProperty(dbusapi.PropertyConnectionState, &connectionState)
getProperty(dbusapi.PropertyIP, &ip)
getProperty(dbusapi.PropertyDevice, &device)
getProperty(dbusapi.PropertyConnectedAt, &connectedAt)
getProperty(dbusapi.PropertyServers, &servers)

log.Println("TrustedNetwork:", trustedNetwork)
log.Println("ConnectionState:", connectionState)
log.Println("IP:", ip)
log.Println("Device:", device)
log.Println("ConnectedAt:", connectedAt)
log.Println("Servers:", servers)

// handle signals
c := make(chan *dbus.Signal, 10)
conn.Signal(c)
for s := range c {
// make sure it's a properties changed signal
if s.Path != dbusapi.Path ||
s.Name != "org.freedesktop.DBus.Properties.PropertiesChanged" {
log.Error("Not a properties changed signal")
continue
}

// check properties changed signal
if v, ok := s.Body[0].(string); !ok || v != dbusapi.Interface {
log.Error("Not the right properties changed signal")
continue
}

// get changed properties
changed, ok := s.Body[1].(map[string]dbus.Variant)
if !ok {
log.Error("Invalid changed properties in properties changed signal")
continue
}
for name, value := range changed {
fmt.Printf("Changed property: %s ", name)
switch name {
case dbusapi.PropertyTrustedNetwork:
value.Store(&trustedNetwork)
fmt.Println(trustedNetwork)
case dbusapi.PropertyConnectionState:
value.Store(&connectionState)
fmt.Println(connectionState)
case dbusapi.PropertyIP:
value.Store(&ip)
fmt.Println(ip)
case dbusapi.PropertyDevice:
value.Store(&device)
fmt.Println(device)
case dbusapi.PropertyConnectedAt:
value.Store(&connectedAt)
fmt.Println(connectedAt)
case dbusapi.PropertyServers:
value.Store(&servers)
fmt.Println(servers)
}
}

// get invalidated properties
invalid, ok := s.Body[2].([]string)
if !ok {
log.Error("Invalid invalidated properties in properties changed signal")
}
for _, name := range invalid {
// not expected to happen currently, but handle it anyway
switch name {
case dbusapi.PropertyTrustedNetwork:
trustedNetwork = dbusapi.TrustedNetworkUnknown
case dbusapi.PropertyConnectionState:
connectionState = dbusapi.ConnectionStateUnknown
case dbusapi.PropertyIP:
ip = dbusapi.IPInvalid
case dbusapi.PropertyDevice:
device = dbusapi.DeviceInvalid
case dbusapi.PropertyConnectedAt:
connectedAt = dbusapi.ConnectedAtInvalid
case dbusapi.PropertyServers:
servers = dbusapi.ServersInvalid
}
fmt.Printf("Invalidated property: %s\n", name)
}
}
}

0 comments on commit debc7a9

Please sign in to comment.