Skip to content

Commit

Permalink
[ESP32] decouple Thread support from specific chip model (#10005)
Browse files Browse the repository at this point in the history
This change makes OpenThread available on all ESP32 series when
CONFIG_OPENTHREAD_ENABLED is on.
  • Loading branch information
gjc13 authored and pull[bot] committed Nov 24, 2021
1 parent fb937e3 commit 3059133
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 379 deletions.
11 changes: 6 additions & 5 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ if(NOT CHIP_ROOT)
endif()

set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs)
if(${IDF_TARGET} STREQUAL "esp32h2")
list(APPEND CHIP_REQURIE_COMPONENTS openthread)
endif()

if (CONFIG_ENABLE_CHIP_SHELL)
list(APPEND CHIP_REQURIE_COMPONENTS console)
endif()

if (CONFIG_OPENTHREAD_ENABLED)
list(APPEND CHIP_REQURIE_COMPONENTS openthread)
endif()

idf_component_register(SRCS chip.c chip.cpp
PRIV_REQUIRES ${CHIP_REQURIE_COMPONENTS})

Expand Down Expand Up @@ -210,9 +211,9 @@ endif()
idf_component_get_property(main_lib main COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${main_lib}>)

target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group
target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group
${chip_libraries}
$<TARGET_FILE:mbedcrypto> $<TARGET_FILE:${esp32_mbedtls_lib}>
$<TARGET_FILE:mbedcrypto> $<TARGET_FILE:${esp32_mbedtls_lib}>
-Wl,--end-group)

# Make the component dependent on our CHIP build
Expand Down
4 changes: 4 additions & 0 deletions examples/all-clusters-app/esp32/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ if("${CONFIG_DEVICE_TYPE_ESP32_C3_DEVKITM}" STREQUAL "y")
list(APPEND PRIV_REQUIRES_LIST led_strip)
endif()

if (CONFIG_OPENTHREAD_ENABLED)
list(APPEND PRIV_REQUIRES_LIST openthread)
endif()

idf_component_register(PRIV_INCLUDE_DIRS ${PRIV_INCLUDE_DIRS_LIST}
SRC_DIRS ${SRC_DIRS_LIST}
PRIV_REQUIRES ${PRIV_REQUIRES_LIST})
Expand Down
111 changes: 111 additions & 0 deletions examples/all-clusters-app/esp32/main/OpenThreadLaunch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "OpenThreadLaunch.h"

#include "sdkconfig.h"

#if CONFIG_OPENTHREAD_ENABLED

#include <stdio.h>
#include <unistd.h>

#include "driver/uart.h"
#include "esp_err.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_netif.h"
#include "esp_netif_types.h"
#include "esp_openthread.h"
#include "esp_openthread_lock.h"
#include "esp_openthread_netif_glue.h"
#include "esp_openthread_types.h"
#include "esp_vfs_eventfd.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hal/uart_types.h"
#include "openthread/instance.h"
#include "openthread/logging.h"
#include "openthread/platform/logging.h"
#include "sdkconfig.h"

#if CONFIG_IDF_TARGET_ESP32H2
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_NATIVE, \
}
#else
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
{ \
.radio_mode = RADIO_MODE_UART_RCP, \
.radio_uart_config = { \
.port = 1, \
.uart_config = \
{ \
.baud_rate = 115200, \
.data_bits = UART_DATA_8_BITS, \
.parity = UART_PARITY_DISABLE, \
.stop_bits = UART_STOP_BITS_1, \
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
.rx_flow_ctrl_thresh = 0, \
.source_clk = UART_SCLK_APB, \
}, \
.rx_pin = 4, \
.tx_pin = 5, \
}, \
}
#endif

#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
{ \
.host_connection_mode = HOST_CONNECTION_MODE_NONE, \
}

#define ESP_OPENTHREAD_DEFAULT_PORT_CONFIG() \
{ \
.storage_partition_name = "ot_storage", .netif_queue_size = 10, .task_queue_size = 10, \
}

static void OpenThreadTask(void * aContext)
{
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(),
};

// Initialize the OpenThread stack
ESP_ERROR_CHECK(esp_openthread_init(&config));
(void) otLoggingSetLevel(OT_LOG_LEVEL_INFO);

// Run the main loop
esp_openthread_launch_mainloop();

esp_vfs_eventfd_unregister();
vTaskDelete(NULL);
}

void LaunchOpenThread()
{
esp_vfs_eventfd_config_t eventfd_config = {
.max_fds = 3,
};
ESP_ERROR_CHECK(esp_vfs_eventfd_register(&eventfd_config));
xTaskCreate(OpenThreadTask, "ot", 10240, xTaskGetCurrentTaskHandle(), 5, NULL);
}

