diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 2e1988e4d88672..615e07525e4290 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -32,7 +32,6 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lock-app/linux/src" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" diff --git a/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt b/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt index c0fafc5861ff08..a51bc9cdf120de 100644 --- a/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-minimal-app/esp32/main/CMakeLists.txt @@ -29,7 +29,6 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/all-clusters-minimal-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" diff --git a/examples/bridge-app/esp32/main/CMakeLists.txt b/examples/bridge-app/esp32/main/CMakeLists.txt index 6dee91afaf7952..31365efaf2f14d 100644 --- a/examples/bridge-app/esp32/main/CMakeLists.txt +++ b/examples/bridge-app/esp32/main/CMakeLists.txt @@ -49,7 +49,6 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/on-off-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" PRIV_REQUIRES chip QRCode bt) diff --git a/examples/light-switch-app/esp32/main/CMakeLists.txt b/examples/light-switch-app/esp32/main/CMakeLists.txt index bca86c5408a1c5..5e52ded45aeed1 100644 --- a/examples/light-switch-app/esp32/main/CMakeLists.txt +++ b/examples/light-switch-app/esp32/main/CMakeLists.txt @@ -27,7 +27,6 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/light-switch-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" diff --git a/examples/lighting-app/esp32/main/CMakeLists.txt b/examples/lighting-app/esp32/main/CMakeLists.txt index 7b2f18556db7a5..bd4914505a7e4d 100644 --- a/examples/lighting-app/esp32/main/CMakeLists.txt +++ b/examples/lighting-app/esp32/main/CMakeLists.txt @@ -31,7 +31,6 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lighting-app/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/lighting-app/lighting-common/src" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" diff --git a/examples/lock-app/esp32/main/CMakeLists.txt b/examples/lock-app/esp32/main/CMakeLists.txt index 1bf3401dc890c9..85c931431c2d00 100644 --- a/examples/lock-app/esp32/main/CMakeLists.txt +++ b/examples/lock-app/esp32/main/CMakeLists.txt @@ -152,8 +152,7 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated/attributes" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/app-common/app-common/zap-generated" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/zzz_generated/lock-app/zap-generated" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/server" diff --git a/examples/ota-provider-app/esp32/main/CMakeLists.txt b/examples/ota-provider-app/esp32/main/CMakeLists.txt index 01b8c67ad9d788..10d5eadde50d1c 100644 --- a/examples/ota-provider-app/esp32/main/CMakeLists.txt +++ b/examples/ota-provider-app/esp32/main/CMakeLists.txt @@ -52,7 +52,6 @@ idf_component_register(PRIV_INCLUDE_DIRS "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-provider" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/ota-provider-app/ota-provider-common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" EXCLUDE_SRCS diff --git a/examples/ota-requestor-app/esp32/main/CMakeLists.txt b/examples/ota-requestor-app/esp32/main/CMakeLists.txt index bcb8d1908d0619..306707421ec134 100644 --- a/examples/ota-requestor-app/esp32/main/CMakeLists.txt +++ b/examples/ota-requestor-app/esp32/main/CMakeLists.txt @@ -59,8 +59,7 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/ota-requestor" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/shell_extension" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" + "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/ota" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" ) diff --git a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp index 899ca7e3b3d381..df02cad16443de 100644 --- a/examples/platform/esp32/common/CommonDeviceCallbacks.cpp +++ b/examples/platform/esp32/common/CommonDeviceCallbacks.cpp @@ -32,7 +32,6 @@ #include "esp_err.h" #include "esp_heap_caps.h" #include "esp_log.h" -#include "route_hook/esp_route_hook.h" #include #include #include @@ -121,10 +120,6 @@ void CommonDeviceCallbacks::DeviceEventCallback(const ChipDeviceEvent * event, i // newly selected address. chip::app::DnssdServer::Instance().StartServer(); } - if (event->InterfaceIpAddressChanged.Type == InterfaceIpChangeType::kIpV6_Assigned) - { - ESP_ERROR_CHECK(esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"))); - } break; } diff --git a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt index 0f138ab0d442e8..cae8e844be7978 100644 --- a/examples/temperature-measurement-app/esp32/main/CMakeLists.txt +++ b/examples/temperature-measurement-app/esp32/main/CMakeLists.txt @@ -52,7 +52,6 @@ set(SRC_DIRS_LIST "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/general-commissioning-server" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/network-commissioning" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/src/app/clusters/operational-credentials-server" - "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/route_hook" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/platform/esp32/common" "${CMAKE_SOURCE_DIR}/third_party/connectedhomeip/examples/providers" ) diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index d5f8b2d1590dea..59ab8d1dcafb1c 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -82,6 +82,10 @@ static_library("ESP32") { "ConnectivityManagerImpl_WiFi.cpp", "NetworkCommissioningDriver.cpp", "NetworkCommissioningDriver.h", + "route_hook/ESP32RouteHook.c", + "route_hook/ESP32RouteHook.h", + "route_hook/ESP32RouteTable.c", + "route_hook/ESP32RouteTable.h", ] if (chip_mdns == "platform") { sources += [ diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp index c068bba75cb492..831775ef50436f 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp @@ -28,6 +28,7 @@ #include #include #include +#include #include #include "esp_event.h" @@ -1094,6 +1095,8 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & event.Type = DeviceEventType::kInterfaceIpAddressChanged; event.InterfaceIpAddressChanged.Type = InterfaceIpChangeType::kIpV6_Assigned; PlatformMgr().PostEventOrDie(&event); + + esp_route_hook_init(esp_netif_get_handle_from_ifkey("WIFI_STA_DEF")); } } // namespace DeviceLayer diff --git a/examples/platform/esp32/route_hook/esp_route_hook.c b/src/platform/ESP32/route_hook/ESP32RouteHook.c similarity index 96% rename from examples/platform/esp32/route_hook/esp_route_hook.c rename to src/platform/ESP32/route_hook/ESP32RouteHook.c index ab60fc23a8a55a..b6acadeda48eac 100644 --- a/examples/platform/esp32/route_hook/esp_route_hook.c +++ b/src/platform/ESP32/route_hook/ESP32RouteHook.c @@ -4,15 +4,15 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "esp_route_hook.h" +#include +#include #include #include #include "esp_check.h" #include "esp_err.h" #include "esp_netif.h" -#include "esp_route_table.h" #include "lwip/icmp6.h" #include "lwip/mld6.h" @@ -26,8 +26,6 @@ #define PIO_FLAG_ON_LINK (1 << 7) #define PIO_FLAG_AUTO_CONFIG (1 << 6) -#define TAG "ROUTE_HOOK" - typedef struct esp_route_hook_t { struct netif * netif; @@ -137,12 +135,12 @@ static uint8_t icmp6_raw_recv_handler(void * arg, struct raw_pcb * pcb, struct p if (p->tot_len != p->len) { - ESP_LOGW(TAG, "Ignore segmented ICMP packet"); + ESP_LOGI(TAG, "Ignore segmented ICMP packet"); return 0; } if (p->tot_len <= sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr)) { - ESP_LOGW(TAG, "Ignore invalid ICMP packet"); + ESP_LOGI(TAG, "Ignore invalid ICMP packet"); return 0; } if (!ip6_addr_islinklocal(&dest) && !ip6_addr_isallnodes_linklocal(&dest) && !ip6_addr_isallrouters_linklocal(&dest)) diff --git a/examples/platform/esp32/route_hook/esp_route_hook.h b/src/platform/ESP32/route_hook/ESP32RouteHook.h similarity index 100% rename from examples/platform/esp32/route_hook/esp_route_hook.h rename to src/platform/ESP32/route_hook/ESP32RouteHook.h diff --git a/examples/platform/esp32/route_hook/esp_route_table.c b/src/platform/ESP32/route_hook/ESP32RouteTable.c similarity index 56% rename from examples/platform/esp32/route_hook/esp_route_table.c rename to src/platform/ESP32/route_hook/ESP32RouteTable.c index 8d2b5d1cb71cdd..73263d4bd54778 100644 --- a/examples/platform/esp32/route_hook/esp_route_table.c +++ b/src/platform/ESP32/route_hook/ESP32RouteTable.c @@ -4,7 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ -#include "esp_route_table.h" +#include #include @@ -14,48 +14,53 @@ #include "lwip/netif.h" #include "lwip/timeouts.h" -#define MAX_RIO_ROUTE 20 #define MAX_RIO_TIMEOUT UINT32_MAX / (1000 * 4) // lwIP defined reasonable timeout value -#define TAG "ROUTE_HOOK" - -static esp_route_entry_t s_route_entries[MAX_RIO_ROUTE]; +static esp_route_entry_t * s_route_entries = NULL; static esp_route_entry_t * find_route_entry(const esp_route_entry_t * route_entry) { - for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next) { - if (s_route_entries[i].netif == NULL) - { - break; - } - if (s_route_entries[i].netif == route_entry->netif && s_route_entries[i].prefix_length == route_entry->prefix_length && - memcmp(s_route_entries[i].gateway.addr, route_entry->gateway.addr, sizeof(route_entry->gateway.addr)) == 0 && - memcmp(s_route_entries[i].prefix.addr, route_entry->prefix.addr, route_entry->prefix_length / 8) == 0) + if (iter->netif == route_entry->netif && iter->prefix_length == route_entry->prefix_length && + memcmp(iter->gateway.addr, route_entry->gateway.addr, sizeof(route_entry->gateway.addr)) == 0 && + memcmp(iter->prefix.addr, route_entry->prefix.addr, route_entry->prefix_length / 8) == 0) { - return &s_route_entries[i]; + return iter; } } + return NULL; } -static esp_route_entry_t * find_empty_route_entry(void) +static esp_err_t remove_route_entry(esp_route_entry_t * route_entry) { - for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + if (s_route_entries == route_entry) + { + s_route_entries = s_route_entries->next; + free(route_entry); + return ESP_OK; + } + + for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next) { - if (s_route_entries[i].netif == NULL) + if (iter->next == route_entry) { - return &s_route_entries[i]; + iter->next = route_entry->next; + free(route_entry); + return ESP_OK; } } - return NULL; + + ESP_LOGW(TAG, "The given route entry is not found"); + return ESP_ERR_NOT_FOUND; } static void route_timeout_handler(void * arg) { esp_route_entry_t * route = (esp_route_entry_t *) arg; - esp_route_table_remove_route_entry(route); + remove_route_entry(route); } esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * route_entry) @@ -69,16 +74,21 @@ esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * ro if (entry == NULL) { - entry = find_empty_route_entry(); + entry = (esp_route_entry_t *) malloc(sizeof(esp_route_entry_t)); if (entry == NULL) { + ESP_LOGW(TAG, "Cannot allocate route entry"); return NULL; } + entry->netif = route_entry->netif; entry->gateway = route_entry->gateway; ip6_addr_assign_zone(&entry->gateway, IP6_UNICAST, entry->netif); entry->prefix = route_entry->prefix; entry->prefix_length = route_entry->prefix_length; + + entry->next = s_route_entries; + s_route_entries = entry; } else { @@ -90,25 +100,8 @@ esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * ro { sys_timeout(entry->lifetime_seconds * 1000, route_timeout_handler, entry); } - return entry; -} -esp_err_t esp_route_table_remove_route_entry(esp_route_entry_t * route_entry) -{ - if (route_entry < &s_route_entries[0] || route_entry > &s_route_entries[LWIP_ARRAYSIZE(s_route_entries)]) - { - return ESP_ERR_INVALID_ARG; - } - route_entry->netif = NULL; - for (esp_route_entry_t * moved = route_entry; moved < &s_route_entries[LWIP_ARRAYSIZE(s_route_entries) - 1]; moved++) - { - *moved = *(moved + 1); - if (moved->netif == NULL) - { - break; - } - } - return ESP_OK; + return entry; } static inline bool is_better_route(const esp_route_entry_t * lhs, const esp_route_entry_t * rhs) @@ -134,15 +127,11 @@ struct netif * lwip_hook_ip6_route(const ip6_addr_t * src, const ip6_addr_t * de { esp_route_entry_t * route = NULL; - for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next) { - if (s_route_entries[i].netif == NULL) - { - break; - } - if (route_match(&s_route_entries[i], dest) && is_better_route(&s_route_entries[i], route)) + if (route_match(iter, dest) && is_better_route(iter, route)) { - route = &s_route_entries[i]; + route = iter; } } @@ -160,16 +149,11 @@ const ip6_addr_t * lwip_hook_nd6_get_gw(struct netif * netif, const ip6_addr_t * { esp_route_entry_t * route = NULL; - for (size_t i = 0; i < LWIP_ARRAYSIZE(s_route_entries); i++) + for (esp_route_entry_t * iter = s_route_entries; iter != NULL; iter = iter->next) { - if (s_route_entries[i].netif == NULL) - { - break; - } - if (s_route_entries[i].netif == netif && route_match(&s_route_entries[i], dest) && - is_better_route(&s_route_entries[i], route)) + if (iter->netif == netif && route_match(iter, dest) && is_better_route(iter, route)) { - route = &s_route_entries[i]; + route = iter; } } diff --git a/examples/platform/esp32/route_hook/esp_route_table.h b/src/platform/ESP32/route_hook/ESP32RouteTable.h similarity index 82% rename from examples/platform/esp32/route_hook/esp_route_table.h rename to src/platform/ESP32/route_hook/ESP32RouteTable.h index e69fee1ad293bf..40d06ee103187b 100644 --- a/examples/platform/esp32/route_hook/esp_route_table.h +++ b/src/platform/ESP32/route_hook/ESP32RouteTable.h @@ -12,11 +12,13 @@ extern "C" { #endif +#define TAG "ROUTE_HOOK" + /** * @brief Route table entry * */ -typedef struct +typedef struct esp_route_entry_t { ip6_addr_t prefix; uint8_t prefix_length; @@ -24,6 +26,7 @@ typedef struct int8_t preference; uint32_t lifetime_seconds; struct netif * netif; + struct esp_route_entry_t * next; } esp_route_entry_t; /** @@ -38,18 +41,6 @@ typedef struct */ esp_route_entry_t * esp_route_table_add_route_entry(const esp_route_entry_t * route_entry); -/** - * @brief Removes an entry from the route table - * - * @param[in] route_entry The route entry to be removed - * - * @return - * - ESP_OK - * - ESP_ERR_INVALID_ARG The provided route_entry is not in the route table. - * - */ -esp_err_t esp_route_table_remove_route_entry(esp_route_entry_t * route_entry); - /** * @brief The lwIP ip6 route hook, called by the lwIP function ip6_route when sending packets. *