This source file includes following definitions.
- weak_ptr_factory_
- OnOriginThread
- Get
- Set
- GetAll
- OnExported
- Create
#include "chromeos/dbus/bluetooth_gatt_service_service_provider.h"
#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "base/sys_info.h"
#include "base/threading/platform_thread.h"
#include "chromeos/dbus/fake_bluetooth_gatt_service_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";
}
class BluetoothGattServiceServiceProviderImpl
: public BluetoothGattServiceServiceProvider {
public:
BluetoothGattServiceServiceProviderImpl(
dbus::Bus* bus,
const dbus::ObjectPath& object_path,
const std::string& uuid,
const std::vector<dbus::ObjectPath>& includes)
: origin_thread_id_(base::PlatformThread::CurrentId()),
uuid_(uuid),
includes_(includes),
bus_(bus),
object_path_(object_path),
weak_ptr_factory_(this) {
VLOG(1) << "Creating Bluetooth GATT service: " << object_path_.value()
<< " UUID: " << uuid;
DCHECK(!uuid_.empty());
DCHECK(object_path_.IsValid());
DCHECK(bus_);
exported_object_ = bus_->GetExportedObject(object_path_);
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGet,
base::Bind(&BluetoothGattServiceServiceProviderImpl::Get,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesSet,
base::Bind(&BluetoothGattServiceServiceProviderImpl::Set,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
exported_object_->ExportMethod(
dbus::kDBusPropertiesInterface,
dbus::kDBusPropertiesGetAll,
base::Bind(&BluetoothGattServiceServiceProviderImpl::GetAll,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&BluetoothGattServiceServiceProviderImpl::OnExported,
weak_ptr_factory_.GetWeakPtr()));
}
virtual ~BluetoothGattServiceServiceProviderImpl() {
VLOG(1) << "Cleaning up Bluetooth GATT service: " << object_path_.value();
bus_->UnregisterExportedObject(object_path_);
}
private:
bool OnOriginThread() {
return base::PlatformThread::CurrentId() == origin_thread_id_;
}
void Get(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattServiceServiceProvider::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_service::kBluetoothGattServiceInterface) {
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_service::kUUIDProperty &&
property_name != bluetooth_gatt_service::kIncludesProperty) {
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorInvalidArgs,
"No such property: '" + property_name + "'.");
response_sender.Run(error_response.PassAs<dbus::Response>());
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_service::kUUIDProperty) {
writer.OpenVariant("s", &variant_writer);
variant_writer.AppendString(uuid_);
writer.CloseContainer(&variant_writer);
} else {
writer.OpenVariant("ao", &variant_writer);
variant_writer.AppendArrayOfObjectPaths(includes_);
writer.CloseContainer(&variant_writer);
}
response_sender.Run(response.Pass());
}
void Set(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattServiceServiceProvider::Set: "
<< object_path_.value();
DCHECK(OnOriginThread());
scoped_ptr<dbus::ErrorResponse> error_response =
dbus::ErrorResponse::FromMethodCall(
method_call, kErrorPropertyReadOnly,
"All properties are read-only.");
response_sender.Run(error_response.PassAs<dbus::Response>());
}
void GetAll(dbus::MethodCall* method_call,
dbus::ExportedObject::ResponseSender response_sender) {
VLOG(2) << "BluetoothGattServiceServiceProvider::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_service::kBluetoothGattServiceInterface) {
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;
}
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_service::kUUIDProperty);
dict_entry_writer.AppendVariantOfString(uuid_);
array_writer.CloseContainer(&dict_entry_writer);
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(bluetooth_gatt_service::kIncludesProperty);
dict_entry_writer.OpenVariant("ao", &variant_writer);
variant_writer.AppendArrayOfObjectPaths(includes_);
dict_entry_writer.CloseContainer(&variant_writer);
array_writer.CloseContainer(&dict_entry_writer);
writer.CloseContainer(&array_writer);
response_sender.Run(response.Pass());
}
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;
}
base::PlatformThreadId origin_thread_id_;
std::string uuid_;
std::vector<dbus::ObjectPath> includes_;
dbus::Bus* bus_;
dbus::ObjectPath object_path_;
scoped_refptr<dbus::ExportedObject> exported_object_;
base::WeakPtrFactory<BluetoothGattServiceServiceProviderImpl>
weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothGattServiceServiceProviderImpl);
};
BluetoothGattServiceServiceProvider::BluetoothGattServiceServiceProvider() {
}
BluetoothGattServiceServiceProvider::~BluetoothGattServiceServiceProvider() {
}
BluetoothGattServiceServiceProvider*
BluetoothGattServiceServiceProvider::Create(
dbus::Bus* bus,
const dbus::ObjectPath& object_path,
const std::string& uuid,
const std::vector<dbus::ObjectPath>& includes) {
if (base::SysInfo::IsRunningOnChromeOS()) {
return new BluetoothGattServiceServiceProviderImpl(
bus, object_path, uuid, includes);
}
return new FakeBluetoothGattServiceServiceProvider(
object_path, uuid, includes);
}
}