Skip to content

Commit 8b42aed

Browse files
esp32: fix issues for network commissioning test cases of incorrect ssid or password (#42320)
* esp32: fix issues for network commissioning test cases of incorrect ssid or password * review changes * call esp_wifi_connect() anyway * Restyled by clang-format * fix some potential no-def warnings --------- Co-authored-by: Restyled.io <[email protected]>
1 parent 3d39492 commit 8b42aed

File tree

3 files changed

+53
-14
lines changed

3 files changed

+53
-14
lines changed

src/platform/ESP32/CHIPDevicePlatformConfig.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,17 @@
106106
#define CHIP_DEVICE_CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX CONFIG_BLE_FAST_ADVERTISING_INTERVAL_MAX
107107
#define CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MIN
108108
#define CHIP_DEVICE_CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX CONFIG_BLE_SLOW_ADVERTISING_INTERVAL_MAX
109+
#ifdef CONFIG_CHIPOBLE_SINGLE_CONNECTION
109110
#define CHIP_DEVICE_CONFIG_CHIPOBLE_SINGLE_CONNECTION CONFIG_CHIPOBLE_SINGLE_CONNECTION
111+
#else
112+
#define CHIP_DEVICE_CONFIG_CHIPOBLE_SINGLE_CONNECTION 0
113+
#endif
114+
115+
#ifdef CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART
110116
#define CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART
117+
#else
118+
#define CHIP_DEVICE_CONFIG_CHIPOBLE_ENABLE_ADVERTISING_AUTOSTART 0
119+
#endif
111120

112121
#ifdef CONFIG_ENABLE_TEST_SETUP_PARAMS
113122
#define CHIP_DEVICE_CONFIG_ENABLE_TEST_SETUP_PARAMS 1
@@ -136,10 +145,22 @@
136145
#define CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION_LABEL
137146
#define CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL
138147
#define CHIP_DEVICE_CONFIG_CHIP_KVS_NAMESPACE_PARTITION CONFIG_CHIP_KVS_NAMESPACE_PARTITION_LABEL
148+
#ifdef CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER
139149
#define CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER CONFIG_ENABLE_ESP32_DEVICE_INSTANCE_INFO_PROVIDER
150+
#else
151+
#define CHIP_DEVICE_CONFIG_ENABLE_DEVICE_INSTANCE_INFO_PROVIDER 0
152+
#endif
140153
#define CHIP_DEVICE_CONFIG_DISCOVERY_TIMEOUT_SECS CONFIG_CHIP_DISCOVERY_TIMEOUT_SECS
154+
#ifdef CONFIG_ENABLE_ESP32_BLE_CONTROLLER
141155
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE CONFIG_ENABLE_ESP32_BLE_CONTROLLER
156+
#else
157+
#define CHIP_DEVICE_CONFIG_ENABLE_BOTH_COMMISSIONER_AND_COMMISSIONEE 0
158+
#endif
159+
#ifdef CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART
142160
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART
161+
#else
162+
#define CHIP_DEVICE_CONFIG_ENABLE_PAIRING_AUTOSTART 0
163+
#endif
143164

144165
#ifdef CONFIG_ENABLE_BLE_EXT_ANNOUNCEMENT
145166
#define CHIP_DEVICE_CONFIG_EXT_ADVERTISING CONFIG_ENABLE_BLE_EXT_ANNOUNCEMENT

src/platform/ESP32/ESP32Utils.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,20 @@ CHIP_ERROR ESP32Utils::ClearWiFiStationProvision(void)
248248
{
249249
wifi_config_t stationConfig;
250250

251+
esp_err_t err = esp_wifi_disconnect();
252+
if (err != ESP_OK)
253+
{
254+
ChipLogProgress(DeviceLayer, "esp_wifi_disconnect() failed: %s", esp_err_to_name(err));
255+
// Does not return error here as we just call esp_wifi_disconnect() to ensure that the Wi-Fi is not connecting.
256+
}
251257
// Clear the ESP WiFi station configuration.
252258
memset(&stationConfig, 0, sizeof(stationConfig));
253-
esp_wifi_set_config(WIFI_IF_STA, &stationConfig);
259+
err = esp_wifi_set_config(WIFI_IF_STA, &stationConfig);
260+
if (err != ESP_OK)
261+
{
262+
ChipLogError(DeviceLayer, "esp_wifi_set_config() failed: %s", esp_err_to_name(err));
263+
return MapError(err);
264+
}
254265

255266
return CHIP_NO_ERROR;
256267
}

src/platform/ESP32/NetworkCommissioningDriver.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -224,22 +224,14 @@ Status ESPWiFiDriver::ReorderNetwork(ByteSpan networkId, uint8_t index, MutableC
224224

225225
CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen, const char * key, uint8_t keyLen)
226226
{
227-
// If device is already connected to WiFi, then disconnect the WiFi,
228-
// clear the WiFi configurations and add the newly provided WiFi configurations.
227+
// Clear the WiFi configurations and add the newly provided WiFi configurations.
229228
if (chip::DeviceLayer::Internal::ESP32Utils::IsStationProvisioned())
230229
{
231-
ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
232-
esp_err_t err = esp_wifi_disconnect();
233-
if (err != ESP_OK)
234-
{
235-
ChipLogError(DeviceLayer, "esp_wifi_disconnect() failed: %s", esp_err_to_name(err));
236-
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
237-
}
238230
CHIP_ERROR error = chip::DeviceLayer::Internal::ESP32Utils::ClearWiFiStationProvision();
239231
if (error != CHIP_NO_ERROR)
240232
{
241233
ChipLogError(DeviceLayer, "ClearWiFiStationProvision failed: %" CHIP_ERROR_FORMAT, error.Format());
242-
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
234+
return error;
243235
}
244236
}
245237

@@ -260,7 +252,6 @@ CHIP_ERROR ESPWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen,
260252
return chip::DeviceLayer::Internal::ESP32Utils::MapError(err);
261253
}
262254

263-
ReturnErrorOnFailure(ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Disabled));
264255
return ConnectivityMgr().SetWiFiStationMode(ConnectivityManager::kWiFiStationMode_Enabled);
265256
}
266257

@@ -290,7 +281,23 @@ void ESPWiFiDriver::OnConnectWiFiNetworkFailed()
290281
{
291282
if (mpConnectCallback)
292283
{
293-
mpConnectCallback->OnResult(Status::kNetworkNotFound, CharSpan(), 0);
284+
Status status = Status::kOtherConnectionFailure;
285+
switch (mLastDisconnectedReason)
286+
{
287+
case WIFI_REASON_AUTH_FAIL:
288+
case WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT:
289+
case WIFI_REASON_HANDSHAKE_TIMEOUT:
290+
case WIFI_REASON_MIC_FAILURE:
291+
case WIFI_REASON_CONNECTION_FAIL:
292+
status = Status::kAuthFailure;
293+
break;
294+
case WIFI_REASON_NO_AP_FOUND:
295+
status = Status::kNetworkNotFound;
296+
break;
297+
default:
298+
break;
299+
}
300+
mpConnectCallback->OnResult(status, CharSpan(), 0);
294301
mpConnectCallback = nullptr;
295302
}
296303
}
@@ -360,7 +367,7 @@ CHIP_ERROR ESPWiFiDriver::StartScanWiFiNetworks(ByteSpan ssid)
360367
}
361368
else
362369
{
363-
err = esp_wifi_scan_start(NULL, false);
370+
err = esp_wifi_scan_start(nullptr, false);
364371
}
365372
if (err != ESP_OK)
366373
{

0 commit comments

Comments
 (0)