summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSMacOSX/coreBLE.m
blob: 8af8422cd4281cd994ca5aa4d9af9fd6b3bb8b0e (plain) (blame)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* -*- Mode: C; tab-width: 4 -*-
 *
 * Copyright (c) 2015-2016 Apple Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#if ENABLE_BLE_TRIGGERED_BONJOUR

#include "mDNSEmbeddedAPI.h"
#include "DNSCommon.h"

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>
#import <CoreBluetooth/CoreBluetooth_Private.h>
#import "mDNSMacOSX.h"
#import "BLE.h"
#import "coreBLE.h"

static coreBLE * coreBLEptr;

// Call Bluetooth subsystem to start/stop the the Bonjour BLE beacon and
// beacon scanning based on the current Bloom filter.
void updateBLEBeacon(serviceHash_t bloomFilter)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];

    LogInfo("updateBLEBeacon: bloomFilter = 0x%lx", bloomFilter);

    [coreBLEptr updateBeacon:bloomFilter];
}

// Stop the current BLE beacon.
void stopBLEBeacon(void)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];

    [coreBLEptr stopBeacon];
}

bool currentlyBeaconing(void)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];

    return [coreBLEptr isBeaconing];
}

// Start the scan.
void startBLEScan(void)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];
    [coreBLEptr startScan];
}

// Stop the scan.
void stopBLEScan(void)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];

    [coreBLEptr stopScan];
}

@implementation coreBLE
{
    CBCentralManager     *_centralManager;
    CBPeripheralManager  *_peripheralManager;

    NSData               *_currentlyAdvertisedData;

    // [_centralManager isScanning] is only available on iOS and not OSX,
    // so track scanning state locally.
    BOOL                 _isScanning;
    BOOL                 _centralManagerIsOn;
    BOOL                 _peripheralManagerIsOn;
}

