Skip to content

Commit 732f797

Browse files
ESP32: Add ICD support for Thread devices (project-chip#28359)
* ESP32: Add ICD support for Thread devices * Restyled by clang-format * fix compile issue * fix compile issue --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 0ba5315 commit 732f797

19 files changed

+344
-154
lines changed

config/esp32/components/chip/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ if(CHIP_CODEGEN_PREGEN_DIR)
132132
chip_gn_arg_append("chip_code_pre_generated_directory" "\"${CHIP_CODEGEN_PREGEN_DIR}\"")
133133
endif()
134134

135+
if(CONFIG_ENABLE_ICD_SERVER)
136+
chip_gn_arg_append("chip_enable_icd_server" "true")
137+
endif()
138+
135139
if(CONFIG_ENABLE_PW_RPC)
136140
string(APPEND chip_gn_args "import(\"//build_overrides/pigweed.gni\")\n")
137141
chip_gn_arg_append("remove_default_configs" "[\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:toolchain_cpp_standard\"]")

config/esp32/components/chip/Kconfig

+25-33
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,31 @@ menu "CHIP Device Layer"
292292
help
293293
Enables or Disables the support for Commissionable Device Type.
294294

295+
config ENABLE_ICD_SERVER
296+
bool "Enable ICD server"
297+
depends on OPENTHREAD_MTD
298+
default n
299+
help
300+
Enables or Disables ICD server
301+
302+
config ICD_SLOW_POLL_INTERVAL
303+
int "ICD Slow Polling Interval"
304+
depends on ENABLE_ICD_SERVER
305+
default 5000
306+
help
307+
The value defines the fastest frequency at which the device will typically receive
308+
messages in Idle Mode. The Slow Polling interval MAY be the same as the Idle Mode
309+
Interval.
310+
311+
config ICD_FAST_POLL_INTERVAL
312+
int "ICD Fast Polling Interval"
313+
depends on ENABLE_ICD_SERVER
314+
default 200
315+
help
316+
Fast Polling defines the fastest frequency at which the device can receive messages
317+
in Active Mode. The Fast Polling interval SHALL be smaller than the Active Mode
318+
Interval.
319+
295320
config ENABLE_BG_EVENT_PROCESSING
296321
bool "Enable Background event processing"
297322
default n
@@ -435,39 +460,6 @@ menu "CHIP Device Layer"
435460
The amount of time (in milliseconds) to wait for Internet connectivity to be established on
436461
the device's WiFi station interface during a Network Provisioning TestConnectivity operation.
437462

438-
choice WIFI_POWER_SAVE_MODE
439-
prompt "WiFi power-saving mode"
440-
default WIFI_POWER_SAVE_MIN
441-
depends on ENABLE_WIFI_STATION && !ENABLE_WIFI_AP
442-
help
443-
The Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol.
444-
445-
config WIFI_POWER_SAVE_MIN
446-
bool "Minimal power-saving mode"
447-
help
448-
In minimum power-saving mode, station wakes up every DTIM to receive beacon.
449-
450-
config WIFI_POWER_SAVE_MAX
451-
bool "Maximum power-saving mode"
452-
help
453-
In maximum power-saving mode, station wakes up in every listen interval to receive beacon.
454-
Listen interval can be configured by calling API 'esp_wifi_set_config()' before connecting
455-
to AP.
456-
457-
config WIFI_POWER_SAVE_NONE
458-
bool "No power-saving"
459-
help
460-
No power save
461-
462-
endchoice
463-
464-
config WIFI_PS_LISTEN_INTERVAL
465-
int "Listen interval for maximum power-saving mode"
466-
depends on WIFI_POWER_SAVE_MAX
467-
default 3
468-
help
469-
Interval for station to listen to beacon from AP. The unit of listen interval is one beacon interval.
470-
471463
endmenu
472464

473465
menu "WiFi AP Options"

examples/all-clusters-app/esp32/main/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ set(SRC_DIRS_LIST "${SRC_DIRS_LIST}"
125125
)
126126
endif (CONFIG_ENABLE_PW_RPC)
127127

128+
if (CONFIG_ENABLE_ICD_SERVER)
129+
list(APPEND PRIV_INCLUDE_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/icd")
130+
list(APPEND SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/icd")
131+
endif()
132+
128133
set(PRIV_REQUIRES_LIST chip QRCode bt app_update nvs_flash spi_flash openthread)
129134

130135
if(${IDF_TARGET} STREQUAL "esp32")

examples/all-clusters-app/esp32/main/Kconfig.projbuild

+5-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ menu "Demo"
5454
config DEVICE_TYPE_ESP32_C6_DEVKITC
5555
bool "ESP32C6-DevKitC"
5656
depends on IDF_TARGET_ESP32C6
57+
config DEVICE_TYPE_ESP32_H2_DEVKITM
58+
bool "ESP32H2-DevKitM"
59+
depends on IDF_TARGET_ESP32H2
5760
endchoice
5861

5962
choice
@@ -84,7 +87,7 @@ menu "Demo"
8487
config TFT_PREDEFINED_DISPLAY_TYPE
8588
int
8689
range 0 5
87-
default 0 if DEVICE_TYPE_ESP32_DEVKITC
90+
default 0 if DEVICE_TYPE_ESP32_DEVKITC || DEVICE_TYPE_ESP32_H2_DEVKITM
8891
default 0 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC
8992
default 3 if DEVICE_TYPE_M5STACK
9093
default 4 if DEVICE_TYPE_ESP32_WROVER_KIT
@@ -115,7 +118,7 @@ menu "Demo"
115118
range 0 40
116119
default 2 if DEVICE_TYPE_ESP32_DEVKITC #Use LED1 (blue LED) as status LED on DevKitC
117120
default 2 if DEVICE_TYPE_ESP32_ETHERNET_KIT
118-
default 8 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC
121+
default 8 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC || DEVICE_TYPE_ESP32_H2_DEVKITM
119122
default 26 if DEVICE_TYPE_ESP32_WROVER_KIT
120123
default 40 if DEVICE_TYPE_M5STACK
121124
help

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

+3-25
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <binding-handler.h>
3939
#include <common/CHIPDeviceManager.h>
4040
#include <common/Esp32AppServer.h>
41+
#include <common/Esp32ThreadInit.h>
4142
#include <credentials/DeviceAttestationCredsProvider.h>
4243
#include <credentials/examples/DeviceAttestationCredsExample.h>
4344
#include <examples/platform/esp32/mode-support/static-supported-modes-manager.h>
@@ -52,12 +53,6 @@
5253
#include "Rpc.h"
5354
#endif
5455

55-
#if CONFIG_OPENTHREAD_ENABLED
56-
#include <common/OpenthreadConfig.h>
57-
#include <platform/ESP32/OpenthreadLauncher.h>
58-
#include <platform/ThreadStackManager.h>
59-
#endif
60-
6156
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
6257
#include <platform/ESP32/ESP32FactoryDataProvider.h>
6358
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
@@ -71,6 +66,7 @@
7166
using namespace ::chip;
7267
using namespace ::chip::Shell;
7368
using namespace ::chip::DeviceManager;
69+
using namespace ::chip::DeviceLayer;
7470
using namespace ::chip::Credentials;
7571

7672
// Used to indicate that an IP address has been added to the QRCode
@@ -206,25 +202,7 @@ extern "C" void app_main()
206202
{
207203
ESP_LOGE(TAG, "GetAppTask().StartAppTask() failed : %" CHIP_ERROR_FORMAT, error.Format());
208204
}
209-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
210-
esp_openthread_platform_config_t config = {
211-
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
212-
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
213-
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
214-
};
215-
set_openthread_platform_config(&config);
216-
217-
if (DeviceLayer::ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
218-
{
219-
ESP_LOGE(TAG, "Failed to initialize Thread stack");
220-
return;
221-
}
222-
if (DeviceLayer::ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
223-
{
224-
ESP_LOGE(TAG, "Failed to launch Thread task");
225-
return;
226-
}
227-
#endif
205+
ESPOpenThreadInit();
228206

229207
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
230208
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
CONFIG_IDF_TARGET="esp32h2"
2+
CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_2=y
3+
4+
# Default to 921600 baud when flashing and monitoring device
5+
CONFIG_ESPTOOLPY_BAUD_921600B=y
6+
CONFIG_ESPTOOLPY_BAUD=921600
7+
CONFIG_ESPTOOLPY_COMPRESSED=y
8+
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
9+
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
10+
CONFIG_ESPTOOLPY_FLASHFREQ="40m"
11+
CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y
12+
CONFIG_ESPTOOLPY_MONITOR_BAUD=115200
13+
14+
# libsodium
15+
CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y
16+
17+
18+
# NIMBLE
19+
CONFIG_BT_ENABLED=y
20+
CONFIG_BT_NIMBLE_ENABLED=y
21+
CONFIG_BT_NIMBLE_EXT_ADV=n
22+
CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70
23+
CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=n
24+
25+
# Enable OpenThread
26+
CONFIG_OPENTHREAD_ENABLED=y
27+
CONFIG_OPENTHREAD_SRP_CLIENT=y
28+
CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n
29+
CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y
30+
CONFIG_OPENTHREAD_CLI=n
31+
CONFIG_OPENTHREAD_DNS_CLIENT=y
32+
33+
# Disable lwip ipv6 autoconfig
34+
CONFIG_LWIP_IPV6_AUTOCONFIG=n
35+
36+
# Use a custom partition table
37+
CONFIG_PARTITION_TABLE_CUSTOM=y
38+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
39+
40+
# LwIP config for OpenThread
41+
CONFIG_LWIP_IPV6_NUM_ADDRESSES=8
42+
CONFIG_LWIP_MULTICAST_PING=y
43+
44+
# mbedTLS
45+
CONFIG_MBEDTLS_HARDWARE_AES=n
46+
CONFIG_MBEDTLS_HARDWARE_MPI=n
47+
CONFIG_MBEDTLS_HARDWARE_SHA=n
48+
CONFIG_MBEDTLS_HARDWARE_ECC=y
49+
CONFIG_MBEDTLS_ATCA_HW_ECDSA_SIGN=n
50+
CONFIG_MBEDTLS_ATCA_HW_ECDSA_VERIFY=n
51+
CONFIG_MBEDTLS_CMAC_C=y
52+
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
53+
CONFIG_MBEDTLS_KEY_EXCHANGE_ECJPAKE=y
54+
55+
# rtc clk for ble
56+
# CONFIG_ESP32H2_RTC_CLK_SRC_EXT_CRYS=y
57+
58+
# MDNS platform
59+
CONFIG_USE_MINIMAL_MDNS=n
60+
CONFIG_ENABLE_EXTENDED_DISCOVERY=y
61+
62+
# FreeRTOS should use legacy API
63+
CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y
64+
65+
# Disable STA and AP for ESP32H2
66+
CONFIG_ENABLE_WIFI_STATION=n
67+
CONFIG_ENABLE_WIFI_AP=n
68+
# Enable this to avoid implicit declaration of function 'esp_send_assoc_resp'
69+
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y
70+
71+
# Enable OTA Requestor
72+
CONFIG_ENABLE_OTA_REQUESTOR=y
73+
74+
# Enable chip shell
75+
CONFIG_ENABLE_CHIP_SHELL=y
76+
77+
# Enable HKDF in mbedtls
78+
CONFIG_MBEDTLS_HKDF_C=y

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

+2-25
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <app/util/af.h>
4040
#include <binding-handler.h>
4141
#include <common/Esp32AppServer.h>
42+
#include <common/Esp32ThreadInit.h>
4243
#include <credentials/DeviceAttestationCredsProvider.h>
4344
#include <credentials/examples/DeviceAttestationCredsExample.h>
4445
#include <platform/ESP32/ESP32Utils.h>
@@ -51,12 +52,6 @@
5152
#include "Rpc.h"
5253
#endif
5354

54-
#if CONFIG_OPENTHREAD_ENABLED
55-
#include <common/OpenthreadConfig.h>
56-
#include <platform/ESP32/OpenthreadLauncher.h>
57-
#include <platform/ThreadStackManager.h>
58-
#endif
59-
6055
#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
6156
#include <platform/ESP32/ESP32FactoryDataProvider.h>
6257
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
@@ -188,25 +183,7 @@ extern "C" void app_main()
188183
ESP_LOGE(TAG, "GetAppTask().StartAppTask() failed : %" CHIP_ERROR_FORMAT, error.Format());
189184
}
190185

191-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
192-
esp_openthread_platform_config_t config = {
193-
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
194-
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
195-
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
196-
};
197-
set_openthread_platform_config(&config);
198-
199-
if (DeviceLayer::ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
200-
{
201-
ESP_LOGE(TAG, "Failed to initialize Thread stack");
202-
return;
203-
}
204-
if (DeviceLayer::ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
205-
{
206-
ESP_LOGE(TAG, "Failed to launch Thread task");
207-
return;
208-
}
209-
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
186+
ESPOpenThreadInit();
210187
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
211188
}
212189

examples/lighting-app/esp32/main/main.cpp

+2-24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "AppTask.h"
2121
#include <common/CHIPDeviceManager.h>
2222
#include <common/Esp32AppServer.h>
23+
#include <common/Esp32ThreadInit.h>
2324

2425
#include "esp_log.h"
2526
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
@@ -46,11 +47,6 @@
4647
#include "Rpc.h"
4748
#endif
4849

49-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
50-
#include <common/OpenthreadConfig.h>
51-
#include <platform/ESP32/OpenthreadLauncher.h>
52-
#endif
53-
5450
#include "DeviceWithDisplay.h"
5551

5652
#if CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER
@@ -162,25 +158,7 @@ extern "C" void app_main()
162158
#endif
163159

164160
SetDeviceAttestationCredentialsProvider(get_dac_provider());
165-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
166-
esp_openthread_platform_config_t config = {
167-
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
168-
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
169-
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
170-
};
171-
set_openthread_platform_config(&config);
172-
173-
if (ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
174-
{
175-
ESP_LOGE(TAG, "Failed to initialize Thread stack");
176-
return;
177-
}
178-
if (ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
179-
{
180-
ESP_LOGE(TAG, "Failed to launch Thread task");
181-
return;
182-
}
183-
#endif
161+
ESPOpenThreadInit();
184162

185163
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
186164

examples/platform/esp32/common/Esp32AppServer.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818

1919
#include "Esp32AppServer.h"
2020
#include "CHIPDeviceManager.h"
21+
#include <app/InteractionModelEngine.h>
2122
#include <app/clusters/network-commissioning/network-commissioning.h>
2223
#include <app/clusters/ota-requestor/OTATestEventTriggerDelegate.h>
2324
#include <app/server/Dnssd.h>
2425
#include <app/server/Server.h>
2526
#include <platform/ESP32/NetworkCommissioningDriver.h>
27+
#if CONFIG_ENABLE_ICD_SERVER
28+
#include <ICDSubscriptionCallback.h>
29+
#endif
2630
#include <string.h>
2731

2832
using namespace chip;
@@ -50,6 +54,9 @@ static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLe
5054
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
5155
0xcc, 0xdd, 0xee, 0xff };
5256
#endif
57+
#if CONFIG_ENABLE_ICD_SERVER
58+
static ICDSubscriptionCallback sICDSubscriptionHandler;
59+
#endif
5360
} // namespace
5461

5562
#if CONFIG_TEST_EVENT_TRIGGER_ENABLED
@@ -113,6 +120,10 @@ void Esp32AppServer::Init(AppDelegate * sAppDelegate)
113120
initParams.appDelegate = sAppDelegate;
114121
}
115122
chip::Server::GetInstance().Init(initParams);
123+
#if CONFIG_ENABLE_ICD_SERVER
124+
// Register ICD subscription callback to match subscription max intervals to its idle time interval
125+
chip::app::InteractionModelEngine::GetInstance()->RegisterReadHandlerAppCallback(&sICDSubscriptionHandler);
126+
#endif // CONFIG_ENABLE_ICD_SERVER
116127

117128
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
118129
sWiFiNetworkCommissioningInstance.Init();

0 commit comments

Comments
 (0)