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 authored and PSONALl committed Oct 5, 2023
1 parent 24288ec commit c0be0ef
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ConfigurationManager
};

virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) = 0;
virtual CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf) = 0;
virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
CHIP_ERROR SetFirmwareBuildChipEpochTime(System::Clock::Seconds32 buildTime) override;
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override;
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf) override;
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override;
CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override;
Expand Down
14 changes: 14 additions & 0 deletions src/include/platform/internal/GenericConfigurationManagerImpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,12 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryWiFiMACAddres
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}

template <class ConfigClass>
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryEthernetMACAddress(MutableByteSpan buf)
{
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}

template <class ConfigClass>
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(MutableByteSpan buf)
{
Expand All @@ -374,6 +380,14 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(Mu

memset(buf.data(), 0, buf.size());

#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryEthernetMACAddress(buf) == CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "Using Ethernet extended MAC for hostname.");
return CHIP_NO_ERROR;
}
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
if (chip::DeviceLayer::ThreadStackMgr().GetPrimary802154MACAddress(buf.data()) == CHIP_NO_ERROR)
{
Expand Down
17 changes: 17 additions & 0 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,6 +265,20 @@ CHIP_ERROR ConfigurationManagerImpl::StoreCountryCode(const char * code, size_t
return GenericConfigurationManagerImpl<ESP32Config>::StoreCountryCode(code, codeLen);
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryEthernetMACAddress(MutableByteSpan buf)
{
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
if (buf.size() < ConfigurationManager::kPrimaryMACAddressLength)
return CHIP_ERROR_BUFFER_TOO_SMALL;

esp_err_t err = esp_read_mac(buf.data(), ESP_MAC_ETH);
buf.reduce_size(ConfigurationManager::kPrimaryMACAddressLength);
return MapConfigError(err);
#else
return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif
}

CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
{
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
Expand Down
1 change: 1 addition & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
// ===== Members that implement the ConfigurationManager public interface.

CHIP_ERROR Init(void) override;
CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf) override;
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
bool CanFactoryReset(void) override;
void InitiateFactoryReset(void) override;
Expand Down
1 change: 1 addition & 0 deletions src/platform/fake/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ConfigurationManagerImpl : public ConfigurationManager
CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
Expand Down

0 comments on commit c0be0ef

Please sign in to comment.