Rewrite dbus code using sdbus-c++

This commit is contained in:
Vibhav Pant
2023-08-15 20:52:51 +05:30
parent aea2e16937
commit c8af59666d
29 changed files with 1632 additions and 738 deletions
@@ -1,23 +1,27 @@
#ifndef PLATFORM_IMPL_LINUX_BLUETOOTH_CLASSIC_DEVICE_H_
#define PLATFORM_IMPL_LINUX_BLUETOOTH_CLASSIC_DEVICE_H_
#include <systemd/sd-bus.h>
#include <sdbus-c++/IConnection.h>
#include <sdbus-c++/IProxy.h>
#include <sdbus-c++/ProxyInterfaces.h>
#include <sdbus-c++/StandardInterfaces.h>
#include <sdbus-c++/Types.h>
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "internal/base/observer_list.h"
#include "internal/platform/implementation/bluetooth_classic.h"
#include "internal/platform/implementation/linux/bluez_device_client_glue.h"
namespace nearby {
namespace linux {
const char *BLUEZ_DEVICE_INTERFACE = "org.bluez.Device1";
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html.
class BluetoothDevice : public api::BluetoothDevice {
class BluetoothDevice
: public api::BluetoothDevice,
public sdbus::ProxyInterfaces<org::bluez::Device1_proxy> {
public:
BluetoothDevice(sd_bus *system_bus, absl::string_view adapter,
absl::string_view address);
BluetoothDevice(sd_bus *system_bus, absl::string_view device_object_path);
BluetoothDevice(const BluetoothDevice &device);
~BluetoothDevice() override { sd_bus_unref(system_bus_); };
BluetoothDevice(sdbus::IConnection &system_bus, const sdbus::ObjectPath &);
~BluetoothDevice() = default;
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()
std::string GetName() const override;
@@ -25,11 +29,55 @@ public:
// Returns BT MAC address assigned to this device.
std::string GetMacAddress() const override;
bool ConnectToProfile(absl::string_view service_uuid);
void
set_pair_reply_callback(absl::AnyInvocable<void(const sdbus::Error *)> cb) {
absl::MutexLock l(&pair_callback_lock_);
on_pair_reply_cb_ = std::move(cb);
}
void reset_pair_reply_callback() {
absl::MutexLock l(&pair_callback_lock_);
on_pair_reply_cb_ = DefaultCallback<const sdbus::Error *>();
}
protected:
void onConnectProfileReply(const sdbus::Error *error) override;
void onPairReply(const sdbus::Error *error) override {
absl::ReaderMutexLock l(&pair_callback_lock_);
on_pair_reply_cb_(error);
};
private:
sd_bus *system_bus_;
std::string object_path_;
std::string mac_addr_;
absl::Mutex pair_callback_lock_;
absl::AnyInvocable<void(const sdbus::Error *)> on_pair_reply_cb_ =
DefaultCallback<const sdbus::Error *>();
};
class MonitoredBluetoothDevice
: public BluetoothDevice,
public sdbus::ProxyInterfaces<sdbus::Properties_proxy> {
public:
using sdbus::ProxyInterfaces<sdbus::Properties_proxy>::registerProxy;
using sdbus::ProxyInterfaces<sdbus::Properties_proxy>::unregisterProxy;
using sdbus::ProxyInterfaces<sdbus::Properties_proxy>::getObjectPath;
MonitoredBluetoothDevice(
sdbus::IConnection &system_bus, const sdbus::ObjectPath &,
ObserverList<api::BluetoothClassicMedium::Observer> &observers);
~MonitoredBluetoothDevice() { unregisterProxy(); }
protected:
void onPropertiesChanged(
const std::string &interfaceName,
const std::map<std::string, sdbus::Variant> &changedProperties,
const std::vector<std::string> &invalidatedProperties) override;
private:
ObserverList<api::BluetoothClassicMedium::Observer> &observers_;
};
} // namespace linux
} // namespace nearby