mirror of
https://github.com/esphome/esphome.git
synced 2026-10-14 10:12:52 +00:00
[api] Add encode_progmem_string for progmem string fields
Generated code passes the ProgmemStringRef instead of spelling out the cast.
This commit is contained in:
@@ -498,6 +498,17 @@ class ProtoEncode {
|
||||
progmem_memcpy(pos, data, len);
|
||||
return pos + len;
|
||||
}
|
||||
/// encode_progmem_bytes for a string field that may be in flash.
|
||||
[[nodiscard]] static inline uint8_t *encode_progmem_string(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const ProgmemStringRef &ref) {
|
||||
return encode_progmem_bytes(pos PROTO_ENCODE_DEBUG_ARG, field_id,
|
||||
reinterpret_cast<const uint8_t *>(ref.progmem_ptr()), ref.size());
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_progmem_string_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, const ProgmemStringRef &ref) {
|
||||
return encode_progmem_bytes_force(pos PROTO_ENCODE_DEBUG_ARG, field_id,
|
||||
reinterpret_cast<const uint8_t *>(ref.progmem_ptr()), ref.size());
|
||||
}
|
||||
[[nodiscard]] static inline uint8_t *encode_uint32_force(uint8_t *__restrict__ pos PROTO_ENCODE_DEBUG_PARAM,
|
||||
uint32_t field_id, uint32_t value) {
|
||||
pos = encode_field_raw(pos PROTO_ENCODE_DEBUG_ARG, field_id, 0);
|
||||
|
||||
@@ -1285,10 +1285,9 @@ class PointerToStringBufferType(PointerToBufferTypeBase):
|
||||
return _encode_call(encode_fn, str(tag), f"this->{self.field_name}")
|
||||
if self.progmem:
|
||||
return _encode_call(
|
||||
"encode_progmem_bytes",
|
||||
"encode_progmem_string",
|
||||
str(self.number),
|
||||
f"reinterpret_cast<const uint8_t *>(this->{self.field_name}.progmem_ptr())",
|
||||
f"this->{self.field_name}.size()",
|
||||
f"this->{self.field_name}",
|
||||
force=self.force,
|
||||
)
|
||||
if result := self._encode_bytes_with_precomputed_tag(
|
||||
|
||||
@@ -419,8 +419,8 @@ def test_progmem_rejected_on_non_bytes_field() -> None:
|
||||
("force", "max_len", "encode_fn"),
|
||||
[
|
||||
(True, 120, "encode_short_progmem_string_force("),
|
||||
(False, None, "encode_progmem_bytes("),
|
||||
(True, None, "encode_progmem_bytes_force("),
|
||||
(False, None, "encode_progmem_string("),
|
||||
(True, None, "encode_progmem_string_force("),
|
||||
],
|
||||
)
|
||||
def test_progmem_string_field_copies_from_flash(
|
||||
|
||||
Reference in New Issue
Block a user