summaryrefslogtreecommitdiff
path: root/chromium/third_party/nearby/src/internal/platform/implementation/ios/Mediums/Ble/Sockets/Source/Peripheral/GNSPeripheralManager.m
blob: 38d549c7ef1a619d0cc100c539406409b0012a62 (plain)
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2020 Google LLC
//
// 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
//
//     https://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.

#import "internal/platform/implementation/ios/Mediums/Ble/Sockets/Source/Peripheral/GNSPeripheralManager+Private.h"

#import "internal/platform/implementation/ios/Mediums/Ble/Sockets/Source/Peripheral/GNSPeripheralServiceManager+Private.h"
#import "internal/platform/implementation/ios/Mediums/Ble/Sockets/Source/Shared/GNSSocket+Private.h"
#import "GoogleToolboxForMac/GTMLogger.h"

#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif

static NSString *CBManagerStateString(CBManagerState state) {
  switch (state) {
    case CBManagerStateUnknown:
      return @"CBManagerStateUnknown";
    case CBManagerStateResetting:
      return @"CBManagerStateResetting";
    case CBManagerStateUnsupported:
      return @"CBManagerStateUnsupported";
    case CBManagerStateUnauthorized:
      return @"CBManagerStateUnauthorized";
    case CBManagerStatePoweredOff:
      return @"CBManagerStatePoweredOff";
    case CBManagerStatePoweredOn:
      return @"CBManagerStatePoweredOn";
  }
  return @"CBManagerState Unknown";
}

// http://b/28875581 On iOS, under some misterious conditions, the Bluetooth daemon (BTServer
// process) continously crashes when attempting to register Bluetooth services. In fact, if the
// BTServer process is killed, the OS spins a new instace of this process after 10 seconds.
//
// It is defined as a resetting episode a set of successive state changes of a CBPeripheralManager
// that include at least one state CBManagerStateResetting.
// As a heuristic, a resetting episode is considered a crash loop of the Bluetooth daemon iff:
// * the CBPeripheralManager enters more than 5 times in CBManagerStateResetting;
// * the time interval between successive CBManagerStateResetting notifications is
//   less than 15 seconds;
//
// The minimum number of successive CBManagerStateResetting states for a resetting
// episode to be considered a Bluetooth daemon crash loop.
static NSInteger gKBTCrashLoopMinNumberOfResetting = 5;

// The maximum time interval between successive CBManagerStateResetting states for a
// resetting episode to be considered a Bluetooth daemon crash loop.
static NSTimeInterval gKBTCrashLoopMaxTimeBetweenResetting = 15.f;

@interface GNSPeripheralManager () {
  // key: -[GNSPeripheralServiceManager serviceUUID], value: GNSPeripheralServiceManager
  NSMapTable *_peripheralServiceManagers;
  // Queue of blocks to update BLE values
  NSMutableDictionary<NSUUID *, NSMutableArray<GNSUpdateValueHandler> *>
      *_handlerQueuePerSocketIdentifier;
  NSDictionary<NSString *, id> *_advertisementInProgressData;
  NSDictionary<NSString *, id> *_advertisementData;
  dispatch_queue_t _queue;

  // Bluetooth daemon crash info.
  NSDate *_btCrashLastResettingDate;
  NSInteger _btCrashCount;
}

