This source file includes following definitions.
- weak_ptr_factory_
- SendValueChanged
- OnOriginThread
- Get
- Set
- GetAll
- OnExported
- OnGetAll
- OnGet
- OnSet
- OnFailure
- BluetoothGattDescriptorServiceProvider
- BluetoothGattDescriptorServiceProvider
- Create
#include "chromeos/dbus/bluetooth_gatt_descriptor_service_provider.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_util.h"
#include "base/sys_info.h"
#include "base/threading/platform_thread.h"
#include "chromeos/dbus/fake_bluetooth_gatt_descriptor_service_provider.h"
#include "dbus/exported_object.h"
#include "dbus/message.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
namespace {
const char kErrorInvalidArgs[] =
"org.freedesktop.DBus.Error.InvalidArgs";
const char kErrorPropertyReadOnly[] =
"org.freedesktop.DBus.Error.PropertyReadOnly";
const char kErrorFailed[] =
"org.freedesktop.DBus.Error.Failed";
}
class BluetoothGattDescriptorServiceProviderImpl
: public BluetoothGattDescriptorServiceProvider {
public:
BluetoothGattDescriptorServiceProviderImpl(
dbus::Bus* bus,
const dbus::ObjectPath& object_path,
Delegate* delegate,
const std::string& uuid,
const std::vector<std::string>& permissions,
const dbus::ObjectPath& characteristic_path)
: origin_thread_id_(base::PlatformThread::CurrentId()),
uuid_(uuid),
bus_(bus),
delegate_(delegate),
object_path_(object_path),
characteristic_path_(characteristic_path),
weak_ptr_factory_(this) {
VLOG(1) << "Created Bluetooth GATT characteristic descriptor: "
<< object_path.value() << " UUID: " << uuid;
DCHECK(bus_);
DCHECK(delegate_);
DCHECK(!uuid_.empty());
DCHECK(object_path_.IsValid());
DCHECK(characteristic_path_.IsValid());
DCHECK(StartsWithASCII(
object_path_.value(), characteristic_path_.value() + "/", true));
exported_object_ = bus_->GetExportedObject(object_path_);
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGet,
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::Get,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesSet,
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::Set,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGetAll,
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::GetAll,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
}
virtual ~BluetoothGattDescriptorServiceProviderImpl() {
VLOG(1) << "Cleaning up Bluetooth GATT characteristic descriptor: "
<< object_path_.value();
bus_->UnregisterExportedObject(object_path_);
}
virtual void SendValueChanged(const std::vector<uint8>& value) OVERRIDE {
VLOG(2) << "Emitting a PropertiesChanged signal for descriptor value.";
dbus::Signal signal(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesChangedSignal);
dbus::MessageWriter writer(&signal);
dbus::MessageWriter array_writer(NULL);
dbus::MessageWriter dict_entry_writer(NULL);
dbus::MessageWriter variant_writer(NULL);
writer.AppendString(
bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface);
writer.OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(bluetooth_gatt_descriptor::kValueProperty);
dict_entry_writer.OpenVariant("ay", &variant_writer);
variant_writer.AppendArrayOfBytes(value.data(), value.size());
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
writer.OpenArray("s", &array_writer);
writer.CloseContainer(&array_writer);
exported_object_->SendSignal(&signal);
}
private:
bool OnOriginThread() {
return base::PlatformThread::CurrentId() == origin_thread_id_;
}
void Get(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattDescriptorServiceProvider::Get: "
<< object_path_.value();
DCHECK(OnOriginThread());
dbus::MessageReader reader(method_call);
std::string interface_name;
std::string property_name;
if (!reader.PopString(&interface_name) ||
!reader.PopString(&property_name) ||
reader.HasMoreData()) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs, "Expected 'ss'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
if (interface_name !=
bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"No such interface: '" + interface_name + "'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
if (property_name == bluetooth_gatt_descriptor::kValueProperty) {
DCHECK(delegate_);
delegate_->GetDescriptorValue(
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGet,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender));
return;
}
scoped_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
dbus::MessageWriter writer(response.get());
dbus::MessageWriter variant_writer(NULL);
if (property_name == bluetooth_gatt_descriptor::kUUIDProperty) {
writer.OpenVariant("s", &variant_writer);
variant_writer.AppendString(uuid_);
writer.CloseContainer(&variant_writer);
} else if (property_name ==
bluetooth_gatt_descriptor::kCharacteristicProperty) {
writer.OpenVariant("o", &variant_writer);
variant_writer.AppendObjectPath(characteristic_path_);
writer.CloseContainer(&variant_writer);
} else {
response = dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"No such property: '" + property_name + "'.")
.PassAs<dbus::Response>();
}
response_sender.Run(response.Pass());
}
void Set(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattDescriptorServiceProvider::Set: "
<< object_path_.value();
DCHECK(OnOriginThread());
dbus::MessageReader reader(method_call);
std::string interface_name;
std::string property_name;
dbus::MessageReader variant_reader(NULL);
if (!reader.PopString(&interface_name) ||
!reader.PopString(&property_name) ||
!reader.PopVariant(&variant_reader) ||
reader.HasMoreData()) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs, "Expected 'ssv'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
if (interface_name !=
bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"No such interface: '" + interface_name + "'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
if (property_name != bluetooth_gatt_descriptor::kValueProperty) {
std::string error_name;
std::string error_message;
if (property_name == bluetooth_gatt_descriptor::kUUIDProperty ||
property_name == bluetooth_gatt_descriptor::kCharacteristicProperty) {
error_name = kErrorPropertyReadOnly;
error_message = "Read-only property: '" + property_name + "'.";
} else {
error_name = kErrorInvalidArgs;
error_message = "No such property: '" + property_name + "'.";
}
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, error_name, error_message);
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
const uint8* bytes = NULL;
size_t length = 0;
if (!variant_reader.PopArrayOfBytes(&bytes, &length)) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"Property '" + property_name + "' has type 'ay'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
std::vector<uint8> value(bytes, bytes + length);
DCHECK(delegate_);
delegate_->SetDescriptorValue(
value,
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnSet,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender));
}
void GetAll(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattDescriptorServiceProvider::GetAll: "
<< object_path_.value();
DCHECK(OnOriginThread());
dbus::MessageReader reader(method_call);
std::string interface_name;
if (!reader.PopString(&interface_name) || reader.HasMoreData()) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs, "Expected 's'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
if (interface_name !=
bluetooth_gatt_descriptor::kBluetoothGattDescriptorInterface) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"No such interface: '" + interface_name + "'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
return;
}
DCHECK(delegate_);
delegate_->GetDescriptorValue(
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnGetAll,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender),
base::Bind(&BluetoothGattDescriptorServiceProviderImpl::OnFailure,
weak_ptr_factory_.GetWeakPtr(),
method_call, response_sender));
}
void OnExported(const std::string& interface_name,
const std::string& method_name,
bool success) {
LOG_IF(WARNING, !success) << "Failed to export "
<< interface_name << "." << method_name;
}
void OnGetAll(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender,
const std::vector<uint8>& value) {
VLOG(2) << "Descriptor value obtained from delegate. Responding to "
<< "GetAll.";
scoped_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
dbus::MessageWriter writer(response.get());
dbus::MessageWriter array_writer(NULL);
dbus::MessageWriter dict_entry_writer(NULL);
dbus::MessageWriter variant_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(
bluetooth_gatt_descriptor::kUUIDProperty);
dict_entry_writer.AppendVariantOfString(uuid_);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(
bluetooth_gatt_descriptor::kCharacteristicProperty);
dict_entry_writer.AppendVariantOfObjectPath(characteristic_path_);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(
bluetooth_gatt_descriptor::kValueProperty);
dict_entry_writer.OpenVariant("ay", &variant_writer);
variant_writer.AppendArrayOfBytes(value.data(), value.size());
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
response_sender.Run(response.Pass());
}
void OnGet(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender,
const std::vector<uint8>& value) {
VLOG(2) << "Returning descriptor value obtained from delegate.";
scoped_ptr<dbus::Response> response =
dbus::Response::FromMethodCall(method_call);
dbus::MessageWriter writer(response.get());
dbus::MessageWriter variant_writer(NULL);
writer.OpenVariant("ay", &variant_writer);
variant_writer.AppendArrayOfBytes(value.data(), value.size());
writer.CloseContainer(&variant_writer);
response_sender.Run(response.Pass());
}
void OnSet(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "Successfully set descriptor value. Return success.";
response_sender.Run(dbus::Response::FromMethodCall(method_call));
}
void OnFailure(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "Failed to get/set descriptor value. Report error.";
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorFailed,
"Failed to get/set descriptor value.");
response_sender.Run(error_response.PassAs<dbus::Response>());
}
base::PlatformThreadId origin_thread_id_;
std::string uuid_;
dbus::Bus* bus_;
Delegate* delegate_;
dbus::ObjectPath object_path_;
dbus::ObjectPath characteristic_path_;
scoped_refptr<dbus::ExportedObject> exported_object_;
base::WeakPtrFactory<BluetoothGattDescriptorServiceProviderImpl>
weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothGattDescriptorServiceProviderImpl);
};
BluetoothGattDescriptorServiceProvider::
BluetoothGattDescriptorServiceProvider() {
}
BluetoothGattDescriptorServiceProvider::
~BluetoothGattDescriptorServiceProvider() {
}
BluetoothGattDescriptorServiceProvider*
BluetoothGattDescriptorServiceProvider::Create(
dbus::Bus* bus,
const dbus::ObjectPath& object_path,
Delegate* delegate,
const std::string& uuid,
const std::vector<std::string>& permissions,
const dbus::ObjectPath& characteristic_path) {
if (base::SysInfo::IsRunningOnChromeOS()) {
return new BluetoothGattDescriptorServiceProviderImpl(
bus, object_path, delegate, uuid, permissions, characteristic_path);
}
return new FakeBluetoothGattDescriptorServiceProvider(
object_path, delegate, uuid, permissions, characteristic_path);
}
}