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

Side by Side Diff: device/bluetooth/test/bluetooth_test_win.cc

Issue 1690133002: Implement BluetoothRemoteGattServiceWin and related unit tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_win.h" 5 #include "device/bluetooth/test/bluetooth_test_win.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/sys_string_conversions.h" 8 #include "base/strings/sys_string_conversions.h"
9 #include "device/bluetooth/bluetooth_adapter_win.h" 9 #include "device/bluetooth/bluetooth_adapter_win.h"
10 #include "device/bluetooth/bluetooth_low_energy_win.h" 10 #include "device/bluetooth/bluetooth_low_energy_win.h"
11 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_win.h"
12 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h"
11 13
12 namespace { 14 namespace {
13 15
14 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS( 16 BLUETOOTH_ADDRESS CanonicalStringToBLUETOOTH_ADDRESS(
15 std::string device_address) { 17 std::string device_address) {
16 BLUETOOTH_ADDRESS win_addr; 18 BLUETOOTH_ADDRESS win_addr;
17 unsigned int data[6]; 19 unsigned int data[6];
18 int result = 20 int result =
19 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X", 21 sscanf_s(device_address.c_str(), "%02X:%02X:%02X:%02X:%02X:%02X",
20 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]); 22 &data[5], &data[4], &data[3], &data[2], &data[1], &data[0]);
21 CHECK(result == 6); 23 CHECK(result == 6);
22 for (int i = 0; i < 6; i++) { 24 for (int i = 0; i < 6; i++) {
23 win_addr.rgBytes[i] = data[i]; 25 win_addr.rgBytes[i] = data[i];
24 } 26 }
25 return win_addr; 27 return win_addr;
26 } 28 }
27 29
30 // The canonical UUID string format is device::BluetoothUUID.value().
31 BTH_LE_UUID CanonicalStringToBTH_LE_UUID(std::string uuid) {
32 BTH_LE_UUID win_uuid = {0};
33 if (uuid.size() == 4) {
34 win_uuid.IsShortUuid = TRUE;
35 unsigned int data[1];
36 int result = sscanf_s(uuid.c_str(), "%04x", &data[0]);
37 CHECK(result == 1);
38 win_uuid.Value.ShortUuid = data[0];
39 } else if (uuid.size() == 36) {
40 win_uuid.IsShortUuid = FALSE;
41 unsigned int data[11];
42 int result = sscanf_s(
43 uuid.c_str(), "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
44 &data[0], &data[1], &data[2], &data[3], &data[4], &data[5], &data[6],
45 &data[7], &data[8], &data[9], &data[10]);
46 CHECK(result == 11);
47 win_uuid.Value.LongUuid.Data1 = data[0];
48 win_uuid.Value.LongUuid.Data2 = data[1];
49 win_uuid.Value.LongUuid.Data3 = data[2];
50 win_uuid.Value.LongUuid.Data4[0] = data[3];
51 win_uuid.Value.LongUuid.Data4[1] = data[4];
52 win_uuid.Value.LongUuid.Data4[2] = data[5];
53 win_uuid.Value.LongUuid.Data4[3] = data[6];
54 win_uuid.Value.LongUuid.Data4[4] = data[7];
55 win_uuid.Value.LongUuid.Data4[5] = data[8];
56 win_uuid.Value.LongUuid.Data4[6] = data[9];
57 win_uuid.Value.LongUuid.Data4[7] = data[10];
58 } else {
59 CHECK(false);
60 }
61
62 return win_uuid;
63 }
64
28 } // namespace 65 } // namespace
29 66
30 namespace device { 67 namespace device {
31 BluetoothTestWin::BluetoothTestWin() 68 BluetoothTestWin::BluetoothTestWin()
32 : ui_task_runner_(new base::TestSimpleTaskRunner()), 69 : ui_task_runner_(new base::TestSimpleTaskRunner()),
33 bluetooth_task_runner_(new base::TestSimpleTaskRunner()) {} 70 bluetooth_task_runner_(new base::TestSimpleTaskRunner()) {}
34 BluetoothTestWin::~BluetoothTestWin() {} 71 BluetoothTestWin::~BluetoothTestWin() {}
35 72
36 bool BluetoothTestWin::PlatformSupportsLowEnergy() { 73 bool BluetoothTestWin::PlatformSupportsLowEnergy() {
37 return win::IsBluetoothLowEnergySupported(); 74 return win::IsBluetoothLowEnergySupported();
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 } break; 140 } break;
104 case 4: { 141 case 4: {
105 device_name = kTestDeviceNameEmpty; 142 device_name = kTestDeviceNameEmpty;
106 device_address = kTestDeviceAddress2; 143 device_address = kTestDeviceAddress2;
107 } break; 144 } break;
108 } 145 }
109 146
110 win::BLEDevice* simulated_device = fake_bt_le_wrapper_->SimulateBLEDevice( 147 win::BLEDevice* simulated_device = fake_bt_le_wrapper_->SimulateBLEDevice(
111 device_name, CanonicalStringToBLUETOOTH_ADDRESS(device_address)); 148 device_name, CanonicalStringToBLUETOOTH_ADDRESS(device_address));
112 if (simulated_device != nullptr) { 149 if (simulated_device != nullptr) {
113 if (!service_uuid_1.empty()) 150 if (!service_uuid_1.empty()) {
114 fake_bt_le_wrapper_->SimulateBLEGattService(simulated_device, 151 fake_bt_le_wrapper_->SimulateBLEGattService(
115 service_uuid_1); 152 simulated_device, nullptr,
116 if (!service_uuid_2.empty()) 153 CanonicalStringToBTH_LE_UUID(service_uuid_1));
117 fake_bt_le_wrapper_->SimulateBLEGattService(simulated_device, 154 }
118 service_uuid_2); 155 if (!service_uuid_2.empty()) {
156 fake_bt_le_wrapper_->SimulateBLEGattService(
157 simulated_device, nullptr,
158 CanonicalStringToBTH_LE_UUID(service_uuid_2));
159 }
119 } 160 }
120 bluetooth_task_runner_->RunPendingTasks(); 161 bluetooth_task_runner_->RunPendingTasks();
121 ui_task_runner_->RunPendingTasks(); 162 ui_task_runner_->RunPendingTasks();
122 163
123 std::vector<BluetoothDevice*> devices = adapter_win_->GetDevices(); 164 std::vector<BluetoothDevice*> devices = adapter_win_->GetDevices();
124 for (auto device : devices) { 165 for (auto device : devices) {
125 if (device->GetAddress() == device_address) 166 if (device->GetAddress() == device_address)
126 return device; 167 return device;
127 } 168 }
128 169
(...skipping 12 matching lines...) Expand all
141 } 182 }
142 183
143 void BluetoothTestWin::SimulateGattServicesDiscovered( 184 void BluetoothTestWin::SimulateGattServicesDiscovered(
144 BluetoothDevice* device, 185 BluetoothDevice* device,
145 const std::vector<std::string>& uuids) { 186 const std::vector<std::string>& uuids) {
146 win::BLEDevice* simulated_device = 187 win::BLEDevice* simulated_device =
147 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device->GetAddress()); 188 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device->GetAddress());
148 CHECK(simulated_device); 189 CHECK(simulated_device);
149 190
150 for (auto uuid : uuids) { 191 for (auto uuid : uuids) {
151 fake_bt_le_wrapper_->SimulateBLEGattService(simulated_device, uuid); 192 fake_bt_le_wrapper_->SimulateBLEGattService(
193 simulated_device, nullptr, CanonicalStringToBTH_LE_UUID(uuid));
152 } 194 }
153 195
154 bluetooth_task_runner_->RunPendingTasks(); 196 bluetooth_task_runner_->RunPendingTasks();
155 ui_task_runner_->RunPendingTasks(); 197 ui_task_runner_->RunPendingTasks();
156 } 198 }
199
200 void BluetoothTestWin::SimulateGattServiceRemoved(
201 BluetoothGattService* service) {
202 std::string device_address = service->GetDevice()->GetAddress();
203 win::BLEDevice* target_device =
204 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address);
205 CHECK(target_device);
206
207 BluetoothRemoteGattServiceWin* win_service =
208 static_cast<BluetoothRemoteGattServiceWin*>(service);
209 std::string service_att_handle =
210 std::to_string(win_service->GetAttributeHandle());
211 if (service->IsPrimary()) {
212 fake_bt_le_wrapper_->SimulateBLEGattServiceRemove(target_device, nullptr,
213 service_att_handle);
214 } else {
215 win::BLEGattService* target_service =
216 GetSimulatedService(target_device, win_service->GetParentService());
217 fake_bt_le_wrapper_->SimulateBLEGattServiceRemove(
218 target_device, target_service, service_att_handle);
219 }
220
221 ForceRefreshDevice();
222 }
223
224 void BluetoothTestWin::SimulateGattCharacteristic(BluetoothGattService* service,
225 const std::string& uuid,
226 int properties) {
227 std::string device_address = service->GetDevice()->GetAddress();
228 win::BLEDevice* target_device =
229 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address);
230 CHECK(target_device);
231 win::BLEGattService* target_service =
232 GetSimulatedService(target_device, service);
233 CHECK(target_service);
234
235 BTH_LE_GATT_CHARACTERISTIC win_cha_info;
236 win_cha_info.CharacteristicUuid = CanonicalStringToBTH_LE_UUID(uuid);
237 if (properties & BluetoothGattCharacteristic::PROPERTY_BROADCAST)
238 win_cha_info.IsBroadcastable = TRUE;
239 if (properties & BluetoothGattCharacteristic::PROPERTY_READ)
240 win_cha_info.IsReadable = TRUE;
241 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE_WITHOUT_RESPONSE)
242 win_cha_info.IsWritableWithoutResponse = TRUE;
243 if (properties & BluetoothGattCharacteristic::PROPERTY_WRITE)
244 win_cha_info.IsWritable = TRUE;
245 if (properties & BluetoothGattCharacteristic::PROPERTY_NOTIFY)
246 win_cha_info.IsNotifiable = TRUE;
247 if (properties & BluetoothGattCharacteristic::PROPERTY_INDICATE)
248 win_cha_info.IsIndicatable = TRUE;
249 if (properties &
250 BluetoothGattCharacteristic::PROPERTY_AUTHENTICATED_SIGNED_WRITES)
251 win_cha_info.IsSignedWritable = TRUE;
252 if (properties & BluetoothGattCharacteristic::PROPERTY_EXTENDED_PROPERTIES)
253 win_cha_info.HasExtendedProperties = TRUE;
254 fake_bt_le_wrapper_->SimulateCharacterisc(device_address, target_service,
255 win_cha_info);
256
257 ForceRefreshDevice();
258 }
259
260 void BluetoothTestWin::SimulateGattCharacteristicRemove(
261 BluetoothGattService* service,
262 BluetoothGattCharacteristic* characteristic) {
263 CHECK(service);
264 CHECK(characteristic);
265
266 std::string device_address = service->GetDevice()->GetAddress();
267 win::BLEGattService* target_service = GetSimulatedService(
268 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address), service);
269 CHECK(target_service);
270
271 std::string characteristic_att_handle = std::to_string(
272 static_cast<BluetoothRemoteGattCharacteristicWin*>(characteristic)
273 ->GetAttributeHandle());
274 fake_bt_le_wrapper_->SimulateCharacteriscRemove(target_service,
275 characteristic_att_handle);
276
277 ForceRefreshDevice();
278 }
279
280 void BluetoothTestWin::SimulateIncludedGattServicesDiscovered(
281 BluetoothGattService* service,
282 const std::vector<std::string>& uuids) {
283 std::string device_address = service->GetDevice()->GetAddress();
284 win::BLEDevice* target_device =
285 fake_bt_le_wrapper_->GetSimulatedBLEDevice(device_address);
286 CHECK(target_device);
287 win::BLEGattService* target_service =
288 GetSimulatedService(target_device, service);
289 CHECK(target_service);
290
291 for (const auto& uuid : uuids) {
292 fake_bt_le_wrapper_->SimulateBLEGattService(
293 target_device, target_service, CanonicalStringToBTH_LE_UUID(uuid));
294 }
295
296 ForceRefreshDevice();
297 }
298
299 win::BLEGattService* BluetoothTestWin::GetSimulatedService(
300 win::BLEDevice* device,
301 BluetoothGattService* service) {
302 CHECK(device);
303 CHECK(service);
304
305 std::vector<std::string> chain_of_att_handles;
306 BluetoothRemoteGattServiceWin* win_service =
307 static_cast<BluetoothRemoteGattServiceWin*>(service);
308 while (!win_service->IsPrimary()) {
309 chain_of_att_handles.insert(
310 chain_of_att_handles.begin(),
311 std::to_string(win_service->GetAttributeHandle()));
312 win_service = win_service->GetParentService();
313 }
314 chain_of_att_handles.insert(
315 chain_of_att_handles.begin(),
316 std::to_string(win_service->GetAttributeHandle()));
317 win::BLEGattService* simulated_service =
318 fake_bt_le_wrapper_->GetSimulatedGattService(device,
319 chain_of_att_handles);
320 CHECK(simulated_service);
321 return simulated_service;
322 }
323
324 void BluetoothTestWin::ForceRefreshDevice() {
325 adapter_win_->force_update_device_for_test_ = true;
326 bluetooth_task_runner_->RunPendingTasks();
327 ui_task_runner_->RunPendingTasks();
328 }
157 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698