blob: 8f3719572d34b502cfa83d2c827b06ed54c72953 [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/bluetooth/dbus/bluetooth_gatt_manager_client.h"
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_manager.h"
#include "dbus/object_proxy.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace bluez {
const char BluetoothGattManagerClient::kNoResponseError[] =
"org.chromium.Error.NoResponse";
// The BluetoothGattManagerClient implementation used in production.
class BluetoothGattManagerClientImpl : public BluetoothGattManagerClient {
public:
BluetoothGattManagerClientImpl()
: object_manager_(nullptr), weak_ptr_factory_(this) {}
~BluetoothGattManagerClientImpl() override {}
// BluetoothGattManagerClient override.
void RegisterApplication(const dbus::ObjectPath& adapter_object_path,
const dbus::ObjectPath& application_path,
const Options& options,
const base::Closure& callback,
const ErrorCallback& error_callback) override {
dbus::MethodCall method_call(
bluetooth_gatt_manager::kBluetoothGattManagerInterface,
bluetooth_gatt_manager::kRegisterApplication);
dbus::MessageWriter writer(&method_call);
writer.AppendObjectPath(application_path);
// The parameters of the Options dictionary are undefined but the method
// signature still requires a value dictionary. Pass an empty dictionary
// and fill in the contents later if and when we add any options.
dbus::MessageWriter array_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
writer.CloseContainer(&array_writer);
DCHECK(object_manager_);
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(adapter_object_path);
DCHECK(object_proxy);
object_proxy->CallMethodWithErrorCallback(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothGattManagerClientImpl::OnSuccess,
weak_ptr_factory_.GetWeakPtr(), callback),
base::Bind(&BluetoothGattManagerClientImpl::OnError,
weak_ptr_factory_.GetWeakPtr(), error_callback));
}
// BluetoothGattManagerClient override.
void UnregisterApplication(const dbus::ObjectPath& adapter_object_path,
const dbus::ObjectPath& application_path,
const base::Closure& callback,
const ErrorCallback& error_callback) override {
dbus::MethodCall method_call(
bluetooth_gatt_manager::kBluetoothGattManagerInterface,
bluetooth_gatt_manager::kUnregisterApplication);
dbus::MessageWriter writer(&method_call);
writer.AppendObjectPath(application_path);
DCHECK(object_manager_);
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(adapter_object_path);
DCHECK(object_proxy);
object_proxy->CallMethodWithErrorCallback(
&method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&BluetoothGattManagerClientImpl::OnSuccess,
weak_ptr_factory_.GetWeakPtr(), callback),
base::Bind(&BluetoothGattManagerClientImpl::OnError,
weak_ptr_factory_.GetWeakPtr(), error_callback));
}
protected:
// bluez::DBusClient override.
void Init(dbus::Bus* bus) override {
DCHECK(bus);
DCHECK(bus);
object_manager_ = bus->GetObjectManager(
bluetooth_object_manager::kBluetoothObjectManagerServiceName,
dbus::ObjectPath(
bluetooth_object_manager::kBluetoothObjectManagerServicePath));
}
private:
// Called when a response for a successful method call is received.
void OnSuccess(const base::Closure& callback, dbus::Response* response) {
DCHECK(response);
callback.Run();
}
// Called when a response for a failed method call is received.
void OnError(const ErrorCallback& error_callback,
dbus::ErrorResponse* response) {
// Error response has optional error message argument.
std::string error_name;
std::string error_message;
if (response) {
dbus::MessageReader reader(response);
error_name = response->GetErrorName();
reader.PopString(&error_message);
} else {
error_name = kNoResponseError;
}
error_callback.Run(error_name, error_message);
}
// The proxy to the bluez object manager.
dbus::ObjectManager* object_manager_;
// Weak pointer factory for generating 'this' pointers that might live longer
// than we do.
// 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<BluetoothGattManagerClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothGattManagerClientImpl);
};
BluetoothGattManagerClient::BluetoothGattManagerClient() {}
BluetoothGattManagerClient::~BluetoothGattManagerClient() {}
// static
BluetoothGattManagerClient* BluetoothGattManagerClient::Create() {
return new BluetoothGattManagerClientImpl();
}
} // namespace bluez