#endif // CONFIG_OPENTHREAD_ENABLED
20 changes: 20 additions & 0 deletions examples/all-clusters-app/esp32/main/include/OpenThreadLaunch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

void LaunchOpenThread();
10 changes: 10 additions & 0 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Globals.h"
#include "LEDWidget.h"
#include "ListScreen.h"
#include "OpenThreadLaunch.h"
#include "QRCodeScreen.h"
#include "ScreenManager.h"
#include "WiFiWidget.h"
Expand Down Expand Up @@ -70,6 +71,10 @@
#include "Rpc.h"
#endif

#if CONFIG_OPENTHREAD_ENABLED
#include <platform/ThreadStackManager.h>
#endif

using namespace ::chip;
using namespace ::chip::Credentials;
using namespace ::chip::DeviceManager;
Expand Down Expand Up @@ -611,6 +616,11 @@ extern "C" void app_main()
chip::LaunchShell();
#endif // CONFIG_ENABLE_CHIP_SHELL

#if CONFIG_OPENTHREAD_ENABLED
LaunchOpenThread();
ThreadStackMgr().InitThreadStack();
#endif

CHIPDeviceManager & deviceMgr = CHIPDeviceManager::GetInstance();

CHIP_ERROR error = deviceMgr.Init(&EchoCallbacks);
Expand Down
4 changes: 2 additions & 2 deletions examples/all-clusters-app/esp32/partitions.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x6000,
phy_init, data, phy, , 0x1000,
# Factory partition size about 1.9MB
factory, app, factory, , 1945K,
factory, app, factory, , 1900K,
ot_storage, data, 0x3a, , 0x2000,
21 changes: 10 additions & 11 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static_library("ESP32") {
"CHIPDevicePlatformEvent.h",
"ConfigurationManagerImpl.cpp",
"ConfigurationManagerImpl.h",
"ConnectivityManagerImpl.cpp",
"ConnectivityManagerImpl.h",
"DeviceNetworkProvisioningDelegateImpl.cpp",
"DeviceNetworkProvisioningDelegateImpl.h",
Expand All @@ -51,23 +52,21 @@ static_library("ESP32") {
"${chip_root}/src/crypto",
"${chip_root}/src/platform:platform_base",
]

if (chip_enable_wifi) {
sources += [
"ConnectivityManagerImpl_WiFi.cpp",
"WiFiProvisioning.cpp",
"WiFiProvisioning.h",
]
}

if (chip_enable_openthread) {
sources += [
"../OpenThread/MdnsImpl.cpp",
"../OpenThread/OpenThreadUtils.cpp",
"ConnectivityManagerImpl_Thread.cpp",
"ConnectivityManagerImpl_Thread.h",
"ESPThreadConfig.h",
"ThreadStackManagerImpl.cpp",
"ThreadStackManagerImpl.h",
]
} else {
sources += [
"ConnectivityManagerImpl_WiFi.cpp",
"ConnectivityManagerImpl_WiFi.h",
"MdnsImpl.cpp",
"ServiceProvisioning.cpp",
"ServiceProvisioning.h",
]
}
}
5 changes: 3 additions & 2 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@
#define CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_PRODUCT_REVISION CONFIG_DEFAULT_DEVICE_PRODUCT_REVISION
#define CHIP_DEVICE_CONFIG_DEVICE_FIRMWARE_REVISION_STRING CONFIG_DEVICE_FIRMWARE_REVISION

#if CONFIG_IDF_TARGET_ESP32H2
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD CONFIG_OPENTHREAD_ENABLED
#define CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT CONFIG_OPENTHREAD_SRP_CLIENT
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI !CONFIG_OPENTHREAD_ENABLED

#if CONFIG_IDF_TARGET_ESP32H2
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI 0
#else
#define CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL CONFIG_WIFI_STATION_RECONNECT_INTERVAL
#define CHIP_DEVICE_CONFIG_MAX_SCAN_NETWORKS_RESULTS CONFIG_MAX_SCAN_NETWORKS_RESULTS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/ConnectivityManager.h>

#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.cpp>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/internal/GenericConnectivityManagerImpl_Thread.cpp>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.cpp>
#endif

#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
Expand All @@ -43,13 +50,23 @@ ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;

CHIP_ERROR ConnectivityManagerImpl::_Init()
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_Init();
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
InitWiFi();
#endif
return CHIP_NO_ERROR;
}

void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_OnPlatformEvent(event);
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
OnWiFiPlatformEvent(event);
#endif
}

} // namespace DeviceLayer
Expand Down
Loading

0 comments on commit 3059133

Please sign in to comment.