Skip to content

Commit

Permalink
[ESP32] Add an API to get MAC address of ethernet device
Browse files Browse the repository at this point in the history
  • Loading branch information
PSONALl committed Sep 26, 2023
1 parent 6bb0b71 commit 88f7686
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
#include <platform/ESP32/ESP32Config.h>
#include <platform/internal/GenericConfigurationManagerImpl.ipp>

#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"
Expand Down Expand Up @@ -262,19 +265,30 @@ CHIP_ERROR ConfigurationManagerImpl::StoreCountryCode(const char * code, size_t
return GenericConfigurationManagerImpl<ESP32Config>::StoreCountryCode(code, codeLen);
}

#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan buf)
{
if (buf.size() != ConfigurationManager::kPrimaryMACAddressLength)
return CHIP_ERROR_INVALID_ARGUMENT;

return MapConfigError(esp_read_mac(buf.data(), ESP_MAC_ETH));
}
#elif CHIP_DEVICE_CONFIG_ENABLE_WIFI
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
wifi_mode_t mode;
esp_wifi_get_mode(&mode);
if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA))
return MapConfigError(esp_wifi_get_mac(WIFI_IF_AP, buf));
else
return MapConfigError(esp_wifi_get_mac(WIFI_IF_STA, buf));
}
#else
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
{
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}
#endif

CHIP_ERROR ConfigurationManagerImpl::MapConfigError(esp_err_t error)
{
Expand Down
4 changes: 4 additions & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ 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;
#else
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
#endif
bool CanFactoryReset(void) override;
void InitiateFactoryReset(void) override;
CHIP_ERROR MapConfigError(esp_err_t error);
Expand Down

0 comments on commit 88f7686

Please sign in to comment.