Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(33)

Side by Side Diff: device/bluetooth/test/bluetooth_test_mac.mm

Issue 1948763003: Adding support for service scan on OS X (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cleanup
Patch Set: Adding disconnect with error Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/test/bluetooth_test_mac.h" 5 #include "device/bluetooth/test/bluetooth_test_mac.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/mac/foundation_util.h" 9 #include "base/mac/foundation_util.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
111 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { 111 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) {
112 TestBluetoothAdapterObserver observer(adapter_); 112 TestBluetoothAdapterObserver observer(adapter_);
113 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; 113 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_;
114 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = 114 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate =
115 adapter_mac_->low_energy_central_manager_delegate_; 115 adapter_mac_->low_energy_central_manager_delegate_;
116 switch (device_ordinal) { 116 switch (device_ordinal) {
117 case 1: { 117 case 1: {
118 scoped_nsobject<MockCBPeripheral> mock_peripheral( 118 scoped_nsobject<MockCBPeripheral> mock_peripheral(
119 [[MockCBPeripheral alloc] 119 [[MockCBPeripheral alloc]
120 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); 120 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]);
121 mock_peripheral.get().bluetoothTestMac = this;
ortuno 2016/05/06 16:03:34 Why do you need to do this?
jlebel 2016/05/07 00:16:12 I need to do this so the mock peripheral can call:
ortuno 2016/05/09 19:54:00 Ah ok.
121 NSArray* uuids = @[ 122 NSArray* uuids = @[
122 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], 123 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())],
123 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] 124 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())]
124 ]; 125 ];
125 scoped_nsobject<NSDictionary> advertisement_data = 126 scoped_nsobject<NSDictionary> advertisement_data =
126 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); 127 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids);
127 [central_manager_delegate centralManager:central_manager 128 [central_manager_delegate centralManager:central_manager
128 didDiscoverPeripheral:mock_peripheral.get().peripheral 129 didDiscoverPeripheral:mock_peripheral.get().peripheral
129 advertisementData:advertisement_data 130 advertisementData:advertisement_data
130 RSSI:@(0)]; 131 RSSI:@(0)];
131 break; 132 break;
132 } 133 }
133 case 2: { 134 case 2: {
134 scoped_nsobject<MockCBPeripheral> mock_peripheral( 135 scoped_nsobject<MockCBPeripheral> mock_peripheral(
135 [[MockCBPeripheral alloc] 136 [[MockCBPeripheral alloc]
136 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); 137 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]);
138 mock_peripheral.get().bluetoothTestMac = this;
137 NSArray* uuids = @[ 139 NSArray* uuids = @[
138 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], 140 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())],
139 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] 141 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())]
140 ]; 142 ];
141 scoped_nsobject<NSDictionary> advertisement_data = 143 scoped_nsobject<NSDictionary> advertisement_data =
142 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); 144 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids);
143 [central_manager_delegate centralManager:central_manager 145 [central_manager_delegate centralManager:central_manager
144 didDiscoverPeripheral:mock_peripheral.get().peripheral 146 didDiscoverPeripheral:mock_peripheral.get().peripheral
145 advertisementData:advertisement_data 147 advertisementData:advertisement_data
146 RSSI:@(0)]; 148 RSSI:@(0)];
147 break; 149 break;
148 } 150 }
149 case 3: { 151 case 3: {
150 scoped_nsobject<MockCBPeripheral> mock_peripheral( 152 scoped_nsobject<MockCBPeripheral> mock_peripheral(
151 [[MockCBPeripheral alloc] 153 [[MockCBPeripheral alloc]
152 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); 154 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]);
155 mock_peripheral.get().bluetoothTestMac = this;
153 scoped_nsobject<NSDictionary> advertisement_data( 156 scoped_nsobject<NSDictionary> advertisement_data(
154 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil)); 157 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil));
155 [central_manager_delegate centralManager:central_manager 158 [central_manager_delegate centralManager:central_manager
156 didDiscoverPeripheral:mock_peripheral.get().peripheral 159 didDiscoverPeripheral:mock_peripheral.get().peripheral
157 advertisementData:advertisement_data 160 advertisementData:advertisement_data
158 RSSI:@(0)]; 161 RSSI:@(0)];
159 break; 162 break;
160 } 163 }
161 case 4: { 164 case 4: {
162 scoped_nsobject<MockCBPeripheral> mock_peripheral( 165 scoped_nsobject<MockCBPeripheral> mock_peripheral(
163 [[MockCBPeripheral alloc] 166 [[MockCBPeripheral alloc]
164 initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]); 167 initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]);
168 mock_peripheral.get().bluetoothTestMac = this;
165 NSArray* uuids = nil; 169 NSArray* uuids = nil;
166 scoped_nsobject<NSDictionary> advertisement_data = 170 scoped_nsobject<NSDictionary> advertisement_data =
167 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids); 171 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids);
168 [central_manager_delegate centralManager:central_manager 172 [central_manager_delegate centralManager:central_manager
169 didDiscoverPeripheral:mock_peripheral.get().peripheral 173 didDiscoverPeripheral:mock_peripheral.get().peripheral
170 advertisementData:advertisement_data 174 advertisementData:advertisement_data
171 RSSI:@(0)]; 175 RSSI:@(0)];
172 break; 176 break;
173 } 177 }
174 } 178 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
210 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); 214 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral();
211 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; 215 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral;
212 [mockPeripheral setState:CBPeripheralStateDisconnected]; 216 [mockPeripheral setState:CBPeripheralStateDisconnected];
213 CBCentralManager* centralManager = 217 CBCentralManager* centralManager =
214 ObjCCast<CBCentralManager>(mock_central_manager_->get()); 218 ObjCCast<CBCentralManager>(mock_central_manager_->get());
215 [centralManager.delegate centralManager:centralManager 219 [centralManager.delegate centralManager:centralManager
216 didDisconnectPeripheral:peripheral 220 didDisconnectPeripheral:peripheral
217 error:nil]; 221 error:nil];
218 } 222 }
219 223
224 void BluetoothTestMac::SimulateGattServicesDiscovered(
225 BluetoothDevice* device,
226 const std::vector<std::string>& uuids) {
227 BluetoothLowEnergyDeviceMac* device_mac =
228 static_cast<BluetoothLowEnergyDeviceMac*>(device);
229 CBPeripheral* peripheral = device_mac->GetPeripheral();
230 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral);
231 scoped_nsobject<NSMutableArray> services = [[NSMutableArray alloc] init];
232 for (auto uuid : uuids) {
233 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())];
234 [services addObject:cb_service_uuid];
ortuno 2016/05/06 16:03:34 Does the ownership of cb_service_uuid get transfer
jlebel 2016/05/07 00:16:12 There is no real ownership in objective-c. The |se
ortuno 2016/05/09 19:54:00 Got it. Thanks for the explanation!
235 }
236 [peripheral_mock removeAllServices];
237 [peripheral_mock addServices:services];
238 [peripheral_mock didDiscoverWithError:nil];
239 }
240
241 void BluetoothTestMac::SimulateGattServiceRemoved(
242 BluetoothRemoteGattService* service) {
243 BluetoothUUID bluetooth_service_uuid = service->GetUUID();
244 std::string service_uuid_string = bluetooth_service_uuid.canonical_value();
245 CBUUID* cb_service_uuid =
246 [CBUUID UUIDWithString:@(service_uuid_string.c_str())];
247 BluetoothDevice* device = service->GetDevice();
248 BluetoothLowEnergyDeviceMac* device_mac =
249 static_cast<BluetoothLowEnergyDeviceMac*>(device);
250 CBPeripheral* peripheral = device_mac->GetPeripheral();
251 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral);
252 [peripheral_mock removeServiceWithCBUUID:cb_service_uuid];
ortuno 2016/05/06 16:03:34 I don't think we should use the UUID to remove ser
jlebel 2016/05/07 00:16:12 Done.
253 [peripheral_mock didDiscoverWithError:nil];
254 }
255
220 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { 256 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() {
221 gatt_connection_attempts_++; 257 gatt_connection_attempts_++;
222 } 258 }
223 259
224 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { 260 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() {
225 gatt_disconnection_attempts_++; 261 gatt_disconnection_attempts_++;
226 } 262 }
227 263
264 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() {
265 gatt_discovery_attempts_++;
266 }
267
228 // Utility function for generating new (CBUUID, address) pairs where CBUUID 268 // Utility function for generating new (CBUUID, address) pairs where CBUUID
229 // hashes to address. For use when adding a new device address to the testing 269 // hashes to address. For use when adding a new device address to the testing
230 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, 270 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses,
231 // and we construct fake addresses for them by hashing the CBUUID. By changing 271 // and we construct fake addresses for them by hashing the CBUUID. By changing
232 // |target| the user can generate sequentially numbered test addresses. 272 // |target| the user can generate sequentially numbered test addresses.
233 // 273 //
234 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { 274 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() {
235 // // The desired first 6 digits of the hash. For example 0100000, 020000, 275 // // The desired first 6 digits of the hash. For example 0100000, 020000,
236 // // 030000, ... 276 // // 030000, ...
237 // const std::string target = "010000"; 277 // const std::string target = "010000";
(...skipping 16 matching lines...) Expand all
254 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); 294 // crypto::SHA256HashString(input_str, raw, sizeof(raw));
255 // if (base::HexEncode(raw, sizeof(raw)) == target) { 295 // if (base::HexEncode(raw, sizeof(raw)) == target) {
256 // return input_str; 296 // return input_str;
257 // } 297 // }
258 // ++input[0]; 298 // ++input[0];
259 // } 299 // }
260 // return ""; 300 // return "";
261 // } 301 // }
262 302
263 } // namespace device 303 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698