From 9b008bc10d48bef1184cf9f0405b12b41af5d328 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Fri, 6 Dec 2024 11:24:48 +0800 Subject: [PATCH] ESP32: Fix IEEE802154 MAC address in diagnostic provider (#36710) --- .../ESP32/DiagnosticDataProviderImpl.cpp | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/src/platform/ESP32/DiagnosticDataProviderImpl.cpp b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp index fc2992f20d3fc9..8d1a5d662a3031 100644 --- a/src/platform/ESP32/DiagnosticDataProviderImpl.cpp +++ b/src/platform/ESP32/DiagnosticDataProviderImpl.cpp @@ -217,32 +217,37 @@ CHIP_ERROR DiagnosticDataProviderImpl::GetNetworkInterfaces(NetworkInterface ** { NetworkInterface * ifp = new NetworkInterface(); esp_netif_ip_info_t ipv4_info; + uint8_t addressSize = 0; Platform::CopyString(ifp->Name, esp_netif_get_ifkey(ifa)); ifp->name = CharSpan::fromCharString(ifp->Name); ifp->isOperational = true; ifp->type = GetInterfaceType(esp_netif_get_desc(ifa)); ifp->offPremiseServicesReachableIPv4.SetNull(); ifp->offPremiseServicesReachableIPv6.SetNull(); -#if !CHIP_DEVICE_CONFIG_ENABLE_THREAD - if (esp_netif_get_mac(ifa, ifp->MacAddress) != ESP_OK) +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + if (ifp->type == InterfaceTypeEnum::kThread) { - ChipLogError(DeviceLayer, "Failed to get network hardware address"); + static_assert(OT_EXT_ADDRESS_SIZE <= sizeof(ifp->MacAddress), "Unexpected extended address size"); + if (ThreadStackMgr().GetPrimary802154MACAddress(ifp->MacAddress) == CHIP_NO_ERROR) + { + addressSize = OT_EXT_ADDRESS_SIZE; + } } else +#endif + if (esp_netif_get_mac(ifa, ifp->MacAddress) == ESP_OK) { - ifp->hardwareAddress = ByteSpan(ifp->MacAddress, 6); + // For Wi-Fi or Ethernet interface, the MAC address size should be 6 + addressSize = 6; } -#else - if (esp_read_mac(ifp->MacAddress, ESP_MAC_IEEE802154) != ESP_OK) + if (addressSize != 0) { - ChipLogError(DeviceLayer, "Failed to get network hardware address"); + ifp->hardwareAddress = ByteSpan(ifp->MacAddress, addressSize); } else { - ifp->hardwareAddress = ByteSpan(ifp->MacAddress, 8); + ChipLogError(DeviceLayer, "Failed to get network hardware address"); } -#endif - #ifndef CONFIG_DISABLE_IPV4 if (esp_netif_get_ip_info(ifa, &ipv4_info) == ESP_OK) {