Skip to content

Commit

Permalink
platform changes for thread support (#9453)
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 authored Sep 20, 2021
1 parent 80f7986 commit cd2154d
Show file tree
Hide file tree
Showing 15 changed files with 744 additions and 203 deletions.
24 changes: 22 additions & 2 deletions config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ 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()
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
23 changes: 19 additions & 4 deletions src/platform/ESP32/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ static_library("ESP32") {
"CHIPDevicePlatformEvent.h",
"ConfigurationManagerImpl.cpp",
"ConfigurationManagerImpl.h",
"ConnectivityManagerImpl.cpp",
"ConnectivityManagerImpl.h",
"DeviceNetworkProvisioningDelegateImpl.cpp",
"DeviceNetworkProvisioningDelegateImpl.h",
Expand All @@ -38,11 +37,8 @@ static_library("ESP32") {
"KeyValueStoreManagerImpl.h",
"Logging.cpp",
"LwIPCoreLock.cpp",
"MdnsImpl.cpp",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"ServiceProvisioning.cpp",
"ServiceProvisioning.h",
"SoftwareUpdateManagerImpl.h",
"SystemTimeSupport.cpp",
"bluedroid/BLEManagerImpl.cpp",
Expand All @@ -55,4 +51,23 @@ static_library("ESP32") {
"${chip_root}/src/crypto",
"${chip_root}/src/platform:platform_base",
]
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",
]
}
}
11 changes: 10 additions & 1 deletion src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
#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 !CONFIG_OPENTHREAD_ENABLED
#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 +56,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
#define CHIP_DEVICE_CONFIG_ENABLE_WIFI CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP | CHIP_DEVICE_CONFIG_ENABLE_WIFI_STATION
#endif // CONFIG_IDF_TARGET_ESP32H2

#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 +80,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
9 changes: 8 additions & 1 deletion src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,16 @@ 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
}

CHIP_ERROR ConfigurationManagerImpl::MapConfigError(esp_err_t error)
Expand Down Expand Up @@ -169,12 +173,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
190 changes: 3 additions & 187 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,192 +19,8 @@

#pragma once

#include <platform/ConnectivityManager.h>
#include <platform/internal/GenericConnectivityManagerImpl.h>
#include <platform/internal/GenericConnectivityManagerImpl_WiFi.h>
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.h>
#if CONFIG_IDF_TARGET_ESP32H2
#include <platform/ESP32/ConnectivityManagerImpl_Thread.h>
#else
#include <platform/internal/GenericConnectivityManagerImpl_NoBLE.h>
#include <platform/ESP32/ConnectivityManagerImpl_WiFi.h>
#endif
#include <lib/support/BitFlags.h>
#include <platform/internal/GenericConnectivityManagerImpl_NoThread.h>

#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<ConnectivityManagerImpl>,
public Internal::GenericConnectivityManagerImpl_WiFi<ConnectivityManagerImpl>,
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
public Internal::GenericConnectivityManagerImpl_BLE<ConnectivityManagerImpl>,
#else
public Internal::GenericConnectivityManagerImpl_NoBLE<ConnectivityManagerImpl>,
#endif
public Internal::GenericConnectivityManagerImpl_NoThread<ConnectivityManagerImpl>
{

// 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);
bool _HaveIPv4InternetConnectivity(void);
bool _HaveIPv6InternetConnectivity(void);
bool _HaveServiceConnectivity(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<Flags> 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::_HaveIPv4InternetConnectivity(void)
{
return mFlags.Has(Flags::kHaveIPv4InternetConnectivity);
}

inline bool ConnectivityManagerImpl::_HaveIPv6InternetConnectivity(void)
{
return mFlags.Has(Flags::kHaveIPv6InternetConnectivity);
}

inline bool ConnectivityManagerImpl::_CanStartWiFiScan()
{
return mWiFiStationState != kWiFiStationState_Connecting;
}

inline bool ConnectivityManagerImpl::_HaveServiceConnectivity(void)
{
return HaveServiceConnectivityViaThread();
}

/**
* 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
Loading

0 comments on commit cd2154d

Please sign in to comment.