- (id)init
{
    self = [super init];

    if (self)
    {
        _centralManager     = [[CBCentralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
        _peripheralManager  = [[CBPeripheralManager alloc] initWithDelegate:self queue:dispatch_get_main_queue()];
        _currentlyAdvertisedData = nil;
        _isScanning = NO;
        _centralManagerIsOn = NO;
        _peripheralManagerIsOn = NO;

        if (_centralManager == nil || _peripheralManager == nil )
        {
            LogMsg("coreBLE initialization failed!");
        } 
        else
        {
            LogInfo("coreBLE initialized");
        }
    }

    return self;
}

#define ADVERTISEMENTDATALENGTH 28 // 31 - 3 (3 bytes for flags)

// TODO: 
// Define DBDeviceTypeBonjour for prototyping until we move to the TDS beacon format.
// The Bluetooth team recommended using a value < 32 for prototyping, since 32 is the number of
// beacon types they can track in their duplicate beacon filtering logic.
#define DBDeviceTypeBonjour     26

// Beacon flags and version byte
#define BonjourBLEVersion     1

extern mDNS mDNSStorage;
extern mDNSInterfaceID AWDLInterfaceID;

// Transmit the last beacon indicating we are no longer advertising or browsing any services for two seconds.
#define LastBeaconTime 2

- (void) updateBeacon:(serviceHash_t) bloomFilter
{
    uint8_t advertisingData[ADVERTISEMENTDATALENGTH] = {0, 0xff, 0x4c, 0x00 };
    uint8_t advertisingLength = 4;

    // If no longer browsing or advertising, beacon this state for 'LastBeaconTime' seconds
    // so that peers have a chance to notice the state change.
    if (bloomFilter == 0)
    {
        LogInfo("updateBeacon: Stopping beacon in %d seconds", LastBeaconTime);

        if (mDNSStorage.timenow == 0)
        {
            // This should never happen since all calling code paths should have called mDNS_Lock(), which
            // initializes the mDNSStorage.timenow value.
            LogMsg("updateBeacon: NOTE, timenow == 0 ??");
        }

        mDNSStorage.NextBLEServiceTime = NonZeroTime(mDNSStorage.timenow + (LastBeaconTime * mDNSPlatformOneSecond));
        finalBeacon = true;
    }
    else
    {
        // Cancel any pending final beacon processing.
        finalBeacon = false;
    }

    // The beacon type.
    advertisingData[advertisingLength++] = DBDeviceTypeBonjour;

    // Flags and Version field
    advertisingData[advertisingLength++] = BonjourBLEVersion;

    memcpy(& advertisingData[advertisingLength], & bloomFilter, sizeof(serviceHash_t));
    advertisingLength += sizeof(serviceHash_t);

    // Add the MAC address of the awdl0 interface.  Don't cache it since
    // it can get updated periodically.
    if (AWDLInterfaceID)
    {
        NetworkInterfaceInfoOSX *intf = IfindexToInterfaceInfoOSX(AWDLInterfaceID);
        if (intf)
            memcpy(& advertisingData[advertisingLength], & intf->ifinfo.MAC, sizeof(mDNSEthAddr));
        else 
            memset( & advertisingData[advertisingLength], 0, sizeof(mDNSEthAddr));
    }
    else
    {
        // Just use zero if not avaiblable.
       memset( & advertisingData[advertisingLength], 0, sizeof(mDNSEthAddr));
    }
    advertisingLength += sizeof(mDNSEthAddr);

    // Total length of data advertised, minus this length byte.
    advertisingData[0] = (advertisingLength - 1);

    LogInfo("updateBeacon: advertisingLength = %d", advertisingLength);

    if (_currentlyAdvertisedData)
        [_currentlyAdvertisedData release];
    _currentlyAdvertisedData = [[NSData alloc] initWithBytes:advertisingData length:advertisingLength];
    [self startBeacon];
}

- (void) startBeacon
{
    if (!_peripheralManagerIsOn)
    {
        LogInfo("startBeacon: Not starting beacon, CBPeripheralManager not powered on");
        return;
    }

    if (_currentlyAdvertisedData == nil)
    {
        LogInfo("startBeacon: Not starting beacon, no data to advertise");
        return;
    }

    if ([_peripheralManager isAdvertising])
    {
        LogInfo("startBeacon: Stop current beacon transmission before restarting");
        [_peripheralManager stopAdvertising];
    }
    LogInfo("startBeacon: Starting beacon");

#if 0   // Move to this code during Fall 2018 develelopment if still using these APIs.
    [_peripheralManager startAdvertising:@{ CBAdvertisementDataAppleMfgData : _currentlyAdvertisedData, CBManagerIsPrivilegedDaemonKey : @YES, @"kCBAdvOptionUseFGInterval" : @YES }];
#else
    // While CBCentralManagerScanOptionIsPrivilegedDaemonKey is deprecated in current MobileBluetooth project, it's still defined in the current and
    // previous train SDKs.  Suppress deprecated warning for now since we intend to move to a different Bluetooth API to manage the BLE Triggered Bonjour 
    // beacons when this code is enabled by default.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    [_peripheralManager startAdvertising:@{ CBAdvertisementDataAppleMfgData : _currentlyAdvertisedData, CBCentralManagerScanOptionIsPrivilegedDaemonKey : @YES, @"kCBAdvOptionUseFGInterval" : @YES }];
#pragma GCC diagnostic pop
#endif
}

- (bool) isBeaconing
{
    return (_currentlyAdvertisedData != nil);
}

- (void) stopBeacon
{
    if (!_peripheralManagerIsOn)
    {
        LogInfo("stopBeacon: CBPeripheralManager is not powered on");
        return;
    }

    // Only beaconing if we have advertised data to send.
    if (_currentlyAdvertisedData)
    {
        LogInfo("stoptBeacon: Stopping beacon");
        [_peripheralManager stopAdvertising];
        [_currentlyAdvertisedData release];
        _currentlyAdvertisedData = nil;
    }
    else
        LogInfo("stoptBeacon: Note currently beaconing");
}

- (void) startScan
{
    if (!_centralManagerIsOn)
    {
        LogInfo("startScan: Not starting scan, CBCentralManager is not powered on");
        return;
    }

    if (_isScanning)
    {
        LogInfo("startScan: already scanning, stopping scan before restarting");
        [_centralManager stopScan];
    }

    LogInfo("startScan: Starting scan");

    _isScanning = YES;

#if 0   // Move to this code during Fall 2018 develelopment if still using these APIs.
    [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES , CBManagerIsPrivilegedDaemonKey : @YES}];
#else
    // While CBCentralManagerScanOptionIsPrivilegedDaemonKey is deprecated in current MobileBluetooth project, it's still defined in the current and
    // previous train SDKs.  Suppress deprecated warning for now since we intend to move to a different Bluetooth API to manage the BLE Triggered Bonjour 
    // beacons when this code is enabled by default.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
    [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @YES , CBCentralManagerScanOptionIsPrivilegedDaemonKey : @YES}];
#pragma GCC diagnostic pop
#endif
}

- (void) stopScan
{
    if (!_centralManagerIsOn)
    {
        LogInfo("stopScan: Not stopping scan, CBCentralManager is not powered on");
        return;
    }

    if (_isScanning)
    {
        LogInfo("stopScan: Stopping scan");
        [_centralManager stopScan];
        _isScanning = NO;
    }
    else
    {
        LogInfo("stopScan: Not currently scanning");
    }
}

#pragma mark - CBCentralManagerDelegate protocol

- (void)centralManagerDidUpdateState:(CBCentralManager *)central
{
    switch (central.state) {
        case CBManagerStateUnknown:
            LogInfo("centralManagerDidUpdateState: CBManagerStateUnknown");
            break;

        case CBManagerStateResetting:
            LogInfo("centralManagerDidUpdateState: CBManagerStateResetting");
            break;

        case CBManagerStateUnsupported:
            LogInfo("centralManagerDidUpdateState: CBManagerStateUnsupported");
            break;

        case CBManagerStateUnauthorized:
            LogInfo("centralManagerDidUpdateState: CBManagerStateUnauthorized");
            break;

        case CBManagerStatePoweredOff:
            LogInfo("centralManagerDidUpdateState: CBManagerStatePoweredOff");
            break;

        case CBManagerStatePoweredOn:
            // Hold lock to synchronize with main thread from this callback thread.
            KQueueLock();

            LogInfo("centralManagerDidUpdateState: CBManagerStatePoweredOn");
            _centralManagerIsOn = YES;
            // Only start scan if we have data we will be transmitting or if "suppressBeacons"
            // is set, indicating we should be scanning, but not beaconing.
            if (_currentlyAdvertisedData || suppressBeacons)
                [self startScan];
            else
                LogInfo("centralManagerDidUpdateState:: Not starting scan");

            KQueueUnlock("CBManagerStatePoweredOn");
            break;

        default:
            LogInfo("centralManagerDidUpdateState: Unknown state ??");
            break;
    }
}

