Skip to content

Commit

Permalink
ESP32: Fix esp32 platform bugs (#12082)
Browse files Browse the repository at this point in the history
* 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 <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Dec 8, 2021
1 parent 0b84175 commit ce74f38
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 112 deletions.
77 changes: 41 additions & 36 deletions examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/Command.h>
#include <app/clusters/identify-server/identify-server.h>
#include <app/server/Dnssd.h>
#include <app/util/basic-types.h>
#include <app/util/util.h>
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
};
1 change: 1 addition & 0 deletions examples/all-clusters-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
117 changes: 44 additions & 73 deletions src/platform/ESP32/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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.

Expand All @@ -134,6 +139,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
WiFiAPState mWiFiAPState;
System::Clock::Timeout mWiFiStationReconnectInterval;
System::Clock::Timeout mWiFiAPIdleTimeout;
uint8_t mWiFiMacAddress[kMaxHardwareAddrSize];
BitFlags<Flags> mFlags;

CHIP_ERROR InitWiFi(void);
Expand All @@ -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.
Expand Down
Loading

0 comments on commit ce74f38

Please sign in to comment.