From 252dca528fed45caf2e0dccbd74f59d0386e5a53 Mon Sep 17 00:00:00 2001 From: NP v/d Spek Date: Thu, 19 Sep 2024 15:31:10 +0200 Subject: [PATCH] edxtend the functionality of the bytebuffer. --- esphome/core/bytebuffer.cpp | 38 ++++++++++++++++++++----------------- esphome/core/bytebuffer.h | 12 ++++++------ 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index b308e9731e..6bcf36dbc6 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -74,9 +74,10 @@ void ByteBuffer::set_position(size_t position) { void ByteBuffer::clear() { this->limit_ = this->get_capacity(); this->position_ = 0; + this->used_space_ = 0; } void ByteBuffer::flip() { - this->limit_ = this->position_; + this->limit_ = this->used_space_; this->position_ = 0; } @@ -84,7 +85,7 @@ void ByteBuffer::flip() { uint8_t ByteBuffer::get_uint8() { assert(this->get_remaining() >= 1); this->position_++; - this->update_used_(); + this->update_used_space_(); return this->data_[this->position_]; } uint64_t ByteBuffer::get_uint(size_t length) { @@ -103,7 +104,7 @@ uint64_t ByteBuffer::get_uint(size_t length) { value |= this->data_[this->position_++]; } } - this->update_used_(); + this->update_used_space_(); return value; } @@ -127,22 +128,23 @@ std::vector ByteBuffer::get_vector(size_t length) { assert(this->get_remaining() >= length); auto start = this->data_.begin() + this->position_; this->position_ += length; - this->update_used_(); + this->update_used_space_(); return {start, start + length}; } -void ByteBuffer::get_bytes(const uint8_t *data, size_t length) { +void ByteBuffer::get_bytes(uint8_t *data, size_t length) { + size_t index = 0; assert(this->get_remaining() >= length); - auto start = this->data_.begin() + this->position_; - copy(start, start + length, data); - this->position_ += length; - this->update_used_(); + while (length-- != 0) { + *(data + index++) = this->data_[this->position_++]; + } + this->update_used_space_(); } /// Putters void ByteBuffer::put_uint8(uint8_t value) { assert(this->get_remaining() >= 1); this->data_[this->position_++] = value; - this->update_used_(); + this->update_used_space_(); } void ByteBuffer::put_uint(uint64_t value, size_t length) { @@ -160,7 +162,7 @@ void ByteBuffer::put_uint(uint64_t value, size_t length) { value >>= 8; } } - this->update_used_(); + this->update_used_space_(); } void ByteBuffer::put_float(float value) { static_assert(sizeof(float) == sizeof(uint32_t), "Float sizes other than 32 bit not supported"); @@ -176,13 +178,15 @@ void ByteBuffer::put_vector(const std::vector &value) { assert(this->get_remaining() >= value.size()); std::copy(value.begin(), value.end(), this->data_.begin() + this->position_); this->position_ += value.size(); - this->update_used_(); + this->update_used_space_(); } -void ByteBuffer::put_bytes(const uint8_t *data, size_t size) { - assert(this->get_remaining() >= size); - std::copy(data[0], data[size], this->data_.begin() + this->position_); - this->position_ += size; - this->update_used_(); +void ByteBuffer::put_bytes(const uint8_t *data, size_t length) { + assert(this->get_remaining() >= length); + auto index = 0; + while (length-- != 0) { + this->data_[this->position_++] = static_cast(*(data + index++)); + } + this->update_used_space_(); } } // namespace esphome diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index ea7749a485..42a2a21c42 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -92,7 +92,7 @@ class ByteBuffer { bool get_bool() { return this->get_uint8(); } // Get vector of bytes, increment by length std::vector get_vector(size_t length); - void get_bytes(const uint8_t *data, size_t length); + void get_bytes(uint8_t *data, size_t length); // Put values into the buffer, increment the position accordingly // put any integral value, length represents the number of bytes @@ -119,7 +119,7 @@ class ByteBuffer { inline size_t get_position() const { return this->position_; } inline size_t get_limit() const { return this->limit_; } inline size_t get_remaining() const { return this->get_limit() - this->get_position(); } - inline size_t get_used() const { return this->used_; } + inline size_t get_used_space() const { return this->used_space_; } inline Endian get_endianness() const { return this->endianness_; } inline void mark() { this->mark_ = this->position_; } inline void big_endian() { this->endianness_ = BIG; } @@ -134,12 +134,12 @@ class ByteBuffer { std::vector get_data() { return this->data_; }; void rewind() { this->position_ = 0; } void reset() { this->position_ = this->mark_; } - void resize() { this->used_ = this->position_; } + void resize() { this->used_space_ = this->position_; } protected: ByteBuffer(std::vector const &data) : data_(data), limit_(data.size()) {} - void update_used_() { - if (this->used_ < this->position_) { + void update_used_space_() { + if (this->used_space_ < this->position_) { this->resize(); } } @@ -148,7 +148,7 @@ class ByteBuffer { size_t position_{0}; size_t mark_{0}; size_t limit_{0}; - size_t used_{0}; + size_t used_space_{0}; }; } // namespace esphome