Adding the implementation of the windows controller and cross language interfaces. TODO: Unit test needs to be added after the prototyped finished (b/264707418)

PiperOrigin-RevId: 500274314
This commit is contained in:
hai007
2023-01-06 22:19:46 -08:00
committed by Copybara-Service
parent 05a6fbf20c
commit 22c6db1473
15 changed files with 389 additions and 18 deletions
+32 -1
View File
@@ -23,14 +23,45 @@ package_group(
cc_library(
name = "fastpair_controller",
srcs = [],
srcs = [
"fast_pair_controller.cc",
"fast_pair_controller_impl.cc",
],
hdrs = [
"fast_pair_controller.h",
"fast_pair_controller_impl.h",
],
copts = ["-Ithird_party"],
visibility = [
"//fastpair:__subpackages__",
],
deps = [
"//fastpair/internal/api:platform",
"//fastpair/scanning/fastpair:scanning",
"//internal/platform:base",
"//internal/platform:logging",
],
)
cc_test(
name = "fastpair_controller_test",
size = "small",
timeout = "short",
srcs = [
"fast_pair_controller_impl_test.cc",
"fast_pair_controller_test.cc",
],
copts = [
"-Ithird_party",
],
shard_count = 8,
deps = [
":fastpair_controller",
"//fastpair/internal/api:platform",
"//fastpair/scanning/fastpair:scanning",
"//internal/platform:logging",
"//internal/platform/implementation/g3",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_googletest//:gtest_main",
],
)
+16
View File
@@ -30,8 +30,16 @@ lexan.cc_windows_dll(
"_WIN32_WINNT=_WIN32_WINNT_WIN10",
],
tags = ["windows-dll"],
visibility = [
"//fastpair:__subpackages__",
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair:fastpair_controller",
"//fastpair/internal/api:platform",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
"@com_google_absl//absl/strings",
],
)
@@ -53,8 +61,16 @@ lexan.cc_windows_dll(
"_WIN32_WINNT=_WIN32_WINNT_WIN10",
],
tags = ["windows-dll"],
visibility = [
"//fastpair:__subpackages__",
"//location/nearby/apps/better_together/windows/fast_pair:__subpackages__",
],
deps = [
"//fastpair:fastpair_controller",
"//fastpair/internal/api:platform",
"//internal/platform:logging",
"//internal/platform/implementation/windows",
"//internal/platform/implementation/windows/generated:types",
"//third_party/dart_lang/v2:dart_api_dl",
"@com_google_absl//absl/strings",
],
@@ -14,22 +14,59 @@
#include "fastpair/client/windows/fast_pair_controller_adapter.h"
#include "fastpair/fast_pair_controller.h"
#include "fastpair/fast_pair_controller_impl.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace windows {
FastPairController *InitFastPairController() {
return new FastPairController();
static FastPairController *pController_ = nullptr;
void *InitFastPairController() {
FastPairControllerImpl *pController = new FastPairControllerImpl();
pController_ = pController;
return pController;
}
void CloseFastPairController(FastPairController *pController) {
if (pController != nullptr) delete pController;
NEARBY_LOGS(INFO) << "[[ Closing Fast Pair Controller. ]]";
if (pController_ != nullptr) delete pController_;
NEARBY_LOGS(INFO) << "[[ Successfully closed Fast Pair Controller. ]]";
}
void StartScanning(FastPairController *pController) {
void __stdcall StartScan(FastPairController *pController) {
NEARBY_LOGS(INFO) << "StartScan is called";
if (pController_ == nullptr) {
NEARBY_LOGS(INFO) << "The pController is a null pointer.";
return;
}
return pController_->StartScan();
}
void ServerAccess(FastPairController *pController) {
bool __stdcall IsScanning(FastPairController *pController) {
if (pController_ == nullptr) {
return false;
}
return static_cast<FastPairController *>(pController_)->IsScanning();
}
bool __stdcall IsPairing(FastPairController *pController) {
if (pController_ == nullptr) {
return false;
}
// return connecting
return static_cast<FastPairController *>(pController_)->IsPairing();
}
bool __stdcall IsServerAccessing(FastPairController *pController) {
if (pController_ == nullptr) {
return false;
}
return static_cast<FastPairController *>(pController_)->IsServerAccessing();
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -21,18 +21,29 @@
namespace nearby {
namespace fastpair {
namespace windows {
// Initiate a default FastPairController instance.
// Return the instance handle to client
DLL_EXPORT FastPairController *__stdcall InitFastPairController();
DLL_EXPORT void *__stdcall InitFastPairController();
// Closes the controller with stopping all endpoints, then free the memory.
DLL_EXPORT void __stdcall CloseFastPairController(
FastPairController *pController);
DLL_EXPORT void __stdcall StartScanning(FastPairController *pController);
DLL_EXPORT void __stdcall StartScan(FastPairController *pController);
DLL_EXPORT void __stdcall ServerAccess(FastPairController *pController);
// Obtain scanning status
DLL_EXPORT bool __stdcall IsScanning(FastPairController *pController);
// Obtain connecting status
DLL_EXPORT bool __stdcall IsPairing(FastPairController *pController);
// Server Access trigger just for adapter implementation testing, will be delete
// after success
DLL_EXPORT bool __stdcall IsServerAccessing(FastPairController *pController);
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -18,14 +18,17 @@
namespace nearby {
namespace fastpair {
namespace windows {
void StartScanningDart(FastPairController *pController) {
StartScanning(pController);
void StartScanDart(FastPairController *pController) {
if (IsScanning(pController)) return;
StartScan(pController);
}
void ServerAccessDart(FastPairController *pController) {
ServerAccess(pController);
IsServerAccessing(pController);
}
} // namespace windows
} // namespace fastpair
} // namespace nearby
@@ -20,11 +20,13 @@
namespace nearby {
namespace fastpair {
namespace windows {
DLL_EXPORT void __stdcall StartScanningDart(FastPairController* pController);
DLL_EXPORT void __stdcall StartScanDart(FastPairController* pController);
DLL_EXPORT void __stdcall ServerAccessDart(FastPairController* pController);
} // namespace windows
} // namespace fastpair
} // namespace nearby
+43
View File
@@ -0,0 +1,43 @@
// Copyright 2022 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 "fastpair/fast_pair_controller.h"
#include <string>
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
namespace {
using StatusCodes = FastPairController::StatusCodes;
constexpr char kUnknownStatusCodesString[] = "Unknown_StatusCodes";
} // namespace
std::string FastPairController::StatusCodeToString(StatusCodes status_code) {
switch (status_code) {
case StatusCodes::kOk:
return "kOk";
case StatusCodes::kError:
return "kError";
}
NEARBY_LOGS(ERROR) << "Unknown value for Status codes: "
<< static_cast<int>(status_code);
return kUnknownStatusCodesString;
}
} // namespace fastpair
} // namespace nearby
+22 -3
View File
@@ -16,17 +16,36 @@
#define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_H_
#include <functional>
#include <string>
namespace nearby {
namespace fastpair {
class FastPairController {
public:
~FastPairController() = default;
enum class StatusCodes {
// The operation successed.
kOk = 0,
// The operation failed.
kError = 1,
};
void StartScanning();
static std::string StatusCodeToString(StatusCodes status_code);
void ServerAccess();
virtual ~FastPairController() = default;
// Obtain the scanning status
virtual bool IsScanning() = 0;
// Obtain the pairing status
virtual bool IsPairing() = 0;
// Server Access trigger just for adapter implementation testing, will be
// delete after success
virtual bool IsServerAccessing() = 0;
// Trigger function of the scanning
virtual void StartScan() = 0;
};
} // namespace fastpair
+47
View File
@@ -0,0 +1,47 @@
// Copyright 2022 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 "fastpair/fast_pair_controller_impl.h"
#include <memory>
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
#include "internal/platform/logging.h"
namespace nearby {
namespace fastpair {
FastPairControllerImpl::FastPairControllerImpl() {
NEARBY_LOGS(INFO) << "FastPairControllerImpl starts.";
scanner_ = std::make_unique<FastPairScannerImpl>();
}
FastPairControllerImpl::~FastPairControllerImpl() = default;
void FastPairControllerImpl::StartScan() {
if (is_scanning_) {
NEARBY_LOGS(VERBOSE) << __func__ << ": We're currently scanning. ";
return;
}
is_scanning_ = true;
scanner_->StartScanning();
}
bool FastPairControllerImpl::IsScanning() { return is_scanning_; }
bool FastPairControllerImpl::IsPairing() { return is_connecting_; }
bool FastPairControllerImpl::IsServerAccessing() { return is_server_access_; }
} // namespace fastpair
} // namespace nearby
+52
View File
@@ -0,0 +1,52 @@
// Copyright 2022 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_FASTPAIR_FAST_PAIR_CONTROLLER_IMPL_H_
#define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_IMPL_H_
#include <functional>
#include <memory>
#include "fastpair/fast_pair_controller.h"
#include "fastpair/scanning/fastpair/fast_pair_scanner_impl.h"
namespace nearby {
namespace fastpair {
class FastPairScannerImpl;
class FastPairControllerImpl : public FastPairController,
public FastPairScannerImpl {
public:
explicit FastPairControllerImpl();
~FastPairControllerImpl() override;
bool IsScanning() override;
bool IsPairing() override;
bool IsServerAccessing() override;
void StartScan() override;
private:
std::unique_ptr<FastPairScannerImpl> scanner_;
// True if we are currently scanning for remote devices.
bool is_scanning_ = false;
bool is_connecting_ = false;
bool is_server_access_ = false;
};
} // namespace fastpair
} // namespace nearby
#endif // THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_IMPL_H_
@@ -0,0 +1,50 @@
// Copyright 2022 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 "fastpair/fast_pair_controller_impl.h"
#include <memory>
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
namespace nearby {
namespace fastpair {
namespace FastPairControllerUnitTests {
class FastPairControllerImplTest : public ::testing::Test {
public:
FastPairControllerImplTest() = default;
~FastPairControllerImplTest() override = default;
void SetUp() override { controller_ = CreateController(); }
protected:
std::unique_ptr<FastPairControllerImpl> CreateController() {
auto controller = std::make_unique<FastPairControllerImpl>();
return controller;
}
std::unique_ptr<FastPairControllerImpl> controller_;
};
TEST_F(FastPairControllerImplTest, StartScanningSuccess) {
EXPECT_FALSE(controller_->IsScanning());
EXPECT_FALSE(controller_->IsPairing());
EXPECT_FALSE(controller_->IsServerAccessing());
controller_->StartScan();
EXPECT_TRUE(controller_->IsScanning());
}
} // namespace FastPairControllerUnitTests
} // namespace fastpair
} // namespace nearby
+53
View File
@@ -0,0 +1,53 @@
// Copyright 2022 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 "fastpair/fast_pair_controller.h"
#include <string>
#include <vector>
#include "gtest/gtest.h"
namespace nearby {
namespace fastpair {
using ::nearby::fastpair::FastPairController;
using StatusCodes = FastPairController::StatusCodes;
struct StatusCodeToStringData {
StatusCodes status_code;
std::string expected_string_result;
};
std::vector<StatusCodeToStringData> GetTestData() {
static std::vector<StatusCodeToStringData>* kStatusCodeToStringData =
new std::vector<StatusCodeToStringData>({
{StatusCodes::kOk, "kOk"},
{StatusCodes::kError, "kError"},
});
return *kStatusCodeToStringData;
}
using StatusCodeToString = testing::TestWithParam<StatusCodeToStringData>;
TEST_P(StatusCodeToString, ToStringResultMatches) {
EXPECT_EQ(GetParam().expected_string_result,
FastPairController::StatusCodeToString(GetParam().status_code));
}
INSTANTIATE_TEST_CASE_P(StatusCodeToString, StatusCodeToString,
testing::ValuesIn(GetTestData()));
} // namespace fastpair
} // namespace nearby
+1
View File
@@ -37,6 +37,7 @@ cc_library(
],
defines = ["NO_WEBRTC"],
visibility = [
"//fastpair:__subpackages__",
"//internal/platform:__pkg__",
"//internal/platform/implementation:__subpackages__",
"//location/nearby/analytics/cpp:__subpackages__",
@@ -157,7 +157,7 @@ cc_library(
defines = ["_SILENCE_CLANG_COROUTINE_MESSAGE"],
visibility = [
"//connections/clients/windows:__subpackages__",
"//fastpair/internal/impl/windows:__subpackages__",
"//fastpair:__subpackages__",
"//location/nearby:__subpackages__",
],
deps = [
@@ -166,8 +166,10 @@ cc_library(
":types",
"//internal/platform:base",
"//internal/platform:cancellation_flag",
"//internal/platform:comm",
"//internal/platform:logging",
"//internal/platform:types",
"//internal/platform/implementation:comm",
"//internal/platform/implementation:platform",
"//internal/platform/implementation:types",
"//internal/platform/implementation/shared:count_down_latch",
@@ -175,9 +177,13 @@ cc_library(
"//internal/platform/implementation/windows/generated:types",
"//internal/platform/implementation/windows/json:types",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
],
)
@@ -39,7 +39,7 @@ cc_library(
textual_hdrs = glob(["**/*.h"]),
visibility = [
"//connections/windows:__subpackages__",
"//fastpair/internal:__subpackages__",
"//fastpair:__subpackages__",
"//internal:__subpackages__",
"//internal/platform/implementation/windows:__subpackages__",
],