mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Minor refactoring.
This commit is contained in:
@@ -91,7 +91,7 @@ std::string BluetoothAdapter::GetName() const {
|
||||
return bluez_adapter_->Alias();
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Alias", e);
|
||||
return std::string();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,7 +115,7 @@ std::string BluetoothAdapter::GetMacAddress() const {
|
||||
return bluez_adapter_->Address();
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(bluez_adapter_, "Address", e);
|
||||
return std::string();
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -37,9 +37,9 @@ namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
bool ProfileManager::ProfileRegistered(absl::string_view service_uuid) {
|
||||
registered_service_uuids_lock_.ReaderLock();
|
||||
registered_service_uuids_mutex_.ReaderLock();
|
||||
bool registered = registered_services_.count(std::string(service_uuid)) == 1;
|
||||
registered_service_uuids_lock_.ReaderUnlock();
|
||||
registered_service_uuids_mutex_.ReaderUnlock();
|
||||
return registered;
|
||||
}
|
||||
|
||||
@@ -78,9 +78,9 @@ void Profile::NewConnection(
|
||||
|
||||
absl::MutexLock l(&connections_lock_);
|
||||
if (connections_.count(mac_addr) != 0) {
|
||||
connections_[mac_addr].push_back(std::pair(fd, std::move(props)));
|
||||
connections_[mac_addr].push_back(std::pair(fd, props));
|
||||
} else {
|
||||
connections_[mac_addr] = std::vector{std::pair(fd, std::move(props))};
|
||||
connections_[mac_addr] = std::vector{std::pair(fd, props)};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ bool ProfileManager::Register(std::optional<absl::string_view> name,
|
||||
}
|
||||
|
||||
{
|
||||
absl::MutexLock l(®istered_service_uuids_lock_);
|
||||
absl::MutexLock l(®istered_service_uuids_mutex_);
|
||||
registered_services_.emplace(service_uuid, profile);
|
||||
}
|
||||
|
||||
@@ -165,7 +165,7 @@ void ProfileManager::Unregister(absl::string_view service_uuid) {
|
||||
}
|
||||
|
||||
{
|
||||
absl::MutexLock l(®istered_service_uuids_lock_);
|
||||
absl::MutexLock l(®istered_service_uuids_mutex_);
|
||||
registered_services_.erase(std::string(service_uuid));
|
||||
}
|
||||
}
|
||||
@@ -184,9 +184,9 @@ ProfileManager::GetServiceRecordFD(api::BluetoothDevice &remote_device,
|
||||
|
||||
auto mac_addr = remote_device.GetMacAddress();
|
||||
|
||||
registered_service_uuids_lock_.ReaderLock();
|
||||
registered_service_uuids_mutex_.ReaderLock();
|
||||
auto profile = registered_services_[std::string(service_uuid)];
|
||||
registered_service_uuids_lock_.ReaderUnlock();
|
||||
registered_service_uuids_mutex_.ReaderUnlock();
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": " << profile->getObjectPath()
|
||||
<< ": Attempting to get a FD for service "
|
||||
@@ -226,16 +226,19 @@ ProfileManager::GetServiceRecordFD(absl::string_view service_uuid) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
registered_service_uuids_lock_.ReaderLock();
|
||||
registered_service_uuids_mutex_.ReaderLock();
|
||||
auto profile = registered_services_[std::string(service_uuid)];
|
||||
registered_service_uuids_lock_.ReaderUnlock();
|
||||
registered_service_uuids_mutex_.ReaderUnlock();
|
||||
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": " << profile->getObjectPath()
|
||||
<< ": Attempting to get a FD for service "
|
||||
<< profile->getObjectPath();
|
||||
<< service_uuid;
|
||||
|
||||
profile->connections_lock_.Lock();
|
||||
auto cond = [profile]() { return !profile->connections_.empty(); };
|
||||
auto cond = [profile]() {
|
||||
profile->connections_lock_.AssertReaderHeld();
|
||||
return !profile->connections_.empty();
|
||||
};
|
||||
profile->connections_lock_.Await(absl::Condition(&cond));
|
||||
|
||||
auto it = profile->connections_.begin();
|
||||
@@ -246,7 +249,14 @@ ProfileManager::GetServiceRecordFD(absl::string_view service_uuid) {
|
||||
profile->connections_.erase(it);
|
||||
profile->connections_lock_.Unlock();
|
||||
|
||||
return std::pair(devices_.get_device_by_address(mac_addr).value(), fd);
|
||||
auto maybe_device = devices_.get_device_by_address(mac_addr);
|
||||
if (!maybe_device.has_value()) {
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": Device " << mac_addr
|
||||
<< " is no longer available";
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return std::pair(*maybe_device, fd);
|
||||
}
|
||||
|
||||
} // namespace linux
|
||||
|
||||
@@ -38,26 +38,38 @@
|
||||
#include "internal/platform/implementation/bluetooth_classic.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_devices.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_manager_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class Profile : public sdbus::AdaptorInterfaces<org::bluez::Profile1_adaptor> {
|
||||
public:
|
||||
class ProfileManager;
|
||||
|
||||
class Profile final
|
||||
: public sdbus::AdaptorInterfaces<org::bluez::Profile1_adaptor> {
|
||||
public:
|
||||
Profile(const Profile &) = delete;
|
||||
Profile(Profile &&) = delete;
|
||||
Profile &operator=(const Profile &) = delete;
|
||||
Profile &operator=(Profile &&) = delete;
|
||||
Profile(sdbus::IConnection &system_bus, absl::string_view profile_object_path,
|
||||
BluetoothDevices &devices)
|
||||
: AdaptorInterfaces(system_bus, std::string(profile_object_path)),
|
||||
released_(false), devices_(devices) {
|
||||
released_(false),
|
||||
devices_(devices) {
|
||||
registerAdaptor();
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Created a new BlueZ profile at :"
|
||||
<< getObjectPath();
|
||||
}
|
||||
~Profile() { unregisterAdaptor(); }
|
||||
|
||||
private:
|
||||
friend class ProfileManager;
|
||||
|
||||
struct FDProperties {
|
||||
FDProperties(const std::map<std::string, sdbus::Variant> &fd_props) {
|
||||
explicit FDProperties(const std::map<std::string, sdbus::Variant> &fd_props)
|
||||
: version(std::nullopt), features(std::nullopt) {
|
||||
if (fd_props.count("Version") == 1) {
|
||||
version = fd_props.at("Version");
|
||||
}
|
||||
@@ -72,21 +84,27 @@ public:
|
||||
|
||||
void Release() override;
|
||||
void NewConnection(const sdbus::ObjectPath &, const sdbus::UnixFd &,
|
||||
const std::map<std::string, sdbus::Variant> &) override;
|
||||
void RequestDisconnection(const sdbus::ObjectPath &) override;
|
||||
const std::map<std::string, sdbus::Variant> &) override
|
||||
ABSL_LOCKS_EXCLUDED(connections_lock_);
|
||||
void RequestDisconnection(const sdbus::ObjectPath &) override
|
||||
ABSL_LOCKS_EXCLUDED(connections_lock_);
|
||||
|
||||
std::atomic_bool released_;
|
||||
|
||||
absl::Mutex connections_lock_;
|
||||
std::map<std::string, std::vector<std::pair<sdbus::UnixFd, FDProperties>>>
|
||||
connections_;
|
||||
connections_ ABSL_GUARDED_BY(connections_lock_);
|
||||
|
||||
BluetoothDevices &devices_;
|
||||
};
|
||||
|
||||
class ProfileManager
|
||||
class ProfileManager final
|
||||
: private sdbus::ProxyInterfaces<org::bluez::ProfileManager1_proxy> {
|
||||
public:
|
||||
public:
|
||||
ProfileManager(const ProfileManager &) = delete;
|
||||
ProfileManager(ProfileManager &&) = delete;
|
||||
ProfileManager &operator=(const ProfileManager &) = delete;
|
||||
ProfileManager &operator=(ProfileManager &&) = delete;
|
||||
ProfileManager(sdbus::IConnection &system_bus, BluetoothDevices &devices)
|
||||
: ProxyInterfaces(system_bus, bluez::SERVICE_DEST, "/org/bluez"),
|
||||
devices_(devices) {
|
||||
@@ -94,29 +112,35 @@ public:
|
||||
}
|
||||
~ProfileManager() { unregisterProxy(); }
|
||||
|
||||
bool ProfileRegistered(absl::string_view service_uuid);
|
||||
bool ProfileRegistered(absl::string_view service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_);
|
||||
bool Register(std::optional<absl::string_view> service_name,
|
||||
absl::string_view service_uuid);
|
||||
bool Register(absl::string_view service_uuid) {
|
||||
absl::string_view service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_);
|
||||
bool Register(absl::string_view service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_) {
|
||||
return Register(std::nullopt, service_uuid);
|
||||
}
|
||||
void Unregister(absl::string_view service_uuid);
|
||||
void Unregister(absl::string_view service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_);
|
||||
|
||||
std::optional<sdbus::UnixFd>
|
||||
GetServiceRecordFD(api::BluetoothDevice &remote_device,
|
||||
absl::string_view service_uuid,
|
||||
CancellationFlag *cancellation_flag);
|
||||
std::optional<sdbus::UnixFd> GetServiceRecordFD(
|
||||
api::BluetoothDevice &remote_device, absl::string_view service_uuid,
|
||||
CancellationFlag *cancellation_flag)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_);
|
||||
std::optional<
|
||||
std::pair<std::reference_wrapper<BluetoothDevice>, sdbus::UnixFd>>
|
||||
GetServiceRecordFD(absl::string_view service_uuid);
|
||||
GetServiceRecordFD(absl::string_view service_uuid)
|
||||
ABSL_LOCKS_EXCLUDED(registered_service_uuids_mutex_);
|
||||
|
||||
private:
|
||||
private:
|
||||
BluetoothDevices &devices_;
|
||||
// Maps service UUIDs to RegisteredService
|
||||
std::map<std::string, std::shared_ptr<Profile>> registered_services_;
|
||||
absl::Mutex registered_service_uuids_lock_;
|
||||
absl::Mutex registered_service_uuids_mutex_;
|
||||
std::map<std::string, std::shared_ptr<Profile>> registered_services_
|
||||
ABSL_GUARDED_BY(registered_service_uuids_mutex_);
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
#endif
|
||||
|
||||
@@ -25,9 +25,9 @@
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
BluetoothDevice::BluetoothDevice(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &device_object_path)
|
||||
sdbus::ObjectPath device_object_path)
|
||||
: ProxyInterfaces(system_bus, bluez::SERVICE_DEST,
|
||||
std::string(device_object_path)) {
|
||||
std::move(device_object_path)) {
|
||||
registerProxy();
|
||||
try {
|
||||
last_known_name_ = Alias();
|
||||
|
||||
@@ -34,21 +34,27 @@ class BluetoothDevice
|
||||
: public api::BluetoothDevice,
|
||||
public sdbus::ProxyInterfaces<org::bluez::Device1_proxy> {
|
||||
public:
|
||||
BluetoothDevice(sdbus::IConnection &system_bus, const sdbus::ObjectPath &);
|
||||
~BluetoothDevice() = default;
|
||||
BluetoothDevice(const BluetoothDevice &) = delete;
|
||||
BluetoothDevice(BluetoothDevice &&) = delete;
|
||||
BluetoothDevice &operator=(const BluetoothDevice &) = delete;
|
||||
BluetoothDevice &operator=(BluetoothDevice &&) = delete;
|
||||
BluetoothDevice(sdbus::IConnection &system_bus, sdbus::ObjectPath device_object_path);
|
||||
~BluetoothDevice() override {
|
||||
unregisterProxy();
|
||||
}
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()
|
||||
std::string GetName() const override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#getName()
|
||||
std::string GetName() const override;
|
||||
|
||||
// Returns BT MAC address assigned to this device.
|
||||
std::string GetMacAddress() const override;
|
||||
// Returns BT MAC address assigned to this device.
|
||||
std::string GetMacAddress() const override;
|
||||
|
||||
bool ConnectToProfile(absl::string_view service_uuid);
|
||||
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 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() {
|
||||
@@ -73,7 +79,7 @@ private:
|
||||
mutable std::string last_known_address_ ABSL_GUARDED_BY(properties_mutex_);
|
||||
};
|
||||
|
||||
class MonitoredBluetoothDevice
|
||||
class MonitoredBluetoothDevice final
|
||||
: public BluetoothDevice,
|
||||
public sdbus::ProxyInterfaces<sdbus::Properties_proxy> {
|
||||
public:
|
||||
@@ -81,10 +87,15 @@ public:
|
||||
using sdbus::ProxyInterfaces<sdbus::Properties_proxy>::unregisterProxy;
|
||||
using sdbus::ProxyInterfaces<sdbus::Properties_proxy>::getObjectPath;
|
||||
|
||||
MonitoredBluetoothDevice(const MonitoredBluetoothDevice &) = delete;
|
||||
MonitoredBluetoothDevice(MonitoredBluetoothDevice &&) = delete;
|
||||
MonitoredBluetoothDevice &operator=(const MonitoredBluetoothDevice &) =
|
||||
delete;
|
||||
MonitoredBluetoothDevice &operator=(MonitoredBluetoothDevice &&) = delete;
|
||||
MonitoredBluetoothDevice(
|
||||
sdbus::IConnection &system_bus, const sdbus::ObjectPath &,
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers);
|
||||
~MonitoredBluetoothDevice() { unregisterProxy(); }
|
||||
~MonitoredBluetoothDevice() override { unregisterProxy(); }
|
||||
|
||||
protected:
|
||||
void onPropertiesChanged(
|
||||
|
||||
@@ -47,8 +47,6 @@ BluetoothClassicMedium::BluetoothClassicMedium(
|
||||
registerProxy();
|
||||
}
|
||||
|
||||
BluetoothClassicMedium::~BluetoothClassicMedium() { unregisterProxy(); }
|
||||
|
||||
void BluetoothClassicMedium::onInterfacesAdded(
|
||||
const sdbus::ObjectPath &object,
|
||||
const std::map<std::string, std::map<std::string, sdbus::Variant>>
|
||||
@@ -74,7 +72,7 @@ void BluetoothClassicMedium::onInterfacesAdded(
|
||||
discovery_cb_->device_discovered_cb(device);
|
||||
}
|
||||
|
||||
for (auto &observer : observers_.GetObservers()) {
|
||||
for (const auto &observer : observers_.GetObservers()) {
|
||||
observer->DeviceAdded(device);
|
||||
}
|
||||
}
|
||||
@@ -88,7 +86,7 @@ void BluetoothClassicMedium::onInterfacesRemoved(
|
||||
return;
|
||||
}
|
||||
|
||||
for (auto &interface : interfaces) {
|
||||
for (const auto &interface : interfaces) {
|
||||
if (interface == org::bluez::Device1_proxy::INTERFACE_NAME) {
|
||||
{
|
||||
auto device = devices_->get_device_by_path(object);
|
||||
@@ -107,7 +105,7 @@ void BluetoothClassicMedium::onInterfacesRemoved(
|
||||
discovery_cb_->device_lost_cb(*device);
|
||||
}
|
||||
|
||||
for (auto &observer : observers_.GetObservers()) {
|
||||
for (const auto &observer : observers_.GetObservers()) {
|
||||
observer->DeviceRemoved(*device);
|
||||
}
|
||||
}
|
||||
@@ -166,7 +164,10 @@ BluetoothClassicMedium::ConnectToService(api::BluetoothDevice &remote_device,
|
||||
}
|
||||
}
|
||||
|
||||
auto &device = devices_->get_device_by_path(device_object_path).value().get();
|
||||
auto maybe_device = devices_->get_device_by_path(device_object_path);
|
||||
if (!maybe_device.has_value()) return nullptr;
|
||||
|
||||
auto &device = maybe_device->get();
|
||||
device.ConnectToProfile(service_uuid);
|
||||
|
||||
auto fd = profile_manager_->GetServiceRecordFD(remote_device, service_uuid,
|
||||
@@ -202,7 +203,7 @@ BluetoothClassicMedium::ListenForService(const std::string &service_name,
|
||||
api::BluetoothDevice *
|
||||
BluetoothClassicMedium::GetRemoteDevice(const std::string &mac_address) {
|
||||
auto device = devices_->get_device_by_address(mac_address);
|
||||
if (device.has_value())
|
||||
if (!device.has_value())
|
||||
return nullptr;
|
||||
|
||||
return &(device->get());
|
||||
@@ -211,6 +212,8 @@ BluetoothClassicMedium::GetRemoteDevice(const std::string &mac_address) {
|
||||
std::unique_ptr<api::BluetoothPairing>
|
||||
BluetoothClassicMedium::CreatePairing(api::BluetoothDevice &remote_device) {
|
||||
auto device = devices_->get_device_by_address(remote_device.GetMacAddress());
|
||||
if (!device.has_value()) return nullptr;
|
||||
|
||||
return std::unique_ptr<api::BluetoothPairing>(
|
||||
new BluetoothPairing(*adapter_, *device));
|
||||
}
|
||||
|
||||
@@ -39,69 +39,70 @@ namespace nearby {
|
||||
namespace linux {
|
||||
// Container of operations that can be performed over the Bluetooth Classic
|
||||
// medium.
|
||||
class BluetoothClassicMedium
|
||||
class BluetoothClassicMedium final
|
||||
: public api::BluetoothClassicMedium,
|
||||
sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||
public:
|
||||
BluetoothClassicMedium(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path);
|
||||
~BluetoothClassicMedium() override;
|
||||
BluetoothClassicMedium(const BluetoothClassicMedium &) = delete;
|
||||
BluetoothClassicMedium(BluetoothClassicMedium &&) = delete;
|
||||
BluetoothClassicMedium &operator=(const BluetoothClassicMedium &) = delete;
|
||||
BluetoothClassicMedium &operator=(BluetoothClassicMedium &&) = delete;
|
||||
BluetoothClassicMedium(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path);
|
||||
~BluetoothClassicMedium() override { unregisterProxy(); };
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
|
||||
//
|
||||
// Returns true once the process of discovery has been initiated.
|
||||
bool StartDiscovery(DiscoveryCallback discovery_callback) override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery()
|
||||
//
|
||||
// Returns true once discovery is well and truly stopped; after this returns,
|
||||
// there must be no more invocations of the DiscoveryCallback passed in to
|
||||
// StartDiscovery().
|
||||
bool StopDiscovery() override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#startDiscovery()
|
||||
//
|
||||
// Returns true once the process of discovery has been initiated.
|
||||
bool StartDiscovery(DiscoveryCallback discovery_callback) override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#cancelDiscovery()
|
||||
//
|
||||
// Returns true once discovery is well and truly stopped; after this returns,
|
||||
// there must be no more invocations of the DiscoveryCallback passed in to
|
||||
// StartDiscovery().
|
||||
bool StopDiscovery() override;
|
||||
|
||||
// A combination of
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord
|
||||
// followed by
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect().
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// On success, returns a new BluetoothSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::BluetoothSocket>
|
||||
ConnectToService(api::BluetoothDevice &remote_device,
|
||||
const std::string &service_uuid,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
// A combination of
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createInsecureRfcommSocketToServiceRecord
|
||||
// followed by
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#connect().
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// On success, returns a new BluetoothSocket.
|
||||
// On error, returns nullptr.
|
||||
std::unique_ptr<api::BluetoothSocket> ConnectToService(
|
||||
api::BluetoothDevice &remote_device, const std::string &service_uuid,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#listenUsingInsecureRfcommWithServiceRecord
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// Returns nullptr error.
|
||||
std::unique_ptr<api::BluetoothServerSocket>
|
||||
ListenForService(const std::string &service_name,
|
||||
const std::string &service_uuid) override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#listenUsingInsecureRfcommWithServiceRecord
|
||||
//
|
||||
// service_uuid is the canonical textual representation
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Format) of a
|
||||
// type 3 name-based
|
||||
// (https://en.wikipedia.org/wiki/Universally_unique_identifier#Versions_3_and_5_(namespace_name-based))
|
||||
// UUID.
|
||||
//
|
||||
// Returns nullptr error.
|
||||
std::unique_ptr<api::BluetoothServerSocket> ListenForService(
|
||||
const std::string &service_name, const std::string &service_uuid) override;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond()
|
||||
//
|
||||
// Start the bonding (pairing) process with the remote device.
|
||||
// Return a Bluetooth pairing instance to handle the pairing process with the
|
||||
// remote device.
|
||||
std::unique_ptr<api::BluetoothPairing>
|
||||
CreatePairing(api::BluetoothDevice &remote_device) override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html#createBond()
|
||||
//
|
||||
// Start the bonding (pairing) process with the remote device.
|
||||
// Return a Bluetooth pairing instance to handle the pairing process with the
|
||||
// remote device.
|
||||
std::unique_ptr<api::BluetoothPairing> CreatePairing(
|
||||
api::BluetoothDevice &remote_device) override;
|
||||
|
||||
api::BluetoothDevice *
|
||||
GetRemoteDevice(const std::string &mac_address) override;
|
||||
api::BluetoothDevice *GetRemoteDevice(const std::string &mac_address) override;
|
||||
|
||||
void AddObserver(Observer *observer) override {
|
||||
observers_.AddObserver(observer);
|
||||
void AddObserver(Observer *observer) override {
|
||||
observers_.AddObserver(observer);
|
||||
};
|
||||
void RemoveObserver(Observer *observer) override {
|
||||
observers_.RemoveObserver(observer);
|
||||
|
||||
@@ -22,28 +22,27 @@
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothServerSocket : public api::BluetoothServerSocket {
|
||||
class BluetoothServerSocket final : public api::BluetoothServerSocket {
|
||||
public:
|
||||
BluetoothServerSocket(ProfileManager &profile_manager,
|
||||
absl::string_view service_uuid)
|
||||
: profile_manager_(profile_manager), service_uuid_(service_uuid) {}
|
||||
~BluetoothServerSocket() = default;
|
||||
BluetoothServerSocket(ProfileManager &profile_manager,
|
||||
absl::string_view service_uuid)
|
||||
: profile_manager_(profile_manager), service_uuid_(service_uuid) {}
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#accept()
|
||||
//
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be
|
||||
// closed.
|
||||
std::unique_ptr<api::BluetoothSocket> Accept() override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#accept()
|
||||
//
|
||||
// Blocks until either:
|
||||
// - at least one incoming connection request is available, or
|
||||
// - ServerSocket is closed.
|
||||
// On success, returns connected socket, ready to exchange data.
|
||||
// Returns nullptr on error.
|
||||
// Once error is reported, it is permanent, and ServerSocket has to be
|
||||
// closed.
|
||||
std::unique_ptr<api::BluetoothSocket> Accept() override;
|
||||
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#close()
|
||||
//
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html#close()
|
||||
//
|
||||
// Returns Exception::kIo on error, Exception::kSuccess otherwise.
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
ProfileManager &profile_manager_;
|
||||
|
||||
@@ -55,7 +55,7 @@ Exception OutputStream::Write(const ByteArray &data) {
|
||||
if (!fd_.has_value())
|
||||
return Exception{Exception::kIo};
|
||||
|
||||
ssize_t written = 0;
|
||||
size_t written = 0;
|
||||
while (written < data.size()) {
|
||||
ssize_t ret = write(fd_->get(), data.data(), data.size());
|
||||
if (ret < 1) {
|
||||
|
||||
@@ -27,15 +27,15 @@
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothSocket : public api::BluetoothSocket {
|
||||
class BluetoothSocket final : public api::BluetoothSocket {
|
||||
public:
|
||||
BluetoothSocket(api::BluetoothDevice &device, sdbus::UnixFd fd)
|
||||
: device_(device), output_stream_(fd), input_stream_(fd) {}
|
||||
BluetoothSocket(api::BluetoothDevice &device, sdbus::UnixFd fd)
|
||||
: device_(device), output_stream_(fd), input_stream_(fd) {}
|
||||
|
||||
nearby::InputStream &GetInputStream() override { return input_stream_; }
|
||||
nearby::OutputStream &GetOutputStream() override { return output_stream_; }
|
||||
Exception Close() override;
|
||||
api::BluetoothDevice *GetRemoteDevice() override { return &device_; };
|
||||
nearby::InputStream &GetInputStream() override { return input_stream_; }
|
||||
nearby::OutputStream &GetOutputStream() override { return output_stream_; }
|
||||
Exception Close() override;
|
||||
api::BluetoothDevice *GetRemoteDevice() override { return &device_; };
|
||||
|
||||
private:
|
||||
api::BluetoothDevice &device_;
|
||||
|
||||
@@ -28,15 +28,14 @@
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothDevices {
|
||||
class BluetoothDevices final {
|
||||
public:
|
||||
BluetoothDevices(
|
||||
sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &adapter_object_path,
|
||||
sdbus::ObjectPath adapter_object_path,
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> &observers)
|
||||
: system_bus_(system_bus), observers_(observers),
|
||||
adapter_object_path_(adapter_object_path) {}
|
||||
~BluetoothDevices() = default;
|
||||
adapter_object_path_(std::move(adapter_object_path)) {}
|
||||
|
||||
std::optional<std::reference_wrapper<BluetoothDevice>>
|
||||
get_device_by_path(const sdbus::ObjectPath &);
|
||||
|
||||
@@ -29,8 +29,9 @@ namespace linux {
|
||||
|
||||
void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) {
|
||||
if (error != nullptr && error->isValid()) {
|
||||
auto name = error->getName();
|
||||
api::BluetoothPairingCallback::PairingError err;
|
||||
const auto &name = error->getName();
|
||||
api::BluetoothPairingCallback::PairingError err =
|
||||
api::BluetoothPairingCallback::PairingError::kAuthFailed;
|
||||
|
||||
NEARBY_LOGS(ERROR) << __func__ << ": "
|
||||
<< "Got error '" << error->getName()
|
||||
@@ -46,8 +47,6 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) {
|
||||
err = api::BluetoothPairingCallback::PairingError::kAuthRejected;
|
||||
} else if (name == "org.bluez.Error.AuthenticationTimeout") {
|
||||
err = api::BluetoothPairingCallback::PairingError::kAuthTimeout;
|
||||
} else {
|
||||
err = api::BluetoothPairingCallback::PairingError::kAuthFailed;
|
||||
}
|
||||
|
||||
if (pairing_cb_.on_pairing_error_cb != nullptr) {
|
||||
@@ -59,7 +58,6 @@ void BluetoothPairing::pairing_reply_handler(const sdbus::Error *error) {
|
||||
if (pairing_cb_.on_paired_cb != nullptr) {
|
||||
pairing_cb_.on_paired_cb();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
BluetoothPairing::BluetoothPairing(BluetoothAdapter &adapter,
|
||||
|
||||
@@ -29,10 +29,9 @@
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class BluetoothPairing : public api::BluetoothPairing {
|
||||
class BluetoothPairing final : public api::BluetoothPairing {
|
||||
public:
|
||||
BluetoothPairing(BluetoothAdapter &adapter, BluetoothDevice &remote_device);
|
||||
~BluetoothPairing() override = default;
|
||||
|
||||
bool InitiatePairing(api::BluetoothPairingCallback pairing_cb) override;
|
||||
bool FinishPairing(std::optional<absl::string_view> pin_code) override;
|
||||
|
||||
@@ -108,7 +108,7 @@ std::optional<std::u16string> DeviceInfo::GetFullName() const {
|
||||
|
||||
std::optional<std::string> DeviceInfo::GetProfileUserName() const {
|
||||
struct passwd *pwd = getpwuid(getuid());
|
||||
if (!pwd) {
|
||||
if (pwd == nullptr) {
|
||||
return std::nullopt;
|
||||
}
|
||||
char *name = strtok(pwd->pw_gecos, ",");
|
||||
@@ -122,7 +122,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetDownloadPath() const {
|
||||
|
||||
std::optional<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
|
||||
char *dir = getenv("XDG_CONFIG_HOME");
|
||||
if (dir == NULL) {
|
||||
if (dir == nullptr) {
|
||||
return std::filesystem::path("/tmp");
|
||||
}
|
||||
return std::filesystem::path(std::string(dir)) / "Google Nearby";
|
||||
@@ -130,7 +130,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetLocalAppDataPath() const {
|
||||
|
||||
std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
|
||||
char *dir = getenv("XDG_RUNTIME_PATH");
|
||||
if (dir == NULL) {
|
||||
if (dir == nullptr) {
|
||||
return std::filesystem::path("/tmp");
|
||||
}
|
||||
return std::filesystem::path(std::string(dir)) / "Google Nearby";
|
||||
@@ -138,7 +138,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetTemporaryPath() const {
|
||||
|
||||
std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
|
||||
char *dir = getenv("XDG_STATE_HOME");
|
||||
if (dir == NULL) {
|
||||
if (dir == nullptr) {
|
||||
return std::filesystem::path("/tmp");
|
||||
}
|
||||
return std::filesystem::path(std::string(dir)) / "Google Nearby" / "logs";
|
||||
@@ -146,7 +146,7 @@ std::optional<std::filesystem::path> DeviceInfo::GetLogPath() const {
|
||||
|
||||
std::optional<std::filesystem::path> DeviceInfo::GetCrashDumpPath() const {
|
||||
char *dir = getenv("XDG_STATE_HOME");
|
||||
if (dir == NULL) {
|
||||
if (dir == nullptr) {
|
||||
return std::filesystem::path("/tmp");
|
||||
}
|
||||
return std::filesystem::path(std::string(dir)) / "Google Nearby" / "crashes";
|
||||
|
||||
@@ -34,15 +34,19 @@
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
|
||||
class CurrentUserSession
|
||||
class CurrentUserSession final
|
||||
: public sdbus::ProxyInterfaces<org::freedesktop::login1::Session_proxy> {
|
||||
public:
|
||||
CurrentUserSession(sdbus::IConnection &system_bus)
|
||||
public:
|
||||
CurrentUserSession(const CurrentUserSession &) = delete;
|
||||
CurrentUserSession(CurrentUserSession &&) = delete;
|
||||
CurrentUserSession &operator=(const CurrentUserSession &) = delete;
|
||||
CurrentUserSession &operator=(CurrentUserSession &&) = delete;
|
||||
~CurrentUserSession() { unregisterProxy(); }
|
||||
explicit CurrentUserSession(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.login1",
|
||||
"/org/freedesktop/login1/session/auto") {
|
||||
registerProxy();
|
||||
}
|
||||
~CurrentUserSession() { unregisterProxy(); }
|
||||
|
||||
void RegisterScreenLockedListener(
|
||||
absl::string_view listener_name,
|
||||
@@ -51,7 +55,7 @@ public:
|
||||
void UnregisterScreenLockedListener(absl::string_view listener_name)
|
||||
ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onPauseDevice(const uint32_t &major, const uint32_t &minor,
|
||||
const std::string &type) override {}
|
||||
void onResumeDevice(const uint32_t &major, const uint32_t &minor,
|
||||
@@ -60,7 +64,7 @@ protected:
|
||||
void onLock() override ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_);
|
||||
void onUnlock() override ABSL_LOCKS_EXCLUDED(screen_lock_listeners_mutex_);
|
||||
|
||||
private:
|
||||
private:
|
||||
absl::Mutex screen_lock_listeners_mutex_;
|
||||
absl::flat_hash_map<std::string,
|
||||
std::function<void(api::DeviceInfo::ScreenStatus)>>
|
||||
@@ -69,26 +73,33 @@ private:
|
||||
|
||||
class Hostnamed
|
||||
: public sdbus::ProxyInterfaces<org::freedesktop::hostname1_proxy> {
|
||||
public:
|
||||
Hostnamed(sdbus::IConnection &system_bus)
|
||||
public:
|
||||
Hostnamed(const Hostnamed &) = delete;
|
||||
Hostnamed(Hostnamed &&) = delete;
|
||||
Hostnamed &operator=(const Hostnamed &) = delete;
|
||||
Hostnamed &operator=(Hostnamed &&) = delete;
|
||||
explicit Hostnamed(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.hostname1",
|
||||
"/org/freedesktop/hostname1") {
|
||||
registerProxy();
|
||||
}
|
||||
~Hostnamed() { unregisterProxy(); }
|
||||
~Hostnamed() { unregisterProxy(); }
|
||||
};
|
||||
|
||||
class LoginManager
|
||||
class LoginManager final
|
||||
: public sdbus::ProxyInterfaces<org::freedesktop::login1::Manager_proxy> {
|
||||
public:
|
||||
LoginManager(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces("org.freedesktop.login1",
|
||||
"/org/freedesktop/login1") {
|
||||
public:
|
||||
LoginManager(const LoginManager &) = delete;
|
||||
LoginManager(LoginManager &&) = delete;
|
||||
LoginManager &operator=(const LoginManager &) = delete;
|
||||
LoginManager &operator=(LoginManager &&) = delete;
|
||||
explicit LoginManager(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.login1", "/org/freedesktop/login1") {
|
||||
registerProxy();
|
||||
}
|
||||
~LoginManager() { unregisterProxy(); }
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onSessionNew(const std::string &session_id,
|
||||
const sdbus::ObjectPath &object_path) override {}
|
||||
void onSessionRemoved(const std::string &session_id,
|
||||
@@ -105,15 +116,14 @@ protected:
|
||||
void onPrepareForSleep(const bool &start) override {}
|
||||
};
|
||||
|
||||
class DeviceInfo : public api::DeviceInfo {
|
||||
public:
|
||||
DeviceInfo(sdbus::IConnection &system_bus);
|
||||
~DeviceInfo() override = default;
|
||||
class DeviceInfo final : public api::DeviceInfo {
|
||||
public:
|
||||
explicit DeviceInfo(sdbus::IConnection &system_bus);
|
||||
|
||||
std::optional<std::u16string> GetOsDeviceName() const override;
|
||||
api::DeviceInfo::DeviceType GetDeviceType() const override;
|
||||
api::DeviceInfo::OsType GetOsType() const override {
|
||||
return api::DeviceInfo::OsType::kWindows; // Or ChromeOS?
|
||||
return api::DeviceInfo::OsType::kWindows; // Or ChromeOS?
|
||||
}
|
||||
std::optional<std::u16string> GetFullName() const override;
|
||||
std::optional<std::u16string> GetGivenName() const override {
|
||||
@@ -138,23 +148,23 @@ public:
|
||||
absl::string_view listener_name,
|
||||
std::function<void(api::DeviceInfo::ScreenStatus)> callback) override {
|
||||
current_user_session_->RegisterScreenLockedListener(listener_name,
|
||||
std::move(callback));
|
||||
std::move(callback));
|
||||
}
|
||||
void
|
||||
UnregisterScreenLockedListener(absl::string_view listener_name) override {
|
||||
void UnregisterScreenLockedListener(
|
||||
absl::string_view listener_name) override {
|
||||
current_user_session_->UnregisterScreenLockedListener(listener_name);
|
||||
}
|
||||
|
||||
bool PreventSleep() override;
|
||||
bool AllowSleep() override;
|
||||
|
||||
private:
|
||||
private:
|
||||
sdbus::IConnection &system_bus_;
|
||||
std::unique_ptr<CurrentUserSession> current_user_session_;
|
||||
std::unique_ptr<LoginManager> login_manager_;
|
||||
std::optional<sdbus::UnixFd> inhibit_fd_;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif // PLATFORM_IMPL_LINUX_DEVICE_INFO_H_
|
||||
#endif // PLATFORM_IMPL_LINUX_DEVICE_INFO_H_
|
||||
|
||||
@@ -31,21 +31,24 @@ namespace linux {
|
||||
|
||||
class ThreadPool {
|
||||
public:
|
||||
ThreadPool(size_t max_pool_size);
|
||||
~ThreadPool();
|
||||
ThreadPool(const ThreadPool &) = delete;
|
||||
ThreadPool(ThreadPool &&) = delete;
|
||||
ThreadPool &operator=(const ThreadPool &) = delete;
|
||||
ThreadPool &operator=(ThreadPool &&) = delete;
|
||||
explicit ThreadPool(size_t max_pool_size);
|
||||
~ThreadPool();
|
||||
|
||||
bool Start() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
bool Start() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
// Runs a task on thread pool. The result indicates whether the task is put
|
||||
// into the thread pool.
|
||||
bool Run(Runnable &&task) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
// Runs a task on thread pool. The result indicates whether the task is put
|
||||
// into the thread pool.
|
||||
bool Run(Runnable &&task) ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
void ShutDown() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
void ShutDown() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
Runnable NextTask() ABSL_LOCKS_EXCLUDED(mutex_);
|
||||
|
||||
private:
|
||||
size_t max_pool_size_;
|
||||
std::atomic_bool shut_down_;
|
||||
|
||||
|
||||
@@ -30,9 +30,8 @@ public:
|
||||
sdbus::IConnection &system_bus,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
std::unique_ptr<NetworkManagerWifiMedium> wireless_device)
|
||||
: system_bus_(system_bus), network_manager_(network_manager),
|
||||
: system_bus_(system_bus), network_manager_(std::move(network_manager)),
|
||||
wireless_device_(std::move(wireless_device)) {}
|
||||
~NetworkManagerWifiDirectMedium() {}
|
||||
|
||||
bool IsInterfaceValid() const override { return true; }
|
||||
std::unique_ptr<api::WifiDirectSocket>
|
||||
|
||||
@@ -15,30 +15,35 @@
|
||||
#ifndef PLATFORM_IMPL_LINUX_WIFI_DIRECT_SERVER_SOCKET_H_
|
||||
#define PLATFORM_IMPL_LINUX_WIFI_DIRECT_SERVER_SOCKET_H_
|
||||
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
#include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
#include "internal/platform/implementation/wifi_direct.h"
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class NetworkManagerWifiDirectServerSocket
|
||||
: public api::WifiDirectServerSocket {
|
||||
public:
|
||||
NetworkManagerWifiDirectServerSocket(int socket, sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &active_connection_path,
|
||||
std::shared_ptr<NetworkManager> network_manager) : fd_(socket), system_bus_(system_bus), active_connection_path_(active_connection_path), network_manager_(network_manager) {}
|
||||
~NetworkManagerWifiDirectServerSocket() {}
|
||||
namespace linux {
|
||||
class NetworkManagerWifiDirectServerSocket
|
||||
: public api::WifiDirectServerSocket {
|
||||
public:
|
||||
NetworkManagerWifiDirectServerSocket(
|
||||
int socket, sdbus::IConnection &system_bus,
|
||||
sdbus::ObjectPath active_connection_path,
|
||||
std::shared_ptr<NetworkManager> network_manager)
|
||||
: fd_(socket),
|
||||
system_bus_(system_bus),
|
||||
active_connection_path_(std::move(active_connection_path)),
|
||||
network_manager_(std::move(network_manager)) {}
|
||||
|
||||
std::string GetIPAddress() const override;
|
||||
int GetPort() const override;
|
||||
std::unique_ptr<api::WifiDirectSocket> Accept() override;
|
||||
Exception Close() override;
|
||||
private:
|
||||
std::string GetIPAddress() const override;
|
||||
int GetPort() const override;
|
||||
std::unique_ptr<api::WifiDirectSocket> Accept() override;
|
||||
Exception Close() override;
|
||||
|
||||
private:
|
||||
sdbus::UnixFd fd_;
|
||||
sdbus::IConnection &system_bus_;
|
||||
sdbus::ObjectPath active_connection_path_;
|
||||
std::shared_ptr<NetworkManager> network_manager_ ;
|
||||
};
|
||||
}
|
||||
}
|
||||
std::shared_ptr<NetworkManager> network_manager_;
|
||||
};
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,9 +23,8 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiDirectSocket : public api::WifiDirectSocket {
|
||||
public:
|
||||
WifiDirectSocket(int socket)
|
||||
: fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {}
|
||||
~WifiDirectSocket() = default;
|
||||
explicit WifiDirectSocket(int socket)
|
||||
: fd_(sdbus::UnixFd(socket)), output_stream_(fd_), input_stream_(fd_) {}
|
||||
|
||||
InputStream &GetInputStream() override { return input_stream_; };
|
||||
OutputStream &GetOutputStream() override { return output_stream_; };
|
||||
|
||||
@@ -25,38 +25,38 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class NetworkManagerWifiHotspotMedium : public api::WifiHotspotMedium {
|
||||
public:
|
||||
NetworkManagerWifiHotspotMedium(
|
||||
sdbus::IConnection &system_bus,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
const sdbus::ObjectPath &wireless_device_object_path)
|
||||
: system_bus_(system_bus),
|
||||
wireless_device_(std::make_unique<NetworkManagerWifiMedium>(
|
||||
network_manager, system_bus, wireless_device_object_path)),
|
||||
network_manager_(network_manager) {}
|
||||
NetworkManagerWifiHotspotMedium(
|
||||
sdbus::IConnection &system_bus,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
std::unique_ptr<NetworkManagerWifiMedium> wireless_device)
|
||||
: system_bus_(system_bus), wireless_device_(std::move(wireless_device)),
|
||||
network_manager_(network_manager) {}
|
||||
~NetworkManagerWifiHotspotMedium() {}
|
||||
NetworkManagerWifiHotspotMedium(
|
||||
sdbus::IConnection &system_bus,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
sdbus::ObjectPath wireless_device_object_path)
|
||||
: system_bus_(system_bus),
|
||||
wireless_device_(std::make_unique<NetworkManagerWifiMedium>(
|
||||
network_manager, system_bus, std::move(wireless_device_object_path))),
|
||||
network_manager_(std::move(network_manager)) {}
|
||||
NetworkManagerWifiHotspotMedium(
|
||||
sdbus::IConnection &system_bus,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
std::unique_ptr<NetworkManagerWifiMedium> wireless_device)
|
||||
: system_bus_(system_bus),
|
||||
wireless_device_(std::move(wireless_device)),
|
||||
network_manager_(std::move(network_manager)) {}
|
||||
|
||||
bool IsInterfaceValid() const override { return true; }
|
||||
std::unique_ptr<api::WifiHotspotSocket>
|
||||
ConnectToService(absl::string_view ip_address, int port,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
std::unique_ptr<api::WifiHotspotServerSocket>
|
||||
ListenForService(int port) override;
|
||||
bool IsInterfaceValid() const override { return true; }
|
||||
std::unique_ptr<api::WifiHotspotSocket> ConnectToService(
|
||||
absl::string_view ip_address, int port,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
std::unique_ptr<api::WifiHotspotServerSocket> ListenForService(
|
||||
int port) override;
|
||||
|
||||
bool StartWifiHotspot(HotspotCredentials *hotspot_credentials) override;
|
||||
bool StopWifiHotspot() override;
|
||||
bool StartWifiHotspot(HotspotCredentials *hotspot_credentials) override;
|
||||
bool StopWifiHotspot() override;
|
||||
|
||||
bool ConnectWifiHotspot(HotspotCredentials *hotspot_credentials) override;
|
||||
bool DisconnectWifiHotspot() override;
|
||||
bool ConnectWifiHotspot(HotspotCredentials *hotspot_credentials) override;
|
||||
bool DisconnectWifiHotspot() override;
|
||||
|
||||
absl::optional<std::pair<std::int32_t, std::int32_t>>
|
||||
GetDynamicPortRange() override {
|
||||
return absl::nullopt;
|
||||
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
|
||||
override {
|
||||
return absl::nullopt;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -27,12 +27,11 @@ class NetworkManagerWifiHotspotServerSocket
|
||||
public:
|
||||
NetworkManagerWifiHotspotServerSocket(
|
||||
int socket, sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &active_connection_path,
|
||||
sdbus::ObjectPath active_connection_path,
|
||||
std::shared_ptr<NetworkManager> network_manager)
|
||||
: fd_(socket), system_bus_(system_bus),
|
||||
active_connection_path_(active_connection_path),
|
||||
network_manager_(network_manager) {}
|
||||
~NetworkManagerWifiHotspotServerSocket() {}
|
||||
active_connection_path_(std::move(active_connection_path)),
|
||||
network_manager_(std::move(network_manager)) {}
|
||||
|
||||
std::string GetIPAddress() const override;
|
||||
int GetPort() const override;
|
||||
|
||||
@@ -22,10 +22,9 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiHotspotSocket : public api::WifiHotspotSocket {
|
||||
public:
|
||||
WifiHotspotSocket(int connection_fd)
|
||||
explicit WifiHotspotSocket(int connection_fd)
|
||||
: fd_(sdbus::UnixFd(connection_fd)), output_stream_(fd_),
|
||||
input_stream_(fd_) {}
|
||||
~WifiHotspotSocket() {}
|
||||
|
||||
nearby::InputStream &GetInputStream() override { return input_stream_; };
|
||||
nearby::OutputStream &GetOutputStream() override { return output_stream_; };
|
||||
|
||||
@@ -27,8 +27,7 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiLanMedium : public api::WifiLanMedium {
|
||||
public:
|
||||
WifiLanMedium(sdbus::IConnection &system_bus);
|
||||
~WifiLanMedium() override = default;
|
||||
explicit WifiLanMedium(sdbus::IConnection &system_bus);
|
||||
|
||||
bool IsNetworkConnected() const override;
|
||||
|
||||
|
||||
@@ -28,12 +28,11 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiLanServerSocket : public api::WifiLanServerSocket {
|
||||
public:
|
||||
WifiLanServerSocket(int socket,
|
||||
explicit WifiLanServerSocket(int socket,
|
||||
std::shared_ptr<NetworkManager> network_manager,
|
||||
sdbus::IConnection &system_bus)
|
||||
: fd_(sdbus::UnixFd(socket)), network_manager_(network_manager),
|
||||
: fd_(sdbus::UnixFd(socket)), network_manager_(std::move(network_manager)),
|
||||
system_bus_(system_bus) {}
|
||||
~WifiLanServerSocket() override = default;
|
||||
|
||||
std::string GetIPAddress() const override;
|
||||
int GetPort() const override;
|
||||
|
||||
@@ -28,9 +28,8 @@ namespace nearby {
|
||||
namespace linux {
|
||||
class WifiLanSocket : public api::WifiLanSocket {
|
||||
public:
|
||||
WifiLanSocket(sdbus::UnixFd fd)
|
||||
explicit WifiLanSocket(sdbus::UnixFd fd)
|
||||
: fd_(fd), output_stream_(fd), input_stream_(fd) {}
|
||||
~WifiLanSocket() = default;
|
||||
|
||||
nearby::InputStream &GetInputStream() override {
|
||||
return input_stream_;
|
||||
|
||||
@@ -21,30 +21,35 @@
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <ostream>
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
#include <sdbus-c++/ProxyInterfaces.h>
|
||||
#include <sdbus-c++/StandardInterfaces.h>
|
||||
#include <sdbus-c++/Types.h>
|
||||
#include <ostream>
|
||||
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/linux/dbus.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/access_point_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/networkmanager_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/connection_active_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/device_wireless_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/ip4config_client.h"
|
||||
#include "internal/platform/implementation/linux/generated/dbus/networkmanager/networkmanager_client.h"
|
||||
#include "internal/platform/implementation/wifi.h"
|
||||
#include "internal/platform/logging.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
class NetworkManager
|
||||
class NetworkManager final
|
||||
: public sdbus::ProxyInterfaces<org::freedesktop::NetworkManager_proxy> {
|
||||
public:
|
||||
NetworkManager(sdbus::IConnection &system_bus)
|
||||
public:
|
||||
NetworkManager(const NetworkManager &) = delete;
|
||||
NetworkManager(NetworkManager &&) = delete;
|
||||
NetworkManager &operator=(const NetworkManager &) = delete;
|
||||
NetworkManager &operator=(NetworkManager &&) = delete;
|
||||
explicit NetworkManager(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
"/org/freedesktop/NetworkManager") {
|
||||
"/org/freedesktop/NetworkManager"),
|
||||
state_(0) {
|
||||
registerProxy();
|
||||
try {
|
||||
state_ = State();
|
||||
@@ -56,20 +61,24 @@ public:
|
||||
|
||||
std::uint32_t getState() const { return state_; }
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onCheckPermissions() override {}
|
||||
void onStateChanged(const uint32_t &state) override { state_ = state; }
|
||||
void onDeviceAdded(const sdbus::ObjectPath &device_path) override {}
|
||||
void onDeviceRemoved(const sdbus::ObjectPath &device_path) override {}
|
||||
|
||||
private:
|
||||
private:
|
||||
std::atomic_uint32_t state_;
|
||||
};
|
||||
|
||||
class NetworkManagerIP4Config
|
||||
: public sdbus::ProxyInterfaces<
|
||||
org::freedesktop::NetworkManager::IP4Config_proxy> {
|
||||
public:
|
||||
public:
|
||||
NetworkManagerIP4Config(const NetworkManagerIP4Config &) = delete;
|
||||
NetworkManagerIP4Config(NetworkManagerIP4Config &&) = delete;
|
||||
NetworkManagerIP4Config &operator=(const NetworkManagerIP4Config &) = delete;
|
||||
NetworkManagerIP4Config &operator=(NetworkManagerIP4Config &&) = delete;
|
||||
NetworkManagerIP4Config(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &config_object_path)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
@@ -82,11 +91,16 @@ public:
|
||||
class NetworkManagerAccessPoint
|
||||
: public sdbus::ProxyInterfaces<
|
||||
org::freedesktop::NetworkManager::AccessPoint_proxy> {
|
||||
public:
|
||||
public:
|
||||
NetworkManagerAccessPoint(const NetworkManagerAccessPoint &) = delete;
|
||||
NetworkManagerAccessPoint(NetworkManagerAccessPoint &&) = delete;
|
||||
NetworkManagerAccessPoint &operator=(const NetworkManagerAccessPoint &) =
|
||||
delete;
|
||||
NetworkManagerAccessPoint &operator=(NetworkManagerAccessPoint &&) = delete;
|
||||
NetworkManagerAccessPoint(sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &access_point_object_path)
|
||||
sdbus::ObjectPath access_point_object_path)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
access_point_object_path) {
|
||||
std::move(access_point_object_path)) {
|
||||
registerProxy();
|
||||
}
|
||||
~NetworkManagerAccessPoint() { unregisterProxy(); }
|
||||
@@ -123,12 +137,20 @@ extern std::ostream &operator<<(std::ostream &s,
|
||||
class NetworkManagerActiveConnection
|
||||
: public sdbus::ProxyInterfaces<
|
||||
org::freedesktop::NetworkManager::Connection::Active_proxy> {
|
||||
public:
|
||||
NetworkManagerActiveConnection(
|
||||
sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &active_connection_path)
|
||||
public:
|
||||
NetworkManagerActiveConnection(const NetworkManagerActiveConnection &) =
|
||||
delete;
|
||||
NetworkManagerActiveConnection(NetworkManagerActiveConnection &&) = delete;
|
||||
NetworkManagerActiveConnection &operator=(
|
||||
const NetworkManagerActiveConnection &) = delete;
|
||||
NetworkManagerActiveConnection &operator=(NetworkManagerActiveConnection &&) =
|
||||
delete;
|
||||
explicit NetworkManagerActiveConnection(
|
||||
sdbus::IConnection &system_bus, sdbus::ObjectPath active_connection_path)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
active_connection_path) {
|
||||
std::move(active_connection_path)),
|
||||
state_(kStateUnknown),
|
||||
reason_(kStateReasonUnknown) {
|
||||
registerProxy();
|
||||
try {
|
||||
auto state = State();
|
||||
@@ -139,9 +161,9 @@ public:
|
||||
DBUS_LOG_PROPERTY_GET_ERROR(this, "State", e);
|
||||
}
|
||||
}
|
||||
~NetworkManagerActiveConnection() { unregisterProxy(); }
|
||||
virtual ~NetworkManagerActiveConnection() { unregisterProxy(); }
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onStateChanged(const uint32_t &state, const uint32_t &reason) override
|
||||
ABSL_LOCKS_EXCLUDED(state_mutex_) {
|
||||
absl::MutexLock l(&state_mutex_);
|
||||
@@ -153,9 +175,9 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
std::pair<std::optional<ActiveConnectionStateReason>, bool>
|
||||
WaitForConnection(absl::Duration timeout = absl::Seconds(10))
|
||||
public:
|
||||
std::pair<std::optional<ActiveConnectionStateReason>, bool> WaitForConnection(
|
||||
absl::Duration timeout = absl::Seconds(10))
|
||||
ABSL_LOCKS_EXCLUDED(state_mutex_) {
|
||||
NEARBY_LOGS(VERBOSE) << __func__ << ": Waiting for an update to "
|
||||
<< getObjectPath() << "'s state";
|
||||
@@ -208,36 +230,42 @@ public:
|
||||
return ip4addresses;
|
||||
}
|
||||
|
||||
private:
|
||||
private:
|
||||
absl::Mutex state_mutex_;
|
||||
ActiveConnectionState state_ ABSL_GUARDED_BY(state_mutex_);
|
||||
ActiveConnectionStateReason reason_ ABSL_GUARDED_BY(state_mutex_);
|
||||
};
|
||||
|
||||
class NetworkManagerObjectManager
|
||||
class NetworkManagerObjectManager final
|
||||
: public sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||
public:
|
||||
NetworkManagerObjectManager(sdbus::IConnection &system_bus)
|
||||
public:
|
||||
NetworkManagerObjectManager(const NetworkManagerObjectManager &) = delete;
|
||||
NetworkManagerObjectManager(NetworkManagerObjectManager &&) = delete;
|
||||
NetworkManagerObjectManager &operator=(const NetworkManagerObjectManager &) =
|
||||
delete;
|
||||
NetworkManagerObjectManager &operator=(NetworkManagerObjectManager &&) =
|
||||
delete;
|
||||
explicit NetworkManagerObjectManager(sdbus::IConnection &system_bus)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
"/org/freedesktop") {
|
||||
registerProxy();
|
||||
}
|
||||
~NetworkManagerObjectManager() { unregisterProxy(); }
|
||||
|
||||
std::unique_ptr<NetworkManagerIP4Config>
|
||||
GetIp4Config(const sdbus::ObjectPath &access_point);
|
||||
std::unique_ptr<NetworkManagerIP4Config> GetIp4Config(
|
||||
const sdbus::ObjectPath &access_point);
|
||||
std::unique_ptr<NetworkManagerActiveConnection>
|
||||
GetActiveConnectionForAccessPoint(const sdbus::ObjectPath &access_point_path,
|
||||
const sdbus::ObjectPath &device_path);
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onInterfacesAdded(
|
||||
const sdbus::ObjectPath &objectPath,
|
||||
const std::map<std::string, std::map<std::string, sdbus::Variant>>
|
||||
&interfacesAndProperties) override {}
|
||||
void
|
||||
onInterfacesRemoved(const sdbus::ObjectPath &objectPath,
|
||||
const std::vector<std::string> &interfaces) override {}
|
||||
void onInterfacesRemoved(
|
||||
const sdbus::ObjectPath &objectPath,
|
||||
const std::vector<std::string> &interfaces) override {}
|
||||
};
|
||||
|
||||
class NetworkManagerWifiMedium
|
||||
@@ -245,21 +273,26 @@ class NetworkManagerWifiMedium
|
||||
public sdbus::ProxyInterfaces<
|
||||
org::freedesktop::NetworkManager::Device::Wireless_proxy,
|
||||
sdbus::Properties_proxy> {
|
||||
public:
|
||||
public:
|
||||
NetworkManagerWifiMedium(const NetworkManagerWifiMedium &) = delete;
|
||||
NetworkManagerWifiMedium(NetworkManagerWifiMedium &&) = delete;
|
||||
NetworkManagerWifiMedium &operator=(const NetworkManagerWifiMedium &) =
|
||||
delete;
|
||||
NetworkManagerWifiMedium &operator=(NetworkManagerWifiMedium &&) = delete;
|
||||
NetworkManagerWifiMedium(std::shared_ptr<NetworkManager> network_manager,
|
||||
sdbus::IConnection &system_bus,
|
||||
const sdbus::ObjectPath &wireless_device_object_path)
|
||||
: ProxyInterfaces(system_bus, "org.freedesktop.NetworkManager",
|
||||
wireless_device_object_path),
|
||||
network_manager_(std::move(network_manager)) {
|
||||
network_manager_(std::move(network_manager)),
|
||||
last_scan_(-1) {
|
||||
registerProxy();
|
||||
}
|
||||
|
||||
~NetworkManagerWifiMedium() override { unregisterProxy(); }
|
||||
|
||||
class ScanResultCallback : public api::WifiMedium::ScanResultCallback {
|
||||
public:
|
||||
~ScanResultCallback() override = default;
|
||||
public:
|
||||
void OnScanResults(
|
||||
const std::vector<api::WifiScanResult> &scan_results) override {
|
||||
// TODO: Add implementation at some point
|
||||
@@ -272,21 +305,20 @@ public:
|
||||
bool Scan(
|
||||
const api::WifiMedium::ScanResultCallback &scan_result_callback) override;
|
||||
|
||||
std::shared_ptr<NetworkManagerAccessPoint>
|
||||
SearchBySSID(absl::string_view ssid,
|
||||
absl::Duration scan_timeout = absl::Seconds(15))
|
||||
std::shared_ptr<NetworkManagerAccessPoint> SearchBySSID(
|
||||
absl::string_view ssid, absl::Duration scan_timeout = absl::Seconds(15))
|
||||
ABSL_LOCKS_EXCLUDED(known_access_points_lock_);
|
||||
|
||||
api::WifiConnectionStatus
|
||||
ConnectToNetwork(absl::string_view ssid, absl::string_view password,
|
||||
api::WifiAuthType auth_type) override;
|
||||
api::WifiConnectionStatus ConnectToNetwork(
|
||||
absl::string_view ssid, absl::string_view password,
|
||||
api::WifiAuthType auth_type) override;
|
||||
|
||||
bool VerifyInternetConnectivity() override;
|
||||
std::string GetIpAddress() override;
|
||||
|
||||
std::unique_ptr<NetworkManagerActiveConnection> GetActiveConnection();
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void onPropertiesChanged(
|
||||
const std::string &interfaceName,
|
||||
const std::map<std::string, sdbus::Variant> &changedProperties,
|
||||
@@ -306,9 +338,9 @@ protected:
|
||||
known_access_points_.erase(access_point);
|
||||
}
|
||||
|
||||
private:
|
||||
std::shared_ptr<NetworkManagerAccessPoint>
|
||||
SearchBySSIDNoScan(std::vector<std::uint8_t> &ssid)
|
||||
private:
|
||||
std::shared_ptr<NetworkManagerAccessPoint> SearchBySSIDNoScan(
|
||||
std::vector<std::uint8_t> &ssid)
|
||||
ABSL_LOCKS_EXCLUDED(known_access_points_lock_);
|
||||
|
||||
std::shared_ptr<NetworkManager> network_manager_;
|
||||
@@ -329,7 +361,7 @@ private:
|
||||
std::int64_t last_scan_ ABSL_GUARDED_BY(last_scan_lock_);
|
||||
};
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user