read endpoint info

PiperOrigin-RevId: 729312008
This commit is contained in:
Guogang Li
2025-02-20 17:31:45 -08:00
committed by Copybara-Service
parent add588c622
commit fdb0f0f351
6 changed files with 170 additions and 2 deletions
+1
View File
@@ -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",
@@ -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",
],
)
@@ -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 <cstdint>
#include <optional>
#include <string>
#include "internal/platform/byte_array.h"
#include "internal/platform/stream_reader.h"
namespace nearby::connections::advertisements {
std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info) {
StreamReader reader(endpoint_info);
std::optional<uint8_t> version = reader.ReadBits(3);
if (!version.has_value() || *version != 1) {
return std::nullopt;
}
std::optional<uint8_t> has_device_name = reader.ReadBits(1);
if (!has_device_name.has_value() || *has_device_name == 0) {
return std::nullopt;
}
std::optional<uint8_t> device_type = reader.ReadBits(4);
if (!device_type.has_value()) {
return std::nullopt;
}
if (!reader.ReadBytes(16).has_value()) {
return std::nullopt;
}
std::optional<uint8_t> device_length = reader.ReadUint8();
if (!device_length.has_value() || *device_length == 0) {
return std::nullopt;
}
std::optional<ByteArray> device_name = reader.ReadBytes(*device_length);
if (!device_name.has_value()) {
return std::nullopt;
}
return std::string(*device_name);
}
} // namespace nearby::connections::advertisements
@@ -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 <optional>
#include <string>
#include "internal/platform/byte_array.h"
namespace nearby::connections::advertisements {
// Reads the device name from the endpoint info.
std::optional<std::string> ReadDeviceName(const ByteArray& endpoint_info);
} // namespace nearby::connections::advertisements
#endif // THIRD_PARTY_NEARBY_CONNECTIONS_IMPLEMENTATION_MEDIUMS_ADVERTISEMENTS_ADVERTISEMENT_UTIL_H_
@@ -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 <optional>
#include <string>
#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<std::string> 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
+2 -2
View File
@@ -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};
};