Skip to content

Commit

Permalink
try to avoid #if large amount of code
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Sep 6, 2021
1 parent 85f2594 commit 8edbaa3
Show file tree
Hide file tree
Showing 19 changed files with 433 additions and 288 deletions.
2 changes: 1 addition & 1 deletion config/esp32/components/chip/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ if(NOT CHIP_ROOT)
get_filename_component(CHIP_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../.. REALPATH)
endif()

set(CHIP_REQURIE_COMPONENTS freertos lwip bt nvs_flash mdns mbedtls fatfs)
set(CHIP_REQURIE_COMPONENTS freertos lwip bt mdns mbedtls fatfs)
if(${IDF_TARGET} STREQUAL "esp32h2")
list(APPEND CHIP_REQURIE_COMPONENTS openthread)
endif()
Expand Down
13 changes: 9 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 @@ -40,8 +39,6 @@ static_library("ESP32") {
"LwIPCoreLock.cpp",
"PlatformManagerImpl.cpp",
"PlatformManagerImpl.h",
"ServiceProvisioning.cpp",
"ServiceProvisioning.h",
"SoftwareUpdateManagerImpl.h",
"SystemTimeSupport.cpp",
"bluedroid/BLEManagerImpl.cpp",
Expand All @@ -58,11 +55,19 @@ static_library("ESP32") {
sources += [
"../OpenThread/MdnsImpl.cpp",
"../OpenThread/OpenThreadUtils.cpp",
"ConnectivityManagerImpl_Thread.cpp",
"ConnectivityManagerImpl_Thread.h",
"ESPThreadConfig.h",
"ThreadStackManagerImpl.cpp",
"ThreadStackManagerImpl.h",
]
} else {
sources += [ "MdnsImpl.cpp" ]
sources += [
"ConnectivityManagerImpl_WiFi.cpp",
"ConnectivityManagerImpl_WiFi.h",
"MdnsImpl.cpp",
"ServiceProvisioning.cpp",
"ServiceProvisioning.h",
]
}
}
7 changes: 2 additions & 5 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@
#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
#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
Expand All @@ -60,9 +57,9 @@
#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_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 Down
7 changes: 3 additions & 4 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
#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 @@ -114,7 +113,7 @@ CHIP_ERROR ConfigurationManagerImpl::_GetPrimaryWiFiMACAddress(uint8_t * buf)
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 @@ -130,7 +129,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
220 changes: 3 additions & 217 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,222 +19,8 @@

#pragma once

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

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

// Allow the ConnectivityManager interface class to delegate method calls to
// the implementation methods provided by this class.
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);
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();
#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);
friend ConnectivityManagerImpl & ConnectivityMgrImpl(void);

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;
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);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI
};
#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::_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;
}
#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.
*
* 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
56 changes: 56 additions & 0 deletions src/platform/ESP32/ConnectivityManagerImpl_Thread.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* 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.
*/
/* 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

#include <platform/internal/GenericConnectivityManagerImpl_Thread.cpp>

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

using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::System;
using namespace ::chip::TLV;

namespace chip {
namespace DeviceLayer {

ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;
// ==================== ConnectivityManager Platform Internal Methods ====================

CHIP_ERROR ConnectivityManagerImpl::_Init()
{
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_Init();
return CHIP_NO_ERROR;
}

void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
GenericConnectivityManagerImpl_Thread<ConnectivityManagerImpl>::_OnPlatformEvent(event);
}

} // namespace DeviceLayer
} // namespace chip
Loading

0 comments on commit 8edbaa3

Please sign in to comment.