#if TARGET_OS_IPHONE
@property(nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskId;
#endif

@property(nonatomic, getter=isStarted) BOOL started;

@end

@implementation GNSPeripheralManager

- (instancetype)init {
  [self doesNotRecognizeSelector:_cmd];
  return nil;
}

- (instancetype)initWithAdvertisedName:(NSString *)advertisedName
                     restoreIdentifier:(NSString *)restoreIdentifier
                                 queue:(dispatch_queue_t)queue {
  self = [super init];
  if (self) {
    _restoreIdentifier = [restoreIdentifier copy];
    _advertisedName = [advertisedName copy];
    _handlerQueuePerSocketIdentifier = [NSMutableDictionary dictionary];
    _peripheralServiceManagers = [NSMapTable strongToWeakObjectsMapTable];
    _btCrashLastResettingDate = [NSDate dateWithTimeIntervalSince1970:0];
    _queue = queue;

#if TARGET_OS_IPHONE
    _backgroundTaskId = UIBackgroundTaskInvalid;
#endif
  }
  return self;
}

- (instancetype)initWithAdvertisedName:(NSString *)advertisedName
                     restoreIdentifier:(NSString *)restoreIdentifier {
  return [self initWithAdvertisedName:advertisedName
                    restoreIdentifier:restoreIdentifier
                                queue:dispatch_get_main_queue()];
}

- (void)dealloc {
  [self stop];
}

- (void)addPeripheralServiceManager:(GNSPeripheralServiceManager *)peripheralServiceManager
          bleServiceAddedCompletion:(GNSErrorHandler)completion {
  [_peripheralServiceManagers setObject:peripheralServiceManager
                                 forKey:peripheralServiceManager.serviceUUID];
  [peripheralServiceManager addedToPeripheralManager:self bleServiceAddedCompletion:completion];
  if (_started) {
    [self addBleServiceForServiceManager:peripheralServiceManager];
  }
  // Update all advertised services to make sure that the right services are advertised in case
  // all BLE services were already added.
  [self updateAdvertisedServices];
}

- (void)start {
  if (_started) {
    GTMLoggerInfo(@"Peripheral manager already started.");
    return;
  }
  NSMutableDictionary<NSString *, id> *options =
      [@{CBCentralManagerOptionShowPowerAlertKey : @NO} mutableCopy];
#if TARGET_OS_IPHONE
  // Restored API only supported on iOS.
  if (_restoreIdentifier) {
    options[CBPeripheralManagerOptionRestoreIdentifierKey] = _restoreIdentifier;
  }
#endif
  _cbPeripheralManager = [self cbPeripheralManagerWithDelegate:self queue:_queue options:options];
  GTMLoggerInfo(@"Peripheral manager started.");
  _started = YES;
  // From Apple documentation |-[GNSPeripheralManager peripheralManagerDidUpdateState:]| will be
  // called no matter the current bluetooth state. Also, if the CBPeripheralManager is being
  // restored by iOS, |-[GNSPeripheralManager peripheralManager:willRestoreState:]| will be called
  // before. Therefore, adding and advertising services have to be done in
  // |peripheralManagerDidUpdateState:| method, so that the restored services will not be added
  // (if there are restored services).
}

- (void)stop {
  if (!_started) {
    GTMLoggerInfo(@"Peripheral manager already stopped.");
    return;
  }
  GTMLoggerInfo(@"Peripheral manager stopped.");
  _started = NO;
  [self removeAllBleServicesAndStopAdvertising];
  _cbPeripheralManager.delegate = nil;
  _cbPeripheralManager = nil;
}

- (NSString *)description {
  return [NSString stringWithFormat:@"<%@ %p: started %@, services %@>", self.class, self,
                                    _started ? @"YES" : @"NO",
                                    _peripheralServiceManagers.keyEnumerator];
}

- (void)addBleServiceForServiceManager:(GNSPeripheralServiceManager *)peripheralServiceManager {
  if (peripheralServiceManager.cbServiceState != GNSBluetoothServiceStateNotAdded) {
    // Ignored if the peripheralServiceManager.cbService was already added.
    return;
  }
  [peripheralServiceManager willAddCBService];
  NSAssert(peripheralServiceManager.cbService, @"%@ had no cbService", peripheralServiceManager);
  [_cbPeripheralManager addService:peripheralServiceManager.cbService];
}

- (void)addAllBleServicesAndStartAdvertising {
  // Do not attempt to add any services if the Bluetooth Daemon is in a crash loop.
  if ([self isBTCrashLoop]) {
    // http://b/28875581 Adding new service when Bluetooth is powered off may be the reason for
    // the Bluetooth Daemon crash loop. Avoid adding any BLE services here and wait for the next
    // state change of the |_cbPeripheralManager|.
    GTMLoggerError(@"Bluetooth Daemon crash loop detected crashing. Avoid adding BLE services.");
    return;
  }

#if TARGET_OS_IPHONE
  if (_backgroundTaskId == UIBackgroundTaskInvalid) {
    GTMLoggerInfo(@"Start background task to add BLE services and start advertising.");
    _backgroundTaskId = [[UIApplication sharedApplication]
        beginBackgroundTaskWithName:@"Add BLE services and start advertising"
                  expirationHandler:^{
                    GTMLoggerError(@"Application was suspended before add BLE services and start "
                                   @"advertising finished.");
                    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskId];
                    _backgroundTaskId = UIBackgroundTaskInvalid;
                  }];
  }
#endif

  for (GNSPeripheralServiceManager *peripheralServiceManager in _peripheralServiceManagers
           .objectEnumerator) {
    [self addBleServiceForServiceManager:peripheralServiceManager];
  }
  // Update all advertised services to make sure that the right services are advertised in case all
  // BLE services were already added.
  [self updateAdvertisedServices];
}

