Added dummy implementation for BLE sockets

PiperOrigin-RevId: 512797910
This commit is contained in:
Guogang Li
2023-02-27 19:29:58 -08:00
committed by Copybara-Service
parent 843a4c1a35
commit d750e54bc8
5 changed files with 287 additions and 0 deletions
@@ -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",
@@ -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 <cstddef>
#include <exception>
#include <memory>
#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<BluetoothAdapter*>(adapter)) {
DCHECK_NE(adapter_, nullptr);
}
std::unique_ptr<api::ble_v2::BleSocket> 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<BleV2Socket>(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
@@ -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 <deque>
#include <memory>
#include <utility>
#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<BleV2Socket> pending_sockets_ ABSL_GUARDED_BY(mutex_);
};
} // namespace windows
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_INTERNAL_PLATFORM_IMPLEMENTATION_WINDOWS_BLE_V2_SERVER_SOCKET_H_
@@ -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 <cstddef>
#include <memory>
#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<ByteArray> 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<ByteArray>(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
@@ -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 <cstddef>
#include <cstdint>
#include <memory>
#include <utility>
#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<ByteArray> 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_