From 3059133862a7f7ea629f47a899719ae9c4828fd8 Mon Sep 17 00:00:00 2001 From: Jiacheng Guo Date: Wed, 29 Sep 2021 04:02:41 +0800 Subject: [PATCH] [ESP32] decouple Thread support from specific chip model (#10005) This change makes OpenThread available on all ESP32 series when CONFIG_OPENTHREAD_ENABLED is on. --- config/esp32/components/chip/CMakeLists.txt | 11 +- .../esp32/main/CMakeLists.txt | 4 + .../esp32/main/OpenThreadLaunch.cpp | 111 ++++++++++ .../esp32/main/include/OpenThreadLaunch.h | 20 ++ examples/all-clusters-app/esp32/main/main.cpp | 10 + .../all-clusters-app/esp32/partitions.csv | 4 +- src/platform/ESP32/BUILD.gn | 21 +- src/platform/ESP32/CHIPDevicePlatformConfig.h | 5 +- ...Thread.cpp => ConnectivityManagerImpl.cpp} | 17 ++ src/platform/ESP32/ConnectivityManagerImpl.h | 202 +++++++++++++++++- .../ESP32/ConnectivityManagerImpl_Thread.h | 94 -------- .../ESP32/ConnectivityManagerImpl_WiFi.cpp | 34 +-- .../ESP32/ConnectivityManagerImpl_WiFi.h | 192 ----------------- .../DeviceNetworkProvisioningDelegateImpl.cpp | 2 +- src/platform/ESP32/ThreadStackManagerImpl.cpp | 41 +--- src/platform/ESP32/ThreadStackManagerImpl.h | 3 - ...eProvisioning.cpp => WiFiProvisioning.cpp} | 2 +- ...rviceProvisioning.h => WiFiProvisioning.h} | 0 18 files changed, 394 insertions(+), 379 deletions(-) create mode 100644 examples/all-clusters-app/esp32/main/OpenThreadLaunch.cpp create mode 100644 examples/all-clusters-app/esp32/main/include/OpenThreadLaunch.h rename src/platform/ESP32/{ConnectivityManagerImpl_Thread.cpp => ConnectivityManagerImpl.cpp} (83%) delete mode 100644 src/platform/ESP32/ConnectivityManagerImpl_Thread.h delete mode 100644 src/platform/ESP32/ConnectivityManagerImpl_WiFi.h rename src/platform/ESP32/{ServiceProvisioning.cpp => WiFiProvisioning.cpp} (98%) rename src/platform/ESP32/{ServiceProvisioning.h => WiFiProvisioning.h} (100%) diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index 336b693a244de8..1b3f6331127010 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -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}) @@ -210,9 +211,9 @@ endif() idf_component_get_property(main_lib main COMPONENT_LIB) list(APPEND chip_libraries $) -target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group +target_link_libraries(${COMPONENT_LIB} INTERFACE -Wl,--start-group ${chip_libraries} - $ $ + $ $ -Wl,--end-group) # Make the component dependent on our CHIP build diff --git a/examples/all-clusters-app/esp32/main/CMakeLists.txt b/examples/all-clusters-app/esp32/main/CMakeLists.txt index 3cfc62a3fcb11e..9106669c7d3038 100644 --- a/examples/all-clusters-app/esp32/main/CMakeLists.txt +++ b/examples/all-clusters-app/esp32/main/CMakeLists.txt @@ -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}) diff --git a/examples/all-clusters-app/esp32/main/OpenThreadLaunch.cpp b/examples/all-clusters-app/esp32/main/OpenThreadLaunch.cpp new file mode 100644 index 00000000000000..e3372456226e0b --- /dev/null +++ b/examples/all-clusters-app/esp32/main/OpenThreadLaunch.cpp @@ -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 +#include + +#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 diff --git a/examples/all-clusters-app/esp32/main/include/OpenThreadLaunch.h b/examples/all-clusters-app/esp32/main/include/OpenThreadLaunch.h new file mode 100644 index 00000000000000..86b02d6328fdb5 --- /dev/null +++ b/examples/all-clusters-app/esp32/main/include/OpenThreadLaunch.h @@ -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(); diff --git a/examples/all-clusters-app/esp32/main/main.cpp b/examples/all-clusters-app/esp32/main/main.cpp index b2e0674e2df293..aed052b93d6909 100644 --- a/examples/all-clusters-app/esp32/main/main.cpp +++ b/examples/all-clusters-app/esp32/main/main.cpp @@ -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" @@ -70,6 +71,10 @@ #include "Rpc.h" #endif +#if CONFIG_OPENTHREAD_ENABLED +#include +#endif + using namespace ::chip; using namespace ::chip::Credentials; using namespace ::chip::DeviceManager; @@ -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); diff --git a/examples/all-clusters-app/esp32/partitions.csv b/examples/all-clusters-app/esp32/partitions.csv index b338ff11a11589..665b8fe311bd5e 100644 --- a/examples/all-clusters-app/esp32/partitions.csv +++ b/examples/all-clusters-app/esp32/partitions.csv @@ -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, diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index f664bfa68034db..dd2679f50b7d7b 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -26,6 +26,7 @@ static_library("ESP32") { "CHIPDevicePlatformEvent.h", "ConfigurationManagerImpl.cpp", "ConfigurationManagerImpl.h", + "ConnectivityManagerImpl.cpp", "ConnectivityManagerImpl.h", "DeviceNetworkProvisioningDelegateImpl.cpp", "DeviceNetworkProvisioningDelegateImpl.h", @@ -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", - ] } } diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index 0c533e40ad9d8a..d60770091449fb 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -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 diff --git a/src/platform/ESP32/ConnectivityManagerImpl_Thread.cpp b/src/platform/ESP32/ConnectivityManagerImpl.cpp similarity index 83% rename from src/platform/ESP32/ConnectivityManagerImpl_Thread.cpp rename to src/platform/ESP32/ConnectivityManagerImpl.cpp index 117088f78513bd..73084de21b1fd2 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_Thread.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl.cpp @@ -20,11 +20,18 @@ #include #include + #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE #include #endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD #include +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +#include +#endif #include #include @@ -43,13 +50,23 @@ ConnectivityManagerImpl ConnectivityManagerImpl::sInstance; CHIP_ERROR ConnectivityManagerImpl::_Init() { +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD GenericConnectivityManagerImpl_Thread::_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::_OnPlatformEvent(event); +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + OnWiFiPlatformEvent(event); +#endif } } // namespace DeviceLayer diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index 7ee462fc3033d7..6c6dae662c5161 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -19,8 +19,204 @@ #pragma once -#if CONFIG_IDF_TARGET_ESP32H2 -#include +#include + +#include +#include + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +#include +#else +#include +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#include +#else +#include +#endif + +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE +#include +#else +#include +#endif + +#include + +#include "esp_event.h" + +namespace Inet { +class IPAddress; +} // namespace Inet + +namespace chip { +namespace DeviceLayer { + +class PlatformManagerImpl; + +/** + * Concrete implementation of the ConnectivityManager singleton object for the ESP32 platform. + */ +class ConnectivityManagerImpl final : public ConnectivityManager, + public Internal::GenericConnectivityManagerImpl, +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + public Internal::GenericConnectivityManagerImpl_WiFi, #else -#include + public Internal::GenericConnectivityManagerImpl_NoWiFi, #endif +#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE + public Internal::GenericConnectivityManagerImpl_BLE, +#else + public Internal::GenericConnectivityManagerImpl_NoBLE, +#endif +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + public Internal::GenericConnectivityManagerImpl_Thread +#else + public Internal::GenericConnectivityManagerImpl_NoThread +#endif +{ + + // Allow the ConnectivityManager interface class to delegate method calls to + // the implementation methods provided by this class. + friend class ConnectivityManager; + +private: + CHIP_ERROR _Init(void); + void _OnPlatformEvent(const ChipDeviceEvent * event); + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags; + // ===== Members that implement the ConnectivityManager abstract interface. + + WiFiStationMode _GetWiFiStationMode(void); + CHIP_ERROR _SetWiFiStationMode(WiFiStationMode val); + bool _IsWiFiStationEnabled(void); + bool _IsWiFiStationApplicationControlled(void); + bool _IsWiFiStationConnected(void); + uint32_t _GetWiFiStationReconnectIntervalMS(void); + CHIP_ERROR _SetWiFiStationReconnectIntervalMS(uint32_t val); + bool _IsWiFiStationProvisioned(void); + void _ClearWiFiStationProvision(void); + WiFiAPMode _GetWiFiAPMode(void); + CHIP_ERROR _SetWiFiAPMode(WiFiAPMode val); + bool _IsWiFiAPActive(void); + bool _IsWiFiAPApplicationControlled(void); + void _DemandStartWiFiAP(void); + void _StopOnDemandWiFiAP(void); + void _MaintainOnDemandWiFiAP(void); + uint32_t _GetWiFiAPIdleTimeoutMS(void); + void _SetWiFiAPIdleTimeoutMS(uint32_t val); + CHIP_ERROR _GetAndLogWifiStatsCounters(void); + bool _CanStartWiFiScan(); + void _OnWiFiScanDone(); + void _OnWiFiStationProvisionChange(); + + // ===== Members for internal use by the following friends. + + friend ConnectivityManager & ConnectivityMgr(void); + friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); + + // ===== Private members reserved for use by this class only. + + uint64_t mLastStationConnectFailTime; + uint64_t mLastAPDemandTime; + WiFiStationMode mWiFiStationMode; + WiFiStationState mWiFiStationState; + WiFiAPMode mWiFiAPMode; + WiFiAPState mWiFiAPState; + uint32_t mWiFiStationReconnectIntervalMS; + uint32_t mWiFiAPIdleTimeoutMS; + BitFlags mFlags; + + CHIP_ERROR InitWiFi(void); + void OnWiFiPlatformEvent(const ChipDeviceEvent * event); + + void DriveStationState(void); + void OnStationConnected(void); + void OnStationDisconnected(void); + void ChangeWiFiStationState(WiFiStationState newState); + static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState); + + void DriveAPState(void); + CHIP_ERROR ConfigureWiFiAP(void); + void ChangeWiFiAPState(WiFiAPState newState); + static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState); + + void UpdateInternetConnectivityState(void); + void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip); + void OnStationIPv4AddressLost(void); + void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); + +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI + + static ConnectivityManagerImpl sInstance; +}; + +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +inline bool ConnectivityManagerImpl::_IsWiFiStationApplicationControlled(void) +{ + return mWiFiStationMode == kWiFiStationMode_ApplicationControlled; +} + +inline bool ConnectivityManagerImpl::_IsWiFiStationConnected(void) +{ + return mWiFiStationState == kWiFiStationState_Connected; +} + +inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void) +{ + return mWiFiAPMode == kWiFiAPMode_ApplicationControlled; +} + +inline uint32_t ConnectivityManagerImpl::_GetWiFiStationReconnectIntervalMS(void) +{ + return mWiFiStationReconnectIntervalMS; +} + +inline ConnectivityManager::WiFiAPMode ConnectivityManagerImpl::_GetWiFiAPMode(void) +{ + return mWiFiAPMode; +} + +inline bool ConnectivityManagerImpl::_IsWiFiAPActive(void) +{ + return mWiFiAPState == kWiFiAPState_Active; +} + +inline uint32_t ConnectivityManagerImpl::_GetWiFiAPIdleTimeoutMS(void) +{ + return mWiFiAPIdleTimeoutMS; +} + +inline bool ConnectivityManagerImpl::_CanStartWiFiScan() +{ + return mWiFiStationState != kWiFiStationState_Connecting; +} + +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI + +/** + * Returns the public interface of the ConnectivityManager singleton object. + * + * Chip applications should use this to access features of the ConnectivityManager object + * that are common to all platforms. + */ +inline ConnectivityManager & ConnectivityMgr(void) +{ + return ConnectivityManagerImpl::sInstance; +} + +/** + * Returns the platform-specific implementation of the ConnectivityManager singleton object. + * + * Chip applications can use this to gain access to features of the ConnectivityManager + * that are specific to the ESP32 platform. + */ +inline ConnectivityManagerImpl & ConnectivityMgrImpl(void) +{ + return ConnectivityManagerImpl::sInstance; +} + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/ESP32/ConnectivityManagerImpl_Thread.h b/src/platform/ESP32/ConnectivityManagerImpl_Thread.h deleted file mode 100644 index c398e31213515b..00000000000000 --- a/src/platform/ESP32/ConnectivityManagerImpl_Thread.h +++ /dev/null @@ -1,94 +0,0 @@ -/* - * - * Copyright (c) 2020-2021 Project CHIP Authors - * Copyright (c) 2018 Nest Labs, Inc. - * All rights reserved. - * - * 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 - -#include -#include -#include -#include -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE -#include -#else -#include -#endif -#include - -namespace Inet { -class IPAddress; -} // namespace Inet - -namespace chip { -namespace DeviceLayer { - -/** - * Concrete implementation of the ConnectivityManager singleton object for the ESP32 platform. - */ -class ConnectivityManagerImpl final : public ConnectivityManager, - public Internal::GenericConnectivityManagerImpl_NoWiFi, -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE - public Internal::GenericConnectivityManagerImpl_BLE, -#else - public Internal::GenericConnectivityManagerImpl_NoBLE, -#endif - public Internal::GenericConnectivityManagerImpl_Thread, - public Internal::GenericConnectivityManagerImpl -{ - - // Allow the ConnectivityManager interface class to delegate method calls to - // the implementation methods provided by this class. - friend class ConnectivityManager; - -private: - // ===== Members that implement the ConnectivityManager abstract interface. - - CHIP_ERROR _Init(void); - void _OnPlatformEvent(const ChipDeviceEvent * event); - // ===== Members for internal use by the following friends. - - friend ConnectivityManager & ConnectivityMgr(void); - friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); - - static ConnectivityManagerImpl sInstance; -}; - -/** - * Returns the public interface of the ConnectivityManager singleton object. - * - * Chip applications should use this to access features of the ConnectivityManager object - * that are common to all platforms. - */ -inline ConnectivityManager & ConnectivityMgr(void) -{ - return ConnectivityManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConnectivityManager singleton object. - * - * Chip applications can use this to gain access to features of the ConnectivityManager - * that are specific to the ESP32 platform. - */ -inline ConnectivityManagerImpl & ConnectivityMgrImpl(void) -{ - return ConnectivityManagerImpl::sInstance; -} - -} // namespace DeviceLayer -} // namespace chip diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp index 7790c45fac90b8..ad6e33d5048273 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp @@ -20,10 +20,6 @@ #include #include -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE -#include -#endif -#include #include #include @@ -39,15 +35,7 @@ #include #include -#include - -#if !CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION -#error "WiFi Station support must be enabled when building for ESP32" -#endif - -#if !CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP -#error "WiFi AP support must be enabled when building for ESP32" -#endif +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI using namespace ::chip; using namespace ::chip::Inet; @@ -58,8 +46,6 @@ using chip::DeviceLayer::Internal::ESP32Utils; namespace chip { namespace DeviceLayer { -ConnectivityManagerImpl ConnectivityManagerImpl::sInstance; - ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMode(void) { if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled) @@ -387,9 +373,7 @@ CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWifiStatsCounters(void) return CHIP_NO_ERROR; } -// ==================== ConnectivityManager Platform Internal Methods ==================== - -CHIP_ERROR ConnectivityManagerImpl::_Init() +CHIP_ERROR ConnectivityManagerImpl::InitWiFi() { mLastStationConnectFailTime = 0; mLastAPDemandTime = 0; @@ -448,7 +432,7 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() return CHIP_NO_ERROR; } -void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) +void ConnectivityManagerImpl::OnWiFiPlatformEvent(const ChipDeviceEvent * event) { // Handle ESP system events... if (event->Type == DeviceEventType::kESPSystemEvent) @@ -536,8 +520,6 @@ void ConnectivityManagerImpl::_OnWiFiStationProvisionChange() DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL); } -// ==================== ConnectivityManager Private Methods ==================== - void ConnectivityManagerImpl::DriveStationState() { bool stationConnected; @@ -974,8 +956,6 @@ void ConnectivityManagerImpl::OnStationIPv4AddressAvailable(const ip_event_got_i } #endif // CHIP_PROGRESS_LOGGING - RefreshMessageLayer(); - UpdateInternetConnectivityState(); ChipDeviceEvent event; @@ -988,8 +968,6 @@ void ConnectivityManagerImpl::OnStationIPv4AddressLost(void) { ChipLogProgress(DeviceLayer, "IPv4 address lost on WiFi station interface"); - RefreshMessageLayer(); - UpdateInternetConnectivityState(); ChipDeviceEvent event; @@ -1007,8 +985,6 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & } #endif // CHIP_PROGRESS_LOGGING - RefreshMessageLayer(); - UpdateInternetConnectivityState(); ChipDeviceEvent event; @@ -1017,7 +993,7 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & PlatformMgr().PostEventOrDie(&event); } -void ConnectivityManagerImpl::RefreshMessageLayer(void) {} - } // namespace DeviceLayer } // namespace chip + +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI diff --git a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.h b/src/platform/ESP32/ConnectivityManagerImpl_WiFi.h deleted file mode 100644 index f51ee882386213..00000000000000 --- a/src/platform/ESP32/ConnectivityManagerImpl_WiFi.h +++ /dev/null @@ -1,192 +0,0 @@ -/* - * - * Copyright (c) 2020-2021 Project CHIP Authors - * Copyright (c) 2018 Nest Labs, Inc. - * All rights reserved. - * - * 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 - -#include -#include -#include -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE -#include -#else -#include -#endif -#include -#include - -#include "esp_event.h" - -namespace Inet { -class IPAddress; -} // namespace Inet - -namespace chip { -namespace DeviceLayer { - -class PlatformManagerImpl; - -/** - * Concrete implementation of the ConnectivityManager singleton object for the ESP32 platform. - */ -class ConnectivityManagerImpl final : public ConnectivityManager, - public Internal::GenericConnectivityManagerImpl, - public Internal::GenericConnectivityManagerImpl_WiFi, -#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE - public Internal::GenericConnectivityManagerImpl_BLE, -#else - public Internal::GenericConnectivityManagerImpl_NoBLE, -#endif - public Internal::GenericConnectivityManagerImpl_NoThread -{ - - // Allow the ConnectivityManager interface class to delegate method calls to - // the implementation methods provided by this class. - friend class ConnectivityManager; - -private: - using Flags = GenericConnectivityManagerImpl_WiFi::ConnectivityFlags; - // ===== Members that implement the ConnectivityManager abstract interface. - - WiFiStationMode _GetWiFiStationMode(void); - CHIP_ERROR _SetWiFiStationMode(WiFiStationMode val); - bool _IsWiFiStationEnabled(void); - bool _IsWiFiStationApplicationControlled(void); - bool _IsWiFiStationConnected(void); - uint32_t _GetWiFiStationReconnectIntervalMS(void); - CHIP_ERROR _SetWiFiStationReconnectIntervalMS(uint32_t val); - bool _IsWiFiStationProvisioned(void); - void _ClearWiFiStationProvision(void); - WiFiAPMode _GetWiFiAPMode(void); - CHIP_ERROR _SetWiFiAPMode(WiFiAPMode val); - bool _IsWiFiAPActive(void); - bool _IsWiFiAPApplicationControlled(void); - void _DemandStartWiFiAP(void); - void _StopOnDemandWiFiAP(void); - void _MaintainOnDemandWiFiAP(void); - uint32_t _GetWiFiAPIdleTimeoutMS(void); - void _SetWiFiAPIdleTimeoutMS(uint32_t val); - CHIP_ERROR _GetAndLogWifiStatsCounters(void); - CHIP_ERROR _Init(void); - void _OnPlatformEvent(const ChipDeviceEvent * event); - bool _CanStartWiFiScan(); - void _OnWiFiScanDone(); - void _OnWiFiStationProvisionChange(); - - // ===== Members for internal use by the following friends. - - friend ConnectivityManager & ConnectivityMgr(void); - friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); - - static ConnectivityManagerImpl sInstance; - - // ===== Private members reserved for use by this class only. - - uint64_t mLastStationConnectFailTime; - uint64_t mLastAPDemandTime; - WiFiStationMode mWiFiStationMode; - WiFiStationState mWiFiStationState; - WiFiAPMode mWiFiAPMode; - WiFiAPState mWiFiAPState; - uint32_t mWiFiStationReconnectIntervalMS; - uint32_t mWiFiAPIdleTimeoutMS; - BitFlags mFlags; - - void DriveStationState(void); - void OnStationConnected(void); - void OnStationDisconnected(void); - void ChangeWiFiStationState(WiFiStationState newState); - static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState); - - void DriveAPState(void); - CHIP_ERROR ConfigureWiFiAP(void); - void ChangeWiFiAPState(WiFiAPState newState); - static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState); - - void UpdateInternetConnectivityState(void); - void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip); - void OnStationIPv4AddressLost(void); - void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); - - static void RefreshMessageLayer(void); -}; - -inline bool ConnectivityManagerImpl::_IsWiFiStationApplicationControlled(void) -{ - return mWiFiStationMode == kWiFiStationMode_ApplicationControlled; -} - -inline bool ConnectivityManagerImpl::_IsWiFiStationConnected(void) -{ - return mWiFiStationState == kWiFiStationState_Connected; -} - -inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void) -{ - return mWiFiAPMode == kWiFiAPMode_ApplicationControlled; -} - -inline uint32_t ConnectivityManagerImpl::_GetWiFiStationReconnectIntervalMS(void) -{ - return mWiFiStationReconnectIntervalMS; -} - -inline ConnectivityManager::WiFiAPMode ConnectivityManagerImpl::_GetWiFiAPMode(void) -{ - return mWiFiAPMode; -} - -inline bool ConnectivityManagerImpl::_IsWiFiAPActive(void) -{ - return mWiFiAPState == kWiFiAPState_Active; -} - -inline uint32_t ConnectivityManagerImpl::_GetWiFiAPIdleTimeoutMS(void) -{ - return mWiFiAPIdleTimeoutMS; -} - -inline bool ConnectivityManagerImpl::_CanStartWiFiScan() -{ - return mWiFiStationState != kWiFiStationState_Connecting; -} - -/** - * Returns the public interface of the ConnectivityManager singleton object. - * - * Chip applications should use this to access features of the ConnectivityManager object - * that are common to all platforms. - */ -inline ConnectivityManager & ConnectivityMgr(void) -{ - return ConnectivityManagerImpl::sInstance; -} - -/** - * Returns the platform-specific implementation of the ConnectivityManager singleton object. - * - * Chip applications can use this to gain access to features of the ConnectivityManager - * that are specific to the ESP32 platform. - */ -inline ConnectivityManagerImpl & ConnectivityMgrImpl(void) -{ - return ConnectivityManagerImpl::sInstance; -} - -} // namespace DeviceLayer -} // namespace chip diff --git a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp index 2cabe05d785746..25461a06ca5606 100644 --- a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp +++ b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp @@ -19,7 +19,7 @@ #include #include "DeviceNetworkProvisioningDelegateImpl.h" -#include "ServiceProvisioning.h" +#include "WiFiProvisioning.h" namespace chip { namespace DeviceLayer { diff --git a/src/platform/ESP32/ThreadStackManagerImpl.cpp b/src/platform/ESP32/ThreadStackManagerImpl.cpp index 5e23fe606749b8..747ba505b95c73 100644 --- a/src/platform/ESP32/ThreadStackManagerImpl.cpp +++ b/src/platform/ESP32/ThreadStackManagerImpl.cpp @@ -24,6 +24,8 @@ /* this file behaves like a config.h, comes first */ #include +#include + #include #include #include @@ -36,6 +38,7 @@ #include "esp_openthread_netif_glue.h" #include "esp_openthread_types.h" #include "esp_vfs_eventfd.h" +#include "lib/core/CHIPError.h" #include #include #include @@ -49,41 +52,12 @@ ThreadStackManagerImpl ThreadStackManagerImpl::sInstance; CHIP_ERROR ThreadStackManagerImpl::_InitThreadStack() { - CHIP_ERROR err = CHIP_NO_ERROR; - esp_netif_t * openthread_netif = NULL; - esp_netif_config_t netif_cfg = ESP_NETIF_DEFAULT_OPENTHREAD(); - 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(), - }; - - esp_vfs_eventfd_config_t eventfd_config = { - .max_fds = 3, - }; - VerifyOrExit(esp_netif_init() == ESP_OK, err = CHIP_ERROR_INTERNAL); - VerifyOrExit(esp_vfs_eventfd_register(&eventfd_config) == ESP_OK, err = CHIP_ERROR_INTERNAL); - VerifyOrExit(esp_openthread_init(&config) == ESP_OK, err = CHIP_ERROR_INTERNAL); - openthread_netif = esp_netif_new(&netif_cfg); - VerifyOrExit(openthread_netif != NULL, err = CHIP_ERROR_INTERNAL); - VerifyOrExit(esp_netif_attach(openthread_netif, esp_openthread_netif_glue_init(&config)) == ESP_OK, err = CHIP_ERROR_INTERNAL); - err = GenericThreadStackManagerImpl_OpenThread::DoInit(NULL); -exit: - return err; + return GenericThreadStackManagerImpl_OpenThread::DoInit(esp_openthread_get_instance()); } CHIP_ERROR ThreadStackManagerImpl::_StartThreadTask() { - if (mThreadTask != NULL) - { - return CHIP_ERROR_INCORRECT_STATE; - } - xTaskCreate(ESPThreadTask, CHIP_DEVICE_CONFIG_THREAD_TASK_NAME, CHIP_DEVICE_CONFIG_THREAD_TASK_STACK_SIZE / sizeof(StackType_t), - this, CHIP_DEVICE_CONFIG_THREAD_TASK_PRIORITY, &mThreadTask); - if (mThreadTask == NULL) - { - return CHIP_ERROR_NO_MEMORY; - } + // Intentionally empty. return CHIP_NO_ERROR; } @@ -126,8 +100,3 @@ void ThreadStackManagerImpl::ESPThreadTask(void * arg) } // namespace DeviceLayer } // namespace chip - -extern "C" void otTaskletsSignalPending(otInstance * p_instance) -{ - // Intentionally empty -} diff --git a/src/platform/ESP32/ThreadStackManagerImpl.h b/src/platform/ESP32/ThreadStackManagerImpl.h index 69a42c69004ab2..b0d04a47df825b 100644 --- a/src/platform/ESP32/ThreadStackManagerImpl.h +++ b/src/platform/ESP32/ThreadStackManagerImpl.h @@ -35,8 +35,6 @@ #include #include -#include - #include namespace chip { @@ -69,7 +67,6 @@ class ThreadStackManagerImpl final : public ThreadStackManager, void _ProcessThreadActivity(); void _OnCHIPoBLEAdvertisingStart(); void _OnCHIPoBLEAdvertisingStop(); - TaskHandle_t mThreadTask; private: friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void); diff --git a/src/platform/ESP32/ServiceProvisioning.cpp b/src/platform/ESP32/WiFiProvisioning.cpp similarity index 98% rename from src/platform/ESP32/ServiceProvisioning.cpp rename to src/platform/ESP32/WiFiProvisioning.cpp index 7760bdd46f6cef..290c2b35a6be90 100644 --- a/src/platform/ESP32/ServiceProvisioning.cpp +++ b/src/platform/ESP32/WiFiProvisioning.cpp @@ -24,7 +24,7 @@ #include "esp_wifi.h" -#include "ServiceProvisioning.h" +#include "WiFiProvisioning.h" using namespace ::chip::DeviceLayer; diff --git a/src/platform/ESP32/ServiceProvisioning.h b/src/platform/ESP32/WiFiProvisioning.h similarity index 100% rename from src/platform/ESP32/ServiceProvisioning.h rename to src/platform/ESP32/WiFiProvisioning.h