Skip to content

Commit

Permalink
ESP32: Add ICD support for Thread devices (#28359)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
2 people authored and pull[bot] committed Jan 31, 2024
1 parent 46dc7c0 commit 2628549
Show file tree
Hide file tree
Showing 19 changed files with 344 additions and 154 deletions.
4 changes: 4 additions & 0 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ if(CHIP_CODEGEN_PREGEN_DIR)
chip_gn_arg_append("chip_code_pre_generated_directory" "\"${CHIP_CODEGEN_PREGEN_DIR}\"")
endif()

if(CONFIG_ENABLE_ICD_SERVER)
chip_gn_arg_append("chip_enable_icd_server" "true")
endif()

if(CONFIG_ENABLE_PW_RPC)
string(APPEND chip_gn_args "import(\"//build_overrides/pigweed.gni\")\n")
chip_gn_arg_append("remove_default_configs" "[\"//third_party/connectedhomeip/third_party/pigweed/repo/pw_build:toolchain_cpp_standard\"]")
Expand Down
58 changes: 25 additions & 33 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,31 @@ menu "CHIP Device Layer"
help
Enables or Disables the support for Commissionable Device Type.

config ENABLE_ICD_SERVER
bool "Enable ICD server"
depends on OPENTHREAD_MTD
default n
help
Enables or Disables ICD server

config ICD_SLOW_POLL_INTERVAL
int "ICD Slow Polling Interval"
depends on ENABLE_ICD_SERVER
default 5000
help
The value defines the fastest frequency at which the device will typically receive
messages in Idle Mode. The Slow Polling interval MAY be the same as the Idle Mode
Interval.

config ICD_FAST_POLL_INTERVAL
int "ICD Fast Polling Interval"
depends on ENABLE_ICD_SERVER
default 200
help
Fast Polling defines the fastest frequency at which the device can receive messages
in Active Mode. The Fast Polling interval SHALL be smaller than the Active Mode
Interval.

config ENABLE_BG_EVENT_PROCESSING
bool "Enable Background event processing"
default n
Expand Down Expand Up @@ -435,39 +460,6 @@ menu "CHIP Device Layer"
The amount of time (in milliseconds) to wait for Internet connectivity to be established on
the device's WiFi station interface during a Network Provisioning TestConnectivity operation.

choice WIFI_POWER_SAVE_MODE
prompt "WiFi power-saving mode"
default WIFI_POWER_SAVE_MIN
depends on ENABLE_WIFI_STATION && !ENABLE_WIFI_AP
help
The Modem-sleep mode which refers to the legacy power-saving mode in the IEEE 802.11 protocol.

config WIFI_POWER_SAVE_MIN
bool "Minimal power-saving mode"
help
In minimum power-saving mode, station wakes up every DTIM to receive beacon.

config WIFI_POWER_SAVE_MAX
bool "Maximum power-saving mode"
help
In maximum power-saving mode, station wakes up in every listen interval to receive beacon.
Listen interval can be configured by calling API 'esp_wifi_set_config()' before connecting
to AP.

config WIFI_POWER_SAVE_NONE
bool "No power-saving"
help
No power save

endchoice

config WIFI_PS_LISTEN_INTERVAL
int "Listen interval for maximum power-saving mode"
depends on WIFI_POWER_SAVE_MAX
default 3
help
Interval for station to listen to beacon from AP. The unit of listen interval is one beacon interval.

endmenu

menu "WiFi AP Options"
Expand Down
5 changes: 5 additions & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ set(SRC_DIRS_LIST "${SRC_DIRS_LIST}"
)
endif (CONFIG_ENABLE_PW_RPC)

if (CONFIG_ENABLE_ICD_SERVER)
list(APPEND PRIV_INCLUDE_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/icd")
list(APPEND SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/icd")
endif()

set(PRIV_REQUIRES_LIST chip QRCode bt app_update nvs_flash spi_flash openthread)

if(${IDF_TARGET} STREQUAL "esp32")
Expand Down
7 changes: 5 additions & 2 deletions examples/all-clusters-app/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ menu "Demo"
config DEVICE_TYPE_ESP32_C6_DEVKITC
bool "ESP32C6-DevKitC"
depends on IDF_TARGET_ESP32C6
config DEVICE_TYPE_ESP32_H2_DEVKITM
bool "ESP32H2-DevKitM"
depends on IDF_TARGET_ESP32H2
endchoice

choice
Expand Down Expand Up @@ -84,7 +87,7 @@ menu "Demo"
config TFT_PREDEFINED_DISPLAY_TYPE
int
range 0 5
default 0 if DEVICE_TYPE_ESP32_DEVKITC
default 0 if DEVICE_TYPE_ESP32_DEVKITC || DEVICE_TYPE_ESP32_H2_DEVKITM
default 0 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC
default 3 if DEVICE_TYPE_M5STACK
default 4 if DEVICE_TYPE_ESP32_WROVER_KIT
Expand Down Expand Up @@ -115,7 +118,7 @@ menu "Demo"
range 0 40
default 2 if DEVICE_TYPE_ESP32_DEVKITC #Use LED1 (blue LED) as status LED on DevKitC
default 2 if DEVICE_TYPE_ESP32_ETHERNET_KIT
default 8 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC
default 8 if DEVICE_TYPE_ESP32_C3_DEVKITM || DEVICE_TYPE_ESP32_C2_DEVKITM || DEVICE_TYPE_ESP32_C6_DEVKITC || DEVICE_TYPE_ESP32_H2_DEVKITM
default 26 if DEVICE_TYPE_ESP32_WROVER_KIT
default 40 if DEVICE_TYPE_M5STACK
help
Expand Down
28 changes: 3 additions & 25 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <binding-handler.h>
#include <common/CHIPDeviceManager.h>
#include <common/Esp32AppServer.h>
#include <common/Esp32ThreadInit.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <examples/platform/esp32/mode-support/static-supported-modes-manager.h>
Expand All @@ -52,12 +53,6 @@
#include "Rpc.h"
#endif

#if CONFIG_OPENTHREAD_ENABLED
#include <common/OpenthreadConfig.h>
#include <platform/ESP32/OpenthreadLauncher.h>
#include <platform/ThreadStackManager.h>
#endif

#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
#include <platform/ESP32/ESP32FactoryDataProvider.h>
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
Expand All @@ -71,6 +66,7 @@
using namespace ::chip;
using namespace ::chip::Shell;
using namespace ::chip::DeviceManager;
using namespace ::chip::DeviceLayer;
using namespace ::chip::Credentials;

// Used to indicate that an IP address has been added to the QRCode
Expand Down Expand Up @@ -206,25 +202,7 @@ extern "C" void app_main()
{
ESP_LOGE(TAG, "GetAppTask().StartAppTask() failed : %" CHIP_ERROR_FORMAT, error.Format());
}
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
set_openthread_platform_config(&config);

if (DeviceLayer::ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to initialize Thread stack");
return;
}
if (DeviceLayer::ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to launch Thread task");
return;
}
#endif
ESPOpenThreadInit();

chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
}
Expand Down
78 changes: 78 additions & 0 deletions examples/all-clusters-app/esp32/sdkconfig.defaults.esp32h2
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
CONFIG_IDF_TARGET="esp32h2"
CONFIG_IDF_TARGET_ESP32H2_BETA_VERSION_2=y

# Default to 921600 baud when flashing and monitoring device
CONFIG_ESPTOOLPY_BAUD_921600B=y
CONFIG_ESPTOOLPY_BAUD=921600
CONFIG_ESPTOOLPY_COMPRESSED=y
CONFIG_ESPTOOLPY_FLASHMODE_QIO=y
CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
CONFIG_ESPTOOLPY_FLASHFREQ="40m"
CONFIG_ESPTOOLPY_MONITOR_BAUD_115200B=y
CONFIG_ESPTOOLPY_MONITOR_BAUD=115200

# libsodium
CONFIG_LIBSODIUM_USE_MBEDTLS_SHA=y


# NIMBLE
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_BT_NIMBLE_EXT_ADV=n
CONFIG_BT_NIMBLE_HCI_EVT_BUF_SIZE=70
CONFIG_USE_BLE_ONLY_FOR_COMMISSIONING=n

# Enable OpenThread
CONFIG_OPENTHREAD_ENABLED=y
CONFIG_OPENTHREAD_SRP_CLIENT=y
CONFIG_OPENTHREAD_LOG_LEVEL_DYNAMIC=n
CONFIG_OPENTHREAD_LOG_LEVEL_NOTE=y
CONFIG_OPENTHREAD_CLI=n
CONFIG_OPENTHREAD_DNS_CLIENT=y

# Disable lwip ipv6 autoconfig
CONFIG_LWIP_IPV6_AUTOCONFIG=n

# Use a custom partition table
CONFIG_PARTITION_TABLE_CUSTOM=y
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"

# LwIP config for OpenThread
CONFIG_LWIP_IPV6_NUM_ADDRESSES=8
CONFIG_LWIP_MULTICAST_PING=y

# mbedTLS
CONFIG_MBEDTLS_HARDWARE_AES=n
CONFIG_MBEDTLS_HARDWARE_MPI=n
CONFIG_MBEDTLS_HARDWARE_SHA=n
CONFIG_MBEDTLS_HARDWARE_ECC=y
CONFIG_MBEDTLS_ATCA_HW_ECDSA_SIGN=n
CONFIG_MBEDTLS_ATCA_HW_ECDSA_VERIFY=n
CONFIG_MBEDTLS_CMAC_C=y
CONFIG_MBEDTLS_SSL_PROTO_DTLS=y
CONFIG_MBEDTLS_KEY_EXCHANGE_ECJPAKE=y

# rtc clk for ble
# CONFIG_ESP32H2_RTC_CLK_SRC_EXT_CRYS=y

# MDNS platform
CONFIG_USE_MINIMAL_MDNS=n
CONFIG_ENABLE_EXTENDED_DISCOVERY=y

# FreeRTOS should use legacy API
CONFIG_FREERTOS_ENABLE_BACKWARD_COMPATIBILITY=y

# Disable STA and AP for ESP32H2
CONFIG_ENABLE_WIFI_STATION=n
CONFIG_ENABLE_WIFI_AP=n
# Enable this to avoid implicit declaration of function 'esp_send_assoc_resp'
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=y

# Enable OTA Requestor
CONFIG_ENABLE_OTA_REQUESTOR=y

# Enable chip shell
CONFIG_ENABLE_CHIP_SHELL=y

# Enable HKDF in mbedtls
CONFIG_MBEDTLS_HKDF_C=y
27 changes: 2 additions & 25 deletions examples/all-clusters-minimal-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <app/util/af.h>
#include <binding-handler.h>
#include <common/Esp32AppServer.h>
#include <common/Esp32ThreadInit.h>
#include <credentials/DeviceAttestationCredsProvider.h>
#include <credentials/examples/DeviceAttestationCredsExample.h>
#include <platform/ESP32/ESP32Utils.h>
Expand All @@ -51,12 +52,6 @@
#include "Rpc.h"
#endif

#if CONFIG_OPENTHREAD_ENABLED
#include <common/OpenthreadConfig.h>
#include <platform/ESP32/OpenthreadLauncher.h>
#include <platform/ThreadStackManager.h>
#endif

#if CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
#include <platform/ESP32/ESP32FactoryDataProvider.h>
#endif // CONFIG_ENABLE_ESP32_FACTORY_DATA_PROVIDER
Expand Down Expand Up @@ -188,25 +183,7 @@ extern "C" void app_main()
ESP_LOGE(TAG, "GetAppTask().StartAppTask() failed : %" CHIP_ERROR_FORMAT, error.Format());
}

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
set_openthread_platform_config(&config);

if (DeviceLayer::ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to initialize Thread stack");
return;
}
if (DeviceLayer::ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to launch Thread task");
return;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
ESPOpenThreadInit();
chip::DeviceLayer::PlatformMgr().ScheduleWork(InitServer, reinterpret_cast<intptr_t>(nullptr));
}

Expand Down
26 changes: 2 additions & 24 deletions examples/lighting-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "AppTask.h"
#include <common/CHIPDeviceManager.h>
#include <common/Esp32AppServer.h>
#include <common/Esp32ThreadInit.h>

#include "esp_log.h"
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
Expand All @@ -46,11 +47,6 @@
#include "Rpc.h"
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <common/OpenthreadConfig.h>
#include <platform/ESP32/OpenthreadLauncher.h>
#endif

#include "DeviceWithDisplay.h"

#if CONFIG_ENABLE_ESP32_DEVICE_INFO_PROVIDER
Expand Down Expand Up @@ -162,25 +158,7 @@ extern "C" void app_main()
#endif

SetDeviceAttestationCredentialsProvider(get_dac_provider());
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
esp_openthread_platform_config_t config = {
.radio_config = ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG(),
.host_config = ESP_OPENTHREAD_DEFAULT_HOST_CONFIG(),
.port_config = ESP_OPENTHREAD_DEFAULT_PORT_CONFIG(),
};
set_openthread_platform_config(&config);

if (ThreadStackMgr().InitThreadStack() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to initialize Thread stack");
return;
}
if (ThreadStackMgr().StartThreadTask() != CHIP_NO_ERROR)
{
ESP_LOGE(TAG, "Failed to launch Thread task");
return;
}
#endif
ESPOpenThreadInit();

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

Expand Down
11 changes: 11 additions & 0 deletions examples/platform/esp32/common/Esp32AppServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@

#include "Esp32AppServer.h"
#include "CHIPDeviceManager.h"
#include <app/InteractionModelEngine.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/clusters/ota-requestor/OTATestEventTriggerDelegate.h>
#include <app/server/Dnssd.h>
#include <app/server/Server.h>
#include <platform/ESP32/NetworkCommissioningDriver.h>
#if CONFIG_ENABLE_ICD_SERVER
#include <ICDSubscriptionCallback.h>
#endif
#include <string.h>

using namespace chip;
Expand Down Expand Up @@ -50,6 +54,9 @@ static uint8_t sTestEventTriggerEnableKey[TestEventTriggerDelegate::kEnableKeyLe
0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb,
0xcc, 0xdd, 0xee, 0xff };
#endif
#if CONFIG_ENABLE_ICD_SERVER
static ICDSubscriptionCallback sICDSubscriptionHandler;
#endif
} // namespace

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

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
sWiFiNetworkCommissioningInstance.Init();
Expand Down
Loading

0 comments on commit 2628549

Please sign in to comment.