diff --git a/internal/platform/BUILD b/internal/platform/BUILD index 4c7b9279..eb79008e 100644 --- a/internal/platform/BUILD +++ b/internal/platform/BUILD @@ -16,13 +16,27 @@ licenses(["notice"]) +cc_library( + name = "logging", + hdrs = [ + "logging.h", + ], + visibility = [ + "//:__subpackages__", + ], + deps = [ + "@com_google_absl//absl/log", + "@com_google_absl//absl/log:check", + "@com_google_absl//absl/log:globals", + ], +) + cc_library( name = "base", srcs = [ "base64_utils.cc", "bluetooth_utils.cc", "input_stream.cc", - "nsd_service_info.cc", "prng.cc", ], hdrs = [ @@ -46,18 +60,12 @@ cc_library( ], copts = ["-DCORE_ADAPTER_DLL"], visibility = [ + "//:__subpackages__", "//chrome/chromeos/assistant/data_migration/lib:__pkg__", - "//connections:__subpackages__", - "//fastpair:__subpackages__", - "//internal/platform:__subpackages__", - "//internal/platform/implementation:__subpackages__", - "//internal/preferences:__subpackages__", - "//internal/weave:__subpackages__", - "//presence:__subpackages__", - "//sharing:__subpackages__", ], deps = [ "//proto:connections_enums_cc_proto", + "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/functional:any_invocable", @@ -77,7 +85,6 @@ cc_library( ], hdrs = [ "base_input_stream.h", - "base_mutex_lock.h", "byte_utils.h", ], visibility = [ @@ -86,7 +93,6 @@ cc_library( ], deps = [ ":base", - "//internal/platform/implementation:types", "@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/strings:str_format", ], @@ -113,37 +119,6 @@ cc_library( ], ) -cc_library( - name = "connection_info", - srcs = [ - "ble_connection_info.cc", - "bluetooth_connection_info.cc", - "connection_info.cc", - "wifi_lan_connection_info.cc", - ], - hdrs = [ - "ble_connection_info.h", - "bluetooth_connection_info.h", - "connection_info.h", - "wifi_lan_connection_info.h", - ], - visibility = [ - "//connections/implementation:__pkg__", - "//connections/v3:__pkg__", - "//internal/interop:__pkg__", - "//presence:__subpackages__", - ], - deps = [ - ":types", - "//proto:connections_enums_cc_proto", - "@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/types:variant", - ], -) - cc_library( name = "error_code_recorder", srcs = [ @@ -155,7 +130,7 @@ cc_library( ], visibility = ["//connections/implementation:__subpackages__"], deps = [ - ":types", + ":logging", "//proto:connections_enums_cc_proto", "//proto/errorcode:error_code_enums_cc_proto", "@com_google_absl//absl/functional:any_invocable", @@ -177,103 +152,40 @@ cc_library( "//presence:__subpackages__", ], deps = [ - "//internal/platform/implementation:types", + ":base", + "@boringssl//:crypto", "@com_google_absl//absl/strings", ], ) cc_library( - name = "test_util", - testonly = True, + name = "connection_info", srcs = [ - "medium_environment.cc", + "ble_connection_info.cc", + "bluetooth_connection_info.cc", + "connection_info.cc", + "wifi_lan_connection_info.cc", ], hdrs = [ - "medium_environment.h", + "ble_connection_info.h", + "bluetooth_connection_info.h", + "connection_info.h", + "wifi_lan_connection_info.h", ], visibility = [ - "//connections:__subpackages__", - "//fastpair:__subpackages__", - "//internal/platform/implementation:__subpackages__", + "//connections/implementation:__pkg__", + "//connections/v3:__pkg__", + "//internal/interop:__pkg__", "//presence:__subpackages__", ], deps = [ - ":base", - ":types", - ":uuid", - "//internal/base", - "//internal/platform/implementation:comm", - "//internal/test", - "@com_google_absl//absl/base:core_headers", - "@com_google_absl//absl/container:flat_hash_map", - "@com_google_absl//absl/container:flat_hash_set", + ":logging", + "//proto:connections_enums_cc_proto", "@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/time", - "@com_google_absl//absl/types:optional", - ], -) - -cc_test( - name = "platform_base_test", - srcs = [ - "bluetooth_utils_test.cc", - "byte_array_test.cc", - "feature_flags_test.cc", - "input_stream_test.cc", - "prng_test.cc", - ], - deps = [ - ":base", - ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep - "@com_github_protobuf_matchers//protobuf-matchers", - "@com_google_absl//absl/hash:hash_testing", - "@com_google_absl//absl/synchronization", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "platform_util_test", - srcs = [ - "byte_utils_test.cc", - ], - deps = [ - ":base", - ":util", - "@com_github_protobuf_matchers//protobuf-matchers", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "cancellation_flag_test", - srcs = [ - "cancellation_flag_test.cc", - ], - deps = [ - ":base", - ":cancellation_flag", - ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep - "@com_github_protobuf_matchers//protobuf-matchers", - "@com_google_googletest//:gtest_main", - ], -) - -cc_test( - name = "error_code_recorder_test", - srcs = [ - "error_code_recorder_test.cc", - ], - deps = [ - ":error_code_recorder", - ":test_util", - "//internal/platform/implementation/g3", # build_cleaner: keep - "@com_github_protobuf_matchers//protobuf-matchers", - "@com_google_googletest//:gtest_main", + "@com_google_absl//absl/types:variant", ], ) @@ -432,6 +344,101 @@ cc_library( ], ) +cc_library( + name = "test_util", + testonly = True, + srcs = [ + "medium_environment.cc", + ], + hdrs = [ + "medium_environment.h", + ], + visibility = [ + "//connections:__subpackages__", + "//fastpair:__subpackages__", + "//internal/platform/implementation:__subpackages__", + "//presence:__subpackages__", + ], + deps = [ + ":base", + ":types", + ":uuid", + "//internal/base", + "//internal/platform/implementation:comm", + "//internal/test", + "@com_google_absl//absl/base:core_headers", + "@com_google_absl//absl/container:flat_hash_map", + "@com_google_absl//absl/container:flat_hash_set", + "@com_google_absl//absl/status", + "@com_google_absl//absl/strings", + "@com_google_absl//absl/strings:str_format", + "@com_google_absl//absl/time", + "@com_google_absl//absl/types:optional", + ], +) + +cc_test( + name = "platform_base_test", + srcs = [ + "bluetooth_utils_test.cc", + "byte_array_test.cc", + "feature_flags_test.cc", + "input_stream_test.cc", + "prng_test.cc", + ], + deps = [ + ":base", + ":test_util", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_absl//absl/hash:hash_testing", + "@com_google_absl//absl/synchronization", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "platform_util_test", + srcs = [ + "byte_utils_test.cc", + ], + deps = [ + ":base", + ":util", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "cancellation_flag_test", + srcs = [ + "cancellation_flag_test.cc", + ], + deps = [ + ":base", + ":cancellation_flag", + ":test_util", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) + +cc_test( + name = "error_code_recorder_test", + srcs = [ + "error_code_recorder_test.cc", + ], + deps = [ + ":error_code_recorder", + ":test_util", + "//internal/platform/implementation/g3", # build_cleaner: keep + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) + cc_test( name = "public_device_test", size = "small", diff --git a/internal/platform/base_input_stream.h b/internal/platform/base_input_stream.h index a7116de3..02e05bc3 100644 --- a/internal/platform/base_input_stream.h +++ b/internal/platform/base_input_stream.h @@ -15,6 +15,10 @@ #ifndef PLATFORM_BASE_BASE_INPUT_STREAM_H_ #define PLATFORM_BASE_BASE_INPUT_STREAM_H_ +#include +#include +#include + #include "internal/platform/byte_array.h" #include "internal/platform/exception.h" #include "internal/platform/input_stream.h" diff --git a/internal/platform/base_mutex_lock.h b/internal/platform/base_mutex_lock.h deleted file mode 100644 index 90f27a29..00000000 --- a/internal/platform/base_mutex_lock.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2020 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 PLATFORM_BASE_BASE_MUTEX_LOCK_H_ -#define PLATFORM_BASE_BASE_MUTEX_LOCK_H_ - -#include "absl/base/thread_annotations.h" -#include "internal/platform/implementation/mutex.h" - -namespace nearby { - -// An RAII mechanism to acquire a Lock over a block of code. -class ABSL_SCOPED_LOCKABLE BaseMutexLock final { - public: - explicit BaseMutexLock(api::Mutex* mutex) ABSL_EXCLUSIVE_LOCK_FUNCTION(mutex) - : mutex_(mutex) { - mutex_->Lock(); - } - ~BaseMutexLock() ABSL_UNLOCK_FUNCTION() { mutex_->Unlock(); } - - private: - api::Mutex* mutex_; -}; - -} // namespace nearby - -#endif // PLATFORM_BASE_BASE_MUTEX_LOCK_H_ diff --git a/internal/platform/bluetooth_utils.cc b/internal/platform/bluetooth_utils.cc index 18f97b3f..39826a36 100644 --- a/internal/platform/bluetooth_utils.cc +++ b/internal/platform/bluetooth_utils.cc @@ -15,9 +15,15 @@ #include "internal/platform/bluetooth_utils.h" #include +#include +#include +#include "absl/strings/ascii.h" #include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" +#include "absl/strings/string_view.h" +#include "internal/platform/byte_array.h" namespace nearby { diff --git a/internal/platform/bluetooth_utils.h b/internal/platform/bluetooth_utils.h index 83da1993..10d21b85 100644 --- a/internal/platform/bluetooth_utils.h +++ b/internal/platform/bluetooth_utils.h @@ -15,6 +15,9 @@ #ifndef PLATFORM_BASE_BLUETOOTH_UTILS_H_ #define PLATFORM_BASE_BLUETOOTH_UTILS_H_ +#include +#include + #include "absl/strings/string_view.h" #include "internal/platform/byte_array.h" diff --git a/internal/platform/byte_array.h b/internal/platform/byte_array.h index 944e5258..2212c0f5 100644 --- a/internal/platform/byte_array.h +++ b/internal/platform/byte_array.h @@ -24,6 +24,7 @@ #include #include "absl/strings/str_cat.h" +#include "absl/strings/string_view.h" namespace nearby { diff --git a/internal/platform/byte_utils.cc b/internal/platform/byte_utils.cc index dd2a54d7..19cb771f 100644 --- a/internal/platform/byte_utils.cc +++ b/internal/platform/byte_utils.cc @@ -15,9 +15,11 @@ #include "internal/platform/byte_utils.h" #include +#include #include "absl/strings/str_format.h" #include "internal/platform/base_input_stream.h" +#include "internal/platform/byte_array.h" namespace nearby { diff --git a/internal/platform/byte_utils.h b/internal/platform/byte_utils.h index 9d4690a5..a69ef2d3 100644 --- a/internal/platform/byte_utils.h +++ b/internal/platform/byte_utils.h @@ -15,6 +15,7 @@ #ifndef PLATFORM_BASE_BYTE_UTILS_H_ #define PLATFORM_BASE_BYTE_UTILS_H_ +#include #include "internal/platform/byte_array.h" namespace nearby { diff --git a/internal/platform/feature_flags.h b/internal/platform/feature_flags.h index 9df7fcbe..daa3476a 100644 --- a/internal/platform/feature_flags.h +++ b/internal/platform/feature_flags.h @@ -17,6 +17,7 @@ #include +#include "absl/base/thread_annotations.h" #include "absl/synchronization/mutex.h" #include "absl/time/time.h" @@ -45,7 +46,7 @@ class FeatureFlags { bool enable_send_payload_offset = true; // Provide better bookkeeping for bandwidth upgrade initiation. This is // necessary to properly support multiple BWU mediums, multiple service, and - // multiple endpionts. + // multiple endpoints. bool support_multiple_bwu_mediums = true; // Allows the code to change the bluetooth radio state bool enable_set_radio_state = false; @@ -53,7 +54,7 @@ class FeatureFlags { // create connection with remote device in a duration. bool enable_connection_timeout = true; // Controls enable or disable to track the status of Bluetooth classic - // conncetion. + // connection. bool enable_bluetooth_connection_status_track = true; // Controls enable or disable BLE scan advertisement for fast pair // service uuid 0x2cfe diff --git a/internal/platform/input_stream.h b/internal/platform/input_stream.h index 383964b6..ffb84a0a 100644 --- a/internal/platform/input_stream.h +++ b/internal/platform/input_stream.h @@ -15,6 +15,7 @@ #ifndef PLATFORM_BASE_INPUT_STREAM_H_ #define PLATFORM_BASE_INPUT_STREAM_H_ +#include #include #include "internal/platform/byte_array.h" diff --git a/internal/platform/listeners.h b/internal/platform/listeners.h index f1143a13..ac170541 100644 --- a/internal/platform/listeners.h +++ b/internal/platform/listeners.h @@ -15,6 +15,7 @@ #ifndef PLATFORM_BASE_LISTENERS_H_ #define PLATFORM_BASE_LISTENERS_H_ +#include #include "absl/functional/any_invocable.h" namespace nearby { diff --git a/internal/platform/nsd_service_info.cc b/internal/platform/nsd_service_info.cc deleted file mode 100644 index dc7740e7..00000000 --- a/internal/platform/nsd_service_info.cc +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2021 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/nsd_service_info.h" - -namespace nearby { - -constexpr absl::string_view NsdServiceInfo::kNsdTypeFormat; - -} // namespace nearby diff --git a/internal/platform/nsd_service_info.h b/internal/platform/nsd_service_info.h index 533030b6..69900120 100644 --- a/internal/platform/nsd_service_info.h +++ b/internal/platform/nsd_service_info.h @@ -16,8 +16,10 @@ #define PLATFORM_BASE_NSD_SERVICE_INFO_H_ #include +#include #include "absl/container/flat_hash_map.h" +#include "absl/strings/string_view.h" namespace nearby { diff --git a/internal/platform/pipe.cc b/internal/platform/pipe.cc index 8249686d..9c6509a5 100644 --- a/internal/platform/pipe.cc +++ b/internal/platform/pipe.cc @@ -21,29 +21,20 @@ #include #include "absl/base/thread_annotations.h" -#include "internal/platform/base_mutex_lock.h" #include "internal/platform/byte_array.h" +#include "internal/platform/condition_variable.h" #include "internal/platform/exception.h" -#include "internal/platform/implementation/condition_variable.h" -#include "internal/platform/implementation/mutex.h" -#include "internal/platform/implementation/platform.h" #include "internal/platform/input_stream.h" +#include "internal/platform/mutex.h" +#include "internal/platform/mutex_lock.h" #include "internal/platform/output_stream.h" namespace nearby { namespace { -using Platform = api::ImplementationPlatform; - class Pipe { public: - Pipe() { -#pragma push_macro("CreateMutex") -#undef CreateMutex - mutex_ = Platform::CreateMutex(api::Mutex::Mode::kRegular); -#pragma pop_macro("CreateMutex") - cond_ = Platform::CreateConditionVariable(mutex_.get()); - } + Pipe() = default; class PipeInputStream : public InputStream { public: @@ -99,12 +90,12 @@ class Pipe { std::deque ABSL_GUARDED_BY(mutex_) buffer_; // Order of declaration matters: // - mutex must be defined before condvar; - std::unique_ptr mutex_; - std::unique_ptr cond_; + Mutex mutex_; + ConditionVariable cond_{&mutex_}; }; ExceptionOr Pipe::Read(size_t size) { - BaseMutexLock lock(mutex_.get()); + MutexLock lock(&mutex_); // We're done reading all the chunks that were written before the OutputStream // was closed, so there's nothing to do here other than return an empty chunk @@ -114,7 +105,7 @@ ExceptionOr Pipe::Read(size_t size) { } while (buffer_.empty() && !input_stream_closed_) { - Exception wait_exception = cond_->Wait(); + Exception wait_exception = cond_.Wait(); if (wait_exception.Raised()) { return ExceptionOr{wait_exception}; @@ -149,22 +140,22 @@ ExceptionOr Pipe::Read(size_t size) { } Exception Pipe::Write(const ByteArray& data) { - BaseMutexLock lock(mutex_.get()); + MutexLock lock(&mutex_); return WriteLocked(data); } void Pipe::MarkInputStreamClosed() { - BaseMutexLock lock(mutex_.get()); + MutexLock lock(&mutex_); if (input_stream_closed_) return; input_stream_closed_ = true; // Trigger cond_ to unblock a potentially-blocked call to read(), and to let // it know to return Exception::IO. - cond_->Notify(); + cond_.Notify(); } void Pipe::MarkOutputStreamClosed() { - BaseMutexLock lock(mutex_.get()); + MutexLock lock(&mutex_); if (output_stream_closed_) return; // Write a sentinel null chunk before marking output_stream_closed as true. WriteLocked(ByteArray{}); @@ -179,7 +170,7 @@ Exception Pipe::WriteLocked(const ByteArray& data) { buffer_.push_back(data); // Trigger cond_ to unblock a potentially-blocked call to read(), now that // there's more data for it to consume. - cond_->Notify(); + cond_.Notify(); return {Exception::kSuccess}; } diff --git a/internal/platform/prng.cc b/internal/platform/prng.cc index 5a4f94b7..02713e99 100644 --- a/internal/platform/prng.cc +++ b/internal/platform/prng.cc @@ -14,6 +14,8 @@ #include "internal/platform/prng.h" +#include +#include #include #include "absl/time/clock.h" diff --git a/internal/platform/uuid.cc b/internal/platform/uuid.cc index c0e46ba4..abba3ec2 100644 --- a/internal/platform/uuid.cc +++ b/internal/platform/uuid.cc @@ -28,7 +28,9 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_split.h" #include "absl/strings/string_view.h" -#include "internal/platform/implementation/crypto.h" +#include "internal/platform/byte_array.h" +#include +#include namespace nearby { namespace { @@ -39,6 +41,21 @@ std::ostream& write_hex(std::ostream& os, absl::string_view data) { } return os; } + +ByteArray Hash(absl::string_view input, const EVP_MD* algo) { + unsigned int md_out_size = EVP_MAX_MD_SIZE; + uint8_t digest_buffer[EVP_MAX_MD_SIZE]; + if (input.empty()) return {}; + + if (!EVP_Digest(input.data(), input.size(), digest_buffer, &md_out_size, algo, + nullptr)) + return {}; + + return ByteArray{reinterpret_cast(digest_buffer), md_out_size}; +} + +ByteArray Md5(absl::string_view input) { return Hash(input, EVP_md5()); } + } // namespace // Based on the Java implementation @@ -87,7 +104,7 @@ std::optional Uuid::FromString(absl::string_view data) { Uuid::Uuid(absl::string_view data) { // Based on the Java counterpart at // http://androidxref.com/8.0.0_r4/xref/libcore/ojluni/src/main/java/java/util/UUID.java#162. - std::string md5_data(Crypto::Md5(data)); + std::string md5_data(Md5(data)); md5_data[6] &= 0x0f; // Clear version. md5_data[6] |= 0x30; // Set to version 3. md5_data[8] &= 0x3f; // Clear variant.