-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
executable file
·365 lines (332 loc) · 8.25 KB
/
utils.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package geoclue2
import (
"errors"
"fmt"
"github.com/godbus/dbus/v5"
"time"
)
/*
Copied and extended from https://github.com/Wifx/gonetworkmanager/blob/master/utils.go
on April the 1st 2020
The MIT License (MIT)
Copyright (c) 2019 Wifx Sàrl & Copyright (c) 2016 Bellerophon Mobile
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
const (
dbusMethodAddMatch = "org.freedesktop.DBus.AddMatch"
)
type dbusBase struct {
conn *dbus.Conn
obj dbus.BusObject
}
func (d *dbusBase) init(iface string, objectPath dbus.ObjectPath) error {
var err error
d.conn, err = dbus.SystemBus()
if err != nil {
return err
}
d.obj = d.conn.Object(iface, objectPath)
return nil
}
func (d *dbusBase) call(method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Err
}
func (d *dbusBase) callWithReturn(ret interface{}, method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Store(ret)
}
func (d *dbusBase) callWithReturn2(ret1 interface{}, ret2 interface{}, method string, args ...interface{}) error {
return d.obj.Call(method, 0, args...).Store(ret1, ret2)
}
func (d *dbusBase) subscribe(iface, member string) {
rule := fmt.Sprintf("type='signal',interface='%s',path='%s',member='%s'",
iface, d.obj.Path(), GeoclueInterface)
d.conn.BusObject().Call(dbusMethodAddMatch, 0, rule)
}
func (d *dbusBase) subscribeNamespace(namespace string) {
rule := fmt.Sprintf("type='signal',path_namespace='%s'", namespace)
d.conn.BusObject().Call(dbusMethodAddMatch, 0, rule)
}
func (d *dbusBase) getProperty(iface string) (interface{}, error) {
variant, err := d.obj.GetProperty(iface)
return variant.Value(), err
}
func (d *dbusBase) setProperty(iface string, value interface{}) error {
err := d.obj.SetProperty(iface, dbus.MakeVariant(value))
return err
}
func (d *dbusBase) getObjectProperty(iface string) (value dbus.ObjectPath, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(dbus.ObjectPath)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceObjectProperty(iface string) (value []dbus.ObjectPath, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([]dbus.ObjectPath)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getBoolProperty(iface string) (value bool, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(bool)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getStringProperty(iface string) (value string, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(string)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceStringProperty(iface string) (value []string, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([]string)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceSliceByteProperty(iface string) (value [][]byte, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([][]byte)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getMapStringVariantProperty(iface string) (value map[string]dbus.Variant, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(map[string]dbus.Variant)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getTimestampProperty(iface string) (value time.Time, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
parsedValue, ok := prop.([]interface{})
if !ok {
err = makeErrVariantType(iface)
return
}
var sec uint64
sec = 0
var msec uint64
msec = 0
for idx, val := range parsedValue {
if idx == 0 {
sec, ok = val.(uint64)
if !ok {
err = makeErrVariantType(iface)
return
}
}
if idx == 1 {
msec, ok = val.(uint64)
if !ok {
err = makeErrVariantType(iface)
return
}
}
}
value = time.Unix(int64(sec), int64(msec))
return
}
func (d *dbusBase) getUint8Property(iface string) (value uint8, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(uint8)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getUint32Property(iface string) (value uint32, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(uint32)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getInt64Property(iface string) (value int64, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(int64)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getFloat32Property(iface string) (value float32, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(float32)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getFloat64Property(iface string) (value float64, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(float64)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getUint64Property(iface string) (value uint64, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.(uint64)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceUint32Property(iface string) (value []uint32, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([]uint32)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceSliceUint32Property(iface string) (value [][]uint32, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([][]uint32)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceMapStringVariantProperty(iface string) (value []map[string]dbus.Variant, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([]map[string]dbus.Variant)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func (d *dbusBase) getSliceByteProperty(iface string) (value []byte, err error) {
prop, err := d.getProperty(iface)
if err != nil {
return
}
value, ok := prop.([]byte)
if !ok {
err = makeErrVariantType(iface)
return
}
return
}
func makeErrVariantType(iface string) error {
return fmt.Errorf("unexpected variant type for '%s'", iface)
}
func (d *dbusBase) parsePropertiesChanged(v *dbus.Signal) (interfaceName string, changedProperties map[string]dbus.Variant, invalidatedProperties []string, err error) {
if len(v.Body) != 3 {
err = errors.New("error by parsing property changed signal")
return
}
interfaceName, ok := v.Body[0].(string)
if !ok {
err = errors.New("error by parsing interface name")
return
}
changedProperties, ok = v.Body[1].(map[string]dbus.Variant)
if !ok {
err = errors.New("error by parsing changed properties map name")
return
}
invalidatedProperties, ok = v.Body[2].([]string)
if !ok {
err = errors.New("error by parsing invalidated properties")
return
}
return
}