forked from muka/go-bluetooth
/
app.go
187 lines (148 loc) · 3.62 KB
/
app.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
package service
import (
"fmt"
"strings"
"github.com/godbus/dbus"
"github.com/godbus/dbus/introspect"
"github.com/google/uuid"
"github.com/muka/go-bluetooth/api"
"github.com/muka/go-bluetooth/bluez"
"github.com/muka/go-bluetooth/bluez/profile/adapter"
"github.com/muka/go-bluetooth/bluez/profile/advertising"
"github.com/muka/go-bluetooth/bluez/profile/agent"
"github.com/muka/go-bluetooth/bluez/profile/gatt"
log "github.com/sirupsen/logrus"
)
var AppPath = "/org/bluez/%s/apps/app%d"
var UseRandomUUID = false
var baseUUID = "00000000-000%d-1000-8000-00805F9B34FB"
var appCounter = 0
func RandomUUID() (string, error) {
bUUID := fmt.Sprintf(baseUUID, appCounter)
UUID, err := uuid.Parse(bUUID)
if UseRandomUUID {
UUID, err = uuid.NewRandom()
}
return strings.ToUpper(UUID.String()), err
}
// Initialize a new bluetooth service (app)
func NewApp(adapterID string) (*App, error) {
app := new(App)
app.adapterID = adapterID
app.services = make(map[dbus.ObjectPath]*Service)
app.path = dbus.ObjectPath(
fmt.Sprintf(
AppPath,
app.adapterID,
appCounter,
),
)
app.advertisement = &advertising.LEAdvertisement1Properties{
Type: advertising.AdvertisementTypePeripheral,
}
app.AgentCaps = agent.CapKeyboardDisplay
app.AgentSetAsDefault = true
appCounter += 1
return app, app.init()
}
// Wrap a bluetooth application exposing services
type App struct {
path dbus.ObjectPath
baseUUID string
adapterID string
adapter *adapter.Adapter1
agent agent.Agent1Client
AgentCaps string
AgentSetAsDefault bool
conn *dbus.Conn
objectManager *api.DBusObjectManager
services map[dbus.ObjectPath]*Service
advertisement *advertising.LEAdvertisement1Properties
gm *gatt.GattManager1
}
func (app *App) init() error {
// log.Tracef("Exposing %s", app.Path())
// log.Trace("Load adapter")
a, err := adapter.NewAdapter1FromAdapterID(app.adapterID)
if err != nil {
return err
}
app.adapter = a
agent1, err := app.createAgent()
if err != nil {
return err
}
app.agent = agent1
conn, err := dbus.SystemBus()
if err != nil {
return err
}
app.conn = conn
_, err = conn.RequestName(
"org.bluez",
dbus.NameFlagDoNotQueue&dbus.NameFlagReplaceExisting,
)
if err != nil {
return err
}
om, err := api.NewDBusObjectManager(app.DBusConn())
if err != nil {
return err
}
app.objectManager = om
err = conn.Export(app.objectManager, app.Path(), bluez.ObjectManagerInterface)
if err != nil {
return err
}
node := &introspect.Node{
Interfaces: []introspect.Interface{
//Introspect
introspect.IntrospectData,
//ObjectManager
bluez.ObjectManagerIntrospectData,
},
}
introspectable := introspect.NewIntrospectable(node)
err = app.conn.Export(
introspectable,
app.Path(),
"org.freedesktop.DBus.Introspectable",
)
return nil
}
func (app *App) Run() (err error) {
err = app.ExposeAgent(app.AgentCaps, app.AgentSetAsDefault)
if err != nil {
return fmt.Errorf("ExposeAgent: %s", err)
}
gm, err := gatt.NewGattManager1FromAdapterID(app.adapterID)
if err != nil {
return err
}
app.gm = gm
options := map[string]interface{}{}
err = gm.RegisterApplication(app.Path(), options)
if err != nil {
return err
}
return nil
}
func (app *App) Close() {
if app.agent != nil {
err := agent.RemoveAgent(app.agent)
if err != nil {
log.Warnf("RemoveAgent: %s", err)
}
// err =
app.agent.Release()
// if err != nil {
// log.Warnf("Agent1.Release: %s", err)
// }
}
if app.gm != nil {
err1 := app.gm.UnregisterApplication(app.Path())
if err1 != nil {
log.Warnf("GattManager1.UnregisterApplication: %s", err1)
}
}
}