Skip to content

Commit a11ddc6

Browse files
wqx6restyled-commits
authored andcommitted
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 <[email protected]>
1 parent ac58ced commit a11ddc6

File tree

6 files changed

+240
-112
lines changed

6 files changed

+240
-112
lines changed

examples/all-clusters-app/esp32/main/DeviceCallbacks.cpp

+41-36
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include <app-common/zap-generated/attribute-id.h>
3737
#include <app-common/zap-generated/cluster-id.h>
3838
#include <app/Command.h>
39+
#include <app/clusters/identify-server/identify-server.h>
3940
#include <app/server/Dnssd.h>
4041
#include <app/util/basic-types.h>
4142
#include <app/util/util.h>
@@ -49,9 +50,48 @@ using namespace ::chip::Inet;
4950
using namespace ::chip::System;
5051
using namespace ::chip::DeviceLayer;
5152

52-
uint32_t identifyTimerCount;
5353
constexpr uint32_t kIdentifyTimerDelayMS = 250;
5454

55+
void OnIdentifyTriggerEffect(Identify * identify)
56+
{
57+
switch (identify->mCurrentEffectIdentifier)
58+
{
59+
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK:
60+
statusLED1.Blink(kIdentifyTimerDelayMS * 2);
61+
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK");
62+
break;
63+
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE:
64+
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE");
65+
break;
66+
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY:
67+
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY");
68+
break;
69+
case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE:
70+
ChipLogProgress(Zcl, "EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE");
71+
break;
72+
default:
73+
ChipLogProgress(Zcl, "No identifier effect");
74+
break;
75+
}
76+
return;
77+
}
78+
79+
Identify gIdentify0 = {
80+
chip::EndpointId{ 0 },
81+
[](Identify *) { ChipLogProgress(Zcl, "onIdentifyStart"); },
82+
[](Identify *) { ChipLogProgress(Zcl, "onIdentifyStop"); },
83+
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED,
84+
OnIdentifyTriggerEffect,
85+
};
86+
87+
Identify gIdentify1 = {
88+
chip::EndpointId{ 1 },
89+
[](Identify *) { ChipLogProgress(Zcl, "onIdentifyStart"); },
90+
[](Identify *) { ChipLogProgress(Zcl, "onIdentifyStop"); },
91+
EMBER_ZCL_IDENTIFY_IDENTIFY_TYPE_VISIBLE_LED,
92+
OnIdentifyTriggerEffect,
93+
};
94+
5595
void DeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, intptr_t arg)
5696
{
5797
switch (event->Type)
@@ -108,10 +148,6 @@ void DeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, Cluster
108148
OnOnOffPostAttributeChangeCallback(endpointId, attributeId, value);
109149
break;
110150

111-
case ZCL_IDENTIFY_CLUSTER_ID:
112-
OnIdentifyPostAttributeChangeCallback(endpointId, attributeId, value);
113-
break;
114-
115151
case ZCL_LEVEL_CONTROL_CLUSTER_ID:
116152
OnLevelControlAttributeChangeCallback(endpointId, attributeId, value);
117153
break;
@@ -218,37 +254,6 @@ void DeviceCallbacks::OnColorControlAttributeChangeCallback(EndpointId endpointI
218254
}
219255
#endif
220256

221-
void IdentifyTimerHandler(Layer * systemLayer, void * appState)
222-
{
223-
statusLED1.Animate();
224-
225-
if (identifyTimerCount)
226-
{
227-
systemLayer->StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, appState);
228-
// Decrement the timer count.
229-
identifyTimerCount--;
230-
}
231-
}
232-
233-
void DeviceCallbacks::OnIdentifyPostAttributeChangeCallback(EndpointId endpointId, AttributeId attributeId, uint8_t * value)
234-
{
235-
VerifyOrExit(attributeId == ZCL_IDENTIFY_TIME_ATTRIBUTE_ID, ESP_LOGI(TAG, "Unhandled Attribute ID: '0x%04x", attributeId));
236-
VerifyOrExit(endpointId == 1, ESP_LOGE(TAG, "Unexpected EndPoint ID: `0x%02x'", endpointId));
237-
238-
statusLED1.Blink(kIdentifyTimerDelayMS * 2);
239-
240-
// timerCount represents the number of callback executions before we stop the timer.
241-
// value is expressed in seconds and the timer is fired every 250ms, so just multiply value by 4.
242-
// Also, we want timerCount to be odd number, so the ligth state ends in the same state it starts.
243-
identifyTimerCount = (*value) * 4;
244-
245-
DeviceLayer::SystemLayer().CancelTimer(IdentifyTimerHandler, this);
246-
DeviceLayer::SystemLayer().StartTimer(Clock::Milliseconds32(kIdentifyTimerDelayMS), IdentifyTimerHandler, this);
247-
248-
exit:
249-
return;
250-
}
251-
252257
bool emberAfBasicClusterMfgSpecificPingCallback(chip::app::Command * commandObj)
253258
{
254259
emberAfSendDefaultResponse(emberAfCurrentCommand(), EMBER_ZCL_STATUS_SUCCESS);

examples/all-clusters-app/esp32/main/include/DeviceCallbacks.h

-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,5 @@ class DeviceCallbacks : public chip::DeviceManager::CHIPDeviceManagerCallbacks
4545
#if CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM
4646
void OnColorControlAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value);
4747
#endif
48-
void OnIdentifyPostAttributeChangeCallback(chip::EndpointId endpointId, chip::AttributeId attributeId, uint8_t * value);
49-
5048
bool mEndpointOnOffState[2];
5149
};

