/
dbus.go
63 lines (56 loc) · 1.12 KB
/
dbus.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
package bluez
import (
"errors"
"fmt"
"github.com/godbus/dbus"
)
//BusType a type of DBus connection
type BusType int
const (
// SessionBus uses the session bus
SessionBus BusType = iota
// SystemBus uses the system bus
SystemBus
)
var conns = make([]*dbus.Conn, 2)
// Config pass configuration to a DBUS client
type Config struct {
Name string
Iface string
Path string
Bus BusType
}
//GetConnection get a DBus connection
func GetConnection(connType BusType) (*dbus.Conn, error) {
fmt.Sprintf("Get connection %d", connType)
switch connType {
case SystemBus:
{
if conns[SystemBus] == nil {
// c.logger.Debug("Connecting to SystemBus")
conn, err := dbus.SystemBus()
if err != nil {
return nil, err
}
conns[SystemBus] = conn
}
return conns[SystemBus], nil
}
case SessionBus:
{
if conns[SessionBus] == nil {
// c.logger.Debug("Connecting to SessionBus")
conn, err := dbus.SessionBus()
if err != nil {
return nil, err
}
conns[SessionBus] = conn
}
return conns[SessionBus], nil
}
default:
{
panic(errors.New("Unmanged DBus type code"))
}
}
}