jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 5 | #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 6 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 7 | #include "base/mac/foundation_util.h" |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 8 | #include "base/mac/scoped_nsobject.h" |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 9 | #include "device/bluetooth/test/bluetooth_test_mac.h" |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 10 | #include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 11 | #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 12 | |
| 13 | using base::mac::ObjCCast; |
| 14 | using base::scoped_nsobject; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 15 | |
| 16 | @interface MockCBPeripheral () { |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 17 | scoped_nsobject<NSUUID> _identifier; |
| 18 | scoped_nsobject<NSString> _name; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 19 | id<CBPeripheralDelegate> _delegate; |
| 20 | scoped_nsobject<NSMutableArray> _services; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | @end |
| 24 | |
| 25 | @implementation MockCBPeripheral |
| 26 | |
| 27 | @synthesize state = _state; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 28 | @synthesize delegate = _delegate; |
| 29 | @synthesize bluetoothTestMac = _bluetoothTestMac; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 30 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 31 | - (instancetype)init { |
| 32 | [self doesNotRecognizeSelector:_cmd]; |
| 33 | return self; |
| 34 | } |
| 35 | |
| 36 | - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier { |
scheib | dd9ce700 | 2016-07-21 07:06:06 | [diff] [blame] | 37 | return [self initWithUTF8StringIdentifier:utf8Identifier name:nil]; |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 38 | } |
| 39 | |
scheib | dd9ce700 | 2016-07-21 07:06:06 | [diff] [blame] | 40 | - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier |
| 41 | name:(NSString*)name { |
| 42 | scoped_nsobject<NSUUID> identifier( |
| 43 | [[NSUUID alloc] initWithUUIDString:@(utf8Identifier)]); |
| 44 | return [self initWithIdentifier:identifier name:name]; |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | - (instancetype)initWithIdentifier:(NSUUID*)identifier name:(NSString*)name { |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 48 | self = [super init]; |
| 49 | if (self) { |
| 50 | _identifier.reset([identifier retain]); |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 51 | if (name) { |
| 52 | _name.reset([name retain]); |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 53 | } |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 54 | _state = CBPeripheralStateDisconnected; |
| 55 | } |
| 56 | return self; |
| 57 | } |
| 58 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 59 | - (BOOL)isKindOfClass:(Class)aClass { |
| 60 | if (aClass == [CBPeripheral class] || |
| 61 | [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 62 | return YES; |
| 63 | } |
| 64 | return [super isKindOfClass:aClass]; |
| 65 | } |
| 66 | |
| 67 | - (BOOL)isMemberOfClass:(Class)aClass { |
| 68 | if (aClass == [CBPeripheral class] || |
| 69 | [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 70 | return YES; |
| 71 | } |
| 72 | return [super isKindOfClass:aClass]; |
| 73 | } |
| 74 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 75 | - (void)setState:(CBPeripheralState)state { |
| 76 | _state = state; |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 77 | if (_state == CBPeripheralStateDisconnected) { |
ccameron | 1c9b983 | 2016-06-03 23:54:06 | [diff] [blame] | 78 | _services.reset(); |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 79 | } |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 80 | } |
| 81 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 82 | - (void)discoverServices:(NSArray*)serviceUUIDs { |
| 83 | if (_bluetoothTestMac) { |
| 84 | _bluetoothTestMac->OnFakeBluetoothServiceDiscovery(); |
| 85 | } |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 86 | [_delegate peripheral:self.peripheral didDiscoverServices:nil]; |
| 87 | } |
| 88 | |
| 89 | - (void)discoverCharacteristics:(NSArray*)characteristics |
| 90 | forService:(CBService*)service { |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 91 | } |
| 92 | |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 93 | - (void)discoverDescriptorsForCharacteristic:(CBCharacteristic*)characteristic { |
| 94 | MockCBCharacteristic* mock_characteristic = |
| 95 | ObjCCast<MockCBCharacteristic>(characteristic); |
| 96 | [mock_characteristic discoverDescriptors]; |
| 97 | } |
| 98 | |
jlebel | e223e929 | 2016-06-27 15:22:56 | [diff] [blame] | 99 | - (void)readValueForCharacteristic:(CBCharacteristic*)characteristic { |
| 100 | DCHECK(_bluetoothTestMac); |
| 101 | _bluetoothTestMac->OnFakeBluetoothCharacteristicReadValue(); |
| 102 | } |
| 103 | |
jlebel | 4e58d3d | 2016-06-28 01:43:26 | [diff] [blame] | 104 | - (void)writeValue:(NSData*)data |
| 105 | forCharacteristic:(CBCharacteristic*)characteristic |
| 106 | type:(CBCharacteristicWriteType)type { |
| 107 | DCHECK(_bluetoothTestMac); |
| 108 | const uint8_t* buffer = static_cast<const uint8_t*>(data.bytes); |
| 109 | std::vector<uint8_t> value(buffer, buffer + data.length); |
| 110 | _bluetoothTestMac->OnFakeBluetoothCharacteristicWriteValue(value); |
| 111 | } |
| 112 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 113 | - (void)removeAllServices { |
| 114 | [_services.get() removeAllObjects]; |
| 115 | } |
| 116 | |
| 117 | - (void)addServices:(NSArray*)services { |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 118 | if (!_services.get()) { |
| 119 | _services.reset([[NSMutableArray alloc] init]); |
| 120 | } |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 121 | for (CBUUID* uuid in services) { |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 122 | base::scoped_nsobject<MockCBService> service([[MockCBService alloc] |
| 123 | initWithPeripheral:self.peripheral |
| 124 | CBUUID:uuid |
| 125 | primary:YES]); |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 126 | [_services.get() addObject:service.get().service]; |
| 127 | } |
| 128 | } |
| 129 | |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 130 | - (void)didDiscoverServicesWithError:(NSError*)error { |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 131 | [_delegate peripheral:self.peripheral didDiscoverServices:error]; |
| 132 | } |
| 133 | |
| 134 | - (void)removeService:(CBService*)service { |
| 135 | base::scoped_nsobject<CBService> serviceToRemove(service, |
| 136 | base::scoped_policy::RETAIN); |
jlebel | 20060d0 | 2016-05-28 11:23:19 | [diff] [blame] | 137 | DCHECK(serviceToRemove); |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 138 | [_services.get() removeObject:serviceToRemove]; |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 139 | [self didModifyServices:@[ serviceToRemove ]]; |
| 140 | } |
| 141 | |
jlebel | 062d7b4 | 2017-01-07 13:32:20 | [diff] [blame] | 142 | - (void)mockDidDiscoverEvents { |
| 143 | [_delegate peripheral:self.peripheral didDiscoverServices:nil]; |
| 144 | // BluetoothLowEnergyDeviceMac is expected to call |
| 145 | // -[CBPeripheral discoverCharacteristics:forService:] for each services, |
| 146 | // so -[<CBPeripheralDelegate peripheral:didDiscoverCharacteristicsForService: |
| 147 | // error:] needs to be called for all services. |
| 148 | for (CBService* service in _services.get()) { |
| 149 | [_delegate peripheral:self.peripheral |
| 150 | didDiscoverCharacteristicsForService:service |
| 151 | error:nil]; |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 152 | for (CBCharacteristic* characteristic in service.characteristics) { |
| 153 | // After discovering services, BluetoothLowEnergyDeviceMac is expected to |
| 154 | // discover characteristics for all services. |
| 155 | [_delegate peripheral:self.peripheral |
| 156 | didDiscoverDescriptorsForCharacteristic:characteristic |
| 157 | error:nil]; |
| 158 | } |
jlebel | 062d7b4 | 2017-01-07 13:32:20 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 162 | - (void)didModifyServices:(NSArray*)invalidatedServices { |
| 163 | DCHECK( |
| 164 | [_delegate respondsToSelector:@selector(peripheral:didModifyServices:)]); |
| 165 | [_delegate peripheral:self.peripheral didModifyServices:invalidatedServices]; |
| 166 | } |
| 167 | |
| 168 | - (void)didDiscoverDescriptorsWithCharacteristic: |
| 169 | (MockCBCharacteristic*)characteristic_mock { |
| 170 | [_delegate peripheral:self.peripheral |
| 171 | didDiscoverDescriptorsForCharacteristic:characteristic_mock.characteristic |
| 172 | error:nil]; |
| 173 | } |
| 174 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 175 | - (NSUUID*)identifier { |
| 176 | return _identifier.get(); |
| 177 | } |
| 178 | |
| 179 | - (NSString*)name { |
| 180 | return _name.get(); |
| 181 | } |
| 182 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 183 | - (NSArray*)services { |
| 184 | return _services.get(); |
| 185 | } |
| 186 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 187 | - (CBPeripheral*)peripheral { |
| 188 | return ObjCCast<CBPeripheral>(self); |
| 189 | } |
| 190 | |
jlebel | 9db2021 | 2016-06-29 15:03:48 | [diff] [blame] | 191 | - (void)setNotifyValue:(BOOL)notification |
| 192 | forCharacteristic:(CBCharacteristic*)characteristic { |
jlebel | 7c346d7 | 2017-02-14 08:54:42 | [diff] [blame^] | 193 | _bluetoothTestMac->OnFakeBluetoothGattSetCharacteristicNotification( |
| 194 | notification == YES); |
jlebel | 9db2021 | 2016-06-29 15:03:48 | [diff] [blame] | 195 | } |
| 196 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 197 | @end |