examples/all-clusters-app/esp32/sdkconfig.defaults

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"
4141
# Vendor and product id
4242
CONFIG_DEVICE_VENDOR_ID=0x235A
4343
CONFIG_DEVICE_PRODUCT_ID=0x4541
44+
CONFIG_DEVICE_FIRMWARE_REVISION="prerelease"
4445

4546
#enable debug shell
4647
CONFIG_ENABLE_CHIP_SHELL=y

src/platform/ESP32/ConnectivityManagerImpl.cpp

+44-73
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ using namespace ::chip;
4242
using namespace ::chip::Inet;
4343
using namespace ::chip::System;
4444
using namespace ::chip::TLV;
45+
using namespace ::chip::app::Clusters::GeneralDiagnostics;
4546

4647
namespace chip {
4748
namespace DeviceLayer {
@@ -70,92 +71,62 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
7071
#endif
7172
}
7273

73-
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
74-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiSecurityType(uint8_t & securityType)
74+
static InterfaceType GetInterfaceType(const char * if_desc)
7575
{
76-
securityType = 0;
77-
wifi_ap_record_t ap_info;
78-
esp_err_t err;
79-
80-
err = esp_wifi_sta_get_ap_info(&ap_info);
81-
if (err == ESP_OK)
82-
{
83-
securityType = ap_info.authmode;
84-
}
85-
return CHIP_NO_ERROR;
76+
if (strncmp(if_desc, "ap", strnlen(if_desc, 2)) == 0 || strncmp(if_desc, "sta", strnlen(if_desc, 3)) == 0)
77+
return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_WI_FI;
78+
if (strncmp(if_desc, "openthread", strnlen(if_desc, 10)) == 0)
79+
return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_THREAD;
80+
if (strncmp(if_desc, "eth", strnlen(if_desc, 3)) == 0)
81+
return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_ETHERNET;
82+
return InterfaceType::EMBER_ZCL_INTERFACE_TYPE_UNSPECIFIED;
8683
}
8784

88-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiChannelNumber(uint16_t & channelNumber)
85+
CHIP_ERROR ConnectivityManagerImpl::_GetNetworkInterfaces(NetworkInterface ** netifpp)
8986
{
90-
channelNumber = 0;
91-
wifi_ap_record_t ap_info;
92-
esp_err_t err;
93-
94-
err = esp_wifi_sta_get_ap_info(&ap_info);
95-
if (err == ESP_OK)
87+
esp_netif_t * netif = esp_netif_next(NULL);
88+
NetworkInterface * head = NULL;
89+
if (netif == NULL)
9690
{
97-
channelNumber = ap_info.primary;
91+
ChipLogError(DeviceLayer, "Failed to get network interfaces");
9892
}
99-
return CHIP_NO_ERROR;
100-
}
101-
102-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiRssi(int8_t & rssi)
103-
{
104-
rssi = 0;
105-
wifi_ap_record_t ap_info;
106-
esp_err_t err;
107-
108-
err = esp_wifi_sta_get_ap_info(&ap_info);
109-
110-
if (err == ESP_OK)
93+
else
11194
{
112-
rssi = ap_info.rssi;
95+
for (esp_netif_t * ifa = netif; ifa != NULL; ifa = esp_netif_next(ifa))
96+
{
97+
NetworkInterface * ifp = new NetworkInterface();
98+
strncpy(ifp->Name, esp_netif_get_ifkey(ifa), Inet::InterfaceId::kMaxIfNameLength);
99+
ifp->Name[Inet::InterfaceId::kMaxIfNameLength - 1] = '\0';
100+
ifp->name = CharSpan(ifp->Name, strlen(ifp->Name));
101+
ifp->fabricConnected = true;
102+
ifp->type = GetInterfaceType(esp_netif_get_desc(ifa));
103+
ifp->offPremiseServicesReachableIPv4 = false;
104+
ifp->offPremiseServicesReachableIPv6 = false;
105+
if (esp_netif_get_mac(ifa, ifp->MacAddress) != ESP_OK)
106+
{
107+
ChipLogError(DeviceLayer, "Failed to get network hardware address");
108+
}
109+
else
110+
{
111+
ifp->hardwareAddress = ByteSpan(ifp->MacAddress, 6);
112+
}
113+
ifp->Next = head;
114+
head = ifp;
115+
}
113116
}
117+
*netifpp = head;
114118
return CHIP_NO_ERROR;
115119
}
116120

117-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiBeaconLostCount(uint32_t & beaconLostCount)
118-
{
119-
beaconLostCount = 0;
120-
return CHIP_NO_ERROR;
121-
}
122-
123-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiCurrentMaxRate(uint64_t & currentMaxRate)
124-
{
125-
currentMaxRate = 0;
126-
return CHIP_NO_ERROR;
127-
}
128-
129-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastRxCount(uint32_t & packetMulticastRxCount)
121+
void ConnectivityManagerImpl::_ReleaseNetworkInterfaces(NetworkInterface * netifp)
130122
{
131-
packetMulticastRxCount = 0;
132-
return CHIP_NO_ERROR;
133-
}
134-
135-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketMulticastTxCount(uint32_t & packetMulticastTxCount)
136-
{
137-
packetMulticastTxCount = 0;
138-
return CHIP_NO_ERROR;
139-
}
140-
141-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastRxCount(uint32_t & packetUnicastRxCount)
142-
{
143-
packetUnicastRxCount = 0;
144-
return CHIP_NO_ERROR;
145-
}
146-
147-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount)
148-
{
149-
packetUnicastTxCount = 0;
150-
return CHIP_NO_ERROR;
151-
}
152-
153-
CHIP_ERROR ConnectivityManagerImpl::_GetWiFiOverrunCount(uint64_t & overrunCount)
154-
{
155-
overrunCount = 0;
156-
return CHIP_NO_ERROR;
123+
while (netifp)
124+
{
125+
NetworkInterface * del = netifp;
126+
netifp = netifp->Next;
127+
delete del;
128+
}
157129
}
158-
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
159130

