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 | 1cc94fe3 | 2017-04-11 20:04:56 | [diff] [blame] | 11 | #include "device/bluetooth/test/mock_bluetooth_cbdescriptor_mac.h" |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 12 | #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 13 | |
| 14 | using base::mac::ObjCCast; |
| 15 | using base::scoped_nsobject; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 16 | |
| 17 | @interface MockCBPeripheral () { |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 18 | scoped_nsobject<NSUUID> _identifier; |
| 19 | scoped_nsobject<NSString> _name; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 20 | id<CBPeripheralDelegate> _delegate; |
| 21 | scoped_nsobject<NSMutableArray> _services; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | @end |
| 25 | |
| 26 | @implementation MockCBPeripheral |
| 27 | |
| 28 | @synthesize state = _state; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 29 | @synthesize delegate = _delegate; |
| 30 | @synthesize bluetoothTestMac = _bluetoothTestMac; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 31 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 32 | - (instancetype)init { |
| 33 | [self doesNotRecognizeSelector:_cmd]; |
| 34 | return self; |
| 35 | } |
| 36 | |
| 37 | - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier { |
scheib | dd9ce700 | 2016-07-21 07:06:06 | [diff] [blame] | 38 | return [self initWithUTF8StringIdentifier:utf8Identifier name:nil]; |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 39 | } |
| 40 | |
scheib | dd9ce700 | 2016-07-21 07:06:06 | [diff] [blame] | 41 | - (instancetype)initWithUTF8StringIdentifier:(const char*)utf8Identifier |
| 42 | name:(NSString*)name { |
| 43 | scoped_nsobject<NSUUID> identifier( |
| 44 | [[NSUUID alloc] initWithUUIDString:@(utf8Identifier)]); |
| 45 | return [self initWithIdentifier:identifier name:name]; |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | - (instancetype)initWithIdentifier:(NSUUID*)identifier name:(NSString*)name { |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 49 | self = [super init]; |
| 50 | if (self) { |
| 51 | _identifier.reset([identifier retain]); |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 52 | if (name) { |
| 53 | _name.reset([name retain]); |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 54 | } |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 55 | _state = CBPeripheralStateDisconnected; |
| 56 | } |
| 57 | return self; |
| 58 | } |
| 59 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 60 | - (BOOL)isKindOfClass:(Class)aClass { |
| 61 | if (aClass == [CBPeripheral class] || |
| 62 | [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 63 | return YES; |
| 64 | } |
| 65 | return [super isKindOfClass:aClass]; |
| 66 | } |
| 67 | |
| 68 | - (BOOL)isMemberOfClass:(Class)aClass { |
| 69 | if (aClass == [CBPeripheral class] || |
| 70 | [aClass isSubclassOfClass:[CBPeripheral class]]) { |
| 71 | return YES; |
| 72 | } |
| 73 | return [super isKindOfClass:aClass]; |
| 74 | } |
| 75 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 76 | - (void)setState:(CBPeripheralState)state { |
| 77 | _state = state; |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 78 | if (_state == CBPeripheralStateDisconnected) { |
ccameron | 1c9b983 | 2016-06-03 23:54:06 | [diff] [blame] | 79 | _services.reset(); |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 80 | } |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 81 | } |
| 82 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 83 | - (void)discoverServices:(NSArray*)serviceUUIDs { |
| 84 | if (_bluetoothTestMac) { |
| 85 | _bluetoothTestMac->OnFakeBluetoothServiceDiscovery(); |
| 86 | } |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | - (void)discoverCharacteristics:(NSArray*)characteristics |
| 90 | forService:(CBService*)service { |
jlebel | 0d7b7b5 | 2017-03-06 23:57:33 | [diff] [blame] | 91 | if (_bluetoothTestMac) { |
| 92 | _bluetoothTestMac->OnFakeBluetoothCharacteristicDiscovery(); |
| 93 | } |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 94 | } |
| 95 | |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 96 | - (void)discoverDescriptorsForCharacteristic:(CBCharacteristic*)characteristic { |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 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 | 1cc94fe3 | 2017-04-11 20:04:56 | [diff] [blame] | 113 | - (void)readValueForDescriptor:(CBDescriptor*)descriptor { |
| 114 | DCHECK(_bluetoothTestMac); |
| 115 | _bluetoothTestMac->OnFakeBluetoothDescriptorReadValue(); |
| 116 | } |
| 117 | |
| 118 | - (void)writeValue:(NSData*)data forDescriptor:(CBDescriptor*)descriptor { |
| 119 | DCHECK(_bluetoothTestMac); |
| 120 | const uint8_t* buffer = static_cast<const uint8_t*>(data.bytes); |
| 121 | std::vector<uint8_t> value(buffer, buffer + data.length); |
| 122 | _bluetoothTestMac->OnFakeBluetoothDescriptorWriteValue(value); |
| 123 | } |
| 124 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 125 | - (void)removeAllServices { |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 126 | [_services removeAllObjects]; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | - (void)addServices:(NSArray*)services { |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 130 | if (!_services) { |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 131 | _services.reset([[NSMutableArray alloc] init]); |
| 132 | } |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 133 | for (CBUUID* uuid in services) { |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 134 | base::scoped_nsobject<MockCBService> service([[MockCBService alloc] |
| 135 | initWithPeripheral:self.peripheral |
| 136 | CBUUID:uuid |
| 137 | primary:YES]); |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 138 | [_services addObject:[service service]]; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
jlebel | 471d7dd | 2016-06-03 08:07:12 | [diff] [blame] | 142 | - (void)didDiscoverServicesWithError:(NSError*)error { |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 143 | [_delegate peripheral:self.peripheral didDiscoverServices:error]; |
| 144 | } |
| 145 | |
| 146 | - (void)removeService:(CBService*)service { |
| 147 | base::scoped_nsobject<CBService> serviceToRemove(service, |
| 148 | base::scoped_policy::RETAIN); |
jlebel | 20060d0 | 2016-05-28 11:23:19 | [diff] [blame] | 149 | DCHECK(serviceToRemove); |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 150 | [_services removeObject:serviceToRemove]; |
jlebel | 620327d | 2016-06-20 13:01:00 | [diff] [blame] | 151 | [self didModifyServices:@[ serviceToRemove ]]; |
| 152 | } |
| 153 | |
jlebel | 0d7b7b5 | 2017-03-06 23:57:33 | [diff] [blame] | 154 | - (void)mockDidDiscoverServices { |
jlebel | 062d7b4 | 2017-01-07 13:32:20 | [diff] [blame] | 155 | [_delegate peripheral:self.peripheral didDiscoverServices:nil]; |
jlebel | 0d7b7b5 | 2017-03-06 23:57:33 | [diff] [blame] | 156 | } |
| 157 | |
jlebel | 74fe98e | 2017-05-04 20:45:17 | [diff] [blame] | 158 | - (void)mockDidDiscoverCharacteristicsForService:(CBService*)service { |
| 159 | [_delegate peripheral:self.peripheral |
| 160 | didDiscoverCharacteristicsForService:service |
| 161 | error:nil]; |
| 162 | } |
| 163 | |
| 164 | - (void)mockDidDiscoverDescriptorsForCharacteristic: |
| 165 | (CBCharacteristic*)characteristic { |
| 166 | [_delegate peripheral:self.peripheral |
| 167 | didDiscoverDescriptorsForCharacteristic:characteristic |
| 168 | error:nil]; |
| 169 | } |
| 170 | |
jlebel | 0d7b7b5 | 2017-03-06 23:57:33 | [diff] [blame] | 171 | - (void)mockDidDiscoverEvents { |
| 172 | [self mockDidDiscoverServices]; |
jlebel | 062d7b4 | 2017-01-07 13:32:20 | [diff] [blame] | 173 | // BluetoothLowEnergyDeviceMac is expected to call |
| 174 | // -[CBPeripheral discoverCharacteristics:forService:] for each services, |
| 175 | // so -[<CBPeripheralDelegate peripheral:didDiscoverCharacteristicsForService: |
| 176 | // error:] needs to be called for all services. |
| 177 | for (CBService* service in _services.get()) { |
jlebel | 74fe98e | 2017-05-04 20:45:17 | [diff] [blame] | 178 | [self mockDidDiscoverCharacteristicsForService:service]; |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 179 | for (CBCharacteristic* characteristic in service.characteristics) { |
| 180 | // After discovering services, BluetoothLowEnergyDeviceMac is expected to |
| 181 | // discover characteristics for all services. |
| 182 | [_delegate peripheral:self.peripheral |
| 183 | didDiscoverDescriptorsForCharacteristic:characteristic |
| 184 | error:nil]; |
| 185 | } |
jlebel | 062d7b4 | 2017-01-07 13:32:20 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
jlebel | 9bb8cf24 | 2017-01-10 01:29:02 | [diff] [blame] | 189 | - (void)didModifyServices:(NSArray*)invalidatedServices { |
| 190 | DCHECK( |
| 191 | [_delegate respondsToSelector:@selector(peripheral:didModifyServices:)]); |
| 192 | [_delegate peripheral:self.peripheral didModifyServices:invalidatedServices]; |
| 193 | } |
| 194 | |
| 195 | - (void)didDiscoverDescriptorsWithCharacteristic: |
| 196 | (MockCBCharacteristic*)characteristic_mock { |
| 197 | [_delegate peripheral:self.peripheral |
| 198 | didDiscoverDescriptorsForCharacteristic:characteristic_mock.characteristic |
| 199 | error:nil]; |
| 200 | } |
| 201 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 202 | - (NSUUID*)identifier { |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 203 | return _identifier; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | - (NSString*)name { |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 207 | return _name; |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 208 | } |
| 209 | |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 210 | - (NSArray*)services { |
jlebel | 35be309 | 2017-05-26 19:55:28 | [diff] [blame] | 211 | return _services; |
jlebel | c4b0978 | 2016-05-10 00:31:58 | [diff] [blame] | 212 | } |
| 213 | |
jlebel | f11faf4f | 2016-03-23 11:05:53 | [diff] [blame] | 214 | - (CBPeripheral*)peripheral { |
| 215 | return ObjCCast<CBPeripheral>(self); |
| 216 | } |
| 217 | |
jlebel | 9db2021 | 2016-06-29 15:03:48 | [diff] [blame] | 218 | - (void)setNotifyValue:(BOOL)notification |
| 219 | forCharacteristic:(CBCharacteristic*)characteristic { |
jlebel | 7c346d7 | 2017-02-14 08:54:42 | [diff] [blame] | 220 | _bluetoothTestMac->OnFakeBluetoothGattSetCharacteristicNotification( |
| 221 | notification == YES); |
jlebel | 9db2021 | 2016-06-29 15:03:48 | [diff] [blame] | 222 | } |
| 223 | |
jlebel | a58fd75 | 2016-03-14 10:33:28 | [diff] [blame] | 224 | @end |