Bluetooth class changes for implementing local GATT attributes.
In this CL, we implement the changes to the platform independent and BlueZ
specific class changes to support local GATT services. The major changes
from the code in trunk are,
.) Cleaning up of the classes and implementing all missing parts.
.) Adding code to properly take ownership of attributes of all three kinds.
.) Adding structure to the attributes to correctly setup the hierarchy
expected (services <-- characteristics <-- descriptors).
.) Addition of an AttributeDelegate to handle calls from DBus and wrappers
to translate them to the BluetoothLocalGattService::Delegate.
.) Cleanup in all the code touched.
Part 1 of a 3 patch series:
https://crrev.com/1915803002 Bluetooth class changes <<<
https://crrev.com/1914893002 DBus class changes
https://crrev.com/1919683002 Adapter changes + tests
[email protected], [email protected]
BUG=601935
Review-Url: https://codereview.chromium.org/1915803002
Cr-Commit-Position: refs/heads/master@{#390315}
diff --git a/device/bluetooth/bluetooth_local_gatt_characteristic.cc b/device/bluetooth/bluetooth_local_gatt_characteristic.cc
index 9d4d37aa..e4350c0 100644
--- a/device/bluetooth/bluetooth_local_gatt_characteristic.cc
+++ b/device/bluetooth/bluetooth_local_gatt_characteristic.cc
@@ -9,19 +9,20 @@
namespace device {
+#if !defined(OS_CHROMEOS) && !defined(OS_LINUX)
+// static
+base::WeakPtr<BluetoothLocalGattCharacteristic>
+BluetoothLocalGattCharacteristic::Create(const BluetoothUUID& uuid,
+ Properties properties,
+ Permissions permissions,
+ BluetoothLocalGattService* service) {
+ NOTIMPLEMENTED();
+ return nullptr;
+}
+#endif
+
BluetoothLocalGattCharacteristic::BluetoothLocalGattCharacteristic() {}
BluetoothLocalGattCharacteristic::~BluetoothLocalGattCharacteristic() {}
-// static
-BluetoothLocalGattCharacteristic* BluetoothLocalGattCharacteristic::Create(
- const BluetoothUUID& uuid,
- const std::vector<uint8_t>& value,
- Properties properties,
- Permissions permissions,
- BluetoothLocalGattService* service) {
- NOTIMPLEMENTED();
- return NULL;
-}
-
} // namespace device
diff --git a/device/bluetooth/bluetooth_local_gatt_characteristic.h b/device/bluetooth/bluetooth_local_gatt_characteristic.h
index f062452e..9170d38 100644
--- a/device/bluetooth/bluetooth_local_gatt_characteristic.h
+++ b/device/bluetooth/bluetooth_local_gatt_characteristic.h
@@ -42,9 +42,8 @@
// characteristic.
// TODO(rkc): Investigate how to handle |PROPERTY_EXTENDED_PROPERTIES|
// correctly.
- static BluetoothLocalGattCharacteristic* Create(
+ static base::WeakPtr<BluetoothLocalGattCharacteristic> Create(
const BluetoothUUID& uuid,
- const std::vector<uint8_t>& value,
Properties properties,
Permissions permissions,
BluetoothLocalGattService* service);
diff --git a/device/bluetooth/bluetooth_local_gatt_descriptor.cc b/device/bluetooth/bluetooth_local_gatt_descriptor.cc
index d13ab43..2ffa979 100644
--- a/device/bluetooth/bluetooth_local_gatt_descriptor.cc
+++ b/device/bluetooth/bluetooth_local_gatt_descriptor.cc
@@ -8,19 +8,20 @@
namespace device {
+#if !defined(OS_CHROMEOS) && !defined(OS_LINUX)
+// static
+base::WeakPtr<BluetoothLocalGattDescriptor>
+BluetoothLocalGattDescriptor::Create(
+ const BluetoothUUID& uuid,
+ BluetoothGattCharacteristic::Permissions permissions,
+ BluetoothLocalGattCharacteristic* characteristic) {
+ NOTIMPLEMENTED();
+ return nullptr;
+}
+#endif
+
BluetoothLocalGattDescriptor::BluetoothLocalGattDescriptor() {}
BluetoothLocalGattDescriptor::~BluetoothLocalGattDescriptor() {}
-// static
-BluetoothLocalGattDescriptor* BluetoothLocalGattDescriptor::Create(
- const BluetoothUUID& uuid,
- const std::vector<uint8_t>& value,
- BluetoothGattCharacteristic::Permissions permissions,
- BluetoothLocalGattCharacteristic* characteristic) {
- LOG(ERROR) << "Creating local GATT characteristic descriptors currently not "
- << "supported.";
- return NULL;
-}
-
} // namespace device
diff --git a/device/bluetooth/bluetooth_local_gatt_descriptor.h b/device/bluetooth/bluetooth_local_gatt_descriptor.h
index 1fdac39d..9f373727 100644
--- a/device/bluetooth/bluetooth_local_gatt_descriptor.h
+++ b/device/bluetooth/bluetooth_local_gatt_descriptor.h
@@ -46,9 +46,8 @@
// |kCharacteristicPresentationFormat| are supported for locally hosted
// descriptors. This method will return NULL if |uuid| is any one of the
// unsupported predefined descriptor UUIDs.
- static BluetoothLocalGattDescriptor* Create(
+ static base::WeakPtr<BluetoothLocalGattDescriptor> Create(
const BluetoothUUID& uuid,
- const std::vector<uint8_t>& value,
BluetoothGattCharacteristic::Permissions permissions,
BluetoothLocalGattCharacteristic* characteristic);
diff --git a/device/bluetooth/bluetooth_local_gatt_service.cc b/device/bluetooth/bluetooth_local_gatt_service.cc
index 17e43ac2..000d6ab 100644
--- a/device/bluetooth/bluetooth_local_gatt_service.cc
+++ b/device/bluetooth/bluetooth_local_gatt_service.cc
@@ -6,6 +6,19 @@
namespace device {
+#if !defined(OS_CHROMEOS) && !defined(OS_LINUX)
+// static
+base::WeakPtr<BluetoothLocalGattService> BluetoothLocalGattService::Create(
+ BluetoothAdapter* adapter,
+ const BluetoothUUID& uuid,
+ bool is_primary,
+ BluetoothLocalGattService* included_service,
+ BluetoothLocalGattService::Delegate* delegate) {
+ NOTIMPLEMENTED();
+ return nullptr;
+}
+#endif
+
BluetoothLocalGattService::BluetoothLocalGattService() {}
BluetoothLocalGattService::~BluetoothLocalGattService() {}
diff --git a/device/bluetooth/bluetooth_local_gatt_service.h b/device/bluetooth/bluetooth_local_gatt_service.h
index 30c1131..668985ae 100644
--- a/device/bluetooth/bluetooth_local_gatt_service.h
+++ b/device/bluetooth/bluetooth_local_gatt_service.h
@@ -44,7 +44,7 @@
class Delegate {
public:
// Callbacks used for communicating GATT request responses.
- typedef base::Callback<void(const std::vector<uint8_t>)> ValueCallback;
+ typedef base::Callback<void(const std::vector<uint8_t>&)> ValueCallback;
typedef base::Closure ErrorCallback;
// Called when a remote device in the central role requests to read the
@@ -74,18 +74,16 @@
// satisfied by the remote device.
//
// To respond to the request with success the delegate must invoke
- // |callback| with the new value of the characteristic. Doing so will
- // automatically update the value property of |characteristic|. To respond
- // to the request with failure (e.g. if an invalid offset was given),
- // delegates must invoke |error_callback|. If neither callback parameter is
- // invoked, the request will time out and result in an error. Therefore,
- // delegates MUST invoke either |callback| or |error_callback|.
+ // |callback|. To respond to the request with failure delegates must invoke
+ // |error_callback|. If neither callback parameter is invoked, the request
+ // will time out and result in an error. Therefore, delegates MUST invoke
+ // either |callback| or |error_callback|.
virtual void OnCharacteristicWriteRequest(
const BluetoothLocalGattService* service,
const BluetoothLocalGattCharacteristic* characteristic,
const std::vector<uint8_t>& value,
int offset,
- const ValueCallback& callback,
+ const base::Closure& callback,
const ErrorCallback& error_callback) = 0;
// Called when a remote device in the central role requests to read the
@@ -115,18 +113,16 @@
// satisfied by the remote device.
//
// To respond to the request with success the delegate must invoke
- // |callback| with the new value of the descriptor. Doing so will
- // automatically update the value property of |descriptor|. To respond
- // to the request with failure (e.g. if an invalid offset was given),
- // delegates must invoke |error_callback|. If neither callback parameter is
- // invoked, the request will time out and result in an error. Therefore,
- // delegates MUST invoke either |callback| or |error_callback|.
+ // |callback|. To respond to the request with failure delegates must invoke
+ // |error_callback|. If neither callback parameter is invoked, the request
+ // will time out and result in an error. Therefore, delegates MUST invoke
+ // either |callback| or |error_callback|.
virtual void OnDescriptorWriteRequest(
const BluetoothLocalGattService* service,
const BluetoothLocalGattDescriptor* descriptor,
const std::vector<uint8_t>& value,
int offset,
- const ValueCallback& callback,
+ const base::Closure& callback,
const ErrorCallback& error_callback) = 0;
};
diff --git a/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.cc b/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.cc
index 2051ef84..34f99be8 100644
--- a/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.cc
@@ -10,7 +10,7 @@
namespace bluez {
BluetoothGattCharacteristicBlueZ::BluetoothGattCharacteristicBlueZ(
- const dbus::ObjectPath& object_path)
+ dbus::ObjectPath object_path)
: object_path_(object_path), weak_ptr_factory_(this) {}
BluetoothGattCharacteristicBlueZ::~BluetoothGattCharacteristicBlueZ() {}
diff --git a/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h b/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h
index 4c0848c..aa96696 100644
--- a/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h
+++ b/device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h
@@ -27,8 +27,7 @@
const dbus::ObjectPath& object_path() const { return object_path_; }
protected:
- explicit BluetoothGattCharacteristicBlueZ(
- const dbus::ObjectPath& object_path);
+ explicit BluetoothGattCharacteristicBlueZ(dbus::ObjectPath object_path);
~BluetoothGattCharacteristicBlueZ() override;
private:
diff --git a/device/bluetooth/bluez/bluetooth_gatt_service_bluez.cc b/device/bluetooth/bluez/bluetooth_gatt_service_bluez.cc
index 0e9d90e..33f5379 100644
--- a/device/bluetooth/bluez/bluetooth_gatt_service_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_gatt_service_bluez.cc
@@ -5,26 +5,16 @@
#include "device/bluetooth/bluez/bluetooth_gatt_service_bluez.h"
#include "base/logging.h"
+#include "device/bluetooth/bluetooth_gatt_service.h"
#include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
+#include "third_party/cros_system_api/dbus/service_constants.h"
namespace bluez {
-namespace {
-
-// TODO(jamuraa) move these to cros_system_api later
-const char kErrorFailed[] = "org.bluez.Error.Failed";
-const char kErrorInProgress[] = "org.bluez.Error.InProgress";
-const char kErrorInvalidValueLength[] = "org.bluez.Error.InvalidValueLength";
-const char kErrorNotAuthorized[] = "org.bluez.Error.NotAuthorized";
-const char kErrorNotPaired[] = "org.bluez.Error.NotPaired";
-const char kErrorNotSupported[] = "org.bluez.Error.NotSupported";
-const char kErrorNotPermitted[] = "org.bluez.Error.NotPermitted";
-
-} // namespace
-
BluetoothGattServiceBlueZ::BluetoothGattServiceBlueZ(
- BluetoothAdapterBlueZ* adapter)
- : adapter_(adapter) {
+ BluetoothAdapterBlueZ* adapter,
+ dbus::ObjectPath object_path)
+ : adapter_(adapter), object_path_(object_path) {
DCHECK(adapter_);
}
@@ -38,19 +28,20 @@
device::BluetoothGattService::GattErrorCode
BluetoothGattServiceBlueZ::DBusErrorToServiceError(std::string error_name) {
device::BluetoothGattService::GattErrorCode code = GATT_ERROR_UNKNOWN;
- if (error_name == kErrorFailed) {
+ if (error_name == bluetooth_gatt_service::kErrorFailed) {
code = GATT_ERROR_FAILED;
- } else if (error_name == kErrorInProgress) {
+ } else if (error_name == bluetooth_gatt_service::kErrorInProgress) {
code = GATT_ERROR_IN_PROGRESS;
- } else if (error_name == kErrorInvalidValueLength) {
+ } else if (error_name == bluetooth_gatt_service::kErrorInvalidValueLength) {
code = GATT_ERROR_INVALID_LENGTH;
- } else if (error_name == kErrorNotPermitted) {
+ } else if (error_name == bluetooth_gatt_service::kErrorReadNotPermitted ||
+ error_name == bluetooth_gatt_service::kErrorWriteNotPermitted) {
code = GATT_ERROR_NOT_PERMITTED;
- } else if (error_name == kErrorNotAuthorized) {
+ } else if (error_name == bluetooth_gatt_service::kErrorNotAuthorized) {
code = GATT_ERROR_NOT_AUTHORIZED;
- } else if (error_name == kErrorNotPaired) {
+ } else if (error_name == bluetooth_gatt_service::kErrorNotPaired) {
code = GATT_ERROR_NOT_PAIRED;
- } else if (error_name == kErrorNotSupported) {
+ } else if (error_name == bluetooth_gatt_service::kErrorNotSupported) {
code = GATT_ERROR_NOT_SUPPORTED;
}
return code;
diff --git a/device/bluetooth/bluez/bluetooth_gatt_service_bluez.h b/device/bluetooth/bluez/bluetooth_gatt_service_bluez.h
index 45932d0..f907f58 100644
--- a/device/bluetooth/bluez/bluetooth_gatt_service_bluez.h
+++ b/device/bluetooth/bluez/bluetooth_gatt_service_bluez.h
@@ -5,9 +5,7 @@
#ifndef DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_GATT_SERVICE_BLUEZ_H_
#define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_GATT_SERVICE_BLUEZ_H_
-#include <map>
#include <string>
-#include <vector>
#include "base/macros.h"
#include "dbus/object_path.h"
@@ -36,12 +34,10 @@
BluetoothAdapterBlueZ* GetAdapter() const;
protected:
- explicit BluetoothGattServiceBlueZ(BluetoothAdapterBlueZ* adapter);
+ BluetoothGattServiceBlueZ(BluetoothAdapterBlueZ* adapter,
+ dbus::ObjectPath object_path);
~BluetoothGattServiceBlueZ() override;
- // Object path of the GATT service.
- dbus::ObjectPath object_path_;
-
private:
friend class BluetoothDeviceBlueZ;
@@ -49,6 +45,9 @@
// here since |adapter_| indirectly owns this instance.
BluetoothAdapterBlueZ* adapter_;
+ // Object path of the GATT service.
+ dbus::ObjectPath object_path_;
+
DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceBlueZ);
};
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.cc b/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.cc
index 95d5cd50..b5d952d 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.cc
@@ -7,19 +7,76 @@
#include <string>
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
#include "device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h"
+namespace device {
+
+// static
+base::WeakPtr<device::BluetoothLocalGattCharacteristic>
+BluetoothLocalGattCharacteristic::Create(
+ const device::BluetoothUUID& uuid,
+ device::BluetoothGattCharacteristic::Properties properties,
+ device::BluetoothGattCharacteristic::Permissions permissions,
+ device::BluetoothLocalGattService* service) {
+ DCHECK(service);
+ bluez::BluetoothLocalGattServiceBlueZ* service_bluez =
+ static_cast<bluez::BluetoothLocalGattServiceBlueZ*>(service);
+ bluez::BluetoothLocalGattCharacteristicBlueZ* characteristic =
+ new bluez::BluetoothLocalGattCharacteristicBlueZ(uuid, service_bluez);
+ return characteristic->weak_ptr_factory_.GetWeakPtr();
+}
+
+} // device
+
namespace bluez {
BluetoothLocalGattCharacteristicBlueZ::BluetoothLocalGattCharacteristicBlueZ(
- BluetoothLocalGattServiceBlueZ* service,
- const dbus::ObjectPath& object_path)
- : BluetoothGattCharacteristicBlueZ(object_path), weak_ptr_factory_(this) {
+ const device::BluetoothUUID& uuid,
+ BluetoothLocalGattServiceBlueZ* service)
+ : BluetoothGattCharacteristicBlueZ(
+ BluetoothLocalGattServiceBlueZ::AddGuidToObjectPath(
+ service->object_path().value() + "/characteristic")),
+ uuid_(uuid),
+ service_(service),
+ weak_ptr_factory_(this) {
VLOG(1) << "Creating local GATT characteristic with identifier: "
<< GetIdentifier();
+ service->AddCharacteristic(base::WrapUnique(this));
}
BluetoothLocalGattCharacteristicBlueZ::
~BluetoothLocalGattCharacteristicBlueZ() {}
+device::BluetoothUUID BluetoothLocalGattCharacteristicBlueZ::GetUUID() const {
+ return uuid_;
+}
+
+device::BluetoothGattCharacteristic::Properties
+BluetoothLocalGattCharacteristicBlueZ::GetProperties() const {
+ NOTIMPLEMENTED();
+ return Properties();
+}
+
+device::BluetoothGattCharacteristic::Permissions
+BluetoothLocalGattCharacteristicBlueZ::GetPermissions() const {
+ NOTIMPLEMENTED();
+ return Permissions();
+}
+
+BluetoothLocalGattServiceBlueZ*
+BluetoothLocalGattCharacteristicBlueZ::GetService() {
+ return service_;
+}
+
+void BluetoothLocalGattCharacteristicBlueZ::AddDescriptor(
+ std::unique_ptr<BluetoothLocalGattDescriptorBlueZ> descriptor) {
+ descriptors_.push_back(std::move(descriptor));
+}
+
+const std::vector<std::unique_ptr<BluetoothLocalGattDescriptorBlueZ>>&
+BluetoothLocalGattCharacteristicBlueZ::GetDescriptors() const {
+ return descriptors_;
+}
+
} // namespace bluez
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h b/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h
index 466d67d..220b2e8b 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h
@@ -5,11 +5,15 @@
#ifndef DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_CHARACTERISTIC_BLUEZ_H_
#define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_CHARACTERISTIC_BLUEZ_H_
+#include <vector>
+
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "dbus/object_path.h"
#include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
+#include "device/bluetooth/bluetooth_local_gatt_service.h"
+#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/bluez/bluetooth_gatt_characteristic_bluez.h"
+#include "device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.h"
namespace bluez {
@@ -22,13 +26,45 @@
: public BluetoothGattCharacteristicBlueZ,
public device::BluetoothLocalGattCharacteristic {
public:
- private:
- friend class BluetoothLocalGattServiceBlueZ;
+ static base::WeakPtr<device::BluetoothLocalGattCharacteristic> Create(
+ const device::BluetoothUUID& uuid,
+ BluetoothGattCharacteristic::Properties properties,
+ BluetoothGattCharacteristic::Permissions permissions,
+ device::BluetoothLocalGattService* service);
- BluetoothLocalGattCharacteristicBlueZ(BluetoothLocalGattServiceBlueZ* service,
- const dbus::ObjectPath& object_path);
+ BluetoothLocalGattCharacteristicBlueZ(
+ const device::BluetoothUUID& uuid,
+ BluetoothLocalGattServiceBlueZ* service);
~BluetoothLocalGattCharacteristicBlueZ() override;
+ // device::BluetoothLocalGattCharacteristic overrides.
+ device::BluetoothUUID GetUUID() const override;
+ Properties GetProperties() const override;
+ Permissions GetPermissions() const override;
+
+ BluetoothLocalGattServiceBlueZ* GetService();
+
+ const std::vector<std::unique_ptr<BluetoothLocalGattDescriptorBlueZ>>&
+ GetDescriptors() const;
+
+ private:
+ friend class BluetoothLocalGattDescriptorBlueZ;
+ // Needs access to weak_ptr_factory_.
+ friend device::BluetoothLocalGattCharacteristic;
+
+ // Adds a descriptor to this characteristic.
+ void AddDescriptor(
+ std::unique_ptr<BluetoothLocalGattDescriptorBlueZ> descriptor);
+
+ // UUID of this characteristic.
+ device::BluetoothUUID uuid_;
+
+ // Service that contains this characteristic.
+ BluetoothLocalGattServiceBlueZ* service_;
+
+ // Descriptors contained by this characteristic.
+ std::vector<std::unique_ptr<BluetoothLocalGattDescriptorBlueZ>> descriptors_;
+
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<BluetoothLocalGattCharacteristicBlueZ> weak_ptr_factory_;
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.cc b/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.cc
index 950d9d9..fd65cc39d 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.cc
@@ -7,16 +7,61 @@
#include <string>
#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h"
+#include "device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h"
+
+namespace device {
+
+// static
+base::WeakPtr<BluetoothLocalGattDescriptor>
+BluetoothLocalGattDescriptor::Create(
+ const BluetoothUUID& uuid,
+ BluetoothGattCharacteristic::Permissions permissions,
+ BluetoothLocalGattCharacteristic* characteristic) {
+ DCHECK(characteristic);
+ bluez::BluetoothLocalGattCharacteristicBlueZ* characteristic_bluez =
+ static_cast<bluez::BluetoothLocalGattCharacteristicBlueZ*>(
+ characteristic);
+ bluez::BluetoothLocalGattDescriptorBlueZ* descriptor =
+ new bluez::BluetoothLocalGattDescriptorBlueZ(uuid, characteristic_bluez);
+ return descriptor->weak_ptr_factory_.GetWeakPtr();
+}
+
+} // device
namespace bluez {
BluetoothLocalGattDescriptorBlueZ::BluetoothLocalGattDescriptorBlueZ(
- const dbus::ObjectPath& object_path)
- : BluetoothGattDescriptorBlueZ(object_path), weak_ptr_factory_(this) {
+ const device::BluetoothUUID& uuid,
+ BluetoothLocalGattCharacteristicBlueZ* characteristic)
+ : BluetoothGattDescriptorBlueZ(
+ BluetoothLocalGattServiceBlueZ::AddGuidToObjectPath(
+ characteristic->object_path().value() + "/descriptor")),
+ uuid_(uuid),
+ characteristic_(characteristic),
+ weak_ptr_factory_(this) {
+ DCHECK(characteristic->GetService());
VLOG(1) << "Creating local GATT descriptor with identifier: "
<< GetIdentifier();
+ characteristic->AddDescriptor(base::WrapUnique(this));
}
BluetoothLocalGattDescriptorBlueZ::~BluetoothLocalGattDescriptorBlueZ() {}
+device::BluetoothUUID BluetoothLocalGattDescriptorBlueZ::GetUUID() const {
+ return uuid_;
+}
+
+device::BluetoothGattCharacteristic::Permissions
+BluetoothLocalGattDescriptorBlueZ::GetPermissions() const {
+ NOTIMPLEMENTED();
+ return device::BluetoothGattCharacteristic::Permissions();
+}
+
+BluetoothLocalGattCharacteristicBlueZ*
+BluetoothLocalGattDescriptorBlueZ::GetCharacteristic() const {
+ return characteristic_;
+}
+
} // namespace bluez
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.h b/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.h
index 566070e..3520ef3 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.h
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_descriptor_bluez.h
@@ -7,25 +7,48 @@
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
-#include "dbus/object_path.h"
+#include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
+#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/bluez/bluetooth_gatt_descriptor_bluez.h"
namespace bluez {
+class BluetoothLocalGattCharacteristicBlueZ;
+
// The BluetoothLocalGattDescriptorBlueZ class implements
// BluetoothRemoteGattDescriptor for remote and local GATT characteristic
-// descriptors
-// for platforms that use BlueZ.
+// descriptors for platforms that use BlueZ.
class BluetoothLocalGattDescriptorBlueZ
: public BluetoothGattDescriptorBlueZ,
public device::BluetoothLocalGattDescriptor {
public:
- private:
- explicit BluetoothLocalGattDescriptorBlueZ(
- const dbus::ObjectPath& object_path);
+ static base::WeakPtr<BluetoothLocalGattDescriptor> Create(
+ const device::BluetoothUUID& uuid,
+ device::BluetoothGattCharacteristic::Permissions permissions,
+ device::BluetoothLocalGattCharacteristic* characteristic);
+
+ BluetoothLocalGattDescriptorBlueZ(
+ const device::BluetoothUUID& uuid,
+ BluetoothLocalGattCharacteristicBlueZ* characteristic);
~BluetoothLocalGattDescriptorBlueZ() override;
+ // device::BluetoothLocalGattDescriptor overrides.
+ device::BluetoothUUID GetUUID() const override;
+ device::BluetoothGattCharacteristic::Permissions GetPermissions()
+ const override;
+
+ BluetoothLocalGattCharacteristicBlueZ* GetCharacteristic() const;
+
+ private:
+ // Needs access to weak_ptr_factory_.
+ friend class device::BluetoothLocalGattDescriptor;
+ // UUID of this descriptor.
+ device::BluetoothUUID uuid_;
+
+ // Characteristic that contains this descriptor.
+ BluetoothLocalGattCharacteristicBlueZ* characteristic_;
+
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<BluetoothLocalGattDescriptorBlueZ> weak_ptr_factory_;
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.cc b/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.cc
index 267b047..f965c8e76 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.cc
@@ -2,9 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h"
+#include <device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h>
-#include "base/callback.h"
#include "base/guid.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
@@ -12,6 +11,25 @@
#include "dbus/object_path.h"
#include "device/bluetooth/bluez/bluetooth_adapter_bluez.h"
+namespace device {
+
+// static
+base::WeakPtr<BluetoothLocalGattService> BluetoothLocalGattService::Create(
+ BluetoothAdapter* adapter,
+ const BluetoothUUID& uuid,
+ bool is_primary,
+ BluetoothLocalGattService* included_service,
+ BluetoothLocalGattService::Delegate* delegate) {
+ bluez::BluetoothAdapterBlueZ* adapter_bluez =
+ static_cast<bluez::BluetoothAdapterBlueZ*>(adapter);
+ bluez::BluetoothLocalGattServiceBlueZ* service =
+ new bluez::BluetoothLocalGattServiceBlueZ(adapter_bluez, uuid, is_primary,
+ delegate);
+ return service->weak_ptr_factory_.GetWeakPtr();
+}
+
+} // device
+
namespace bluez {
BluetoothLocalGattServiceBlueZ::BluetoothLocalGattServiceBlueZ(
@@ -19,19 +37,14 @@
const device::BluetoothUUID& uuid,
bool is_primary,
device::BluetoothLocalGattService::Delegate* delegate)
- : BluetoothGattServiceBlueZ(adapter),
+ : BluetoothGattServiceBlueZ(adapter, AddGuidToObjectPath("/service")),
uuid_(uuid),
is_primary_(is_primary),
delegate_(delegate),
weak_ptr_factory_(this) {
- // TODO(rkc): Move this code in a common location. It is used by
- // BluetoothAdvertisementBlueZ() also.
- std::string GuidString = base::GenerateGUID();
- base::RemoveChars(GuidString, "-", &GuidString);
- object_path_ = dbus::ObjectPath(adapter_->object_path().value() +
- "/service/" + GuidString);
- VLOG(1) << "Creating local GATT service with identifier: "
- << object_path_.value();
+ // TODO(rkc): Get base application path from adapter and prefix it here.
+ VLOG(1) << "Creating local GATT service with identifier: " << GetIdentifier();
+ adapter->AddLocalGattService(base::WrapUnique(this));
}
BluetoothLocalGattServiceBlueZ::~BluetoothLocalGattServiceBlueZ() {}
@@ -44,42 +57,36 @@
return is_primary_;
}
-// static
-base::WeakPtr<device::BluetoothLocalGattService>
-BluetoothLocalGattServiceBlueZ::Create(
- device::BluetoothAdapter* adapter,
- const device::BluetoothUUID& uuid,
- bool is_primary,
- BluetoothLocalGattService* /* included_service */,
- BluetoothLocalGattService::Delegate* delegate) {
- BluetoothAdapterBlueZ* adapter_bluez =
- static_cast<BluetoothAdapterBlueZ*>(adapter);
- BluetoothLocalGattServiceBlueZ* service = new BluetoothLocalGattServiceBlueZ(
- adapter_bluez, uuid, is_primary, delegate);
- adapter_bluez->AddLocalGattService(base::WrapUnique(service));
- return service->weak_ptr_factory_.GetWeakPtr();
-}
-
void BluetoothLocalGattServiceBlueZ::Register(
const base::Closure& callback,
const ErrorCallback& error_callback) {
- // TODO(rkc): Call adapter_->RegisterGattService.
+ // GetAdapter()->RegisterGattService(this, callback, error_callback);
}
void BluetoothLocalGattServiceBlueZ::Unregister(
const base::Closure& callback,
const ErrorCallback& error_callback) {
- // TODO(rkc): Call adapter_->UnregisterGattService.
+ DCHECK(GetAdapter());
+ // GetAdapter()->UnregisterGattService(this, callback, error_callback);
}
-void BluetoothLocalGattServiceBlueZ::OnRegistrationError(
- const ErrorCallback& error_callback,
- const std::string& error_name,
- const std::string& error_message) {
- VLOG(1) << "[Un]Register Service failed: " << error_name
- << ", message: " << error_message;
- error_callback.Run(
- BluetoothGattServiceBlueZ::DBusErrorToServiceError(error_name));
+const std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>>&
+BluetoothLocalGattServiceBlueZ::GetCharacteristics() const {
+ return characteristics_;
+}
+
+// static
+dbus::ObjectPath BluetoothLocalGattServiceBlueZ::AddGuidToObjectPath(
+ const std::string& path) {
+ std::string GuidString = base::GenerateGUID();
+ base::RemoveChars(GuidString, "-", &GuidString);
+
+ return dbus::ObjectPath(path + GuidString);
+}
+
+void BluetoothLocalGattServiceBlueZ::AddCharacteristic(
+ std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ> characteristic) {
+ characteristics_.push_back(std::move(characteristic));
}
} // namespace bluez
diff --git a/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h b/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h
index aba656b..059fc53 100644
--- a/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h
+++ b/device/bluetooth/bluez/bluetooth_local_gatt_service_bluez.h
@@ -6,6 +6,7 @@
#define DEVICE_BLUETOOTH_BLUEZ_BLUETOOTH_LOCAL_GATT_SERVICE_BLUEZ_H_
#include <string>
+#include <vector>
#include "base/callback_forward.h"
#include "base/macros.h"
@@ -13,6 +14,7 @@
#include "device/bluetooth/bluetooth_local_gatt_service.h"
#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/bluez/bluetooth_gatt_service_bluez.h"
+#include "device/bluetooth/bluez/bluetooth_local_gatt_characteristic_bluez.h"
namespace device {
@@ -24,7 +26,7 @@
namespace bluez {
class BluetoothAdapterBlueZ;
-class BluetoothDeviceBlueZ;
+class BluetoothLocalGattCharacteristicBlueZ;
// The BluetoothLocalGattServiceBlueZ class implements BluetootGattService
// for local GATT services for platforms that use BlueZ.
@@ -32,41 +34,66 @@
: public BluetoothGattServiceBlueZ,
public device::BluetoothLocalGattService {
public:
- // device::BluetoothGattService overrides.
- device::BluetoothUUID GetUUID() const override;
- bool IsPrimary() const override;
-
- // device::BluetoothLocalGattService overrides.
static base::WeakPtr<device::BluetoothLocalGattService> Create(
device::BluetoothAdapter* adapter,
const device::BluetoothUUID& uuid,
bool is_primary,
BluetoothLocalGattService* included_service,
BluetoothLocalGattService::Delegate* delegate);
- void Register(const base::Closure& callback,
- const ErrorCallback& error_callback) override;
- void Unregister(const base::Closure& callback,
- const ErrorCallback& error_callback) override;
BluetoothLocalGattServiceBlueZ(
BluetoothAdapterBlueZ* adapter,
const device::BluetoothUUID& uuid,
bool is_primary,
device::BluetoothLocalGattService::Delegate* delegate);
+
~BluetoothLocalGattServiceBlueZ() override;
+ // device::BluetoothGattService overrides.
+ device::BluetoothUUID GetUUID() const override;
+ bool IsPrimary() const override;
+
+ // device::BluetoothLocalGattService overrides.
+ void Register(const base::Closure& callback,
+ const ErrorCallback& error_callback) override;
+ void Unregister(const base::Closure& callback,
+ const ErrorCallback& error_callback) override;
+
+ const std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>>&
+ GetCharacteristics() const;
+
+ Delegate* GetDelegate() { return delegate_; }
+
+ static dbus::ObjectPath AddGuidToObjectPath(const std::string& path);
+
private:
+ friend class BluetoothLocalGattCharacteristicBlueZ;
+ // Needs access to weak_ptr_factory_.
+ friend class device::BluetoothLocalGattService;
+
// Called by dbus:: on unsuccessful completion of a request to register a
// local service.
void OnRegistrationError(const ErrorCallback& error_callback,
const std::string& error_name,
const std::string& error_message);
- BluetoothAdapterBlueZ* adapter_;
- const device::BluetoothUUID& uuid_;
+ void AddCharacteristic(
+ std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ> characteristic);
+
+ // UUID of this service.
+ device::BluetoothUUID uuid_;
+
+ // If this service is primary.
bool is_primary_;
+
+ // Delegate to receive read/write requests for attribute values contained
+ // in this service.
device::BluetoothLocalGattService::Delegate* delegate_;
+ // Characteristics contained by this service.
+ std::vector<std::unique_ptr<BluetoothLocalGattCharacteristicBlueZ>>
+ characteristics_;
+
// Note: This should remain the last member so it'll be destroyed and
// invalidate its weak pointers before any other members are destroyed.
base::WeakPtrFactory<BluetoothLocalGattServiceBlueZ> weak_ptr_factory_;
diff --git a/device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.cc b/device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.cc
index 76cbbc0..d409a34 100644
--- a/device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.cc
+++ b/device/bluetooth/bluez/bluetooth_remote_gatt_service_bluez.cc
@@ -20,11 +20,10 @@
BluetoothAdapterBlueZ* adapter,
BluetoothDeviceBlueZ* device,
const dbus::ObjectPath& object_path)
- : BluetoothGattServiceBlueZ(adapter),
+ : BluetoothGattServiceBlueZ(adapter, object_path),
device_(device),
discovery_complete_(false),
weak_ptr_factory_(this) {
- object_path_ = object_path;
VLOG(1) << "Creating remote GATT service with identifier: "
<< object_path.value();
DCHECK(GetAdapter());
diff --git a/device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.cc b/device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.cc
index 15a2190..40f68fa 100644
--- a/device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.cc
+++ b/device/bluetooth/dbus/fake_bluetooth_gatt_characteristic_client.cc
@@ -134,23 +134,24 @@
const ValueCallback& callback,
const ErrorCallback& error_callback) {
if (!authenticated_) {
- error_callback.Run("org.bluez.Error.NotPaired", "Please login");
+ error_callback.Run(bluetooth_gatt_service::kErrorNotPaired, "Please login");
return;
}
if (!authorized_) {
- error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first");
+ error_callback.Run(bluetooth_gatt_service::kErrorNotAuthorized,
+ "Authorize first");
return;
}
if (object_path.value() == heart_rate_control_point_path_) {
- error_callback.Run("org.bluez.Error.NotPermitted",
+ error_callback.Run(bluetooth_gatt_service::kErrorReadNotPermitted,
"Reads of this value are not allowed");
return;
}
if (object_path.value() == heart_rate_measurement_path_) {
- error_callback.Run("org.bluez.Error.NotSupported",
+ error_callback.Run(bluetooth_gatt_service::kErrorNotSupported,
"Action not supported on this characteristic");
return;
}
@@ -164,7 +165,7 @@
action_extra_requests_.end()) {
DelayedCallback* delayed = action_extra_requests_["ReadValue"];
delayed->delay_--;
- error_callback.Run("org.bluez.Error.InProgress",
+ error_callback.Run(bluetooth_gatt_service::kErrorInProgress,
"Another read is currenty in progress");
if (delayed->delay_ == 0) {
delayed->callback_.Run();
@@ -200,12 +201,13 @@
const base::Closure& callback,
const ErrorCallback& error_callback) {
if (!authenticated_) {
- error_callback.Run("org.bluez.Error.NotPaired", "Please login");
+ error_callback.Run(bluetooth_gatt_service::kErrorNotPaired, "Please login");
return;
}
if (!authorized_) {
- error_callback.Run("org.bluez.Error.NotAuthorized", "Authorize first");
+ error_callback.Run(bluetooth_gatt_service::kErrorNotAuthorized,
+ "Authorize first");
return;
}
@@ -215,13 +217,13 @@
}
if (object_path.value() == heart_rate_measurement_path_) {
- error_callback.Run("org.bluez.Error.NotSupported",
+ error_callback.Run(bluetooth_gatt_service::kErrorNotSupported,
"Action not supported on this characteristic");
return;
}
if (object_path.value() != heart_rate_control_point_path_) {
- error_callback.Run("org.bluez.Error.NotPermitted",
+ error_callback.Run(bluetooth_gatt_service::kErrorWriteNotPermitted,
"Writes of this value are not allowed");
return;
}
@@ -231,7 +233,7 @@
action_extra_requests_.end()) {
DelayedCallback* delayed = action_extra_requests_["WriteValue"];
delayed->delay_--;
- error_callback.Run("org.bluez.Error.InProgress",
+ error_callback.Run(bluetooth_gatt_service::kErrorInProgress,
"Another write is in progress");
if (delayed->delay_ == 0) {
delayed->callback_.Run();
@@ -242,12 +244,13 @@
}
base::Closure completed_callback;
if (value.size() != 1) {
- completed_callback =
- base::Bind(error_callback, "org.bluez.Error.InvalidValueLength",
- "Invalid length for write");
+ completed_callback = base::Bind(
+ error_callback, bluetooth_gatt_service::kErrorInvalidValueLength,
+ "Invalid length for write");
} else if (value[0] > 1) {
- completed_callback = base::Bind(error_callback, "org.bluez.Error.Failed",
- "Invalid value given for write");
+ completed_callback =
+ base::Bind(error_callback, bluetooth_gatt_service::kErrorFailed,
+ "Invalid value given for write");
} else if (value[0] == 1) {
// TODO(jamuraa): make this happen when the callback happens
calories_burned_ = 0;
@@ -272,13 +275,13 @@
}
if (object_path.value() != heart_rate_measurement_path_) {
- error_callback.Run("org.bluez.Error.NotSupported",
+ error_callback.Run(bluetooth_gatt_service::kErrorNotSupported,
"This characteristic does not support notifications");
return;
}
if (heart_rate_measurement_properties_->notifying.value()) {
- error_callback.Run("org.bluez.Error.InProgress",
+ error_callback.Run(bluetooth_gatt_service::kErrorInProgress,
"Characteristic already notifying");
return;
}
@@ -302,13 +305,13 @@
}
if (object_path.value() != heart_rate_measurement_path_) {
- error_callback.Run("org.bluez.Error.NotSupported",
+ error_callback.Run(bluetooth_gatt_service::kErrorNotSupported,
"This characteristic does not support notifications");
return;
}
if (!heart_rate_measurement_properties_->notifying.value()) {
- error_callback.Run("org.bluez.Error.Failed", "Not notifying");
+ error_callback.Run(bluetooth_gatt_service::kErrorFailed, "Not notifying");
return;
}
diff --git a/device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.cc b/device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.cc
index c0dfc3b..434e97ff 100644
--- a/device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.cc
+++ b/device/bluetooth/dbus/fake_bluetooth_gatt_descriptor_client.cc
@@ -130,7 +130,7 @@
// Since the only fake descriptor is "Client Characteristic Configuration"
// and BlueZ doesn't allow writing to it, return failure.
- error_callback.Run("org.bluez.Error.NotPermitted",
+ error_callback.Run(bluetooth_gatt_service::kErrorWriteNotPermitted,
"Writing to the Client Characteristic Configuration "
"descriptor not allowed");
}