From d8906162d9663173373828aabea5243b88124650 Mon Sep 17 00:00:00 2001 From: PSONALl Date: Fri, 22 Sep 2023 15:34:21 +0530 Subject: [PATCH] [ESP32] Add an API to get MAC address of ethernet device --- .../ESP32/ConfigurationManagerImpl.cpp | 28 +++++++++++++++++++ src/platform/ESP32/ConfigurationManagerImpl.h | 4 +++ 2 files changed, 32 insertions(+) diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index 188fb75a43e86b..562c729b6f55e2 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -31,6 +31,9 @@ #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET +#include "esp_mac.h" +#endif #include "esp_ota_ops.h" #include "esp_phy_init.h" #include "esp_wifi.h" @@ -262,6 +265,31 @@ CHIP_ERROR ConfigurationManagerImpl::StoreCountryCode(const char * code, size_t return GenericConfigurationManagerImpl::StoreCountryCode(code, codeLen); } +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + +CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan buf) +{ + if (GetPrimaryEthernetMACAddress(buf) == CHIP_NO_ERROR) + { + ChipLogDetail(DeviceLayer, "Using Ethernet MAC for hostname."); + return CHIP_NO_ERROR; + } + return CHIP_ERROR_NOT_FOUND; +} + +CHIP_ERROR ConfigurationManagerImpl::GetPrimaryEthernetMACAddress(MutableByteSpan buf) +{ + if (buf.size() < ConfigurationManager::kPrimaryMACAddressLength) + return CHIP_ERROR_BUFFER_TOO_SMALL; + + memset(buf.data(), 0, buf.size()); + + esp_err_t err = esp_read_mac(buf.data(), ESP_MAC_ETH); + buf.reduce_size(ConfigurationManager::kPrimaryMACAddressLength); + return MapConfigError(err); +} +#endif + CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf) { #if CHIP_DEVICE_CONFIG_ENABLE_WIFI diff --git a/src/platform/ESP32/ConfigurationManagerImpl.h b/src/platform/ESP32/ConfigurationManagerImpl.h index fce74d9307b823..3779ac816dc463 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.h +++ b/src/platform/ESP32/ConfigurationManagerImpl.h @@ -66,6 +66,10 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp // ===== Members that implement the ConfigurationManager public interface. CHIP_ERROR Init(void) override; +#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET + CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override; + CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf); +#endif CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override; bool CanFactoryReset(void) override; void InitiateFactoryReset(void) override;