Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mac address get for dnssd #15221

Merged
merged 6 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/app/server/Dnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ CHIP_ERROR DnssdServer::AdvertiseOperational()
{
uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
MutableByteSpan mac(macBuffer);
chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
{
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
}

const auto advertiseParameters = chip::Dnssd::OperationalAdvertisingParameters()
.SetPeerId(fabricInfo.GetPeerId())
Expand Down Expand Up @@ -293,7 +297,11 @@ CHIP_ERROR DnssdServer::Advertise(bool commissionableNode, chip::Dnssd::Commissi

uint8_t macBuffer[DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength];
MutableByteSpan mac(macBuffer);
chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac);
if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryMACAddress(mac) != CHIP_NO_ERROR)
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
{
ChipLogError(Discovery, "Failed to get primary mac address of device. Generating a random one.");
Crypto::DRBG_get_bytes(macBuffer, sizeof(macBuffer));
}
advertiseParameters.SetMac(mac);

uint16_t value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,15 @@ CHIP_ERROR GenericConfigurationManagerImpl<ConfigClass>::GetPrimaryMACAddress(Mu
ChipLogDetail(DeviceLayer, "Using Thread extended MAC for hostname.");
return CHIP_NO_ERROR;
}
#else
if (DeviceLayer::ConfigurationMgr().GetPrimaryWiFiMACAddress(buf.data()) == CHIP_NO_ERROR)
#endif

if (chip::DeviceLayer::ConfigurationMgr().GetPrimaryWiFiMACAddress(buf.data()) == CHIP_NO_ERROR)
{
ChipLogDetail(DeviceLayer, "Using wifi MAC for hostname");
return CHIP_NO_ERROR;
}
#endif

ChipLogError(DeviceLayer, "MAC is not known, using a default.");
uint8_t temp[ConfigurationManager::kMaxMACAddressLength] = { 0xEE, 0xAA, 0xBA, 0xDA, 0xBA, 0xD0, 0xDD, 0xCA };
memcpy(buf.data(), temp, buf.size());
return CHIP_NO_ERROR;
return CHIP_ERROR_NOT_FOUND;
}

template <class ConfigClass>
Expand Down
26 changes: 5 additions & 21 deletions src/platform/tests/TestConfigurationMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ static void TestConfigurationMgr_Breadcrumb(nlTestSuite * inSuite, void * inCont

static void TestConfigurationMgr_GetPrimaryMACAddress(nlTestSuite * inSuite, void * inContext)
{
CHIP_ERROR err = CHIP_NO_ERROR;
const uint8_t defaultMacAddress[8] = { 0xEE, 0xAA, 0xBA, 0xDA, 0xBA, 0xD0, 0xDD, 0xCA };
CHIP_ERROR err = CHIP_NO_ERROR;
uint8_t macBuffer8Bytes[8];
uint8_t macBuffer6Bytes[6];
MutableByteSpan mac8Bytes(macBuffer8Bytes);
Expand All @@ -199,32 +198,17 @@ static void TestConfigurationMgr_GetPrimaryMACAddress(nlTestSuite * inSuite, voi
{
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
}
else
{
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

// Verify default MAC address value
NL_TEST_ASSERT(inSuite,
strncmp(reinterpret_cast<char *>(mac8Bytes.data()), reinterpret_cast<const char *>(defaultMacAddress),
mac8Bytes.size()) == 0);
}

err = ConfigurationMgr().GetPrimaryMACAddress(mac6Bytes);
if (mac6Bytes.size() != ConfigurationManager::kPrimaryMACAddressLength)
{
NL_TEST_ASSERT(inSuite, err == CHIP_ERROR_INVALID_ARGUMENT);
}
else
{
NL_TEST_ASSERT(inSuite, err == CHIP_NO_ERROR);

#ifndef __MBED__
// Verify default MAC address value
NL_TEST_ASSERT(inSuite,
strncmp(reinterpret_cast<char *>(mac6Bytes.data()), reinterpret_cast<const char *>(defaultMacAddress),
mac6Bytes.size()) == 0);
#endif
}
// NOTICE for above:
// no validation for CHIP_NO_ERROR:
// - there is no guarantee in CI that a valid IP address exists,
// expecially if running in emulators (zephyr and qemu)
}

/**
Expand Down