Skip to content

Commit d8f5caf

Browse files
committed
Add thread br app for ESP32
1 parent c57c373 commit d8f5caf

18 files changed

+5837
-1
lines changed

examples/platform/esp32/common/CommonDeviceCallbacks.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i
8484
// newly selected address.
8585
chip::app::DnssdServer::Instance().StartServer();
8686
}
87+
if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned)
88+
{
89+
appDelegate = DeviceCallbacksDelegate::Instance().GetAppDelegate();
90+
if (appDelegate != nullptr)
91+
{
92+
appDelegate->OnIPv6ConnectivityEstablished();
93+
}
94+
}
8795
break;
8896
}
8997

examples/platform/esp32/common/CommonDeviceCallbacks.h

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class DeviceCallbacksDelegate
4141
virtual void OnIPv4ConnectivityEstablished() {}
4242
virtual void OnIPv4ConnectivityLost() {}
4343
virtual void OnDnssdInitialized() {}
44+
virtual void OnIPv6ConnectivityEstablished() {}
4445
DeviceCallbacksDelegate * mDelegate = nullptr;
4546
void SetAppDelegate(DeviceCallbacksDelegate * delegate) { mDelegate = delegate; }
4647
DeviceCallbacksDelegate * GetAppDelegate() { return mDelegate; }

examples/platform/esp32/common/Esp32AppServer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static constexpr char TAG[] = "ESP32Appserver";
4848

