-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdbus.go
174 lines (140 loc) · 3.61 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
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
166
167
168
169
170
171
172
173
174
package connman
import (
"errors"
"fmt"
"reflect"
"strings"
"github.com/godbus/dbus"
)
type DBusInterface struct {
Connection *dbus.Conn
Object dbus.BusObject
Interface string
}
func (db *DBusInterface) Call(name string, args ...interface{}) ([]interface{}, error) {
call := db.Object.Call(db.Interface+"."+name, 0, args...)
return call.Body, call.Err
}
func (db *DBusInterface) Set(name string, value interface{}) error {
v := dbus.MakeVariant(value)
return db.Object.Call(db.Interface+".SetProperty", 0, name, v).Err
}
func (db *DBusInterface) Get(name string) (interface{}, error) {
call := db.Object.Call(db.Interface+".GetProperties", 0)
if call.Err != nil {
return nil, call.Err
}
props := call.Body[0].(map[string]dbus.Variant)
if prop, ok := props[name]; !ok {
return nil, errors.New("Invalid property")
} else {
return prop.Value(), nil
}
}
func (db *DBusInterface) Done() error {
return db.Connection.Close()
}
func DBus(service string, path dbus.ObjectPath, ifname string) (*DBusInterface, error) {
conn, err := dbus.SystemBus()
if err != nil {
return nil, err
}
obj := conn.Object(service, path)
if obj == nil {
conn.Close()
return nil, err
}
return &DBusInterface{
Connection: conn,
Object: obj,
Interface: ifname,
}, nil
}
func DBusClock() (*DBusInterface, error) {
return DBus("net.connman", "/", "net.connman.Clock")
}
func DBusWifi() (*DBusInterface, error) {
return DBus(
"net.connman",
"/net/connman/technology/wifi",
"net.connman.Technology")
}
func DBusManager() (*DBusInterface, error) {
return DBus("net.connman", "/", "net.connman.Manager")
}
func DBusService(svc dbus.ObjectPath) (*DBusInterface, error) {
return DBus("net.connman", svc, "net.connman.Service")
}
func DBusTechnology(tech dbus.ObjectPath) (*DBusInterface, error) {
return DBus("net.connman", tech, "net.connman.Technology")
}
/*
* D-Bus to Go mapping functions
*/
func setField(dst interface{}, key string, val dbus.Variant) error {
key = strings.Replace(key, ".", "", -1)
sv := reflect.ValueOf(dst).Elem()
sfv := sv.FieldByName(key)
if !sfv.IsValid() {
return fmt.Errorf("No such field %s in structure", key)
}
if !sfv.CanSet() {
return fmt.Errorf("Cannot set %s field value", key)
}
sft := sfv.Type()
v := reflect.ValueOf(val.Value())
vt := reflect.TypeOf(val.Value())
switch vt {
case reflect.MapOf(reflect.TypeOf(key), reflect.TypeOf(val)):
subf := reflect.New(sft)
if err := dictToStruct(v.Interface().(map[string]dbus.Variant), subf.Interface()); err != nil {
return err
}
sfv.Set(reflect.Indirect(subf))
default:
if sft != vt {
return fmt.Errorf("Value type (%v) does not match field type (%s) : %v",
vt, sft.Name(), v)
}
sfv.Set(v)
}
return nil
}
func dictToStruct(dict map[string]dbus.Variant, dst interface{}) error {
for k, v := range dict {
err := setField(dst, k, v)
if err != nil {
return err
}
}
return nil
}
func structToDict(src interface{}) (map[string]dbus.Variant, error) {
st := reflect.TypeOf(src)
sv := reflect.ValueOf(src)
for st.Kind() == reflect.Ptr {
sv = reflect.Indirect(reflect.ValueOf(src))
st = sv.Type()
}
if st.Kind() != reflect.Struct {
return nil, fmt.Errorf("Expected struct, found %v", st.Kind())
}
ret := make(map[string]dbus.Variant)
for i := 0; i < sv.NumField(); i++ {
sft := st.Field(i).Type
sfn := st.Field(i).Name
sfv := sv.Field(i).Interface()
switch sft.Kind() {
case reflect.Struct:
sub, err := structToDict(sfv)
if err != nil {
return nil, err
}
ret[sfn] = dbus.MakeVariant(sub)
break
default:
ret[sfn] = dbus.MakeVariant(sfv)
}
}
return ret, nil
}