This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Device.go
209 lines (174 loc) · 4.89 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
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
package gonetworkmanager
import (
"encoding/json"
"errors"
"github.com/godbus/dbus"
)
const (
DeviceInterface = NetworkManagerInterface + ".Device"
DevicePropertyInterface = DeviceInterface + ".Interface"
DevicePropertyIpInterface = DeviceInterface + ".IpInterface"
DevicePropertyState = DeviceInterface + ".State"
DevicePropertyIP4Config = DeviceInterface + ".Ip4Config"
DevicePropertyDeviceType = DeviceInterface + ".DeviceType"
DevicePropertyAvailableConnections = DeviceInterface + ".AvailableConnections"
DevicePropertyDhcp4Config = DeviceInterface + ".Dhcp4Config"
)
func DeviceFactory(objectPath dbus.ObjectPath) (Device, error) {
d, err := NewDevice(objectPath)
if err != nil {
return nil, err
}
dt, err := d.GetDeviceType()
if err != nil {
return nil, err
}
switch dt {
case NmDeviceTypeWifi:
return NewWirelessDevice(objectPath)
}
return d, nil
}
type Device interface {
GetPath() dbus.ObjectPath
// GetInterface gets the name of the device's control (and often data)
// interface.
GetInterface() (string, error)
// GetIpInterface gets the IP interface name of the device.
GetIpInterface() (string, error)
// GetState gets the current state of the device.
GetState() (NmDeviceState, error)
// GetIP4Config gets the Ip4Config object describing the configuration of the
// device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED
// state.
GetIP4Config() (IP4Config, error)
// GetDHCP4Config gets the Dhcp4Config object describing the configuration of the
// device. Only valid when the device is in the NM_DEVICE_STATE_ACTIVATED
// state.
GetDHCP4Config() (DHCP4Config, error)
// GetDeviceType gets the general type of the network device; ie Ethernet,
// WiFi, etc.
GetDeviceType() (NmDeviceType, error)
// GetAvailableConnections gets an array of object paths of every configured
// connection that is currently 'available' through this device.
GetAvailableConnections() ([]Connection, error)
MarshalJSON() ([]byte, error)
}
func NewDevice(objectPath dbus.ObjectPath) (Device, error) {
var d device
return &d, d.init(NetworkManagerInterface, objectPath)
}
type device struct {
dbusBase
}
func (d *device) GetPath() dbus.ObjectPath {
return d.obj.Path()
}
func (d *device) GetInterface() (string, error) {
return d.getStringProperty(DevicePropertyInterface)
}
func (d *device) GetIpInterface() (string, error) {
return d.getStringProperty(DevicePropertyIpInterface)
}
func (d *device) GetState() (NmDeviceState, error) {
r, err := d.getUint32Property(DevicePropertyState)
if err != nil {
return NmDeviceStateFailed, err
}
return NmDeviceState(r), nil
}
func (d *device) GetIP4Config() (IP4Config, error) {
path, err := d.getObjectProperty(DevicePropertyIP4Config)
if err != nil {
return nil, err
}
if path == "/" {
return nil, errors.New("device path was empty")
}
cfg, err := NewIP4Config(path)
if err != nil {
return nil, err
}
return cfg, nil
}
func (d *device) GetDHCP4Config() (DHCP4Config, error) {
path, err := d.getObjectProperty(DevicePropertyDhcp4Config)
if err != nil {
return nil, err
}
if path == "/" {
return nil, errors.New("device path was empty")
}
cfg, err := NewDHCP4Config(path)
if err != nil {
return nil, err
}
return cfg, nil
}
func (d *device) GetDeviceType() (NmDeviceType, error) {
r, err := d.getUint32Property(DevicePropertyDeviceType)
if err != nil {
return NmDeviceTypeUnknown, err
}
return NmDeviceType(r), nil
}
func (d *device) GetAvailableConnections() ([]Connection, error) {
connPaths, err := d.getSliceObjectProperty(DevicePropertyAvailableConnections)
if err != nil {
return nil, err
}
conns := make([]Connection, len(connPaths))
for i, path := range connPaths {
conns[i], err = NewConnection(path)
if err != nil {
return nil, err
}
}
return conns, nil
}
func (d *device) marshalMap() (map[string]interface{}, error) {
Interface, err := d.GetInterface()
if err != nil {
return nil, err
}
IPinterface, err := d.GetIpInterface()
if err != nil {
return nil, err
}
State, err := d.GetState()
if err != nil {
return nil, err
}
IP4Config, err := d.GetIP4Config()
if err != nil {
return nil, err
}
DHCP4Config, err := d.GetDHCP4Config()
if err != nil {
return nil, err
}
DeviceType, err := d.GetDeviceType()
if err != nil {
return nil, err
}
AvailableConnections, err := d.GetAvailableConnections()
if err != nil {
return nil, err
}
return map[string]interface{}{
"Interface": Interface,
"IP interface": IPinterface,
"State": State.String(),
"IP4Config": IP4Config,
"DHCP4Config": DHCP4Config,
"DeviceType": DeviceType.String(),
"AvailableConnections": AvailableConnections,
}, nil
}
func (d *device) MarshalJSON() ([]byte, error) {
m, err := d.marshalMap()
if err != nil {
return nil, err
}
return json.Marshal(m)
}