removed bespoke stream semantics of l2cap socket and replaced with generic linux version

This commit is contained in:
Lasan Mahaliyana
2026-06-19 11:27:37 +05:30
parent 14fbeb67c4
commit b504039bcb
3 changed files with 17 additions and 147 deletions
@@ -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",
@@ -34,101 +34,8 @@
namespace nearby {
namespace linux {
BleL2capInputStream::~BleL2capInputStream() {
Close();
}
ExceptionOr<ByteArray> BleL2capInputStream::Read(std::int64_t size) {
std::vector<char> 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<sdbus::UnixFd>(fd)),
input_stream_(std::make_unique<BleL2capInputStream>(fd_)),
output_stream_(std::make_unique<BleL2capOutputStream>(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<void()> notifier) {}
bool BleL2capSocket::IsClosed() const {
return closed_;
}
// Migrated from bespoke l2cap socket stream semantics to linux platform stream
// semantics
//
} // namespace linux
} // namespace nearby
@@ -17,7 +17,6 @@
#include "dbus.h"
#include <atomic>
#include <memory>
#include <optional>
@@ -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<sdbus::UnixFd> fd_raw_): fd_raw_(std::move(fd_raw_)) {};
~BleL2capInputStream() override;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override;
private:
std::shared_ptr<sdbus::UnixFd> fd_raw_;
};
class BleL2capOutputStream final : public OutputStream {
public:
explicit BleL2capOutputStream(std::shared_ptr<sdbus::UnixFd> 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<sdbus::UnixFd> 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<void()> 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<sdbus::UnixFd > fd_ ;
std::unique_ptr<BleL2capInputStream> input_stream_;
std::unique_ptr<BleL2capOutputStream> output_stream_;
sdbus::UnixFd fd_;
OutputStream output_stream_;
InputStream input_stream_;
api::ble::BlePeripheral::UniqueId peripheral_id_;
};