From 202915c31023ea9b0f69ca32ee3aab63a33c7155 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Mon, 24 Feb 2025 18:16:28 -0800 Subject: [PATCH] Add DCT advertising UUID PiperOrigin-RevId: 730673334 --- connections/implementation/mediums/ble_v2.cc | 5 +++-- .../implementation/mediums/ble_v2/ble_utils.cc | 16 ++++++++++++---- .../implementation/mediums/ble_v2/ble_utils.h | 13 ++++++------- .../mediums/ble_v2/ble_utils_test.cc | 5 ++++- 4 files changed, 25 insertions(+), 14 deletions(-) diff --git a/connections/implementation/mediums/ble_v2.cc b/connections/implementation/mediums/ble_v2.cc index e179ce34..d3d2dd8c 100644 --- a/connections/implementation/mediums/ble_v2.cc +++ b/connections/implementation/mediums/ble_v2.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -707,7 +708,7 @@ bool BleV2::GenerateAdvertisementCharacteristic( GattCharacteristic::Property property = GattCharacteristic::Property::kRead; // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional advertiement_uuid = + std::optional advertiement_uuid = mediums::bleutils::GenerateAdvertisementUuid(slot); if (!advertiement_uuid.has_value()) { LOG(INFO) << "Unable to generate advertisement uuid."; @@ -779,7 +780,7 @@ void BleV2::ProcessFetchGattAdvertisementsRequest( // failure because there's nothing we could've done about a // non-existed characteristic. // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional advertiement_uuid = + std::optional advertiement_uuid = mediums::bleutils::GenerateAdvertisementUuid(slot); if (!advertiement_uuid.has_value()) { continue; diff --git a/connections/implementation/mediums/ble_v2/ble_utils.cc b/connections/implementation/mediums/ble_v2/ble_utils.cc index 3cbb1806..76edd5b0 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils.cc +++ b/connections/implementation/mediums/ble_v2/ble_utils.cc @@ -16,12 +16,12 @@ #include #include +#include #include #include "absl/base/attributes.h" #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" -#include "absl/types/optional.h" #include "connections/implementation/mediums/ble_v2/ble_advertisement.h" #include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" #include "connections/implementation/mediums/ble_v2/ble_packet.h" @@ -52,6 +52,11 @@ constexpr std::int64_t kAdvertisementUuidLsb = 0x8000000000000000; const std::uint64_t kCopresenceServiceUuidMsb = 0x0000FEF300001000; const std::uint64_t kCopresenceServiceUuidLsb = 0x800000805F9B34FB; +// The most significant bits and the least significant bits for the DCT +// UUID. +const std::uint64_t kDctServiceUuidMsb = 0x0000FC7300001000; +const std::uint64_t kDctServiceUuidLsb = 0x800000805F9B34FB; + // Creates a string as a space separated listing of hex bytes with [] at the // beginning and the end. // @@ -69,7 +74,10 @@ std::string StringToPrintableHexString(const std::string& source) { } // namespace ABSL_CONST_INIT const Uuid kCopresenceServiceUuid(kCopresenceServiceUuidMsb, - kCopresenceServiceUuidLsb); + kCopresenceServiceUuidLsb); + +ABSL_CONST_INIT const Uuid kDctServiceUuid(kDctServiceUuidMsb, + kDctServiceUuidLsb); ByteArray GenerateHash(const std::string& source, size_t size) { return Utils::Sha256Hash(source, size); @@ -104,9 +112,9 @@ ByteArray GenerateAdvertisementHash(const ByteArray& advertisement_bytes) { } // NOLINTNEXTLINE(google3-legacy-absl-backports) -absl::optional GenerateAdvertisementUuid(int slot) { +std::optional GenerateAdvertisementUuid(int slot) { if (slot < 0) { - return absl::nullopt; // NOLINT + return std::nullopt; } return Uuid(kAdvertisementUuidMsb, kAdvertisementUuidLsb | slot); } diff --git a/connections/implementation/mediums/ble_v2/ble_utils.h b/connections/implementation/mediums/ble_v2/ble_utils.h index acb80a6f..9db8e8a1 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils.h +++ b/connections/implementation/mediums/ble_v2/ble_utils.h @@ -15,15 +15,13 @@ #ifndef CORE_INTERNAL_MEDIUMS_BLE_V2_UTILS_H_ #define CORE_INTERNAL_MEDIUMS_BLE_V2_UTILS_H_ +#include +#include #include -#include "absl/strings/str_format.h" -#include "absl/types/optional.h" +#include "absl/base/attributes.h" #include "connections/implementation/mediums/ble_v2//ble_advertisement.h" -#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h" -#include "connections/implementation/mediums/ble_v2/ble_packet.h" -#include "connections/implementation/mediums/utils.h" -#include "internal/platform/prng.h" +#include "internal/platform/byte_array.h" #include "internal/platform/uuid.h" namespace nearby { @@ -32,6 +30,7 @@ namespace mediums { namespace bleutils { ABSL_CONST_INIT extern const Uuid kCopresenceServiceUuid; +ABSL_CONST_INIT extern const Uuid kDctServiceUuid; // Return SHA256 hash. // @@ -62,7 +61,7 @@ ByteArray GenerateAdvertisementHash(const ByteArray& advertisement_bytes); // // slot - the advertisement slot to generate a UUID for. // NOLINTNEXTLINE(google3-legacy-absl-backports) -absl::optional GenerateAdvertisementUuid(int slot); +std::optional GenerateAdvertisementUuid(int slot); } // namespace bleutils } // namespace mediums diff --git a/connections/implementation/mediums/ble_v2/ble_utils_test.cc b/connections/implementation/mediums/ble_v2/ble_utils_test.cc index 6e726a4d..3394c44e 100644 --- a/connections/implementation/mediums/ble_v2/ble_utils_test.cc +++ b/connections/implementation/mediums/ble_v2/ble_utils_test.cc @@ -14,11 +14,14 @@ #include "connections/implementation/mediums/ble_v2/ble_utils.h" +#include #include #include "gmock/gmock.h" #include "protobuf-matchers/protocol-buffer-matchers.h" #include "gtest/gtest.h" +#include "internal/platform/byte_array.h" +#include "internal/platform/uuid.h" namespace nearby { namespace connections { @@ -75,7 +78,7 @@ TEST(BleUtilsTest, CanGenerateAdvertisementHash) { TEST(BleUtilsTest, CanGenerateAdvertisementUuid) { // NOLINTNEXTLINE(google3-legacy-absl-backports) - absl::optional generated_uuid = GenerateAdvertisementUuid(0); + std::optional generated_uuid = GenerateAdvertisementUuid(0); ASSERT_TRUE(generated_uuid.has_value()); EXPECT_THAT(std::string(*generated_uuid), StrCaseEq("00000000-0000-3000-8000-000000000000"));