#define beaconTypeByteIndex  2   // Offset of beacon type in received CBAdvertisementDataManufacturerDataKey byte array.
#define beaconDataLength    18  // Total number of bytes in the CBAdvertisementDataManufacturerDataKey.

- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *, id> *)advertisementData RSSI:(NSNumber *)RSSI
{
    (void) central;
    (void) peripheral;
    (void) RSSI;

    NSData *data = [advertisementData objectForKey:CBAdvertisementDataManufacturerDataKey];
   
    // Just return if the beacon data does not match what we are looking for.
    if (!data || ([data length] != beaconDataLength))
    {
        return;
    }

    unsigned char *bytes = (unsigned char *)data.bytes;
    
    // Just parse the DBDeviceTypeBonjour beacons.
    if (bytes[beaconTypeByteIndex] == DBDeviceTypeBonjour)
    {
        serviceHash_t peerBloomFilter;
        mDNSEthAddr   peerMAC;
        unsigned char flagsAndVersion;
        unsigned char *ptr;

#if VERBOSE_BLE_DEBUG
        LogInfo("didDiscoverPeripheral: received DBDeviceTypeBonjour beacon, length = %d", [data length]);
        LogInfo("didDiscoverPeripheral: central = 0x%x, peripheral = 0x%x", central, peripheral);
#endif // VERBOSE_BLE_DEBUG

        // The DBDeviceTypeBonjour beacon bytes will be:
        // 0x4C (1 byte), 0x0 (1 byte), DBDeviceTypeBonjour byte, flags and version byte, 8 byte Bloom filter,
        // 6 byte sender AWDL MAC address

        ptr = & bytes[beaconTypeByteIndex + 1];
        flagsAndVersion = *ptr++;
        memcpy(& peerBloomFilter, ptr, sizeof(serviceHash_t));
        ptr += sizeof(serviceHash_t);
        memcpy(& peerMAC, ptr, sizeof(peerMAC));

#if VERBOSE_BLE_DEBUG
        LogInfo("didDiscoverPeripheral: version = 0x%x, peerBloomFilter = 0x%x",
                flagsAndVersion, peerBloomFilter);
        LogInfo("didDiscoverPeripheral: sender MAC = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x",
            peerMAC.b[0], peerMAC.b[1], peerMAC.b[2], peerMAC.b[3], peerMAC.b[4], peerMAC.b[5]);
#else
        (void)flagsAndVersion; // Unused
#endif  // VERBOSE_BLE_DEBUG

        responseReceived(peerBloomFilter, & peerMAC);
    }
}

#pragma mark - CBPeripheralManagerDelegate protocol

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral
{

    switch (peripheral.state) {
        case CBManagerStateUnknown:
            LogInfo("peripheralManagerDidUpdateState: CBManagerStateUnknown");
            break;

        case CBManagerStateResetting:
            LogInfo("peripheralManagerDidUpdateState: CBManagerStateResetting");
            break;

        case CBManagerStateUnsupported:
            LogInfo("peripheralManagerDidUpdateState: CBManagerStateUnsupported");
            break;

        case CBManagerStateUnauthorized:
            LogInfo("peripheralManagerDidUpdateState: CBManagerStateUnauthorized");
            break;

        case CBManagerStatePoweredOff:
            LogInfo("peripheralManagerDidUpdateState: CBManagerStatePoweredOff");
            break;

        case CBManagerStatePoweredOn:
            // Hold lock to synchronize with main thread from this callback thread.
            KQueueLock();

            LogInfo("peripheralManagerDidUpdateState: CBManagerStatePoweredOn");
            _peripheralManagerIsOn = YES;

            // Start beaconing if we have initialized beacon data to send.
            if (_currentlyAdvertisedData)
                [self startBeacon];

            KQueueUnlock("CBManagerStatePoweredOn");
            break;

        default:
            LogInfo("peripheralManagerDidUpdateState: Unknown state ??");
            break;
    }
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(nullable NSError *)error
{
    (void) peripheral;

    if (error)
    {
        const char * errorString = [[error localizedDescription] cStringUsingEncoding:NSASCIIStringEncoding];
        LogInfo("peripheralManagerDidStartAdvertising: error = %s", errorString ? errorString: "unknown");
    }
    else
    {
        LogInfo("peripheralManagerDidStartAdvertising:");
    }
}

@end
#endif  // ENABLE_BLE_TRIGGERED_BONJOUR