mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Internal refactor
PiperOrigin-RevId: 727057179
This commit is contained in:
committed by
Copybara-Service
parent
60d5fe440e
commit
eea3441da9
@@ -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 {
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -211,8 +210,8 @@ 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.");
|
||||
@@ -220,7 +219,7 @@ absl::StatusOr<BleAdvertisement> BleAdvertisement::CreateBleAdvertisement(
|
||||
// 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(
|
||||
@@ -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.");
|
||||
@@ -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;
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -104,7 +104,7 @@ 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;
|
||||
@@ -112,8 +112,7 @@ BluetoothDeviceName::BluetoothDeviceName(
|
||||
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();
|
||||
@@ -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();
|
||||
@@ -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.";
|
||||
@@ -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();
|
||||
@@ -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.";
|
||||
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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.");
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,9 +21,9 @@
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/strings/str_cat.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"
|
||||
#include "proto/mediums/ble_frames.pb.h"
|
||||
|
||||
namespace nearby {
|
||||
@@ -137,10 +137,9 @@ BlePacket::BlePacket(const ByteArray& ble_packet_bytes) {
|
||||
}
|
||||
|
||||
ByteArray packet_bytes(ble_packet_bytes);
|
||||
BaseInputStream base_input_stream{packet_bytes};
|
||||
StreamReader stream_reader{packet_bytes};
|
||||
// The first 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 BlePacket: service_id_hash.";
|
||||
return;
|
||||
@@ -155,8 +154,8 @@ BlePacket::BlePacket(const ByteArray& ble_packet_bytes) {
|
||||
}
|
||||
|
||||
// The rest bytes are supposed to be the data.
|
||||
auto data_bytes = base_input_stream.ReadBytes(ble_packet_bytes.size() -
|
||||
kServiceIdHashLength);
|
||||
auto data_bytes =
|
||||
stream_reader.ReadBytes(ble_packet_bytes.size() - kServiceIdHashLength);
|
||||
if (!data_bytes.has_value()) {
|
||||
LOG(INFO) << "Cannot deserialize BlePacket: data.";
|
||||
return;
|
||||
|
||||
@@ -23,10 +23,10 @@
|
||||
#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/nsd_service_info.h"
|
||||
#include "internal/platform/stream_reader.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace connections {
|
||||
@@ -96,9 +96,9 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
return;
|
||||
}
|
||||
|
||||
BaseInputStream base_input_stream{service_info_bytes};
|
||||
StreamReader stream_reader{service_info_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 WifiLanServiceInfo: version_and_pcp.";
|
||||
return;
|
||||
@@ -124,7 +124,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
}
|
||||
|
||||
// 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 WifiLanServiceInfo: endpoint_id.";
|
||||
return;
|
||||
@@ -132,8 +132,7 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
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 WifiLanServiceInfo: service_id_hash.";
|
||||
endpoint_id_.clear();
|
||||
@@ -144,13 +143,12 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
// The next 1 byte is supposed to be the length of the uwb_address. If
|
||||
// available, continues to deserialize UWB address and extra field of WebRtc
|
||||
// state.
|
||||
if (base_input_stream.IsAvailable(1)) {
|
||||
auto expected_uwb_address_length =
|
||||
base_input_stream.ReadUint8().value_or(0);
|
||||
if (stream_reader.IsAvailable(1)) {
|
||||
auto expected_uwb_address_length = stream_reader.ReadUint8().value_or(0);
|
||||
// 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()) {
|
||||
LOG(INFO) << "Cannot deserialize WifiLanServiceInfo: uwb_address.";
|
||||
endpoint_id_.clear();
|
||||
@@ -161,8 +159,8 @@ WifiLanServiceInfo::WifiLanServiceInfo(const NsdServiceInfo& nsd_service_info) {
|
||||
|
||||
// The next 1 byte is extra field.
|
||||
web_rtc_state_ = WebRtcState::kUndefined;
|
||||
if (base_input_stream.IsAvailable(kExtraFieldLength)) {
|
||||
auto extra_field = base_input_stream.ReadUint8().value_or(0);
|
||||
if (stream_reader.IsAvailable(kExtraFieldLength)) {
|
||||
auto extra_field = stream_reader.ReadUint8().value_or(0);
|
||||
web_rtc_state_ = (extra_field & kWebRtcConnectableFlagBitmask) == 1
|
||||
? WebRtcState::kConnectable
|
||||
: WebRtcState::kUnconnectable;
|
||||
|
||||
Reference in New Issue
Block a user