Decode the action in Base NP advertisements back into a list of Data Elements

PiperOrigin-RevId: 466204445
This commit is contained in:
Janusz Sobczak
2022-08-08 19:39:05 -07:00
committed by Copybara-Service
parent 076b00ed61
commit 6e6ae3ebd7
9 changed files with 140 additions and 18 deletions
@@ -14,6 +14,7 @@
#include "third_party/nearby/presence/implementation/advertisement_decoder.h"
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
@@ -24,6 +25,7 @@
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "internal/platform/logging.h"
#include "third_party/nearby/presence/action_factory.h"
#include "third_party/nearby/presence/data_element.h"
namespace nearby {
@@ -65,6 +67,23 @@ bool IsIdentity(int data_type) {
data_type <= DataElement::kProvisionedIdentityFieldType;
}
void DecodeBaseAction(absl::string_view serialized_action,
std::vector<DataElement>& output) {
if (serialized_action.size() != sizeof(uint16_t)) {
NEARBY_LOGS(WARNING) << "Base NP action \'"
<< absl::BytesToHexString(serialized_action)
<< "\' has wrong length " << serialized_action.size()
<< " , expected " << sizeof(uint16_t);
return;
}
// Two bytes in Big Endian order
uint8_t high = serialized_action[0];
uint8_t low = serialized_action[1];
Action action = {.action = static_cast<uint16_t>((high << 8) | low)};
ActionFactory::DecodeAction(action, output);
}
} // namespace
absl::StatusOr<std::vector<DataElement>>
@@ -114,7 +133,11 @@ AdvertisementDecoder::DecodeAdvertisement(absl::string_view advertisement) {
advertisement = *decrypted;
index = 0;
}
result.push_back(*std::move(elem));
if (elem->GetType() == DataElement::kActionFieldType) {
DecodeBaseAction(elem->GetValue(), result);
} else {
result.push_back(*std::move(elem));
}
}
return result;
}