diff --git a/fastpair/BUILD b/fastpair/BUILD index fe16b91e..0bd8c36e 100644 --- a/fastpair/BUILD +++ b/fastpair/BUILD @@ -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", ], ) diff --git a/fastpair/client/windows/BUILD b/fastpair/client/windows/BUILD index c7f55462..b2389ee8 100644 --- a/fastpair/client/windows/BUILD +++ b/fastpair/client/windows/BUILD @@ -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", ], diff --git a/fastpair/client/windows/fast_pair_controller_adapter.cc b/fastpair/client/windows/fast_pair_controller_adapter.cc index e939edfa..5ea1ff14 100644 --- a/fastpair/client/windows/fast_pair_controller_adapter.cc +++ b/fastpair/client/windows/fast_pair_controller_adapter.cc @@ -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(pController_)->IsScanning(); } +bool __stdcall IsPairing(FastPairController *pController) { + if (pController_ == nullptr) { + return false; + } + // return connecting + return static_cast(pController_)->IsPairing(); +} + +bool __stdcall IsServerAccessing(FastPairController *pController) { + if (pController_ == nullptr) { + return false; + } + return static_cast(pController_)->IsServerAccessing(); +} + +} // namespace windows } // namespace fastpair } // namespace nearby diff --git a/fastpair/client/windows/fast_pair_controller_adapter.h b/fastpair/client/windows/fast_pair_controller_adapter.h index 4c03ddcf..05ca047c 100644 --- a/fastpair/client/windows/fast_pair_controller_adapter.h +++ b/fastpair/client/windows/fast_pair_controller_adapter.h @@ -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 diff --git a/fastpair/client/windows/fast_pair_controller_adapter_dart.cc b/fastpair/client/windows/fast_pair_controller_adapter_dart.cc index bd3b889e..cb775fc2 100644 --- a/fastpair/client/windows/fast_pair_controller_adapter_dart.cc +++ b/fastpair/client/windows/fast_pair_controller_adapter_dart.cc @@ -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 diff --git a/fastpair/client/windows/fast_pair_controller_adapter_dart.h b/fastpair/client/windows/fast_pair_controller_adapter_dart.h index b79252f5..45ef46ca 100644 --- a/fastpair/client/windows/fast_pair_controller_adapter_dart.h +++ b/fastpair/client/windows/fast_pair_controller_adapter_dart.h @@ -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 diff --git a/fastpair/fast_pair_controller.cc b/fastpair/fast_pair_controller.cc new file mode 100644 index 00000000..7f4db56b --- /dev/null +++ b/fastpair/fast_pair_controller.cc @@ -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 + +#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(status_code); + return kUnknownStatusCodesString; +} + +} // namespace fastpair +} // namespace nearby diff --git a/fastpair/fast_pair_controller.h b/fastpair/fast_pair_controller.h index 909f7715..1bdf4639 100644 --- a/fastpair/fast_pair_controller.h +++ b/fastpair/fast_pair_controller.h @@ -16,17 +16,36 @@ #define THIRD_PARTY_NEARBY_FASTPAIR_FAST_PAIR_CONTROLLER_H_ #include +#include 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 diff --git a/fastpair/fast_pair_controller_impl.cc b/fastpair/fast_pair_controller_impl.cc new file mode 100644 index 00000000..aca48989 --- /dev/null +++ b/fastpair/fast_pair_controller_impl.cc @@ -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 + +#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(); +} +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 diff --git a/fastpair/fast_pair_controller_impl.h b/fastpair/fast_pair_controller_impl.h new file mode 100644 index 00000000..9760c2b3 --- /dev/null +++ b/fastpair/fast_pair_controller_impl.h @@ -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 +#include + +#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 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_ diff --git a/fastpair/fast_pair_controller_impl_test.cc b/fastpair/fast_pair_controller_impl_test.cc new file mode 100644 index 00000000..2b6d9a57 --- /dev/null +++ b/fastpair/fast_pair_controller_impl_test.cc @@ -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 + +#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 CreateController() { + auto controller = std::make_unique(); + return controller; + } + std::unique_ptr 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 diff --git a/fastpair/fast_pair_controller_test.cc b/fastpair/fast_pair_controller_test.cc new file mode 100644 index 00000000..1d6bb93b --- /dev/null +++ b/fastpair/fast_pair_controller_test.cc @@ -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 +#include + +#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 GetTestData() { + static std::vector* kStatusCodeToStringData = + new std::vector({ + {StatusCodes::kOk, "kOk"}, + {StatusCodes::kError, "kError"}, + }); + + return *kStatusCodeToStringData; +} + +using StatusCodeToString = testing::TestWithParam; + +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 diff --git a/internal/platform/implementation/BUILD b/internal/platform/implementation/BUILD index d224f3b6..169ca97e 100644 --- a/internal/platform/implementation/BUILD +++ b/internal/platform/implementation/BUILD @@ -37,6 +37,7 @@ cc_library( ], defines = ["NO_WEBRTC"], visibility = [ + "//fastpair:__subpackages__", "//internal/platform:__pkg__", "//internal/platform/implementation:__subpackages__", "//location/nearby/analytics/cpp:__subpackages__", diff --git a/internal/platform/implementation/windows/BUILD b/internal/platform/implementation/windows/BUILD index 16021b02..c6160ab1 100644 --- a/internal/platform/implementation/windows/BUILD +++ b/internal/platform/implementation/windows/BUILD @@ -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", ], ) diff --git a/internal/platform/implementation/windows/generated/BUILD b/internal/platform/implementation/windows/generated/BUILD index 155027be..db324943 100644 --- a/internal/platform/implementation/windows/generated/BUILD +++ b/internal/platform/implementation/windows/generated/BUILD @@ -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__", ],