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..f2b17d755817bba373126509a1639043655c2d41 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,88 @@ 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() { |
+ 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) { |
+ BluetoothRemoteGattCharacteristicMac* gatt_characteristic_mac = |
+ GetBluetoothRemoteGattCharacteristicMac(cb_characteristic); |
+ if (gatt_characteristic_mac) { |
+ const std::string& identifier = gatt_characteristic_mac->GetIdentifier(); |
+ characteristic_identifier_to_remove.erase(identifier); |
+ continue; |
+ } |
+ gatt_characteristic_mac = |
+ new BluetoothRemoteGattCharacteristicMac(this, cb_characteristic); |
+ const 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) { |
+ 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); |
+} |
+ |
+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 |