Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32] Support disabling SOFTAP for saving some flash #22096

Merged
merged 3 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ menu "CHIP Device Layer"
menu "WiFi AP Options"

config ENABLE_WIFI_AP
depends on ESP_WIFI_SOFTAP_SUPPORT
bool "Enable CHIP WIFI AP"
default y
help
Expand Down
2 changes: 2 additions & 0 deletions examples/all-clusters-app/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ menu "Demo"

config RENDEZVOUS_MODE_SOFTAP
bool "Soft-AP"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE
bool "BLE"
depends on BT_ENABLED
config RENDEZVOUS_MODE_ON_NETWORK
bool "On-Network"
config RENDEZVOUS_MODE_SOFTAP_ON_NETWORK
bool "Soft-AP / On-Network"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE_ON_NETWORK
bool "BLE / On-Network"
endchoice
Expand Down
3 changes: 3 additions & 0 deletions examples/all-clusters-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y
# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ menu "Demo"

config RENDEZVOUS_MODE_SOFTAP
bool "Soft-AP"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE
bool "BLE"
depends on BT_ENABLED
config RENDEZVOUS_MODE_ON_NETWORK
bool "On-Network"
config RENDEZVOUS_MODE_SOFTAP_ON_NETWORK
bool "Soft-AP / On-Network"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE_ON_NETWORK
bool "BLE / On-Network"
endchoice
Expand Down
3 changes: 3 additions & 0 deletions examples/all-clusters-minimal-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y
# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
3 changes: 3 additions & 0 deletions examples/bridge-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ CONFIG_PARTITION_TABLE_FILENAME="partitions.csv"

CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
2 changes: 2 additions & 0 deletions examples/chef/esp32/main/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,15 @@ menu "Demo"
bool "Bypass"
config RENDEZVOUS_MODE_SOFTAP
bool "Soft-AP"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE
bool "BLE"
depends on BT_ENABLED
config RENDEZVOUS_MODE_ON_NETWORK
bool "On-Network"
config RENDEZVOUS_MODE_SOFTAP_ON_NETWORK
bool "Soft-AP / On-Network"
select ESP_WIFI_SOFTAP_SUPPORT
config RENDEZVOUS_MODE_BLE_ON_NETWORK
bool "BLE / On-Network"
endchoice
Expand Down
3 changes: 3 additions & 0 deletions examples/chef/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ CONFIG_STACK_CHECK=y
# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
5 changes: 4 additions & 1 deletion examples/light-switch-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

#enable debug shell
CONFIG_ENABLE_CHIP_SHELL=y
CONFIG_ENABLE_CHIP_SHELL=y

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
3 changes: 3 additions & 0 deletions examples/lighting-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y
# Serial Flasher config
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
3 changes: 3 additions & 0 deletions examples/lock-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

#Lock app PID
CONFIG_DEVICE_PRODUCT_ID=0x8006

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
3 changes: 3 additions & 0 deletions examples/ota-provider-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,6 @@ CONFIG_USE_TEST_SETUP_DISCRIMINATOR=0xF01

# Enable chip shell
CONFIG_ENABLE_CHIP_SHELL=y

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
3 changes: 3 additions & 0 deletions examples/ota-requestor-app/esp32/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ CONFIG_DEVICE_SOFTWARE_VERSION_NUMBER=2

# Enable Chip Shell
CONFIG_ENABLE_CHIP_SHELL=y

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,6 @@ CONFIG_ESP_MAIN_TASK_STACK_SIZE=4096
#enable lwIP route hooks
CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y
CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y

# Disable softap support by default
CONFIG_ESP_WIFI_SOFTAP_SUPPORT=n
2 changes: 2 additions & 0 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@
#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
#define CHIP_DEVICE_CONFIG_WIFI_CONNECTIVITY_TIMEOUT CONFIG_WIFI_CONNECTIVITY_TIMEOUT
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
#define CHIP_DEVICE_CONFIG_WIFI_AP_SSID_PREFIX CONFIG_WIFI_AP_SSID_PREFIX
#define CHIP_DEVICE_CONFIG_WIFI_AP_CHANNEL CONFIG_WIFI_AP_CHANNEL
#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
#endif /* CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP */
#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
Expand Down
42 changes: 24 additions & 18 deletions src/platform/ESP32/ConnectivityManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,6 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
CHIP_ERROR _SetWiFiStationReconnectInterval(System::Clock::Timeout 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);
System::Clock::Timeout _GetWiFiAPIdleTimeout(void);
void _SetWiFiAPIdleTimeout(System::Clock::Timeout val);
CHIP_ERROR _GetAndLogWiFiStatsCounters(void);
bool _CanStartWiFiScan();
void _OnWiFiScanDone();
Expand All @@ -126,13 +117,9 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
// ===== Private members reserved for use by this class only.

