mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Enabled BleV2 medium by default. BLE v2 advertisements are now correctly broadcasted
This commit is contained in:
@@ -39,7 +39,7 @@ constexpr auto kEnableAutoReconnect =
|
|||||||
flags::Flag<bool>(kConfigPackage, "45427690", false);
|
flags::Flag<bool>(kConfigPackage, "45427690", false);
|
||||||
// Disable/Enable BLE v2 in Nearby Connections SDK.
|
// Disable/Enable BLE v2 in Nearby Connections SDK.
|
||||||
constexpr auto kEnableBleV2 =
|
constexpr auto kEnableBleV2 =
|
||||||
flags::Flag<bool>(kConfigPackage, "45401515", false);
|
flags::Flag<bool>(kConfigPackage, "45401515", true);
|
||||||
// Disable/Enable GATT query in thread in BLE V2.
|
// Disable/Enable GATT query in thread in BLE V2.
|
||||||
// Manual edit: setting this to false for ChromeOS rollout as well.
|
// Manual edit: setting this to false for ChromeOS rollout as well.
|
||||||
constexpr auto kEnableGattQueryInThread =
|
constexpr auto kEnableGattQueryInThread =
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ cc_library(
|
|||||||
hdrs = [
|
hdrs = [
|
||||||
"avahi.h",
|
"avahi.h",
|
||||||
# "ble_medium.h",
|
# "ble_medium.h",
|
||||||
# "ble_v2_medium.h",
|
"ble_v2.h",
|
||||||
"bluetooth_adapter.h",
|
"bluetooth_adapter.h",
|
||||||
# "bluetooth_bluez_profile.h",
|
# "bluetooth_bluez_profile.h",
|
||||||
# "bluetooth_classic_device.h",
|
# "bluetooth_classic_device.h",
|
||||||
@@ -66,6 +66,8 @@ cc_library(
|
|||||||
# "bluetooth_classic_socket.h",
|
# "bluetooth_classic_socket.h",
|
||||||
# "bluetooth_devices.h",
|
# "bluetooth_devices.h",
|
||||||
# "bluetooth_pairing.h",
|
# "bluetooth_pairing.h",
|
||||||
|
"ble_v2_advertisement.h",
|
||||||
|
"bluez_profiles.h",
|
||||||
"bluez.h",
|
"bluez.h",
|
||||||
"dbus.h",
|
"dbus.h",
|
||||||
"network_manager.h",
|
"network_manager.h",
|
||||||
@@ -135,6 +137,9 @@ cc_library(
|
|||||||
# "bluetooth_classic_socket.cc",
|
# "bluetooth_classic_socket.cc",
|
||||||
# "bluetooth_devices.cc",
|
# "bluetooth_devices.cc",
|
||||||
# "bluetooth_pairing.cc",
|
# "bluetooth_pairing.cc",
|
||||||
|
"bluez_profiles.cc",
|
||||||
|
"ble_v2_advertisement.cc",
|
||||||
|
"ble_v2.cc",
|
||||||
"bluez.cc",
|
"bluez.cc",
|
||||||
"dbus.cc",
|
"dbus.cc",
|
||||||
"executor.cc",
|
"executor.cc",
|
||||||
|
|||||||
@@ -0,0 +1,156 @@
|
|||||||
|
#include <sdbus-c++/Types.h>
|
||||||
|
#include "internal/platform/implementation/linux/ble_v2.h"
|
||||||
|
|
||||||
|
#include "internal/platform/implementation/linux/ble_v2_advertisement.h"
|
||||||
|
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||||
|
#include "internal/platform/logging.h"
|
||||||
|
|
||||||
|
namespace nearby {
|
||||||
|
namespace linux {
|
||||||
|
BleV2Medium::BleV2Medium(api::BluetoothAdapter &adapter) : adapter_(dynamic_cast<linux::BluetoothAdapter *>(&adapter))
|
||||||
|
{
|
||||||
|
auto system_bus = linux::getSystemBusConnection();
|
||||||
|
bluez_object_manager_ = std::make_unique<bluez::BluezObjectManager>(*system_bus);
|
||||||
|
}
|
||||||
|
bool BleV2Medium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters)
|
||||||
|
{
|
||||||
|
if (!adapter_->IsEnabled())
|
||||||
|
{
|
||||||
|
NEARBY_LOGS(WARNING) << "BLE cannot start advertising because the "
|
||||||
|
"bluetooth adapter is not enabled.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (advertising_data.service_data.empty())
|
||||||
|
{
|
||||||
|
NEARBY_LOGS(WARNING) << "BLE cannot start to advertise due to invalid service data.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto system_bus = adapter_->GetConnection();
|
||||||
|
auto le_advertisement = linux::LEAdvertisement::CreateLEAdvertisement(
|
||||||
|
*system_bus, advertising_data, advertise_set_parameters);
|
||||||
|
|
||||||
|
if (!le_advertisement)
|
||||||
|
{
|
||||||
|
NEARBY_LOGS(WARNING) << "Some error occurred while creating LEAdvertisement object.";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
auto le_advertisement_manager =
|
||||||
|
std::make_unique<linux::LEAdvertisementManager>(*system_bus, *adapter_);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
le_advertisement_manager->RegisterAdvertisement(le_advertisement->getObjectPath(), {});
|
||||||
|
}
|
||||||
|
catch (const sdbus::Error& e)
|
||||||
|
{
|
||||||
|
DBUS_LOG_METHOD_CALL_ERROR(le_advertisement_manager, "RegisterAdvertisement", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// dummy overrides
|
||||||
|
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession>
|
||||||
|
BleV2Medium::StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters, AdvertisingCallback callback)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
bool BleV2Medium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
|
ScanCallback callback)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool BleV2Medium::StopAdvertising() { return false; }
|
||||||
|
bool BleV2Medium::IsExtendedAdvertisementsAvailable() { return true; }
|
||||||
|
std::unique_ptr<api::ble_v2::GattServer>
|
||||||
|
BleV2Medium::StartGattServer(api::ble_v2::ServerGattConnectionCallback callback)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
bool BleV2Medium::StopScanning() { return false; }
|
||||||
|
std::unique_ptr<api::ble_v2::GattClient>
|
||||||
|
BleV2Medium::ConnectToGattServer(api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
|
api::ble_v2::ClientGattConnectionCallback callback)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(const std::string &service_id)
|
||||||
|
{
|
||||||
|
|
||||||
|
LOG(INFO) << "OpenServerSocket is called";
|
||||||
|
|
||||||
|
auto server_socket = std::make_unique<BleV2ServerSocket>(*adapter_);
|
||||||
|
|
||||||
|
|
||||||
|
return server_socket;
|
||||||
|
}
|
||||||
|
bool BleV2Medium::GetRemotePeripheral(const std::string &mac_address, GetRemotePeripheralCallback callback)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
std::unique_ptr<api::ble_v2::BleSocket> BleV2Medium::Connect(const std::string &service_id,
|
||||||
|
api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
|
api::ble_v2::BlePeripheral &peripheral,
|
||||||
|
CancellationFlag *cancellation_flag)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
std::unique_ptr<api::ble_v2::BleMedium::ScanningSession>
|
||||||
|
BleV2Medium::StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
|
ScanningCallback callback)
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
// end of dummy overrides
|
||||||
|
|
||||||
|
BleV2ServerSocket::BleV2ServerSocket(api::BluetoothAdapter &adapter) :
|
||||||
|
adapter_(dynamic_cast<linux::BluetoothAdapter *>(&adapter))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<api::ble_v2::BleSocket> BleV2ServerSocket::Accept()
|
||||||
|
{
|
||||||
|
absl::MutexLock lock(&mutex_);
|
||||||
|
LOG(INFO) << __func__ << ": Accept is called.";
|
||||||
|
|
||||||
|
while (!closed_ && pending_sockets_.empty())
|
||||||
|
{
|
||||||
|
cond_.Wait(&mutex_);
|
||||||
|
}
|
||||||
|
if (closed_)
|
||||||
|
return nullptr;
|
||||||
|
|
||||||
|
linux::BleV2Socket ble_socket = pending_sockets_.front();
|
||||||
|
pending_sockets_.pop_front();
|
||||||
|
|
||||||
|
LOG(INFO) << __func__ << ": Accepted a remote connection.";
|
||||||
|
return std::make_unique<linux::BleV2Socket>(ble_socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
Exception BleV2ServerSocket::Close()
|
||||||
|
{
|
||||||
|
// TODO(b/271031645): implement BLE socket using weave
|
||||||
|
absl::MutexLock lock(&mutex_);
|
||||||
|
LOG(INFO) << __func__ << ": Close is called.";
|
||||||
|
|
||||||
|
if (closed_)
|
||||||
|
{
|
||||||
|
return {Exception::kSuccess};
|
||||||
|
}
|
||||||
|
|
||||||
|
closed_ = true;
|
||||||
|
cond_.SignalAll();
|
||||||
|
|
||||||
|
return {Exception::kSuccess};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace linux
|
||||||
|
} // namespace nearby
|
||||||
@@ -1,4 +1,6 @@
|
|||||||
|
#include "bluetooth_adapter.h"
|
||||||
#include "internal/platform/implementation/ble_v2.h"
|
#include "internal/platform/implementation/ble_v2.h"
|
||||||
|
#include "internal/platform/implementation/linux/stream.h"
|
||||||
|
|
||||||
namespace nearby {
|
namespace nearby {
|
||||||
namespace linux {
|
namespace linux {
|
||||||
@@ -6,110 +8,141 @@ class BleV2Peripheral : public api::ble_v2::BlePeripheral
|
|||||||
{
|
{
|
||||||
std::string GetAddress() const override;
|
std::string GetAddress() const override;
|
||||||
UniqueId GetUniqueId() const override;
|
UniqueId GetUniqueId() const override;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class GattClient: public api::ble_v2::GattClient
|
class BleV2Socket : public api::ble_v2::BleSocket {
|
||||||
|
public:
|
||||||
|
BleV2Socket() = delete;
|
||||||
|
explicit BleV2Socket(sdbus::UnixFd fd): input_stream_(fd), output_stream_(fd) {};
|
||||||
|
~BleV2Socket() override = default;
|
||||||
|
|
||||||
|
nearby::InputStream& GetInputStream() override { return input_stream_;};
|
||||||
|
|
||||||
|
nearby::OutputStream& GetOutputStream() override {return output_stream_;};
|
||||||
|
|
||||||
|
Exception Close() override
|
||||||
|
{
|
||||||
|
input_stream_.Close();
|
||||||
|
output_stream_.Close();
|
||||||
|
return Exception{Exception::kSuccess};
|
||||||
|
};
|
||||||
|
|
||||||
|
api::ble_v2::BlePeripheral* GetRemotePeripheral() override
|
||||||
|
{
|
||||||
|
return ble_peripheral_;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
linux::InputStream input_stream_;
|
||||||
|
linux::OutputStream output_stream_;
|
||||||
|
api::ble_v2::BlePeripheral* ble_peripheral_ = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
class GattClient : public api::ble_v2::GattClient
|
||||||
{
|
{
|
||||||
bool DiscoverServiceAndCharacteristics(const Uuid& service_uuid, const std::vector<Uuid>& characteristic_uuids) override;
|
bool DiscoverServiceAndCharacteristics(const Uuid &service_uuid,
|
||||||
|
const std::vector<Uuid> &characteristic_uuids) override;
|
||||||
|
|
||||||
absl::optional<api::ble_v2::GattCharacteristic>
|
absl::optional<api::ble_v2::GattCharacteristic> GetCharacteristic(const Uuid &service_uuid,
|
||||||
GetCharacteristic(const Uuid& service_uuid, const Uuid& characteristic_uuid) override;
|
const Uuid &characteristic_uuid) override;
|
||||||
|
|
||||||
absl::optional<std::string>
|
absl::optional<std::string> ReadCharacteristic(const api::ble_v2::GattCharacteristic &characteristic) override;
|
||||||
ReadCharacteristic(const api::ble_v2::GattCharacteristic& characteristic) override;
|
|
||||||
|
|
||||||
bool WriteCharacteristic(
|
bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, absl::string_view value,
|
||||||
const api::ble_v2::GattCharacteristic& characteristic,
|
WriteType type) override;
|
||||||
absl::string_view value, WriteType type) override;
|
|
||||||
|
|
||||||
bool SetCharacteristicSubscription(
|
bool SetCharacteristicSubscription(
|
||||||
const api::ble_v2::GattCharacteristic& characteristic,
|
const api::ble_v2::GattCharacteristic &characteristic, bool enable,
|
||||||
bool enable, absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb)
|
absl::AnyInvocable<void(absl::string_view value)> on_characteristic_changed_cb) override;
|
||||||
override;
|
|
||||||
|
|
||||||
void Disconnect() override;
|
void Disconnect() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class GattServer : public api::ble_v2::GattServer
|
class GattServer : public api::ble_v2::GattServer
|
||||||
{
|
{
|
||||||
api::ble_v2::BlePeripheral& GetBlePeripheral() override;
|
api::ble_v2::BlePeripheral &GetBlePeripheral() override;
|
||||||
|
|
||||||
absl::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
|
absl::optional<api::ble_v2::GattCharacteristic>
|
||||||
const Uuid& service_uuid, const Uuid& characteristic_uuid,
|
CreateCharacteristic(const Uuid &service_uuid, const Uuid &characteristic_uuid,
|
||||||
api::ble_v2::GattCharacteristic::Permission permission,
|
api::ble_v2::GattCharacteristic::Permission permission,
|
||||||
api::ble_v2::GattCharacteristic::Property property)
|
api::ble_v2::GattCharacteristic::Property property) override;
|
||||||
override;
|
|
||||||
|
|
||||||
bool UpdateCharacteristic(
|
bool UpdateCharacteristic(const api::ble_v2::GattCharacteristic &characteristic,
|
||||||
const api::ble_v2::GattCharacteristic& characteristic, const nearby::ByteArray& value)
|
const nearby::ByteArray &value) override;
|
||||||
override;
|
|
||||||
|
|
||||||
absl::Status NotifyCharacteristicChanged(
|
absl::Status NotifyCharacteristicChanged(const api::ble_v2::GattCharacteristic &characteristic, bool confirm,
|
||||||
const api::ble_v2::GattCharacteristic& characteristic, bool confirm,
|
const ByteArray &new_value) override;
|
||||||
const ByteArray& new_value)
|
|
||||||
override;
|
|
||||||
|
|
||||||
void Stop() override;
|
void Stop() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class BleV2Socket: public api::ble_v2::BleSocket
|
class BleV2ServerSocket : public api::ble_v2::BleServerSocket
|
||||||
{
|
|
||||||
InputStream& GetInputStream() override;
|
|
||||||
OutputStream& GetOutputStream() override;
|
|
||||||
Exception Close() override;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BleV2ServerSocket: public api::ble_v2::BleServerSocket
|
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
|
explicit BleV2ServerSocket(api::BluetoothAdapter& adapter);
|
||||||
|
~BleV2ServerSocket() override = default;
|
||||||
std::unique_ptr<api::ble_v2::BleSocket> Accept() override;
|
std::unique_ptr<api::ble_v2::BleSocket> Accept() override;
|
||||||
Exception Close() override;
|
Exception Close() override;
|
||||||
|
private:
|
||||||
|
// Accept notification.
|
||||||
|
absl::Mutex mutex_;
|
||||||
|
absl::CondVar cond_;
|
||||||
|
bool closed_ = false;
|
||||||
|
linux::BluetoothAdapter *adapter_;
|
||||||
|
std::deque<linux::BleV2Socket> pending_sockets_ ABSL_GUARDED_BY(mutex_);
|
||||||
};
|
};
|
||||||
class BleV2Medium: public api::ble_v2::BleMedium
|
|
||||||
|
class BleV2Medium : public api::ble_v2::BleMedium
|
||||||
{
|
{
|
||||||
std::unique_ptr<AdvertisingSession>
|
public:
|
||||||
StartAdvertising(const api::ble_v2::BleAdvertisementData& advertising_data,
|
BleV2Medium(const BleV2Medium &) = delete;
|
||||||
api::ble_v2::AdvertiseParameters advertise_set_parameters,
|
BleV2Medium(BleV2Medium &&) = delete;
|
||||||
AdvertisingCallback callback) override;
|
BleV2Medium &operator=(const BleV2Medium &) = delete;
|
||||||
|
BleV2Medium &operator=(BleV2Medium &&) = delete;
|
||||||
|
|
||||||
|
explicit BleV2Medium(api::BluetoothAdapter &adapter);
|
||||||
|
~BleV2Medium() override = default;
|
||||||
|
|
||||||
|
bool StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters) override;
|
||||||
|
|
||||||
|
std::unique_ptr<AdvertisingSession> StartAdvertising(const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters,
|
||||||
|
AdvertisingCallback callback) override;
|
||||||
|
|
||||||
|
|
||||||
bool StopAdvertising() override;
|
bool StopAdvertising() override;
|
||||||
|
|
||||||
std::unique_ptr<ScanningSession>
|
std::unique_ptr<ScanningSession> StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
StartScanning(const Uuid& service_uuid,
|
ScanningCallback callback) override;
|
||||||
api::ble_v2::TxPowerLevel tx_power_level,
|
|
||||||
ScanningCallback callback)
|
bool StartScanning(const Uuid &service_uuid, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
override;
|
ScanCallback callback) override;
|
||||||
|
|
||||||
bool StopScanning() override;
|
bool StopScanning() override;
|
||||||
|
|
||||||
std::unique_ptr<api::ble_v2::GattServer>
|
std::unique_ptr<api::ble_v2::GattServer> StartGattServer(api::ble_v2::ServerGattConnectionCallback callback) override;
|
||||||
StartGattServer(api::ble_v2::ServerGattConnectionCallback callback)
|
|
||||||
override;
|
|
||||||
|
|
||||||
std::unique_ptr<api::ble_v2::GattClient>
|
std::unique_ptr<api::ble_v2::GattClient>
|
||||||
ConnectToGattServer(api::ble_v2::BlePeripheral& peripheral,
|
ConnectToGattServer(api::ble_v2::BlePeripheral &peripheral, api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
api::ble_v2::TxPowerLevel tx_power_level,
|
api::ble_v2::ClientGattConnectionCallback callback) override;
|
||||||
api::ble_v2::ClientGattConnectionCallback callback)
|
|
||||||
override;
|
|
||||||
|
|
||||||
std::unique_ptr<api::ble_v2::BleServerSocket>
|
std::unique_ptr<api::ble_v2::BleServerSocket> OpenServerSocket(const std::string &service_id) override;
|
||||||
OpenServerSocket(const std::string& service_id)
|
|
||||||
override;
|
|
||||||
|
|
||||||
std::unique_ptr<api::ble_v2::BleSocket>
|
std::unique_ptr<api::ble_v2::BleSocket> Connect(const std::string &service_id,
|
||||||
Connect(const std::string& service_id,
|
api::ble_v2::TxPowerLevel tx_power_level,
|
||||||
api::ble_v2::TxPowerLevel tx_power_level,
|
api::ble_v2::BlePeripheral &peripheral,
|
||||||
api::ble_v2::BlePeripheral& peripheral,
|
CancellationFlag *cancellation_flag) override;
|
||||||
CancellationFlag* cancellation_flag)
|
|
||||||
override;
|
|
||||||
|
|
||||||
|
|
||||||
bool IsExtendedAdvertisementsAvailable() override;
|
bool IsExtendedAdvertisementsAvailable() override;
|
||||||
bool GetRemotePeripheral(const std::string &mac_address,
|
bool GetRemotePeripheral(const std::string &mac_address, GetRemotePeripheralCallback callback) override;
|
||||||
GetRemotePeripheralCallback callback) override;
|
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id, GetRemotePeripheralCallback callback) override;
|
||||||
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
|
||||||
GetRemotePeripheralCallback callback) override;
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
linux::BluetoothAdapter *adapter_;
|
||||||
|
std::unique_ptr<bluez::BluezObjectManager> bluez_object_manager_;
|
||||||
};
|
};
|
||||||
}
|
} // namespace linux
|
||||||
}
|
} // namespace nearby
|
||||||
|
|||||||
@@ -0,0 +1,38 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "internal/platform/implementation/ble_v2.h"
|
||||||
|
#include "internal/platform/implementation/linux/bluez.h"
|
||||||
|
#include "internal/platform/implementation/linux/ble_v2_advertisement.h"
|
||||||
|
#include "internal/platform/logging.h"
|
||||||
|
#include "internal/platform/uuid.h"
|
||||||
|
|
||||||
|
namespace nearby {
|
||||||
|
namespace linux {
|
||||||
|
LEAdvertisement::LEAdvertisement(
|
||||||
|
sdbus::IConnection& system_bus, sdbus::ObjectPath path,
|
||||||
|
const api::ble_v2::BleAdvertisementData& advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters)
|
||||||
|
: AdaptorInterfaces(system_bus, std::move(path)),
|
||||||
|
is_extended_advertisement_(advertising_data.is_extended_advertisement),
|
||||||
|
advertise_set_parameters_(advertise_set_parameters) {
|
||||||
|
for (const auto& [uuid, data] : advertising_data.service_data) {
|
||||||
|
std::string uuid_string(uuid);
|
||||||
|
std::vector<uint8_t> data_bytes(data.size());
|
||||||
|
const auto* bytes = data.data();
|
||||||
|
|
||||||
|
service_uuids_.push_back(uuid_string);
|
||||||
|
for (size_t i = 0; i < data.size(); i++) {
|
||||||
|
data_bytes[i] = bytes[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
service_data_.insert({uuid_string, std::move(data_bytes)});
|
||||||
|
}
|
||||||
|
|
||||||
|
registerAdaptor();
|
||||||
|
|
||||||
|
NEARBY_VLOG(1) << __func__
|
||||||
|
<< ": Created a org.bluez.LEAdvertisement1 instance at "
|
||||||
|
<< getObjectPath();
|
||||||
|
}
|
||||||
|
} // namespace linux
|
||||||
|
} // namespace nearby
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#ifndef PLATFORM_IMPL_LINUX_API_BLUEZ_BLE_ADVERTISEMENT_H_
|
||||||
|
#define PLATFORM_IMPL_LINUX_API_BLUEZ_BLE_ADVERTISEMENT_H_
|
||||||
|
|
||||||
|
#include <sdbus-c++/AdaptorInterfaces.h>
|
||||||
|
#include <sdbus-c++/IConnection.h>
|
||||||
|
#include <sdbus-c++/ProxyInterfaces.h>
|
||||||
|
#include <sdbus-c++/StandardInterfaces.h>
|
||||||
|
#include <sdbus-c++/Types.h>
|
||||||
|
|
||||||
|
#include "internal/platform/implementation/ble_v2.h"
|
||||||
|
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||||
|
#include "internal/platform/implementation/linux/bluez.h"
|
||||||
|
#include "internal/platform/implementation/linux/generated/dbus/bluez/le_advertisement_manager_client.h"
|
||||||
|
#include "internal/platform/implementation/linux/generated/dbus/bluez/le_advertisement_server.h"
|
||||||
|
#include "internal/platform/logging.h"
|
||||||
|
|
||||||
|
namespace nearby {
|
||||||
|
namespace linux {
|
||||||
|
class LEAdvertisement final
|
||||||
|
: public sdbus::AdaptorInterfaces<org::bluez::LEAdvertisement1_adaptor, sdbus::ObjectManager_adaptor>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LEAdvertisement(const LEAdvertisement &) = delete;
|
||||||
|
LEAdvertisement(LEAdvertisement &&) = delete;
|
||||||
|
LEAdvertisement &operator=(const LEAdvertisement &) = delete;
|
||||||
|
LEAdvertisement &operator=(LEAdvertisement &&) = delete;
|
||||||
|
|
||||||
|
LEAdvertisement(sdbus::IConnection &system_bus, sdbus::ObjectPath path,
|
||||||
|
const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters);
|
||||||
|
|
||||||
|
static std::unique_ptr<LEAdvertisement>
|
||||||
|
CreateLEAdvertisement(sdbus::IConnection &system_bus, const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||||
|
api::ble_v2::AdvertiseParameters advertising_parameters)
|
||||||
|
{
|
||||||
|
static std::atomic<size_t> adv_count = 0;
|
||||||
|
auto object_path = bluez::ble_advertisement_path(adv_count++);
|
||||||
|
return std::make_unique<LEAdvertisement>(system_bus, object_path, advertising_data, advertising_parameters);
|
||||||
|
}
|
||||||
|
~LEAdvertisement() { unregisterAdaptor(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
// Methods
|
||||||
|
void Release() override { NEARBY_LOGS(INFO) << __func__ << ": LE Advertisement released: " << getObjectPath(); }
|
||||||
|
|
||||||
|
// Properties
|
||||||
|
std::string Type() override { return "peripheral"; }
|
||||||
|
std::vector<std::string> ServiceUUIDs() override { return service_uuids_; }
|
||||||
|
std::map<std::string, sdbus::Variant> ManufacturerData() override { return {}; }
|
||||||
|
std::vector<std::string> SolicitUUIDs() override { return {}; }
|
||||||
|
std::map<std::string, sdbus::Variant> ServiceData() override { return service_data_; }
|
||||||
|
std::vector<std::string> Includes() override { return {}; }
|
||||||
|
std::string LocalName() override { return {}; }
|
||||||
|
uint16_t Duration() override { return 0; }
|
||||||
|
uint16_t Timeout() override { return 0; }
|
||||||
|
// Windows seems to hardcode the scan interval to 118.125 milliseconds, so
|
||||||
|
// lets just replicate that.
|
||||||
|
uint32_t MinInterval() override { return 118; }
|
||||||
|
uint32_t MaxInterval() override { return 119; }
|
||||||
|
int16_t TxPower() override { return bluez::TxPowerLevelDbm(advertise_set_parameters_.tx_power_level); };
|
||||||
|
|
||||||
|
bool is_extended_advertisement_;
|
||||||
|
std::vector<std::string> service_uuids_;
|
||||||
|
std::map<std::string, sdbus::Variant> service_data_;
|
||||||
|
api::ble_v2::AdvertiseParameters advertise_set_parameters_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class LEAdvertisementManager final : public sdbus::ProxyInterfaces<org::bluez::LEAdvertisingManager1_proxy>
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LEAdvertisementManager(sdbus::IConnection &system_bus, BluetoothAdapter &adapter) :
|
||||||
|
ProxyInterfaces(system_bus, "org.bluez", adapter.GetObjectPath())
|
||||||
|
{
|
||||||
|
registerProxy();
|
||||||
|
}
|
||||||
|
~LEAdvertisementManager() { unregisterProxy(); }
|
||||||
|
|
||||||
|
LEAdvertisementManager(const LEAdvertisementManager &) = delete;
|
||||||
|
LEAdvertisementManager(LEAdvertisementManager &&) = delete;
|
||||||
|
LEAdvertisementManager &operator=(const LEAdvertisementManager &) = delete;
|
||||||
|
LEAdvertisementManager &operator=(LEAdvertisementManager &&) = delete;
|
||||||
|
};
|
||||||
|
} // namespace linux
|
||||||
|
} // namespace nearby
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -63,7 +63,7 @@ int16_t TxPowerLevelDbm(api::ble_v2::TxPowerLevel level);
|
|||||||
class BluezObjectManager
|
class BluezObjectManager
|
||||||
: public sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
: public sdbus::ProxyInterfaces<sdbus::ObjectManager_proxy> {
|
||||||
public:
|
public:
|
||||||
explicit BluezObjectManager(sdbus::IConnection &system_bus)
|
explicit BluezObjectManager(sdbus::IConnection& system_bus)
|
||||||
: ProxyInterfaces(system_bus, "org.bluez", "/") {
|
: ProxyInterfaces(system_bus, "org.bluez", "/") {
|
||||||
registerProxy();
|
registerProxy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include "internal/platform/implementation/linux/bluez_profiles.h"
|
||||||
|
|
||||||
|
#include "internal/platform/logging.h"
|
||||||
|
|
||||||
|
namespace nearby{
|
||||||
|
namespace linux{
|
||||||
|
namespace bluez{
|
||||||
|
void BluezProfile::NewConnection(const sdbus::ObjectPath& device, const sdbus::UnixFd& fd, const std::map<std::string, sdbus::Variant>& fd_properties)
|
||||||
|
{
|
||||||
|
NEARBY_VLOG(1) << __func__ << ": New connection to device " << device;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#include <sdbus-c++/ProxyInterfaces.h>
|
||||||
|
|
||||||
|
#include "internal/platform/implementation/linux/bluez.h"
|
||||||
|
#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_manager_client.h"
|
||||||
|
#include "internal/platform/implementation/linux/generated/dbus/bluez/profile_server.h"
|
||||||
|
|
||||||
|
namespace nearby {
|
||||||
|
class CancellationFlag;
|
||||||
|
namespace linux {
|
||||||
|
namespace bluez {
|
||||||
|
class BluezProfile : public sdbus::AdaptorInterfaces<org::bluez::Profile1_adaptor>
|
||||||
|
{
|
||||||
|
BluezProfile(const BluezProfile &) = delete;
|
||||||
|
BluezProfile(BluezProfile &&) = delete;
|
||||||
|
BluezProfile &operator=(const BluezProfile &) = delete;
|
||||||
|
BluezProfile &operator=(BluezProfile &&) = delete;
|
||||||
|
BluezProfile(sdbus::IConnection &system_bus, sdbus::ObjectPath profile_object_path) :
|
||||||
|
AdaptorInterfaces(system_bus, std::move(profile_object_path))
|
||||||
|
{
|
||||||
|
registerAdaptor();
|
||||||
|
}
|
||||||
|
~BluezProfile() { unregisterAdaptor(); }
|
||||||
|
|
||||||
|
void NewConnection(const sdbus::ObjectPath &device, const sdbus::UnixFd &fd,
|
||||||
|
const std::map<std::string, sdbus::Variant> &fd_properties) override;
|
||||||
|
|
||||||
|
};
|
||||||
|
class BluezProfileManager : public sdbus::ProxyInterfaces<org::bluez::ProfileManager1_proxy>
|
||||||
|
{
|
||||||
|
|
||||||
|
BluezProfileManager(const BluezProfileManager &) = delete;
|
||||||
|
BluezProfileManager(BluezProfileManager &&) = delete;
|
||||||
|
BluezProfileManager &operator=(const BluezProfileManager &) = delete;
|
||||||
|
BluezProfileManager &operator=(BluezProfileManager &&) = delete;
|
||||||
|
BluezProfileManager(sdbus::IConnection &system_bus) : ProxyInterfaces(system_bus, bluez::SERVICE_DEST, "/org/bluez")
|
||||||
|
{
|
||||||
|
registerProxy();
|
||||||
|
}
|
||||||
|
~BluezProfileManager() { unregisterProxy(); }
|
||||||
|
|
||||||
|
public:
|
||||||
|
bool ProfileRegistered(absl::string_view service_uuid);
|
||||||
|
bool Register(std::optional<absl::string_view> service_name, absl::string_view service_uuid);
|
||||||
|
bool Register(absl::string_view service_uuid) { return Register(std::nullopt, service_uuid); }
|
||||||
|
void Unregister(absl::string_view service_uuid);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::map<std::string, std::shared_ptr<BluezProfile>> registered_services_;
|
||||||
|
};
|
||||||
|
} // namespace bluez
|
||||||
|
} // namespace linux
|
||||||
|
} // namespace nearby
|
||||||
@@ -31,12 +31,12 @@
|
|||||||
#include "internal/platform/implementation/linux/atomic_uint32.h"
|
#include "internal/platform/implementation/linux/atomic_uint32.h"
|
||||||
// #include "internal/platform/implementation/linux/ble_medium.h"
|
// #include "internal/platform/implementation/linux/ble_medium.h"
|
||||||
// #include "internal/platform/implementation/linux/ble_v2_medium.h"
|
// #include "internal/platform/implementation/linux/ble_v2_medium.h"
|
||||||
// #include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||||
// #include "internal/platform/implementation/linux/bluetooth_classic_medium.h"
|
// #include "internal/platform/implementation/linux/bluetooth_classic_medium.h"
|
||||||
// #include "internal/platform/implementation/linux/bluez.h"
|
#include "internal/platform/implementation/linux/bluez.h"
|
||||||
#include "internal/platform/implementation/linux/condition_variable.h"
|
#include "internal/platform/implementation/linux/condition_variable.h"
|
||||||
#include "internal/platform/implementation/linux/dbus.h"
|
#include "internal/platform/implementation/linux/dbus.h"
|
||||||
// #include "internal/platform/implementation/linux/generated/dbus/bluez/adapter_client.h"
|
#include "internal/platform/implementation/linux/generated/dbus/bluez/adapter_client.h"
|
||||||
#include "internal/platform/implementation/linux/mutex.h"
|
#include "internal/platform/implementation/linux/mutex.h"
|
||||||
#include "internal/platform/implementation/linux/preferences_manager.h"
|
#include "internal/platform/implementation/linux/preferences_manager.h"
|
||||||
#include "internal/platform/implementation/linux/submittable_executor.h"
|
#include "internal/platform/implementation/linux/submittable_executor.h"
|
||||||
@@ -50,10 +50,7 @@
|
|||||||
#include "internal/platform/implementation/shared/file.h"
|
#include "internal/platform/implementation/shared/file.h"
|
||||||
#include "internal/platform/implementation/submittable_executor.h"
|
#include "internal/platform/implementation/submittable_executor.h"
|
||||||
// #include "internal/platform/implementation/wifi_hotspot.h"
|
// #include "internal/platform/implementation/wifi_hotspot.h"
|
||||||
#include "ble_medium.h"
|
#include "internal/platform/implementation/linux/ble_v2.h"
|
||||||
#include "ble_v2_medium.h"
|
|
||||||
#include "bluetooth_adapter.h"
|
|
||||||
#include "bluez.h"
|
|
||||||
#include "internal/platform/implementation/wifi_lan.h"
|
#include "internal/platform/implementation/wifi_lan.h"
|
||||||
#include "internal/platform/payload_id.h"
|
#include "internal/platform/payload_id.h"
|
||||||
#include "log_message.h"
|
#include "log_message.h"
|
||||||
@@ -171,15 +168,14 @@ ImplementationPlatform::CreateScheduledExecutor() {
|
|||||||
|
|
||||||
std::unique_ptr<api::BluetoothAdapter>
|
std::unique_ptr<api::BluetoothAdapter>
|
||||||
ImplementationPlatform::CreateBluetoothAdapter() {
|
ImplementationPlatform::CreateBluetoothAdapter() {
|
||||||
auto manager =
|
auto system_bus = linux::getSystemBusConnection();
|
||||||
linux::bluez::BluezObjectManager(*linux::getSystemBusConnection());
|
auto manager = linux::bluez::BluezObjectManager(*system_bus);
|
||||||
try {
|
try {
|
||||||
auto interfaces = manager.GetManagedObjects();
|
auto interfaces = manager.GetManagedObjects();
|
||||||
for (auto &[object, properties] : interfaces) {
|
for (auto &[object, properties] : interfaces) {
|
||||||
if (properties.count(org::bluez::Adapter1_proxy::INTERFACE_NAME) == 1) {
|
if (properties.count(org::bluez::Adapter1_proxy::INTERFACE_NAME) == 1) {
|
||||||
NEARBY_LOGS(INFO) << __func__ << ": found bluetooth adapter " << object;
|
NEARBY_LOGS(INFO) << __func__ << ": found bluetooth adapter " << object;
|
||||||
return std::make_unique<linux::BluetoothAdapter>(
|
return std::make_unique<linux::BluetoothAdapter>(system_bus, object);
|
||||||
linux::getSystemBusConnection(), object);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (const sdbus::Error &e) {
|
} catch (const sdbus::Error &e) {
|
||||||
@@ -189,7 +185,7 @@ ImplementationPlatform::CreateBluetoothAdapter() {
|
|||||||
|
|
||||||
NEARBY_LOGS(ERROR) << __func__
|
NEARBY_LOGS(ERROR) << __func__
|
||||||
<< ": couldn't find a bluetooth adapter on this system";
|
<< ": couldn't find a bluetooth adapter on this system";
|
||||||
return nullptr; //TODO: implement bluetooth adapter
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<api::BluetoothClassicMedium>
|
std::unique_ptr<api::BluetoothClassicMedium>
|
||||||
@@ -209,7 +205,7 @@ std::unique_ptr<BleMedium> ImplementationPlatform::CreateBleMedium(
|
|||||||
//
|
//
|
||||||
std::unique_ptr<api::ble_v2::BleMedium>
|
std::unique_ptr<api::ble_v2::BleMedium>
|
||||||
ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter &adapter) {
|
ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter &adapter) {
|
||||||
return std::make_unique<linux::BleV2Medium>(adapter);
|
return std::make_unique<nearby::linux::BleV2Medium>(adapter);
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|||||||
Reference in New Issue
Block a user