Clean-up MAC address helpers (#2713)

This commit is contained in:
Oxan van Leeuwen 2021-11-15 15:48:16 +01:00 committed by GitHub
parent 0b193eee43
commit 5404163be0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 23 deletions

View file

@ -6,11 +6,10 @@
#include <cstring> #include <cstring>
#if defined(USE_ESP8266) #if defined(USE_ESP8266)
#ifdef USE_WIFI
#include <ESP8266WiFi.h>
#endif
#include <Arduino.h>
#include <osapi.h> #include <osapi.h>
#include <user_interface.h>
// for xt_rsil()/xt_wsr_ps()
#include <Arduino.h>
#elif defined(USE_ESP32_FRAMEWORK_ARDUINO) #elif defined(USE_ESP32_FRAMEWORK_ARDUINO)
#include <Esp.h> #include <Esp.h>
#elif defined(USE_ESP_IDF) #elif defined(USE_ESP_IDF)
@ -31,8 +30,8 @@ namespace esphome {
static const char *const TAG = "helpers"; static const char *const TAG = "helpers";
void get_mac_address_raw(uint8_t *mac) { void get_mac_address_raw(uint8_t *mac) {
#ifdef USE_ESP32 #if defined(USE_ESP32)
#ifdef USE_ESP32_IGNORE_EFUSE_MAC_CRC #if defined(USE_ESP32_IGNORE_EFUSE_MAC_CRC)
// On some devices, the MAC address that is burnt into EFuse does not // 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 // 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,
@ -41,30 +40,21 @@ void get_mac_address_raw(uint8_t *mac) {
#else #else
esp_efuse_mac_get_default(mac); esp_efuse_mac_get_default(mac);
#endif #endif
#endif #elif defined(USE_ESP8266)
#if (defined USE_ESP8266 && defined USE_WIFI) wifi_get_macaddr(STATION_IF, mac);
WiFi.macAddress(mac);
#endif #endif
} }
std::string get_mac_address() { std::string get_mac_address() {
char tmp[20];
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); get_mac_address_raw(mac);
#ifdef USE_WIFI return str_sprintf("%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
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);
} }
std::string get_mac_address_pretty() { std::string get_mac_address_pretty() {
char tmp[20];
uint8_t mac[6]; uint8_t mac[6];
get_mac_address_raw(mac); 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 str_sprintf("%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
return std::string(tmp);
} }
#ifdef USE_ESP32 #ifdef USE_ESP32

View file

@ -25,14 +25,13 @@
namespace esphome { 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); void get_mac_address_raw(uint8_t *mac);
/// Get the MAC address as a string, using lower case hex notation. /// Get the device MAC address as a string, in lowercase hex notation.
/// This can be used as way to identify this ESP.
std::string get_mac_address(); 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(); std::string get_mac_address_pretty();
#ifdef USE_ESP32 #ifdef USE_ESP32