[wifi] Use custom MAC address if programmed (#7498)
Some checks failed
CI / Create common environment (push) Has been cancelled
YAML lint / yamllint (push) Has been cancelled
CI / Check flake8 (push) Has been cancelled
CI / Check pylint (push) Has been cancelled
CI / Check black (push) Has been cancelled
CI / Check pyupgrade (push) Has been cancelled
CI / Run script/ci-custom (push) Has been cancelled
CI / Run pytest (push) Has been cancelled
CI / Check clang-format (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 1/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 2/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 3/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 Arduino 4/4 (push) Has been cancelled
CI / Run script/clang-tidy for ESP32 IDF (push) Has been cancelled
CI / Run script/clang-tidy for ESP8266 (push) Has been cancelled
CI / list-components (push) Has been cancelled
CI / Component test (push) Has been cancelled
CI / Split components for testing into 20 groups maximum (push) Has been cancelled
CI / Test split components (push) Has been cancelled
CI / CI Status (push) Has been cancelled

This commit is contained in:
Keith Burzinski 2024-09-26 17:24:18 -05:00 committed by GitHub
parent 3df25a183a
commit 529ff4bd52
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 3 deletions

View file

@ -130,11 +130,16 @@ void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, voi
} }
void WiFiComponent::wifi_pre_setup_() { void WiFiComponent::wifi_pre_setup_() {
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
uint8_t mac[6]; uint8_t mac[6];
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC
get_mac_address_raw(mac); get_mac_address_raw(mac);
set_mac_address(mac); set_mac_address(mac);
ESP_LOGV(TAG, "Use EFuse MAC without checking CRC: %s", get_mac_address_pretty().c_str()); ESP_LOGV(TAG, "Use EFuse MAC without checking CRC: %s", get_mac_address_pretty().c_str());
#else
if (has_custom_mac_address()) {
get_mac_address_raw(mac);
set_mac_address(mac);
}
#endif #endif
esp_err_t err = esp_netif_init(); esp_err_t err = esp_netif_init();
if (err != ERR_OK) { if (err != ERR_OK) {

View file

@ -671,9 +671,17 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
// match the CRC that goes along with it. For those devices, this // match the CRC that goes along with it. For those devices, this
// work-around reads and uses the MAC address as-is from EFuse, // work-around reads and uses the MAC address as-is from EFuse,
// without doing the CRC check. // without doing the CRC check.
if (has_custom_mac_address()) {
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
} else {
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48); esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
}
#else #else
if (has_custom_mac_address()) {
esp_efuse_mac_get_custom(mac);
} else {
esp_efuse_mac_get_default(mac); esp_efuse_mac_get_default(mac);
}
#endif #endif
#elif defined(USE_ESP8266) #elif defined(USE_ESP8266)
wifi_get_macaddr(STATION_IF, mac); wifi_get_macaddr(STATION_IF, mac);
@ -685,20 +693,53 @@ void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parame
// this should be an error, but that messes with CI checks. #error No mac address method defined // this should be an error, but that messes with CI checks. #error No mac address method defined
#endif #endif
} }
std::string get_mac_address() { std::string get_mac_address() {
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); get_mac_address_raw(mac);
return str_snprintf("%02x%02x%02x%02x%02x%02x", 12, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return str_snprintf("%02x%02x%02x%02x%02x%02x", 12, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
} }
std::string get_mac_address_pretty() { std::string get_mac_address_pretty() {
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); get_mac_address_raw(mac);
return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); return str_snprintf("%02X:%02X:%02X:%02X:%02X:%02X", 17, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
} }
#ifdef USE_ESP32 #ifdef USE_ESP32
void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); } void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); }
#endif #endif
bool has_custom_mac_address() {
#ifdef USE_ESP32
uint8_t mac[6];
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED) || defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC)
return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac);
#else
return (esp_efuse_mac_get_custom(mac) == ESP_OK) && mac_address_is_valid(mac);
#endif
#else
return false;
#endif
}
bool mac_address_is_valid(const uint8_t *mac) {
bool is_all_zeros = true;
bool is_all_ones = true;
for (uint8_t i = 0; i < 6; i++) {
if (mac[i] != 0) {
is_all_zeros = false;
}
}
for (uint8_t i = 0; i < 6; i++) {
if (mac[i] != 0xFF) {
is_all_ones = false;
}
}
return !(is_all_zeros || is_all_ones);
}
void delay_microseconds_safe(uint32_t us) { // avoids CPU locks that could trigger WDT or affect WiFi/BT stability void delay_microseconds_safe(uint32_t us) { // avoids CPU locks that could trigger WDT or affect WiFi/BT stability
uint32_t start = micros(); uint32_t start = micros();

View file

@ -635,6 +635,14 @@ std::string get_mac_address_pretty();
void set_mac_address(uint8_t *mac); void set_mac_address(uint8_t *mac);
#endif #endif
/// Check if a custom MAC address is set (ESP32 & variants)
/// @return True if a custom MAC address is set (ESP32 & variants), else false
bool has_custom_mac_address();
/// Check if the MAC address is not all zeros or all ones
/// @return True if MAC is valid, else false
bool mac_address_is_valid(const uint8_t *mac);
/// Delay for the given amount of microseconds, possibly yielding to other processes during the wait. /// Delay for the given amount of microseconds, possibly yielding to other processes during the wait.
void delay_microseconds_safe(uint32_t us); void delay_microseconds_safe(uint32_t us);