From fdb0f0f35105228ca930301e115aaa3cb5a54e17 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 20 Feb 2025 17:30:18 -0800 Subject: [PATCH] read endpoint info PiperOrigin-RevId: 729312008 --- Package.swift | 1 + .../mediums/advertisements/BUILD | 24 ++++++++ .../advertisements/advertisement_util.cc | 60 +++++++++++++++++++ .../advertisements/advertisement_util.h | 30 ++++++++++ .../advertisements/advertisement_util_test.cc | 53 ++++++++++++++++ internal/platform/stream_reader.h | 4 +- 6 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 connections/implementation/mediums/advertisements/advertisement_util.cc create mode 100644 connections/implementation/mediums/advertisements/advertisement_util.h create mode 100644 connections/implementation/mediums/advertisements/advertisement_util_test.cc diff --git a/Package.swift b/Package.swift index 588a2252..1103cc0b 100644 --- a/Package.swift +++ b/Package.swift @@ -457,6 +457,7 @@ let package = Package( "connections/implementation/analytics/throughput_recorder_test.cc", "connections/implementation/mediums/advertisements/data_element_test.cc", "connections/implementation/mediums/advertisements/dct_advertisement_test.cc", + "connections/implementation/mediums/advertisements/advertisement_util_test.cc", "connections/implementation/mediums/ble_v2_test.cc", "connections/implementation/mediums/ble_v2/bloom_filter_test.cc", "connections/implementation/mediums/ble_v2/ble_packet_test.cc", diff --git a/connections/implementation/mediums/advertisements/BUILD b/connections/implementation/mediums/advertisements/BUILD index 5364f482..70555806 100644 --- a/connections/implementation/mediums/advertisements/BUILD +++ b/connections/implementation/mediums/advertisements/BUILD @@ -41,6 +41,18 @@ cc_library( ], ) +cc_library( + name = "util", + srcs = ["advertisement_util.cc"], + hdrs = ["advertisement_util.h"], + deps = [ + ":dct_advertisement", + "//internal/platform:base", + "//internal/platform:logging", + "//internal/platform:util", + ], +) + cc_test( name = "data_element_test", srcs = ["data_element_test.cc"], @@ -64,3 +76,15 @@ cc_test( "@com_google_googletest//:gtest_main", ], ) + +cc_test( + name = "advertisement_util_test", + srcs = ["advertisement_util_test.cc"], + deps = [ + ":util", + "//internal/platform:base", + "//internal/platform:util", + "@com_github_protobuf_matchers//protobuf-matchers", + "@com_google_googletest//:gtest_main", + ], +) diff --git a/connections/implementation/mediums/advertisements/advertisement_util.cc b/connections/implementation/mediums/advertisements/advertisement_util.cc new file mode 100644 index 00000000..122ea55b --- /dev/null +++ b/connections/implementation/mediums/advertisements/advertisement_util.cc @@ -0,0 +1,60 @@ +// Copyright 2025 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 "connections/implementation/mediums/advertisements/advertisement_util.h" + +#include +#include +#include + +#include "internal/platform/byte_array.h" +#include "internal/platform/stream_reader.h" + +namespace nearby::connections::advertisements { + +std::optional ReadDeviceName(const ByteArray& endpoint_info) { + StreamReader reader(endpoint_info); + std::optional version = reader.ReadBits(3); + if (!version.has_value() || *version != 1) { + return std::nullopt; + } + + std::optional has_device_name = reader.ReadBits(1); + if (!has_device_name.has_value() || *has_device_name == 0) { + return std::nullopt; + } + + std::optional device_type = reader.ReadBits(4); + if (!device_type.has_value()) { + return std::nullopt; + } + + if (!reader.ReadBytes(16).has_value()) { + return std::nullopt; + } + + std::optional device_length = reader.ReadUint8(); + if (!device_length.has_value() || *device_length == 0) { + return std::nullopt; + } + + std::optional device_name = reader.ReadBytes(*device_length); + if (!device_name.has_value()) { + return std::nullopt; + } + + return std::string(*device_name); +} + +} // namespace nearby::connections::advertisements diff --git a/connections/implementation/mediums/advertisements/advertisement_util.h b/connections/implementation/mediums/advertisements/advertisement_util.h new file mode 100644 index 00000000..908f50e6 --- /dev/null +++ b/connections/implementation/mediums/advertisements/advertisement_util.h @@ -0,0 +1,30 @@ +// Copyright 2025 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_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_ +#define THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_ + +#include +#include + +#include "internal/platform/byte_array.h" + +namespace nearby::connections::advertisements { + +// Reads the device name from the endpoint info. +std::optional ReadDeviceName(const ByteArray& endpoint_info); + +} // namespace nearby::connections::advertisements + +#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_ diff --git a/connections/implementation/mediums/advertisements/advertisement_util_test.cc b/connections/implementation/mediums/advertisements/advertisement_util_test.cc new file mode 100644 index 00000000..e352401f --- /dev/null +++ b/connections/implementation/mediums/advertisements/advertisement_util_test.cc @@ -0,0 +1,53 @@ +// Copyright 2025 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 "connections/implementation/mediums/advertisements/advertisement_util.h" + +#include +#include + +#include "gtest/gtest.h" +#include "internal/platform/byte_array.h" + +namespace nearby::connections::advertisements { +namespace { + +TEST(AdvertisementUtilTest, ReadDeviceName) { + ByteArray endpoint_info{ + "\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0b" + "\x54\x65\x73\x74\x20\x64\x65\x76\x69\x63\x65", + 29}; + std::optional parsed_device_name = ReadDeviceName(endpoint_info); + ASSERT_TRUE(parsed_device_name.has_value()); + EXPECT_EQ(*parsed_device_name, "Test device"); +} + +TEST(AdvertisementUtilTest, ReadInvaidDeviceName) { + EXPECT_FALSE(ReadDeviceName(ByteArray("")).has_value()); + EXPECT_FALSE(ReadDeviceName(ByteArray("\x30")).has_value()); + EXPECT_FALSE(ReadDeviceName(ByteArray("\x32")).has_value()); + EXPECT_FALSE( + ReadDeviceName(ByteArray("\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00", + 17)) + .has_value()); + EXPECT_FALSE( + ReadDeviceName(ByteArray("\x32\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" + "\x00\x00\x00\x00\x00\x00\x0b" + "\x54\x65\x73\x74\x20\x64\x65\x76\x69\x63\x65", + 25)) + .has_value()); +} + +} // namespace +} // namespace nearby::connections::advertisements diff --git a/internal/platform/stream_reader.h b/internal/platform/stream_reader.h index 2131dc3b..ecd6b6f5 100644 --- a/internal/platform/stream_reader.h +++ b/internal/platform/stream_reader.h @@ -27,7 +27,7 @@ namespace nearby { // A base {@link InputStream } for reading the contents of a byte array. class StreamReader { public: - explicit StreamReader(ByteArray &buffer) : buffer_{buffer} {} + explicit StreamReader(const ByteArray &buffer) : buffer_{buffer} {} StreamReader(const StreamReader &) = delete; StreamReader &operator=(const StreamReader &) = delete; ~StreamReader() = default; @@ -54,7 +54,7 @@ class StreamReader { uint8_t bits_unused_{0}; uint8_t bits_buffer_{0}; - ByteArray &buffer_; + const ByteArray &buffer_; size_t position_{0}; };