summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSMacOSX/coreBLE.m
blob: e764169a18fc39717ded4272199f5fa04fd2adce (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
/* -*- 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.
 */

#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 browse and registration hashes.
void updateBLEBeaconAndScan(serviceHash_t browseHash, serviceHash_t registeredHash)
{
    if (coreBLEptr == 0)
        coreBLEptr = [[coreBLE alloc] init];

    LogInfo("updateBLEBeaconAndScan: browseHash = 0x%x, registeredHash = 0x%x", browseHash, registeredHash);

    [coreBLEptr advertiseBrowses:browseHash andRegistrations: registeredHash];
    [coreBLEptr updateScan:(browseHash || registeredHash)];
}

// Stop the current BLE beacon.
void stopBLEBeacon(void)
{
    if (coreBLEptr == 0)
    {
        LogInfo("stopBLEBeacon called before BLE scan initialized ??");
        return;
    }

    LogInfo("stopBLEBeacon Stopping beacon");
    [coreBLEptr stopBeacon];
}

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

    NSData               *_currentlyAdvertisedData;

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

- (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;

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

    return self;
}

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

// TODO: 
// DBDeviceTypeBonjour should eventually be added to the DBDeviceType definitions in WirelessProximity
// 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) advertiseBrowses:(serviceHash_t) browseHash andRegistrations:(serviceHash_t) registeredHash
{
    uint8_t advertisingData[ADVERTISEMENTDATALENGTH] = {0, 0xff, 0x4c, 0x00 };
    uint8_t advertisingLength = 4;

    // TODO: If we have been transmitting a beacon, we probably want to continue transmitting
    // for a few seconds after both hashes are zero so that that any devices scanning 
    // can see the beacon indicating we have stopped all browses and advertisements.

    // Stop the beacon if there is no data to advertise.
    if (browseHash == 0 && registeredHash == 0)
    {
        LogInfo("advertiseBrowses:andRegistrations Stopping beacon in %d seconds", LastBeaconTime);
        if (mDNSStorage.NextBLEServiceTime)
            LogInfo("advertiseBrowses:andRegistrations: NextBLEServiceTime already set ??");

        mDNSStorage.NextBLEServiceTime = NonZeroTime(mDNS_TimeNow_NoLock(& mDNSStorage) + LastBeaconTime * mDNSPlatformOneSecond);
    }
    else
    {
        mDNSStorage.NextBLEServiceTime = 0;
    }

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

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

    memcpy(& advertisingData[advertisingLength], & browseHash, sizeof(serviceHash_t));
    advertisingLength += sizeof(serviceHash_t);
    memcpy(& advertisingData[advertisingLength], & registeredHash, 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(& mDNSStorage, 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 lenght byte.
    advertisingData[0] = (advertisingLength - 1);

    LogInfo("advertiseBrowses:andRegistrations advertisingLength = %d", advertisingLength);

    NSData* data = [NSData dataWithBytes:advertisingData length:advertisingLength];

    if([_peripheralManager isAdvertising] && [data isEqualToData: _currentlyAdvertisedData])
    {
        // No need to restart the advertisement if it is already active with the same data.
        LogInfo("advertiseBrowses:andRegistrations: No change in advertised data");
    }
    else
    {
        _currentlyAdvertisedData = data;

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

        [_peripheralManager startAdvertising:@{ CBAdvertisementDataAppleMfgData : _currentlyAdvertisedData, CBCentralManagerScanOptionIsPrivilegedDaemonKey : @YES, @"kCBAdvOptionUseFGInterval" : @YES }];
    }
}

- (void) stopBeacon
{
    [_peripheralManager stopAdvertising];
}

- (void) updateScan:(bool) start
{
    if (_isScanning)
    {
        if (!start)
        {
            LogInfo("updateScan: stopping scan");
            [_centralManager stopScan];
            _isScanning = NO;
        }
    }
    else
    {
        if (start)
        {
            LogInfo("updateScan: starting scan");

            _isScanning = YES;
            [_centralManager scanForPeripheralsWithServices:nil options:@{ CBCentralManagerScanOptionAllowDuplicatesKey : @NO }];
        }
    }
}

#pragma mark - CBCentralManagerDelegate protocol

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

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

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

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

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

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

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

// offset of beacon type in recieved CBAdvertisementDataManufacturerDataKey bytes
#define beaconTypeByteIndex 2

- (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];
   
    if (!data) {
        return;
    }

    unsigned char *bytes = (unsigned char *)data.bytes;
    
    // Just parse the DBDeviceTypeBonjour beacons.
    if (bytes[beaconTypeByteIndex] == DBDeviceTypeBonjour)
    {
        serviceHash_t browseHash, registeredHash;
        mDNSEthAddr senderMAC;
        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:
        // x4C, 0x0, 0x2A, flags_and_version_byte,, browseHash, advertisingServices_hash_bytes,
        // 6_bytes_of_sender_AWDL_MAC_address

        ptr = & bytes[beaconTypeByteIndex + 1];
        flagsAndVersion = *ptr++;
        memcpy(& browseHash, ptr, sizeof(serviceHash_t));
        ptr += sizeof(serviceHash_t);
        memcpy(& registeredHash, ptr, sizeof(serviceHash_t));
        ptr += sizeof(serviceHash_t);
        memcpy(& senderMAC, ptr, sizeof(senderMAC));

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

        responseReceived(browseHash, registeredHash, & senderMAC);

#if VERBOSE_BLE_DEBUG
        // Log every 4th package during debug
        static int pkgsIn = 0;

        if (((pkgsIn++) & 3) == 0)
        {
            LogInfo("0x%x 0x%x 0x%x 0x%x 0x%x", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4]);
//            LogInfo("0x%x 0x%x 0x%x 0x%x 0x%x", bytes[5], bytes[6], bytes[7], bytes[9], bytes[9]);
//            LogInfo("0x%x 0x%x 0x%x 0x%x 0x%x", bytes[10], bytes[11], bytes[12], bytes[13], bytes[14]);
//            LogInfo("0x%x 0x%x 0x%x 0x%x 0x%x", bytes[15], bytes[16], bytes[17], bytes[18], bytes[19]);
        }
#endif  // VERBOSE_BLE_DEBUG

    }
}

#pragma mark - CBPeripheralManagerDelegate protocol

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

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

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

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

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

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

        case CBPeripheralManagerStatePoweredOn:
            LogInfo("peripheralManagerDidUpdateState: CBPeripheralManagerStatePoweredOn");
            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