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>
#if defined(USE_ESP8266)
#ifdef USE_WIFI
#include <ESP8266WiFi.h>
#endif
#include <Arduino.h>
#include <osapi.h>
#include <user_interface.h>
// for xt_rsil()/xt_wsr_ps()
#include <Arduino.h>
#elif defined(USE_ESP32_FRAMEWORK_ARDUINO)
#include <Esp.h>
#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

View file

@ -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