diff --git a/config/esp32/components/chip/Kconfig b/config/esp32/components/chip/Kconfig index 10d4e9e219664e..145bbf0c74b7b7 100644 --- a/config/esp32/components/chip/Kconfig +++ b/config/esp32/components/chip/Kconfig @@ -431,9 +431,16 @@ menu "CHIP Device Layer" menu "WiFi Station Options" + config ENABLE_WIFI_STATION + bool "Enable CHIP WIFI STATION" + default y + help + Enables WiFi station for CHIP. + config DEFAULT_WIFI_SSID string "Default WiFi SSID" default "" + depends on ENABLE_WIFI_STATION help The SSID of network to connect to if no WiFi station configuration exists in NV storage at the time the device boots. @@ -443,6 +450,7 @@ menu "CHIP Device Layer" config DEFAULT_WIFI_PASSWORD string "Default WiFi Password" default "" + depends on ENABLE_WIFI_STATION help The password for the default WiFi network. @@ -452,6 +460,7 @@ menu "CHIP Device Layer" int "WiFi Station Interface Reconnect Interval (ms)" range 0 65535 default 5000 + depends on ENABLE_WIFI_STATION help The interval at which the CHIP platform will attempt to reconnect to the configured WiFi network (in milliseconds). @@ -459,6 +468,7 @@ menu "CHIP Device Layer" int "Max ScanNetworks Results" range 0 65535 default 10 + depends on ENABLE_WIFI_STATION help The maximum number of networks to return as a result of a CHIP NetworkProvisioning:ScanNetworks request. @@ -466,6 +476,7 @@ menu "CHIP Device Layer" int "WiFi Scan Completion Timeout (ms)" range 0 65535 default 10000 + depends on ENABLE_WIFI_STATION help The amount of time (in milliseconds) after which the CHIP platform will timeout a WiFi scan operation that hasn't completed. A value of 0 will disable the timeout logic. @@ -474,6 +485,7 @@ menu "CHIP Device Layer" int "WiFi Connectivity Timeout (ms)" range 0 65535 default 30000 + depends on ENABLE_WIFI_STATION help The amount of time (in milliseconds) to wait for Internet connectivity to be established on the device's WiFi station interface during a Network Provisioning TestConnectivity operation. @@ -482,9 +494,16 @@ menu "CHIP Device Layer" menu "WiFi AP Options" + config ENABLE_WIFI_AP + bool "Enable CHIP WIFI AP" + default y + help + Enables WiFi AP for CHIP. + config WIFI_AP_SSID_PREFIX string "WiFi AP SSID Prefix" default "MATTER-" + depends on ENABLE_WIFI_AP help A prefix string used in forming the WiFi soft-AP SSID. The remainder of the SSID consists of the final two bytes of the device's primary WiFi MAC address in hex. @@ -493,6 +512,7 @@ menu "CHIP Device Layer" int "WiFi AP Channel" range 1 14 default 1 + depends on ENABLE_WIFI_AP help The WiFi channel number to be used by the soft-AP. @@ -500,6 +520,7 @@ menu "CHIP Device Layer" int "WiFi AP Max Allowed Stations" range 1 10 default 4 + depends on ENABLE_WIFI_AP help The maximum number of stations allowed to connect to the soft-AP. @@ -507,6 +528,7 @@ menu "CHIP Device Layer" int "WiFi AP Beacon Interval (ms)" range 100 60000 default 100 + depends on ENABLE_WIFI_AP help The beacon interval (in milliseconds) for the WiFi soft-AP. @@ -514,6 +536,7 @@ menu "CHIP Device Layer" int "WiFi AP Idle Timeout (ms)" range 0 600000 default 120000 + depends on ENABLE_WIFI_AP help The amount of time (in milliseconds) after which the CHIP platform will deactivate the soft-AP if it has been idle. diff --git a/src/include/platform/PlatformManager.h b/src/include/platform/PlatformManager.h index 38cf83fa41c04f..7e35cf82d845ae 100644 --- a/src/include/platform/PlatformManager.h +++ b/src/include/platform/PlatformManager.h @@ -393,8 +393,10 @@ inline CHIP_ERROR PlatformManager::StopEventLoopTask() */ inline CHIP_ERROR PlatformManager::Shutdown() { - mInitialized = false; - return static_cast(this)->_Shutdown(); + CHIP_ERROR err = static_cast(this)->_Shutdown(); + if (err == CHIP_NO_ERROR) + mInitialized = false; + return err; } inline void PlatformManager::LockChipStack() diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp index 93687345fa42a0..e4dd42476fcd9f 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp +++ b/src/include/platform/internal/GenericPlatformManagerImpl_FreeRTOS.cpp @@ -245,7 +245,6 @@ void GenericPlatformManagerImpl_FreeRTOS::PostEventFromISR(const Chip template CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS::_Shutdown(void) { - VerifyOrDieWithMsg(false, DeviceLayer, "Shutdown is not implemented"); return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/platform/ESP32/CHIPPlatformConfig.h b/src/platform/ESP32/CHIPPlatformConfig.h index 899aae8cd57be0..33bca2b5d3725c 100644 --- a/src/platform/ESP32/CHIPPlatformConfig.h +++ b/src/platform/ESP32/CHIPPlatformConfig.h @@ -96,3 +96,15 @@ #define CHIP_CONFIG_ENABLE_CASE_INITIATOR CONFIG_ENABLE_CASE_INITIATOR #define CHIP_CONFIG_ENABLE_CASE_RESPONDER CONFIG_ENABLE_CASE_RESPONDER #define CHIP_CONFIG_DEFAULT_INCOMING_CONNECTION_IDLE_TIMEOUT CONFIG_DEFAULT_INCOMING_CONNECTION_IDLE_TIMEOUT + +#ifdef CONFIG_ENABLE_WIFI_STATION +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 1 +#else +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION 0 +#endif + +#ifdef CONFIG_ENABLE_WIFI_AP +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 1 +#else +#define CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP 0 +#endif diff --git a/src/platform/ESP32/ConnectivityManagerImpl.h b/src/platform/ESP32/ConnectivityManagerImpl.h index efa0b6b6c331b6..e1ffdb0529d0e4 100644 --- a/src/platform/ESP32/ConnectivityManagerImpl.h +++ b/src/platform/ESP32/ConnectivityManagerImpl.h @@ -124,11 +124,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager, CHIP_ERROR _GetWiFiCurrentMaxRate(uint64_t & currentMaxRate); CHIP_ERROR _GetWiFiOverrunCount(uint64_t & overrunCount); - // ===== 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. System::Clock::Timestamp mLastStationConnectFailTime; @@ -162,6 +157,11 @@ class ConnectivityManagerImpl final : public ConnectivityManager, #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI + // ===== Members for internal use by the following friends. + + friend ConnectivityManager & ConnectivityMgr(void); + friend ConnectivityManagerImpl & ConnectivityMgrImpl(void); + static ConnectivityManagerImpl sInstance; }; diff --git a/src/platform/ESP32/PlatformManagerImpl.cpp b/src/platform/ESP32/PlatformManagerImpl.cpp index 94989ac1fca5f2..9236f51ee98d4a 100644 --- a/src/platform/ESP32/PlatformManagerImpl.cpp +++ b/src/platform/ESP32/PlatformManagerImpl.cpp @@ -61,18 +61,15 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) // 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) @@ -81,32 +78,38 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void) } #if CHIP_DEVICE_CONFIG_ENABLE_WIFI - esp_netif_create_default_wifi_ap(); - esp_netif_create_default_wifi_sta(); + { + wifi_init_config_t cfg; + uint8_t ap_mac[6]; + wifi_mode_t mode; - esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); - esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); - mStartTime = System::SystemClock().GetMonotonicTimestamp(); + esp_netif_create_default_wifi_ap(); + esp_netif_create_default_wifi_sta(); - // Initialize the ESP WiFi layer. - cfg = WIFI_INIT_CONFIG_DEFAULT(); - err = esp_wifi_init(&cfg); - if (err != ESP_OK) - { - goto exit; - } + esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); + esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); + mStartTime = System::SystemClock().GetMonotonicTimestamp(); - esp_wifi_get_mode(&mode); - if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA)) - { - esp_fill_random(ap_mac, sizeof(ap_mac)); - /* Bit 0 of the first octet of MAC Address should always be 0 */ - ap_mac[0] &= (uint8_t) ~0x01; - err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac); + // Initialize the ESP WiFi layer. + cfg = WIFI_INIT_CONFIG_DEFAULT(); + err = esp_wifi_init(&cfg); if (err != ESP_OK) { goto exit; } + + esp_wifi_get_mode(&mode); + if ((mode == WIFI_MODE_AP) || (mode == WIFI_MODE_APSTA)) + { + esp_fill_random(ap_mac, sizeof(ap_mac)); + /* Bit 0 of the first octet of MAC Address should always be 0 */ + ap_mac[0] &= (uint8_t) ~0x01; + err = esp_wifi_set_mac(WIFI_IF_AP, ap_mac); + if (err != ESP_OK) + { + goto exit; + } + } } #endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI diff --git a/src/platform/Zephyr/PlatformManagerImpl.h b/src/platform/Zephyr/PlatformManagerImpl.h index e7999e579455ed..bf07fa1c0e1f35 100644 --- a/src/platform/Zephyr/PlatformManagerImpl.h +++ b/src/platform/Zephyr/PlatformManagerImpl.h @@ -77,5 +77,17 @@ inline PlatformManager & PlatformMgr(void) { return PlatformManagerImpl::sInstance; } + +/** + * Returns the platform-specific implementation of the PlatformManager singleton object. + * + * chip applications can use this to gain access to features of the PlatformManager + * that are specific to the ESP32 platform. + */ +inline PlatformManagerImpl & PlatformMgrImpl() +{ + return PlatformManagerImpl::sInstance; +} + } // namespace DeviceLayer } // namespace chip diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 70c7d6bf943575..15a9a5e3a6a34f 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -37,6 +37,7 @@ #include #include #include +#include #include #if CHIP_SYSTEM_CONFIG_USE_LWIP @@ -214,6 +215,10 @@ PacketBufferTest::PacketBufferTest(TestContext * context) : mContext(context) int PacketBufferTest::TestSetup(void * inContext) { chip::Platform::MemoryInit(); + + if (chip::DeviceLayer::PlatformMgr().InitChipStack() != CHIP_NO_ERROR) + return FAILURE; + TestContext * const theContext = reinterpret_cast(inContext); theContext->test = new PacketBufferTest(theContext); if (theContext->test == nullptr) @@ -225,6 +230,11 @@ int PacketBufferTest::TestSetup(void * inContext) int PacketBufferTest::TestTeardown(void * inContext) { + CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().Shutdown(); + // RTOS shutdown is not implemented, ignore CHIP_ERROR_NOT_IMPLEMENTED + if (err != CHIP_NO_ERROR && err != CHIP_ERROR_NOT_IMPLEMENTED) + return FAILURE; + return SUCCESS; } diff --git a/src/test_driver/esp32/main/main_app.cpp b/src/test_driver/esp32/main/main_app.cpp index a742ac81c2b1fa..1eef2ae345439a 100644 --- a/src/test_driver/esp32/main/main_app.cpp +++ b/src/test_driver/esp32/main/main_app.cpp @@ -38,13 +38,6 @@ using namespace ::chip::DeviceLayer; const char * TAG = "CHIP-tests"; -static int app_entropy_source(void * data, unsigned char * output, size_t len, size_t * olen) -{ - esp_fill_random(output, len); - *olen = len; - return 0; -} - static void tester_task(void * pvParameters) { ESP_LOGI(TAG, "Starting CHIP tests!"); @@ -74,56 +67,5 @@ extern "C" void app_main() exit(err); } - // Initialize the LwIP core lock. This must be done before the ESP - // tcpip_adapter layer is initialized. - CHIP_ERROR error = PlatformMgrImpl().InitLwIPCoreLock(); - if (error != CHIP_NO_ERROR) - { - ESP_LOGE(TAG, "PlatformMgr().InitLocks() failed: %s", ErrorStr(error)); - exit(1); - } - - err = esp_netif_init(); - if (err != ESP_OK) - { - ESP_LOGE(TAG, "esp_netif_init() failed: %s", esp_err_to_name(err)); - exit(err); - } - - // Arrange for the ESP event loop to deliver events into the CHIP Device layer. - err = esp_event_loop_create_default(); - if (err != ESP_OK) - { - ESP_LOGE(TAG, "esp_event_loop_create_default() failed: %s", esp_err_to_name(err)); - exit(err); - } - esp_netif_create_default_wifi_ap(); - esp_netif_create_default_wifi_sta(); - - err = esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); - if (err != ESP_OK) - { - ESP_LOGE(TAG, "esp_event_handler_register() failed for WIFI_EVENT: %s", esp_err_to_name(err)); - exit(err); - } - err = esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL); - if (err != ESP_OK) - { - ESP_LOGE(TAG, "esp_event_handler_register() failed for IP_EVENT: %s", esp_err_to_name(err)); - exit(err); - } - - error = Crypto::add_entropy_source(app_entropy_source, NULL, 16); - if (error != CHIP_NO_ERROR) - { - ESP_LOGE(TAG, "add_entropy_source() failed: %s", ErrorStr(error)); - exit(error.AsInteger()); - } - - xTaskCreate(tester_task, "tester", 12288, (void *) NULL, tskIDLE_PRIORITY + 10, NULL); - - while (1) - { - vTaskDelay(50 / portTICK_PERIOD_MS); - } + tester_task(nullptr); } diff --git a/src/test_driver/esp32/sdkconfig_qemu.defaults b/src/test_driver/esp32/sdkconfig_qemu.defaults index cfce44fe4d4d2b..6509c5ba7d9f2e 100644 --- a/src/test_driver/esp32/sdkconfig_qemu.defaults +++ b/src/test_driver/esp32/sdkconfig_qemu.defaults @@ -35,7 +35,11 @@ CONFIG_ESP32_PANIC_PRINT_REBOOT=y CONFIG_ESP32_PANIC_PRINT_HALT=n # enable BT -CONFIG_BT_ENABLED=y +CONFIG_BT_ENABLED=n + +# disable WiFi +CONFIG_ENABLE_WIFI_STATION=n +CONFIG_ENABLE_WIFI_AP=n # Use a custom partition table CONFIG_PARTITION_TABLE_CUSTOM=y