Skip to content

Commit

Permalink
platform changes for thread support
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Sep 3, 2021
1 parent 53dd583 commit 64ebb8a
Show file tree
Hide file tree
Showing 19 changed files with 458 additions and 64 deletions.
26 changes: 23 additions & 3 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -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"
Expand All @@ -173,15 +181,27 @@ 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 $<TARGET_FILE:${bt_lib}> -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 $<TARGET_FILE:${bt_lib}>)
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 $<TARGET_FILE:${bt_lib}> -lbtdm_app)
endif()
endif()

if (CONFIG_ENABLE_CHIP_SHELL)
idf_component_get_property(console_lib console COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${console_lib}>)
endif()

if(CONFIG_OPENTHREAD_ENABLED)
idf_component_get_property(openthread_lib openthread COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${openthread_lib}>)
endif()

if(NOT CONFIG_USE_MINIMAL_MDNS)
idf_component_get_property(mdns_lib mdns COMPONENT_LIB)
list(APPEND chip_libraries $<TARGET_FILE:${mdns_lib}>)
Expand Down
12 changes: 11 additions & 1 deletion src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ static_library("ESP32") {
"KeyValueStoreManagerImpl.h",
"Logging.cpp",
"LwIPCoreLock.cpp",
"MdnsImpl.cpp",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"ServiceProvisioning.cpp",
Expand All @@ -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" ]
}
}
14 changes: 13 additions & 1 deletion src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
#include <platform/ConfigurationManager.h>
#include <platform/ESP32/ESP32Config.h>
#include <platform/internal/GenericConfigurationManagerImpl.cpp>

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include "esp_wifi.h"
#endif
#include "nvs.h"
#include "nvs_flash.h"
namespace chip {
Expand Down Expand Up @@ -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)
Expand All @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
36 changes: 20 additions & 16 deletions src/platform/ESP32/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,32 @@
*/
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <platform/ConnectivityManager.h>
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.cpp>
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
#include "esp_netif.h"
#include "esp_wifi.h"
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.cpp>
#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <platform/internal/GenericConnectivityManagerImpl_Thread.cpp>
#endif

#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ESP32/ESP32Utils.h>
#include <platform/internal/BLEManager.h>

#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"

#include <lwip/dns.h>
#include <lwip/ip_addr.h>
#include <lwip/nd6.h>
#include <lwip/netif.h>

#include <type_traits>

#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;
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ConnectivityManagerImpl>::_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)
{
Expand Down Expand Up @@ -521,8 +521,12 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
}
}
}
#elif CHIP_DEVICE_CONFIG_ENABLE_THREAD
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_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
Expand Down Expand Up @@ -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
Loading

0 comments on commit 64ebb8a

Please sign in to comment.