From 8129636d43ef0e3cfa41f83bb862b3b17cb441c2 Mon Sep 17 00:00:00 2001 From: Wang Qixiang <43193572+wqx6@users.noreply.github.com> Date: Mon, 22 Nov 2021 22:39:49 +0800 Subject: [PATCH] ESP32: Fix esp32 platform bugs (#12082) * fix WiFiDiagnostics Read bug for ESP32 platform * Add GetNetworkInterface and ReleaseNetworkInterface for ESP32 platform * fix identifytime does't decrease on ESP platform * Restyled by clang-format Co-authored-by: Restyled.io --- .../esp32/main/DeviceCallbacks.cpp | 77 ++++----- .../esp32/main/include/DeviceCallbacks.h | 2 - .../all-clusters-app/esp32/sdkconfig.defaults | 1 + .../ESP32/ConnectivityManagerImpl.cpp | 117 ++++++-------- src/platform/ESP32/ConnectivityManagerImpl.h | 7 +- .../ESP32/ConnectivityManagerImpl_WiFi.cpp | 148 ++++++++++++++++++ 6 files changed, 240 insertions(+), 112 deletions(-) diff --git a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp index 6ec297cfb51f05..edccd2b899e960 100644 --- a/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp +++ b/examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include #include @@ -49,9 +50,48 @@ using namespace ::chip::Inet; using namespace ::chip::System; using namespace ::chip::DeviceLayer; -uint32_t identifyTimerCount; constexpr uint32_t kIdentifyTimerDelayMS = 250; +void OnIdentifyTriggerEffect(Identify * identify) +{ + switch (identify->mCurrentEffectIdentifier) + { + case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK: + statusLED1.Blink(kIdentifyTimerDelayMS * 2); + ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK"); + break; + case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE: + ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE"); + break; + case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY: + ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY"); + break; + case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE: + ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE"); + break; + default: + ChipLogProgress(Zcl, "No identifier effect"); + break; + } + return; +} + +Identify gIdentify0 = { + chip::EndpointId{ 0 }, + [](Identify *) { ChipLogProgress(Zcl, "onIdentifyStart"); }, + [](Identify *) { ChipLogProgress(Zcl, "onIdentifyStop"); }, + EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED, + OnIdentifyTriggerEffect, +}; + +Identify gIdentify1 = { + chip::EndpointId{ 1 }, + [](Identify *) { ChipLogProgress(Zcl, "onIdentifyStart"); }, + [](Identify *) { ChipLogProgress(Zcl, "onIdentifyStop"); }, + EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED, + OnIdentifyTriggerEffect, +}; + void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg) { switch (event->Type) @@ -108,10 +148,6 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster OnOnOffPostAttributeChangeCallback(endpointId, attributeId, value); break; - case ZCL_IDENTIFY_CLUSTER_ID: - OnIdentifyPostAttributeChangeCallback(endpointId, attributeId, value); - break; - case ZCL_LEVEL_CONTROL_CLUSTER_ID: OnLevelControlAttributeChangeCallback(endpointId, attributeId, value); break; @@ -218,37 +254,6 @@ void DeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointI } #endif -void IdentifyTimerHandler(Layer * systemLayer, void * appState) -{ - statusLED1.Animate(); - - if (identifyTimerCount) - { - systemLayer->StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, appState); - // Decrement the timer count. - identifyTimerCount--; - } -} - -void DeviceCallbacks::OnIdentifyPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value) -{ - VerifyOrExit(attributeId == ZCL_IDENTIFY_TIME_ATTRIBUTE_ID, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId)); - VerifyOrExit(endpointId == 1, ESP_LOGE(TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId)); - - statusLED1.Blink(kIdentifyTimerDelayMS * 2); - - // timerCount represents the number of callback executions before we stop the timer. - // value is expressed in seconds and the timer is fired every 250ms, so just multiply value by 4. - // Also, we want timerCount to be odd number, so the ligth state ends in the same state it starts. - identifyTimerCount = (*value) * 4; - - DeviceLayer::SystemLayer().CancelTimer(IdentifyTimerHandler, this); - DeviceLayer::SystemLayer().StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, this); - -exit: - return; -} - bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::Command * commandObj) { emberAfSendDefaultResponse(emberAfCurrentCommand(), EMBER_ZCL_STATUS_SUCCESS); diff --git a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h index c4549e6d3a938d..91cfcba3936eea 100644 --- a/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h +++ b/examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h @@ -45,7 +45,5 @@ class DeviceCallbacks : public chip::DeviceManager::CHIPDeviceManagerCallbacks #if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM void OnColorControlAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); #endif - void OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value); - bool mEndpointOnOffState[2]; }; diff --git a/examples/all-clusters-app/esp32/sdkconfig.defaults b/examples/all-clusters-app/esp32/sdkconfig.defaults index 8cb1093fbfb606..3da9d42e2bc2f3 100644 --- a/examples/all-clusters-app/esp32/sdkconfig.defaults +++ b/examples/all-clusters-app/esp32/sdkconfig.defaults @@ -41,6 +41,7 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv" # Vendor and product id CONFIG_DEVICE_VENDOR_ID=0x235A CONFIG_DEVICE_PRODUCT_ID=0x4541 +CONFIG_DEVICE_FIRMWARE_REVISION="prerelease" #enable debug shell CONFIG_ENABLE_CHIP_SHELL=y diff --git a/src/platform/ESP32/ConnectivityManagerImpl.cpp b/src/platform/ESP32/ConnectivityManagerImpl.cpp index 7d0428121bcbfd..6c8e8ee3aeacc5 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl.cpp @@ -42,6 +42,7 @@ using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; using namespace ::chip::TLV; +using namespace ::chip::app::Clusters::GeneralDiagnostics; namespace chip { namespace DeviceLayer { @@ -70,92 +71,62 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) #endif } -#if CHIP_DEVICE_CONFIG_ENABLE_WIFI -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiSecurityType(uint8_t & securityType) +static InterfaceType GetInterfaceType(const char * if_desc) { - securityType = 0; - wifi_ap_record_t ap_info; - esp_err_t err; - - err = esp_wifi_sta_get_ap_info(&ap_info); - if (err == ESP_OK) - { - securityType = ap_info.authmode; - } - return CHIP_NO_ERROR; + if (strncmp(if_desc, "ap", strnlen(if_desc, 2)) == 0 || strncmp(if_desc, "sta", strnlen(if_desc, 3)) == 0) + return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_WI_FI; + if (strncmp(if_desc, "openthread", strnlen(if_desc, 10)) == 0) + return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_THREAD; + if (strncmp(if_desc, "eth", strnlen(if_desc, 3)) == 0) + return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_ETHERNET; + return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_UNSPECIFIED; } -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiChannelNumber(uint16_t & channelNumber) +CHIP_ERROR ConnectivityManagerImpl::_GetNetworkInterfaces(NetworkInterface ** netifpp) { - channelNumber = 0; - wifi_ap_record_t ap_info; - esp_err_t err; - - err = esp_wifi_sta_get_ap_info(&ap_info); - if (err == ESP_OK) + esp_netif_t * netif = esp_netif_next(NULL); + NetworkInterface * head = NULL; + if (netif == NULL) { - channelNumber = ap_info.primary; + ChipLogError(DeviceLayer, "Failed to get network interfaces"); } - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiRssi(int8_t & rssi) -{ - rssi = 0; - wifi_ap_record_t ap_info; - esp_err_t err; - - err = esp_wifi_sta_get_ap_info(&ap_info); - - if (err == ESP_OK) + else { - rssi = ap_info.rssi; + for (esp_netif_t * ifa = netif; ifa != NULL; ifa = esp_netif_next(ifa)) + { + NetworkInterface * ifp = new NetworkInterface(); + strncpy(ifp->Name, esp_netif_get_ifkey(ifa), Inet::InterfaceId::kMaxIfNameLength); + ifp->Name[Inet::InterfaceId::kMaxIfNameLength - 1] = '\0'; + ifp->name = CharSpan(ifp->Name, strlen(ifp->Name)); + ifp->fabricConnected = true; + ifp->type = GetInterfaceType(esp_netif_get_desc(ifa)); + ifp->offPremiseServicesReachableIPv4 = false; + ifp->offPremiseServicesReachableIPv6 = false; + if (esp_netif_get_mac(ifa, ifp->MacAddress) != ESP_OK) + { + ChipLogError(DeviceLayer, "Failed to get network hardware address"); + } + else + { + ifp->hardwareAddress = ByteSpan(ifp->MacAddress, 6); + } + ifp->Next = head; + head = ifp; + } } + *netifpp = head; return CHIP_NO_ERROR; } -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiBeaconLostCount(uint32_t & beaconLostCount) -{ - beaconLostCount = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiCurrentMaxRate(uint64_t & currentMaxRate) -{ - currentMaxRate = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastRxCount(uint32_t & packetMulticastRxCount) +void ConnectivityManagerImpl::_ReleaseNetworkInterfaces(NetworkInterface * netifp) { - packetMulticastRxCount = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastTxCount(uint32_t & packetMulticastTxCount) -{ - packetMulticastTxCount = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastRxCount(uint32_t & packetUnicastRxCount) -{ - packetUnicastRxCount = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount) -{ - packetUnicastTxCount = 0; - return CHIP_NO_ERROR; -} - -CHIP_ERROR ConnectivityManagerImpl::_GetWiFiOverrunCount(uint64_t & overrunCount) -{ - overrunCount = 0; - return CHIP_NO_ERROR; + while (netifp) + { + NetworkInterface * del = netifp; + netifp = netifp->Next; + delete del; + } } -#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index e1ffdb0529d0e4..ed31629427dc67 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -86,6 +86,8 @@ class ConnectivityManagerImpl final : public ConnectivityManager, CHIP_ERROR _Init(void); void _OnPlatformEvent(const ChipDeviceEvent * event); + CHIP_ERROR _GetNetworkInterfaces(NetworkInterface ** netifpp); + void _ReleaseNetworkInterfaces(NetworkInterface * netifp); #if CHIP_DEVICE_CONFIG_ENABLE_WIFI using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags; // ===== Members that implement the ConnectivityManager abstract interface. @@ -113,7 +115,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void _OnWiFiScanDone(); void _OnWiFiStationProvisionChange(); + CHIP_ERROR _GetWiFiBssId(ByteSpan & BssId); CHIP_ERROR _GetWiFiSecurityType(uint8_t & securityType); + CHIP_ERROR _GetWiFiVersion(uint8_t & wifiVersion); CHIP_ERROR _GetWiFiChannelNumber(uint16_t & channelNumber); CHIP_ERROR _GetWiFiRssi(int8_t & rssi); CHIP_ERROR _GetWiFiBeaconLostCount(uint32_t & beaconLostCount); @@ -123,6 +127,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager, CHIP_ERROR _GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount); CHIP_ERROR _GetWiFiCurrentMaxRate(uint64_t & currentMaxRate); CHIP_ERROR _GetWiFiOverrunCount(uint64_t & overrunCount); + CHIP_ERROR _ResetWiFiNetworkDiagnosticsCounts(); // ===== Private members reserved for use by this class only. @@ -134,6 +139,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager, WiFiAPState mWiFiAPState; System::Clock::Timeout mWiFiStationReconnectInterval; System::Clock::Timeout mWiFiAPIdleTimeout; + uint8_t mWiFiMacAddress[kMaxHardwareAddrSize]; BitFlags mFlags; CHIP_ERROR InitWiFi(void); @@ -154,7 +160,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip); void OnStationIPv4AddressLost(void); void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); - #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI // ===== Members for internal use by the following friends. diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp index aa484a1b33ea80..6e976369a2289a 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp @@ -994,6 +994,154 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & PlatformMgr().PostEventOrDie(&event); } +uint8_t MapAuthModeToSecurityType(wifi_auth_mode_t authmode) +{ + switch (authmode) + { + case WIFI_AUTH_OPEN: + return 1; + case WIFI_AUTH_WEP: + return 2; + case WIFI_AUTH_WPA_PSK: + return 3; + case WIFI_AUTH_WPA2_PSK: + return 4; + case WIFI_AUTH_WPA3_PSK: + return 5; + default: + return 0; + } +} + +uint8_t GetWiFiVersionFromAPRecord(wifi_ap_record_t ap_info) +{ + if (ap_info.phy_11n) + return 3; + else if (ap_info.phy_11g) + return 2; + else if (ap_info.phy_11b) + return 1; + else + return 0; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiBssId(ByteSpan & BssId) +{ + wifi_ap_record_t ap_info; + esp_err_t err; + + err = esp_wifi_sta_get_ap_info(&ap_info); + if (err == ESP_OK) + { + memcpy(mWiFiMacAddress, ap_info.bssid, 6); + } + BssId = ByteSpan(mWiFiMacAddress, 6); + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiSecurityType(uint8_t & securityType) +{ + securityType = 0; + wifi_ap_record_t ap_info; + esp_err_t err; + + err = esp_wifi_sta_get_ap_info(&ap_info); + if (err == ESP_OK) + { + securityType = MapAuthModeToSecurityType(ap_info.authmode); + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiVersion(uint8_t & wifiVersion) +{ + wifiVersion = 0; + wifi_ap_record_t ap_info; + esp_err_t err; + err = esp_wifi_sta_get_ap_info(&ap_info); + if (err == ESP_OK) + { + wifiVersion = GetWiFiVersionFromAPRecord(ap_info); + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiChannelNumber(uint16_t & channelNumber) +{ + channelNumber = 0; + wifi_ap_record_t ap_info; + esp_err_t err; + + err = esp_wifi_sta_get_ap_info(&ap_info); + if (err == ESP_OK) + { + channelNumber = ap_info.primary; + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiRssi(int8_t & rssi) +{ + rssi = 0; + wifi_ap_record_t ap_info; + esp_err_t err; + + err = esp_wifi_sta_get_ap_info(&ap_info); + + if (err == ESP_OK) + { + rssi = ap_info.rssi; + } + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiBeaconLostCount(uint32_t & beaconLostCount) +{ + beaconLostCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiCurrentMaxRate(uint64_t & currentMaxRate) +{ + currentMaxRate = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastRxCount(uint32_t & packetMulticastRxCount) +{ + packetMulticastRxCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastTxCount(uint32_t & packetMulticastTxCount) +{ + packetMulticastTxCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastRxCount(uint32_t & packetUnicastRxCount) +{ + packetUnicastRxCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount) +{ + packetUnicastTxCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_GetWiFiOverrunCount(uint64_t & overrunCount) +{ + overrunCount = 0; + return CHIP_NO_ERROR; +} + +CHIP_ERROR ConnectivityManagerImpl::_ResetWiFiNetworkDiagnosticsCounts() +{ + return CHIP_NO_ERROR; +} + } // namespace DeviceLayer } // namespace chip