ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
helpers.cpp
Go to the documentation of this file.
3
4#ifdef USE_ESP32
5
6#include "esp_efuse.h"
7#include "esp_efuse_table.h"
8#include "esp_mac.h"
9
10#include <freertos/FreeRTOS.h>
11#include <freertos/portmacro.h>
12#include "esphome/core/log.h"
13#include "esp_random.h"
14#include "esp_system.h"
15
16namespace esphome {
17
18ESPHOME_LOG_TAG(TAG, "esp32");
19
20bool random_bytes(uint8_t *data, size_t len) {
21 esp_fill_random(data, len);
22 return true;
23}
24
25// only affects the executing core
26// so should not be used as a mutex lock, only to get accurate timing
27IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); }
28IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); }
29
30#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
31#include "lwip/priv/tcpip_priv.h"
32#endif
33
35#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
36 // A thread that already holds the core lock (lwIP took it for a tcpip_api_call or netif callback, or an
37 // outer LwIPLock did) must not take it again, and must not release it when this nested guard ends.
38 if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) {
39 LOCK_TCPIP_CORE();
40 this->locked_ = true;
41 }
42#endif
43}
44
46#ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING
47 if (this->locked_) {
48 UNLOCK_TCPIP_CORE();
49 }
50#endif
51}
52
54static bool read_valid_mac(uint8_t *mac, esp_err_t err) { return err == ESP_OK && mac_address_is_valid(mac); }
55
56static constexpr size_t MAC_ADDRESS_SIZE_BITS = MAC_ADDRESS_SIZE * 8; // 48 bits
57
58// Must not use the ESPHome logger (may run before it exists, e.g. from app_main()).
59bool get_custom_mac_address(uint8_t *mac) {
60 // has_custom_mac_address() checks the raw eFuse field, while the reads below select their
61 // method differently and may still fail (CRC), so the result must be validated again.
63 return false;
64#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
65 return read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS));
66#else
67 return read_valid_mac(mac, esp_efuse_mac_get_custom(mac));
68#endif
69}
70
71void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
72 if (get_custom_mac_address(mac)) {
73 return;
74 }
75#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
76 // When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
77 // returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead.
78 // This already reads raw eFuse bytes, so there is no CRC-bypass fallback
79 // (unlike the non-IEEE802154 path where esp_efuse_mac_get_default does CRC checks).
80 if (read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, MAC_ADDRESS_SIZE_BITS))) {
81 return;
82 }
83#else
84 if (read_valid_mac(mac, esp_efuse_mac_get_default(mac))) {
85 return;
86 }
87 // Default MAC read failed (e.g., eFuse CRC error) - try reading raw eFuse bytes
88 // directly, bypassing CRC validation. A MAC that passes mac_address_is_valid()
89 // (non-zero, non-broadcast, unicast) is almost certainly the real factory MAC
90 // with a corrupted CRC byte, which is far better than returning garbage or zeros.
91 if (read_valid_mac(mac, esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, MAC_ADDRESS_SIZE_BITS))) {
92 ESP_LOGW(TAG, "eFuse MAC CRC failed but raw bytes appear valid - using raw eFuse MAC");
93 return;
94 }
95#endif
96 // All methods failed - zero the MAC rather than returning garbage
97 ESP_LOGE(TAG, "Failed to read a valid MAC address from eFuse");
98 memset(mac, 0, MAC_ADDRESS_SIZE);
99}
100
101void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); }
102
104#if !defined(USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC)
105 uint8_t mac[MAC_ADDRESS_SIZE];
106 // do not use 'esp_efuse_mac_get_custom(mac)' because it drops an error in the logs whenever it fails
107#ifndef USE_ESP32_VARIANT_ESP32
108 return (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS) == ESP_OK) &&
110#else
111 return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, MAC_ADDRESS_SIZE_BITS) == ESP_OK) &&
113#endif
114#else
115 return false;
116#endif
117}
118
119} // namespace esphome
120
121#endif // USE_ESP32
ESPHOME_ALWAYS_INLINE InterruptLock()
Definition helpers.h:2255
ESPHOME_ALWAYS_INLINE ~InterruptLock()
Definition helpers.h:2256
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
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
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:865
const void size_t len
Definition hal.h:68
bool has_custom_mac_address()
Check if a custom MAC address is set (ESP32 & variants)
Definition helpers.cpp:103
void set_mac_address(uint8_t *mac)
Set the MAC address to use from the provided byte array (6 bytes).
Definition helpers.cpp:101
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:71
bool get_custom_mac_address(uint8_t *mac)
Read the custom MAC address from eFuse into the provided byte array (6 bytes).
Definition helpers.cpp:59