2019-06-18 19:31:22 +02:00
|
|
|
#include "proto.h"
|
2023-04-10 23:20:02 +01:00
|
|
|
#include <cinttypes>
|
2019-04-17 12:06:00 +02:00
|
|
|
#include "esphome/core/log.h"
|
|
|
|
|
|
|
|
namespace esphome {
|
|
|
|
namespace api {
|
|
|
|
|
2021-06-10 22:19:44 +02:00
|
|
|
static const char *const TAG = "api.proto";
|
2019-04-17 12:06:00 +02:00
|
|
|
|
2019-06-18 19:31:22 +02:00
|
|
|
void ProtoMessage::decode(const uint8_t *buffer, size_t length) {
|
2019-04-17 12:06:00 +02:00
|
|
|
uint32_t i = 0;
|
|
|
|
bool error = false;
|
|
|
|
while (i < length) {
|
|
|
|
uint32_t consumed;
|
2019-06-18 19:31:22 +02:00
|
|
|
auto res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!res.has_value()) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Invalid field start at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-06-18 19:31:22 +02:00
|
|
|
uint32_t field_type = (res->as_uint32()) & 0b111;
|
|
|
|
uint32_t field_id = (res->as_uint32()) >> 3;
|
2019-04-17 12:06:00 +02:00
|
|
|
i += consumed;
|
|
|
|
|
|
|
|
switch (field_type) {
|
|
|
|
case 0: { // VarInt
|
2019-06-18 19:31:22 +02:00
|
|
|
res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!res.has_value()) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Invalid VarInt at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!this->decode_varint(field_id, *res)) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Cannot decode VarInt field %" PRIu32 " with value %" PRIu32 "!", field_id, res->as_uint32());
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
i += consumed;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: { // Length-delimited
|
2019-06-18 19:31:22 +02:00
|
|
|
res = ProtoVarInt::parse(&buffer[i], length - i, &consumed);
|
2019-04-17 12:06:00 +02:00
|
|
|
if (!res.has_value()) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Invalid Length Delimited at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-18 19:31:22 +02:00
|
|
|
uint32_t field_length = res->as_uint32();
|
2019-04-17 12:06:00 +02:00
|
|
|
i += consumed;
|
2019-06-18 19:31:22 +02:00
|
|
|
if (field_length > length - i) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Out-of-bounds Length Delimited at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2019-06-18 19:31:22 +02:00
|
|
|
if (!this->decode_length(field_id, ProtoLengthDelimited(&buffer[i], field_length))) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Cannot decode Length Delimited field %" PRIu32 "!", field_id);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
2019-06-18 19:31:22 +02:00
|
|
|
i += field_length;
|
2019-04-17 12:06:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 5: { // 32-bit
|
|
|
|
if (length - i < 4) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Out-of-bounds Fixed32-bit at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-01-09 20:53:12 -08:00
|
|
|
uint32_t val = encode_uint32(buffer[i + 3], buffer[i + 2], buffer[i + 1], buffer[i]);
|
2019-06-18 19:31:22 +02:00
|
|
|
if (!this->decode_32bit(field_id, Proto32Bit(val))) {
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Cannot decode 32-bit field %" PRIu32 " with value %" PRIu32 "!", field_id, val);
|
2019-04-17 12:06:00 +02:00
|
|
|
}
|
|
|
|
i += 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2023-04-10 23:20:02 +01:00
|
|
|
ESP_LOGV(TAG, "Invalid field type at %" PRIu32, i);
|
2019-04-17 12:06:00 +02:00
|
|
|
error = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (error) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-09 22:32:06 +02:00
|
|
|
#ifdef HAS_PROTO_MESSAGE_DUMP
|
2019-06-18 19:31:22 +02:00
|
|
|
std::string ProtoMessage::dump() const {
|
|
|
|
std::string out;
|
|
|
|
this->dump_to(out);
|
|
|
|
return out;
|
|
|
|
}
|
2021-08-09 22:32:06 +02:00
|
|
|
#endif
|
2019-06-18 19:31:22 +02:00
|
|
|
|
2019-04-17 12:06:00 +02:00
|
|
|
} // namespace api
|
|
|
|
} // namespace esphome
|