From b504039bcbaf1c10740ed7f73669b0a49d8e967d Mon Sep 17 00:00:00 2001 From: Lasan Mahaliyana Date: Fri, 19 Jun 2026 11:27:37 +0530 Subject: [PATCH] removed bespoke stream semantics of l2cap socket and replaced with generic linux version --- internal/platform/implementation/linux/BUILD | 1 - .../implementation/linux/ble_l2cap_socket.cc | 99 +------------------ .../implementation/linux/ble_l2cap_socket.h | 64 +++--------- 3 files changed, 17 insertions(+), 147 deletions(-) diff --git a/internal/platform/implementation/linux/BUILD b/internal/platform/implementation/linux/BUILD index ad1b127e..1b55b1c4 100644 --- a/internal/platform/implementation/linux/BUILD +++ b/internal/platform/implementation/linux/BUILD @@ -196,7 +196,6 @@ cc_library( "ble_v2_server_socket.cc", "ble_v2_socket.cc", "ble_l2cap_server_socket.cc", - "ble_l2cap_socket.cc", "bluetooth_adapter.cc", "bluetooth_bluez_profile.cc", "bluetooth_classic_device.cc", diff --git a/internal/platform/implementation/linux/ble_l2cap_socket.cc b/internal/platform/implementation/linux/ble_l2cap_socket.cc index a12ad0a7..ae39a4c4 100644 --- a/internal/platform/implementation/linux/ble_l2cap_socket.cc +++ b/internal/platform/implementation/linux/ble_l2cap_socket.cc @@ -34,101 +34,8 @@ namespace nearby { namespace linux { -BleL2capInputStream::~BleL2capInputStream() { - Close(); -} - -ExceptionOr BleL2capInputStream::Read(std::int64_t size) { - std::vector buffer(size); - - pollfd pfds[1]; - pfds[0].fd = fd_raw_->get(); - pfds[0].events = POLLIN; - ssize_t rcvd = 0; - - while (rcvd < size) { - int r = poll(pfds, 1, -1); - if (r < 0) { - if (errno == EINTR) { - continue; - } - return Exception{Exception::kIo}; - } - if (pfds[0].revents & POLLIN) { - auto r = recv(fd_raw_->get(), buffer.data() + rcvd, size - rcvd, 0); - if (r < 0) { - return Exception{Exception::kIo}; - } - rcvd += r; - } - } - - return ExceptionOr{ByteArray(std::string(buffer.begin(), buffer.end()))}; -} - -Exception BleL2capInputStream::Close() { - if (!fd_raw_->isValid()) return {Exception::kSuccess}; - fd_raw_->reset(); - return {Exception::kSuccess}; -} -BleL2capOutputStream::~BleL2capOutputStream() { - Close(); -} - -Exception BleL2capOutputStream::Write(absl::string_view data) { - pollfd pfds[1]; - pfds[0].fd = fd_raw_->get(); - pfds[0].events = POLLOUT; - ssize_t sent = 0; - - while (sent < data.size()) { - int r = poll(pfds, 1, -1); - if (r < 0) { - if (errno == EINTR) { - continue; - } - return Exception{Exception::kIo}; - } - if (pfds[0].revents & POLLOUT) { - auto r = send(fd_raw_->get(), data.data() + sent, data.size(), 0); - if (r < 0) { - return Exception{Exception::kIo}; - } - sent += r; - } - } - return {Exception::kSuccess}; -} - -Exception BleL2capOutputStream::Close() { - if (!fd_raw_->isValid()) return {Exception::kSuccess}; - fd_raw_->reset(); - return {Exception::kSuccess}; -} - -BleL2capSocket::BleL2capSocket(int fd, - api::ble::BlePeripheral::UniqueId peripheral_id, - std::string service_id) - : fd_(std::make_shared(fd)), - input_stream_(std::make_unique(fd_)), - output_stream_(std::make_unique(fd_)), - peripheral_id_(peripheral_id) {} - -BleL2capSocket::~BleL2capSocket() { - Close(); -} - -Exception BleL2capSocket::Close() { - if (!fd_->isValid()) return {Exception::kIo}; - fd_->reset(); - return {Exception::kSuccess}; -} - -void BleL2capSocket::SetCloseNotifier(absl::AnyInvocable notifier) {} - -bool BleL2capSocket::IsClosed() const { - return closed_; -} - +// Migrated from bespoke l2cap socket stream semantics to linux platform stream +// semantics +// } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/ble_l2cap_socket.h b/internal/platform/implementation/linux/ble_l2cap_socket.h index d13ba98c..e05305ba 100644 --- a/internal/platform/implementation/linux/ble_l2cap_socket.h +++ b/internal/platform/implementation/linux/ble_l2cap_socket.h @@ -17,7 +17,6 @@ #include "dbus.h" - #include #include #include @@ -29,70 +28,35 @@ #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" #include "internal/platform/implementation/ble.h" -#include "internal/platform/input_stream.h" -#include "internal/platform/output_stream.h" +#include "internal/platform/implementation/linux/stream.h" namespace nearby { namespace linux { -// TODO: use linux stream instead of bespoke l2cap input/output stream - class BleL2capSocket; -class BleL2capInputStream final : public InputStream { - public: - explicit BleL2capInputStream(std::shared_ptr fd_raw_): fd_raw_(std::move(fd_raw_)) {}; - ~BleL2capInputStream() override; - - ExceptionOr Read(std::int64_t size) override; - Exception Close() override; - -private: - std::shared_ptr fd_raw_; -}; - -class BleL2capOutputStream final : public OutputStream { -public: - explicit BleL2capOutputStream(std::shared_ptr fd_raw_): fd_raw_(std::move(fd_raw_)) {}; - ~BleL2capOutputStream() override; - - Exception Write(absl::string_view data) override; - Exception Flush() override { return {Exception::kSuccess}; } - Exception Close() override; - -private: - std::shared_ptr fd_raw_; - -}; - class BleL2capSocket final : public api::ble::BleL2capSocket { public: - BleL2capSocket(int fd, api::ble::BlePeripheral::UniqueId peripheral_id, - std::string service_id = ""); - ~BleL2capSocket() override; + std::string service_id = "") + : fd_(sdbus::UnixFd(fd)), output_stream_(fd_), input_stream_(fd_) {}; - InputStream& GetInputStream() override { return *input_stream_; } - OutputStream& GetOutputStream() override { return *output_stream_; } - Exception Close() override ABSL_LOCKS_EXCLUDED(mutex_); - void SetCloseNotifier(absl::AnyInvocable notifier) override - ABSL_LOCKS_EXCLUDED(mutex_); + InputStream& GetInputStream() override { return input_stream_; } + OutputStream& GetOutputStream() override { return output_stream_; } + Exception Close() override { + input_stream_.Close(); + output_stream_.Close(); + + return Exception{Exception::kSuccess}; + }; api::ble::BlePeripheral::UniqueId GetRemotePeripheralId() override { return peripheral_id_; } - bool IsClosed() const ABSL_LOCKS_EXCLUDED(mutex_); - private: - friend class BleL2capInputStream; - friend class BleL2capOutputStream; - - mutable absl::Mutex mutex_; - mutable absl::Mutex io_mutex_; - bool closed_ ABSL_GUARDED_BY(mutex_) = false; - std::shared_ptr fd_ ; - std::unique_ptr input_stream_; - std::unique_ptr output_stream_; + sdbus::UnixFd fd_; + OutputStream output_stream_; + InputStream input_stream_; api::ble::BlePeripheral::UniqueId peripheral_id_; };