- (void)removeAllBleServicesAndStopAdvertising {
  [_cbPeripheralManager stopAdvertising];
  _advertisementInProgressData = nil;
  _advertisementData = nil;

  // Remove services and inform all service managers that their service was removed.
  [_cbPeripheralManager removeAllServices];
  for (GNSPeripheralServiceManager *peripheralServiceManager in _peripheralServiceManagers
           .objectEnumerator) {
    [peripheralServiceManager didRemoveCBService];
  }
}

- (void)updateAdvertisedServices {
  if (!_started) {
    // Do not start advertising if this peripheral manager is stopped.
    return;
  }

  CBManagerState cbState = _cbPeripheralManager.state;
  if ((cbState != CBManagerStatePoweredOn) && (cbState != CBManagerStatePoweredOff)) {
    GTMLoggerInfo(@"Do not start advertising on state %@", CBManagerStateString(cbState));
    return;
  }
  if (_advertisementInProgressData) {
    GTMLoggerInfo(@"Another start advertising operation is in progress.");
    return;
  }
  if (!_cbPeripheralManager.isAdvertising) {
    GTMLoggerInfo(@"Reset the advertiment data as the peripheral is not advertising.");
    _advertisementData = nil;
  }

  BOOL allServicesAdded = YES;
  NSMutableArray<CBUUID *> *serviceUUIDsToAdvertise = [NSMutableArray array];
  for (GNSPeripheralServiceManager *peripheralServiceManager in _peripheralServiceManagers
           .objectEnumerator) {
    if (peripheralServiceManager.cbServiceState != GNSBluetoothServiceStateAdded) {
      allServicesAdded = NO;
      break;
    }
    if (peripheralServiceManager.isAdvertising) {
      [serviceUUIDsToAdvertise addObject:peripheralServiceManager.serviceUUID];
    }
  }
  if (!allServicesAdded) {
    GTMLoggerInfo(@"Do not start advertising as not all bluetooth services are added.");
    return;
  }
  NSMutableDictionary<NSString *, id> *advertisementData = [NSMutableDictionary dictionary];
  advertisementData[CBAdvertisementDataServiceUUIDsKey] = serviceUUIDsToAdvertise;
  if (_advertisedName) {
    advertisementData[CBAdvertisementDataLocalNameKey] = _advertisedName;
  }
  if ([advertisementData isEqual:_advertisementData]) {
    GTMLoggerInfo(
        @"Finished adding BLE services and starting advertising (advertisement data = %@).",
        _advertisementData);
#if TARGET_OS_IPHONE
    [[UIApplication sharedApplication] endBackgroundTask:_backgroundTaskId];
    _backgroundTaskId = UIBackgroundTaskInvalid;
#endif
    return;
  }

  GTMLoggerInfo(@"Start advertising: %@", advertisementData);
  _advertisementInProgressData = [advertisementData copy];
  [_cbPeripheralManager stopAdvertising];
  [_cbPeripheralManager startAdvertising:_advertisementInProgressData];
}

#pragma mark - Private

- (void)updateOutgoingCharOnSocket:(GNSSocket *)socket withHandler:(GNSUpdateValueHandler)handler {
  NSUUID *socketIdentifier = socket.socketIdentifier;
  NSMutableArray<GNSUpdateValueHandler> *handlerQueue =
      _handlerQueuePerSocketIdentifier[socketIdentifier];
  if (!handlerQueue) {
    if (handler()) return;
    handlerQueue = [NSMutableArray array];
    _handlerQueuePerSocketIdentifier[socketIdentifier] = handlerQueue;
    GTMLoggerInfo(@"Queueing value for socket: %@", socketIdentifier);
  }
  [handlerQueue addObject:[handler copy]];
}

- (void)processUpdateValueBlocks {
  GTMLoggerInfo(@"About to send values for sockets: %@", _handlerQueuePerSocketIdentifier.allKeys);
  for (NSUUID *socketIdentifier in _handlerQueuePerSocketIdentifier.allKeys) {
    NSMutableArray<GNSUpdateValueHandler> *handlerQueue =
        _handlerQueuePerSocketIdentifier[socketIdentifier];
    GTMLoggerInfo(@"%lu queued values to send.", (unsigned long)handlerQueue.count);
    while (handlerQueue.count > 0) {
      GNSUpdateValueHandler handler = handlerQueue[0];
      if (!handler()) {
        GTMLoggerInfo(@"Value failed to be send, still in the queue to try later.");
        break;
      }
      GTMLoggerInfo(@"Value sent successfully.");
      [handlerQueue removeObjectAtIndex:0];
      if (handlerQueue.count == 0) {
        GTMLoggerInfo(@"Removing pending value queue for socket: %@", socketIdentifier);
        [_handlerQueuePerSocketIdentifier removeObjectForKey:socketIdentifier];
      }
    }
  }
}

