From eea2a91139a5118890c9eec83b3883735396f615 Mon Sep 17 00:00:00 2001 From: jfcarroll Date: Wed, 6 Oct 2021 07:42:17 -0700 Subject: [PATCH] These are the fixes thus far. Most involve whether we're using a pointer or not. PiperOrigin-RevId: 401241518 --- .../impl/windows/bluetooth_classic_medium.cc | 12 ++++++----- .../bluetooth_classic_server_socket.cc | 19 +++++++++--------- .../windows/bluetooth_classic_server_socket.h | 8 ++++++-- .../impl/windows/bluetooth_classic_socket.cc | 20 ++++++++++++------- .../impl/windows/bluetooth_classic_socket.h | 7 +++---- 5 files changed, 39 insertions(+), 27 deletions(-) diff --git a/cpp/platform/impl/windows/bluetooth_classic_medium.cc b/cpp/platform/impl/windows/bluetooth_classic_medium.cc index 071d509d..c26b827d 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_medium.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_medium.cc @@ -60,7 +60,9 @@ void BluetoothClassicMedium::OnScanModeChanged( bool radioDiscoverable = bluetooth_adapter_.GetScanMode() == BluetoothAdapter::ScanMode::kConnectableDiscoverable; - bluetooth_server_socket_->SetScanMode(radioDiscoverable); + if (bluetooth_server_socket_ != nullptr) { + bluetooth_server_socket_->SetScanMode(radioDiscoverable); + } } bool BluetoothClassicMedium::StartDiscovery( @@ -251,14 +253,14 @@ bool BluetoothClassicMedium::CheckSdp(RfcommDeviceService requestedService) { // Do various checks of the SDP record to make sure you are talking to a // device that actually supports the Bluetooth Rfcomm Service // https://docs.microsoft.com/en-us/uwp/api/windows.devices.bluetooth.rfcomm.rfcommdeviceservice.getsdprawattributesasync?view=winrt-20348 - auto attributes = /*await*/ requestedService.GetSdpRawAttributesAsync(); - if (!attributes.get().HasKey(Constants::SdpServiceNameAttributeId)) { + auto attributes = requestedService.GetSdpRawAttributesAsync().get(); + if (!attributes.HasKey(Constants::SdpServiceNameAttributeId)) { NEARBY_LOGS(ERROR) << __func__ << ": Missing SdpServiceNameAttributeId."; return false; } auto attributeReader = DataReader::FromBuffer( - attributes.get().Lookup(Constants::SdpServiceNameAttributeId)); + attributes.Lookup(Constants::SdpServiceNameAttributeId)); auto attributeType = attributeReader.ReadByte(); @@ -374,7 +376,7 @@ winrt::fire_and_forget BluetoothClassicMedium::DeviceWatcher_Added( EnterCriticalSection(&critical_section_); std::unique_ptr bluetoothDevice = - std::make_unique(async.GetResults()); + std::make_unique(async.get()); devices_by_id_[deviceInfo.Id()] = std::move(bluetoothDevice); diff --git a/cpp/platform/impl/windows/bluetooth_classic_server_socket.cc b/cpp/platform/impl/windows/bluetooth_classic_server_socket.cc index 9c8c1350..d676cf16 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_server_socket.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_server_socket.cc @@ -26,6 +26,7 @@ namespace location { namespace nearby { namespace windows { + BluetoothServerSocket::BluetoothServerSocket(const std::string service_name, const std::string service_uuid) : radio_discoverable_(false), @@ -104,15 +105,16 @@ Exception BluetoothServerSocket::StartListening(bool radioDiscoverable) { RfcommServiceId::FromUuid(winrt::guid(service_uuid_))) .get(); - rfcomm_provider_ = &rfcommProviderRef; + rfcomm_provider_ = rfcommProviderRef; - std::wstring_convert> converter; stream_socket_listener_ - .BindServiceNameAsync(winrt::to_hstring(service_name_.c_str())) + .BindServiceNameAsync( + winrt::to_hstring(rfcomm_provider_.ServiceId().AsString()), + SocketProtectionLevel::PlainSocket) .get(); // Set the SDP attributes and start Bluetooth advertising - InitializeServiceSdpAttributes(*rfcomm_provider_, service_name_); + InitializeServiceSdpAttributes(rfcomm_provider_, service_name_); } catch (std::exception exception) { // We will log and eat the exception since the caller // expects nullptr if it fails @@ -133,9 +135,8 @@ Exception BluetoothServerSocket::StartListening(bool radioDiscoverable) { Exception BluetoothServerSocket::StartAdvertising() { try { - rfcomm_provider_->StartAdvertising( - stream_socket_listener_.as(), - radio_discoverable_); + rfcomm_provider_.StartAdvertising(stream_socket_listener_, + radio_discoverable_); } catch (std::exception exception) { // We will log and eat the exception since the caller // expects nullptr if it fails @@ -151,7 +152,7 @@ Exception BluetoothServerSocket::StartAdvertising() { } void BluetoothServerSocket::StopAdvertising() { - rfcomm_provider_->StopAdvertising(); + rfcomm_provider_.StopAdvertising(); } void BluetoothServerSocket::InitializeServiceSdpAttributes( @@ -180,7 +181,7 @@ Exception BluetoothServerSocket::Close() { EnterCriticalSection(&critical_section_); closed_ = true; bluetooth_sockets_ = {}; - rfcomm_provider_->StopAdvertising(); + rfcomm_provider_.StopAdvertising(); LeaveCriticalSection(&critical_section_); return {Exception::kSuccess}; diff --git a/cpp/platform/impl/windows/bluetooth_classic_server_socket.h b/cpp/platform/impl/windows/bluetooth_classic_server_socket.h index b291ea3c..1bfb47be 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_server_socket.h +++ b/cpp/platform/impl/windows/bluetooth_classic_server_socket.h @@ -64,6 +64,10 @@ using winrt::Windows::Storage::Streams::DataWriter; // https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.unicodeencoding?view=winrt-20348 using winrt::Windows::Storage::Streams::UnicodeEncoding; +// Specifies the level of encryption to use on a StreamSocket object. +// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.socketprotectionlevel?view=winrt-22000 +using winrt::Windows::Networking::Sockets::SocketProtectionLevel; + // https://developer.android.com/reference/android/bluetooth/BluetoothServerSocket.html. class BluetoothServerSocket : public api::BluetoothServerSocket { public: @@ -90,8 +94,8 @@ class BluetoothServerSocket : public api::BluetoothServerSocket { Exception StartListening(bool radioDiscoverable); void SetScanMode(bool radioDiscoverable) { - radio_discoverable_ = radioDiscoverable; StopAdvertising(); + radio_discoverable_ = radioDiscoverable; StartAdvertising(); } @@ -116,7 +120,7 @@ class BluetoothServerSocket : public api::BluetoothServerSocket { const std::string service_name_; const std::string service_uuid_; - RfcommServiceProvider* rfcomm_provider_; + RfcommServiceProvider rfcomm_provider_; }; } // namespace windows diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.cc b/cpp/platform/impl/windows/bluetooth_classic_socket.cc index d80dded4..c54a3d9a 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.cc @@ -19,8 +19,13 @@ namespace location { namespace nearby { namespace windows { -BluetoothSocket::BluetoothSocket() { - windows_socket_ = IStreamSocket(); + +BluetoothSocket::BluetoothSocket(StreamSocket streamSocket) + : windows_socket_(streamSocket) { + bluetooth_device_ = std::make_unique( + winrt::Windows::Devices::Bluetooth::BluetoothDevice::FromHostNameAsync( + windows_socket_.Information().RemoteHostName()) + .get()); input_stream_ = std::make_unique(windows_socket_.InputStream()); output_stream_ = @@ -59,8 +64,9 @@ Exception BluetoothSocket::Close() { // https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#getRemoteDevice() // Returns valid BluetoothDevice pointer if there is a connection, and // nullptr otherwise. -// TODO(b/184975123): replace with real implementation. -api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() { return nullptr; } +api::BluetoothDevice* BluetoothSocket::GetRemoteDevice() { + return bluetooth_device_.get(); +} // Starts an asynchronous operation on a StreamSocket object to connect to a // remote network destination specified by a remote hostname and a remote @@ -81,7 +87,7 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( std::int64_t size) { Buffer buffer = Buffer(size); - winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None); + winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None).get(); DataReader dataReader = DataReader::FromBuffer(buffer); @@ -93,7 +99,7 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( IAsyncAction BluetoothSocket::CancelIOAsync() { // Cancels pending reads and writes over a StreamSocket object. // https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.cancelioasync?view=winrt-20348 - return windows_socket_.as().CancelIOAsync(); + return windows_socket_.CancelIOAsync(); } Exception BluetoothSocket::BluetoothInputStream::Close() { @@ -117,7 +123,7 @@ Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) { std::memcpy(buffer.data(), data.data(), data.size()); try { - winrt_stream_.WriteAsync(buffer); + winrt_stream_.WriteAsync(buffer).get(); } catch (std::exception exception) { return {Exception::kFailed}; } diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.h b/cpp/platform/impl/windows/bluetooth_classic_socket.h index 39d357be..8ebc7b62 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.h +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.h @@ -66,9 +66,7 @@ class BluetoothSocket : public api::BluetoothSocket { public: BluetoothSocket(); - BluetoothSocket( - winrt::Windows::Networking::Sockets::StreamSocket streamSocket) - : windows_socket_(streamSocket) {} + BluetoothSocket(StreamSocket streamSocket); ~BluetoothSocket() override; @@ -130,7 +128,8 @@ class BluetoothSocket : public api::BluetoothSocket { std::unique_ptr input_stream_; std::unique_ptr output_stream_; - IStreamSocket windows_socket_; + StreamSocket windows_socket_; + std::unique_ptr bluetooth_device_; }; } // namespace windows