Skip to content

Commit 9f077fd

Browse files
bzbarsky-applegmarcosb
authored andcommitted
Pass the arg of GetPrimaryOrFallbackMACAddress by reference. (project-chip#37609)
* Pass the arg of GetPrimaryOrFallbackMACAddress by reference. Implementations want to resize it to the size of the data they actually put in it. * Fix TestConfigurationMgr to have correct expectations.
1 parent f595828 commit 9f077fd

11 files changed

+20
-13
lines changed

src/app/server/Dnssd.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void DnssdServer::AddICDKeyToAdvertisement(AdvertisingParams & advParams)
170170
}
171171
#endif
172172

173-
void DnssdServer::GetPrimaryOrFallbackMACAddress(chip::MutableByteSpan mac)
173+
void DnssdServer::GetPrimaryOrFallbackMACAddress(MutableByteSpan & mac)
174174
{
175175
if (ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
176176
{

src/app/server/Dnssd.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ class DLL_EXPORT DnssdServer : public ICDStateObserver
168168
// Our randomly-generated fallback "MAC address", in case we don't have a real one.
169169
uint8_t mFallbackMAC[chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength] = { 0 };
170170

171-
void GetPrimaryOrFallbackMACAddress(chip::MutableByteSpan mac);
171+
void GetPrimaryOrFallbackMACAddress(MutableByteSpan & mac);
172172

173173
//
174174
// Check if we have any valid operational credentials present in the fabric table and return true

src/include/platform/ConfigurationManager.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ConfigurationManager
9494
// Copies the primary MAC into a mutable span, which must be of size kPrimaryMACAddressLength.
9595
// Upon success, the span will be reduced to the size of the MAC address being returned, which
9696
// can be less than kPrimaryMACAddressLength on a device that supports Thread.
97-
virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) = 0;
97+
virtual CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) = 0;
9898

9999
// Copies the primary WiFi MAC into a buffer of size kEthernetMACAddressLength
100100
virtual CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) = 0;

src/include/platform/internal/GenericConfigurationManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
6969
CHIP_ERROR GetFirmwareBuildChipEpochTime(System::Clock::Seconds32 & buildTime) override;
7070
CHIP_ERROR SetFirmwareBuildChipEpochTime(System::Clock::Seconds32 buildTime) override;
7171
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override;
72-
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
72+
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) override;
7373
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
7474
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override;
7575
CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override;

src/include/platform/internal/GenericConfigurationManagerImpl.ipp

+1-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryWiFiMACAddres
369369
}
370370

371371
template <class ConfigClass>
372-
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(MutableByteSpan buf)
372+
CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(MutableByteSpan & buf)
373373
{
374374
if (buf.size() != ConfigurationManager::kPrimaryMACAddressLength)
375375
return CHIP_ERROR_INVALID_ARGUMENT;

src/platform/ESP32/ConfigurationManagerImpl.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ CHIP_ERROR ConfigurationManagerImpl::StoreCountryCode(const char * code, size_t
312312

313313
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
314314

315-
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan buf)
315+
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan & buf)
316316
{
317317
if (GetPrimaryEthernetMACAddress(buf) == CHIP_NO_ERROR)
318318
{
@@ -322,7 +322,7 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan buf)
322322
return CHIP_ERROR_NOT_FOUND;
323323
}
324324

325-
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryEthernetMACAddress(MutableByteSpan buf)
325+
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryEthernetMACAddress(MutableByteSpan & buf)
326326
{
327327
if (buf.size() < ConfigurationManager::kPrimaryMACAddressLength)
328328
return CHIP_ERROR_BUFFER_TOO_SMALL;

src/platform/ESP32/ConfigurationManagerImpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
7070

7171
CHIP_ERROR Init(void) override;
7272
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
73-
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
74-
CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan buf);
73+
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) override;
74+
CHIP_ERROR GetPrimaryEthernetMACAddress(MutableByteSpan & buf);
7575
#endif
7676
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override;
7777
bool CanFactoryReset(void) override;

src/platform/bouffalolab/BL702/ConfigurationManagerImpl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CHIP_ERROR ConfigurationManagerImpl::GetPrimaryWiFiMACAddress(uint8_t * buf)
3838
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
3939

4040
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
41-
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan buf)
41+
CHIP_ERROR ConfigurationManagerImpl::GetPrimaryMACAddress(MutableByteSpan & buf)
4242
{
4343
if (buf.size() != ConfigurationManager::kPrimaryMACAddressLength)
4444
return CHIP_ERROR_INVALID_ARGUMENT;

src/platform/bouffalolab/common/ConfigurationManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp
7070
#endif
7171

7272
#if CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
73-
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
73+
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) override;
7474
#endif
7575

7676
static void DoFactoryReset(intptr_t arg);

src/platform/fake/ConfigurationManagerImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class ConfigurationManagerImpl : public ConfigurationManager
5050
CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
5151
CHIP_ERROR StoreSoftwareVersion(uint32_t softwareVer) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
5252
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
53-
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
53+
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan & buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
5454
CHIP_ERROR GetPrimaryWiFiMACAddress(uint8_t * buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
5555
CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
5656
CHIP_ERROR StoreManufacturingDate(const char * mfgDate, size_t mfgDateLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }

src/platform/tests/TestConfigurationMgr.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -389,10 +389,17 @@ TEST_F(TestConfigurationMgr, GetPrimaryMACAddress)
389389
MutableByteSpan mac6Bytes(macBuffer6Bytes);
390390

391391
err = ConfigurationMgr().GetPrimaryMACAddress(mac8Bytes);
392-
if (mac8Bytes.size() != ConfigurationManager::kPrimaryMACAddressLength)
392+
if (sizeof(macBuffer8Bytes) != ConfigurationManager::kPrimaryMACAddressLength)
393393
{
394+
// Should have failed input validation
394395
EXPECT_EQ(err, CHIP_ERROR_INVALID_ARGUMENT);
395396
}
397+
else if (mac8Bytes.size() != ConfigurationManager::kPrimaryMACAddressLength)
398+
{
399+
// This can happen if the primary address is Thread but then there is no
400+
// Thread address to be had and we fell back to Wi-Fi.
401+
EXPECT_EQ(mac8Bytes.size(), ConfigurationManager::kEthernetMACAddressLength);
402+
}
396403

397404
err = ConfigurationMgr().GetPrimaryMACAddress(mac6Bytes);
398405
if (mac6Bytes.size() != ConfigurationManager::kPrimaryMACAddressLength)

0 commit comments

Comments
 (0)