mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Enabled BLE advertisements with extended + GATT server.
This commit is contained in:
@@ -56,10 +56,10 @@ cc_library(
|
||||
hdrs = [
|
||||
"avahi.h",
|
||||
"ble_gatt_server.h",
|
||||
"ble_gatt_client.h",
|
||||
# "ble_gatt_client.h",
|
||||
# "ble_medium.h",
|
||||
# "ble_v2_medium.h",
|
||||
# "ble_v2_server_socket.h",
|
||||
"ble_v2_medium.h",
|
||||
"ble_v2_server_socket.h",
|
||||
"bluetooth_adapter.h",
|
||||
"bluetooth_bluez_profile.h",
|
||||
"bluetooth_classic_device.h",
|
||||
@@ -71,14 +71,14 @@ cc_library(
|
||||
"bluez.h",
|
||||
"bluez_device.h",
|
||||
# "bluez_agent.h",
|
||||
# "bluez_advertisement_monitor.h",
|
||||
# "bluez_advertisement_monitor_manager.h",
|
||||
"bluez_advertisement_monitor.h",
|
||||
"bluez_advertisement_monitor_manager.h",
|
||||
# "bluez_gatt_characteristic_client.h",
|
||||
# "bluez_gatt_characteristic_server.h",
|
||||
# "bluez_gatt_manager.h",
|
||||
"bluez_gatt_characteristic_server.h",
|
||||
"bluez_gatt_manager.h",
|
||||
# "bluez_gatt_service_client.h",
|
||||
# "bluez_gatt_service_server.h",
|
||||
# "bluez_le_advertisement.h",
|
||||
"bluez_gatt_service_server.h",
|
||||
"bluez_le_advertisement.h",
|
||||
"dbus.h",
|
||||
"network_manager.h",
|
||||
"network_manager_active_connection.h",
|
||||
@@ -98,6 +98,7 @@ cc_library(
|
||||
"wifi_socket.h",
|
||||
],
|
||||
deps = [
|
||||
":crypto",
|
||||
"//internal/platform:base",
|
||||
"//internal/platform:comm",
|
||||
"//internal/platform:types",
|
||||
@@ -127,7 +128,7 @@ cc_library(
|
||||
srcs = [
|
||||
"crypto.cc",
|
||||
],
|
||||
visibility = ["//visibility:private"],
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
"//internal/platform:base",
|
||||
"//internal/platform/implementation:types",
|
||||
@@ -140,8 +141,9 @@ cc_library(
|
||||
srcs = [
|
||||
"avahi.cc",
|
||||
# "ble_gatt_client.cc",
|
||||
# "ble_gatt_server.cc",
|
||||
# "ble_v2_medium.cc",
|
||||
"ble_gatt_server.cc",
|
||||
# "ble_medium.cc",
|
||||
"ble_v2_medium.cc",
|
||||
"bluetooth_adapter.cc",
|
||||
"bluetooth_bluez_profile.cc",
|
||||
"bluetooth_classic_socket.cc",
|
||||
@@ -152,11 +154,11 @@ cc_library(
|
||||
"bluetooth_pairing.cc",
|
||||
"bluez.cc",
|
||||
#"bluez_agent.cc",
|
||||
# "bluez_advertisement_monitor.cc",
|
||||
"bluez_advertisement_monitor.cc",
|
||||
# "bluez_gatt_characteristic_client.cc",
|
||||
# "bluez_gatt_characteristic_server.cc",
|
||||
# "bluez_gatt_service_server.cc",
|
||||
# "bluez_le_advertisement.cc",
|
||||
"bluez_gatt_characteristic_server.cc",
|
||||
"bluez_gatt_service_server.cc",
|
||||
"bluez_le_advertisement.cc",
|
||||
"dbus.cc",
|
||||
"executor.cc",
|
||||
"network_manager.cc",
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#define PLATFORM_IMPL_LINUX_API_BLE_GATT_SERVER_H_
|
||||
|
||||
#include <memory>
|
||||
#include <regex>
|
||||
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
|
||||
@@ -35,7 +36,9 @@ namespace linux {
|
||||
class LocalBlePeripheral : public api::ble_v2::BlePeripheral {
|
||||
public:
|
||||
explicit LocalBlePeripheral(BluetoothAdapter& adapter) : adapter_(adapter) {
|
||||
unique_id_ = BluetoothUtils::ToNumber(adapter_.GetMacAddress());
|
||||
// temp fix till everything transitions to GGetAddress()
|
||||
unique_id_ = std::stoull(std::regex_replace(adapter_.GetMacAddress(),
|
||||
std::regex("[:\\-]"), ""), nullptr, 16);
|
||||
}
|
||||
|
||||
std::string GetAddress() const override { return adapter_.GetMacAddress(); }
|
||||
@@ -64,9 +67,6 @@ class GattServer : public api::ble_v2::GattServer {
|
||||
std::move(server_cb))) {}
|
||||
~GattServer() override = default;
|
||||
|
||||
api::ble_v2::BlePeripheral& GetBlePeripheral() override {
|
||||
return local_peripheral_;
|
||||
}
|
||||
absl::optional<api::ble_v2::GattCharacteristic> CreateCharacteristic(
|
||||
const Uuid& service_uuid, const Uuid& characteristic_uuid,
|
||||
api::ble_v2::GattCharacteristic::Permission permission,
|
||||
|
||||
@@ -20,9 +20,11 @@
|
||||
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/linux/ble_gatt_client.h"
|
||||
#include "internal/platform/implementation/linux/ble_gatt_server.h"
|
||||
// #include "internal/platform/implementation/linux/ble_gatt_client.h"
|
||||
// #include "internal/platform/implementation/linux/ble_gatt_server.h"
|
||||
#include "internal/platform/implementation/linux/ble_v2_medium.h"
|
||||
|
||||
#include "ble_gatt_server.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_classic_device.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_devices.h"
|
||||
#include "internal/platform/implementation/linux/bluez.h"
|
||||
@@ -40,7 +42,7 @@ BleV2Medium::BleV2Medium(BluetoothAdapter &adapter)
|
||||
adapter_(adapter),
|
||||
devices_(std::make_unique<BluetoothDevices>(
|
||||
system_bus_, adapter_.GetObjectPath(), observers_)),
|
||||
gatt_discovery_(std::make_shared<BluezGattDiscovery>(system_bus_)),
|
||||
// gatt_discovery_(std::make_shared<BluezGattDiscovery>(system_bus_)),
|
||||
root_object_manager_(std::make_unique<RootObjectManager>(*system_bus_)),
|
||||
adv_monitor_manager_(
|
||||
bluez::AdvertisementMonitorManager::
|
||||
@@ -59,12 +61,15 @@ BleV2Medium::BleV2Medium(BluetoothAdapter &adapter)
|
||||
DBUS_LOG_METHOD_CALL_ERROR(adv_monitor_manager_, "RegisterMonitor", e);
|
||||
}
|
||||
}
|
||||
if (gatt_discovery_->InitializeKnownServices()) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Could not initialize known GATT services";
|
||||
}
|
||||
// if (gatt_discovery_->InitializeKnownServices()) {
|
||||
// LOG(ERROR) << __func__
|
||||
// << ": Could not initialize known GATT services";
|
||||
// }
|
||||
}
|
||||
|
||||
// sync api
|
||||
// called twice. Once with extended regular advertisement ( when IsExtendedAdvertisementsAvailable() == true )
|
||||
// and another for GATT-backed header advertisement for legacy devices
|
||||
bool BleV2Medium::StartAdvertising(
|
||||
const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||
api::ble_v2::AdvertiseParameters advertise_set_parameters) {
|
||||
@@ -80,24 +85,20 @@ bool BleV2Medium::StartAdvertising(
|
||||
return false;
|
||||
}
|
||||
|
||||
absl::MutexLock lock(&cur_adv_mutex_);
|
||||
if (cur_adv_ != nullptr) {
|
||||
LOG(ERROR) << __func__
|
||||
<< "Advertising is already enabled for this medium.";
|
||||
return false;
|
||||
}
|
||||
absl::MutexLock l (&advs_mutex_);
|
||||
advs_.push_front(bluez::LEAdvertisement::CreateLEAdvertisement(
|
||||
*system_bus_, advertising_data, advertise_set_parameters));
|
||||
auto it = advs_.begin();
|
||||
|
||||
cur_adv_ = bluez::LEAdvertisement::CreateLEAdvertisement(
|
||||
*system_bus_, advertising_data, advertise_set_parameters);
|
||||
|
||||
LOG(INFO) << __func__ << ": Registering advertisement "
|
||||
<< cur_adv_->getObjectPath() << " on bluetooth adapter "
|
||||
LOG(INFO) << __func__ << ": Registering advertisement, is_extended: " << advertising_data.is_extended_advertisement
|
||||
<< " " << (*it) -> getObjectPath() << " on bluetooth adapter "
|
||||
<< adapter_.GetObjectPath();
|
||||
|
||||
try {
|
||||
adv_manager_->RegisterAdvertisement(cur_adv_->getObjectPath(), {});
|
||||
adv_manager_->RegisterAdvertisement((*it)->getObjectPath(), {});
|
||||
} catch (const sdbus::Error &e) {
|
||||
cur_adv_ = nullptr;
|
||||
advs_.erase(it);
|
||||
DBUS_LOG_METHOD_CALL_ERROR(adv_manager_, "RegisterAdvertisement", e);
|
||||
return false;
|
||||
}
|
||||
@@ -106,25 +107,23 @@ bool BleV2Medium::StartAdvertising(
|
||||
}
|
||||
|
||||
bool BleV2Medium::StopAdvertising() {
|
||||
absl::MutexLock lock(&cur_adv_mutex_);
|
||||
if (cur_adv_ == nullptr) {
|
||||
LOG(ERROR) << __func__ << ": Advertising is not enabled.";
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << __func__ << "Unregistering advertisement object "
|
||||
<< cur_adv_->getObjectPath();
|
||||
|
||||
absl::MutexLock l(&advs_mutex_);
|
||||
try {
|
||||
adv_manager_->UnregisterAdvertisement(cur_adv_->getObjectPath());
|
||||
for (auto& adv: advs_)
|
||||
{
|
||||
adv_manager_->UnregisterAdvertisement(adv->getObjectPath());
|
||||
}
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(adv_manager_, "UnregisterAdvertisement", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
cur_adv_ = nullptr;
|
||||
advs_.clear();
|
||||
return true;
|
||||
}
|
||||
|
||||
//async api
|
||||
// this doesn't run. wonder why
|
||||
std::unique_ptr<api::ble_v2::BleMedium::AdvertisingSession>
|
||||
BleV2Medium::StartAdvertising(
|
||||
const api::ble_v2::BleAdvertisementData &advertising_data,
|
||||
@@ -201,26 +200,42 @@ BleV2Medium::StartAdvertising(
|
||||
advs_.erase(adv_it);
|
||||
return absl::OkStatus();
|
||||
};
|
||||
|
||||
return std::make_unique<api::ble_v2::BleMedium::AdvertisingSession>(
|
||||
api::ble_v2::BleMedium::AdvertisingSession{std::move(stop_adv)});
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattServer> BleV2Medium::StartGattServer(
|
||||
api::ble_v2::ServerGattConnectionCallback callback) {
|
||||
return std::make_unique<GattServer>(*system_bus_, adapter_, devices_,
|
||||
std::move(callback));
|
||||
(void)callback;
|
||||
|
||||
return std::make_unique<GattServer>(
|
||||
*system_bus_, adapter_, devices_,std::move(callback)
|
||||
);
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> BleV2Medium::ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral &peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) {
|
||||
auto path = bluez::device_object_path(adapter_.GetObjectPath(),
|
||||
peripheral.GetAddress());
|
||||
(void)peripheral_id;
|
||||
(void)tx_power_level;
|
||||
(void)callback;
|
||||
LOG(WARNING) << __func__
|
||||
<< ": GATT client connection is not supported on Linux yet.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return std::make_unique<GattClient>(system_bus_, path, gatt_discovery_,
|
||||
std::move(callback.disconnected_cb));
|
||||
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::UniqueId peripheral_id,
|
||||
CancellationFlag *cancellation_flag) {
|
||||
(void)service_id;
|
||||
(void)tx_power_level;
|
||||
(void)peripheral_id;
|
||||
(void)cancellation_flag;
|
||||
LOG(WARNING) << __func__
|
||||
<< ": BLE socket connection is not supported on Linux yet.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool BleV2Medium::IsExtendedAdvertisementsAvailable() {
|
||||
@@ -305,9 +320,8 @@ bool BleV2Medium::StartScanning(const Uuid &service_uuid,
|
||||
<< "' and message '" << e.getMessage() << "'";
|
||||
return false;
|
||||
}
|
||||
|
||||
auto device_watcher = std::make_unique<DeviceWatcher>(
|
||||
*system_bus_, adapter_.GetObjectPath(), devices_);
|
||||
*system_bus_, adapter_.GetObjectPath(), adapter_, devices_);
|
||||
if (!StartLEDiscovery()) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Could not start LE discovery on adapter "
|
||||
@@ -325,6 +339,7 @@ bool BleV2Medium::StartScanning(const Uuid &service_uuid,
|
||||
}
|
||||
return false;
|
||||
}
|
||||
LOG(INFO) << __func__ << " :Started monitoring for service UUID: " << std::string(service_uuid);
|
||||
|
||||
active_adv_monitors_[service_uuid] =
|
||||
std::make_pair(std::move(monitor), std::move(device_watcher));
|
||||
@@ -348,7 +363,7 @@ bool BleV2Medium::StopScanning() {
|
||||
LOG(INFO) << __func__ << ": Stopping discovery for adapter "
|
||||
<< adapter.getObjectPath();
|
||||
try {
|
||||
adapter.StopDiscovery();
|
||||
adapter.StopDiscovery(); // this will stop bluetooth classic discovery as well. do we want this?
|
||||
} catch (const sdbus::Error &e) {
|
||||
DBUS_LOG_METHOD_CALL_ERROR(&adapter, "StopDiscovery", e);
|
||||
}
|
||||
@@ -403,7 +418,7 @@ BleV2Medium::StartScanning(const Uuid &service_uuid,
|
||||
}
|
||||
|
||||
auto device_watcher = std::make_unique<DeviceWatcher>(
|
||||
*system_bus_, adapter_.GetObjectPath(), devices_);
|
||||
*system_bus_, adapter_.GetObjectPath(),adapter_, devices_);
|
||||
if (!StartLEDiscovery()) {
|
||||
LOG(ERROR) << __func__
|
||||
<< ": Could not start LE discovery on adapter "
|
||||
@@ -461,20 +476,69 @@ BleV2Medium::StartScanning(const Uuid &service_uuid,
|
||||
}});
|
||||
}
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(const std::string &mac_address,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
auto device = devices_->get_device_by_address(mac_address);
|
||||
if (device == nullptr) return false;
|
||||
callback(*device);
|
||||
std::unique_ptr<api::ble_v2::BleServerSocket> BleV2Medium::OpenServerSocket(
|
||||
const std::string &service_id) {
|
||||
LOG(INFO) << __func__ << ": Opening BLE server socket for service "
|
||||
<< service_id;
|
||||
return std::make_unique<BleV2ServerSocket>();
|
||||
}
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleL2capServerSocket>
|
||||
BleV2Medium::OpenL2capServerSocket(const std::string &service_id) {
|
||||
LOG(WARNING) << __func__ << ": L2CAP server sockets not implemented on Linux";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// LOG(WARNING) << __func__ << ": BLE socket connections not implemented on Linux";
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleL2capSocket> BleV2Medium::ConnectOverL2cap(
|
||||
int psm, const std::string &service_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
CancellationFlag *cancellation_flag) {
|
||||
LOG(WARNING) << __func__ << ": L2CAP socket connections not implemented on Linux";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool BleV2Medium::StartMultipleServicesScanning(
|
||||
const std::vector<Uuid> &service_uuids,
|
||||
api::ble_v2::TxPowerLevel tx_power_level, ScanCallback callback) {
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Multiple services scanning not implemented on Linux. "
|
||||
<< "Use single service scanning instead.";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool BleV2Medium::PauseMediumScanning() {
|
||||
LOG(INFO) << __func__ << ": Pause scanning not implemented, returning success";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool BleV2Medium::GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) {
|
||||
auto device = devices_->get_device_by_unique_id(id);
|
||||
if (device == nullptr) return false;
|
||||
callback(*device);
|
||||
bool BleV2Medium::ResumeMediumScanning() {
|
||||
LOG(INFO) << __func__ << ": Resume scanning not implemented, returning success";
|
||||
return true;
|
||||
}
|
||||
|
||||
void BleV2Medium::AddAlternateUuidForService(uint16_t uuid,
|
||||
const std::string &service_id) {
|
||||
LOG(INFO) << __func__ << ": Alternate UUID mapping not implemented. UUID: "
|
||||
<< uuid << ", service_id: " << service_id;
|
||||
}
|
||||
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId>
|
||||
BleV2Medium::RetrieveBlePeripheralIdFromNativeId(
|
||||
const std::string &ble_peripheral_native_id) {
|
||||
LOG(WARNING) << __func__
|
||||
<< ": Retrieval from native ID not implemented. Native ID: "
|
||||
<< ble_peripheral_native_id;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
} // namespace linux
|
||||
} // namespace nearby
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
#define PLATFORM_IMPL_LINUX_API_BLE_V2_MEDIUM_H_
|
||||
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <vector>
|
||||
|
||||
#include <sdbus-c++/IConnection.h>
|
||||
|
||||
@@ -23,7 +25,7 @@
|
||||
#include "absl/container/flat_hash_map.h"
|
||||
#include "absl/synchronization/mutex.h"
|
||||
#include "internal/platform/implementation/ble_v2.h"
|
||||
#include "internal/platform/implementation/linux/ble_gatt_client.h"
|
||||
// #include "internal/platform/implementation/linux/ble_gatt_client.h"
|
||||
#include "internal/platform/implementation/linux/ble_v2_server_socket.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_adapter.h"
|
||||
#include "internal/platform/implementation/linux/bluetooth_devices.h"
|
||||
@@ -69,26 +71,48 @@ class BleV2Medium final : public api::ble_v2::BleMedium {
|
||||
api::ble_v2::ServerGattConnectionCallback callback) override;
|
||||
|
||||
std::unique_ptr<api::ble_v2::GattClient> ConnectToGattServer(
|
||||
api::ble_v2::BlePeripheral &peripheral,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::ClientGattConnectionCallback callback) override;
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleServerSocket> OpenServerSocket(
|
||||
const std::string &service_id) override {
|
||||
return std::make_unique<BleV2ServerSocket>();
|
||||
}
|
||||
const std::string &service_id) override;
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleL2capServerSocket> OpenL2capServerSocket(
|
||||
const std::string &service_id) override;
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleSocket> Connect(
|
||||
const std::string &service_id, api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral &peripheral,
|
||||
CancellationFlag *cancellation_flag) override {
|
||||
return nullptr;
|
||||
}
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleL2capSocket> ConnectOverL2cap(
|
||||
int psm, const std::string &service_id,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
api::ble_v2::BlePeripheral::UniqueId peripheral_id,
|
||||
CancellationFlag *cancellation_flag) override;
|
||||
|
||||
bool StartMultipleServicesScanning(const std::vector<Uuid> &service_uuids,
|
||||
api::ble_v2::TxPowerLevel tx_power_level,
|
||||
ScanCallback callback) override;
|
||||
|
||||
bool PauseMediumScanning() override;
|
||||
|
||||
bool ResumeMediumScanning() override;
|
||||
|
||||
bool IsExtendedAdvertisementsAvailable() override;
|
||||
bool GetRemotePeripheral(const std::string &mac_address,
|
||||
GetRemotePeripheralCallback callback) override;
|
||||
bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
GetRemotePeripheralCallback callback) override;
|
||||
|
||||
void AddAlternateUuidForService(uint16_t uuid,
|
||||
const std::string &service_id) override;
|
||||
|
||||
std::optional<api::ble_v2::BlePeripheral::UniqueId>
|
||||
RetrieveBlePeripheralIdFromNativeId(
|
||||
const std::string &ble_peripheral_native_id) override;
|
||||
|
||||
// bool GetRemotePeripheral(const std::string &mac_address,
|
||||
// GetRemotePeripheralCallback callback) override;
|
||||
// bool GetRemotePeripheral(api::ble_v2::BlePeripheral::UniqueId id,
|
||||
// GetRemotePeripheralCallback callback) override;
|
||||
|
||||
private:
|
||||
bool StartLEDiscovery();
|
||||
@@ -116,7 +140,7 @@ class BleV2Medium final : public api::ble_v2::BleMedium {
|
||||
BluetoothAdapter adapter_;
|
||||
ObserverList<api::BluetoothClassicMedium::Observer> observers_ = {};
|
||||
std::shared_ptr<BluetoothDevices> devices_;
|
||||
std::shared_ptr<BluezGattDiscovery> gatt_discovery_;
|
||||
// std::shared_ptr<BluezGattDiscovery> gatt_discovery_;
|
||||
|
||||
std::unique_ptr<RootObjectManager> root_object_manager_;
|
||||
std::unique_ptr<bluez::AdvertisementMonitorManager> adv_monitor_manager_;
|
||||
|
||||
@@ -39,7 +39,6 @@ namespace nearby {
|
||||
namespace linux {
|
||||
// https://developer.android.com/reference/android/bluetooth/BluetoothDevice.html.
|
||||
|
||||
// TODO: This used to inherit from ble_v2::BlePeripheral. Removed that since APIs have now changed
|
||||
class BluetoothDevice : public api::BluetoothDevice {
|
||||
public:
|
||||
using UniqueId = std::uint64_t;
|
||||
|
||||
@@ -68,9 +68,9 @@ int16_t TxPowerLevelDbm(api::ble_v2::TxPowerLevel level) {
|
||||
case api::ble_v2::TxPowerLevel::kLow:
|
||||
return 0;
|
||||
case api::ble_v2::TxPowerLevel::kMedium:
|
||||
return 3;
|
||||
return 5;
|
||||
case api::ble_v2::TxPowerLevel::kHigh:
|
||||
return 6;
|
||||
return 10; // Increased from 6 to 10 dBm (maximum for most adapters)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ void AdvertisementMonitor::DeviceFound(const sdbus::ObjectPath &device) {
|
||||
adv_data.service_data.emplace(*uuid,
|
||||
std::string(bytes.begin(), bytes.end()));
|
||||
}
|
||||
scan_callback_.advertisement_found_cb(*peripheral, adv_data);
|
||||
// scan_callback_.advertisement_found_cb(*peripheral, adv_data);
|
||||
}
|
||||
|
||||
void AdvertisementMonitor::DeviceLost(const sdbus::ObjectPath &device) {
|
||||
|
||||
@@ -93,38 +93,39 @@ void GattCharacteristicServer::ReadValue(
|
||||
return;
|
||||
}
|
||||
auto characteristic = characteristic_;
|
||||
server_cb_->on_characteristic_read_cb(
|
||||
*device, characteristic, static_cast<int>(offset),
|
||||
[result = std::move(result),
|
||||
this](absl::StatusOr<absl::string_view> data) {
|
||||
const auto &status = data.status();
|
||||
if (status.ok()) {
|
||||
auto str = data.value();
|
||||
std::vector<uint8_t> bytes(str.size());
|
||||
for (auto i = 0; i < str.size(); i++) {
|
||||
bytes[i] = str[i];
|
||||
}
|
||||
result.returnResults(bytes);
|
||||
|
||||
absl::MutexLock lock(&cached_value_mutex_);
|
||||
cached_value_ = bytes;
|
||||
} else if (absl::IsPermissionDenied(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotPermitted",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsUnauthenticated(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotAuthorized",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsOutOfRange(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.InvalidOffset",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsUnimplemented(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotSupported",
|
||||
std::string(status.message())));
|
||||
} else {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.Failed",
|
||||
std::string(status.message())));
|
||||
}
|
||||
});
|
||||
// TODO: enable the callback
|
||||
// server_cb_->on_characteristic_read_cb(
|
||||
// *device, characteristic, static_cast<int>(offset),
|
||||
// [result = std::move(result),
|
||||
// this](absl::StatusOr<absl::string_view> data) {
|
||||
// const auto &status = data.status();
|
||||
// if (status.ok()) {
|
||||
// auto str = data.value();
|
||||
// std::vector<uint8_t> bytes(str.size());
|
||||
// for (auto i = 0; i < str.size(); i++) {
|
||||
// bytes[i] = str[i];
|
||||
// }
|
||||
// result.returnResults(bytes);
|
||||
//
|
||||
// absl::MutexLock lock(&cached_value_mutex_);
|
||||
// cached_value_ = bytes;
|
||||
// } else if (absl::IsPermissionDenied(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotPermitted",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsUnauthenticated(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotAuthorized",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsOutOfRange(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.InvalidOffset",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsUnimplemented(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotSupported",
|
||||
// std::string(status.message())));
|
||||
// } else {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.Failed",
|
||||
// std::string(status.message())));
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
void GattCharacteristicServer::WriteValue(
|
||||
@@ -144,29 +145,30 @@ void GattCharacteristicServer::WriteValue(
|
||||
std::string data(value.begin(), value.end());
|
||||
auto characteristic = characteristic_;
|
||||
|
||||
// TODO: enable the callback
|
||||
// TODO: Support writes without response.
|
||||
server_cb_->on_characteristic_write_cb(
|
||||
*device, characteristic, static_cast<int>(offset), data,
|
||||
[result = std::move(result)](absl::Status status) {
|
||||
if (status.ok()) {
|
||||
result.returnResults();
|
||||
} else if (absl::IsPermissionDenied(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotPermitted",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsUnauthenticated(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotAuthorized",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsOutOfRange(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.InvalidOffset",
|
||||
std::string(status.message())));
|
||||
} else if (absl::IsUnimplemented(status)) {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.NotSupported",
|
||||
std::string(status.message())));
|
||||
} else {
|
||||
result.returnError(sdbus::Error("org.bluez.Error.Failed",
|
||||
std::string(status.message())));
|
||||
}
|
||||
});
|
||||
// server_cb_->on_characteristic_write_cb(
|
||||
// *device, characteristic, static_cast<int>(offset), data,
|
||||
// [result = std::move(result)](absl::Status status) {
|
||||
// if (status.ok()) {
|
||||
// result.returnResults();
|
||||
// } else if (absl::IsPermissionDenied(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotPermitted",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsUnauthenticated(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotAuthorized",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsOutOfRange(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.InvalidOffset",
|
||||
// std::string(status.message())));
|
||||
// } else if (absl::IsUnimplemented(status)) {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.NotSupported",
|
||||
// std::string(status.message())));
|
||||
// } else {
|
||||
// result.returnError(sdbus::Error("org.bluez.Error.Failed",
|
||||
// std::string(status.message())));
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
void GattCharacteristicServer::StartNotify() {
|
||||
|
||||
@@ -23,6 +23,14 @@
|
||||
namespace nearby {
|
||||
namespace linux {
|
||||
namespace bluez {
|
||||
std::string BytesToHexString(const std::vector<uint8_t>& bytes) {
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setfill('0');
|
||||
for (uint8_t b : bytes) {
|
||||
oss << std::setw(2) << static_cast<int>(b);
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
LEAdvertisement::LEAdvertisement(
|
||||
sdbus::IConnection& system_bus, sdbus::ObjectPath path,
|
||||
const api::ble_v2::BleAdvertisementData& advertising_data,
|
||||
@@ -36,11 +44,15 @@ LEAdvertisement::LEAdvertisement(
|
||||
const auto* bytes = data.data();
|
||||
|
||||
service_uuids_.push_back(uuid_string);
|
||||
// service_uuids_.push_back("0000FE2C-0000-1000-8000-00805F9B34FB");
|
||||
// service_uuids_.push_back("0000FE2C-0000-1000-8000-00805F9B34FB");
|
||||
for (size_t i = 0; i < data.size(); i++) {
|
||||
data_bytes[i] = bytes[i];
|
||||
}
|
||||
|
||||
// LOG(INFO)<< __func__ << ": " << uuid_string;
|
||||
// LOG(INFO)<< __func__ << ": " << BytesToHexString(data_bytes);
|
||||
service_data_.insert({uuid_string, std::move(data_bytes)});
|
||||
// service_data_.insert({"0000FE2C-0000-1000-8000-00805F9B34FB", std::move(data_bytes)});
|
||||
}
|
||||
|
||||
registerAdaptor();
|
||||
|
||||
@@ -62,7 +62,7 @@ class LEAdvertisement final
|
||||
<< ": LE Advertisement released: " << getObjectPath();
|
||||
}
|
||||
|
||||
// Properties
|
||||
// Properties
|
||||
std::string Type() override { return "peripheral"; }
|
||||
std::vector<std::string> ServiceUUIDs() override { return service_uuids_; }
|
||||
std::map<std::string, sdbus::Variant> ManufacturerData() override {
|
||||
@@ -72,7 +72,12 @@ class LEAdvertisement final
|
||||
std::map<std::string, sdbus::Variant> ServiceData() override {
|
||||
return service_data_;
|
||||
}
|
||||
std::vector<std::string> Includes() override { return {}; }
|
||||
std::map<std::string, sdbus::Variant> ScanResponseServiceData() override {
|
||||
return {};
|
||||
}
|
||||
std::vector<std::string> Includes() override {
|
||||
return {};
|
||||
}
|
||||
std::string LocalName() override { return {}; }
|
||||
uint16_t Duration() override { return 0; }
|
||||
uint16_t Timeout() override { return 0; }
|
||||
|
||||
@@ -28,6 +28,7 @@ protected:
|
||||
object_.registerProperty("ManufacturerData").onInterface(INTERFACE_NAME).withGetter([this](){ return this->ManufacturerData(); });
|
||||
object_.registerProperty("SolicitUUIDs").onInterface(INTERFACE_NAME).withGetter([this](){ return this->SolicitUUIDs(); });
|
||||
object_.registerProperty("ServiceData").onInterface(INTERFACE_NAME).withGetter([this](){ return this->ServiceData(); });
|
||||
object_.registerProperty("ScanResponseServiceData").onInterface(INTERFACE_NAME).withGetter([this](){ return this->ScanResponseServiceData(); });
|
||||
object_.registerProperty("Includes").onInterface(INTERFACE_NAME).withGetter([this](){ return this->Includes(); });
|
||||
object_.registerProperty("LocalName").onInterface(INTERFACE_NAME).withGetter([this](){ return this->LocalName(); });
|
||||
object_.registerProperty("Duration").onInterface(INTERFACE_NAME).withGetter([this](){ return this->Duration(); });
|
||||
@@ -48,6 +49,7 @@ private:
|
||||
virtual std::map<std::string, sdbus::Variant> ManufacturerData() = 0;
|
||||
virtual std::vector<std::string> SolicitUUIDs() = 0;
|
||||
virtual std::map<std::string, sdbus::Variant> ServiceData() = 0;
|
||||
virtual std::map<std::string, sdbus::Variant> ScanResponseServiceData() = 0;
|
||||
virtual std::vector<std::string> Includes() = 0;
|
||||
virtual std::string LocalName() = 0;
|
||||
virtual uint16_t Duration() = 0;
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
// #include "internal/platform/implementation/linux/wifi_medium.h"
|
||||
#include "internal/platform/implementation/platform.h"
|
||||
|
||||
#include "ble_v2_medium.h"
|
||||
#include "absl/strings/str_cat.h"
|
||||
|
||||
#include "internal/platform/implementation/shared/count_down_latch.h"
|
||||
@@ -246,10 +247,10 @@ std::unique_ptr<BleMedium> ImplementationPlatform::CreateBleMedium(
|
||||
|
||||
std::unique_ptr<api::ble_v2::BleMedium>
|
||||
ImplementationPlatform::CreateBleV2Medium(api::BluetoothAdapter &adapter) {
|
||||
return nullptr;
|
||||
// return nullptr;
|
||||
// TODO: Enable BLEv2 once BlueZ support is added.
|
||||
// return std::make_unique<linux::BleV2Medium>(
|
||||
// dynamic_cast<linux::BluetoothAdapter &>(adapter));
|
||||
return std::make_unique<linux::BleV2Medium>(
|
||||
dynamic_cast<linux::BluetoothAdapter &>(adapter));
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
Reference in New Issue
Block a user