/
dbus.go
68 lines (53 loc) · 1.55 KB
/
dbus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"github.com/Necoro/bolt-listener/pkg/log"
"github.com/godbus/dbus/v5"
)
// Bolt DBus
const (
boltSenderName = "org.freedesktop.bolt"
boltDeviceInterface = "org.freedesktop.bolt1.Device"
boltDevicePath = "/org/freedesktop/bolt/devices/"
propertiesInterface = "org.freedesktop.DBus.Properties"
)
func (d dockConfig) objectPath() dbus.ObjectPath {
return dbus.ObjectPath(boltDevicePath + d.Uuid)
}
func addSignal(conn *dbus.Conn, path dbus.ObjectPath) error {
log.Debug("Watching for signals of ", path)
options := []dbus.MatchOption{
dbus.WithMatchObjectPath(path),
dbus.WithMatchInterface(propertiesInterface),
}
if !testMode {
options = append(options, dbus.WithMatchSender(boltSenderName))
}
return conn.AddMatchSignal(options...)
}
func dbusConnect(busName string) (*dbus.Conn, error) {
log.Debugf("Connecting to the %s bus", busName)
switch busName {
case "session":
return dbus.SessionBus()
case "system":
return dbus.SystemBus()
default:
return nil, fmt.Errorf("unknown bus '%s'", busName)
}
}
func getStatus(v *dbus.Signal) (string, error) {
var interfaceName string
var changed map[string]interface{}
var invalidated []string
if err := dbus.Store(v.Body, &interfaceName, &changed, &invalidated); err != nil {
return "", fmt.Errorf("Unexpected data: %s; Error: %w", v.Body, err)
}
if interfaceName != boltDeviceInterface {
return "", fmt.Errorf("Unexpected Interface: %s", interfaceName)
}
if status, ok := changed["Status"]; ok {
return status.(string), nil
}
return "", nil
}