diff --git a/internal/platform/implementation/linux/bluetooth_classic_server_socket.cc b/internal/platform/implementation/linux/bluetooth_classic_server_socket.cc index ec6d868c..d06741ca 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_server_socket.cc +++ b/internal/platform/implementation/linux/bluetooth_classic_server_socket.cc @@ -13,34 +13,39 @@ // limitations under the License. #include "internal/platform/implementation/linux/bluetooth_classic_server_socket.h" -#include "absl/strings/str_replace.h" -#include "absl/strings/substitute.h" #include "internal/platform/exception.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_classic_device.h" #include "internal/platform/implementation/linux/bluetooth_classic_socket.h" -#include "internal/platform/implementation/linux/bluez.h" #include "internal/platform/logging.h" namespace nearby { namespace linux { std::unique_ptr BluetoothServerSocket::Accept() { - auto pair = profile_manager_.GetServiceRecordFD(service_uuid_); + if (stopped_.Cancelled()) { + NEARBY_LOGS(ERROR) << __func__ << ": server socket has been stopped"; + return nullptr; + } + + NEARBY_LOGS(VERBOSE) << __func__ + << ": accepting new connections for service uuid " + << service_uuid_; + + auto pair = profile_manager_.GetServiceRecordFD(service_uuid_, stopped_); if (!pair.has_value()) { NEARBY_LOGS(ERROR) << __func__ << "Failed to get a new connection for profile " - << service_uuid_ << " for device "; + << service_uuid_; return nullptr; } auto [device, fd] = *pair; - return std::unique_ptr(new BluetoothSocket(device, fd)); + return std::make_unique(device, std::move(fd)); } Exception BluetoothServerSocket::Close() { - auto profile_object_path = - absl::Substitute("/com/google/nearby/profiles/$0", service_uuid_); - + NEARBY_LOGS(ERROR) << __func__ << ": closing bluetooth server socket"; + stopped_.Cancel(); profile_manager_.Unregister(service_uuid_); return {Exception::kSuccess}; diff --git a/internal/platform/implementation/linux/bluetooth_classic_server_socket.h b/internal/platform/implementation/linux/bluetooth_classic_server_socket.h index 7efdd817..7697eefb 100644 --- a/internal/platform/implementation/linux/bluetooth_classic_server_socket.h +++ b/internal/platform/implementation/linux/bluetooth_classic_server_socket.h @@ -16,6 +16,7 @@ #define PLATFORM_IMPL_LINUX_BLUETOOTH_SERVER_SOCKET_H_ #include "absl/strings/string_view.h" +#include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/implementation/bluetooth_classic.h" #include "internal/platform/implementation/linux/bluetooth_bluez_profile.h" @@ -45,6 +46,7 @@ class BluetoothServerSocket final : public api::BluetoothServerSocket { Exception Close() override; private: + CancellationFlag stopped_; ProfileManager &profile_manager_; std::string service_uuid_; };