45 using Filler = std::function<size_t(uint8_t *buf,
size_t size)>;
78 Bytes(
const char *str) :
Bytes((uint8_t *)str, strlen(str) + 1)
91 static_assert(std::is_integral<T>::value,
"only integral types are allowed");
138 _copy(rhs.mBuffer, rhs.mSize);
156 _copy((
const uint8_t *)rhs.c_str(), rhs.length());
179 size_t len = hexStr.length();
186 bytes._reserve(len / 2);
187 for (
size_t i = 0; i < len; i += 2) {
188 char buf[3] = {hexStr.charAt(i), hexStr.charAt(i + 1),
'\0'};
189 uint8_t b = strtol(buf,
nullptr, 16);
190 bytes.mBuffer[i / 2] = b;
207 return filler(mBuffer, mSize);
222 if (newSize < mSize) {
235 const uint8_t *
raw()
const {
252 if (mBuffer[mSize - 1] !=
'\0') {
254 mBuffer[mSize - 1] =
'\0';
267 return (
const char *)mBuffer;
278 return String(this->
c_str());
290 for (
size_t i = 0; i < mSize; i++) {
292 snprintf(buf,
sizeof(buf),
"%02X", mBuffer[i]);
324 return CRC32(mBuffer, mSize);
333 inline void _init(
void) {
343 void _invalidate(
void) {
360 bool _reserve(
size_t newSize) {
361 mBuffer = (uint8_t *)realloc(mBuffer, newSize);
362 if (mBuffer && (newSize > mSize)) {
363 memset(mBuffer + mSize, 0, newSize - mSize);
378 Bytes &_copy(
const uint8_t *data,
size_t size) {
379 if (_reserve(
size)) {
380 memcpy(mBuffer, data,
size);
uint32_t checksum() const
Calculates a checksum of the byte array.
Definition Bytes.h:323
void clean()
Deallocates the internal buffer.
Definition Bytes.h:312
String toString() const
Converts the byte array to an Arduino String.
Definition Bytes.h:277
Bytes & terminate()
Ensures the byte array is null-terminated.
Definition Bytes.h:246
Bytes & operator=(const Bytes &rhs)
Assignment operator.
Definition Bytes.h:135
const uint8_t * raw() const
Gets a const pointer to the raw byte array.
Definition Bytes.h:235
Bytes(const String &value)
Constructor from Arduino String.
Definition Bytes.h:113
const char * c_str() const
Gets the byte array as a C string.
Definition Bytes.h:266
Bytes(const uint8_t *data, size_t size)
Constructor from raw byte array.
Definition Bytes.h:62
Bytes & prune(size_t newSize)
Reduces the size of the buffer.
Definition Bytes.h:221
Bytes(const Bytes &value)
Copy constructor.
Definition Bytes.h:101
Bytes & operator=(const String &rhs)
Assignment from Arduino String.
Definition Bytes.h:154
size_t fill(Filler filler)
Fills the buffer using a provided filler function.
Definition Bytes.h:205
String toHexString() const
Converts the byte array to a hexadecimal string.
Definition Bytes.h:288
std::function< size_t(uint8_t *buf, size_t size)> Filler
Function type for filling byte arrays.
Definition Bytes.h:45
virtual ~Bytes()
Destructor.
Definition Bytes.h:123
static Bytes fromHexString(const String &hexStr)
Creates a Bytes object from a hexadecimal string.
Definition Bytes.h:178
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:303
Bytes()
Default constructor.
Definition Bytes.h:52
Bytes(T value)
Constructor from integral type.
Definition Bytes.h:90
Bytes(const char *str)
Constructor from C-string.
Definition Bytes.h:78
uint32_t CRC32(const void *data, size_t length, uint32_t crc=0)
Calculates CRC32-C (Castagnoli) checksum for data integrity verification.
Definition Common.h:72
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226