Chromium Code Reviews| Index: device/bluetooth/bluetooth_remote_gatt_service_mac.mm |
| diff --git a/device/bluetooth/bluetooth_remote_gatt_service_mac.mm b/device/bluetooth/bluetooth_remote_gatt_service_mac.mm |
| index 2e840703e40d32391166aa9f42ca0fcaa277b2ab..75189c0dfdaa97c76df66de02ae62d1b6e01dcb1 100644 |
| --- a/device/bluetooth/bluetooth_remote_gatt_service_mac.mm |
| +++ b/device/bluetooth/bluetooth_remote_gatt_service_mac.mm |
| @@ -8,8 +8,10 @@ |
| #include <vector> |
| #include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| +#include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
| #include "device/bluetooth/bluetooth_uuid.h" |
| namespace device { |
| @@ -20,7 +22,8 @@ BluetoothRemoteGattServiceMac::BluetoothRemoteGattServiceMac( |
| bool is_primary) |
| : bluetooth_device_mac_(bluetooth_device_mac), |
| service_(service, base::scoped_policy::RETAIN), |
| - is_primary_(is_primary) { |
| + is_primary_(is_primary), |
| + is_discovery_complete_(false) { |
| uuid_ = BluetoothAdapterMac::BluetoothUUIDWithCBUUID([service_.get() UUID]); |
| identifier_ = |
| [NSString stringWithFormat:@"%s-%p", uuid_.canonical_value().c_str(), |
| @@ -48,8 +51,13 @@ BluetoothDevice* BluetoothRemoteGattServiceMac::GetDevice() const { |
| std::vector<BluetoothRemoteGattCharacteristic*> |
| BluetoothRemoteGattServiceMac::GetCharacteristics() const { |
| - NOTIMPLEMENTED(); |
| - return std::vector<BluetoothRemoteGattCharacteristic*>(); |
| + std::vector<BluetoothRemoteGattCharacteristic*> gatt_characteristics; |
| + for (const auto& iter : gatt_characteristic_macs_) { |
| + BluetoothRemoteGattCharacteristic* gatt_characteristic = |
| + static_cast<BluetoothRemoteGattCharacteristic*>(iter.second.get()); |
| + gatt_characteristics.push_back(gatt_characteristic); |
| + } |
| + return gatt_characteristics; |
| } |
| std::vector<BluetoothRemoteGattService*> |
| @@ -61,12 +69,86 @@ BluetoothRemoteGattServiceMac::GetIncludedServices() const { |
| BluetoothRemoteGattCharacteristic* |
| BluetoothRemoteGattServiceMac::GetCharacteristic( |
| const std::string& identifier) const { |
| - NOTIMPLEMENTED(); |
| - return nullptr; |
| + auto searched_pair = gatt_characteristic_macs_.find(identifier); |
| + if (searched_pair == gatt_characteristic_macs_.end()) { |
| + return nullptr; |
| + } |
| + return static_cast<BluetoothRemoteGattCharacteristic*>( |
| + searched_pair->second.get()); |
| +} |
| + |
| +void BluetoothRemoteGattServiceMac::DiscoverCharacteristics() { |
| + is_discovery_complete_ = false; |
| + [GetCBPeripheral() discoverCharacteristics:nil forService:GetService()]; |
| +} |
| + |
| +void BluetoothRemoteGattServiceMac::DidDiscoverCharacteristics() { |
|
ortuno
2016/06/15 19:28:51
When would you call this method for a service for
jlebel
2016/06/15 20:28:46
Every time at least one characteristic is added, r
|
| + DCHECK(!is_discovery_complete_); |
| + std::unordered_set<std::string> characteristic_identifier_to_remove; |
| + for (const auto& iter : gatt_characteristic_macs_) { |
| + characteristic_identifier_to_remove.insert(iter.first); |
| + } |
| + for (CBCharacteristic* cb_characteristic in GetService().characteristics) { |
|
ortuno
2016/06/15 19:28:51
nit: new line above.
jlebel
2016/06/15 20:28:47
Done.
|
| + BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = |
| + GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
| + if (gatt_characteristic_mac) { |
| + std::string identifier = gatt_characteristic_mac->GetIdentifier(); |
|
ortuno
2016/06/15 19:28:51
nit: const std::string&
ortuno
2016/06/15 19:28:51
nit: const std::string&
jlebel
2016/06/15 20:28:47
Done.
jlebel
2016/06/15 20:28:47
Done.
|
| + characteristic_identifier_to_remove.erase(identifier); |
| + continue; |
| + } |
| + gatt_characteristic_mac = |
| + new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); |
| + std::string identifier = gatt_characteristic_mac->GetIdentifier(); |
| + auto result_iter = gatt_characteristic_macs_.insert( |
| + {identifier, base::WrapUnique(gatt_characteristic_mac)}); |
| + DCHECK(result_iter.second); |
| + GetMacAdapter()->NotifyGattCharacteristicAdded(gatt_characteristic_mac); |
| + } |
| + for (const std::string& identifier : characteristic_identifier_to_remove) { |
|
ortuno
2016/06/15 19:28:51
nit: new line above.
jlebel
2016/06/15 20:28:47
Done.
|
| + auto pair_to_remove = gatt_characteristic_macs_.find(identifier); |
| + std::unique_ptr<BluetoothRemoteGattCharacteristicMac> |
| + characteristic_to_remove; |
| + pair_to_remove->second.swap(characteristic_to_remove); |
| + gatt_characteristic_macs_.erase(pair_to_remove); |
| + GetMacAdapter()->NotifyGattCharacteristicRemoved( |
| + characteristic_to_remove.get()); |
| + } |
| + is_discovery_complete_ = true; |
| + GetMacAdapter()->NotifyGattServiceChanged(this); |
|
ortuno
2016/06/15 19:28:51
I don't think we want to call this here. See bluez
jlebel
2016/06/15 20:28:47
If I don't put this line, BluetoothRemoteGattServi
ortuno
2016/06/15 20:57:47
*sigh* we really need fix the order of our events.
|
| +} |
| + |
| +bool BluetoothRemoteGattServiceMac::IsDiscoveryComplete() { |
| + return is_discovery_complete_; |
| +} |
| + |
| +BluetoothAdapterMac* BluetoothRemoteGattServiceMac::GetMacAdapter() const { |
| + return bluetooth_device_mac_->GetMacAdapter(); |
| +} |
| + |
| +CBPeripheral* BluetoothRemoteGattServiceMac::GetCBPeripheral() const { |
| + return bluetooth_device_mac_->GetPeripheral(); |
| } |
| CBService* BluetoothRemoteGattServiceMac::GetService() const { |
| return service_.get(); |
| } |
| +BluetoothRemoteGattCharacteristicMac* |
| +BluetoothRemoteGattServiceMac::GetBluetoothRemoteGattCharacteristicMac( |
| + CBCharacteristic* characteristic) const { |
| + auto found = std::find_if( |
| + gatt_characteristic_macs_.begin(), gatt_characteristic_macs_.end(), |
| + [characteristic]( |
| + const std::pair< |
| + const std::string, |
| + std::unique_ptr<BluetoothRemoteGattCharacteristicMac>>& pair) { |
| + return pair.second->GetCBCharacteristic() == characteristic; |
| + }); |
| + if (found == gatt_characteristic_macs_.end()) { |
| + return nullptr; |
| + } else { |
| + return found->second.get(); |
| + } |
| +} |
| + |
| } // namespace device |