forked from plorefice/gonnman
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.go
84 lines (72 loc) · 1.63 KB
/
agent.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
package connman
import (
"fmt"
"log"
"os"
"github.com/godbus/dbus"
)
type Agent struct {
Service string
Path dbus.ObjectPath
Interface string
Name string
Passphrase string
}
func NewAgent(ssid, psk string) *Agent {
agent := &Agent{
Service: "net.gonnman",
Path: "/net/connman/Agent",
Interface: "net.connman.Agent",
Name: ssid,
Passphrase: psk,
}
conn, err := dbus.SystemBus()
if err != nil {
fmt.Println(err)
return nil
}
reply, err := conn.RequestName(agent.Service, dbus.NameFlagDoNotQueue)
if err != nil {
fmt.Println(err)
return nil
}
if reply != dbus.RequestNameReplyPrimaryOwner {
fmt.Fprintln(os.Stderr, "Name already taken")
return nil
}
conn.Export(agent, agent.Path, agent.Interface)
return agent
}
func (a *Agent) Destroy() error {
conn, err := dbus.SystemBus()
if err != nil {
return err
}
reply, err := conn.ReleaseName(a.Service)
if err != nil {
return err
}
if reply != dbus.ReleaseNameReplyReleased {
return fmt.Errorf("Could not release the name\n")
}
conn.Export(nil, a.Path, a.Interface)
return nil
}
func (a *Agent) RequestInput(service dbus.ObjectPath, rq map[string]dbus.Variant) (map[string]dbus.Variant, *dbus.Error) {
var in map[string]dbus.Variant
if a.Name != "" {
in = map[string]dbus.Variant{
"Name": dbus.MakeVariant(a.Name),
"Passphrase": dbus.MakeVariant(a.Passphrase),
}
} else {
in = map[string]dbus.Variant{
"Passphrase": dbus.MakeVariant(a.Passphrase),
}
}
return in, nil
}
func (a *Agent) ReportError(service dbus.ObjectPath, err string) *dbus.Error {
log.Printf("%s: %s\n", service, err)
return nil
}