1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-14 15:53:48 +01:00

[api] Optimize protobuf memory usage with fixed-size arrays for Bluetooth UUIDs (#9782)

This commit is contained in:
J. Nick Koston
2025-07-21 23:50:49 -10:00
committed by GitHub
parent 0f0038df24
commit ac08fb314f
5 changed files with 165 additions and 51 deletions

View File

@@ -1463,19 +1463,19 @@ message BluetoothGATTGetServicesRequest {
}
message BluetoothGATTDescriptor {
repeated uint64 uuid = 1;
repeated uint64 uuid = 1 [(fixed_array_size) = 2];
uint32 handle = 2;
}
message BluetoothGATTCharacteristic {
repeated uint64 uuid = 1;
repeated uint64 uuid = 1 [(fixed_array_size) = 2];
uint32 handle = 2;
uint32 properties = 3;
repeated BluetoothGATTDescriptor descriptors = 4;
}
message BluetoothGATTService {
repeated uint64 uuid = 1;
repeated uint64 uuid = 1 [(fixed_array_size) = 2];
uint32 handle = 2;
repeated BluetoothGATTCharacteristic characteristics = 3;
}
@@ -1486,7 +1486,7 @@ message BluetoothGATTGetServicesResponse {
option (ifdef) = "USE_BLUETOOTH_PROXY";
uint64 address = 1;
repeated BluetoothGATTService services = 2;
repeated BluetoothGATTService services = 2 [(fixed_array_size) = 1];
}
message BluetoothGATTGetServicesDoneResponse {

View File

@@ -1891,23 +1891,18 @@ bool BluetoothGATTGetServicesRequest::decode_varint(uint32_t field_id, ProtoVarI
return true;
}
void BluetoothGATTDescriptor::encode(ProtoWriteBuffer buffer) const {
for (auto &it : this->uuid) {
buffer.encode_uint64(1, it, true);
}
buffer.encode_uint64(1, this->uuid[0], true);
buffer.encode_uint64(1, this->uuid[1], true);
buffer.encode_uint32(2, this->handle);
}
void BluetoothGATTDescriptor::calculate_size(uint32_t &total_size) const {
if (!this->uuid.empty()) {
for (const auto &it : this->uuid) {
ProtoSize::add_uint64_field_repeated(total_size, 1, it);
}
}
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]);
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]);
ProtoSize::add_uint32_field(total_size, 1, this->handle);
}
void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const {
for (auto &it : this->uuid) {
buffer.encode_uint64(1, it, true);
}
buffer.encode_uint64(1, this->uuid[0], true);
buffer.encode_uint64(1, this->uuid[1], true);
buffer.encode_uint32(2, this->handle);
buffer.encode_uint32(3, this->properties);
for (auto &it : this->descriptors) {
@@ -1915,42 +1910,33 @@ void BluetoothGATTCharacteristic::encode(ProtoWriteBuffer buffer) const {
}
}
void BluetoothGATTCharacteristic::calculate_size(uint32_t &total_size) const {
if (!this->uuid.empty()) {
for (const auto &it : this->uuid) {
ProtoSize::add_uint64_field_repeated(total_size, 1, it);
}
}
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]);
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]);
ProtoSize::add_uint32_field(total_size, 1, this->handle);
ProtoSize::add_uint32_field(total_size, 1, this->properties);
ProtoSize::add_repeated_message(total_size, 1, this->descriptors);
}
void BluetoothGATTService::encode(ProtoWriteBuffer buffer) const {
for (auto &it : this->uuid) {
buffer.encode_uint64(1, it, true);
}
buffer.encode_uint64(1, this->uuid[0], true);
buffer.encode_uint64(1, this->uuid[1], true);
buffer.encode_uint32(2, this->handle);
for (auto &it : this->characteristics) {
buffer.encode_message(3, it, true);
}
}
void BluetoothGATTService::calculate_size(uint32_t &total_size) const {
if (!this->uuid.empty()) {
for (const auto &it : this->uuid) {
ProtoSize::add_uint64_field_repeated(total_size, 1, it);
}
}
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[0]);
ProtoSize::add_uint64_field_repeated(total_size, 1, this->uuid[1]);
ProtoSize::add_uint32_field(total_size, 1, this->handle);
ProtoSize::add_repeated_message(total_size, 1, this->characteristics);
}
void BluetoothGATTGetServicesResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint64(1, this->address);
for (auto &it : this->services) {
buffer.encode_message(2, it, true);
}
buffer.encode_message(2, this->services[0], true);
}
void BluetoothGATTGetServicesResponse::calculate_size(uint32_t &total_size) const {
ProtoSize::add_uint64_field(total_size, 1, this->address);
ProtoSize::add_repeated_message(total_size, 1, this->services);
ProtoSize::add_message_object_repeated(total_size, 1, this->services[0]);
}
void BluetoothGATTGetServicesDoneResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_uint64(1, this->address);

View File

@@ -1796,7 +1796,7 @@ class BluetoothGATTGetServicesRequest : public ProtoDecodableMessage {
};
class BluetoothGATTDescriptor : public ProtoMessage {
public:
std::vector<uint64_t> uuid{};
std::array<uint64_t, 2> uuid{};
uint32_t handle{0};
void encode(ProtoWriteBuffer buffer) const override;
void calculate_size(uint32_t &total_size) const override;
@@ -1808,7 +1808,7 @@ class BluetoothGATTDescriptor : public ProtoMessage {
};
class BluetoothGATTCharacteristic : public ProtoMessage {
public:
std::vector<uint64_t> uuid{};
std::array<uint64_t, 2> uuid{};
uint32_t handle{0};
uint32_t properties{0};
std::vector<BluetoothGATTDescriptor> descriptors{};
@@ -1822,7 +1822,7 @@ class BluetoothGATTCharacteristic : public ProtoMessage {
};
class BluetoothGATTService : public ProtoMessage {
public:
std::vector<uint64_t> uuid{};
std::array<uint64_t, 2> uuid{};
uint32_t handle{0};
std::vector<BluetoothGATTCharacteristic> characteristics{};
void encode(ProtoWriteBuffer buffer) const override;
@@ -1836,12 +1836,12 @@ class BluetoothGATTService : public ProtoMessage {
class BluetoothGATTGetServicesResponse : public ProtoMessage {
public:
static constexpr uint8_t MESSAGE_TYPE = 71;
static constexpr uint8_t ESTIMATED_SIZE = 38;
static constexpr uint8_t ESTIMATED_SIZE = 21;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *message_name() const override { return "bluetooth_gatt_get_services_response"; }
#endif
uint64_t address{0};
std::vector<BluetoothGATTService> services{};
std::array<BluetoothGATTService, 1> services{};
void encode(ProtoWriteBuffer buffer) const override;
void calculate_size(uint32_t &total_size) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP