Index: device/bluetooth/bluetooth_remote_gatt_service_chromeos.cc |
diff --git a/device/bluetooth/bluetooth_remote_gatt_service_chromeos.cc b/device/bluetooth/bluetooth_remote_gatt_service_chromeos.cc |
index 2c018f28c46a2647d0db549c42bfe2a3c16923df..dae88081676b435669604fb097ecfbcba18c5d45 100644 |
--- a/device/bluetooth/bluetooth_remote_gatt_service_chromeos.cc |
+++ b/device/bluetooth/bluetooth_remote_gatt_service_chromeos.cc |
@@ -5,11 +5,27 @@ |
#include "device/bluetooth/bluetooth_remote_gatt_service_chromeos.h" |
#include "base/logging.h" |
+#include "base/strings/stringprintf.h" |
#include "chromeos/dbus/bluetooth_gatt_service_client.h" |
#include "chromeos/dbus/dbus_thread_manager.h" |
+#include "device/bluetooth/bluetooth_remote_gatt_characteristic_chromeos.h" |
namespace chromeos { |
+namespace { |
+ |
+// Stream operator for logging vector<uint8>. |
+std::ostream& operator<<(std::ostream& out, const std::vector<uint8> bytes) { |
+ out << "["; |
+ for (std::vector<uint8>::const_iterator iter = bytes.begin(); |
+ iter != bytes.end(); ++iter) { |
+ out << base::StringPrintf("%02X", *iter); |
+ } |
+ return out << "]"; |
+} |
+ |
+} // namespace |
+ |
BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( |
BluetoothDeviceChromeOS* device, |
const dbus::ObjectPath& object_path) |
@@ -19,11 +35,47 @@ BluetoothRemoteGattServiceChromeOS::BluetoothRemoteGattServiceChromeOS( |
VLOG(1) << "Creating remote GATT service with identifier: " |
<< object_path.value() << ", UUID: " << GetUUID().canonical_value(); |
DBusThreadManager::Get()->GetBluetoothGattServiceClient()->AddObserver(this); |
+ DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
+ AddObserver(this); |
+ |
+ // Add all known GATT characteristics. |
+ const std::vector<dbus::ObjectPath> gatt_chars = |
+ DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
+ GetCharacteristics(); |
+ for (std::vector<dbus::ObjectPath>::const_iterator iter = gatt_chars.begin(); |
+ iter != gatt_chars.end(); ++iter) |
+ GattCharacteristicAdded(*iter); |
} |
BluetoothRemoteGattServiceChromeOS::~BluetoothRemoteGattServiceChromeOS() { |
DBusThreadManager::Get()->GetBluetoothGattServiceClient()-> |
RemoveObserver(this); |
+ DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
+ RemoveObserver(this); |
+ |
+ // Clean up all the characteristics. Copy the characteristics list here and |
+ // clear the original so that when we send GattCharacteristicRemoved(), |
+ // GetCharacteristics() returns no characteristics. |
+ CharacteristicMap characteristics = characteristics_; |
+ characteristics_.clear(); |
+ for (CharacteristicMap::iterator iter = characteristics.begin(); |
+ iter != characteristics.end(); ++iter) { |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattCharacteristicRemoved(this, iter->second)); |
+ delete iter->second; |
+ } |
+} |
+ |
+void BluetoothRemoteGattServiceChromeOS::AddObserver( |
+ device::BluetoothGattService::Observer* observer) { |
+ DCHECK(observer); |
+ observers_.AddObserver(observer); |
+} |
+ |
+void BluetoothRemoteGattServiceChromeOS::RemoveObserver( |
+ device::BluetoothGattService::Observer* observer) { |
+ DCHECK(observer); |
+ observers_.RemoveObserver(observer); |
} |
std::string BluetoothRemoteGattServiceChromeOS::GetIdentifier() const { |
@@ -50,26 +102,30 @@ bool BluetoothRemoteGattServiceChromeOS::IsPrimary() const { |
return properties->primary.value(); |
} |
-const std::vector<device::BluetoothGattCharacteristic*>& |
+std::vector<device::BluetoothGattCharacteristic*> |
BluetoothRemoteGattServiceChromeOS::GetCharacteristics() const { |
- return characteristics_; |
+ std::vector<device::BluetoothGattCharacteristic*> characteristics; |
+ for (CharacteristicMap::const_iterator iter = characteristics_.begin(); |
+ iter != characteristics_.end(); ++iter) { |
+ characteristics.push_back(iter->second); |
+ } |
+ return characteristics; |
} |
-const std::vector<device::BluetoothGattService*>& |
+std::vector<device::BluetoothGattService*> |
BluetoothRemoteGattServiceChromeOS::GetIncludedServices() const { |
- return includes_; |
+ // TODO(armansito): Return the actual included services here. |
+ return std::vector<device::BluetoothGattService*>(); |
} |
-void BluetoothRemoteGattServiceChromeOS::AddObserver( |
- device::BluetoothGattService::Observer* observer) { |
- DCHECK(observer); |
- observers_.AddObserver(observer); |
-} |
- |
-void BluetoothRemoteGattServiceChromeOS::RemoveObserver( |
- device::BluetoothGattService::Observer* observer) { |
- DCHECK(observer); |
- observers_.RemoveObserver(observer); |
+device::BluetoothGattCharacteristic* |
+BluetoothRemoteGattServiceChromeOS::GetCharacteristic( |
+ const std::string& identifier) { |
+ CharacteristicMap::const_iterator iter = |
+ characteristics_.find(dbus::ObjectPath(identifier)); |
+ if (iter == characteristics_.end()) |
+ return NULL; |
+ return iter->second; |
} |
bool BluetoothRemoteGattServiceChromeOS::AddCharacteristic( |
@@ -105,4 +161,84 @@ void BluetoothRemoteGattServiceChromeOS::GattServicePropertyChanged( |
GattServiceChanged(this)); |
} |
+void BluetoothRemoteGattServiceChromeOS::GattCharacteristicAdded( |
+ const dbus::ObjectPath& object_path) { |
+ if (characteristics_.find(object_path) != characteristics_.end()) { |
+ VLOG(1) << "Remote GATT characteristic already exists: " |
+ << object_path.value(); |
+ return; |
+ } |
+ |
+ BluetoothGattCharacteristicClient::Properties* properties = |
+ DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
+ GetProperties(object_path); |
+ DCHECK(properties); |
+ if (properties->service.value() != object_path_) { |
+ VLOG(2) << "Remote GATT characteristic does not belong to this service."; |
+ return; |
+ } |
+ |
+ VLOG(1) << "Adding new remote GATT characteristic for GATT service: " |
+ << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
+ |
+ BluetoothRemoteGattCharacteristicChromeOS* characteristic = |
+ new BluetoothRemoteGattCharacteristicChromeOS(this, object_path); |
+ characteristics_[object_path] = characteristic; |
+ DCHECK(characteristic->GetIdentifier() == object_path.value()); |
+ DCHECK(characteristic->GetUUID().IsValid()); |
+ |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattCharacteristicAdded(this, characteristic)); |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattServiceChanged(this)); |
+} |
+ |
+void BluetoothRemoteGattServiceChromeOS::GattCharacteristicRemoved( |
+ const dbus::ObjectPath& object_path) { |
+ CharacteristicMap::iterator iter = characteristics_.find(object_path); |
+ if (iter == characteristics_.end()) { |
+ VLOG(2) << "Unknown GATT characteristic removed: " << object_path.value(); |
+ return; |
+ } |
+ |
+ VLOG(1) << "Removing remote GATT characteristic from service: " |
+ << GetIdentifier() << ", UUID: " << GetUUID().canonical_value(); |
+ |
+ BluetoothRemoteGattCharacteristicChromeOS* characteristic = iter->second; |
+ DCHECK(characteristic->object_path() == object_path); |
+ characteristics_.erase(iter); |
+ |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattCharacteristicRemoved(this, characteristic)); |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattServiceChanged(this)); |
+ |
+ delete characteristic; |
+} |
+ |
+void BluetoothRemoteGattServiceChromeOS::GattCharacteristicPropertyChanged( |
+ const dbus::ObjectPath& object_path, |
+ const std::string& property_name) { |
+ CharacteristicMap::iterator iter = characteristics_.find(object_path); |
+ if (iter == characteristics_.end()) { |
+ VLOG(2) << "Unknown GATT characteristic property changed: " |
+ << object_path.value(); |
+ return; |
+ } |
+ |
+ // Ignore all property changes except for "Value". |
+ BluetoothGattCharacteristicClient::Properties* properties = |
+ DBusThreadManager::Get()->GetBluetoothGattCharacteristicClient()-> |
+ GetProperties(object_path); |
+ DCHECK(properties); |
+ if (property_name != properties->value.name()) |
+ return; |
+ |
+ VLOG(1) << "GATT characteristic value has changed: " << object_path.value() |
+ << ": " << properties->value.value(); |
+ FOR_EACH_OBSERVER(device::BluetoothGattService::Observer, observers_, |
+ GattCharacteristicValueChanged(this, iter->second, |
+ properties->value.value())); |
+} |
+ |
} // namespace chromeos |