160131
} // namespace DeviceLayer
161132
} // namespace chip

src/platform/ESP32/ConnectivityManagerImpl.h

+6-1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
8686
CHIP_ERROR _Init(void);
8787
void _OnPlatformEvent(const ChipDeviceEvent * event);
8888

89+
CHIP_ERROR _GetNetworkInterfaces(NetworkInterface ** netifpp);
90+
void _ReleaseNetworkInterfaces(NetworkInterface * netifp);
8991
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
9092
using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags;
9193
// ===== Members that implement the ConnectivityManager abstract interface.
@@ -113,7 +115,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
113115
void _OnWiFiScanDone();
114116
void _OnWiFiStationProvisionChange();
115117

118+
CHIP_ERROR _GetWiFiBssId(ByteSpan & BssId);
116119
CHIP_ERROR _GetWiFiSecurityType(uint8_t & securityType);
120+
CHIP_ERROR _GetWiFiVersion(uint8_t & wifiVersion);
117121
CHIP_ERROR _GetWiFiChannelNumber(uint16_t & channelNumber);
118122
CHIP_ERROR _GetWiFiRssi(int8_t & rssi);
119123
CHIP_ERROR _GetWiFiBeaconLostCount(uint32_t & beaconLostCount);
@@ -123,6 +127,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
123127
CHIP_ERROR _GetWiFiPacketUnicastTxCount(uint32_t & packetUnicastTxCount);
124128
CHIP_ERROR _GetWiFiCurrentMaxRate(uint64_t & currentMaxRate);
125129
CHIP_ERROR _GetWiFiOverrunCount(uint64_t & overrunCount);
130+
CHIP_ERROR _ResetWiFiNetworkDiagnosticsCounts();
126131

127132
// ===== Private members reserved for use by this class only.
128133

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

139145
CHIP_ERROR InitWiFi(void);
@@ -154,7 +160,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
154160
void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip);
155161
void OnStationIPv4AddressLost(void);
156162
void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip);
157-
158163
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
159164

160165
// ===== Members for internal use by the following friends.

0 commit comments

Comments
 (0)