-
Notifications
You must be signed in to change notification settings - Fork 3
/
base.go
113 lines (84 loc) · 2.39 KB
/
base.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
package nm
import (
"errors"
"launchpad.net/~jamesh/go-dbus/trunk"
)
type Client struct {
*dbus.ObjectProxy
*dbus.Properties
conn *dbus.Connection
}
const (
NM_UNIQ_NAME = "org.freedesktop.NetworkManager"
NM_BASE_IFACE = "org.freedesktop.NetworkManager"
NM_DEV_IFACE = "org.freedesktop.NetworkManager.Device"
NM_DEV_WIRELESS_IFACE = "org.freedesktop.NetworkManager.Device.Wireless"
NM_DEV_WIRED_IFACE = "org.freedesktop.NetworkManager.Device.Wired"
NM_AP_IFACE = "org.freedesktop.NetworkManager.AccessPoint"
NM_CONN_IFACE = "org.freedesktop.NetworkManager.Settings.Connection"
)
func New(conn *dbus.Connection) *Client {
obj := conn.Object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
cli := &Client{
ObjectProxy: obj,
Properties: &dbus.Properties{obj},
conn: conn,
}
return cli
}
func (cli *Client) GetDevices() ([]*Device, error) {
msg, err := cli.Call(NM_BASE_IFACE, "GetDevices")
if err != nil {
return nil, err
}
var paths []dbus.ObjectPath
if err = msg.Args(&paths); err != nil {
return nil, err
}
ret := make([]*Device, 0, len(paths))
for _, path := range paths {
ret = append(ret, cli.newDevice(path))
}
return ret, nil
}
func (cli *Client) GetDeviceByIpIface(iface string) (*Device, error) {
msg, err := cli.Call(NM_BASE_IFACE, "GetDeviceByIpIface", iface)
if err != nil {
return nil, err
}
var path dbus.ObjectPath
if err = msg.Args(&path); err != nil {
return nil, err
}
return cli.newDevice(path), nil
}
func (cli *Client) AddAndActivateConnection(conn *Connection, dev *Device, objPath dbus.ObjectPath) error {
var data map[string]map[string]*dbus.Variant
if conn != nil {
data = conn.Data
}
msg, err := cli.Call(NM_BASE_IFACE, "AddAndActivateConnection", data, dev.ObjectPath(), objPath)
if err != nil {
return err
}
var path dbus.ObjectPath
var activeConn dbus.ObjectPath
if err = msg.Args(&path, &activeConn); err != nil {
return err
}
return nil
}
func (cli *Client) ActivateConnection(conn *Connection, dev *Device, objPath dbus.ObjectPath) error {
if conn == nil {
return errors.New("Must pass connection to ActivateConnection")
}
msg, err := cli.Call(NM_BASE_IFACE, "ActivateConnection", conn.ObjectPath, dev.ObjectPath(), objPath)
if err != nil {
return nil
}
var path dbus.ObjectPath
if err = msg.Args(&path); err != nil {
return err
}
return nil
}