-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdevice.go
128 lines (98 loc) · 1.95 KB
/
device.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
package nm
import (
"fmt"
"launchpad.net/~jamesh/go-dbus/trunk"
)
type Device struct {
*dbus.ObjectProxy
*dbus.Properties
cli *Client
typ DeviceType
}
type DeviceType uint32
type DeviceHandler func(*Device)
const (
Unknown DeviceType = iota
Wired
Wireless
)
func (cli *Client) newDevice(path dbus.ObjectPath) *Device {
obj := cli.conn.Object(NM_UNIQ_NAME, path)
if obj == nil {
return nil
}
dev := &Device{
ObjectProxy: obj,
Properties: &dbus.Properties{obj},
cli: cli,
}
ret, err := dev.Get(NM_DEV_IFACE, "DeviceType")
if err == nil {
tmp := ret.(uint32)
dev.typ = DeviceType(tmp)
} else {
dev.typ = Unknown
}
return dev
}
func (dev *Device) Disconnect() error {
_, err := dev.Call(NM_DEV_IFACE, "Disconnect")
if err != nil {
return err
}
return nil
}
func (dev *Device) Type() DeviceType {
return dev.typ
}
func (dev *Device) Driver() (string, error) {
ret, err := dev.Get(NM_DEV_IFACE, "Driver")
if err != nil {
return "", err
}
return ret.(string), nil
}
func (dev *Device) DriverVersion() (string, error) {
ret, err := dev.Get(NM_DEV_IFACE, "DriverVersion")
if err != nil {
return "", err
}
return ret.(string), nil
}
func (dev *Device) FirmwareVersion() (string, error) {
ret, err := dev.Get(NM_DEV_IFACE, "FirmwareVersion")
if err != nil {
return "", err
}
return ret.(string), nil
}
func (dev *Device) State() (uint32, error) {
ret, err := dev.Get(NM_DEV_IFACE, "State")
if err != nil {
return 0, err
}
return ret.(uint32), err
}
func (dev *Device) PropChanged(handler DeviceHandler) error {
iface := ""
if dev.typ == Wired {
iface = NM_DEV_WIRED_IFACE
} else if dev.typ == Wireless {
iface = NM_DEV_WIRELESS_IFACE
} else {
return fmt.Errorf("Unknown device type: %s", dev.typ)
}
watcher, err := dev.WatchSignal(iface, "PropertiesChanged")
if err != nil {
return err
}
go func() {
for {
select {
case <-watcher.C:
go handler(dev)
}
}
}()
return err
}