From 5404163be08ec98b95970de204105ab424d21aaf Mon Sep 17 00:00:00 2001 From: Oxan van Leeuwen Date: Mon, 15 Nov 2021 15:48:16 +0100 Subject: [PATCH] Clean-up MAC address helpers (#2713) --- esphome/core/helpers.cpp | 28 +++++++++------------------- esphome/core/helpers.h | 7 +++---- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/esphome/core/helpers.cpp b/esphome/core/helpers.cpp index 27608a84c1..3e614eb515 100644 --- a/esphome/core/helpers.cpp +++ b/esphome/core/helpers.cpp @@ -6,11 +6,10 @@ #include #if defined(USE_ESP8266) -#ifdef USE_WIFI -#include -#endif -#include #include +#include +// for xt_rsil()/xt_wsr_ps() +#include #elif defined(USE_ESP32_FRAMEWORK_ARDUINO) #include #elif defined(USE_ESP_IDF) @@ -31,8 +30,8 @@ namespace esphome { static const char *const TAG = "helpers"; void get_mac_address_raw(uint8_t *mac) { -#ifdef USE_ESP32 -#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC +#if defined(USE_ESP32) +#if defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC) // On some devices, the MAC address that is burnt into EFuse does not // match the CRC that goes along with it. For those devices, this // work-around reads and uses the MAC address as-is from EFuse, @@ -41,30 +40,21 @@ void get_mac_address_raw(uint8_t *mac) { #else esp_efuse_mac_get_default(mac); #endif -#endif -#if (defined USE_ESP8266 && defined USE_WIFI) - WiFi.macAddress(mac); +#elif defined(USE_ESP8266) + wifi_get_macaddr(STATION_IF, mac); #endif } std::string get_mac_address() { - char tmp[20]; uint8_t mac[6]; get_mac_address_raw(mac); -#ifdef USE_WIFI - sprintf(tmp, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); -#else - return ""; -#endif - return std::string(tmp); + return str_sprintf("%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } std::string get_mac_address_pretty() { - char tmp[20]; uint8_t mac[6]; get_mac_address_raw(mac); - sprintf(tmp, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - return std::string(tmp); + return str_sprintf("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); } #ifdef USE_ESP32 diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index c67ad8eea3..120642c62e 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -25,14 +25,13 @@ namespace esphome { -/// Read the raw MAC address into the provided byte array (6 bytes). +/// Get the device MAC address as raw bytes, written into the provided byte array (6 bytes). void get_mac_address_raw(uint8_t *mac); -/// Get the MAC address as a string, using lower case hex notation. -/// This can be used as way to identify this ESP. +/// Get the device MAC address as a string, in lowercase hex notation. std::string get_mac_address(); -/// Get the MAC address as a string, using colon-separated upper case hex notation. +/// Get the device MAC address as a string, in colon-separated uppercase hex notation. std::string get_mac_address_pretty(); #ifdef USE_ESP32