System::Clock::Timestamp mLastStationConnectFailTime;
System::Clock::Timestamp mLastAPDemandTime;
WiFiStationMode mWiFiStationMode;
WiFiStationState mWiFiStationState;
WiFiAPMode mWiFiAPMode;
WiFiAPState mWiFiAPState;
System::Clock::Timeout mWiFiStationReconnectInterval;
System::Clock::Timeout mWiFiAPIdleTimeout;
BitFlags<Flags> mFlags;

CHIP_ERROR InitWiFi(void);
Expand All @@ -144,10 +131,27 @@ class ConnectivityManagerImpl final : public ConnectivityManager,
void ChangeWiFiStationState(WiFiStationState newState);
static void DriveStationState(::chip::System::Layer * aLayer, void * aAppState);

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
WiFiAPMode _GetWiFiAPMode(void);
CHIP_ERROR _SetWiFiAPMode(WiFiAPMode val);
bool _IsWiFiAPActive(void);
void _DemandStartWiFiAP(void);
void _StopOnDemandWiFiAP(void);
void _MaintainOnDemandWiFiAP(void);
System::Clock::Timeout _GetWiFiAPIdleTimeout(void);
void _SetWiFiAPIdleTimeout(System::Clock::Timeout val);
bool _IsWiFiAPApplicationControlled(void);

System::Clock::Timestamp mLastAPDemandTime;
WiFiAPMode mWiFiAPMode;
WiFiAPState mWiFiAPState;
System::Clock::Timeout mWiFiAPIdleTimeout;

void DriveAPState(void);
CHIP_ERROR ConfigureWiFiAP(void);
void ChangeWiFiAPState(WiFiAPState newState);
static void DriveAPState(::chip::System::Layer * aLayer, void * aAppState);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

void UpdateInternetConnectivityState(void);
void OnStationIPv4AddressAvailable(const ip_event_got_ip_t & got_ip);
Expand All @@ -174,16 +178,12 @@ inline bool ConnectivityManagerImpl::_IsWiFiStationConnected(void)
return mWiFiStationState == kWiFiStationState_Connected;
}

inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void)
{
return mWiFiAPMode == kWiFiAPMode_ApplicationControlled;
}

inline System::Clock::Timeout ConnectivityManagerImpl::_GetWiFiStationReconnectInterval(void)
{
return mWiFiStationReconnectInterval;
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
inline ConnectivityManager::WiFiAPMode ConnectivityManagerImpl::_GetWiFiAPMode(void)
{
return mWiFiAPMode;
Expand All @@ -199,6 +199,12 @@ inline System::Clock::Timeout ConnectivityManagerImpl::_GetWiFiAPIdleTimeout(voi
return mWiFiAPIdleTimeout;
}

inline bool ConnectivityManagerImpl::_IsWiFiAPApplicationControlled(void)
{
return mWiFiAPMode == kWiFiAPMode_ApplicationControlled;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

inline bool ConnectivityManagerImpl::_CanStartWiFiScan()
{
return mWiFiStationState != kWiFiStationState_Connecting;
Expand Down
23 changes: 19 additions & 4 deletions src/platform/ESP32/ConnectivityManagerImpl_WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,13 @@ void ConnectivityManagerImpl::_ClearWiFiStationProvision(void)
esp_wifi_set_config(WIFI_IF_STA, &stationConfig);

DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
}
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
CHIP_ERROR ConnectivityManagerImpl::_SetWiFiAPMode(WiFiAPMode val)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -167,6 +170,7 @@ void ConnectivityManagerImpl::_SetWiFiAPIdleTimeout(System::Clock::Timeout val)
mWiFiAPIdleTimeout = val;
DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

#define WIFI_BAND_2_4GHZ 2400
#define WIFI_BAND_5_0GHZ 5000
Expand Down Expand Up @@ -382,13 +386,17 @@ CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWiFiStatsCounters(void)
CHIP_ERROR ConnectivityManagerImpl::InitWiFi()
{
mLastStationConnectFailTime = System::Clock::kZero;
mLastAPDemandTime = System::Clock::kZero;
mWiFiStationMode = kWiFiStationMode_Disabled;
mWiFiStationState = kWiFiStationState_NotConnected;
mWiFiAPMode = kWiFiAPMode_Disabled;
mWiFiAPState = kWiFiAPState_NotActive;
mWiFiStationReconnectInterval = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL);
mWiFiAPIdleTimeout = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT);

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
mLastAPDemandTime = System::Clock::kZero;
mWiFiAPMode = kWiFiAPMode_Disabled;
mWiFiAPState = kWiFiAPState_NotActive;
mWiFiAPIdleTimeout = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_AP_IDLE_TIMEOUT);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

mFlags.SetRaw(0);

// TODO Initialize the Chip Addressing and Routing Module.
Expand Down Expand Up @@ -435,7 +443,10 @@ CHIP_ERROR ConnectivityManagerImpl::InitWiFi()

// Queue work items to bootstrap the AP and station state machines once the Chip event loop is running.
ReturnErrorOnFailure(DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL));

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
ReturnErrorOnFailure(DeviceLayer::SystemLayer().ScheduleWork(DriveAPState, NULL));
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

