ESPHome 2026.5.0
Loading...
Searching...
No Matches
helpers.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <array>
5#include <cassert>
6#include <cmath>
7#include <cstdarg>
8#include <cstdint>
9#include <cstdio>
10#include <cstring>
11#include <functional>
12#include <iterator>
13#include <limits>
14#include <memory>
15#include <span>
16#include <string>
17#include <type_traits>
18#include <vector>
19#include <concepts>
20#include <strings.h>
21
24
25// Backward compatibility re-export of heap-allocating helpers.
26// These functions have moved to alloc_helpers.h. External components should
27// update their includes to use #include "esphome/core/alloc_helpers.h" directly.
28// This re-export will be removed in 2026.11.0.
30
31#ifdef USE_ESP8266
32#include <Esp.h>
33#include <pgmspace.h>
34#endif
35
36#ifdef USE_RP2040
37#include <Arduino.h>
38#endif
39
40#ifdef USE_ESP32
41#include <esp_heap_caps.h>
42#endif
43
44#if defined(USE_ESP32)
45#include <freertos/FreeRTOS.h>
46#include <freertos/semphr.h>
47#elif defined(USE_LIBRETINY)
48#include <FreeRTOS.h>
49#include <semphr.h>
50#endif
51
52#ifdef USE_HOST
53#include <mutex>
54#endif
55
56#define HOT __attribute__((hot))
57#define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
58#define ESPHOME_ALWAYS_INLINE __attribute__((always_inline))
59#define PACKED __attribute__((packed))
60
61namespace esphome {
62
63// Forward declaration to avoid circular dependency with string_ref.h
64class StringRef;
65
68
69// Keep "using" even after the removal of our backports, to avoid breaking existing code.
70using std::to_string;
71using std::is_trivially_copyable;
72using std::make_unique;
73using std::enable_if_t;
74using std::clamp;
75using std::is_invocable;
76#if __cpp_lib_bit_cast >= 201806
77using std::bit_cast;
78#else
80template<
81 typename To, typename From,
82 enable_if_t<sizeof(To) == sizeof(From) && is_trivially_copyable<From>::value && is_trivially_copyable<To>::value,
83 int> = 0>
84To bit_cast(const From &src) {
85 To dst;
86 memcpy(&dst, &src, sizeof(To));
87 return dst;
88}
89#endif
90
91// clang-format off
92inline float lerp(float completion, float start, float end) = delete; // Please use std::lerp. Notice that it has different order on arguments!
93// clang-format on
94
95// std::byteswap from C++23
96template<typename T> constexpr T byteswap(T n) {
97 T m;
98 for (size_t i = 0; i < sizeof(T); i++)
99 reinterpret_cast<uint8_t *>(&m)[i] = reinterpret_cast<uint8_t *>(&n)[sizeof(T) - 1 - i];
100 return m;
101}
102template<> constexpr uint8_t byteswap(uint8_t n) { return n; }
103#ifdef USE_LIBRETINY
104// LibreTiny's Beken framework redefines __builtin_bswap functions as non-constexpr
105template<> inline uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
106template<> inline uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
107template<> inline uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
108template<> inline int8_t byteswap(int8_t n) { return n; }
109template<> inline int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
110template<> inline int32_t byteswap(int32_t n) { return __builtin_bswap32(n); }
111template<> inline int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
112#else
113template<> constexpr uint16_t byteswap(uint16_t n) { return __builtin_bswap16(n); }
114template<> constexpr uint32_t byteswap(uint32_t n) { return __builtin_bswap32(n); }
115template<> constexpr uint64_t byteswap(uint64_t n) { return __builtin_bswap64(n); }
116template<> constexpr int8_t byteswap(int8_t n) { return n; }
117template<> constexpr int16_t byteswap(int16_t n) { return __builtin_bswap16(n); }
118template<> constexpr int32_t byteswap(int32_t n) { return __builtin_bswap32(n); }
119template<> constexpr int64_t byteswap(int64_t n) { return __builtin_bswap64(n); }
120#endif
121
123
126
130
131template<typename T> class ConstVector {
132 public:
133 constexpr ConstVector(const T *data, size_t size) : data_(data), size_(size) {}
134
135 const constexpr T &operator[](size_t i) const { return data_[i]; }
136 constexpr size_t size() const { return size_; }
137 constexpr bool empty() const { return size_ == 0; }
138
139 protected:
140 const T *data_;
141 size_t size_;
142};
143
147template<size_t InlineSize = 8> class SmallInlineBuffer {
148 public:
149 SmallInlineBuffer() = default;
151 if (!this->is_inline_())
152 delete[] this->heap_;
153 }
154
155 // Move constructor
156 SmallInlineBuffer(SmallInlineBuffer &&other) noexcept : len_(other.len_) {
157 if (other.is_inline_()) {
158 memcpy(this->inline_, other.inline_, this->len_);
159 } else {
160 this->heap_ = other.heap_;
161 other.heap_ = nullptr;
162 }
163 other.len_ = 0;
164 }
165
166 // Move assignment
168 if (this != &other) {
169 if (!this->is_inline_())
170 delete[] this->heap_;
171 this->len_ = other.len_;
172 if (other.is_inline_()) {
173 memcpy(this->inline_, other.inline_, this->len_);
174 } else {
175 this->heap_ = other.heap_;
176 other.heap_ = nullptr;
177 }
178 other.len_ = 0;
179 }
180 return *this;
181 }
182
183 // Disable copy (would need deep copy of heap data)
186
188 void set(const uint8_t *src, size_t size) {
189 // Free existing heap allocation if switching from heap to inline or different heap size
191 delete[] this->heap_;
192 this->heap_ = nullptr; // Defensive: prevent use-after-free if logic changes
193 }
194 // Allocate new heap buffer if needed
195 if (size > InlineSize && (this->is_inline_() || size != this->len_)) {
196 this->heap_ = new uint8_t[size]; // NOLINT(cppcoreguidelines-owning-memory)
197 }
198 this->len_ = size;
199 memcpy(this->data(), src, size);
200 }
201
202 uint8_t *data() { return this->is_inline_() ? this->inline_ : this->heap_; }
203 const uint8_t *data() const { return this->is_inline_() ? this->inline_ : this->heap_; }
204 size_t size() const { return this->len_; }
205
206 protected:
207 bool is_inline_() const { return this->len_ <= InlineSize; }
208
209 size_t len_{0};
210 union {
211 uint8_t inline_[InlineSize]{}; // Zero-init ensures clean initial state
212 uint8_t *heap_;
213 };
214};
215
217template<typename T, size_t N> class StaticVector {
218 public:
219 using value_type = T;
220 using iterator = typename std::array<T, N>::iterator;
221 using const_iterator = typename std::array<T, N>::const_iterator;
222 using reverse_iterator = std::reverse_iterator<iterator>;
223 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
224
225 private:
226 std::array<T, N> data_; // intentionally not value-initialized to avoid memset
227 size_t count_{0};
228
229 public:
230 // Default constructor
231 StaticVector() = default;
232
233 // Iterator range constructor
234 template<typename InputIt> StaticVector(InputIt first, InputIt last) {
235 while (first != last && count_ < N) {
236 data_[count_++] = *first++;
237 }
238 }
239
240 // Initializer list constructor
241 StaticVector(std::initializer_list<T> init) {
242 for (const auto &val : init) {
243 if (count_ >= N)
244 break;
245 data_[count_++] = val;
246 }
247 }
248
249 // Minimal vector-compatible interface - only what we actually use
250 void push_back(const T &value) {
251 if (count_ < N) {
252 data_[count_++] = value;
253 }
254 }
255
256 // Clear all elements
257 void clear() { count_ = 0; }
258
259 // Assign from iterator range
260 template<typename InputIt> void assign(InputIt first, InputIt last) {
261 count_ = 0;
262 while (first != last && count_ < N) {
263 data_[count_++] = *first++;
264 }
265 }
266
267 // Return reference to next element and increment count (with bounds checking)
269 if (count_ >= N) {
270 // Should never happen with proper size calculation
271 // Return reference to last element to avoid crash
272 return data_[N - 1];
273 }
274 return data_[count_++];
275 }
276
277 size_t size() const { return count_; }
278 bool empty() const { return count_ == 0; }
279
280 // Direct access to underlying data
281 T *data() { return data_.data(); }
282 const T *data() const { return data_.data(); }
283
284 T &operator[](size_t i) { return data_[i]; }
285 const T &operator[](size_t i) const { return data_[i]; }
286
287 // For range-based for loops
288 iterator begin() { return data_.begin(); }
289 iterator end() { return data_.begin() + count_; }
290 const_iterator begin() const { return data_.begin(); }
291 const_iterator end() const { return data_.begin() + count_; }
292
293 // Reverse iterators
298
299 // Conversion to std::span for compatibility with span-based APIs
300 operator std::span<T>() { return std::span<T>(data_.data(), count_); }
301 operator std::span<const T>() const { return std::span<const T>(data_.data(), count_); }
302};
303
311template<typename T, size_t N> class StaticRingBuffer {
312 using index_type = std::conditional_t<(N <= std::numeric_limits<uint8_t>::max()), uint8_t, uint16_t>;
313
314 public:
315 class Iterator {
316 public:
317 Iterator(StaticRingBuffer *buf, index_type pos) : buf_(buf), pos_(pos) {}
318 T &operator*() { return buf_->data_[(buf_->head_ + pos_) % N]; }
320 ++pos_;
321 return *this;
322 }
323 bool operator!=(const Iterator &other) const { return pos_ != other.pos_; }
324
325 private:
326 StaticRingBuffer *buf_;
327 index_type pos_;
328 };
329
331 public:
332 ConstIterator(const StaticRingBuffer *buf, index_type pos) : buf_(buf), pos_(pos) {}
333 const T &operator*() const { return buf_->data_[(buf_->head_ + pos_) % N]; }
335 ++pos_;
336 return *this;
337 }
338 bool operator!=(const ConstIterator &other) const { return pos_ != other.pos_; }
339
340 private:
341 const StaticRingBuffer *buf_;
342 index_type pos_;
343 };
344
345 bool push(const T &value) {
346 if (this->count_ >= N) {
347 return false;
348 }
349 this->data_[this->tail_] = value;
350 this->tail_ = (this->tail_ + 1) % N;
351 ++this->count_;
352 return true;
353 }
354
355 void pop() {
356 if (this->count_ > 0) {
357 this->head_ = (this->head_ + 1) % N;
358 --this->count_;
359 }
360 }
361
362 T &front() { return this->data_[this->head_]; }
363 const T &front() const { return this->data_[this->head_]; }
364 index_type size() const { return this->count_; }
365 bool empty() const { return this->count_ == 0; }
366
368 void clear() {
369 this->head_ = 0;
370 this->tail_ = 0;
371 this->count_ = 0;
372 }
373
374 Iterator begin() { return Iterator(this, 0); }
375 Iterator end() { return Iterator(this, this->count_); }
376 ConstIterator begin() const { return ConstIterator(this, 0); }
377 ConstIterator end() const { return ConstIterator(this, this->count_); }
378
379 protected:
380 T data_[N];
381 index_type head_{0};
382 index_type tail_{0};
383 index_type count_{0};
384};
385
390template<typename T, size_t MAX_CAPACITY = std::numeric_limits<uint16_t>::max()> class FixedRingBuffer {
391 using index_type = std::conditional_t<
392 (MAX_CAPACITY <= std::numeric_limits<uint8_t>::max()), uint8_t,
393 std::conditional_t<(MAX_CAPACITY <= std::numeric_limits<uint16_t>::max()), uint16_t, uint32_t>>;
394
395 public:
396 class Iterator {
397 public:
398 Iterator(FixedRingBuffer *buf, index_type pos) : buf_(buf), pos_(pos) {}
399 T &operator*() { return buf_->data_[(buf_->head_ + pos_) % buf_->capacity_]; }
401 ++pos_;
402 return *this;
403 }
404 bool operator!=(const Iterator &other) const { return pos_ != other.pos_; }
405
406 private:
407 FixedRingBuffer *buf_;
408 index_type pos_;
409 };
410
412 public:
413 ConstIterator(const FixedRingBuffer *buf, index_type pos) : buf_(buf), pos_(pos) {}
414 const T &operator*() const { return buf_->data_[(buf_->head_ + pos_) % buf_->capacity_]; }
416 ++pos_;
417 return *this;
418 }
419 bool operator!=(const ConstIterator &other) const { return pos_ != other.pos_; }
420
421 private:
422 const FixedRingBuffer *buf_;
423 index_type pos_;
424 };
425
426 FixedRingBuffer() = default;
428 if constexpr (std::is_trivially_copyable<T>::value && std::is_trivially_default_constructible<T>::value) {
429 ::operator delete(this->data_);
430 } else {
431 delete[] this->data_;
432 }
433 }
434
435 // Disable copy
438
440 void init(index_type capacity) {
441 if constexpr (std::is_trivially_copyable<T>::value && std::is_trivially_default_constructible<T>::value) {
442 // Raw allocation without initialization (elements are written before read)
443 // NOLINTNEXTLINE(bugprone-sizeof-expression)
444 this->data_ = static_cast<T *>(::operator new(capacity * sizeof(T)));
445 } else {
446 this->data_ = new T[capacity];
447 }
448 this->capacity_ = capacity;
449 }
450
452 bool push(const T &value) {
453 if (this->count_ >= this->capacity_)
454 return false;
455 this->data_[this->tail_] = value;
456 this->tail_ = (this->tail_ + 1) % this->capacity_;
457 ++this->count_;
458 return true;
459 }
460
462 void push_overwrite(const T &value) {
463 this->data_[this->tail_] = value;
464 this->tail_ = (this->tail_ + 1) % this->capacity_;
465 if (this->count_ >= this->capacity_) {
466 // Buffer full - advance head to drop oldest, count stays at capacity
467 this->head_ = this->tail_;
468 } else {
469 ++this->count_;
470 }
471 }
472
474 void pop() {
475 if (this->count_ > 0) {
476 this->head_ = (this->head_ + 1) % this->capacity_;
477 --this->count_;
478 }
479 }
480
481 T &front() { return this->data_[this->head_]; }
482 const T &front() const { return this->data_[this->head_]; }
483 index_type size() const { return this->count_; }
484 bool empty() const { return this->count_ == 0; }
485 index_type capacity() const { return this->capacity_; }
486 bool full() const { return this->count_ == this->capacity_; }
487
489 void clear() {
490 this->head_ = 0;
491 this->tail_ = 0;
492 this->count_ = 0;
493 }
494
495 Iterator begin() { return Iterator(this, 0); }
496 Iterator end() { return Iterator(this, this->count_); }
497 ConstIterator begin() const { return ConstIterator(this, 0); }
498 ConstIterator end() const { return ConstIterator(this, this->count_); }
499
500 protected:
501 T *data_{nullptr};
502 index_type head_{0};
503 index_type tail_{0};
504 index_type count_{0};
505 index_type capacity_{0};
506};
507
512template<typename T, size_t N> inline void init_array_from(std::array<T, N> &dest, std::initializer_list<T> src) {
513#ifdef ESPHOME_DEBUG
514 assert(src.size() == N);
515#endif
516 if constexpr (std::is_trivially_copyable_v<T>) {
517 __builtin_memcpy(dest.data(), src.begin(), N * sizeof(T));
518 } else {
519 size_t i = 0;
520 for (const auto &v : src) {
521 dest[i++] = v;
522 }
523 }
524}
525
529template<typename T> class FixedVector {
530 private:
531 T *data_{nullptr};
532 size_t size_{0};
533 size_t capacity_{0};
534
535 // Helper to destroy all elements without freeing memory
536 void destroy_elements_() {
537 // Only call destructors for non-trivially destructible types
538 if constexpr (!std::is_trivially_destructible<T>::value) {
539 for (size_t i = 0; i < size_; i++) {
540 data_[i].~T();
541 }
542 }
543 }
544
545 // Helper to destroy elements and free memory
546 void cleanup_() {
547 if (data_ != nullptr) {
548 destroy_elements_();
549 // Free raw memory
550 ::operator delete(data_);
551 }
552 }
553
554 // Helper to reset pointers after cleanup
555 void reset_() {
556 data_ = nullptr;
557 capacity_ = 0;
558 size_ = 0;
559 }
560
561 // Helper to assign from initializer list (shared by constructor and assignment operator)
562 void assign_from_initializer_list_(std::initializer_list<T> init_list) {
563 init(init_list.size());
564 size_t idx = 0;
565 for (const auto &item : init_list) {
566 new (data_ + idx) T(item);
567 ++idx;
568 }
569 size_ = init_list.size();
570 }
571
572 public:
573 FixedVector() = default;
574
577 FixedVector(std::initializer_list<T> init_list) { assign_from_initializer_list_(init_list); }
578
579 ~FixedVector() { cleanup_(); }
580
581 // Disable copy operations (avoid accidental expensive copies)
582 FixedVector(const FixedVector &) = delete;
584
585 // Enable move semantics (allows use in move-only containers like std::vector)
586 FixedVector(FixedVector &&other) noexcept : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
587 other.reset_();
588 }
589
590 // Allow conversion to std::vector
591 operator std::vector<T>() const { return {data_, data_ + size_}; }
592
593 FixedVector &operator=(FixedVector &&other) noexcept {
594 if (this != &other) {
595 // Delete our current data
596 cleanup_();
597 // Take ownership of other's data
598 data_ = other.data_;
599 size_ = other.size_;
600 capacity_ = other.capacity_;
601 // Leave other in valid empty state
602 other.reset_();
603 }
604 return *this;
605 }
606
609 FixedVector &operator=(std::initializer_list<T> init_list) {
610 cleanup_();
611 reset_();
612 assign_from_initializer_list_(init_list);
613 return *this;
614 }
615
616 // Allocate capacity - can be called multiple times to reinit
617 // IMPORTANT: After calling init(), you MUST use push_back() to add elements.
618 // Direct assignment via operator[] does NOT update the size counter.
619 void init(size_t n) {
620 cleanup_();
621 reset_();
622 if (n > 0) {
623 // Allocate raw memory without calling constructors
624 // sizeof(T) is correct here for any type T (value types, pointers, etc.)
625 // NOLINTNEXTLINE(bugprone-sizeof-expression)
626 data_ = static_cast<T *>(::operator new(n * sizeof(T)));
627 capacity_ = n;
628 }
629 }
630
631 // Clear the vector (destroy all elements, reset size to 0, keep capacity)
632 void clear() {
633 destroy_elements_();
634 size_ = 0;
635 }
636
637 // Release all memory (destroys elements and frees memory)
638 void release() {
639 cleanup_();
640 reset_();
641 }
642
646 void push_back(const T &value) {
647 if (size_ < capacity_) {
648 // Use placement new to construct the object in pre-allocated memory
649 new (&data_[size_]) T(value);
650 size_++;
651 }
652 }
653
657 void push_back(T &&value) {
658 if (size_ < capacity_) {
659 // Use placement new to move-construct the object in pre-allocated memory
660 new (&data_[size_]) T(std::move(value));
661 size_++;
662 }
663 }
664
669 template<typename... Args> T &emplace_back(Args &&...args) {
670 // Use placement new to construct the object in pre-allocated memory
671 new (&data_[size_]) T(std::forward<Args>(args)...);
672 size_++;
673 return data_[size_ - 1];
674 }
675
678 T &front() { return data_[0]; }
679 const T &front() const { return data_[0]; }
680
683 T &back() { return data_[size_ - 1]; }
684 const T &back() const { return data_[size_ - 1]; }
685
686 size_t size() const { return size_; }
687 bool empty() const { return size_ == 0; }
688 size_t capacity() const { return capacity_; }
689 bool full() const { return size_ == capacity_; }
690
693 T &operator[](size_t i) { return data_[i]; }
694 const T &operator[](size_t i) const { return data_[i]; }
695
698 T &at(size_t i) { return data_[i]; }
699 const T &at(size_t i) const { return data_[i]; }
700
701 // Iterator support for range-based for loops
702 T *begin() { return data_; }
703 T *end() { return data_ + size_; }
704 const T *begin() const { return data_; }
705 const T *end() const { return data_ + size_; }
706};
707
713template<size_t STACK_SIZE, typename T = uint8_t> class SmallBufferWithHeapFallback {
714 public:
716 if (size <= STACK_SIZE) {
717 this->buffer_ = this->stack_buffer_;
718 } else {
719 this->heap_buffer_ = new T[size];
720 this->buffer_ = this->heap_buffer_;
721 }
722 }
723 ~SmallBufferWithHeapFallback() { delete[] this->heap_buffer_; }
724
725 // Delete copy and move operations to prevent double-delete
730
731 T *get() { return this->buffer_; }
732
733 private:
734 T stack_buffer_[STACK_SIZE];
735 T *heap_buffer_{nullptr};
736 T *buffer_;
737};
738
740
743
747int8_t ilog10(float value);
748
752inline float pow10_int(int8_t exp) {
753 float result = 1.0f;
754 if (exp >= 0) {
755 for (int8_t i = 0; i < exp; i++)
756 result *= 10.0f;
757 } else {
758 for (int8_t i = exp; i < 0; i++)
759 result /= 10.0f;
760 }
761 return result;
762}
763
765template<typename T, typename U> T remap(U value, U min, U max, T min_out, T max_out) {
766 return (value - min) * (max_out - min_out) / (max - min) + min_out;
767}
768
770uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc = 0x00, uint8_t poly = 0x8C, bool msb_first = false);
771
773uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc = 0xffff, uint16_t reverse_poly = 0xa001,
774 bool refin = false, bool refout = false);
775uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc = 0, uint16_t poly = 0x1021, bool refin = false,
776 bool refout = false);
777
780uint32_t fnv1_hash(const char *str);
781inline uint32_t fnv1_hash(const std::string &str) { return fnv1_hash(str.c_str()); }
782
784constexpr uint32_t FNV1_OFFSET_BASIS = 2166136261UL;
786constexpr uint32_t FNV1_PRIME = 16777619UL;
787
789template<std::integral T> constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value) {
790 using UnsignedT = std::make_unsigned_t<T>;
791 UnsignedT uvalue = static_cast<UnsignedT>(value);
792 for (size_t i = 0; i < sizeof(T); i++) {
793 hash *= FNV1_PRIME;
794 hash ^= (uvalue >> (i * 8)) & 0xFF;
795 }
796 return hash;
797}
799constexpr uint32_t fnv1_hash_extend(uint32_t hash, const char *str) {
800 if (str) {
801 while (*str) {
802 hash *= FNV1_PRIME;
803 hash ^= *str++;
804 }
805 }
806 return hash;
807}
808inline uint32_t fnv1_hash_extend(uint32_t hash, const std::string &str) { return fnv1_hash_extend(hash, str.c_str()); }
809
811constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str) {
812 if (str) {
813 while (*str) {
814 hash ^= *str++;
815 hash *= FNV1_PRIME;
816 }
817 }
818 return hash;
819}
820inline uint32_t fnv1a_hash_extend(uint32_t hash, const std::string &str) {
821 return fnv1a_hash_extend(hash, str.c_str());
822}
824template<std::integral T> constexpr uint32_t fnv1a_hash_extend(uint32_t hash, T value) {
825 using UnsignedT = std::make_unsigned_t<T>;
826 UnsignedT uvalue = static_cast<UnsignedT>(value);
827 for (size_t i = 0; i < sizeof(T); i++) {
828 hash ^= (uvalue >> (i * 8)) & 0xFF;
829 hash *= FNV1_PRIME;
830 }
831 return hash;
832}
834constexpr uint32_t fnv1a_hash(const char *str) { return fnv1a_hash_extend(FNV1_OFFSET_BASIS, str); }
835inline uint32_t fnv1a_hash(const std::string &str) { return fnv1a_hash(str.c_str()); }
836
837// micros_to_millis<>() lives in its own lightweight header so hal.h can pull it
838// in for inline millis_64() without forcing every TU that includes hal.h to
839// also include the rest of helpers.h.
840
848float random_float();
851bool random_bytes(uint8_t *data, size_t len);
852
854
857
859constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb) {
860 return (static_cast<uint16_t>(msb) << 8) | (static_cast<uint16_t>(lsb));
861}
863constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3) {
864 return (static_cast<uint32_t>(byte1) << 16) | (static_cast<uint32_t>(byte2) << 8) | (static_cast<uint32_t>(byte3));
865}
867constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4) {
868 return (static_cast<uint32_t>(byte1) << 24) | (static_cast<uint32_t>(byte2) << 16) |
869 (static_cast<uint32_t>(byte3) << 8) | (static_cast<uint32_t>(byte4));
870}
871
873template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> constexpr T encode_value(const uint8_t *bytes) {
874 T val = 0;
875 for (size_t i = 0; i < sizeof(T); i++) {
876 val <<= 8;
877 val |= bytes[i];
878 }
879 return val;
880}
882template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
883constexpr T encode_value(const std::array<uint8_t, sizeof(T)> bytes) {
884 return encode_value<T>(bytes.data());
885}
887template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
888constexpr std::array<uint8_t, sizeof(T)> decode_value(T val) {
889 std::array<uint8_t, sizeof(T)> ret{};
890 for (size_t i = sizeof(T); i > 0; i--) {
891 ret[i - 1] = val & 0xFF;
892 val >>= 8;
893 }
894 return ret;
895}
896
898inline uint8_t reverse_bits(uint8_t x) {
899 x = ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
900 x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
901 x = ((x & 0xF0) >> 4) | ((x & 0x0F) << 4);
902 return x;
903}
905inline uint16_t reverse_bits(uint16_t x) {
906 return (reverse_bits(static_cast<uint8_t>(x & 0xFF)) << 8) | reverse_bits(static_cast<uint8_t>((x >> 8) & 0xFF));
907}
910 return (reverse_bits(static_cast<uint16_t>(x & 0xFFFF)) << 16) |
911 reverse_bits(static_cast<uint16_t>((x >> 16) & 0xFFFF));
912}
913
915template<typename T> constexpr T convert_big_endian(T val) {
916#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
917 return byteswap(val);
918#else
919 return val;
920#endif
921}
922
924template<typename T> constexpr T convert_little_endian(T val) {
925#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
926 return val;
927#else
928 return byteswap(val);
929#endif
930}
931
933
936
938bool str_equals_case_insensitive(const std::string &a, const std::string &b);
940bool str_equals_case_insensitive(StringRef a, StringRef b);
942inline bool str_equals_case_insensitive(const char *a, const char *b) { return strcasecmp(a, b) == 0; }
943inline bool str_equals_case_insensitive(const std::string &a, const char *b) { return strcasecmp(a.c_str(), b) == 0; }
944inline bool str_equals_case_insensitive(const char *a, const std::string &b) { return strcasecmp(a, b.c_str()) == 0; }
945
947bool str_startswith(const std::string &str, const std::string &start);
949bool str_endswith(const std::string &str, const std::string &end);
950
952bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len);
953inline bool str_endswith_ignore_case(const char *str, const char *suffix) {
954 return str_endswith_ignore_case(str, strlen(str), suffix, strlen(suffix));
955}
956inline bool str_endswith_ignore_case(const std::string &str, const char *suffix) {
957 return str_endswith_ignore_case(str.c_str(), str.size(), suffix, strlen(suffix));
958}
959
960// str_truncate moved to alloc_helpers.h - remove this include before 2026.11.0
961
962// str_until, str_lower_case, str_upper_case moved to alloc_helpers.h - remove this comment before 2026.11.0
963
965constexpr char to_snake_case_char(char c) { return (c == ' ') ? '_' : (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
966// str_snake_case moved to alloc_helpers.h - remove this comment before 2026.11.0
967
969constexpr char to_sanitized_char(char c) {
970 return (c == '-' || c == '_' || (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) ? c : '_';
971}
972
982char *str_sanitize_to(char *buffer, size_t buffer_size, const char *str);
983
985template<size_t N> inline char *str_sanitize_to(char (&buffer)[N], const char *str) {
986 return str_sanitize_to(buffer, N, str);
987}
988
989// str_sanitize moved to alloc_helpers.h - remove this comment before 2026.11.0
990
995inline uint32_t fnv1_hash_object_id(const char *str, size_t len) {
997 for (size_t i = 0; i < len; i++) {
998 hash *= FNV1_PRIME;
999 // Apply snake_case (space->underscore, uppercase->lowercase) then sanitize
1000 hash ^= static_cast<uint8_t>(to_sanitized_char(to_snake_case_char(str[i])));
1001 }
1002 return hash;
1003}
1004
1005// str_snprintf, str_sprintf moved to alloc_helpers.h - remove this comment before 2026.11.0
1006
1007#ifdef USE_ESP8266
1008// ESP8266: Use vsnprintf_P to keep format strings in flash (PROGMEM)
1009// Format strings must be wrapped with PSTR() macro
1016inline size_t buf_append_printf_p(char *buf, size_t size, size_t pos, PGM_P fmt, ...) {
1017 if (pos >= size) {
1018 return size;
1019 }
1020 va_list args;
1021 va_start(args, fmt);
1022 int written = vsnprintf_P(buf + pos, size - pos, fmt, args);
1023 va_end(args);
1024 if (written < 0) {
1025 return pos; // encoding error
1026 }
1027 return std::min(pos + static_cast<size_t>(written), size);
1028}
1029#define buf_append_printf(buf, size, pos, fmt, ...) buf_append_printf_p(buf, size, pos, PSTR(fmt), ##__VA_ARGS__)
1030#else
1038__attribute__((format(printf, 4, 5))) inline size_t buf_append_printf(char *buf, size_t size, size_t pos,
1039 const char *fmt, ...) {
1040 if (pos >= size) {
1041 return size;
1042 }
1043 va_list args;
1045 int written = vsnprintf(buf + pos, size - pos, fmt, args);
1047 if (written < 0) {
1048 return pos; // encoding error
1049 }
1050 return std::min(pos + static_cast<size_t>(written), size);
1051}
1052#endif
1053
1054#ifdef USE_ESP8266
1064inline size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str) {
1065 if (pos >= size) {
1066 return size;
1067 }
1068 size_t remaining = size - pos - 1; // reserve space for null terminator
1069 size_t len = strnlen_P(str, remaining);
1070 memcpy_P(buf + pos, str, len);
1071 pos += len;
1072 buf[pos] = '\0';
1073 return pos;
1074}
1078#define buf_append_str(buf, size, pos, str) buf_append_str_p(buf, size, pos, PSTR(str))
1079#else
1087inline size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str) {
1088 if (pos >= size) {
1089 return size;
1090 }
1091 size_t remaining = size - pos - 1; // reserve space for null terminator
1092 size_t len = 0;
1093 while (len < remaining && str[len] != '\0') {
1094 len++;
1095 }
1096 memcpy(buf + pos, str, len);
1097 pos += len;
1098 buf[pos] = '\0';
1099 return pos;
1100}
1101#endif
1102
1111std::string make_name_with_suffix(const std::string &name, char sep, const char *suffix_ptr, size_t suffix_len);
1112
1121std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr,
1122 size_t suffix_len);
1123
1133size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep,
1134 const char *suffix_ptr, size_t suffix_len);
1135
1137
1140
1142template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
1143optional<T> parse_number(const char *str) {
1144 char *end = nullptr;
1145 unsigned long value = ::strtoul(str, &end, 10); // NOLINT(google-runtime-int)
1146 if (end == str || *end != '\0' || value > std::numeric_limits<T>::max())
1147 return {};
1148 return value;
1149}
1151template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value), int> = 0>
1152optional<T> parse_number(const std::string &str) {
1153 return parse_number<T>(str.c_str());
1154}
1156template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
1157optional<T> parse_number(const char *str) {
1158 char *end = nullptr;
1159 signed long value = ::strtol(str, &end, 10); // NOLINT(google-runtime-int)
1160 if (end == str || *end != '\0' || value < std::numeric_limits<T>::min() || value > std::numeric_limits<T>::max())
1161 return {};
1162 return value;
1163}
1165template<typename T, enable_if_t<(std::is_integral<T>::value && std::is_signed<T>::value), int> = 0>
1166optional<T> parse_number(const std::string &str) {
1167 return parse_number<T>(str.c_str());
1168}
1170template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0> optional<T> parse_number(const char *str) {
1171 char *end = nullptr;
1172 float value = ::strtof(str, &end);
1173 if (end == str || *end != '\0' || value == HUGE_VALF)
1174 return {};
1175 return value;
1176}
1178template<typename T, enable_if_t<(std::is_same<T, float>::value), int> = 0>
1179optional<T> parse_number(const std::string &str) {
1180 return parse_number<T>(str.c_str());
1181}
1182
1194size_t parse_hex(const char *str, size_t len, uint8_t *data, size_t count);
1196inline bool parse_hex(const char *str, uint8_t *data, size_t count) {
1197 return parse_hex(str, strlen(str), data, count) == 2 * count;
1198}
1200inline bool parse_hex(const std::string &str, uint8_t *data, size_t count) {
1201 return parse_hex(str.c_str(), str.length(), data, count) == 2 * count;
1202}
1204inline bool parse_hex(const char *str, std::vector<uint8_t> &data, size_t count) {
1205 data.resize(count);
1206 return parse_hex(str, strlen(str), data.data(), count) == 2 * count;
1207}
1209inline bool parse_hex(const std::string &str, std::vector<uint8_t> &data, size_t count) {
1210 data.resize(count);
1211 return parse_hex(str.c_str(), str.length(), data.data(), count) == 2 * count;
1212}
1218template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
1219optional<T> parse_hex(const char *str, size_t len) {
1220 T val = 0;
1221 if (len > 2 * sizeof(T) || parse_hex(str, len, reinterpret_cast<uint8_t *>(&val), sizeof(T)) == 0)
1222 return {};
1223 return convert_big_endian(val);
1224}
1226template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const char *str) {
1227 return parse_hex<T>(str, strlen(str));
1228}
1230template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> optional<T> parse_hex(const std::string &str) {
1231 return parse_hex<T>(str.c_str(), str.length());
1232}
1233
1236static constexpr uint8_t INVALID_HEX_CHAR = 255;
1237
1238constexpr uint8_t parse_hex_char(char c) {
1239 if (c >= '0' && c <= '9')
1240 return c - '0';
1241 if (c >= 'A' && c <= 'F')
1242 return c - 'A' + 10;
1243 if (c >= 'a' && c <= 'f')
1244 return c - 'a' + 10;
1245 return INVALID_HEX_CHAR;
1246}
1247
1249ESPHOME_ALWAYS_INLINE inline char format_hex_char(uint8_t v, char base) { return v >= 10 ? base + (v - 10) : '0' + v; }
1250
1252ESPHOME_ALWAYS_INLINE inline char format_hex_char(uint8_t v) { return format_hex_char(v, 'a'); }
1253
1255ESPHOME_ALWAYS_INLINE inline char format_hex_pretty_char(uint8_t v) { return format_hex_char(v, 'A'); }
1256
1259inline char *int8_to_str(char *buf, int8_t val) {
1260 int32_t v = val;
1261 if (v < 0) {
1262 *buf++ = '-';
1263 v = -v;
1264 }
1265 if (v >= 100) {
1266 *buf++ = '1'; // int8 max is 128, so hundreds digit is always 1
1267 v -= 100;
1268 // Must write tens digit (even if 0) after hundreds
1269 int32_t tens = v / 10;
1270 *buf++ = '0' + tens;
1271 v -= tens * 10;
1272 } else if (v >= 10) {
1273 int32_t tens = v / 10;
1274 *buf++ = '0' + tens;
1275 v -= tens * 10;
1276 }
1277 *buf++ = '0' + v;
1278 return buf;
1279}
1280
1285inline char *buf_append_sep_str(char *buf, size_t remaining, char separator, const char *str, size_t str_len) {
1286 if (remaining < 2) {
1287 if (remaining >= 1) {
1288 *buf = '\0';
1289 }
1290 return buf;
1291 }
1292 *buf++ = separator;
1293 remaining--;
1294 size_t copy_len = std::min(str_len, remaining - 1);
1295 memcpy(buf, str, copy_len);
1296 buf += copy_len;
1297 *buf = '\0';
1298 return buf;
1299}
1300
1302inline uint32_t small_pow10(int8_t n) { return n == 3 ? 1000 : n == 2 ? 100 : n == 1 ? 10 : 1; }
1303
1305static constexpr size_t UINT32_MAX_STR_SIZE = 11;
1306
1309char *uint32_to_str_unchecked(char *buf, uint32_t val);
1310
1313inline size_t uint32_to_str(std::span<char, UINT32_MAX_STR_SIZE> buf, uint32_t val) {
1314 char *end = uint32_to_str_unchecked(buf.data(), val);
1315 *end = '\0';
1316 return static_cast<size_t>(end - buf.data());
1317}
1318
1322inline char *frac_to_str_unchecked(char *buf, uint32_t frac, uint32_t divisor) {
1323 while (divisor > 0) {
1324 *buf++ = '0' + static_cast<char>(frac / divisor);
1325 frac %= divisor;
1326 divisor /= 10;
1327 }
1328 return buf;
1329}
1330
1332char *format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length);
1333
1336template<size_t N> inline char *format_hex_to(char (&buffer)[N], const uint8_t *data, size_t length) {
1337 static_assert(N >= 3, "Buffer must hold at least one hex byte (3 chars)");
1338 return format_hex_to(buffer, N, data, length);
1339}
1340
1342template<size_t N, typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
1343inline char *format_hex_to(char (&buffer)[N], T val) {
1344 static_assert(N >= sizeof(T) * 2 + 1, "Buffer too small for type");
1346 return format_hex_to(buffer, reinterpret_cast<const uint8_t *>(&val), sizeof(T));
1347}
1348
1350template<size_t N> inline char *format_hex_to(char (&buffer)[N], const std::vector<uint8_t> &data) {
1351 return format_hex_to(buffer, data.data(), data.size());
1352}
1353
1355template<size_t N, size_t M> inline char *format_hex_to(char (&buffer)[N], const std::array<uint8_t, M> &data) {
1356 return format_hex_to(buffer, data.data(), data.size());
1357}
1358
1360constexpr size_t format_hex_size(size_t byte_count) { return byte_count * 2 + 1; }
1361
1363constexpr size_t format_hex_prefixed_size(size_t byte_count) { return byte_count * 2 + 3; }
1364
1366template<size_t N, typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
1367inline char *format_hex_prefixed_to(char (&buffer)[N], T val) {
1368 static_assert(N >= sizeof(T) * 2 + 3, "Buffer too small for prefixed hex");
1369 buffer[0] = '0';
1370 buffer[1] = 'x';
1372 format_hex_to(buffer + 2, N - 2, reinterpret_cast<const uint8_t *>(&val), sizeof(T));
1373 return buffer;
1374}
1375
1377template<size_t N> inline char *format_hex_prefixed_to(char (&buffer)[N], const uint8_t *data, size_t length) {
1378 static_assert(N >= 5, "Buffer must hold at least '0x' + one hex byte + null");
1379 buffer[0] = '0';
1380 buffer[1] = 'x';
1381 format_hex_to(buffer + 2, N - 2, data, length);
1382 return buffer;
1383}
1384
1386constexpr size_t format_hex_pretty_size(size_t byte_count) { return byte_count * 3; }
1387
1399char *format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator = ':');
1400
1402template<size_t N>
1403inline char *format_hex_pretty_to(char (&buffer)[N], const uint8_t *data, size_t length, char separator = ':') {
1404 static_assert(N >= 3, "Buffer must hold at least one hex byte");
1405 return format_hex_pretty_to(buffer, N, data, length, separator);
1406}
1407
1409template<size_t N>
1410inline char *format_hex_pretty_to(char (&buffer)[N], const std::vector<uint8_t> &data, char separator = ':') {
1411 return format_hex_pretty_to(buffer, data.data(), data.size(), separator);
1412}
1413
1415template<size_t N, size_t M>
1416inline char *format_hex_pretty_to(char (&buffer)[N], const std::array<uint8_t, M> &data, char separator = ':') {
1417 return format_hex_pretty_to(buffer, data.data(), data.size(), separator);
1418}
1419
1421constexpr size_t format_hex_pretty_uint16_size(size_t count) { return count * 5; }
1422
1436char *format_hex_pretty_to(char *buffer, size_t buffer_size, const uint16_t *data, size_t length, char separator = ':');
1437
1439template<size_t N>
1440inline char *format_hex_pretty_to(char (&buffer)[N], const uint16_t *data, size_t length, char separator = ':') {
1441 static_assert(N >= 5, "Buffer must hold at least one hex uint16_t");
1442 return format_hex_pretty_to(buffer, N, data, length, separator);
1443}
1444
1446static constexpr size_t MAC_ADDRESS_SIZE = 6;
1448static constexpr size_t MAC_ADDRESS_PRETTY_BUFFER_SIZE = format_hex_pretty_size(MAC_ADDRESS_SIZE);
1450static constexpr size_t MAC_ADDRESS_BUFFER_SIZE = MAC_ADDRESS_SIZE * 2 + 1;
1451
1453inline char *format_mac_addr_upper(const uint8_t *mac, char *output) {
1454 return format_hex_pretty_to(output, MAC_ADDRESS_PRETTY_BUFFER_SIZE, mac, MAC_ADDRESS_SIZE, ':');
1455}
1456
1458inline void format_mac_addr_lower_no_sep(const uint8_t *mac, char *output) {
1459 format_hex_to(output, MAC_ADDRESS_BUFFER_SIZE, mac, MAC_ADDRESS_SIZE);
1460}
1461
1462// format_mac_address_pretty, format_hex (all overloads) moved to alloc_helpers.h
1463// Remove this comment and the template overloads below before 2026.11.0
1464
1467template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_hex(T val) {
1469 return format_hex(reinterpret_cast<uint8_t *>(&val), sizeof(T));
1470}
1473template<std::size_t N> std::string format_hex(const std::array<uint8_t, N> &data) {
1474 return format_hex(data.data(), data.size());
1475}
1476
1477// format_hex_pretty (all overloads) moved to alloc_helpers.h
1478// Remove this comment and the template overload below before 2026.11.0
1479
1482template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
1483std::string format_hex_pretty(T val, char separator = '.', bool show_length = true) {
1485 return format_hex_pretty(reinterpret_cast<uint8_t *>(&val), sizeof(T), separator, show_length);
1486}
1487
1489constexpr size_t format_bin_size(size_t byte_count) { return byte_count * 8 + 1; }
1490
1510char *format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length);
1511
1513template<size_t N> inline char *format_bin_to(char (&buffer)[N], const uint8_t *data, size_t length) {
1514 static_assert(N >= 9, "Buffer must hold at least one binary byte (9 chars)");
1515 return format_bin_to(buffer, N, data, length);
1516}
1517
1534template<size_t N, typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0>
1535inline char *format_bin_to(char (&buffer)[N], T val) {
1536 static_assert(N >= sizeof(T) * 8 + 1, "Buffer too small for type");
1538 return format_bin_to(buffer, reinterpret_cast<const uint8_t *>(&val), sizeof(T));
1539}
1540
1541// format_bin moved to alloc_helpers.h - remove this comment and template overload before 2026.11.0
1542
1545template<typename T, enable_if_t<std::is_unsigned<T>::value, int> = 0> std::string format_bin(T val) {
1547 return format_bin(reinterpret_cast<uint8_t *>(&val), sizeof(T));
1548}
1549
1558ParseOnOffState parse_on_off(const char *str, const char *on = nullptr, const char *off = nullptr);
1559
1560// value_accuracy_to_string moved to alloc_helpers.h - remove this comment before 2026.11.0
1561
1563static constexpr size_t VALUE_ACCURACY_MAX_LEN = 64;
1564
1566size_t value_accuracy_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> buf, float value, int8_t accuracy_decimals);
1568size_t value_accuracy_with_uom_to_buf(std::span<char, VALUE_ACCURACY_MAX_LEN> buf, float value,
1569 int8_t accuracy_decimals, StringRef unit_of_measurement);
1570
1572int8_t step_to_accuracy_decimals(float step);
1573
1574// base64_encode (both overloads), base64_decode (vector overload) moved to alloc_helpers.h
1575// Remove this comment before 2026.11.0
1576size_t base64_decode(std::string const &encoded_string, uint8_t *buf, size_t buf_len);
1577size_t base64_decode(const uint8_t *encoded_data, size_t encoded_len, uint8_t *buf, size_t buf_len);
1578
1583bool base64_decode_int32_vector(const std::string &base64, std::vector<int32_t> &out);
1584
1586
1589
1591// Remove before 2026.9.0
1592ESPDEPRECATED("Use LightState::gamma_correct_lut() instead. Removed in 2026.9.0.", "2026.3.0")
1593float gamma_correct(float value, float gamma);
1595// Remove before 2026.9.0
1596ESPDEPRECATED("Use LightState::gamma_uncorrect_lut() instead. Removed in 2026.9.0.", "2026.3.0")
1597float gamma_uncorrect(float value, float gamma);
1598
1600void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value);
1602void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue);
1603
1605
1608
1610constexpr float celsius_to_fahrenheit(float value) { return value * 1.8f + 32.0f; }
1612constexpr float fahrenheit_to_celsius(float value) { return (value - 32.0f) / 1.8f; }
1613
1615
1618
1624template<typename... X> struct Callback;
1625
1626template<typename... Ts> struct Callback<void(Ts...)> {
1627 // The inline storage path stores callable bytes in ctx_ via memcpy.
1628 // sizeof equality with uintptr_t ensures void* can round-trip arbitrary bit patterns,
1629 // which combined with flat address spaces on all ESPHome targets means no trap representations.
1630 static_assert(sizeof(void *) == sizeof(std::uintptr_t), "void* must be the same size as uintptr_t");
1631
1632 void (*fn_)(void *, Ts...){nullptr};
1633 void *ctx_{nullptr};
1634
1636 void call(Ts... args) const { this->fn_(this->ctx_, std::forward<Ts>(args)...); }
1637
1640 template<typename F> static Callback create(F &&callable) {
1641 using DecayF = std::decay_t<F>;
1642 if constexpr (sizeof(DecayF) <= sizeof(void *) && std::is_trivially_copyable_v<DecayF>) {
1643 // Small trivial callable (e.g. [this]() { this->method(); }) - store inline in ctx.
1644 // Safe under C++20 (P0593R6): byte copy into aligned storage implicitly
1645 // creates objects of implicit-lifetime types (trivially copyable qualifies).
1646 Callback cb; // fn and ctx are zero-initialized by default
1647 // Decay callable to a local variable first. When F is a function reference
1648 // (e.g. void(&)(int)), &callable would point at machine code, not a pointer variable.
1649 DecayF decayed = std::forward<F>(callable);
1650 __builtin_memcpy(&cb.ctx_, &decayed, sizeof(DecayF));
1651 cb.fn_ = [](void *c, Ts... args) {
1652 alignas(DecayF) char buf[sizeof(DecayF)];
1653 __builtin_memcpy(buf, &c, sizeof(DecayF));
1654 (*std::launder(reinterpret_cast<DecayF *>(buf)))(args...);
1655 };
1656 return cb;
1657 } else {
1658 // Large or non-trivial callable - heap allocate.
1659 // Intentionally never freed: callbacks in ESPHome are registered during setup()
1660 // and live for device lifetime. Same lifetime as the previous std::function approach.
1661 auto *stored = new DecayF(std::forward<F>(callable));
1662 return {[](void *c, Ts... args) { (*static_cast<DecayF *>(c))(args...); }, static_cast<void *>(stored)};
1663 }
1664 }
1665};
1666
1668void *callback_manager_grow(void *data, uint16_t size, uint16_t &capacity, size_t elem_size);
1669
1670template<typename... X> class CallbackManager;
1671
1683template<typename... Ts> class CallbackManager<void(Ts...)> {
1684 using CbType = Callback<void(Ts...)>;
1685 static_assert(std::is_trivially_copyable_v<CbType>, "Callback must be trivially copyable");
1686
1687 public:
1688 CallbackManager() = default;
1689 ~CallbackManager() { ::operator delete(this->data_); }
1690
1691 // Non-copyable (would alias data_), movable (for std::map support)
1695 : data_(other.data_), size_(other.size_), capacity_(other.capacity_) {
1696 other.data_ = nullptr;
1697 other.size_ = 0;
1698 other.capacity_ = 0;
1699 }
1701 std::swap(this->data_, other.data_);
1702 std::swap(this->size_, other.size_);
1703 std::swap(this->capacity_, other.capacity_);
1704 return *this;
1705 }
1706
1709 template<typename F> void add(F &&callback) { this->add_(CbType::create(std::forward<F>(callback))); }
1710
1712 inline void ESPHOME_ALWAYS_INLINE call(const Ts &...args) {
1713 if (this->size_ != 0) {
1714 for (auto *it = this->data_, *end = it + this->size_; it != end; ++it) {
1715 it->call(args...);
1716 }
1717 }
1718 }
1719 uint16_t size() const { return this->size_; }
1720
1722 void operator()(const Ts &...args) { this->call(args...); }
1723
1724 protected:
1725 template<typename...> friend class LazyCallbackManager;
1728 void add_(CbType cb) {
1729 if (this->size_ == this->capacity_) {
1730 this->data_ =
1731 static_cast<CbType *>(callback_manager_grow(this->data_, this->size_, this->capacity_, sizeof(CbType)));
1732 }
1733 this->data_[this->size_++] = cb;
1734 }
1735 CbType *data_{nullptr};
1736 uint16_t size_{0};
1737 uint16_t capacity_{0};
1738};
1739
1748template<size_t N, typename... X> class StaticCallbackManager;
1749
1750template<size_t N, typename... Ts> class StaticCallbackManager<N, void(Ts...)> {
1751 public:
1754 template<typename F> void add(F &&callback) { this->add_(Callback<void(Ts...)>::create(std::forward<F>(callback))); }
1755
1757 void call(Ts... args) {
1758 for (auto &cb : this->callbacks_)
1759 cb.call(args...);
1760 }
1761 size_t size() const { return this->callbacks_.size(); }
1762
1764 void operator()(Ts... args) { call(args...); }
1765
1766 protected:
1768 void add_(Callback<void(Ts...)> cb) { this->callbacks_.push_back(cb); }
1770};
1771
1772template<typename... X> class LazyCallbackManager;
1773
1789template<typename... Ts> class LazyCallbackManager<void(Ts...)> {
1790 public:
1794 ~LazyCallbackManager() { delete this->callbacks_; }
1795
1796 // Non-copyable and non-movable (entities are never copied or moved)
1801
1803 template<typename F> void add(F &&callback) { this->add_(Callback<void(Ts...)>::create(std::forward<F>(callback))); }
1804
1806 void call(Ts... args) {
1807 if (this->callbacks_) {
1808 this->callbacks_->call(args...);
1809 }
1810 }
1811
1813 size_t size() const { return this->callbacks_ ? this->callbacks_->size() : 0; }
1814
1816 bool empty() const { return !this->callbacks_ || this->callbacks_->size() == 0; }
1817
1819 void operator()(Ts... args) { this->call(args...); }
1820
1821 protected:
1823 void add_(Callback<void(Ts...)> cb) {
1824 if (!this->callbacks_) {
1825 this->callbacks_ = new CallbackManager<void(Ts...)>();
1826 }
1827 this->callbacks_->add_(cb);
1828 }
1829 CallbackManager<void(Ts...)> *callbacks_{nullptr};
1830};
1831
1833template<typename T> class Deduplicator {
1834 public:
1836 bool next(T value) {
1837 if (this->has_value_ && !this->value_unknown_ && this->last_value_ == value) {
1838 return false;
1839 }
1840 this->has_value_ = true;
1841 this->value_unknown_ = false;
1842 this->last_value_ = value;
1843 return true;
1844 }
1847 bool ret = !this->value_unknown_;
1848 this->value_unknown_ = true;
1849 return ret;
1850 }
1852 bool has_value() const { return this->has_value_; }
1853
1854 protected:
1855 bool has_value_{false};
1856 bool value_unknown_{false};
1858};
1859
1861template<typename T> class Parented {
1862 public:
1864 Parented(T *parent) : parent_(parent) {}
1865
1867 T *get_parent() const { return parent_; }
1869 void set_parent(T *parent) { parent_ = parent; }
1870
1871 protected:
1872 T *parent_{nullptr};
1873};
1874
1876
1879
1884class Mutex {
1885 public:
1886 Mutex(const Mutex &) = delete;
1887 Mutex &operator=(const Mutex &) = delete;
1888
1889#if defined(USE_ESP8266) || defined(USE_RP2040)
1890 // Single-threaded platforms: inline no-ops so the compiler eliminates all call overhead.
1891 Mutex() = default;
1892 ~Mutex() = default;
1893 void lock() {}
1894 bool try_lock() { return true; }
1895 void unlock() {}
1896#elif defined(USE_ESP32) || defined(USE_LIBRETINY)
1897 // FreeRTOS platforms: inline to avoid out-of-line call overhead.
1898 Mutex() { handle_ = xSemaphoreCreateMutex(); }
1899 ~Mutex() = default;
1900 void lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
1901 bool try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
1902 void unlock() { xSemaphoreGive(this->handle_); }
1903
1904 private:
1905 SemaphoreHandle_t handle_;
1906#else
1907 Mutex();
1908 ~Mutex();
1909 void lock();
1910 bool try_lock();
1911 void unlock();
1912
1913 private:
1914 // d-pointer to store private data on new platforms
1915 void *handle_; // NOLINT(clang-diagnostic-unused-private-field)
1916#endif
1917};
1918
1924 public:
1925 LockGuard(Mutex &mutex) : mutex_(mutex) { mutex_.lock(); }
1926 ~LockGuard() { mutex_.unlock(); }
1927
1928 private:
1929 Mutex &mutex_;
1930};
1931
1953 public:
1954 InterruptLock();
1956
1957 protected:
1958#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_ZEPHYR)
1960#endif
1961};
1962
1972 public:
1973 LwIPLock(const LwIPLock &) = delete;
1974 LwIPLock &operator=(const LwIPLock &) = delete;
1975
1976#if defined(USE_ESP32) || defined(USE_RP2040)
1977 // Platforms with potential lwIP core locking — out-of-line implementations in helpers.cpp
1978 LwIPLock();
1979 ~LwIPLock();
1980#else
1981 // No lwIP core locking — inline no-ops (empty bodies instead of = default
1982 // to prevent clang-tidy unused-variable warnings at call sites)
1985#endif
1986};
1987
1994 public:
1996 void start();
1998 void stop();
1999
2001 static bool is_high_frequency() { return num_requests > 0; }
2002
2003 protected:
2004 bool started_{false};
2005 static uint8_t num_requests; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
2006};
2007
2009void get_mac_address_raw(uint8_t *mac); // NOLINT(readability-non-const-parameter)
2010
2011// get_mac_address, get_mac_address_pretty moved to alloc_helpers.h - remove this comment before 2026.11.0
2012
2016void get_mac_address_into_buffer(std::span<char, MAC_ADDRESS_BUFFER_SIZE> buf);
2017
2021const char *get_mac_address_pretty_into_buffer(std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buf);
2022
2023#ifdef USE_ESP32
2025void set_mac_address(uint8_t *mac);
2026#endif
2027
2031
2034bool mac_address_is_valid(const uint8_t *mac);
2035
2038
2040
2043
2053template<class T> class RAMAllocator {
2054 public:
2055 using value_type = T;
2056
2057 enum Flags {
2058 NONE = 0, // Perform external allocation and fall back to internal memory
2059 ALLOC_EXTERNAL = 1 << 0, // Perform external allocation only.
2060 ALLOC_INTERNAL = 1 << 1, // Perform internal allocation only.
2061 ALLOW_FAILURE = 1 << 2, // Does nothing. Kept for compatibility.
2062 PREFER_INTERNAL = 1 << 3, // Perform internal allocation and fall back to external memory
2063 };
2064
2065 constexpr RAMAllocator() = default;
2066 constexpr RAMAllocator(uint8_t flags) {
2067 if (flags & PREFER_INTERNAL) {
2069 return;
2070 }
2071 const uint8_t alloc_bits = flags & (ALLOC_INTERNAL | ALLOC_EXTERNAL);
2072 if (alloc_bits != 0) {
2073 this->flags_ = alloc_bits;
2074 return;
2075 }
2076 this->flags_ = ALLOC_INTERNAL | ALLOC_EXTERNAL;
2077 }
2078 template<class U> constexpr RAMAllocator(const RAMAllocator<U> &other) : flags_{other.flags_} {}
2079
2080 T *allocate(size_t n) { return this->allocate(n, sizeof(T)); }
2081
2082 T *allocate(size_t n, size_t manual_size) {
2083 size_t size = n * manual_size;
2084 T *ptr = nullptr;
2085#ifdef USE_ESP32
2086 const auto caps = this->get_caps_();
2087 ptr = static_cast<T *>(heap_caps_malloc_prefer(size, 2, caps[0], caps[1]));
2088#else
2089 // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported
2090 ptr = static_cast<T *>(malloc(size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
2091#endif
2092 return ptr;
2093 }
2094
2095 T *reallocate(T *p, size_t n) { return this->reallocate(p, n, sizeof(T)); }
2096
2097 T *reallocate(T *p, size_t n, size_t manual_size) {
2098 size_t size = n * manual_size;
2099 T *ptr = nullptr;
2100#ifdef USE_ESP32
2101 const auto caps = this->get_caps_();
2102 ptr = static_cast<T *>(heap_caps_realloc_prefer(p, size, 2, caps[0], caps[1]));
2103#else
2104 // Ignore ALLOC_EXTERNAL/ALLOC_INTERNAL flags if external allocation is not supported
2105 ptr = static_cast<T *>(realloc(p, size)); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
2106#endif
2107 return ptr;
2108 }
2109
2110 void deallocate(T *p, size_t n) {
2111 free(p); // NOLINT(cppcoreguidelines-owning-memory,cppcoreguidelines-no-malloc)
2112 }
2113
2117 size_t get_free_heap_size() const {
2118#ifdef USE_ESP8266
2119 return ESP.getFreeHeap(); // NOLINT(readability-static-accessed-through-instance)
2120#elif defined(USE_ESP32)
2121 auto max_internal =
2122 this->flags_ & ALLOC_INTERNAL ? heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) : 0;
2123 auto max_external =
2124 this->flags_ & ALLOC_EXTERNAL ? heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM) : 0;
2125 return max_internal + max_external;
2126#elif defined(USE_RP2040)
2127 return ::rp2040.getFreeHeap();
2128#elif defined(USE_LIBRETINY)
2129 return lt_heap_get_free();
2130#else
2131 return 100000;
2132#endif
2133 }
2134
2139#ifdef USE_ESP8266
2140 return ESP.getMaxFreeBlockSize(); // NOLINT(readability-static-accessed-through-instance)
2141#elif defined(USE_ESP32)
2142 auto max_internal =
2143 this->flags_ & ALLOC_INTERNAL ? heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL) : 0;
2144 auto max_external =
2145 this->flags_ & ALLOC_EXTERNAL ? heap_caps_get_largest_free_block(MALLOC_CAP_8BIT | MALLOC_CAP_SPIRAM) : 0;
2146 return std::max(max_internal, max_external);
2147#else
2148 return this->get_free_heap_size();
2149#endif
2150 }
2151
2152 private:
2153#ifdef USE_ESP32
2159 std::array<uint32_t, 2> get_caps_() const {
2160 constexpr uint32_t external_caps = MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT;
2161 constexpr uint32_t internal_caps = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT;
2162 if (this->flags_ & PREFER_INTERNAL) {
2163 return {internal_caps, external_caps};
2164 }
2165 const uint32_t primary = (this->flags_ & ALLOC_EXTERNAL) ? external_caps : internal_caps;
2166 const uint32_t fallback = (this->flags_ & ALLOC_INTERNAL) ? internal_caps : external_caps;
2167 return {primary, fallback};
2168 }
2169#endif
2170
2171 uint8_t flags_{ALLOC_INTERNAL | ALLOC_EXTERNAL};
2172};
2173
2174template<class T> using ExternalRAMAllocator = RAMAllocator<T>;
2175
2180template<typename T, typename U>
2181concept comparable_with = requires(T a, U b) {
2182 { a > b } -> std::convertible_to<bool>;
2183 { a < b } -> std::convertible_to<bool>;
2184};
2185
2186template<std::totally_ordered T, comparable_with<T> U> T clamp_at_least(T value, U min) {
2187 if (value < min)
2188 return min;
2189 return value;
2190}
2191template<std::totally_ordered T, comparable_with<T> U> T clamp_at_most(T value, U max) {
2192 if (value > max)
2193 return max;
2194 return value;
2195}
2196
2199
2204template<typename T, enable_if_t<!std::is_pointer<T>::value, int> = 0> T id(T value) { return value; }
2209template<typename T, enable_if_t<std::is_pointer<T *>::value, int> = 0> T &id(T *value) { return *value; }
2210
2212
2213} // namespace esphome
Heap-allocating helper functions.
uint8_t m
Definition bl0906.h:1
void ESPHOME_ALWAYS_INLINE call(const Ts &...args)
Call all callbacks in this manager.
Definition helpers.h:1712
CallbackManager & operator=(const CallbackManager &)=delete
void operator()(const Ts &...args)
Call all callbacks in this manager.
Definition helpers.h:1722
CallbackManager & operator=(CallbackManager &&other) noexcept
Definition helpers.h:1700
void add(F &&callback)
Add any callable.
Definition helpers.h:1709
CallbackManager(CallbackManager &&other) noexcept
Definition helpers.h:1694
void add_(CbType cb)
Non-template core to avoid code duplication per lambda type.
Definition helpers.h:1728
CallbackManager(const CallbackManager &)=delete
Lightweight read-only view over a const array stored in RODATA (will typically be in flash memory) Av...
Definition helpers.h:131
const constexpr T & operator[](size_t i) const
Definition helpers.h:135
constexpr bool empty() const
Definition helpers.h:137
constexpr ConstVector(const T *data, size_t size)
Definition helpers.h:133
constexpr size_t size() const
Definition helpers.h:136
Helper class to deduplicate items in a series of values.
Definition helpers.h:1833
bool next(T value)
Feeds the next item in the series to the deduplicator and returns false if this is a duplicate.
Definition helpers.h:1836
bool has_value() const
Returns true if this deduplicator has processed any items.
Definition helpers.h:1852
bool next_unknown()
Returns true if the deduplicator's value was previously known.
Definition helpers.h:1846
bool operator!=(const ConstIterator &other) const
Definition helpers.h:419
ConstIterator(const FixedRingBuffer *buf, index_type pos)
Definition helpers.h:413
bool operator!=(const Iterator &other) const
Definition helpers.h:404
Iterator(FixedRingBuffer *buf, index_type pos)
Definition helpers.h:398
Fixed-capacity circular buffer - allocates once at runtime, never reallocates.
Definition helpers.h:390
FixedRingBuffer & operator=(const FixedRingBuffer &)=delete
ConstIterator begin() const
Definition helpers.h:497
bool push(const T &value)
Push a value. Returns false if full.
Definition helpers.h:452
const T & front() const
Definition helpers.h:482
index_type capacity() const
Definition helpers.h:485
void push_overwrite(const T &value)
Push a value, overwriting the oldest if full.
Definition helpers.h:462
void init(index_type capacity)
Allocate capacity - can only be called once.
Definition helpers.h:440
void pop()
Remove the oldest element.
Definition helpers.h:474
void clear()
Clear all elements (reset to empty, keep capacity)
Definition helpers.h:489
FixedRingBuffer(const FixedRingBuffer &)=delete
index_type size() const
Definition helpers.h:483
ConstIterator end() const
Definition helpers.h:498
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
Definition helpers.h:529
const T & at(size_t i) const
Definition helpers.h:699
FixedVector(FixedVector &&other) noexcept
Definition helpers.h:586
FixedVector(std::initializer_list< T > init_list)
Constructor from initializer list - allocates exact size needed This enables brace initialization: Fi...
Definition helpers.h:577
const T * begin() const
Definition helpers.h:704
bool full() const
Definition helpers.h:689
FixedVector & operator=(std::initializer_list< T > init_list)
Assignment from initializer list - avoids temporary and move overhead This enables: FixedVector<int> ...
Definition helpers.h:609
T & front()
Access first element (no bounds checking - matches std::vector behavior) Caller must ensure vector is...
Definition helpers.h:678
const T & operator[](size_t i) const
Definition helpers.h:694
T & operator[](size_t i)
Access element without bounds checking (matches std::vector behavior) Caller must ensure index is val...
Definition helpers.h:693
size_t capacity() const
Definition helpers.h:688
T & back()
Access last element (no bounds checking - matches std::vector behavior) Caller must ensure vector is ...
Definition helpers.h:683
bool empty() const
Definition helpers.h:687
FixedVector & operator=(const FixedVector &)=delete
FixedVector(const FixedVector &)=delete
void push_back(T &&value)
Add element by move without bounds checking Caller must ensure sufficient capacity was allocated via ...
Definition helpers.h:657
T & emplace_back(Args &&...args)
Emplace element without bounds checking - constructs in-place with arguments Caller must ensure suffi...
Definition helpers.h:669
size_t size() const
Definition helpers.h:686
const T & front() const
Definition helpers.h:679
const T & back() const
Definition helpers.h:684
const T * end() const
Definition helpers.h:705
FixedVector & operator=(FixedVector &&other) noexcept
Definition helpers.h:593
T & at(size_t i)
Access element with bounds checking (matches std::vector behavior) Note: No exception thrown on out o...
Definition helpers.h:698
void push_back(const T &value)
Add element without bounds checking Caller must ensure sufficient capacity was allocated via init() S...
Definition helpers.h:646
void init(size_t n)
Definition helpers.h:619
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:1993
static bool is_high_frequency()
Check whether the loop is running continuously.
Definition helpers.h:2001
void stop()
Stop running the loop continuously.
Definition helpers.cpp:736
void start()
Start running the loop continuously.
Definition helpers.cpp:730
Helper class to disable interrupts.
Definition helpers.h:1952
LazyCallbackManager & operator=(const LazyCallbackManager &)=delete
LazyCallbackManager(const LazyCallbackManager &)=delete
size_t size() const
Return the number of registered callbacks.
Definition helpers.h:1813
void add_(Callback< void(Ts...)> cb)
Non-template core to avoid code duplication per lambda type.
Definition helpers.h:1823
void add(F &&callback)
Add any callable. Allocates the underlying CallbackManager on first use.
Definition helpers.h:1803
void operator()(Ts... args)
Call all callbacks in this manager.
Definition helpers.h:1819
LazyCallbackManager & operator=(LazyCallbackManager &&)=delete
~LazyCallbackManager()
Destructor - clean up allocated CallbackManager if any.
Definition helpers.h:1794
void call(Ts... args)
Call all callbacks in this manager. No-op if no callbacks registered.
Definition helpers.h:1806
bool empty() const
Check if any callbacks are registered.
Definition helpers.h:1816
LazyCallbackManager(LazyCallbackManager &&)=delete
Helper class that wraps a mutex with a RAII-style API.
Definition helpers.h:1923
LockGuard(Mutex &mutex)
Definition helpers.h:1925
Helper class to lock the lwIP TCPIP core when making lwIP API calls from non-TCPIP threads.
Definition helpers.h:1971
LwIPLock(const LwIPLock &)=delete
LwIPLock & operator=(const LwIPLock &)=delete
Mutex implementation, with API based on the unavailable std::mutex.
Definition helpers.h:1884
~Mutex()=default
Definition helpers.cpp:36
void unlock()
Definition helpers.h:1895
Mutex()=default
Definition helpers.cpp:35
bool try_lock()
Definition helpers.h:1894
Mutex(const Mutex &)=delete
Mutex & operator=(const Mutex &)=delete
Helper class to easily give an object a parent of type T.
Definition helpers.h:1861
T * get_parent() const
Get the parent of this object.
Definition helpers.h:1867
Parented(T *parent)
Definition helpers.h:1864
void set_parent(T *parent)
Set the parent of this object.
Definition helpers.h:1869
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:2053
constexpr RAMAllocator(uint8_t flags)
Definition helpers.h:2066
T * reallocate(T *p, size_t n, size_t manual_size)
Definition helpers.h:2097
size_t get_free_heap_size() const
Return the total heap space available via this allocator.
Definition helpers.h:2117
T * reallocate(T *p, size_t n)
Definition helpers.h:2095
void deallocate(T *p, size_t n)
Definition helpers.h:2110
size_t get_max_free_block_size() const
Return the maximum size block this allocator could allocate.
Definition helpers.h:2138
T * allocate(size_t n)
Definition helpers.h:2080
constexpr RAMAllocator(const RAMAllocator< U > &other)
Definition helpers.h:2078
T * allocate(size_t n, size_t manual_size)
Definition helpers.h:2082
constexpr RAMAllocator()=default
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:713
SmallBufferWithHeapFallback(const SmallBufferWithHeapFallback &)=delete
SmallBufferWithHeapFallback & operator=(SmallBufferWithHeapFallback &&)=delete
SmallBufferWithHeapFallback & operator=(const SmallBufferWithHeapFallback &)=delete
SmallBufferWithHeapFallback(SmallBufferWithHeapFallback &&)=delete
Small buffer optimization - stores data inline when small, heap-allocates for large data This avoids ...
Definition helpers.h:147
SmallInlineBuffer(const SmallInlineBuffer &)=delete
bool is_inline_() const
Definition helpers.h:207
void set(const uint8_t *src, size_t size)
Set buffer contents, allocating heap if needed.
Definition helpers.h:188
SmallInlineBuffer & operator=(const SmallInlineBuffer &)=delete
size_t size() const
Definition helpers.h:204
uint8_t inline_[InlineSize]
Definition helpers.h:211
SmallInlineBuffer & operator=(SmallInlineBuffer &&other) noexcept
Definition helpers.h:167
const uint8_t * data() const
Definition helpers.h:203
SmallInlineBuffer(SmallInlineBuffer &&other) noexcept
Definition helpers.h:156
void add(F &&callback)
Add any callable.
Definition helpers.h:1754
void call(Ts... args)
Call all callbacks in this manager.
Definition helpers.h:1757
void add_(Callback< void(Ts...)> cb)
Non-template core to avoid code duplication per lambda type.
Definition helpers.h:1768
StaticVector< Callback< void(Ts...)>, N > callbacks_
Definition helpers.h:1769
void operator()(Ts... args)
Call all callbacks in this manager.
Definition helpers.h:1764
CallbackManager backed by StaticVector for compile-time-known callback counts.
Definition helpers.h:1748
ConstIterator(const StaticRingBuffer *buf, index_type pos)
Definition helpers.h:332
bool operator!=(const ConstIterator &other) const
Definition helpers.h:338
bool operator!=(const Iterator &other) const
Definition helpers.h:323
Iterator(StaticRingBuffer *buf, index_type pos)
Definition helpers.h:317
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:311
bool push(const T &value)
Definition helpers.h:345
ConstIterator begin() const
Definition helpers.h:376
ConstIterator end() const
Definition helpers.h:377
index_type size() const
Definition helpers.h:364
const T & front() const
Definition helpers.h:363
void clear()
Clear all elements (reset to empty)
Definition helpers.h:368
Minimal static vector - saves memory by avoiding std::vector overhead.
Definition helpers.h:217
const_reverse_iterator rend() const
Definition helpers.h:297
size_t size() const
Definition helpers.h:277
reverse_iterator rbegin()
Definition helpers.h:294
const T & operator[](size_t i) const
Definition helpers.h:285
reverse_iterator rend()
Definition helpers.h:295
void push_back(const T &value)
Definition helpers.h:250
bool empty() const
Definition helpers.h:278
void assign(InputIt first, InputIt last)
Definition helpers.h:260
const_reverse_iterator rbegin() const
Definition helpers.h:296
T & operator[](size_t i)
Definition helpers.h:284
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition helpers.h:223
typename std::array< T, N >::iterator iterator
Definition helpers.h:220
typename std::array< T, N >::const_iterator const_iterator
Definition helpers.h:221
std::reverse_iterator< iterator > reverse_iterator
Definition helpers.h:222
const T * data() const
Definition helpers.h:282
const_iterator end() const
Definition helpers.h:291
StaticVector(InputIt first, InputIt last)
Definition helpers.h:234
StaticVector(std::initializer_list< T > init)
Definition helpers.h:241
const_iterator begin() const
Definition helpers.h:290
struct @65::@66 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:32
Functions to constrain the range of arithmetic values.
Definition helpers.h:2181
uint16_t flags
uint16_t id
mopeka_std_values val[3]
size_t buf_append_str_p(char *buf, size_t size, size_t pos, PGM_P str)
Safely append a PROGMEM string to buffer, returning new position (capped at size).
Definition helpers.h:1064
T clamp_at_most(T value, U max)
Definition helpers.h:2191
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
Definition helpers.cpp:20
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
Definition helpers.h:1087
constexpr uint32_t fnv1a_hash_extend(uint32_t hash, const char *str)
Extend a FNV-1a hash with additional string data.
Definition helpers.h:811
float random_float()
Return a random float between 0 and 1.
Definition helpers.cpp:198
const char int const __FlashStringHelper * format
Definition log.h:74
ESPHOME_ALWAYS_INLINE char format_hex_char(uint8_t v, char base)
Convert a nibble (0-15) to hex char with specified base ('a' for lowercase, 'A' for uppercase)
Definition helpers.h:1249
float gamma_uncorrect(float value, float gamma)
Definition helpers.cpp:656
uint16_t crc16(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t reverse_poly, bool refin, bool refout)
Calculate a CRC-16 checksum of data with size len.
Definition helpers.cpp:86
size_t make_name_with_suffix_to(char *buffer, size_t buffer_size, const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Zero-allocation version: format name + separator + suffix directly into buffer.
Definition helpers.cpp:242
constexpr T convert_big_endian(T val)
Convert a value between host byte order and big endian (most significant byte first) order.
Definition helpers.h:915
float gamma_correct(float value, float gamma)
Definition helpers.cpp:648
constexpr char to_sanitized_char(char c)
Sanitize a single char: keep alphanumerics, dashes, underscores; replace others with underscore.
Definition helpers.h:969
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:762
ESPHOME_ALWAYS_INLINE char format_hex_pretty_char(uint8_t v)
Convert a nibble (0-15) to uppercase hex char (used for pretty printing)
Definition helpers.h:1255
void format_mac_addr_lower_no_sep(const uint8_t *mac, char *output)
Format MAC address as xxxxxxxxxxxxxx (lowercase, no separators)
Definition helpers.h:1458
constexpr uint32_t FNV1_OFFSET_BASIS
FNV-1 32-bit offset basis.
Definition helpers.h:784
void rgb_to_hsv(float red, float green, float blue, int &hue, float &saturation, float &value)
Convert red, green and blue (all 0-1) values to hue (0-360), saturation (0-1) and value (0-1).
Definition helpers.cpp:665
std::string format_hex(const uint8_t *data, size_t length)
Format the byte array data of length len in lowercased hex.
uint16_t uint16_t size_t elem_size
Definition helpers.cpp:26
size_t value_accuracy_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals)
Format value with accuracy to buffer, returns chars written (excluding null)
Definition helpers.cpp:474
size_t buf_append_printf_p(char *buf, size_t size, size_t pos, PGM_P fmt,...)
Safely append formatted string to buffer, returning new position (capped at size).
Definition helpers.h:1016
ParseOnOffState parse_on_off(const char *str, const char *on, const char *off)
Parse a string that contains either on, off or toggle.
Definition helpers.cpp:401
const char int const __FlashStringHelper va_list args
Definition log.h:74
std::string format_bin(const uint8_t *data, size_t length)
Format the byte array data of length len in binary.
constexpr T convert_little_endian(T val)
Convert a value between host byte order and little endian (least significant byte first) order.
Definition helpers.h:924
constexpr uint32_t fnv1_hash_extend(uint32_t hash, T value)
Extend a FNV-1 hash with an integer (hashes each byte).
Definition helpers.h:789
bool base64_decode_int32_vector(const std::string &base64, std::vector< int32_t > &out)
Decode base64/base64url string directly into vector of little-endian int32 values.
Definition helpers.cpp:610
va_end(args)
std::string size_t len
constexpr size_t format_hex_prefixed_size(size_t byte_count)
Calculate buffer size needed for format_hex_prefixed_to: "0xXXXXXXXX...\0" = bytes * 2 + 3.
Definition helpers.h:1363
constexpr uint32_t encode_uint24(uint8_t byte1, uint8_t byte2, uint8_t byte3)
Encode a 24-bit value given three bytes in most to least significant byte order.
Definition helpers.h:863
uint32_t small_pow10(int8_t n)
Return 10^n for small non-negative n (0-3) as uint32_t, avoiding float.
Definition helpers.h:1302
std::vector< uint8_t > base64_decode(const std::string &encoded_string)
Decode a base64 string to a byte vector.
char * format_hex_prefixed_to(char(&buffer)[N], T val)
Format an unsigned integer as "0x" prefixed lowercase hex to buffer.
Definition helpers.h:1367
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
Definition helpers.cpp:110
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:275
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
Definition helpers.cpp:341
size_t uint32_to_str(std::span< char, UINT32_MAX_STR_SIZE > buf, uint32_t val)
Write unsigned 32-bit integer to buffer with compile-time size check.
Definition helpers.h:1313
uint16_t size
Definition helpers.cpp:25
int8_t ilog10(float value)
Compute floor(log10(fabs(value))) using iterative comparison.
Definition helpers.cpp:416
uint32_t fnv1_hash_object_id(const char *str, size_t len)
Calculate FNV-1 hash of a string while applying snake_case + sanitize transformations.
Definition helpers.h:995
uint32_t fnv1_hash(const char *str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:161
T clamp_at_least(T value, U min)
Definition helpers.h:2186
optional< T > parse_number(const char *str)
Parse an unsigned decimal number from a null-terminated string.
Definition helpers.h:1143
char * frac_to_str_unchecked(char *buf, uint32_t frac, uint32_t divisor)
Write fractional digits with leading zeros to buffer (internal, no size check).
Definition helpers.h:1322
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition helpers.cpp:108
int8_t step_to_accuracy_decimals(float step)
Derive accuracy in decimals from an increment step.
Definition helpers.cpp:504
uint32_t random_uint32()
Return a random 32-bit unsigned integer.
Definition helpers.cpp:12
size_t size_t pos
Definition helpers.h:1038
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.
Definition helpers.cpp:751
char * buf_append_sep_str(char *buf, size_t remaining, char separator, const char *str, size_t str_len)
Append a separator char and a string to a buffer, respecting remaining space.
Definition helpers.h:1285
void delay_microseconds_safe(uint32_t us)
Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
Definition helpers.cpp:786
std::string format_hex_pretty(const uint8_t *data, size_t length, char separator, bool show_length)
Format a byte array in pretty-printed, human-readable hex format.
constexpr size_t format_hex_size(size_t byte_count)
Calculate buffer size needed for format_hex_to: "XXXXXXXX...\0" = bytes * 2 + 1.
Definition helpers.h:1360
bool str_equals_case_insensitive(const std::string &a, const std::string &b)
Compare strings for equality in case-insensitive manner.
Definition helpers.cpp:202
bool str_endswith_ignore_case(const char *str, size_t str_len, const char *suffix, size_t suffix_len)
Case-insensitive check if string ends with suffix (no heap allocation).
Definition helpers.cpp:218
char * str_sanitize_to(char *buffer, size_t buffer_size, const char *str)
Sanitize a string to buffer, keeping only alphanumerics, dashes, and underscores.
Definition helpers.cpp:225
void init_array_from(std::array< T, N > &dest, std::initializer_list< T > src)
Initialize a std::array from an initializer_list.
Definition helpers.h:512
char * int8_to_str(char *buf, int8_t val)
Write int8 value to buffer without modulo operations.
Definition helpers.h:1259
size_t value_accuracy_with_uom_to_buf(std::span< char, VALUE_ACCURACY_MAX_LEN > buf, float value, int8_t accuracy_decimals, StringRef unit_of_measurement)
Format value with accuracy and UOM to buffer, returns chars written (excluding null)
Definition helpers.cpp:493
char * uint32_to_str_unchecked(char *buf, uint32_t val)
Write unsigned 32-bit integer to buffer (internal, no size check).
Definition helpers.cpp:321
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
Definition helpers.h:1386
constexpr uint32_t FNV1_PRIME
FNV-1 32-bit prime.
Definition helpers.h:786
constexpr T encode_value(const uint8_t *bytes)
Encode a value from its constituent bytes (from most to least significant) in an array with length si...
Definition helpers.h:873
void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green, float &blue)
Convert hue (0-360), saturation (0-1) and value (0-1) to red, green and blue (all 0-1).
Definition helpers.cpp:688
void get_mac_address_into_buffer(std::span< char, MAC_ADDRESS_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in lowercase hex notation.
Definition helpers.cpp:745
uint16_t crc16be(const uint8_t *data, uint16_t len, uint16_t crc, uint16_t poly, bool refin, bool refout)
Definition helpers.cpp:126
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
Definition helpers.h:867
uint8_t crc8(const uint8_t *data, uint8_t len, uint8_t crc, uint8_t poly, bool msb_first)
Calculate a CRC-8 checksum of data with size len.
Definition helpers.cpp:59
constexpr float celsius_to_fahrenheit(float value)
Convert degrees Celsius to degrees Fahrenheit.
Definition helpers.h:1610
size_t size_t const char va_start(args, fmt)
constexpr uint16_t encode_uint16(uint8_t msb, uint8_t lsb)
Encode a 16-bit value given the most and least significant byte.
Definition helpers.h:859
void get_mac_address_raw(uint8_t *mac)
Get the device MAC address as raw bytes, written into the provided byte array (6 bytes).
Definition helpers.cpp:74
size_t size_t const char * fmt
Definition helpers.h:1039
constexpr uint8_t parse_hex_char(char c)
Definition helpers.h:1238
bool str_startswith(const std::string &str, const std::string &start)
Check whether a string starts with a value.
Definition helpers.cpp:209
constexpr std::array< uint8_t, sizeof(T)> decode_value(T val)
Decode a value into its constituent bytes (from most to least significant).
Definition helpers.h:888
constexpr size_t format_bin_size(size_t byte_count)
Calculate buffer size needed for format_bin_to: "01234567...\0" = bytes * 8 + 1.
Definition helpers.h:1489
int written
Definition helpers.h:1045
struct ESPDEPRECATED("Use std::index_sequence instead. Removed in 2026.6.0", "2025.12.0") seq
Definition automation.h:26
To bit_cast(const From &src)
Convert data between types, without aliasing issues or undefined behaviour.
Definition helpers.h:84
constexpr char to_snake_case_char(char c)
Convert a single char to snake_case: lowercase and space to underscore.
Definition helpers.h:965
constexpr float fahrenheit_to_celsius(float value)
Convert degrees Fahrenheit to degrees Celsius.
Definition helpers.h:1612
uint8_t reverse_bits(uint8_t x)
Reverse the order of 8 bits.
Definition helpers.h:898
char * format_hex_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as lowercase hex to buffer (base implementation).
Definition helpers.cpp:335
float lerp(float completion, float start, float end)=delete
constexpr size_t format_hex_pretty_uint16_size(size_t count)
Calculate buffer size needed for format_hex_pretty_to with uint16_t data: "XXXX:XXXX:....
Definition helpers.h:1421
T remap(U value, U min, U max, T min_out, T max_out)
Remap value from the range (min, max) to (min_out, max_out).
Definition helpers.h:765
void * callback_manager_grow(void *data, uint16_t size, uint16_t &capacity, size_t elem_size)
Grow a CallbackManager's backing array to exactly size+1. Defined in helpers.cpp.
bool str_endswith(const std::string &str, const std::string &end)
Check whether a string ends with a value.
Definition helpers.cpp:210
constexpr uint32_t fnv1a_hash(const char *str)
Calculate a FNV-1a hash of str.
Definition helpers.h:834
float gamma
Definition helpers.h:1593
uint16_t uint16_t & capacity
Definition helpers.cpp:25
ParseOnOffState
Return values for parse_on_off().
Definition helpers.h:1551
@ PARSE_ON
Definition helpers.h:1553
@ PARSE_TOGGLE
Definition helpers.h:1555
@ PARSE_OFF
Definition helpers.h:1554
@ PARSE_NONE
Definition helpers.h:1552
float pow10_int(int8_t exp)
Compute 10^exp using iterative multiplication/division.
Definition helpers.h:752
std::string make_name_with_suffix(const char *name, size_t name_len, char sep, const char *suffix_ptr, size_t suffix_len)
Optimized string concatenation: name + separator + suffix (const char* overload) Uses a fixed stack b...
Definition helpers.cpp:262
char * format_bin_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length)
Format byte array as binary string to buffer.
Definition helpers.cpp:378
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1453
static void uint32_t
static Callback create(F &&callable)
Create from any callable.
Definition helpers.h:1640
void call(Ts... args) const
Invoke the callback. Only valid on Callbacks created via create(), never on default-constructed insta...
Definition helpers.h:1636
Lightweight type-erased callback (8 bytes on 32-bit) that avoids std::function overhead.
Definition helpers.h:1624
uint8_t end[39]
Definition sun_gtil2.cpp:17
void byteswap()
uint16_t length
Definition tt21100.cpp:0
uint16_t x
Definition tt21100.cpp:5