| OLD | NEW |
| 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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(ScopedMockCentralManager); | 55 DISALLOW_COPY_AND_ASSIGN(ScopedMockCentralManager); |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 namespace { | 58 namespace { |
| 59 | 59 |
| 60 scoped_nsobject<NSDictionary> CreateAdvertisementData(NSString* name, | 60 scoped_nsobject<NSDictionary> CreateAdvertisementData(NSString* name, |
| 61 NSArray* uuids) { | 61 NSArray* uuids) { |
| 62 NSMutableDictionary* advertisement_data( | 62 NSMutableDictionary* advertisement_data( |
| 63 [NSMutableDictionary dictionaryWithDictionary:@{ | 63 [NSMutableDictionary dictionaryWithDictionary:@{ |
| 64 CBAdvertisementDataLocalNameKey : name, | |
| 65 CBAdvertisementDataServiceDataKey : @{}, | 64 CBAdvertisementDataServiceDataKey : @{}, |
| 66 CBAdvertisementDataIsConnectable : @(YES), | 65 CBAdvertisementDataIsConnectable : @(YES), |
| 67 }]); | 66 }]); |
| 67 if (name) { |
| 68 [advertisement_data setObject:name forKey:CBAdvertisementDataLocalNameKey]; |
| 69 } |
| 68 if (uuids) { | 70 if (uuids) { |
| 69 [advertisement_data setObject:uuids | 71 [advertisement_data setObject:uuids |
| 70 forKey:CBAdvertisementDataServiceUUIDsKey]; | 72 forKey:CBAdvertisementDataServiceUUIDsKey]; |
| 71 } | 73 } |
| 72 return scoped_nsobject<NSDictionary>(advertisement_data, | 74 return scoped_nsobject<NSDictionary>(advertisement_data, |
| 73 base::scoped_policy::RETAIN); | 75 base::scoped_policy::RETAIN); |
| 74 } | 76 } |
| 75 | 77 |
| 76 } // namespace | 78 } // namespace |
| 77 | 79 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 [mock_central_manager_->get() setState:CBCentralManagerStatePoweredOn]; | 127 [mock_central_manager_->get() setState:CBCentralManagerStatePoweredOn]; |
| 126 adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager_->get()); | 128 adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager_->get()); |
| 127 } | 129 } |
| 128 } | 130 } |
| 129 | 131 |
| 130 BluetoothDevice* BluetoothTestMac::SimulateLowEnergyDevice(int device_ordinal) { | 132 BluetoothDevice* BluetoothTestMac::SimulateLowEnergyDevice(int device_ordinal) { |
| 131 TestBluetoothAdapterObserver observer(adapter_); | 133 TestBluetoothAdapterObserver observer(adapter_); |
| 132 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | 134 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; |
| 133 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | 135 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = |
| 134 adapter_mac_->low_energy_central_manager_delegate_; | 136 adapter_mac_->low_energy_central_manager_delegate_; |
| 137 |
| 138 const char* identifier; |
| 139 NSString* name; |
| 140 NSArray* uuids; |
| 141 |
| 135 switch (device_ordinal) { | 142 switch (device_ordinal) { |
| 136 case 1: { | 143 case 1: |
| 137 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 144 identifier = kTestPeripheralUUID1.c_str(); |
| 138 [[MockCBPeripheral alloc] | 145 name = @(kTestDeviceName.c_str()); |
| 139 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | 146 uuids = @[ |
| 140 mock_peripheral.get().bluetoothTestMac = this; | |
| 141 NSArray* uuids = @[ | |
| 142 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], | 147 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], |
| 143 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] | 148 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] |
| 144 ]; | 149 ]; |
| 145 scoped_nsobject<NSDictionary> advertisement_data = | |
| 146 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); | |
| 147 [central_manager_delegate centralManager:central_manager | |
| 148 didDiscoverPeripheral:mock_peripheral.get().peripheral | |
| 149 advertisementData:advertisement_data | |
| 150 RSSI:@(0)]; | |
| 151 break; | 150 break; |
| 152 } | 151 case 2: |
| 153 case 2: { | 152 identifier = kTestPeripheralUUID1.c_str(); |
| 154 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 153 name = @(kTestDeviceName.c_str()); |
| 155 [[MockCBPeripheral alloc] | 154 uuids = @[ |
| 156 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | |
| 157 mock_peripheral.get().bluetoothTestMac = this; | |
| 158 NSArray* uuids = @[ | |
| 159 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], | 155 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], |
| 160 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] | 156 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] |
| 161 ]; | 157 ]; |
| 162 scoped_nsobject<NSDictionary> advertisement_data = | |
| 163 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); | |
| 164 [central_manager_delegate centralManager:central_manager | |
| 165 didDiscoverPeripheral:mock_peripheral.get().peripheral | |
| 166 advertisementData:advertisement_data | |
| 167 RSSI:@(0)]; | |
| 168 break; | 158 break; |
| 169 } | 159 case 3: |
| 170 case 3: { | 160 identifier = kTestPeripheralUUID1.c_str(); |
| 171 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 161 name = @(kTestDeviceNameEmpty.c_str()); |
| 172 [[MockCBPeripheral alloc] | 162 uuids = nil; |
| 173 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | |
| 174 mock_peripheral.get().bluetoothTestMac = this; | |
| 175 scoped_nsobject<NSDictionary> advertisement_data( | |
| 176 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil)); | |
| 177 [central_manager_delegate centralManager:central_manager | |
| 178 didDiscoverPeripheral:mock_peripheral.get().peripheral | |
| 179 advertisementData:advertisement_data | |
| 180 RSSI:@(0)]; | |
| 181 break; | 163 break; |
| 182 } | 164 case 4: |
| 183 case 4: { | 165 identifier = kTestPeripheralUUID2.c_str(); |
| 184 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 166 name = @(kTestDeviceNameEmpty.c_str()); |
| 185 [[MockCBPeripheral alloc] | 167 uuids = nil; |
| 186 initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]); | |
| 187 mock_peripheral.get().bluetoothTestMac = this; | |
| 188 NSArray* uuids = nil; | |
| 189 scoped_nsobject<NSDictionary> advertisement_data = | |
| 190 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids); | |
| 191 [central_manager_delegate centralManager:central_manager | |
| 192 didDiscoverPeripheral:mock_peripheral.get().peripheral | |
| 193 advertisementData:advertisement_data | |
| 194 RSSI:@(0)]; | |
| 195 break; | 168 break; |
| 196 } | 169 case 5: |
| 170 identifier = kTestPeripheralUUID1.c_str(); |
| 171 name = nil; |
| 172 uuids = nil; |
| 173 break; |
| 174 default: |
| 175 NOTREACHED() << "SimulateLowEnergyDevice not implemented for " |
| 176 << device_ordinal; |
| 177 identifier = nil; |
| 178 name = nil; |
| 179 uuids = nil; |
| 197 } | 180 } |
| 181 scoped_nsobject<MockCBPeripheral> mock_peripheral([[MockCBPeripheral alloc] |
| 182 initWithUTF8StringIdentifier:identifier |
| 183 name:name]); |
| 184 mock_peripheral.get().bluetoothTestMac = this; |
| 185 [central_manager_delegate centralManager:central_manager |
| 186 didDiscoverPeripheral:mock_peripheral.get().peripheral |
| 187 advertisementData:CreateAdvertisementData(name, uuids) |
| 188 RSSI:@(0)]; |
| 198 return observer.last_device(); | 189 return observer.last_device(); |
| 199 } | 190 } |
| 200 | 191 |
| 201 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { | 192 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { |
| 202 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | 193 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
| 203 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 194 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 204 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | 195 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| 205 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | 196 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| 206 [mockPeripheral setState:CBPeripheralStateConnected]; | 197 [mockPeripheral setState:CBPeripheralStateConnected]; |
| 207 CBCentralManager* centralManager = | 198 CBCentralManager* centralManager = |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 443 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 434 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 444 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 435 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 445 // return input_str; | 436 // return input_str; |
| 446 // } | 437 // } |
| 447 // ++input[0]; | 438 // ++input[0]; |
| 448 // } | 439 // } |
| 449 // return ""; | 440 // return ""; |
| 450 // } | 441 // } |
| 451 | 442 |
| 452 } // namespace device | 443 } // namespace device |
| OLD | NEW |