diff --git a/config/esp32/components/chip/CMakeLists.txt b/config/esp32/components/chip/CMakeLists.txt index ce5ee8875a8229..f26ddfc91599df 100644 --- a/config/esp32/components/chip/CMakeLists.txt +++ b/config/esp32/components/chip/CMakeLists.txt @@ -29,7 +29,11 @@ if(NOT CHIP_ROOT) get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH) endif() -set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs) +set(CHIP_REQURIE_COMPONENTS freertos lwip bt nvs_flash 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() @@ -84,6 +88,9 @@ if (CONFIG_ENABLE_CHIP_SHELL) chip_gn_arg_append("chip_build_libshell" "true") endif() +if (CONFIG_OPENTHREAD_ENABLED) + chip_gn_arg_append("chip_enable_openthread" "true") +endif() set(args_gn_input "${CMAKE_CURRENT_BINARY_DIR}/args.gn.in") file(GENERATE OUTPUT "${args_gn_input}" CONTENT "${chip_gn_args}") @@ -160,6 +167,7 @@ target_include_directories(${COMPONENT_LIB} PRIVATE target_include_directories(${COMPONENT_LIB} INTERFACE "${CHIP_ROOT}/src/platform/ESP32" + "${CHIP_ROOT}/src/platform/OpenThread" "${CHIP_ROOT}/src/include" "${CHIP_ROOT}/src/lib" "${CHIP_ROOT}/src" @@ -173,8 +181,15 @@ target_include_directories(${COMPONENT_LIB} INTERFACE idf_component_get_property(esp32_mbedtls_lib esp32_mbedtls COMPONENT_LIB) if(CONFIG_BT_ENABLED) - idf_component_get_property(bt_lib bt COMPONENT_LIB) - list(APPEND chip_libraries $ -lbtdm_app) + if("${CONFIG_IDF_TARGET}" STREQUAL "esp32h2") + idf_component_get_property(bt_lib bt COMPONENT_LIB) + idf_component_get_property(bt_dir bt COMPONENT_DIR) + list(APPEND chip_libraries $) + list(APPEND chip_libraries ${bt_dir}/controller/lib/esp32h2beta/libcontroller_5p0_seq.a) + else() + idf_component_get_property(bt_lib bt COMPONENT_LIB) + list(APPEND chip_libraries $ -lbtdm_app) + endif() endif() if (CONFIG_ENABLE_CHIP_SHELL) @@ -182,6 +197,11 @@ if (CONFIG_ENABLE_CHIP_SHELL) list(APPEND chip_libraries $) endif() +if(CONFIG_OPENTHREAD_ENABLED) + idf_component_get_property(openthread_lib openthread COMPONENT_LIB) + list(APPEND chip_libraries $) +endif() + if(NOT CONFIG_USE_MINIMAL_MDNS) idf_component_get_property(mdns_lib mdns COMPONENT_LIB) list(APPEND chip_libraries $) diff --git a/src/platform/ESP32/BUILD.gn b/src/platform/ESP32/BUILD.gn index b0edb65c6af00c..58b0c42afab7da 100644 --- a/src/platform/ESP32/BUILD.gn +++ b/src/platform/ESP32/BUILD.gn @@ -38,7 +38,6 @@ static_library("ESP32") { "KeyValueStoreManagerImpl.h", "Logging.cpp", "LwIPCoreLock.cpp", - "MdnsImpl.cpp", "PlatformManagerImpl.cpp", "PlatformManagerImpl.h", "ServiceProvisioning.cpp", @@ -55,4 +54,15 @@ static_library("ESP32") { "${chip_root}/src/crypto", "${chip_root}/src/platform:platform_base", ] + if (chip_enable_openthread) { + sources += [ + "../OpenThread/MdnsImpl.cpp", + "../OpenThread/OpenThreadUtils.cpp", + "ESPThreadConfig.h", + "ThreadStackManagerImpl.cpp", + "ThreadStackManagerImpl.h", + ] + } else { + sources += [ "MdnsImpl.cpp" ] + } } diff --git a/src/platform/ESP32/CHIPDevicePlatformConfig.h b/src/platform/ESP32/CHIPDevicePlatformConfig.h index e79a922b46b39b..5780ccd0e848f9 100644 --- a/src/platform/ESP32/CHIPDevicePlatformConfig.h +++ b/src/platform/ESP32/CHIPDevicePlatformConfig.h @@ -41,6 +41,15 @@ #define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID CONFIG_DEVICE_PRODUCT_ID #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_AP 0 +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY 0 +#define CHIP_DEVICE_CONFIG_THREAD_ENABLE_CLI 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 #define CHIP_DEVICE_CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT CONFIG_WIFI_SCAN_COMPLETION_TIMEOUT @@ -50,6 +59,10 @@ #define CHIP_DEVICE_CONFIG_WIFI_AP_MAX_STATIONS CONFIG_WIFI_AP_MAX_STATIONS #define CHIP_DEVICE_CONFIG_WIFI_AP_BEACON_INTERVAL CONFIG_WIFI_AP_BEACON_INTERVAL #define CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT CONFIG_WIFI_AP_IDLE_TIMEOUT +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY +#endif // CONFIG_IDF_TARGET_ESP32H2 + +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION #define CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE CONFIG_ENABLE_CHIPOBLE #define CHIP_DEVICE_CONFIG_BLE_DEVICE_NAME_PREFIX CONFIG_BLE_DEVICE_NAME_PREFIX #define CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MIN @@ -70,7 +83,6 @@ #define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR CONFIG_USE_TEST_SETUP_DISCRIMINATOR #define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER CONFIG_USE_TEST_SERIAL_NUMBER #define CHIP_DEVICE_CONFIG_ENABLE_TRAIT_MANAGER CONFIG_ENABLE_TRAIT_MANAGER -#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_TELEMETRY CONFIG_ENABLE_WIFI_TELEMETRY #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY CONFIG_ENABLE_THREAD_TELEMETRY #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_TELEMETRY_FULL CONFIG_ENABLE_THREAD_TELEMETRY_FULL #define CHIP_DEVICE_CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE CONFIG_EVENT_LOGGING_CRIT_BUFFER_SIZE diff --git a/src/platform/ESP32/ConfigurationManagerImpl.cpp b/src/platform/ESP32/ConfigurationManagerImpl.cpp index b1543790e4a335..da721bc06bf19b 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.cpp +++ b/src/platform/ESP32/ConfigurationManagerImpl.cpp @@ -30,8 +30,9 @@ #include #include #include - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include "esp_wifi.h" +#endif #include "nvs.h" #include "nvs_flash.h" namespace chip { @@ -102,14 +103,18 @@ CHIP_ERROR ConfigurationManagerImpl::_Init() CHIP_ERROR ConfigurationManagerImpl::_GetPrimaryWiFiMACAddress(uint8_t * buf) { +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI wifi_mode_t mode; esp_wifi_get_mode(&mode); if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA)) return MapConfigError(esp_wifi_get_mac(WIFI_IF_AP, buf)); else return MapConfigError(esp_wifi_get_mac(WIFI_IF_STA, buf)); +#else + return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE; +#endif } - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR ConfigurationManagerImpl::MapConfigError(esp_err_t error) { switch (error) @@ -125,7 +130,7 @@ CHIP_ERROR ConfigurationManagerImpl::MapConfigError(esp_err_t error) return CHIP_ERROR_INTERNAL; } } - +#endif bool ConfigurationManagerImpl::_CanFactoryReset() { // TODO: query the application to determine if factory reset is allowed. @@ -169,12 +174,15 @@ void ConfigurationManagerImpl::DoFactoryReset(intptr_t arg) } // Restore WiFi persistent settings to default values. +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI esp_err_t error = esp_wifi_restore(); if (error != ESP_OK) { ChipLogError(DeviceLayer, "esp_wifi_restore() failed: %s", esp_err_to_name(error)); } - +#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD + ThreadStackMgr().ErasePersistentInfo(); +#endif // Restart the system. ChipLogProgress(DeviceLayer, "System restarting"); esp_restart(); diff --git a/src/platform/ESP32/ConnectivityManagerImpl.cpp b/src/platform/ESP32/ConnectivityManagerImpl.cpp index 57b8b678a6a233..85cbd0c7dd311e 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.cpp +++ b/src/platform/ESP32/ConnectivityManagerImpl.cpp @@ -17,23 +17,26 @@ * limitations under the License. */ /* this file behaves like a config.h, comes first */ +#include +#include +#include +#include +#include #include -#include #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE #include #endif -#include -#include -#include -#include -#include - -#include "esp_event.h" +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include "esp_netif.h" #include "esp_wifi.h" +#include +#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD +#include +#endif +#include "esp_event.h" #include #include #include @@ -41,14 +44,6 @@ #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 - using namespace ::chip; using namespace ::chip::Inet; using namespace ::chip::System; @@ -59,7 +54,7 @@ namespace chip { namespace DeviceLayer { ConnectivityManagerImpl ConnectivityManagerImpl::sInstance; - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMode(void) { if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled) @@ -386,11 +381,12 @@ CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWifiStatsCounters(void) bssid, freq); return CHIP_NO_ERROR; } - +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI // ==================== ConnectivityManager Platform Internal Methods ==================== CHIP_ERROR ConnectivityManagerImpl::_Init() { +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI mLastStationConnectFailTime = 0; mLastAPDemandTime = 0; mWiFiStationMode = kWiFiStationMode_Disabled; @@ -444,12 +440,16 @@ CHIP_ERROR ConnectivityManagerImpl::_Init() // Queue work items to bootstrap the AP and station state machines once the Chip event loop is running. ReturnErrorOnFailure(SystemLayer.ScheduleWork(DriveStationState, NULL)); ReturnErrorOnFailure(SystemLayer.ScheduleWork(DriveAPState, NULL)); - return CHIP_NO_ERROR; +#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD + GenericConnectivityManagerImpl_Thread::_Init(); + return CHIP_NO_ERROR; +#endif } void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) { +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI // Handle ESP system events... if (event->Type == DeviceEventType::kESPSystemEvent) { @@ -521,8 +521,12 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event) } } } +#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD + GenericConnectivityManagerImpl_Thread::_OnPlatformEvent(event); +#endif } +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI void ConnectivityManagerImpl::_OnWiFiScanDone() { // Schedule a call to DriveStationState method in case a station connect attempt was @@ -1018,6 +1022,6 @@ void ConnectivityManagerImpl::OnIPv6AddressAvailable(const ip_event_got_ip6_t & } void ConnectivityManagerImpl::RefreshMessageLayer(void) {} - +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index f379c78dbeadf4..fdffa5ee4d5604 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -21,14 +21,22 @@ #include #include +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include +#else +#include +#endif #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE #include #else #include #endif #include +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +#include +#else #include +#endif #include "esp_event.h" @@ -45,14 +53,22 @@ 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 + public Internal::GenericConnectivityManagerImpl_NoWiFi, +#endif #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE public Internal::GenericConnectivityManagerImpl_BLE, #else public Internal::GenericConnectivityManagerImpl_NoBLE, #endif - public Internal::GenericConnectivityManagerImpl_NoThread +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + public Internal::GenericConnectivityManagerImpl_Thread, +#else + public Internal::GenericConnectivityManagerImpl_NoThread, +#endif + public Internal::GenericConnectivityManagerImpl { // Allow the ConnectivityManager interface class to delegate method calls to @@ -60,9 +76,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, friend class ConnectivityManager; private: +#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); @@ -82,15 +98,15 @@ class ConnectivityManagerImpl final : public ConnectivityManager, uint32_t _GetWiFiAPIdleTimeoutMS(void); void _SetWiFiAPIdleTimeoutMS(uint32_t val); CHIP_ERROR _GetAndLogWifiStatsCounters(void); - bool _HaveIPv4InternetConnectivity(void); - bool _HaveIPv6InternetConnectivity(void); - bool _HaveServiceConnectivity(void); - CHIP_ERROR _Init(void); - void _OnPlatformEvent(const ChipDeviceEvent * event); bool _CanStartWiFiScan(); void _OnWiFiScanDone(); void _OnWiFiStationProvisionChange(); - +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI + CHIP_ERROR _Init(void); + void _OnPlatformEvent(const ChipDeviceEvent * event); + bool _HaveIPv4InternetConnectivity(void); + bool _HaveIPv6InternetConnectivity(void); + bool _HaveServiceConnectivity(void); // ===== Members for internal use by the following friends. friend ConnectivityManager & ConnectivityMgr(void); @@ -99,7 +115,7 @@ class ConnectivityManagerImpl final : public ConnectivityManager, static ConnectivityManagerImpl sInstance; // ===== Private members reserved for use by this class only. - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI uint64_t mLastStationConnectFailTime; uint64_t mLastAPDemandTime; WiFiStationMode mWiFiStationMode; @@ -127,8 +143,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager, void OnIPv6AddressAvailable(const ip_event_got_ip6_t & got_ip); static void RefreshMessageLayer(void); +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI }; - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI inline bool ConnectivityManagerImpl::_IsWiFiStationApplicationControlled(void) { return mWiFiStationMode == kWiFiStationMode_ApplicationControlled; @@ -178,12 +195,25 @@ inline bool ConnectivityManagerImpl::_CanStartWiFiScan() { return mWiFiStationState != kWiFiStationState_Connecting; } +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI inline bool ConnectivityManagerImpl::_HaveServiceConnectivity(void) { return HaveServiceConnectivityViaThread(); } +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD +inline bool ConnectivityManagerImpl::_HaveIPv4InternetConnectivity(void) +{ + return false; +} + +inline bool ConnectivityManagerImpl::_HaveIPv6InternetConnectivity(void) +{ + return false; +} +#endif + /** * Returns the public interface of the ConnectivityManager singleton object. * diff --git a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp index 0bc90aebdc5b30..2cabe05d785746 100644 --- a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp +++ b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.cpp @@ -26,16 +26,33 @@ namespace DeviceLayer { CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionWiFiNetwork(const char * ssid, const char * key) { +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR err = CHIP_NO_ERROR; - ChipLogProgress(NetworkProvisioning, "ESP32NetworkProvisioningDelegate: SSID: %s", ssid); err = SetWiFiStationProvisioning(ssid, key); if (err != CHIP_NO_ERROR) { ChipLogError(NetworkProvisioning, "Failed to connect to WiFi network: %s", chip::ErrorStr(err)); } + return err; +#else + return CHIP_ERROR_NOT_IMPLEMENTED; +#endif +} + +CHIP_ERROR DeviceNetworkProvisioningDelegateImpl::_ProvisionThreadNetwork(ByteSpan threadData) +{ +#if CHIP_DEVICE_CONFIG_ENABLE_THREAD + CHIP_ERROR err = CHIP_NO_ERROR; + SuccessOrExit(err = ThreadStackMgr().SetThreadEnabled(false)); + SuccessOrExit(err = ThreadStackMgr().SetThreadProvision(threadData)); + SuccessOrExit(err = ThreadStackMgr().SetThreadEnabled(true)); +exit: return err; +#else + return CHIP_ERROR_NOT_IMPLEMENTED; +#endif } } // namespace DeviceLayer diff --git a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.h b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.h index 6e89a78e2110b2..102699448bd6bc 100644 --- a/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.h +++ b/src/platform/ESP32/DeviceNetworkProvisioningDelegateImpl.h @@ -36,7 +36,7 @@ class DeviceNetworkProvisioningDelegateImpl final friend class GenericDeviceNetworkProvisioningDelegateImpl; CHIP_ERROR _ProvisionWiFiNetwork(const char * ssid, const char * passwd); - CHIP_ERROR _ProvisionThreadNetwork(ByteSpan threadData) { return CHIP_ERROR_NOT_IMPLEMENTED; } + CHIP_ERROR _ProvisionThreadNetwork(ByteSpan threadData); }; } // namespace DeviceLayer diff --git a/src/platform/ESP32/ESP32Config.cpp b/src/platform/ESP32/ESP32Config.cpp index a63971ec8ac982..84e9f79551ebef 100644 --- a/src/platform/ESP32/ESP32Config.cpp +++ b/src/platform/ESP32/ESP32Config.cpp @@ -61,14 +61,16 @@ const ESP32Config::Key ESP32Config::kConfigKey_SetupPinCode = { kConfigNa const ESP32Config::Key ESP32Config::kConfigKey_SetupDiscriminator = { kConfigNamespace_ChipFactory, "discriminator" }; // Keys stored in the chip-config namespace -const ESP32Config::Key ESP32Config::kConfigKey_FabricId = { kConfigNamespace_ChipConfig, "fabric-id" }; -const ESP32Config::Key ESP32Config::kConfigKey_ServiceConfig = { kConfigNamespace_ChipConfig, "service-config" }; -const ESP32Config::Key ESP32Config::kConfigKey_PairedAccountId = { kConfigNamespace_ChipConfig, "account-id" }; -const ESP32Config::Key ESP32Config::kConfigKey_ServiceId = { kConfigNamespace_ChipConfig, "service-id" }; -const ESP32Config::Key ESP32Config::kConfigKey_GroupKeyIndex = { kConfigNamespace_ChipConfig, "group-key-index" }; -const ESP32Config::Key ESP32Config::kConfigKey_LastUsedEpochKeyId = { kConfigNamespace_ChipConfig, "last-ek-id" }; -const ESP32Config::Key ESP32Config::kConfigKey_FailSafeArmed = { kConfigNamespace_ChipConfig, "fail-safe-armed" }; -const ESP32Config::Key ESP32Config::kConfigKey_WiFiStationSecType = { kConfigNamespace_ChipConfig, "sta-sec-type" }; +const ESP32Config::Key ESP32Config::kConfigKey_FabricId = { kConfigNamespace_ChipConfig, "fabric-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_ServiceConfig = { kConfigNamespace_ChipConfig, "service-config" }; +const ESP32Config::Key ESP32Config::kConfigKey_PairedAccountId = { kConfigNamespace_ChipConfig, "account-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_ServiceId = { kConfigNamespace_ChipConfig, "service-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_GroupKeyIndex = { kConfigNamespace_ChipConfig, "group-key-index" }; +const ESP32Config::Key ESP32Config::kConfigKey_LastUsedEpochKeyId = { kConfigNamespace_ChipConfig, "last-ek-id" }; +const ESP32Config::Key ESP32Config::kConfigKey_FailSafeArmed = { kConfigNamespace_ChipConfig, "fail-safe-armed" }; +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +const ESP32Config::Key ESP32Config::kConfigKey_WiFiStationSecType = { kConfigNamespace_ChipConfig, "sta-sec-type" }; +#endif const ESP32Config::Key ESP32Config::kConfigKey_OperationalDeviceId = { kConfigNamespace_ChipConfig, "op-device-id" }; const ESP32Config::Key ESP32Config::kConfigKey_OperationalDeviceCert = { kConfigNamespace_ChipConfig, "op-device-cert" }; const ESP32Config::Key ESP32Config::kConfigKey_OperationalDeviceICACerts = { kConfigNamespace_ChipConfig, "op-device-ca-certs" }; diff --git a/src/platform/ESP32/ESP32Config.h b/src/platform/ESP32/ESP32Config.h index 39bd6b97534983..74869eb0d20f03 100644 --- a/src/platform/ESP32/ESP32Config.h +++ b/src/platform/ESP32/ESP32Config.h @@ -69,7 +69,9 @@ class ESP32Config static const Key kConfigKey_GroupKeyIndex; static const Key kConfigKey_LastUsedEpochKeyId; static const Key kConfigKey_FailSafeArmed; +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI static const Key kConfigKey_WiFiStationSecType; +#endif static const Key kConfigKey_OperationalDeviceId; static const Key kConfigKey_OperationalDeviceCert; static const Key kConfigKey_OperationalDeviceICACerts; diff --git a/src/platform/ESP32/ESP32Utils.cpp b/src/platform/ESP32/ESP32Utils.cpp index 8f4c94b250d6ba..9b6d4b044049da 100644 --- a/src/platform/ESP32/ESP32Utils.cpp +++ b/src/platform/ESP32/ESP32Utils.cpp @@ -30,13 +30,16 @@ #include #include "esp_event.h" +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include "esp_netif.h" #include "esp_netif_net_stack.h" #include "esp_wifi.h" +#endif using namespace ::chip::DeviceLayer::Internal; using chip::DeviceLayer::Internal::DeviceNetworkInfo; +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR ESP32Utils::IsAPEnabled(bool & apEnabled) { wifi_mode_t curWiFiMode; @@ -304,6 +307,7 @@ CHIP_ERROR ESP32Utils::ClearWiFiStationProvision(void) return CHIP_NO_ERROR; } +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR ESP32Utils::MapError(esp_err_t error) { diff --git a/src/platform/ESP32/ESP32Utils.h b/src/platform/ESP32/ESP32Utils.h index 9f7f6f0a8bb6be..444fd02af5bb17 100644 --- a/src/platform/ESP32/ESP32Utils.h +++ b/src/platform/ESP32/ESP32Utils.h @@ -29,6 +29,7 @@ namespace Internal { class ESP32Utils { public: +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI static CHIP_ERROR IsAPEnabled(bool & apEnabled); static bool IsStationProvisioned(void); static CHIP_ERROR IsStationConnected(bool & connected); @@ -46,6 +47,7 @@ class ESP32Utils static CHIP_ERROR SetWiFiStationProvision(const Internal::DeviceNetworkInfo & netInfo); static CHIP_ERROR ClearWiFiStationProvision(void); +#endif static CHIP_ERROR MapError(esp_err_t error); static void RegisterESP32ErrorFormatter(); static bool FormatError(char * buf, uint16_t bufSize, CHIP_ERROR err); diff --git a/src/platform/ESP32/ESPThreadConfig.h b/src/platform/ESP32/ESPThreadConfig.h new file mode 100644 index 00000000000000..a000e8ccff98d2 --- /dev/null +++ b/src/platform/ESP32/ESPThreadConfig.h @@ -0,0 +1,40 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * 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. + */ + +/** + * @file + * Provide Default Config for ESP Thread Device + */ +#pragma once + +#include "esp_openthread_types.h" + +#define ESP_OPENTHREAD_DEFAULT_RADIO_CONFIG() \ + { \ + .radio_mode = RADIO_MODE_NATIVE, \ + } + +#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, \ + } diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index f96517b2069fba..d54bf85abfc9c5 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -33,17 +33,19 @@ #include "esp_event.h" #include "esp_heap_caps_init.h" #include "esp_log.h" -#include "esp_netif.h" #include "esp_spi_flash.h" #include "esp_system.h" +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI +#include "esp_netif.h" #include "esp_wifi.h" - +#endif namespace chip { namespace DeviceLayer { - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI namespace Internal { extern CHIP_ERROR InitLwIPCoreLock(void); } +#endif PlatformManagerImpl PlatformManagerImpl::sInstance; @@ -57,22 +59,21 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) { esp_err_t err; - wifi_init_config_t cfg; - uint8_t ap_mac[6]; - wifi_mode_t mode; - // Arrange for CHIP-encapsulated ESP32 errors to be translated to text Internal::ESP32Utils::RegisterESP32ErrorFormatter(); +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI + wifi_init_config_t cfg; + uint8_t ap_mac[6]; + wifi_mode_t mode; // Make sure the LwIP core lock has been initialized ReturnErrorOnFailure(Internal::InitLwIPCoreLock()); - err = esp_netif_init(); if (err != ESP_OK) { goto exit; } - +#endif // Arrange for the ESP event loop to deliver events into the CHIP Device layer. err = esp_event_loop_create_default(); if (err != ESP_OK) @@ -80,6 +81,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) goto exit; } + SuccessOrExit(err); +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI esp_netif_create_default_wifi_ap(); esp_netif_create_default_wifi_sta(); @@ -106,7 +109,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) goto exit; } } - +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI // Call _InitChipStack() on the generic implementation base class // to finish the initialization process. ReturnErrorOnFailure(Internal::GenericPlatformManagerImpl_FreeRTOS::_InitChipStack()); @@ -116,7 +119,7 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) exit: return chip::DeviceLayer::Internal::ESP32Utils::MapError(err); } - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR PlatformManagerImpl::InitLwIPCoreLock(void) { return Internal::InitLwIPCoreLock(); @@ -195,5 +198,6 @@ void PlatformManagerImpl::HandleESPSystemEvent(void * arg, esp_event_base_t even sInstance.PostEvent(&event); } +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI } // namespace DeviceLayer } // namespace chip diff --git a/src/platform/ESP32/PlatformManagerImpl.h b/src/platform/ESP32/PlatformManagerImpl.h index 44bed540d9fcb5..ec06e090d1de32 100644 --- a/src/platform/ESP32/PlatformManagerImpl.h +++ b/src/platform/ESP32/PlatformManagerImpl.h @@ -26,7 +26,6 @@ #pragma once #include - namespace chip { namespace DeviceLayer { @@ -47,10 +46,10 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener public: // ===== Platform-specific members that may be accessed directly by the application. - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR InitLwIPCoreLock(void); static void HandleESPSystemEvent(void * arg, esp_event_base_t eventBase, int32_t eventId, void * eventData); - +#endif private: // ===== Methods that implement the PlatformManager abstract interface. diff --git a/src/platform/ESP32/ServiceProvisioning.cpp b/src/platform/ESP32/ServiceProvisioning.cpp index 7760bdd46f6cef..a4b26c7009e0a4 100644 --- a/src/platform/ESP32/ServiceProvisioning.cpp +++ b/src/platform/ESP32/ServiceProvisioning.cpp @@ -21,13 +21,13 @@ #include #include - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI #include "esp_wifi.h" - +#endif #include "ServiceProvisioning.h" using namespace ::chip::DeviceLayer; - +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR SetWiFiStationProvisioning(const char * ssid, const char * key) { ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled); @@ -54,3 +54,4 @@ CHIP_ERROR SetWiFiStationProvisioning(const char * ssid, const char * key) return CHIP_NO_ERROR; } +#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI diff --git a/src/platform/ESP32/ServiceProvisioning.h b/src/platform/ESP32/ServiceProvisioning.h index 109ec4ae33ea6c..ee1dae8b83ba13 100644 --- a/src/platform/ESP32/ServiceProvisioning.h +++ b/src/platform/ESP32/ServiceProvisioning.h @@ -20,6 +20,7 @@ #include +#if CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_ERROR SetWiFiStationProvisioning(const char * ssid, const char * key); - +#endif #endif // _SERVICE_PROVISIONING_H diff --git a/src/platform/ESP32/ThreadStackManagerImpl.cpp b/src/platform/ESP32/ThreadStackManagerImpl.cpp new file mode 100644 index 00000000000000..3a8d8638e11483 --- /dev/null +++ b/src/platform/ESP32/ThreadStackManagerImpl.cpp @@ -0,0 +1,133 @@ +/* + * + * 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. + */ + +/** + * @file + * Provides an implementation of the ThreadStackManager object for + * ESP32 platforms. + * + */ +/* this file behaves like a config.h, comes first */ +#include + +#include +#include +#include + +#include "driver/uart.h" +#include "esp_err.h" +#include "esp_netif.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 +#include +#include + +namespace chip { +namespace DeviceLayer { + +using namespace ::chip::DeviceLayer::Internal; + +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; +} + +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; + } + return CHIP_NO_ERROR; +} + +void ThreadStackManagerImpl::_LockThreadStack() +{ + esp_openthread_lock_acquire(portMAX_DELAY); +} + +bool ThreadStackManagerImpl::_TryLockThreadStack() +{ + return esp_openthread_lock_acquire(0); +} + +void ThreadStackManagerImpl::_UnlockThreadStack() +{ + esp_openthread_lock_release(); +} + +void ThreadStackManagerImpl::_ProcessThreadActivity() +{ + // Intentionally empty. +} +void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStart() +{ + // Intentionally empty. +} + +void ThreadStackManagerImpl::_OnCHIPoBLEAdvertisingStop() +{ + // Intentionally empty. +} + +void ThreadStackManagerImpl::ESPThreadTask(void * arg) +{ + esp_openthread_launch_mainloop(); + esp_openthread_netif_glue_deinit(); + esp_vfs_eventfd_unregister(); + vTaskDelete(NULL); +} + +} // 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 new file mode 100644 index 00000000000000..b696a1b13fcb14 --- /dev/null +++ b/src/platform/ESP32/ThreadStackManagerImpl.h @@ -0,0 +1,105 @@ +/* + * + * Copyright (c) 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. + */ + +/** + * @file + * Provides an implementation of the ThreadStackManager object + * for the ESP32 platform. + */ + +#pragma once + +#include "freertos/FreeRTOS.h" +#include "freertos/semphr.h" +#include "freertos/task.h" + +#include + +#include +#include +#include + +#include + +#include + +namespace chip { +namespace DeviceLayer { + +class ThreadStackManager; +class ThreadStackManagerImpl; + +/** + * Concrate implementation of the ThreadStackManager singleton object for ESP32 platforms. + */ +class ThreadStackManagerImpl final : public ThreadStackManager, + public Internal::GenericThreadStackManagerImpl_OpenThread +{ + friend class ThreadStackManager; + +#ifndef DOXYGEN_SHOULD_SKIP_THIS + friend Internal::GenericThreadStackManagerImpl_OpenThread; +#endif + friend void ::otTaskletsSignalPending(otInstance * otInst); + +public: + CHIP_ERROR _InitThreadStack(); + +protected: + CHIP_ERROR _StartThreadTask(); + void _LockThreadStack(); + bool _TryLockThreadStack(); + void _UnlockThreadStack(); + void _ProcessThreadActivity(); + void _OnCHIPoBLEAdvertisingStart(); + void _OnCHIPoBLEAdvertisingStop(); + TaskHandle_t mThreadTask; + +private: + friend ThreadStackManager & ::chip::DeviceLayer::ThreadStackMgr(void); + friend ThreadStackManagerImpl & ::chip::DeviceLayer::ThreadStackMgrImpl(void); + static ThreadStackManagerImpl sInstance; + ThreadStackManagerImpl() = default; + static void ESPThreadTask(void * arg); +}; + +/** + * Returns the public interface of the ThreadStackManager singleton object. + * + * chip applications should use this to access features of the ThreadStackManager object + * that are common to all platforms. + */ +inline ThreadStackManager & ThreadStackMgr(void) +{ + return ThreadStackManagerImpl::sInstance; +} + +/** + * Returns the platform-specific implementation of the ThreadStackManager singleton object. + * + * chip applications can use this to gain access to features of the ThreadStackManager + * that are specific to ESP32 platforms. + */ +inline ThreadStackManagerImpl & ThreadStackMgrImpl(void) +{ + return ThreadStackManagerImpl::sInstance; +} + +} // namespace DeviceLayer +} // namespace chip