#include "device/bluetooth/dbus/bluetooth_le_advertising_manager_client.h"
#include "base/check.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/observer_list.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 {
BluetoothLEAdvertisingManagerClient::Properties::Properties(
dbus::ObjectProxy* object_proxy,
const std::string& interface_name,
const PropertyChangedCallback& callback)
: dbus::PropertySet(object_proxy, interface_name, callback) {
RegisterProperty(bluetooth_advertising_manager::kSupportedFeatures,
&supported_features);
}
BluetoothLEAdvertisingManagerClient::Properties::~Properties() = default;
const char BluetoothLEAdvertisingManagerClient::kNoResponseError[] =
"org.chromium.Error.NoResponse";
class BluetoothAdvertisementManagerClientImpl
: public BluetoothLEAdvertisingManagerClient,
public dbus::ObjectManager::Interface {
public:
BluetoothAdvertisementManagerClientImpl() : object_manager_(nullptr) {}
BluetoothAdvertisementManagerClientImpl(
const BluetoothAdvertisementManagerClientImpl&) = delete;
BluetoothAdvertisementManagerClientImpl& operator=(
const BluetoothAdvertisementManagerClientImpl&) = delete;
~BluetoothAdvertisementManagerClientImpl() override {
if (object_manager_) {
object_manager_->UnregisterInterface(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface);
}
}
void AddObserver(
BluetoothLEAdvertisingManagerClient::Observer* observer) override {
DCHECK(observer);
observers_.AddObserver(observer);
}
void RemoveObserver(
BluetoothLEAdvertisingManagerClient::Observer* observer) override {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
dbus::PropertySet* CreateProperties(
dbus::ObjectProxy* object_proxy,
const dbus::ObjectPath& object_path,
const std::string& interface_name) override {
return new Properties(object_proxy, interface_name, base::DoNothing());
}
Properties* GetProperties(const dbus::ObjectPath& object_path) override {
return static_cast<Properties*>(object_manager_->GetProperties(
object_path,
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface));
}
void RegisterAdvertisement(const dbus::ObjectPath& manager_object_path,
const dbus::ObjectPath& advertisement_object_path,
base::OnceClosure callback,
ErrorCallback error_callback) override {
dbus::MethodCall method_call(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
bluetooth_advertising_manager::kRegisterAdvertisement);
dbus::MessageWriter writer(&method_call);
writer.AppendObjectPath(advertisement_object_path);
dbus::MessageWriter array_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
writer.CloseContainer(&array_writer);
CallObjectProxyMethod(manager_object_path, &method_call,
std::move(callback), std::move(error_callback));
}
void UnregisterAdvertisement(
const dbus::ObjectPath& manager_object_path,
const dbus::ObjectPath& advertisement_object_path,
base::OnceClosure callback,
ErrorCallback error_callback) override {
dbus::MethodCall method_call(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
bluetooth_advertising_manager::kUnregisterAdvertisement);
dbus::MessageWriter writer(&method_call);
writer.AppendObjectPath(advertisement_object_path);
CallObjectProxyMethod(manager_object_path, &method_call,
std::move(callback), std::move(error_callback));
}
void SetAdvertisingInterval(const dbus::ObjectPath& manager_object_path,
uint16_t min_interval_ms,
uint16_t max_interval_ms,
base::OnceClosure callback,
ErrorCallback error_callback) override {
dbus::MethodCall method_call(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
bluetooth_advertising_manager::kSetAdvertisingIntervals);
dbus::MessageWriter writer(&method_call);
writer.AppendUint16(min_interval_ms);
writer.AppendUint16(max_interval_ms);
CallObjectProxyMethod(manager_object_path, &method_call,
std::move(callback), std::move(error_callback));
}
void ResetAdvertising(const dbus::ObjectPath& manager_object_path,
base::OnceClosure callback,
ErrorCallback error_callback) override {
dbus::MethodCall method_call(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
bluetooth_advertising_manager::kResetAdvertising);
CallObjectProxyMethod(manager_object_path, &method_call,
std::move(callback), std::move(error_callback));
}
protected:
void Init(dbus::Bus* bus,
const std::string& bluetooth_service_name) override {
DCHECK(bus);
object_manager_ = bus->GetObjectManager(
bluetooth_service_name,
dbus::ObjectPath(
bluetooth_object_manager::kBluetoothObjectManagerServicePath));
object_manager_->RegisterInterface(
bluetooth_advertising_manager::kBluetoothAdvertisingManagerInterface,
this);
}
private:
void CallObjectProxyMethod(const dbus::ObjectPath& manager_object_path,
dbus::MethodCall* method_call,
base::OnceClosure callback,
ErrorCallback error_callback) {
DCHECK(object_manager_);
dbus::ObjectProxy* object_proxy =
object_manager_->GetObjectProxy(manager_object_path);
if (!object_proxy) {
std::move(error_callback)
.Run(bluetooth_advertising_manager::kErrorFailed,
"Adapter does not exist.");
return;
}
object_proxy->CallMethodWithErrorResponse(
method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::BindOnce(
&BluetoothAdvertisementManagerClientImpl::OnMethodResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(callback),
std::move(error_callback)));
}
void ObjectAdded(const dbus::ObjectPath& object_path,
const std::string& interface_name) override {
for (auto& observer : observers_)
observer.AdvertisingManagerAdded(object_path);
}
void ObjectRemoved(const dbus::ObjectPath& object_path,
const std::string& interface_name) override {
for (auto& observer : observers_)
observer.AdvertisingManagerRemoved(object_path);
}
void OnMethodResponse(base::OnceClosure callback,
ErrorCallback error_callback,
dbus::Response* response,
dbus::ErrorResponse* error_response) {
if (!response) {
std::string error_name;
std::string error_message;
if (error_response) {
dbus::MessageReader reader(error_response);
error_name = error_response->GetErrorName();
reader.PopString(&error_message);
} else {
error_name = kNoResponseError;
}
std::move(error_callback).Run(error_name, error_message);
return;
}
std::move(callback).Run();
}
raw_ptr<dbus::ObjectManager> object_manager_;
base::ObserverList<BluetoothLEAdvertisingManagerClient::Observer>::Unchecked
observers_;
base::WeakPtrFactory<BluetoothAdvertisementManagerClientImpl>
weak_ptr_factory_{this};
};
BluetoothLEAdvertisingManagerClient::BluetoothLEAdvertisingManagerClient() =
default;
BluetoothLEAdvertisingManagerClient::~BluetoothLEAdvertisingManagerClient() =
default;
BluetoothLEAdvertisingManagerClient*
BluetoothLEAdvertisingManagerClient::Create() {
return new BluetoothAdvertisementManagerClientImpl();
}
}