-
Notifications
You must be signed in to change notification settings - Fork 1
/
Unity3D_BLENativePeripheral.m
318 lines (279 loc) · 9.49 KB
/
Unity3D_BLENativePeripheral.m
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
/*
* Unity3D_BLE — A Unity3D desktop plugin for BlueTooth Low Energy
*
* Created by Pekka Nikander <pekka.nikander@iki.fi> on 08/08/2018.
* Placed in public domain.
*/
#import "Unity3D_BLENativePeripheral.h"
#import <CoreBluetooth/CoreBluetooth.h>
#include <assert.h>
#include <stdio.h>
@interface BLENativePeripheral () <CBPeripheralDelegate>
@end
@implementation BLENativePeripheral
- (id)initWith: (CBPeripheral *)cbperipheral
{
self = [super init];
NSLog(@"BLENativePeripheral init: %@ id: %@", self,
[[cbperipheral identifier] UUIDString]);
if (self) {
self->cbperipheral = cbperipheral;
[cbperipheral retain];
self->createCount = 0;
self->service = NULL;
self->cs_context = NULL;
self->cbservice = NULL;
self->cs_connected = NO;
self->characteristics
= [NSMutableDictionary dictionaryWithCapacity: 1];
[self->characteristics retain];
}
return self;
}
- (void)dealloc
{
NSLog(@"BLENativePeripheral dealloc: %@", self);
if (self->cbperipheral) {
[self->cbperipheral release];
self->cbperipheral = NULL;
}
if (self->service) {
[self->service release];
self->service = NULL;
}
self->cs_context = NULL;
if (self->cbservice) {
[self->cbservice release];
self->cbservice = NULL;
}
if (self->characteristics) {
[self->characteristics release];
self->characteristics = NULL;
}
[super dealloc];
}
- (void)tryLocateMyServiceWithDiscovery: (bool) withDiscovery
{
NSLog(@"Trying to locate my service");
if (NULL == self->cbperipheral) {
NSLog(@"No CBPeripheral yet, giving up");
return;
}
NSArray<CBService *> *services = [self->cbperipheral services];
for (CBService *cbservice in services) {
NSLog(@"Considering service %@", service);
if ([self->service isEqual: cbservice.UUID]) {
NSLog(@"Adapting service %@", cbservice);
if (self->cbservice) {
[self->cbservice release];
self->cbservice = NULL;
}
[cbservice retain]; // Released in SetService or dealloc
self->cbservice = cbservice;
if (characteristics) {
NSArray<CBUUID *> *uuids = [characteristics allKeys];
NSLog(@"Discovering characteristics %@", uuids);
[cbperipheral discoverCharacteristics: uuids
forService: cbservice];
}
}
return;
}
if (withDiscovery) {
/* Not found, kick a discovery if connected. */
if (self->cbperipheral.state == CBPeripheralStateConnected) {
NSLog(@"Discovering %@ for service %@", self, self->service);
[self->cbperipheral discoverServices: @[self->service]];
}
}
}
- (void) peripheral:(CBPeripheral *)cbperipheral
didDiscoverServices:(NSError *)error
{
assert(cbperipheral == self->cbperipheral);
[self tryLocateMyServiceWithDiscovery: NO];
}
- (void) peripheral:(CBPeripheral *)cbperipheral
didModifyServices:(NSArray<CBService *> *)invalidatedServices
{
assert(cbperipheral == self->cbperipheral);
NSLog(@"Discovering service %@ for peripheral %@", service, cbperipheral);
if (service) {
[cbperipheral discoverServices: @[service]];
}
}
- (void) peripheral:(CBPeripheral *)cbperipheral
didDiscoverCharacteristicsForService:(CBService *)service
error:(NSError *)error
{
NSLog(@"Discovered characteristics");
assert(cbperipheral == self->cbperipheral);
if (error) {
NSLog(@"Error discovering characteristics: %@", error);
return;
}
for (CBCharacteristic *sercha in service.characteristics) {
for (CBUUID *mychauuid in [self->characteristics allKeys]) {
if ([mychauuid isEqual: sercha.UUID]) {
NSLog(@"Subscribing(1) to characteristic %@", sercha);
[cbperipheral setNotifyValue: YES forCharacteristic: sercha];
}
}
}
}
- (void) peripheral:(CBPeripheral * )cbperipheral
didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError * )error
{
if (error) {
NSLog(@"Error changing notification state: %@", error);
return;
}
NSLog(@"Notification state changed for %@", characteristic);
}
- (void) peripheral:(CBPeripheral * )cbperipheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError * )error {
NSData *data = characteristic.value;
CBUUID *UUID = [characteristic UUID];
NSValue *nscallback = characteristics[UUID];
if (nscallback) {
const BLENativeCharacteristicUpdatedCallback callback
= (BLENativeCharacteristicUpdatedCallback)
[nscallback pointerValue];
if (callback) {
const char *uuid = [[UUID UUIDString] UTF8String];
char *uuid2 = strdup(uuid);
NSLog(@"callback=%p, context=%p, uuid=%s", callback, cs_context, uuid2);
callback(cs_context, uuid2, [data bytes]);
free(uuid2);
}
}
}
@end
/*
* Unity3D plain C interface
*/
BLENativePeripheral *BLENativeCreatePeripheral(void *cbp)
{
CBPeripheral *cbperipheral = (CBPeripheral *)cbp;
[cbperipheral retain];
if (cbperipheral.delegate) {
BLENativePeripheral *this = (BLENativePeripheral *)cbperipheral.delegate;
[this retain];
NSLog(@"BLENativeCreatePeripheral: Already found: %p", this);
assert(this->createCount > 0);
this->createCount++;
return this;
} else {
BLENativePeripheral *this = [[BLENativePeripheral alloc]
initWith: cbperipheral];
assert(this->createCount == 0);
this->createCount++;
cbperipheral.delegate = this;
NSLog(@"BLENativeCreatePeripheral: Created: %p", this);
return this;
}
}
void BLENativePeripheralRelease(BLENativePeripheral *this)
{
NSLog(@"BLENativePeripheralRelease: %p", this);
assert(this->cbperipheral.delegate == this || this->cbperipheral.delegate == nil);
assert(this->createCount > 0);
this->createCount--;
if (0 == this->createCount) {
assert (this->cbperipheral.delegate);
this->cbperipheral.delegate = nil;
}
[this release]; // For alloc or retain in BLECreatePeripheral
}
void BLENativePeripheralGetIdentifier(
BLENativePeripheral *this, char *identifier, int len)
{
NSString *UUID;
if (this->cbperipheral) {
UUID = [[this->cbperipheral identifier] UUIDString];
} else {
UUID = @"";
}
[UUID getCString: identifier /* [out] */
maxLength: len
encoding: NSASCIIStringEncoding];
}
void BLENativePeripheralGetName(
BLENativePeripheral *this, char *name, int len)
{
NSString *nsname;
if (this->cbperipheral) {
nsname = [this->cbperipheral name];
} else {
nsname = @"";
}
[nsname getCString: name /* [out] */
maxLength: len
encoding: NSASCIIStringEncoding];
}
/**
* XXX
*
*/
void BLENativePeripheralSetService(
BLENativePeripheral *this, const char *servicestring, const void *cs_context)
{
NSLog(@"Configuring peripheral %@ service: %s", this, servicestring);
NSString *nsservice =
[NSString stringWithCString: servicestring
encoding: NSUTF8StringEncoding];
if (this->cbservice) {
[this->cbservice release]; // Retained in tryDiscoveryMyServices
this->cbservice = NULL;
}
if (this->service) {
[this->service release];
this->service = NULL;
}
this->service = [CBUUID UUIDWithString: nsservice];
[this->service retain]; // Released above in if or in dealloc
this->cs_context = cs_context;
/*
* If there is already a CoreBluetooth peripheral,
* initiate service discovery if needed.
*/
[this tryLocateMyServiceWithDiscovery: YES];
}
void BLENativePeripheralAddCharacteristic(
BLENativePeripheral *this,
const char *uuidstring,
BLENativeCharacteristicUpdatedCallback callback)
{
assert(this->characteristics);
NSString *nsstring = [NSString stringWithCString: uuidstring
encoding: NSUTF8StringEncoding];
CBUUID *uuid = [CBUUID UUIDWithString: nsstring];
if (!uuid) {
NSLog(@"Malformed UUID %@", nsstring);
return;
}
NSValue *object = [NSValue valueWithPointer: callback];
[uuid retain];
NSLog(@"Adding characteristic %s to peripheral %@", uuidstring, this);
NSLog(@"callback=%p", callback);
[this->characteristics setObject: object forKey: uuid];
NSLog(@"Checking subscription");
[uuid release];
if (NULL != this->cbperipheral && NULL != this->cbservice) {
if (this->cbperipheral.state == CBPeripheralStateConnected) {
NSLog(@"Subscribing(2) peripheral %@ to characteristic %@",
this, uuid);
NSArray<CBUUID *> *uuids = [this->characteristics allKeys];
[this->cbperipheral
discoverCharacteristics: uuids
forService: this->cbservice];
}
}
}
void BLENativePeripheralRemoveCharacteristic(
BLENativePeripheral *this, const char *chastrinfg)
{
// XXX TBD
}