diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 2d95ca45..cf365aa1 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -63,6 +63,8 @@ cc_library( "ble_socket.h", "ble_v2.h", "ble_v2_peripheral.h", + "ble_v2_server_socket.h", + "ble_v2_socket.h", "bluetooth_adapter.h", "bluetooth_classic.h", "bluetooth_classic_device.h", @@ -139,6 +141,8 @@ cc_library( "ble_socket.cc", "ble_v2.cc", "ble_v2_peripheral.cc", + "ble_v2_server_socket.cc", + "ble_v2_socket.cc", "bluetooth_adapter.cc", "bluetooth_classic_device.cc", "bluetooth_classic_medium.cc", diff --git a/internal/platform/implementation/windows/ble_v2_server_socket.cc b/internal/platform/implementation/windows/ble_v2_server_socket.cc new file mode 100644 index 00000000..0be7bd7f --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_server_socket.cc @@ -0,0 +1,76 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/platform/implementation/windows/ble_v2_server_socket.h" + +#include +#include +#include + +#include "absl/log/check.h" +#include "absl/synchronization/mutex.h" +#include "absl/synchronization/notification.h" +#include "internal/platform/exception.h" +#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/windows/bluetooth_adapter.h" +#include "internal/platform/implementation/windows/utils.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace windows { + +BleV2ServerSocket::BleV2ServerSocket(api::BluetoothAdapter* adapter) + : adapter_(dynamic_cast(adapter)) { + DCHECK_NE(adapter_, nullptr); +} + +std::unique_ptr BleV2ServerSocket::Accept() { + absl::MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) << __func__ << ": Accept is called."; + + while (!closed_ && pending_sockets_.empty()) { + cond_.Wait(&mutex_); + } + if (closed_) return nullptr; + + BleV2Socket ble_socket = pending_sockets_.front(); + pending_sockets_.pop_front(); + + NEARBY_LOGS(INFO) << __func__ << ": Accepted a remote connection."; + return std::make_unique(ble_socket); +} + +Exception BleV2ServerSocket::Close() { + // TODO(b/271031645): implement BLE socket using weave + absl::MutexLock lock(&mutex_); + NEARBY_LOGS(INFO) << __func__ << ": Close is called."; + + if (closed_) { + return {Exception::kSuccess}; + } + + closed_ = true; + cond_.SignalAll(); + + return {Exception::kSuccess}; +} + +bool BleV2ServerSocket::Bind() { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(ERROR) << __func__ << ": GATT socket started."; + return true; +} + +} // namespace windows +} // namespace nearby diff --git a/internal/platform/implementation/windows/ble_v2_server_socket.h b/internal/platform/implementation/windows/ble_v2_server_socket.h new file mode 100644 index 00000000..683e2cf9 --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_server_socket.h @@ -0,0 +1,56 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ + +#include +#include +#include + +#include "absl/synchronization/mutex.h" +#include "absl/synchronization/notification.h" +#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/implementation/windows/ble_v2_socket.h" +#include "internal/platform/implementation/windows/bluetooth_adapter.h" + +namespace nearby { +namespace windows { + +class BleV2ServerSocket : public api::ble_v2::BleServerSocket { + public: + explicit BleV2ServerSocket(api::BluetoothAdapter* adapter); + ~BleV2ServerSocket() override = default; + + std::unique_ptr<::nearby::api::ble_v2::BleSocket> Accept() override; + + Exception Close() override; + + // Create GATT service and send/receive characteristic. + bool Bind(); + + private: + BluetoothAdapter* adapter_ = nullptr; + + // Accept notification. + absl::Mutex mutex_; + absl::CondVar cond_; + bool closed_ = false; + std::deque pending_sockets_ ABSL_GUARDED_BY(mutex_); +}; + +} // namespace windows +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_ diff --git a/internal/platform/implementation/windows/ble_v2_socket.cc b/internal/platform/implementation/windows/ble_v2_socket.cc new file mode 100644 index 00000000..00e72e44 --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_socket.cc @@ -0,0 +1,79 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "internal/platform/implementation/windows/ble_v2_socket.h" + +#include +#include + +#include "absl/synchronization/mutex.h" +#include "absl/time/clock.h" +#include "absl/time/time.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/exception.h" +#include "internal/platform/implementation/windows/utils.h" +#include "internal/platform/logging.h" + +namespace nearby { +namespace windows { + +InputStream& BleV2Socket::GetInputStream() { return input_stream_; } + +OutputStream& BleV2Socket::GetOutputStream() { return output_stream_; } + +Exception BleV2Socket::Close() { return {Exception::kSuccess}; } + +api::ble_v2::BlePeripheral* BleV2Socket::GetRemotePeripheral() { + return ble_peripheral_; +} + +bool BleV2Socket::Connect(api::ble_v2::BlePeripheral* ble_peripheral) { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(VERBOSE) << __func__ << ": Connect to BLE peripheral=" + << ble_peripheral->GetAddress(); + return false; +} + +ExceptionOr BleV2Socket::BleInputStream::Read(std::int64_t size) { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(VERBOSE) << __func__ << ": Read data size=" << size; + return ExceptionOr(Exception::kIo); +} + +Exception BleV2Socket::BleInputStream::Close() { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(VERBOSE) << __func__ << ": Close BLE input stream."; + return {Exception::kSuccess}; +} + +Exception BleV2Socket::BleOutputStream::Write(const ByteArray& data) { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(VERBOSE) << __func__ << ": Write data size=" << data.size(); + return {Exception::kIo}; +} + +Exception BleV2Socket::BleOutputStream::Flush() { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(INFO) << __func__ << ": Flush is called."; + return {Exception::kSuccess}; +} + +Exception BleV2Socket::BleOutputStream::Close() { + // TODO(b/271031645): implement BLE socket using weave + NEARBY_LOGS(INFO) << __func__ << ": close is called."; + return {Exception::kSuccess}; +} + +} // namespace windows +} // namespace nearby diff --git a/internal/platform/implementation/windows/ble_v2_socket.h b/internal/platform/implementation/windows/ble_v2_socket.h new file mode 100644 index 00000000..e7c1c1e5 --- /dev/null +++ b/internal/platform/implementation/windows/ble_v2_socket.h @@ -0,0 +1,72 @@ +// Copyright 2023 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_ +#define THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_ + +#include +#include +#include +#include + +#include "internal/platform/byte_array.h" +#include "internal/platform/exception.h" +#include "internal/platform/implementation/ble_v2.h" +#include "internal/platform/input_stream.h" +#include "internal/platform/output_stream.h" + +namespace nearby { +namespace windows { + +class BleV2Socket : public api::ble_v2::BleSocket { + public: + BleV2Socket() = default; + ~BleV2Socket() override = default; + + InputStream& GetInputStream() override; + + OutputStream& GetOutputStream() override; + + Exception Close() override; + + api::ble_v2::BlePeripheral* GetRemotePeripheral() override; + + bool Connect(api::ble_v2::BlePeripheral* ble_peripheral); + + private: + class BleInputStream : public InputStream { + public: + ~BleInputStream() override = default; + ExceptionOr Read(std::int64_t size) override; + Exception Close() override; + }; + + class BleOutputStream : public OutputStream { + public: + ~BleOutputStream() override = default; + + Exception Write(const ByteArray& data) override; + Exception Flush() override; + Exception Close() override; + }; + + BleInputStream input_stream_{}; + BleOutputStream output_stream_{}; + api::ble_v2::BlePeripheral* ble_peripheral_ = nullptr; +}; + +} // namespace windows +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SOCKET_H_