4949
namespace {
5050
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
51-
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
51+
#if (CHIP_DEVICE_CONFIG_ENABLE_THREAD && !defined(CONFIG_OPENTHREAD_BORDER_ROUTER)) || CHIP_DEVICE_CONFIG_ENABLE_ETHERNET
5252
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0xFFFE;
5353
#else
5454
constexpr chip::EndpointId kNetworkCommissioningEndpointWiFi = 0;

examples/platform/esp32/common/Esp32ThreadInit.h

+47
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,57 @@
2222
#if CONFIG_OPENTHREAD_ENABLED
2323
#include "esp_openthread_types.h"
2424

25+
#if CONFIG_OPENTHREAD_RADIO_NATIVE
2526
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
2627
{ \
2728
.radio_mode = RADIO_MODE_NATIVE, \
2829
}
30+
#elif CONFIG_OPENTHREAD_RADIO_SPINEL_UART
31+
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
32+
{ \
33+
.radio_mode = RADIO_MODE_UART_RCP, \
34+
.radio_uart_config = { \
35+
.port = UART_NUM_1, \
36+
.uart_config = \
37+
{ \
38+
.baud_rate = 460800, \
39+
.data_bits = UART_DATA_8_BITS, \
40+
.parity = UART_PARITY_DISABLE, \
41+
.stop_bits = UART_STOP_BITS_1, \
42+
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE, \
43+
.rx_flow_ctrl_thresh = 0, \
44+
.source_clk = UART_SCLK_DEFAULT, \
45+
}, \
46+
.rx_pin = GPIO_NUM_17, \
47+
.tx_pin = GPIO_NUM_18, \
48+
}, \
49+
}
50+
#else
51+
#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \
52+
{ \
53+
.radio_mode = RADIO_MODE_SPI_RCP, \
54+
.radio_spi_config = { \
55+
.host_device = SPI2_HOST, \
56+
.dma_channel = 2, \
57+
.spi_interface = \
58+
{ \
59+
.mosi_io_num = 11, \
60+
.sclk_io_num = 12, \
61+
.miso_io_num = 13, \
62+
}, \
63+
.spi_device = \
64+
{ \
65+
.cs_ena_pretrans = 2, \
66+
.input_delay_ns = 100, \
67+
.mode = 0, \
68+
.clock_speed_hz = 2500 * 1000, \
69+
.spics_io_num = 10, \
70+
.queue_size = 5, \
71+
}, \
72+
.intr_pin = 8, \
73+
}, \
74+
}
75+
#endif // CONFIG_OPENTHREAD_RADIO_SPINEL_UART OR CONFIG_OPENTHREAD_RADIO_SPINEL_SPI
2976

3077
#define ESP_OPENTHREAD_DEFAULT_HOST_CONFIG() \
3178
{ \
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*.vscode
2+
3+
/build/
4+
/sdkconfig
5+
/sdkconfig.old
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#
2+
# Copyright (c) 2021 Project CHIP Authors
3+
# All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# The following lines of boilerplate have to be in your project's
18+
# CMakeLists in this exact order for cmake to work correctly
19+
cmake_minimum_required(VERSION 3.5)
20+
21+
set(PROJECT_VER "v1.0")
22+
set(PROJECT_VER_NUMBER 1)
23+
24+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
25+
include(${CMAKE_CURRENT_LIST_DIR}/../../common/cmake/idf_flashing.cmake)
26+
27+
set(EXTRA_COMPONENT_DIRS
28+
"${CMAKE_CURRENT_LIST_DIR}/third_party/connectedhomeip/config/esp32/components"
29+
"~/workspace/gitlab-esp/esp-openthread/components"
30+
)
31+
32+
project(chip-thread-br-app)
33+
34+
# C++17 is required for RPC build.
35+
idf_build_set_property(CXX_COMPILE_OPTIONS "-std=gnu++17;-Os;-DCHIP_HAVE_CONFIG_H" APPEND)
36+
idf_build_set_property(C_COMPILE_OPTIONS "-Os" APPEND)
37+
# For the C3, project_include.cmake sets -Wno-format, but does not clear various
38+
# flags that depend on -Wformat
39+
idf_build_set_property(COMPILE_OPTIONS "-Wno-format-nonliteral;-Wno-format-security" APPEND)
40+
41+
# We don't need Thread Network Commissioning Driver
42+
idf_build_set_property(COMPILE_OPTIONS "-D_NO_NETWORK_COMMISSIONING_DRIVER_" APPEND)
43+
44+
# -Wmaybe-uninitialized has too many false positives, including on std::optional
45+
# and chip::Optional. Make it nonfatal.
46+
#
47+
# See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80635
48+
idf_build_set_property(COMPILE_OPTIONS "-Wno-error=maybe-uninitialized" APPEND)
49+
50+
flashing_script()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#
2+
# Copyright (c) 2022 Project CHIP Authors
3+
# All rights reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
19+
idf_component_register(PRIV_INCLUDE_DIRS
20+
"${CMAKE_CURRENT_LIST_DIR}/include"
21+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
22+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32"
23+
SRC_DIRS
24+
"${CMAKE_CURRENT_LIST_DIR}"
25+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes"
26+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated"
27+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers"
28+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota"
29+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common"
30+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension"
31+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server"
32+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/util"
33+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/reporting"
34+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/icd/server"
35+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/access-control-server"
36+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/administrator-commissioning-server"
37+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/basic-information"
38+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/bindings"
39+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/descriptor"
40+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/diagnostic-logs-server"
41+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ethernet-network-diagnostics-server"
42+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/fixed-label-server"
43+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server"
44+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-diagnostics-server"
45+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/identify-server"
46+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/localization-configuration-server"
47+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/time-format-localization-server"
48+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning"
49+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server"
50+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
51+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/power-source-configuration-server"
52+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/software-diagnostics-server"
53+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-network-diagnostics-server"
54+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/thread-border-router-management-server"
55+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/user-label-server"
56+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/wifi-network-diagnostics-server"
57+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor"
58+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/groups-server"
59+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/group-key-mgmt-server"
60+
"${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/time-synchronization-server")
61+
62+
get_filename_component(CHIP_ROOT ${CMAKE_SOURCE_DIR}/third_party/connectedhomeip REALPATH)
63+
64+
include("${CHIP_ROOT}/build/chip/esp32/esp32_codegen.cmake")
65+
66+
chip_app_component_codegen("${CHIP_ROOT}/examples/thread-br-app/thread-br-common/thread-br-app.matter")
67+
chip_app_component_zapgen("${CHIP_ROOT}/examples/thread-br-app/thread-br-common/thread-br-app.zap")
68+
69+
set_property(TARGET ${COMPONENT_LIB} PROPERTY CXX_STANDARD 17)
70+
target_compile_options(${COMPONENT_LIB} PRIVATE "-DCHIP_HAVE_CONFIG_H")
71+
target_compile_options(${COMPONENT_LIB} PUBLIC
72+
"-DCHIP_ADDRESS_RESOLVE_IMPL_INCLUDE_HEADER=<lib/address_resolve/AddressResolve_DefaultImpl.h>"
73+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
*
3+
* Copyright (c) 2022 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/**
20+
* @file DeviceCallbacks.cpp
21+
*
22+
* Implements all the callbacks to the application from the CHIP Stack
23+
*
24+
**/
25+
26+
#include "DeviceCallbacks.h"
27+
#include "common/Esp32ThreadInit.h"
28+
#include "platform/ESP32/OpenthreadLauncher.h"
29+
#include "platform/ThreadStackManager.h"
30+
#include <esp_log.h>
31+
#include <esp_openthread_border_router.h>
32+
#include <esp_openthread_lock.h>
33+
#include <lib/support/logging/CHIPLogging.h>
34+
35+
static const char TAG[] = "thread-br-app-callbacks";
36+
37+
using namespace chip;
38+
using namespace chip::Inet;
39+
using namespace chip::System;
40+
using namespace chip::app::Clusters;
41+
42+
void AppDeviceCallbacks::PostAttributeChangeCallback(EndpointId endpointId, ClusterId clusterId, AttributeId attributeId,
43+
uint8_t type, uint16_t size, uint8_t * value)
44+
{
45+
ESP_LOGI(TAG, "PostAttributeChangeCallback - Cluster ID: '0x%" PRIx32 "', EndPoint ID: '0x%x', Attribute ID: '0x%" PRIx32 "'",
46+
clusterId, endpointId, attributeId);
47+
48+
ESP_LOGI(TAG, "Current free heap: %u\n", static_cast<unsigned int>(heap_caps_get_free_size(MALLOC_CAP_8BIT)));
49+
}
50+
51+
static void InitThreadBRHandler(void)
52+
{
53+
}
54+
55+
void AppDeviceCallbacksDelegate::OnIPv6ConnectivityEstablished(void)
56+
{
57+
static bool sThreadBRInitialized = false;
58+
if (!sThreadBRInitialized)
59+
{
60+
esp_openthread_lock_acquire(portMAX_DELAY);
61+
esp_openthread_border_router_init();
62+
esp_openthread_lock_release();
63+
sThreadBRInitialized = true;
64+
}
65+
}

0 commit comments

Comments
 (0)