Skip to content

Commit

Permalink
Internal refactor
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 727057179
  • Loading branch information
ggli-google authored and copybara-github committed Feb 14, 2025
1 parent 60d5fe4 commit eea3441
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 142 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,13 @@ let package = Package(
"internal/platform/feature_flags_test.cc",
"internal/platform/cancelable_alarm_test.cc",
"internal/platform/crypto_test.cc",
"internal/platform/base_input_stream_test.cc",
"internal/platform/byte_array_test.cc",
"internal/platform/bluetooth_utils_test.cc",
"internal/platform/credential_storage_impl_test.cc",
"internal/platform/input_stream_test.cc",
"internal/platform/single_thread_executor_test.cc",
"internal/platform/scheduled_executor_test.cc",
"internal/platform/stream_reader_test.cc",
"internal/platform/stream_writer_test.cc",
"internal/platform/count_down_latch_test.cc",
"internal/platform/pipe_test.cc",
Expand Down
29 changes: 13 additions & 16 deletions connections/implementation/ble_advertisement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
#include "absl/strings/str_cat.h"
#include "connections/implementation/base_pcp_handler.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/bluetooth_utils.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"

namespace nearby {
namespace connections {
Expand Down Expand Up @@ -113,9 +113,9 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}

ByteArray advertisement_bytes{ble_advertisement_bytes};
BaseInputStream base_input_stream{advertisement_bytes};
StreamReader stream_reader{advertisement_bytes};
// The first 1 byte is supposed to be the version and pcp.
auto version_and_pcp_byte = base_input_stream.ReadUint8();
auto version_and_pcp_byte = stream_reader.ReadUint8();
if (!version_and_pcp_byte.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: version_and_pcp.");
Expand Down Expand Up @@ -145,8 +145,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// advertisement.
ByteArray service_id_hash;
if (!fast_advertisement) {
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: service_id_hash.");
Expand All @@ -156,7 +155,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}

// The next 4 bytes are supposed to be the endpoint_id.
auto endpoint_id_bytes = base_input_stream.ReadBytes(kEndpointIdLength);
auto endpoint_id_bytes = stream_reader.ReadBytes(kEndpointIdLength);
if (!endpoint_id_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_id.");
Expand All @@ -165,7 +164,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
std::string endpoint_id = std::string{*endpoint_id_bytes};

// The next 1 byte is supposed to be the length of the endpoint_info.
auto expected_endpoint_info_length = base_input_stream.ReadUint8();
auto expected_endpoint_info_length = stream_reader.ReadUint8();
if (!expected_endpoint_info_length.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_info_length.");
Expand All @@ -174,7 +173,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// The next x bytes are the endpoint info. (Max length is 131 bytes or 17
// bytes as fast_advertisement being true).
auto endpoint_info_bytes =
base_input_stream.ReadBytes(*expected_endpoint_info_length);
stream_reader.ReadBytes(*expected_endpoint_info_length);
if (!endpoint_info_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: endpoint_info.");
Expand All @@ -197,7 +196,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
std::string bluetooth_mac_address;
if (!fast_advertisement) {
auto bluetooth_mac_address_bytes =
base_input_stream.ReadBytes(BluetoothUtils::kBluetoothMacAddressLength);
stream_reader.ReadBytes(BluetoothUtils::kBluetoothMacAddressLength);
if (!bluetooth_mac_address_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: bluetooth_mac_address.");
Expand All @@ -211,16 +210,16 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// it for remaining bytes.
ByteArray uwb_address;
BleAdvertisement ble_advertisement;
if (base_input_stream.IsAvailable(1)) {
auto expected_uwb_address_length = base_input_stream.ReadUint8();
if (stream_reader.IsAvailable(1)) {
auto expected_uwb_address_length = stream_reader.ReadUint8();
if (!expected_uwb_address_length.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: uwb_address_length.");
}
// If the length of uwb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
auto uwb_address_bytes =
base_input_stream.ReadBytes(*expected_uwb_address_length);
stream_reader.ReadBytes(*expected_uwb_address_length);

if (!uwb_address_bytes.has_value()) {
return absl::InvalidArgumentError(
Expand All @@ -231,8 +230,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(

// The next 1 byte is extra field.
if (!fast_advertisement) {
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
auto extra_field = base_input_stream.ReadUint8();
if (stream_reader.IsAvailable(kExtraFieldLength)) {
auto extra_field = stream_reader.ReadUint8();
if (!extra_field.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: extra_field.");
Expand All @@ -245,8 +244,6 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}
}

base_input_stream.Close();

ble_advertisement.fast_advertisement_ = fast_advertisement;
ble_advertisement.version_ = version;
ble_advertisement.pcp_ = pcp;
Expand Down
25 changes: 12 additions & 13 deletions connections/implementation/bluetooth_device_name.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "connections/implementation/base_pcp_handler.h"
#include "connections/implementation/pcp.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"

namespace nearby {
namespace connections {
Expand Down Expand Up @@ -75,9 +75,9 @@ BluetoothDeviceName::BluetoothDeviceName(
return;
}

BaseInputStream base_input_stream{bluetooth_device_name_bytes};
StreamReader stream_reader{bluetooth_device_name_bytes};
// The first 1 byte is supposed to be the version and pcp.
auto version_and_pcp_byte = base_input_stream.ReadUint8();
auto version_and_pcp_byte = stream_reader.ReadUint8();
if (!version_and_pcp_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: version_and_pcp.";
return;
Expand All @@ -104,16 +104,15 @@ BluetoothDeviceName::BluetoothDeviceName(
}

// The next 4 bytes are supposed to be the endpoint_id.
auto endpoint_id_bytes = base_input_stream.ReadBytes(kEndpointIdLength);
auto endpoint_id_bytes = stream_reader.ReadBytes(kEndpointIdLength);
if (!endpoint_id_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: endpoint_id.";
return;
}
endpoint_id_ = std::string{*endpoint_id_bytes};

// The next 3 bytes are supposed to be the service_id_hash.
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: service_id_hash.";
endpoint_id_.clear();
Expand All @@ -123,7 +122,7 @@ BluetoothDeviceName::BluetoothDeviceName(
service_id_hash_ = *service_id_hash_bytes;

// The next 1 byte is field containing WebRtc state.
auto field_byte = base_input_stream.ReadUint8();
auto field_byte = stream_reader.ReadUint8();
if (!field_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: extra_field.";
endpoint_id_.clear();
Expand All @@ -135,10 +134,10 @@ BluetoothDeviceName::BluetoothDeviceName(

// The next 6 bytes are supposed to be reserved, and can be left
// untouched.
base_input_stream.ReadBytes(kReservedLength);
stream_reader.ReadBytes(kReservedLength);

// The next 1 byte is supposed to be the length of the endpoint_info.
auto expected_endpoint_info_length = base_input_stream.ReadUint8();
auto expected_endpoint_info_length = stream_reader.ReadUint8();
if (!expected_endpoint_info_length.has_value()) {
LOG(INFO)
<< "Cannot deserialize BluetoothDeviceName: endpoint_info_length.";
Expand All @@ -148,7 +147,7 @@ BluetoothDeviceName::BluetoothDeviceName(

// The rest bytes are supposed to be the endpoint_info
auto endpoint_info_bytes =
base_input_stream.ReadBytes(*expected_endpoint_info_length);
stream_reader.ReadBytes(*expected_endpoint_info_length);
if (!endpoint_info_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: endpoint_info.";
endpoint_id_.clear();
Expand All @@ -159,9 +158,9 @@ BluetoothDeviceName::BluetoothDeviceName(
// If the input stream has extra bytes, it's for UWB address. The first byte
// is the address length. It can be 2-byte short address or 8-byte extended
// address.
if (base_input_stream.IsAvailable(1)) {
if (stream_reader.IsAvailable(1)) {
// The next 1 byte is supposed to be the length of the uwb_address.
auto expected_uwb_address_length = base_input_stream.ReadUint8();
auto expected_uwb_address_length = stream_reader.ReadUint8();
if (!expected_uwb_address_length.has_value()) {
LOG(INFO)
<< "Cannot deserialize BluetoothDeviceName: uwb_address_length.";
Expand All @@ -172,7 +171,7 @@ BluetoothDeviceName::BluetoothDeviceName(
// If the length of usb_address is not zero, then retrieve it.
if (expected_uwb_address_length != 0) {
auto uwb_address_bytes =
base_input_stream.ReadBytes(*expected_uwb_address_length);
stream_reader.ReadBytes(*expected_uwb_address_length);
if (!uwb_address_bytes.has_value()) {
LOG(INFO) << "Cannot deserialize BluetoothDeviceName: uwb_address.";
endpoint_id_.clear();
Expand Down
34 changes: 15 additions & 19 deletions connections/implementation/mediums/ble_v2/ble_advertisement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "connections/implementation/mediums/ble_v2/ble_advertisement_header.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"

namespace nearby {
namespace connections {
Expand Down Expand Up @@ -98,10 +98,10 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
}

ByteArray advertisement_bytes(ble_advertisement_bytes);
BaseInputStream base_input_stream(advertisement_bytes);
StreamReader stream_reader(advertisement_bytes);
// The first 1 byte is supposed to be the version, socket version and the fast
// advertisement flag.
auto version_byte = base_input_stream.ReadUint8();
auto version_byte = stream_reader.ReadUint8();
if (!version_byte.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: version.");
Expand Down Expand Up @@ -129,8 +129,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// advertisement.
ByteArray service_id_hash;
if (!fast_advertisement) {
auto service_id_hash_bytes =
base_input_stream.ReadBytes(kServiceIdHashLength);
auto service_id_hash_bytes = stream_reader.ReadBytes(kServiceIdHashLength);
if (!service_id_hash_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: service_id_hash.");
Expand All @@ -141,15 +140,14 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// Data length.
uint32_t expected_data_size;
if (fast_advertisement) {
auto fast_data_size_bytes =
base_input_stream.ReadBytes(kFastDataSizeLength);
auto fast_data_size_bytes = stream_reader.ReadBytes(kFastDataSizeLength);
if (!fast_data_size_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: fast_data_size.");
}
expected_data_size = static_cast<uint32_t>(fast_data_size_bytes->data()[0]);
} else {
auto data_size_bytes = base_input_stream.ReadUint32();
auto data_size_bytes = stream_reader.ReadUint32();
if (!data_size_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: data_size.");
Expand All @@ -161,7 +159,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// Check that the stated data size is the same as what we received.
ByteArray data;
if (expected_data_size > 0) {
auto data_bytes = base_input_stream.ReadBytes(expected_data_size);
auto data_bytes = stream_reader.ReadBytes(expected_data_size);
if (!data_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: data.");
Expand All @@ -178,8 +176,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(

// Device token. If the number of remaining bytes are valid for device token,
// then read it.
if (base_input_stream.IsAvailable(kDeviceTokenLength)) {
auto device_token_bytes = base_input_stream.ReadBytes(kDeviceTokenLength);
if (stream_reader.IsAvailable(kDeviceTokenLength)) {
auto device_token_bytes = stream_reader.ReadBytes(kDeviceTokenLength);
if (!device_token_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: device_token.");
Expand All @@ -196,9 +194,8 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
// to put a random or empty device token in the advertisement.
int extra_fields_byte_number =
kExtraFieldsMaskLength + BleAdvertisementHeader::kPsmValueByteLength;
if (base_input_stream.IsAvailable(extra_fields_byte_number)) {
auto extra_fields_bytes =
base_input_stream.ReadBytes(extra_fields_byte_number);
if (stream_reader.IsAvailable(extra_fields_byte_number)) {
auto extra_fields_bytes = stream_reader.ReadBytes(extra_fields_byte_number);
if (!extra_fields_bytes.has_value()) {
return absl::InvalidArgumentError(
"Cannot deserialize BleAdvertisement: extra_field.");
Expand Down Expand Up @@ -304,18 +301,17 @@ BleAdvertisement::BleExtraFields::BleExtraFields(
}

ByteArray mutated_extra_fields_bytes = {ble_extra_fields_bytes};
BaseInputStream base_input_stream{mutated_extra_fields_bytes};
StreamReader stream_reader{mutated_extra_fields_bytes};
// The first 1 byte is field mask.
auto mask_byte = base_input_stream.ReadUint8().value_or(0);
auto mask_byte = stream_reader.ReadUint8().value_or(0);
if (!mask_byte) {
return;
}

// The next 2 bytes are supposed to be the psm value.
if (HasField(mask_byte, kPsmBitmask) &&
base_input_stream.IsAvailable(
BleAdvertisementHeader::kPsmValueByteLength)) {
psm_ = base_input_stream.ReadUint16().value_or(0);
stream_reader.IsAvailable(BleAdvertisementHeader::kPsmValueByteLength)) {
psm_ = stream_reader.ReadUint16().value_or(0);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "internal/flags/nearby_flags.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/base_input_stream.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/logging.h"
#include "internal/platform/stream_reader.h"

namespace nearby {
namespace connections {
Expand Down Expand Up @@ -86,9 +86,9 @@ BleAdvertisementHeader::BleAdvertisementHeader(
return;
}

BaseInputStream base_input_stream(advertisement_header_bytes);
StreamReader stream_reader(advertisement_header_bytes);
// The first 1 byte is supposed to be the version and number of slots.
auto version_and_num_slots_byte = base_input_stream.ReadUint8();
auto version_and_num_slots_byte = stream_reader.ReadUint8();
if (!version_and_num_slots_byte.has_value()) {
LOG(INFO) << "Cannot deserialize BleAdvertisementHeader: version_and_num.";
return;
Expand All @@ -114,17 +114,16 @@ BleAdvertisementHeader::BleAdvertisementHeader(

// The next 10 bytes are supposed to be the service_id_bloom_filter.
service_id_bloom_filter_ =
base_input_stream.ReadBytes(kServiceIdBloomFilterByteLength)
stream_reader.ReadBytes(kServiceIdBloomFilterByteLength)
.value_or(ByteArray());

// The next 4 bytes are supposed to be the advertisement_hash.
advertisement_hash_ =
base_input_stream.ReadBytes(kAdvertisementHashByteLength)
.value_or(ByteArray());
advertisement_hash_ = stream_reader.ReadBytes(kAdvertisementHashByteLength)
.value_or(ByteArray());

// The next 2 bytes are PSM value.
if (base_input_stream.IsAvailable(kPsmValueByteLength)) {
psm_ = base_input_stream.ReadInt16().value_or(0);
if (stream_reader.IsAvailable(kPsmValueByteLength)) {
psm_ = stream_reader.ReadInt16().value_or(0);
}
}

Expand Down
Loading

0 comments on commit eea3441

Please sign in to comment.