diff --git a/esphome/core/bytebuffer.cpp b/esphome/core/bytebuffer.cpp index 8d63bbea9a..dc6f423140 100644 --- a/esphome/core/bytebuffer.cpp +++ b/esphome/core/bytebuffer.cpp @@ -11,12 +11,18 @@ ByteBuffer ByteBuffer::create(size_t capacity, Endian endianness) { return buffer; } -ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len) { +ByteBuffer ByteBuffer::wrap(const uint8_t *ptr, size_t len, Endian endianness) { std::vector data(ptr, ptr + len); - return {data}; + ByteBuffer buffer = {data}; + buffer.endianness_ = endianness; + return buffer; } -ByteBuffer ByteBuffer::wrap(std::vector data) { return {std::move(data)}; } +ByteBuffer ByteBuffer::wrap(std::vector data, Endian endianness) { + ByteBuffer buffer = {std::move(data)}; + buffer.endianness_ = endianness; + return buffer; +} ByteBuffer ByteBuffer::wrap(uint8_t value) { ByteBuffer buffer = ByteBuffer::create(1); @@ -54,16 +60,6 @@ ByteBuffer ByteBuffer::wrap(double value, Endian endianness) { return buffer; } -ByteBuffer ByteBuffer::wrap(const std::string &data) { - std::vector buffer(data.begin(), data.end()); - return {buffer}; -} - -ByteBuffer ByteBuffer::wrap(std::initializer_list values) { - std::vector buffer(values); - return {buffer}; -} - void ByteBuffer::set_limit(size_t limit) { assert(limit <= this->get_capacity()); this->limit_ = limit; @@ -191,15 +187,6 @@ double ByteBuffer::get_double() { std::memcpy(&value, byte_array, sizeof(double)); return value; } -std::string ByteBuffer::get_string(size_t length) { - assert(this->get_remaining() >= length); - std::string value; - value.reserve(length); - for (size_t i = 0; i < length; i++) { - value.push_back(this->data_[this->position_++]); - } - return value; -} /// Putters void ByteBuffer::put_uint8(uint8_t value) { @@ -293,10 +280,4 @@ void ByteBuffer::put_double(double value) { } } } -void ByteBuffer::put_string(const std::string &value) { - assert(this->get_remaining() >= value.size()); - for (char c : value) { - this->data_[this->position_++] = c; - } -} } // namespace esphome diff --git a/esphome/core/bytebuffer.h b/esphome/core/bytebuffer.h index 61f3a01c3b..1fa206af6b 100644 --- a/esphome/core/bytebuffer.h +++ b/esphome/core/bytebuffer.h @@ -4,7 +4,6 @@ #include #include #include -#include namespace esphome { @@ -39,11 +38,11 @@ class ByteBuffer { /** * Wrap an existing vector in a ByteBufffer */ - static ByteBuffer wrap(std::vector data); + static ByteBuffer wrap(std::vector data, Endian endianness = LITTLE); /** * Wrap an existing array in a ByteBufffer */ - static ByteBuffer wrap(const uint8_t *ptr, size_t len); + static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE); // Convenience functions to create a ByteBuffer from a value static ByteBuffer wrap(uint8_t value); static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE); @@ -56,8 +55,9 @@ class ByteBuffer { static ByteBuffer wrap(float value, Endian endianness = LITTLE); static ByteBuffer wrap(double value, Endian endianness = LITTLE); static ByteBuffer wrap(bool value) { return wrap(value ? (uint8_t) 1 : (uint8_t) 0); } - static ByteBuffer wrap(const std::string &data); - static ByteBuffer wrap(std::initializer_list values); + static ByteBuffer wrap(std::initializer_list values, Endian endianness = LITTLE) { + return wrap(std::vector(values), endianness); + } // Get one byte from the buffer, increment position by 1 uint8_t get_uint8(); @@ -81,8 +81,6 @@ class ByteBuffer { double get_double(); // Get a bool value, increment by 1 bool get_bool() { return this->get_uint8() != 0; } - // Get a string value, increment by the length of the string - std::string get_string(size_t length); // Put values into the buffer, increment the position accordingly void put_uint8(uint8_t value); @@ -99,7 +97,6 @@ class ByteBuffer { void put_float(float value); void put_double(double value); void put_bool(bool value) { this->put_uint8(value ? 1 : 0); } - void put_string(const std::string &value); inline size_t get_capacity() const { return this->data_.size(); } inline size_t get_position() const { return this->position_; }