forked from brltty/brltty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbluetooth_darwin.c
373 lines (303 loc) · 9 KB
/
bluetooth_darwin.c
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* BRLTTY - A background process providing access to the console screen (when in
* text mode) for a blind person using a refreshable braille display.
*
* Copyright (C) 1995-2022 by The BRLTTY Developers.
*
* BRLTTY comes with ABSOLUTELY NO WARRANTY.
*
* This is free software, placed under the terms of the
* GNU Lesser General Public License, as published by the Free Software
* Foundation; either version 2.1 of the License, or (at your option) any
* later version. Please see the file LICENSE-LGPL for details.
*
* Web Page: http://brltty.app/
*
* This software is maintained by Dave Mielke <[email protected]>.
*/
#include "prologue.h"
#include <string.h>
#include <errno.h>
#import <IOBluetooth/objc/IOBluetoothDevice.h>
#import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
#import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
#import <IOBluetooth/objc/IOBluetoothRFCOMMChannel.h>
#include "log.h"
#include "io_misc.h"
#include "io_bluetooth.h"
#include "bluetooth_internal.h"
#include "system_darwin.h"
@interface ServiceQueryResult: AsynchronousResult
- (void) sdpQueryComplete
: (IOBluetoothDevice *) device
status: (IOReturn) status;
@end
@interface BluetoothConnectionDelegate: AsynchronousTask
@property (assign) BluetoothConnectionExtension *bluetoothConnectionExtension;
@end
@interface RfcommChannelDelegate: BluetoothConnectionDelegate
- (void) rfcommChannelData
: (IOBluetoothRFCOMMChannel *) rfcommChannel
data: (void *) dataPointer
length: (size_t) dataLength;
- (void) rfcommChannelClosed
: (IOBluetoothRFCOMMChannel*) rfcommChannel;
- (IOReturn) run;
@end
struct BluetoothConnectionExtensionStruct {
BluetoothDeviceAddress bluetoothAddress;
IOBluetoothDevice *bluetoothDevice;
IOBluetoothRFCOMMChannel *rfcommChannel;
RfcommChannelDelegate *rfcommDelegate;
int inputPipe[2];
};
static void
bthSetError (IOReturn result, const char *action) {
setDarwinSystemError(result);
logSystemError(action);
}
static void
bthInitializeRfcommChannel (BluetoothConnectionExtension *bcx) {
bcx->rfcommChannel = nil;
}
static void
bthDestroyRfcommChannel (BluetoothConnectionExtension *bcx) {
if (bcx->rfcommChannel) {
[bcx->rfcommChannel closeChannel];
[bcx->rfcommChannel release];
bthInitializeRfcommChannel(bcx);
}
}
static void
bthInitializeRfcommDelegate (BluetoothConnectionExtension *bcx) {
bcx->rfcommDelegate = nil;
}
static void
bthDestroyRfcommDelegate (BluetoothConnectionExtension *bcx) {
if (bcx->rfcommDelegate) {
[bcx->rfcommDelegate stop];
[bcx->rfcommDelegate wait:5];
[bcx->rfcommDelegate release];
bthInitializeRfcommDelegate(bcx);
}
}
static void
bthInitializeBluetoothDevice (BluetoothConnectionExtension *bcx) {
bcx->bluetoothDevice = nil;
}
static void
bthDestroyBluetoothDevice (BluetoothConnectionExtension *bcx) {
if (bcx->bluetoothDevice) {
[bcx->bluetoothDevice closeConnection];
[bcx->bluetoothDevice release];
bthInitializeBluetoothDevice(bcx);
}
}
static void
bthInitializeInputPipe (BluetoothConnectionExtension *bcx) {
bcx->inputPipe[0] = bcx->inputPipe[1] = INVALID_FILE_DESCRIPTOR;
}
static void
bthDestroyInputPipe (BluetoothConnectionExtension *bcx) {
int *fileDescriptor = bcx->inputPipe;
const int *end = fileDescriptor + ARRAY_COUNT(bcx->inputPipe);
while (fileDescriptor < end) {
closeFile(fileDescriptor);
fileDescriptor += 1;
}
}
static void
bthMakeAddress (BluetoothDeviceAddress *address, uint64_t bda) {
unsigned int index = sizeof(address->data);
while (index > 0) {
address->data[--index] = bda & 0XFF;
bda >>= 8;
}
}
BluetoothConnectionExtension *
bthNewConnectionExtension (uint64_t bda) {
BluetoothConnectionExtension *bcx;
if ((bcx = malloc(sizeof(*bcx)))) {
memset(bcx, 0, sizeof(*bcx));
bthInitializeInputPipe(bcx);
bthMakeAddress(&bcx->bluetoothAddress, bda);
if ((bcx->bluetoothDevice = [IOBluetoothDevice deviceWithAddress:&bcx->bluetoothAddress])) {
[bcx->bluetoothDevice retain];
return bcx;
}
free(bcx);
} else {
logMallocError();
}
return NULL;
}
void
bthReleaseConnectionExtension (BluetoothConnectionExtension *bcx) {
bthDestroyRfcommChannel(bcx);
bthDestroyRfcommDelegate(bcx);
bthDestroyBluetoothDevice(bcx);
bthDestroyInputPipe(bcx);
free(bcx);
}
int
bthOpenChannel (BluetoothConnectionExtension *bcx, uint8_t channel, int timeout) {
IOReturn result;
if (pipe(bcx->inputPipe) != -1) {
if (setBlockingIo(bcx->inputPipe[0], 0)) {
if ((bcx->rfcommDelegate = [RfcommChannelDelegate new])) {
bcx->rfcommDelegate.bluetoothConnectionExtension = bcx;
if ((result = [bcx->bluetoothDevice openRFCOMMChannelSync:&bcx->rfcommChannel withChannelID:channel delegate:nil]) == kIOReturnSuccess) {
if ([bcx->rfcommDelegate start]) {
return 1;
}
bthDestroyRfcommChannel(bcx);
} else {
bthSetError(result, "RFCOMM channel open");
}
bthDestroyRfcommDelegate(bcx);
}
}
bthDestroyInputPipe(bcx);
} else {
logSystemError("pipe");
}
return 0;
}
static int
bthPerformServiceQuery (BluetoothConnectionExtension *bcx) {
int ok = 0;
IOReturn result;
ServiceQueryResult *target = [ServiceQueryResult new];
if (target) {
if ((result = [bcx->bluetoothDevice performSDPQuery:target]) == kIOReturnSuccess) {
if ([target wait:10]) {
if ((result = target.finalStatus) == kIOReturnSuccess) {
ok = 1;
} else {
bthSetError(result, "service discovery response");
}
}
} else {
bthSetError(result, "service discovery request");
}
[target release];
}
return ok;
}
int
bthDiscoverChannel (
uint8_t *channel, BluetoothConnectionExtension *bcx,
const void *uuidBytes, size_t uuidLength,
int timeout
) {
IOReturn result;
if (bthPerformServiceQuery(bcx)) {
IOBluetoothSDPUUID *uuid = [IOBluetoothSDPUUID uuidWithBytes:uuidBytes length:uuidLength];
if (uuid) {
IOBluetoothSDPServiceRecord *record = [bcx->bluetoothDevice getServiceRecordForUUID:uuid];
if (record) {
if ((result = [record getRFCOMMChannelID:channel]) == kIOReturnSuccess) {
return 1;
} else {
bthSetError(result, "RFCOMM channel lookup");
}
}
}
}
return 0;
}
int
bthMonitorInput (BluetoothConnection *connection, AsyncMonitorCallback *callback, void *data) {
return 0;
}
int
bthPollInput (BluetoothConnectionExtension *bcx, int timeout) {
return awaitFileInput(bcx->inputPipe[0], timeout);
}
ssize_t
bthGetData (
BluetoothConnectionExtension *bcx, void *buffer, size_t size,
int initialTimeout, int subsequentTimeout
) {
return readFile(bcx->inputPipe[0], buffer, size, initialTimeout, subsequentTimeout);
}
ssize_t
bthPutData (BluetoothConnectionExtension *bcx, const void *buffer, size_t size) {
IOReturn result = [bcx->rfcommChannel writeSync:(void *)buffer length:size];
if (result == kIOReturnSuccess) return size;
bthSetError(result, "RFCOMM channel write");
return -1;
}
char *
bthObtainDeviceName (uint64_t bda, int timeout) {
IOReturn result;
BluetoothDeviceAddress address;
bthMakeAddress(&address, bda);
{
IOBluetoothDevice *device = [IOBluetoothDevice deviceWithAddress:&address];
if (device != nil) {
if ((result = [device remoteNameRequest:nil]) == kIOReturnSuccess) {
NSString *nsName = device.name;
if (nsName != nil) {
const char *utf8Name = [nsName UTF8String];
if (utf8Name != NULL) {
char *name = strdup(utf8Name);
if (name != NULL) {
return name;
}
}
}
} else {
bthSetError(result, "device name query");
}
[device closeConnection];
}
}
return NULL;
}
@implementation ServiceQueryResult
- (void) sdpQueryComplete
: (IOBluetoothDevice *) device
status: (IOReturn) status
{
[self setStatus:status];
}
@end
@implementation BluetoothConnectionDelegate
@synthesize bluetoothConnectionExtension;
@end
@implementation RfcommChannelDelegate
- (void) rfcommChannelData
: (IOBluetoothRFCOMMChannel *) rfcommChannel
data: (void *) dataPointer
length: (size_t) dataLength
{
writeFile(self.bluetoothConnectionExtension->inputPipe[1], dataPointer, dataLength);
}
- (void) rfcommChannelClosed
: (IOBluetoothRFCOMMChannel*) rfcommChannel
{
logMessage(LOG_NOTICE, "RFCOMM channel closed");
}
- (IOReturn) run
{
IOReturn result;
logMessage(LOG_CATEGORY(BLUETOOTH_IO), "RFCOMM channel delegate started");
{
BluetoothConnectionExtension *bcx = self.bluetoothConnectionExtension;
if ((result = [bcx->rfcommChannel setDelegate:self]) == kIOReturnSuccess) {
CFRunLoopRun();
result = kIOReturnSuccess;
} else {
bthSetError(result, "RFCOMM channel delegate set");
}
}
logMessage(LOG_CATEGORY(BLUETOOTH_IO), "RFCOMM channel delegate finished");
return result;
}
@end
void
bthProcessDiscoveredDevices (
DiscoveredBluetoothDeviceTester *testDevice, void *data
) {
}