-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
165 lines (154 loc) · 4.67 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
Dbusclient is an example of a D-Bus API client.
*/
package main
import (
"fmt"
"github.com/godbus/dbus/v5"
log "github.com/sirupsen/logrus"
"github.com/telekom-mms/oc-daemon/internal/dbusapi"
)
func main() {
// connect to system 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
ocRunning := dbusapi.OCRunningUnknown
vpnConfig := dbusapi.VPNConfigInvalid
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)
getProperty(dbusapi.PropertyOCRunning, &ocRunning)
getProperty(dbusapi.PropertyVPNConfig, &vpnConfig)
log.Println("TrustedNetwork:", trustedNetwork)
log.Println("ConnectionState:", connectionState)
log.Println("IP:", ip)
log.Println("Device:", device)
log.Println("ConnectedAt:", connectedAt)
log.Println("Servers:", servers)
log.Println("OCRunning:", ocRunning)
log.Println("VPNConfig:", vpnConfig)
// 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:
if err := value.Store(&trustedNetwork); err != nil {
log.Fatal(err)
}
fmt.Println(trustedNetwork)
case dbusapi.PropertyConnectionState:
if err := value.Store(&connectionState); err != nil {
log.Fatal(err)
}
fmt.Println(connectionState)
case dbusapi.PropertyIP:
if err := value.Store(&ip); err != nil {
log.Fatal(err)
}
fmt.Println(ip)
case dbusapi.PropertyDevice:
if err := value.Store(&device); err != nil {
log.Fatal(err)
}
fmt.Println(device)
case dbusapi.PropertyConnectedAt:
if err := value.Store(&connectedAt); err != nil {
log.Fatal(err)
}
fmt.Println(connectedAt)
case dbusapi.PropertyServers:
if err := value.Store(&servers); err != nil {
log.Fatal(err)
}
fmt.Println(servers)
case dbusapi.PropertyOCRunning:
if err := value.Store(&ocRunning); err != nil {
log.Fatal(err)
}
fmt.Println(ocRunning)
case dbusapi.PropertyVPNConfig:
if err := value.Store(&vpnConfig); err != nil {
log.Fatal(err)
}
fmt.Println(vpnConfig)
}
}
// get invalidated properties
invalid, ok := s.Body[2].([]string)
if !ok {
log.Error("Invalid invalidated properties in properties changed signal")
continue
}
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
case dbusapi.PropertyOCRunning:
ocRunning = dbusapi.OCRunningUnknown
case dbusapi.PropertyVPNConfig:
vpnConfig = dbusapi.VPNConfigInvalid
}
fmt.Printf("Invalidated property: %s\n", name)
}
}
}