Chromium Code Reviews| Index: device/bluetooth/bluetooth_device_win.cc |
| diff --git a/device/bluetooth/bluetooth_device_win.cc b/device/bluetooth/bluetooth_device_win.cc |
| index 8678576feeb91dfb76a3ecc70396962cae082ece..78d23acaf09eb69e9c5348e581c985dea4fed5f0 100644 |
| --- a/device/bluetooth/bluetooth_device_win.cc |
| +++ b/device/bluetooth/bluetooth_device_win.cc |
| @@ -12,6 +12,7 @@ |
| #include "base/sequenced_task_runner.h" |
| #include "base/strings/stringprintf.h" |
| #include "device/bluetooth/bluetooth_adapter_win.h" |
| +#include "device/bluetooth/bluetooth_remote_gatt_service_win.h" |
| #include "device/bluetooth/bluetooth_service_record_win.h" |
| #include "device/bluetooth/bluetooth_socket_thread.h" |
| #include "device/bluetooth/bluetooth_socket_win.h" |
| @@ -300,6 +301,89 @@ void BluetoothDeviceWin::UpdateServices( |
| service_record_list_.push_back(service_record); |
| uuids_.push_back(service_record->uuid()); |
| } |
| + |
| + if (!device_state.is_bluetooth_classic()) |
| + UpdateGattServices(device_state.service_record_states); |
| +} |
| + |
| +bool BluetoothDeviceWin::IsGattServiceDiscovered(BluetoothUUID& uuid, |
| + uint16_t attribute_handle) { |
| + GattServiceMap::iterator it = gatt_services_.begin(); |
| + for (; it != gatt_services_.end(); it++) { |
| + uint16_t it_att_handle = |
| + static_cast<BluetoothRemoteGattServiceWin*>(it->second) |
| + ->GetAttributeHandle(); |
| + BluetoothUUID it_uuid = it->second->GetUUID(); |
| + if (attribute_handle == it_att_handle && uuid == it_uuid) { |
| + return true; |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool BluetoothDeviceWin::IsGattServiceExist( |
| + const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| + service_state, |
| + BluetoothGattService* service) { |
| + uint16_t attribute_handle = |
| + static_cast<BluetoothRemoteGattServiceWin*>(service) |
| + ->GetAttributeHandle(); |
| + BluetoothUUID uuid = service->GetUUID(); |
| + ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator it = |
| + service_state.begin(); |
| + for (; it != service_state.end(); ++it) { |
| + if (attribute_handle == (*it)->attribute_handle && uuid == (*it)->gatt_uuid) |
| + break; |
|
scheib
2016/02/09 23:24:57
return true;
instead of break.
gogerald1
2016/02/10 18:00:56
Done.
|
| + } |
| + // 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.
|
| + if (it == service_state.end()) |
| + return false; |
| + return true; |
| +} |
| + |
| +void BluetoothDeviceWin::UpdateGattServices( |
| + const ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>& |
| + service_state) { |
| + // 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.
|
| + 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.
|
| + std::vector<std::string> to_be_removed_services; |
| + for (auto gatt_service : gatt_services_) { |
|
scheib
2016/02/09 23:24:57
const auto&
gogerald1
2016/02/10 18:00:56
Done.
|
| + if (!IsGattServiceExist(service_state, gatt_service.second)) { |
| + to_be_removed_services.push_back(gatt_service.first); |
| + } |
| + } |
| + for (auto service : to_be_removed_services) { |
|
scheib
2016/02/09 23:24:57
const auto&
gogerald1
2016/02/10 18:00:56
Done.
|
| + gatt_services_.erase(service); |
| + } |
| + // Update previously discovered services. |
| + for (auto gatt_service : gatt_services_) { |
| + static_cast<BluetoothRemoteGattServiceWin*>(gatt_service.second) |
| + ->Update(); |
| + } |
| + } |
| + |
| + // Return if no new services have been added. |
| + if (gatt_services_.size() == service_state.size()) |
| + return; |
| + |
| + // Add new services. |
| + for (ScopedVector<BluetoothTaskManagerWin::ServiceRecordState>::const_iterator |
| + it = service_state.begin(); |
| + it != service_state.end(); ++it) { |
| + if (!IsGattServiceDiscovered((*it)->gatt_uuid, (*it)->attribute_handle)) { |
| + BluetoothRemoteGattServiceWin* primary_service = |
| + new BluetoothRemoteGattServiceWin(this, (*it)->path, (*it)->gatt_uuid, |
| + (*it)->attribute_handle, true, |
| + nullptr, ui_task_runner_); |
| + gatt_services_.add(primary_service->GetIdentifier(), |
| + scoped_ptr<BluetoothGattService>(primary_service)); |
| + 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.
|
| + this, primary_service); |
| + } |
| + } |
| + |
| + static_cast<BluetoothAdapterWin*>(adapter_)->NotifyGattServicesDiscovered( |
| + this); |
| } |
| } // namespace device |