return CHIP_NO_ERROR;
}
Expand Down Expand Up @@ -478,6 +489,7 @@ void ConnectivityManagerImpl::OnWiFiPlatformEvent(const ChipDeviceEvent * event)
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_STOP");
DriveStationState();
break;
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
case WIFI_EVENT_AP_START:
ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_START");
ChangeWiFiAPState(kWiFiAPState_Active);
Expand All @@ -492,6 +504,7 @@ void ConnectivityManagerImpl::OnWiFiPlatformEvent(const ChipDeviceEvent * event)
ChipLogProgress(DeviceLayer, "WIFI_EVENT_AP_STACONNECTED");
MaintainOnDemandWiFiAP();
break;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
default:
break;
}
Expand Down Expand Up @@ -769,6 +782,7 @@ void ConnectivityManagerImpl::DriveStationState(::chip::System::Layer * aLayer,
sInstance.DriveStationState();
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
void ConnectivityManagerImpl::DriveAPState()
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down Expand Up @@ -952,6 +966,7 @@ void ConnectivityManagerImpl::DriveAPState(::chip::System::Layer * aLayer, void
{
sInstance.DriveAPState();
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
{
Expand Down
9 changes: 8 additions & 1 deletion src/platform/ESP32/ESP32Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
using namespace ::chip::DeviceLayer::Internal;
using chip::DeviceLayer::Internal::DeviceNetworkInfo;

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
CHIP_ERROR ESP32Utils::IsAPEnabled(bool & apEnabled)
{
wifi_mode_t curWiFiMode;
Expand All @@ -53,6 +54,7 @@ CHIP_ERROR ESP32Utils::IsAPEnabled(bool & apEnabled)

return CHIP_NO_ERROR;
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

CHIP_ERROR ESP32Utils::IsStationEnabled(bool & staEnabled)
{
Expand Down Expand Up @@ -130,6 +132,7 @@ CHIP_ERROR ESP32Utils::EnableStationMode(void)
return ESP32Utils::MapError(err);
}

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
// If station mode is not already enabled (implying the current mode is WIFI_MODE_AP), change
// the mode to WIFI_MODE_APSTA.
if (curWiFiMode == WIFI_MODE_AP)
Expand All @@ -144,15 +147,19 @@ CHIP_ERROR ESP32Utils::EnableStationMode(void)
return ESP32Utils::MapError(err);
}
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

return CHIP_NO_ERROR;
}

CHIP_ERROR ESP32Utils::SetAPMode(bool enabled)
{
wifi_mode_t curWiFiMode, targetWiFiMode;
wifi_mode_t curWiFiMode;
wifi_mode_t targetWiFiMode = WIFI_MODE_STA;

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
targetWiFiMode = (enabled) ? WIFI_MODE_APSTA : WIFI_MODE_STA;
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP

// Get the current ESP WiFI mode.
esp_err_t err = esp_wifi_get_mode(&curWiFiMode);
Expand Down
2 changes: 2 additions & 0 deletions src/platform/ESP32/ESP32Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ namespace Internal {
class ESP32Utils
{
public:
#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
static CHIP_ERROR IsAPEnabled(bool & apEnabled);
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
static CHIP_ERROR IsStationEnabled(bool & staEnabled);
static bool IsStationProvisioned(void);
static CHIP_ERROR IsStationConnected(bool & connected);
Expand Down
3 changes: 2 additions & 1 deletion src/platform/ESP32/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
wifi_init_config_t cfg;
uint8_t ap_mac[6];
wifi_mode_t mode;

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
esp_netif_create_default_wifi_ap();
#endif // CHIP_DEVICE_CONFIG_ENABLE_WIFI_AP
esp_netif_create_default_wifi_sta();

esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, PlatformManagerImpl::HandleESPSystemEvent, NULL);
Expand Down