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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 scoped_nsobject<MockCentralManager> mock_central_manager_; | 53 scoped_nsobject<MockCentralManager> mock_central_manager_; |
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( | 60 scoped_nsobject<NSDictionary> CreateAdvertisementData( |
61 NSString* name, | 61 NSString* name, |
62 NSArray* uuids, | 62 NSArray* uuids, |
63 NSDictionary* service_data) { | 63 NSDictionary* service_data, |
| 64 NSNumber* tx_power) { |
64 NSMutableDictionary* advertisement_data( | 65 NSMutableDictionary* advertisement_data( |
65 [NSMutableDictionary dictionaryWithDictionary:@{ | 66 [NSMutableDictionary dictionaryWithDictionary:@{ |
66 CBAdvertisementDataIsConnectable : @(YES) | 67 CBAdvertisementDataIsConnectable : @(YES) |
67 }]); | 68 }]); |
68 | 69 |
69 if (name) { | 70 if (name) { |
70 [advertisement_data setObject:name forKey:CBAdvertisementDataLocalNameKey]; | 71 [advertisement_data setObject:name forKey:CBAdvertisementDataLocalNameKey]; |
71 } | 72 } |
72 | 73 |
73 if (uuids) { | 74 if (uuids) { |
74 [advertisement_data setObject:uuids | 75 [advertisement_data setObject:uuids |
75 forKey:CBAdvertisementDataServiceUUIDsKey]; | 76 forKey:CBAdvertisementDataServiceUUIDsKey]; |
76 } | 77 } |
77 | 78 |
78 if (service_data) { | 79 if (service_data) { |
79 [advertisement_data setObject:service_data | 80 [advertisement_data setObject:service_data |
80 forKey:CBAdvertisementDataServiceDataKey]; | 81 forKey:CBAdvertisementDataServiceDataKey]; |
81 } | 82 } |
82 | 83 |
| 84 if (tx_power) { |
| 85 [advertisement_data setObject:tx_power |
| 86 forKey:CBAdvertisementDataTxPowerLevelKey]; |
| 87 } |
| 88 |
83 return scoped_nsobject<NSDictionary>(advertisement_data, | 89 return scoped_nsobject<NSDictionary>(advertisement_data, |
84 base::scoped_policy::RETAIN); | 90 base::scoped_policy::RETAIN); |
85 } | 91 } |
86 | 92 |
87 } // namespace | 93 } // namespace |
88 | 94 |
89 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. | 95 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. |
90 const std::string BluetoothTestMac::kTestPeripheralUUID1 = | 96 const std::string BluetoothTestMac::kTestPeripheralUUID1 = |
91 "34045B00-0000-0000-0000-000000000000"; | 97 "34045B00-0000-0000-0000-000000000000"; |
92 const std::string BluetoothTestMac::kTestPeripheralUUID2 = | 98 const std::string BluetoothTestMac::kTestPeripheralUUID2 = |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 146 |
141 BluetoothDevice* BluetoothTestMac::SimulateLowEnergyDevice(int device_ordinal) { | 147 BluetoothDevice* BluetoothTestMac::SimulateLowEnergyDevice(int device_ordinal) { |
142 TestBluetoothAdapterObserver observer(adapter_); | 148 TestBluetoothAdapterObserver observer(adapter_); |
143 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | 149 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; |
144 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | 150 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = |
145 adapter_mac_->low_energy_central_manager_delegate_; | 151 adapter_mac_->low_energy_central_manager_delegate_; |
146 | 152 |
147 const char* identifier; | 153 const char* identifier; |
148 NSString* name; | 154 NSString* name; |
149 NSArray* uuids; | 155 NSArray* uuids; |
| 156 NSNumber* rssi; |
150 NSDictionary* service_data; | 157 NSDictionary* service_data; |
| 158 NSNumber* tx_power; |
151 | 159 |
152 switch (device_ordinal) { | 160 switch (device_ordinal) { |
153 case 1: | 161 case 1: |
154 identifier = kTestPeripheralUUID1.c_str(); | 162 identifier = kTestPeripheralUUID1.c_str(); |
155 name = @(kTestDeviceName.c_str()); | 163 name = @(kTestDeviceName.c_str()); |
| 164 rssi = @(kTestRSSI1); |
156 uuids = @[ | 165 uuids = @[ |
157 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], | 166 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], |
158 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] | 167 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] |
159 ]; | 168 ]; |
160 service_data = @{ | 169 service_data = @{ |
161 [CBUUID UUIDWithString:@(kTestUUIDHeartRate.c_str())] : | 170 [CBUUID UUIDWithString:@(kTestUUIDHeartRate.c_str())] : |
162 [NSData dataWithBytes:(unsigned char[]){1} length:1] | 171 [NSData dataWithBytes:(unsigned char[]){1} length:1] |
163 }; | 172 }; |
| 173 tx_power = @(kTestTxPower1); |
164 break; | 174 break; |
165 case 2: | 175 case 2: |
166 identifier = kTestPeripheralUUID1.c_str(); | 176 identifier = kTestPeripheralUUID1.c_str(); |
167 name = @(kTestDeviceName.c_str()); | 177 name = @(kTestDeviceName.c_str()); |
| 178 rssi = @(kTestRSSI2); |
168 uuids = @[ | 179 uuids = @[ |
169 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], | 180 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], |
170 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] | 181 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] |
171 ]; | 182 ]; |
172 service_data = @{ | 183 service_data = @{ |
173 [CBUUID UUIDWithString:@(kTestUUIDHeartRate.c_str())] : | 184 [CBUUID UUIDWithString:@(kTestUUIDHeartRate.c_str())] : |
174 [NSData dataWithBytes:(unsigned char[]){2} length:1], | 185 [NSData dataWithBytes:(unsigned char[]){2} length:1], |
175 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())] : | 186 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())] : |
176 [NSData dataWithBytes:(unsigned char[]){0} length:1] | 187 [NSData dataWithBytes:(unsigned char[]){0} length:1] |
177 }; | 188 }; |
| 189 tx_power = @(kTestTxPower2); |
178 break; | 190 break; |
179 case 3: | 191 case 3: |
180 identifier = kTestPeripheralUUID1.c_str(); | 192 identifier = kTestPeripheralUUID1.c_str(); |
181 name = @(kTestDeviceNameEmpty.c_str()); | 193 name = @(kTestDeviceNameEmpty.c_str()); |
| 194 rssi = @(kTestRSSI3); |
182 uuids = nil; | 195 uuids = nil; |
183 service_data = nil; | 196 service_data = nil; |
| 197 tx_power = nil; |
184 break; | 198 break; |
185 case 4: | 199 case 4: |
186 identifier = kTestPeripheralUUID2.c_str(); | 200 identifier = kTestPeripheralUUID2.c_str(); |
187 name = @(kTestDeviceNameEmpty.c_str()); | 201 name = @(kTestDeviceNameEmpty.c_str()); |
| 202 rssi = @(kTestRSSI4); |
188 uuids = nil; | 203 uuids = nil; |
189 service_data = nil; | 204 service_data = nil; |
| 205 tx_power = nil; |
190 break; | 206 break; |
191 case 5: | 207 case 5: |
192 identifier = kTestPeripheralUUID1.c_str(); | 208 identifier = kTestPeripheralUUID1.c_str(); |
193 name = nil; | 209 name = nil; |
| 210 rssi = @(kTestRSSI5); |
194 uuids = nil; | 211 uuids = nil; |
195 service_data = nil; | 212 service_data = nil; |
| 213 tx_power = nil; |
196 break; | 214 break; |
197 default: | 215 default: |
198 NOTREACHED() << "SimulateLowEnergyDevice not implemented for " | 216 NOTREACHED() << "SimulateLowEnergyDevice not implemented for " |
199 << device_ordinal; | 217 << device_ordinal; |
200 identifier = nil; | 218 identifier = nil; |
201 name = nil; | 219 name = nil; |
| 220 rssi = nil; |
202 uuids = nil; | 221 uuids = nil; |
203 service_data = nil; | 222 service_data = nil; |
| 223 tx_power = nil; |
204 } | 224 } |
205 scoped_nsobject<MockCBPeripheral> mock_peripheral([[MockCBPeripheral alloc] | 225 scoped_nsobject<MockCBPeripheral> mock_peripheral([[MockCBPeripheral alloc] |
206 initWithUTF8StringIdentifier:identifier | 226 initWithUTF8StringIdentifier:identifier |
207 name:name]); | 227 name:name]); |
208 mock_peripheral.get().bluetoothTestMac = this; | 228 mock_peripheral.get().bluetoothTestMac = this; |
209 [central_manager_delegate | 229 [central_manager_delegate |
210 centralManager:central_manager | 230 centralManager:central_manager |
211 didDiscoverPeripheral:mock_peripheral.get().peripheral | 231 didDiscoverPeripheral:mock_peripheral.get().peripheral |
212 advertisementData:CreateAdvertisementData(name, uuids, service_data) | 232 advertisementData:CreateAdvertisementData(name, uuids, service_data, |
213 RSSI:@(0)]; | 233 tx_power) |
| 234 RSSI:rssi]; |
214 return observer.last_device(); | 235 return observer.last_device(); |
215 } | 236 } |
216 | 237 |
217 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { | 238 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { |
218 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | 239 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
219 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 240 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
220 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | 241 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
221 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | 242 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
222 [mockPeripheral setState:CBPeripheralStateConnected]; | 243 [mockPeripheral setState:CBPeripheralStateConnected]; |
223 CBCentralManager* centralManager = | 244 CBCentralManager* centralManager = |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
467 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 488 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
468 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 489 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
469 // return input_str; | 490 // return input_str; |
470 // } | 491 // } |
471 // ++input[0]; | 492 // ++input[0]; |
472 // } | 493 // } |
473 // return ""; | 494 // return ""; |
474 // } | 495 // } |
475 | 496 |
476 } // namespace device | 497 } // namespace device |
OLD | NEW |