/
obex.go
355 lines (278 loc) · 10.4 KB
/
obex.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package bluez
import (
"context"
"path/filepath"
"sync"
"github.com/godbus/dbus/v5"
"github.com/pkg/errors"
)
const (
dbusObexName = "org.bluez.obex"
dbusObexClientIface = "org.bluez.obex.Client1"
dbusObexSessionIface = "org.bluez.obex.Session1"
dbusObexTransferIface = "org.bluez.obex.Transfer1"
dbusObexObjectPushIface = "org.bluez.obex.ObjectPush1"
dbusObexPath = dbus.ObjectPath("/org/bluez/obex")
)
// ObexSessionProperties describes the session properties
// of an OBEX transfer.
type ObexSessionProperties struct {
Root string
Target string
Source string
Destination string
}
// ObexTransferProperties describes the transfer properties
// of an OBEX transfer.
type ObexTransferProperties struct {
Name string
Type string
Status string
Filename string
Size uint64
Transferred uint64
Session string
}
// ObexProperties stores the session and transfer paths of
// an OBEX transfer, along with their properties.
type ObexProperties struct {
SessionPath dbus.ObjectPath
SessionProperties ObexSessionProperties
TransferPath dbus.ObjectPath
TransferProperties ObexTransferProperties
}
// Obex represents an OBEX connection. It also stores
// information about the currently running transfers.
type Obex struct {
conn *dbus.Conn
Store map[dbus.ObjectPath]ObexProperties
StoreLock sync.Mutex
}
// NewObex returns a new Obex.
func NewObex() (*Obex, error) {
var o *Obex
conn, err := dbus.SessionBus()
if err != nil {
return nil, errors.Wrap(err, "unable to create dbus session bus:")
}
o = &Obex{
conn: conn,
Store: make(map[dbus.ObjectPath]ObexProperties),
}
return o, nil
}
// Close closes the OBEX connection.
func (o *Obex) Close() {
o.conn.Close()
}
// Conn returns the current SessionBus connection.
func (o *Obex) Conn() *dbus.Conn {
return o.conn
}
// CreateSession creates a new OBEX transfer session.
func (o *Obex) CreateSession(ctx context.Context, address string) (dbus.ObjectPath, error) {
var sessionPath dbus.ObjectPath
args := make(map[string]interface{})
args["Target"] = "opp"
session := o.CallClientAsync(ctx, "CreateSession", address, args)
select {
case <-ctx.Done():
return "", ctx.Err()
case call := <-session.Done:
if call.Err != nil {
return "", call.Err
}
if err := call.Store(&sessionPath); err != nil {
return "", err
}
}
sessionProperties, err := o.GetSessionProperties(sessionPath)
if err != nil {
return "", err
}
o.addSessionPropertiesToStore(sessionPath, sessionProperties)
return sessionPath, nil
}
// SendFile sends a file to the target device.
func (o *Obex) SendFile(sessionPath dbus.ObjectPath, path string) (dbus.ObjectPath, ObexTransferProperties, error) {
var transferPath dbus.ObjectPath
transferPropertyMap := make(map[string]dbus.Variant)
if err := o.CallObjectPush(sessionPath, "SendFile", path).Store(&transferPath, &transferPropertyMap); err != nil {
return "", ObexTransferProperties{}, err
}
transferProperties, err := o.GetTransferProperties(transferPropertyMap)
o.addTransferPropertiesToStore(transferPath, transferProperties)
return transferPath, transferProperties, err
}
// ReceiveFile returns a path where the OBEX daemon (obexd) will receive the file, along with
// the transfer properties.
func (o *Obex) ReceiveFile(sessionPath, transferPath dbus.ObjectPath) (string, string, ObexTransferProperties, error) {
var sessionProperty ObexSessionProperties
var transferProperty ObexTransferProperties
objMap, err := o.ManagedObjects()
if err != nil {
return "", "", ObexTransferProperties{}, err
}
for path, valueMap := range objMap {
switch path {
case sessionPath:
for iface, value := range valueMap {
if iface != dbusObexSessionIface {
continue
}
sessionProperty, err = o.GetSessionProperties(sessionPath, value)
if err != nil {
return "", "", ObexTransferProperties{}, errors.New("Session error")
}
o.addSessionPropertiesToStore(sessionPath, sessionProperty)
}
case transferPath:
for iface, value := range valueMap {
if iface != dbusObexTransferIface {
continue
}
transferProperty, err = o.GetTransferProperties(value)
if err != nil || transferProperty.Status == "error" {
return "", "", ObexTransferProperties{}, errors.New("Transfer error")
}
o.addTransferPropertiesToStore(transferPath, transferProperty)
}
}
}
if sessionProperty == (ObexSessionProperties{}) || transferProperty == (ObexTransferProperties{}) {
return "", "", ObexTransferProperties{}, errors.New("Cannot get obex properties")
}
return filepath.Join(sessionProperty.Root, transferProperty.Name), sessionProperty.Destination, transferProperty, nil
}
// CancelTransfer cancels the transfer.
func (o *Obex) CancelTransfer(transferPath dbus.ObjectPath) error {
return o.CallTransfer(transferPath, "Cancel").Store()
}
// SuspendTransfer suspends the transfer.
func (o *Obex) SuspendTransfer(transferPath dbus.ObjectPath) error {
return o.CallTransfer(transferPath, "Suspend").Store()
}
// ResumeTransfer resumes the transfer.
func (o *Obex) ResumeTransfer(transferPath dbus.ObjectPath) error {
return o.CallTransfer(transferPath, "Resume").Store()
}
// RemoveSession removes the OBEX transfer session and cancels any pending transfers.
func (o *Obex) RemoveSession(sessionPath dbus.ObjectPath) error {
o.removePropertiesFromStore(sessionPath)
return o.CallClient("RemoveSession", sessionPath).Store()
}
// GetSessionProperties converts a map of OBEX session properties to ObexSessionProperties.
func (o *Obex) GetSessionProperties(sessionPath dbus.ObjectPath, sprop ...map[string]dbus.Variant) (ObexSessionProperties, error) {
var sessionProperties ObexSessionProperties
props := make(map[string]dbus.Variant)
if sprop == nil {
if err := o.conn.Object(dbusObexName, sessionPath).Call(dbusPropertiesGetAllPath, 0, dbusObexSessionIface).Store(&props); err != nil {
return ObexSessionProperties{}, err
}
} else {
props = sprop[0]
}
return sessionProperties, DecodeVariantMap(props, &sessionProperties)
}
// GetTransferProperties converts a map of transfer properties to ObexTransferProperties.
func (o *Obex) GetTransferProperties(props map[string]dbus.Variant) (ObexTransferProperties, error) {
var obexTransferProperties ObexTransferProperties
return obexTransferProperties, DecodeVariantMap(props, &obexTransferProperties)
}
// ManagedObjects gets the currently managed objects from the OBEX DBus.
func (o *Obex) ManagedObjects() (map[dbus.ObjectPath]map[string]map[string]dbus.Variant, error) {
result := make(map[dbus.ObjectPath]map[string]map[string]dbus.Variant)
if err := o.conn.Object(dbusObexName, "/").Call(dbusObjectManagerPath, 0).Store(&result); err != nil {
return result, err
}
return result, nil
}
// WatchSignal will register a signal and watch for events from the OBEX DBus interface.
func (o *Obex) WatchSignal() chan *dbus.Signal {
signalMatch := "type='signal', sender='org.bluez.obex'"
o.conn.BusObject().Call("org.freedesktop.DBus.AddMatch", 0, signalMatch)
ch := make(chan *dbus.Signal, 1)
o.conn.Signal(ch)
return ch
}
// ParseSignalData parses OBEX DBus signal data.
func (o *Obex) ParseSignalData(signal *dbus.Signal) interface{} {
switch signal.Name {
case "org.freedesktop.DBus.Properties.PropertiesChanged":
objInterface, ok := signal.Body[0].(string)
if !ok {
return nil
}
objMap, ok := signal.Body[1].(map[string]dbus.Variant)
if !ok {
return nil
}
switch objInterface {
case dbusObexTransferIface:
sessionPath := dbus.ObjectPath(filepath.Dir(string(signal.Path)))
props, ok := o.getPropertiesFromStore(sessionPath)
if !ok {
break
}
for prop, value := range objMap {
switch prop {
case "Status":
props.TransferProperties.Status, _ = value.Value().(string)
case "Transferred":
props.TransferProperties.Transferred, _ = value.Value().(uint64)
}
}
o.addTransferPropertiesToStore(signal.Path, props.TransferProperties)
return props
}
}
return nil
}
// CallClient calls the Client1 interface with the provided method.
func (o *Obex) CallClient(method string, args ...interface{}) *dbus.Call {
return o.conn.Object(dbusObexName, dbusObexPath).Call(dbusObexClientIface+"."+method, 0, args...)
}
// CallClientAsync calls the Client1 interface asynchronously with the provided method.
func (o *Obex) CallClientAsync(ctx context.Context, method string, args ...interface{}) *dbus.Call {
return o.conn.Object(dbusObexName, dbusObexPath).GoWithContext(ctx, dbusObexClientIface+"."+method, 0, nil, args...)
}
// CallObjectPush calls the ObjectPush1 interface with the provided method.
func (o *Obex) CallObjectPush(sessionPath dbus.ObjectPath, method string, args ...interface{}) *dbus.Call {
return o.conn.Object(dbusObexName, sessionPath).Call(dbusObexObjectPushIface+"."+method, 0, args...)
}
// CallTransfer calls the Transfer1 interface with the provided method.
func (o *Obex) CallTransfer(transferPath dbus.ObjectPath, method string, args ...interface{}) *dbus.Call {
return o.conn.Object(dbusObexName, transferPath).Call(dbusObexTransferIface+"."+method, 0, args...)
}
// addSessionPropertiesToStore adds a session path and property to the store.
func (o *Obex) addSessionPropertiesToStore(sessionPath dbus.ObjectPath, sessionProperties ObexSessionProperties) {
o.StoreLock.Lock()
defer o.StoreLock.Unlock()
obexProperties := o.Store[sessionPath]
obexProperties.SessionPath = sessionPath
obexProperties.SessionProperties = sessionProperties
o.Store[sessionPath] = obexProperties
}
// addTransferPropertiesToStore adds a transfer path and property to the store.
func (o *Obex) addTransferPropertiesToStore(transferPath dbus.ObjectPath, transferProperties ObexTransferProperties) {
o.StoreLock.Lock()
defer o.StoreLock.Unlock()
transferObjectPath := dbus.ObjectPath(transferProperties.Session)
obexProperties := o.Store[transferObjectPath]
obexProperties.TransferPath = transferPath
obexProperties.TransferProperties = transferProperties
o.Store[transferObjectPath] = obexProperties
}
// getPropertiesFromStore gets the ObexProperties from the store.
func (o *Obex) getPropertiesFromStore(sessionPath dbus.ObjectPath) (ObexProperties, bool) {
o.StoreLock.Lock()
defer o.StoreLock.Unlock()
props, ok := o.Store[sessionPath]
return props, ok
}
// removePropertiesFromStore removes the session path and ObexProperties from the store.
func (o *Obex) removePropertiesFromStore(sessionPath dbus.ObjectPath) {
o.StoreLock.Lock()
defer o.StoreLock.Unlock()
delete(o.Store, sessionPath)
}