- (BOOL)updateOutgoingCharacteristic:(NSData *)data onSocket:(GNSSocket *)socket {
  GNSPeripheralServiceManager *peripheralServiceManager = socket.owner;
  NSAssert(peripheralServiceManager, @"%@ should have an owner.", socket);
  return [_cbPeripheralManager updateValue:data
                         forCharacteristic:peripheralServiceManager.weaveOutgoingCharacteristic
                      onSubscribedCentrals:@[ socket.peerAsCentral ]];
}

- (CBPeripheralManager *)cbPeripheralManagerWithDelegate:(id<CBPeripheralManagerDelegate>)delegate
                                                   queue:(dispatch_queue_t)queue
                                                 options:(NSDictionary<NSString *, id> *)options {
  return [[CBPeripheralManager alloc] initWithDelegate:delegate queue:queue options:options];
}

- (void)socketDidDisconnect:(GNSSocket *)socket {
  NSAssert(!socket.connected, @"Should be disconnected");
  NSUUID *socketIdentifier = socket.socketIdentifier;
  NSMutableArray<GNSUpdateValueHandler> *handlerQueue =
      _handlerQueuePerSocketIdentifier[socketIdentifier];
  // The pending handlers have to be called so the associated send data completion blocks are
  // called with a disconnect error.
  for (GNSUpdateValueHandler handler in handlerQueue) {
    handler();
  }
  [_handlerQueuePerSocketIdentifier removeObjectForKey:socketIdentifier];
}

#pragma mark - Bluetooth Daemon Crash Loop

- (BOOL)isBTCrashLoop {
  NSTimeInterval timeSinceLastResetting = -[_btCrashLastResettingDate timeIntervalSinceNow];
  return (timeSinceLastResetting <= gKBTCrashLoopMaxTimeBetweenResetting) &&
         (_btCrashCount > gKBTCrashLoopMinNumberOfResetting);
}

- (void)updateBTCrashLoopHeuristic {
  NSAssert(_cbPeripheralManager.state == CBCentralManagerStateResetting, @"Unexpected CB state %@",
           CBManagerStateString(_cbPeripheralManager.state));
  NSDate *now = [NSDate date];
  if ([now timeIntervalSinceDate:_btCrashLastResettingDate] >
      gKBTCrashLoopMaxTimeBetweenResetting) {
    _btCrashCount = 0;
  }
  _btCrashLastResettingDate = now;
  _btCrashCount++;
}

#pragma mark - CBPeripheralManagerDelegate

- (void)peripheralManager:(CBPeripheralManager *)peripheral
         willRestoreState:(NSDictionary<NSString *, id> *)dict {
#if TARGET_OS_IPHONE
  // Restored API only supported on iOS.
  GTMLoggerInfo(@"Restore bluetooth services");
  _advertisementData = dict[CBPeripheralManagerRestoredStateAdvertisementDataKey];
  for (CBMutableService *service in dict[CBPeripheralManagerRestoredStateServicesKey]) {
    GNSPeripheralServiceManager *serviceManager =
        [_peripheralServiceManagers objectForKey:service.UUID];
    if (serviceManager) {
      [serviceManager restoredCBService:service];
    } else {
      GTMLoggerError(@"restoreFromCBService %@", service);
    }
  }
#endif
}

- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheralManager {
  NSAssert(peripheralManager == _cbPeripheralManager, @"Wrong peripheral manager.");
  GTMLoggerInfo(@"Peripheral manager state updated: %@",
                CBManagerStateString(_cbPeripheralManager.state));
  switch (_cbPeripheralManager.state) {
    case CBManagerStatePoweredOn:
    case CBManagerStatePoweredOff:
      // As instructed by Apple engineers:
      // * BLE services should be added on power or on power off.
      // * BLE advertisment should be started on power on.
      //
      // Note 1: During testing it seems safe to start advertising when Bluetooth is turned off:
      // the advertisment resumes when the state changes to power on.
      //
      // Note 2: It is important to keep the added services and to keep advertising on power off,
      // to avoid the scenario when the application receives a power off notification right before
      // being suspended by the OS.
      [self addAllBleServicesAndStartAdvertising];
      break;
    case CBManagerStateResetting:
      // As instructed by Apple enginners, clean-up all internal state when CoreBluetooth is
      // resetting.
      [self updateBTCrashLoopHeuristic];
      [self removeAllBleServicesAndStopAdvertising];
      break;
    case CBManagerStateUnknown:
      // Clean-up all internal state when the CoreBluetooth state is unknown.
      [self removeAllBleServicesAndStopAdvertising];
      break;
    case CBManagerStateUnauthorized:
      // Clean-up all internal state if the application is not authorized to use Bluetooth.
      [self removeAllBleServicesAndStopAdvertising];
      break;
    case CBManagerStateUnsupported:
      // The application should have never attempted to start advertising if Bluetooth Low Energy
      // is not supported on the device.
      NSAssert(!peripheralManager.isAdvertising, @"Peripheral is not advertising");
      break;
  }
  if (_peripheralManagerStateHandler) {
    _peripheralManagerStateHandler(_cbPeripheralManager.state);
  }
}

- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral
                                       error:(NSError *)error {
  _advertisementData = _advertisementInProgressData;
  _advertisementInProgressData = nil;
  if (error) {
    GTMLoggerError(@"Start advertisment failed with error %@. Will retry to start advertising on "
                    "the next BLE state change notification. ",
                   error);
    _advertisementData = nil;
    return;
  }

  NSAssert(peripheral.isAdvertising, @"Peripheral should be advertising.");
  GTMLoggerInfo(@"Peripheral did start advertising %@", _advertisementData);

  // Once an advertisment operation is over, check if the advertised data is up-to-date.
  [self updateAdvertisedServices];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
            didAddService:(CBService *)service
                    error:(NSError *)error {
  GNSPeripheralServiceManager *peripheralServiceManager =
      [_peripheralServiceManagers objectForKey:service.UUID];
  if (!peripheralServiceManager) {
    GTMLoggerError(@"Unknown service %@ with %@", service, self);
    return;
  }
  [peripheralServiceManager didAddCBServiceWithError:error];
  [self updateAdvertisedServices];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                         central:(CBCentral *)central
    didSubscribeToCharacteristic:(CBCharacteristic *)characteristic {
  GNSPeripheralServiceManager *peripheralServiceManager =
      [_peripheralServiceManagers objectForKey:characteristic.service.UUID];
  [peripheralServiceManager central:central didSubscribeToCharacteristic:characteristic];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
                             central:(CBCentral *)central
    didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic {
  GNSPeripheralServiceManager *peripheralServiceManager =
      [_peripheralServiceManagers objectForKey:characteristic.service.UUID];
  [peripheralServiceManager central:central didUnsubscribeFromCharacteristic:characteristic];

  // Restarting the peripheral manager after a disconnect. This is a workaround for b/31752176.
  // Update: This problem persists in iOS 10 & 11.  Discussion: https://goo.gl/fdE19G
  [self stop];
  [self start];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
    didReceiveReadRequest:(CBATTRequest *)request {
  GNSPeripheralServiceManager *peripheralServiceManager =
      [_peripheralServiceManagers objectForKey:request.characteristic.service.UUID];
  if (!peripheralServiceManager) {
    [_cbPeripheralManager respondToRequest:request withResult:CBATTErrorAttributeNotFound];
    return;
  }
  CBATTError requestError = [peripheralServiceManager canProcessReadRequest:request];
  if (requestError != CBATTErrorSuccess) {
    [_cbPeripheralManager respondToRequest:request withResult:requestError];
    return;
  }
  [peripheralServiceManager processReadRequest:request];
  [_cbPeripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
}

- (void)peripheralManager:(CBPeripheralManager *)peripheral
    didReceiveWriteRequests:(NSArray<CBATTRequest *> *)requests {
  // According to Apple's doc, if any one request cannot be processed, no request should
  // be processed. Only one CBATTError should be returned with the first request.
  if (requests.count == 0) {
    return;
  }
  CBATTError requestError = CBATTErrorSuccess;
  for (CBATTRequest *request in requests) {
    GNSPeripheralServiceManager *peripheralServiceManager =
        [_peripheralServiceManagers objectForKey:request.characteristic.service.UUID];
    if (!peripheralServiceManager) {
      requestError = CBATTErrorAttributeNotFound;
      break;
    }
    requestError = [peripheralServiceManager canProcessWriteRequest:request];
    if (requestError != CBATTErrorSuccess) {
      break;
    }
  }
  if (requestError != CBATTErrorSuccess) {
    [_cbPeripheralManager respondToRequest:requests[0] withResult:requestError];
    return;
  }
  for (CBATTRequest *request in requests) {
    GNSPeripheralServiceManager *peripheralServiceManager =
        [_peripheralServiceManagers objectForKey:request.characteristic.service.UUID];
    [peripheralServiceManager processWriteRequest:request];
  }
  [_cbPeripheralManager respondToRequest:requests[0] withResult:requestError];
}

- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral {
  GTMLoggerInfo(@"Ready to update subscribers.");
  [self processUpdateValueBlocks];
}

@end