mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Implement encoding/decoding of actions
PiperOrigin-RevId: 528534899
This commit is contained in:
committed by
Copybara-Service
parent
465aa7a489
commit
560df8ea8e
@@ -15,10 +15,10 @@
|
||||
#include "internal/platform/ble_connection_info.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "absl/status/status.h"
|
||||
#include "absl/status/statusor.h"
|
||||
#include "absl/strings/str_format.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "internal/platform/connection_info.h"
|
||||
|
||||
@@ -41,22 +41,22 @@ std::string BleConnectionInfo::ToDataElementBytes() const {
|
||||
mask |= has_psm ? kPsmMask : 0;
|
||||
payload_data.push_back(mask);
|
||||
if (has_mac) {
|
||||
payload_data.insert(payload_data.end(), mac_address_.begin(),
|
||||
payload_data.append(mac_address_.begin(),
|
||||
mac_address_.end());
|
||||
}
|
||||
if (has_gatt) {
|
||||
payload_data.push_back(gatt_characteristic_.length());
|
||||
payload_data.insert(payload_data.end(), gatt_characteristic_.begin(),
|
||||
payload_data.append(gatt_characteristic_.begin(),
|
||||
gatt_characteristic_.end());
|
||||
}
|
||||
if (has_psm) {
|
||||
payload_data.insert(payload_data.end(), psm_.begin(), psm_.end());
|
||||
payload_data.append(psm_.begin(), psm_.end());
|
||||
}
|
||||
payload_data.push_back(actions_);
|
||||
payload_data.append(actions_.begin(), actions_.end());
|
||||
std::string ret;
|
||||
ret.push_back(kDataElementFieldType);
|
||||
ret.push_back(payload_data.size());
|
||||
ret.insert(ret.end(), payload_data.begin(), payload_data.end());
|
||||
ret.append(payload_data.begin(), payload_data.end());
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -111,12 +111,9 @@ absl::StatusOr<BleConnectionInfo> BleConnectionInfo::FromDataElementBytes(
|
||||
return absl::InvalidArgumentError(
|
||||
"Insufficient remaining bytes to read action.");
|
||||
}
|
||||
char action = bytes[position];
|
||||
// Check that we don't have any remaining bytes.
|
||||
if (bytes.size() != ++position) {
|
||||
return absl::InvalidArgumentError(absl::StrFormat(
|
||||
"Nonzero remaining bytes: %d.", bytes.size() - position));
|
||||
}
|
||||
return BleConnectionInfo(address, characteristic, psm, action);
|
||||
auto action_str = bytes.substr(position);
|
||||
return BleConnectionInfo(
|
||||
address, characteristic, psm,
|
||||
std::vector<uint8_t>(action_str.begin(), action_str.end()));
|
||||
}
|
||||
} // namespace nearby
|
||||
|
||||
Reference in New Issue
Block a user