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

Side by Side Diff: device/bluetooth/bluetooth_device_win.cc

Issue 1681853003: Add BluetoothRemoteGattServiceWin to BluetoothDeviceWin (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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/bluetooth_device_win.h" 5 #include "device/bluetooth/bluetooth_device_win.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/containers/scoped_ptr_hash_map.h" 9 #include "base/containers/scoped_ptr_hash_map.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/memory/scoped_vector.h" 11 #include "base/memory/scoped_vector.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "device/bluetooth/bluetooth_adapter_win.h" 14 #include "device/bluetooth/bluetooth_adapter_win.h"
15 #include "device/bluetooth/bluetooth_remote_gatt_service_win.h"
15 #include "device/bluetooth/bluetooth_service_record_win.h" 16 #include "device/bluetooth/bluetooth_service_record_win.h"
16 #include "device/bluetooth/bluetooth_socket_thread.h" 17 #include "device/bluetooth/bluetooth_socket_thread.h"
17 #include "device/bluetooth/bluetooth_socket_win.h" 18 #include "device/bluetooth/bluetooth_socket_win.h"
18 #include "device/bluetooth/bluetooth_task_manager_win.h" 19 #include "device/bluetooth/bluetooth_task_manager_win.h"
19 #include "device/bluetooth/bluetooth_uuid.h" 20 #include "device/bluetooth/bluetooth_uuid.h"
20 21
21 namespace { 22 namespace {
22 23
23 const char kApiUnavailable[] = "This API is not implemented on this platform."; 24 const char kApiUnavailable[] = "This API is not implemented on this platform.";
24 25
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 294
294 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator 295 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
295 iter = device_state.service_record_states.begin(); 296 iter = device_state.service_record_states.begin();
296 iter != device_state.service_record_states.end(); ++iter) { 297 iter != device_state.service_record_states.end(); ++iter) {
297 BluetoothServiceRecordWin* service_record = 298 BluetoothServiceRecordWin* service_record =
298 new BluetoothServiceRecordWin(device_state.address, (*iter)->name, 299 new BluetoothServiceRecordWin(device_state.address, (*iter)->name,
299 (*iter)->sdp_bytes, (*iter)->gatt_uuid); 300 (*iter)->sdp_bytes, (*iter)->gatt_uuid);
300 service_record_list_.push_back(service_record); 301 service_record_list_.push_back(service_record);
301 uuids_.push_back(service_record->uuid()); 302 uuids_.push_back(service_record->uuid());
302 } 303 }
304
305 if (!device_state.is_bluetooth_classic())
306 UpdateGattServices(device_state.service_record_states);
307 }
308
309 bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid,
310 uint16_t attribute_handle) {
311 GattServiceMap::iterator it = gatt_services_.begin();
312 for (; it != gatt_services_.end(); it++) {
313 uint16_t it_att_handle =
314 static_cast<BluetoothRemoteGattServiceWin*>(it->second)
315 ->GetAttributeHandle();
316 BluetoothUUID it_uuid = it->second->GetUUID();
317 if (attribute_handle == it_att_handle && uuid == it_uuid) {
318 return true;
319 }
320 }
321 return false;
322 }
323
324 bool BluetoothDeviceWin::IsGattServiceExist(
325 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>&
326 service_state,
327 BluetoothGattService* service) {
328 uint16_t attribute_handle =
329 static_cast<BluetoothRemoteGattServiceWin*>(service)
330 ->GetAttributeHandle();
331 BluetoothUUID uuid = service->GetUUID();
332 ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it =
333 service_state.begin();
334 for (; it != service_state.end(); ++it) {
335 if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid)
336 break;
scheib 2016/02/09 23:24:57 return true; instead of break.
gogerald1 2016/02/10 18:00:56 Done.
337 }
338 // it == service_state.end() means |service| no longer exist.
scheib 2016/02/09 23:24:57 comment and if aren't needed, just return false.
gogerald1 2016/02/10 18:00:56 Done.
339 if (it == service_state.end())
340 return false;
341 return true;
342 }
343
344 void BluetoothDeviceWin::UpdateGattServices(
345 const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>&
346 service_state) {
347 // Remove no longer exist GATT service first.
scheib 2016/02/09 23:24:57 First, remove GATT services that no longer exist.
gogerald1 2016/02/10 18:00:56 Done.
348 if (gatt_services_.size() != 0) {
scheib 2016/02/09 23:24:57 The if is redundant with the for() predicates. Re
gogerald1 2016/02/10 18:00:56 Done.
349 std::vector<std::string> to_be_removed_services;
350 for (auto gatt_service : gatt_services_) {
scheib 2016/02/09 23:24:57 const auto&
gogerald1 2016/02/10 18:00:56 Done.
351 if (!IsGattServiceExist(service_state, gatt_service.second)) {
352 to_be_removed_services.push_back(gatt_service.first);
353 }
354 }
355 for (auto service : to_be_removed_services) {
scheib 2016/02/09 23:24:57 const auto&
gogerald1 2016/02/10 18:00:56 Done.
356 gatt_services_.erase(service);
357 }
358 // Update previously discovered services.
359 for (auto gatt_service : gatt_services_) {
360 static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second)
361 ->Update();
362 }
363 }
364
365 // Return if no new services have been added.
366 if (gatt_services_.size() == service_state.size())
367 return;
368
369 // Add new services.
370 for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator
371 it = service_state.begin();
372 it != service_state.end(); ++it) {
373 if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) {
374 BluetoothRemoteGattServiceWin* primary_service =
375 new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid,
376 (*it)->attribute_handle, true,
377 nullptr, ui_task_runner_);
378 gatt_services_.add(primary_service->GetIdentifier(),
379 scoped_ptr<BluetoothGattService>(primary_service));
380 static_cast<BluetoothAdapterWin*>(adapter_)->NotifyGattServiceAdded(
scheib 2016/02/09 23:24:57 instead of static_cast everywhere make a Bluetooth
gogerald1 2016/02/10 18:00:56 Done.
381 this, primary_service);
382 }
383 }
384
385 static_cast<BluetoothAdapterWin*>(adapter_)->NotifyGattServicesDiscovered(
386 this);
303 } 387 }
304 388
305 } // namespace device 389 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698