ESPHome  2024.10.2
bytebuffer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 #include <cinttypes>
6 #include <cstddef>
7 
8 namespace esphome {
9 
10 enum Endian { LITTLE, BIG };
11 
33 class ByteBuffer {
34  public:
35  // Default constructor (compatibility with TEMPLATABLE_VALUE)
36  ByteBuffer() : ByteBuffer(std::vector<uint8_t>()) {}
40  ByteBuffer(size_t capacity, Endian endianness = LITTLE)
41  : data_(std::vector<uint8_t>(capacity)), endianness_(endianness), limit_(capacity){};
45  static ByteBuffer wrap(std::vector<uint8_t> const &data, Endian endianness = LITTLE);
49  static ByteBuffer wrap(const uint8_t *ptr, size_t len, Endian endianness = LITTLE);
50  // Convenience functions to create a ByteBuffer from a value
51  static ByteBuffer wrap(uint8_t value);
52  static ByteBuffer wrap(uint16_t value, Endian endianness = LITTLE);
53  static ByteBuffer wrap(uint32_t value, Endian endianness = LITTLE);
54  static ByteBuffer wrap(uint64_t value, Endian endianness = LITTLE);
55  static ByteBuffer wrap(int8_t value) { return wrap(static_cast<uint8_t>(value)); }
56  static ByteBuffer wrap(int16_t value, Endian endianness = LITTLE) {
57  return wrap(static_cast<uint16_t>(value), endianness);
58  }
59  static ByteBuffer wrap(int32_t value, Endian endianness = LITTLE) {
60  return wrap(static_cast<uint32_t>(value), endianness);
61  }
62  static ByteBuffer wrap(int64_t value, Endian endianness = LITTLE) {
63  return wrap(static_cast<uint64_t>(value), endianness);
64  }
65  static ByteBuffer wrap(float value, Endian endianness = LITTLE);
66  static ByteBuffer wrap(double value, Endian endianness = LITTLE);
67  static ByteBuffer wrap(bool value) { return wrap(static_cast<uint8_t>(value)); }
68 
69  // Get an integral value from the buffer, increment position by length
70  uint64_t get_uint(size_t length);
71  // Get one byte from the buffer, increment position by 1
72  uint8_t get_uint8();
73  // Get a 16 bit unsigned value, increment by 2
74  uint16_t get_uint16() { return static_cast<uint16_t>(this->get_uint(sizeof(uint16_t))); };
75  // Get a 24 bit unsigned value, increment by 3
76  uint32_t get_uint24() { return static_cast<uint32_t>(this->get_uint(3)); };
77  // Get a 32 bit unsigned value, increment by 4
78  uint32_t get_uint32() { return static_cast<uint32_t>(this->get_uint(sizeof(uint32_t))); };
79  // Get a 64 bit unsigned value, increment by 8
80  uint64_t get_uint64() { return this->get_uint(sizeof(uint64_t)); };
81  // Signed versions of the get functions
82  uint8_t get_int8() { return static_cast<int8_t>(this->get_uint8()); };
83  int16_t get_int16() { return static_cast<int16_t>(this->get_uint(sizeof(int16_t))); }
84  uint32_t get_int24();
85  int32_t get_int32() { return static_cast<int32_t>(this->get_uint(sizeof(int32_t))); }
86  int64_t get_int64() { return static_cast<int64_t>(this->get_uint(sizeof(int64_t))); }
87  // Get a float value, increment by 4
88  float get_float();
89  // Get a double value, increment by 8
90  double get_double();
91  // Get a bool value, increment by 1
92  bool get_bool() { return this->get_uint8(); }
93  // Get vector of bytes, increment by length
94  std::vector<uint8_t> get_vector(size_t length);
95 
96  // Put values into the buffer, increment the position accordingly
97  // put any integral value, length represents the number of bytes
98  void put_uint(uint64_t value, size_t length);
99  void put_uint8(uint8_t value);
100  void put_uint16(uint16_t value) { this->put_uint(value, sizeof(uint16_t)); }
101  void put_uint24(uint32_t value) { this->put_uint(value, 3); }
102  void put_uint32(uint32_t value) { this->put_uint(value, sizeof(uint32_t)); }
103  void put_uint64(uint64_t value) { this->put_uint(value, sizeof(uint64_t)); }
104  // Signed versions of the put functions
105  void put_int8(int8_t value) { this->put_uint8(static_cast<uint8_t>(value)); }
106  void put_int16(int32_t value) { this->put_uint(static_cast<uint16_t>(value), sizeof(uint16_t)); }
107  void put_int24(int32_t value) { this->put_uint(static_cast<uint32_t>(value), 3); }
108  void put_int32(int32_t value) { this->put_uint(static_cast<uint32_t>(value), sizeof(uint32_t)); }
109  void put_int64(int64_t value) { this->put_uint(static_cast<uint64_t>(value), sizeof(uint64_t)); }
110  // Extra put functions
111  void put_float(float value);
112  void put_double(double value);
113  void put_bool(bool value) { this->put_uint8(value); }
114  void put_vector(const std::vector<uint8_t> &value);
115 
116  inline size_t get_capacity() const { return this->data_.size(); }
117  inline size_t get_position() const { return this->position_; }
118  inline size_t get_limit() const { return this->limit_; }
119  inline size_t get_remaining() const { return this->get_limit() - this->get_position(); }
120  inline Endian get_endianness() const { return this->endianness_; }
121  inline void mark() { this->mark_ = this->position_; }
122  inline void big_endian() { this->endianness_ = BIG; }
123  inline void little_endian() { this->endianness_ = LITTLE; }
124  void set_limit(size_t limit);
125  void set_position(size_t position);
126  // set position to 0, limit to capacity.
127  void clear();
128  // set limit to current position, postition to zero. Used when swapping from write to read operations.
129  void flip();
130  // retrieve a pointer to the underlying data.
131  std::vector<uint8_t> get_data() { return this->data_; };
132  void rewind() { this->position_ = 0; }
133  void reset() { this->position_ = this->mark_; }
134 
135  protected:
136  ByteBuffer(std::vector<uint8_t> const &data) : data_(data), limit_(data.size()) {}
137  std::vector<uint8_t> data_;
139  size_t position_{0};
140  size_t mark_{0};
141  size_t limit_{0};
142 };
143 
144 } // namespace esphome
void put_uint32(uint32_t value)
Definition: bytebuffer.h:102
void put_int8(int8_t value)
Definition: bytebuffer.h:105
size_t get_limit() const
Definition: bytebuffer.h:118
void set_limit(size_t limit)
Definition: bytebuffer.cpp:66
void put_uint64(uint64_t value)
Definition: bytebuffer.h:103
std::vector< uint8_t > data_
Definition: bytebuffer.h:137
static ByteBuffer wrap(int64_t value, Endian endianness=LITTLE)
Definition: bytebuffer.h:62
STL namespace.
uint32_t get_uint32()
Definition: bytebuffer.h:78
static ByteBuffer wrap(std::vector< uint8_t > const &data, Endian endianness=LITTLE)
Wrap an existing vector in a ByteBufffer.
Definition: bytebuffer.cpp:18
std::vector< uint8_t > get_vector(size_t length)
Definition: bytebuffer.cpp:123
std::vector< uint8_t > get_data()
Definition: bytebuffer.h:131
void put_int64(int64_t value)
Definition: bytebuffer.h:109
void put_uint8(uint8_t value)
Putters.
Definition: bytebuffer.cpp:131
size_t get_position() const
Definition: bytebuffer.h:117
int64_t get_int64()
Definition: bytebuffer.h:86
uint8_t get_int8()
Definition: bytebuffer.h:82
static ByteBuffer wrap(bool value)
Definition: bytebuffer.h:67
uint8_t get_uint8()
Getters.
Definition: bytebuffer.cpp:84
void put_uint(uint64_t value, size_t length)
Definition: bytebuffer.cpp:136
void put_int24(int32_t value)
Definition: bytebuffer.h:107
uint32_t get_int24()
Definition: bytebuffer.cpp:107
void put_double(double value)
Definition: bytebuffer.cpp:157
int32_t get_int32()
Definition: bytebuffer.h:85
uint64_t get_uint(size_t length)
Definition: bytebuffer.cpp:88
static ByteBuffer wrap(int32_t value, Endian endianness=LITTLE)
Definition: bytebuffer.h:59
int16_t get_int16()
Definition: bytebuffer.h:83
static ByteBuffer wrap(int8_t value)
Definition: bytebuffer.h:55
void put_uint16(uint16_t value)
Definition: bytebuffer.h:100
size_t get_capacity() const
Definition: bytebuffer.h:116
void put_float(float value)
Definition: bytebuffer.cpp:152
ByteBuffer(size_t capacity, Endian endianness=LITTLE)
Create a new Bytebuffer with the given capacity.
Definition: bytebuffer.h:40
void put_int16(int32_t value)
Definition: bytebuffer.h:106
uint16_t get_uint16()
Definition: bytebuffer.h:74
size_t get_remaining() const
Definition: bytebuffer.h:119
std::string size_t len
Definition: helpers.h:292
void set_position(size_t position)
Definition: bytebuffer.cpp:70
static ByteBuffer wrap(int16_t value, Endian endianness=LITTLE)
Definition: bytebuffer.h:56
Endian get_endianness() const
Definition: bytebuffer.h:120
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void put_uint24(uint32_t value)
Definition: bytebuffer.h:101
float position
Definition: cover.h:14
void put_int32(int32_t value)
Definition: bytebuffer.h:108
A class modelled on the Java ByteBuffer class.
Definition: bytebuffer.h:33
uint64_t get_uint64()
Definition: bytebuffer.h:80
void put_vector(const std::vector< uint8_t > &value)
Definition: bytebuffer.cpp:162
void put_bool(bool value)
Definition: bytebuffer.h:113
uint32_t get_uint24()
Definition: bytebuffer.h:76
ByteBuffer(std::vector< uint8_t > const &data)
Definition: bytebuffer.h:136