Implemented bluetooth socket stubs

This commit is contained in:
kidfromjupiter
2026-01-12 10:45:09 +00:00
parent 329f2ce2c9
commit f14d0ee04d
7 changed files with 68 additions and 25 deletions
@@ -60,6 +60,7 @@ cc_library(
# "ble_medium.h",
"ble_v2_medium.h",
"ble_v2_server_socket.h",
"ble_v2_socket.h",
"bluetooth_adapter.h",
"bluetooth_bluez_profile.h",
"bluetooth_classic_device.h",
@@ -145,6 +146,8 @@ cc_library(
"ble_gatt_server.cc",
# "ble_medium.cc",
"ble_v2_medium.cc",
"ble_v2_server_socket.cc",
"ble_v2_socket.cc",
"bluetooth_adapter.cc",
"bluetooth_bluez_profile.cc",
"bluetooth_classic_socket.cc",
@@ -13,8 +13,13 @@
// limitations under the License.
#include <algorithm>
#include <array>
#include <cassert>
#include <cerrno>
#include <cstring>
#include <sys/socket.h>
#include <unistd.h>
#include <sdbus-c++/IProxy.h>
#include <sdbus-c++/Types.h>
@@ -28,6 +33,8 @@
#include "internal/platform/implementation/linux/bluetooth_classic_device.h"
#include "internal/platform/implementation/linux/bluetooth_devices.h"
#include "internal/platform/implementation/linux/bluez.h"
#include "internal/platform/mac_address.h"
#include "absl/types/span.h"
#include "internal/platform/implementation/linux/bluez_advertisement_monitor.h"
#include "internal/platform/implementation/linux/bluez_advertisement_monitor_manager.h"
#include "internal/platform/implementation/linux/bluez_le_advertisement.h"
@@ -234,12 +241,8 @@ 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.";
auto device = devices_ -> get_device_by_unique_id(peripheral_id);
LOG(INFO) << __func__ << ": Resolved device with address " << device -> GetMacAddress();
return nullptr;
}
@@ -490,7 +493,7 @@ 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>();
return std::make_unique<BleV2ServerSocket>(service_id);
}
std::unique_ptr<api::ble_v2::BleL2capServerSocket>
@@ -15,27 +15,42 @@
#ifndef PLATFORM_IMPL_LINUX_API_BLE_V2_SERVER_SOCKET_H_
#define PLATFORM_IMPL_LINUX_API_BLE_V2_SERVER_SOCKET_H_
#include "absl/synchronization/notification.h"
#include <deque>
#include <memory>
#include <string>
#include "absl/synchronization/mutex.h"
#include "internal/platform/implementation/ble_v2.h"
#include "internal/platform/implementation/linux/ble_v2_socket.h"
namespace nearby {
namespace linux {
class BleV2ServerSocket final : public api::ble_v2::BleServerSocket {
public:
std::unique_ptr<api::ble_v2::BleSocket> Accept() override {
stopped_.WaitForNotification();
return nullptr;
}
explicit BleV2ServerSocket(const std::string& service_id)
: service_id_(service_id) {}
~BleV2ServerSocket() override = default;
Exception Close() override {
if (stopped_.HasBeenNotified()) return {Exception::kIo};
stopped_.Notify();
return {Exception::kSuccess};
}
std::unique_ptr<api::ble_v2::BleSocket> Accept() override
ABSL_LOCKS_EXCLUDED(mutex_);
Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_);
void AddPendingSocket(std::unique_ptr<BleV2Socket> socket)
ABSL_LOCKS_EXCLUDED(mutex_);
std::string GetServiceId() const { return service_id_; }
private:
absl::Notification stopped_;
std::string service_id_;
absl::Mutex mutex_;
absl::CondVar cond_;
bool closed_ ABSL_GUARDED_BY(mutex_) = false;
std::deque<std::unique_ptr<BleV2Socket>> pending_sockets_
ABSL_GUARDED_BY(mutex_);
};
} // namespace linux
} // namespace nearby
@@ -83,6 +83,18 @@ std::string BluetoothDevice::GetMacAddress() const {
}
}
std::string BluetoothDevice::GetAddressType() const {
auto device = device_;
if (device == nullptr) return "public";
try {
return device->AddressType();
} catch (const sdbus::Error& e) {
DBUS_LOG_PROPERTY_GET_ERROR(device, "AddressType", e);
return "public";
}
}
bool BluetoothDevice::ConnectToProfile(absl::string_view service_uuid) {
auto device = device_;
if (device == nullptr) return false;
@@ -52,6 +52,7 @@ class BluetoothDevice : public api::BluetoothDevice {
std::string GetName() const override;
std::string GetMacAddress() const override;
std::string GetAddressType() const;
MacAddress GetAddress() const override { return last_known_address_; }
@@ -43,7 +43,22 @@ std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_path(
return devices_by_path_[device_object_path];
}
std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_unique_id(
api::ble_v2::BlePeripheral::UniqueId id)
{
// converting from stoull to mac again
id &= 0x0000FFFFFFFFFFFFULL; // keep 48 bits
std::ostringstream oss;
oss << std::hex << std::setfill('0') << std::setw(12) << id;
std::string hex = oss.str(); // e.g. "aabbccddeeff"
std::string mac;
for (int i = 0; i < 6; ++i) {
if (i) mac.push_back(':');
mac.append(hex.substr(i * 2, 2));
}
return get_device_by_address(mac);
}
std::shared_ptr<BluetoothDevice> BluetoothDevices::get_device_by_address(
const std::string &addr) {
auto device_object_path =
@@ -50,13 +50,7 @@ class BluetoothDevices final {
ABSL_LOCKS_EXCLUDED(devices_by_path_lock_);
std::shared_ptr<BluetoothDevice> get_device_by_address(const std::string &);
std::shared_ptr<BluetoothDevice> get_device_by_unique_id(
api::ble_v2::BlePeripheral::UniqueId id) {
// TODO: Should probably remove BlePeripheral stuff from here but we can keep it since we can convert to/from
// uint64_t
MacAddress tmp;
MacAddress::FromUint64(id, tmp);
return get_device_by_address(tmp.ToString());
}
api::ble_v2::BlePeripheral::UniqueId id);
std::shared_ptr<MonitoredBluetoothDevice> add_new_device(sdbus::ObjectPath)
ABSL_LOCKS_EXCLUDED(devices_by_path_lock_);