32
32
#include < string>
33
33
#include < utils_log.h>
34
34
35
+ #define WIFI_STA_DISCONNECT_DELAY (pdMS_TO_TICKS(200 ))
36
+
35
37
using namespace ::chip;
36
38
// #if CHIP_DEVICE_CONFIG_ENABLE_WIFI
37
39
namespace chip {
@@ -121,6 +123,9 @@ bool BLWiFiDriver::NetworkMatch(const WiFiNetwork & network, ByteSpan networkId)
121
123
Status BLWiFiDriver::AddOrUpdateNetwork (ByteSpan ssid, ByteSpan credentials, MutableCharSpan & outDebugText,
122
124
uint8_t & outNetworkIndex)
123
125
{
126
+ outDebugText.reduce_size (0 );
127
+ outNetworkIndex = 0 ;
128
+
124
129
VerifyOrReturnError (mStagingNetwork .ssidLen == 0 || NetworkMatch (mStagingNetwork , ssid), Status::kBoundsExceeded );
125
130
VerifyOrReturnError (credentials.size () <= sizeof (mStagingNetwork .credentials ), Status::kOutOfRange );
126
131
VerifyOrReturnError (ssid.size () <= sizeof (mStagingNetwork .ssid ), Status::kOutOfRange );
@@ -136,6 +141,9 @@ Status BLWiFiDriver::AddOrUpdateNetwork(ByteSpan ssid, ByteSpan credentials, Mut
136
141
137
142
Status BLWiFiDriver::RemoveNetwork (ByteSpan networkId, MutableCharSpan & outDebugText, uint8_t & outNetworkIndex)
138
143
{
144
+ outDebugText.reduce_size (0 );
145
+ outNetworkIndex = 0 ;
146
+
139
147
VerifyOrReturnError (NetworkMatch (mStagingNetwork , networkId), Status::kNetworkIDNotFound );
140
148
141
149
// Use empty ssid for representing invalid network
@@ -145,6 +153,8 @@ Status BLWiFiDriver::RemoveNetwork(ByteSpan networkId, MutableCharSpan & outDebu
145
153
146
154
Status BLWiFiDriver::ReorderNetwork (ByteSpan networkId, uint8_t index, MutableCharSpan & outDebugText)
147
155
{
156
+ outDebugText.reduce_size (0 );
157
+
148
158
// Only one network is supported now
149
159
VerifyOrReturnError (index == 0 , Status::kOutOfRange );
150
160
VerifyOrReturnError (NetworkMatch (mStagingNetwork , networkId), Status::kNetworkIDNotFound );
@@ -157,12 +167,24 @@ CHIP_ERROR BLWiFiDriver::ConnectWiFiNetwork(const char * ssid, uint8_t ssidLen,
157
167
158
168
char wifi_ssid[64 ] = { 0 };
159
169
char passwd[64 ] = { 0 };
160
- // Set the wifi configuration
170
+ int state = 0 ;
171
+
172
+ wifi_mgmr_sta_disconnect ();
173
+ vTaskDelay (WIFI_STA_DISCONNECT_DELAY);
174
+
175
+ wifi_mgmr_sta_disable (NULL );
176
+ wifi_mgmr_state_get (&state);
177
+ while (state != WIFI_STATE_IDLE)
178
+ {
179
+ wifi_mgmr_state_get (&state);
180
+ vTaskDelay (100 );
181
+ }
182
+
161
183
memcpy (wifi_ssid, ssid, ssidLen);
162
184
memcpy (passwd, key, keyLen);
163
185
wifi_interface_t wifi_interface;
164
186
wifi_interface = wifi_mgmr_sta_enable ();
165
- wifi_mgmr_sta_connect (wifi_interface, ssid , passwd, NULL , NULL , 0 , 0 );
187
+ wifi_mgmr_sta_connect (wifi_interface, wifi_ssid , passwd, NULL , NULL , 0 , 0 );
166
188
167
189
ReturnErrorOnFailure (ConnectivityMgr ().SetWiFiStationMode (ConnectivityManager::kWiFiStationMode_Disabled ));
168
190
@@ -280,6 +302,7 @@ void BLWiFiDriver::OnScanWiFiNetworkDone()
280
302
}
281
303
}))
282
304
{
305
+ vPortFree (ScanResult);
283
306
ChipLogProgress (DeviceLayer, " ScheduleLambda OK" );
284
307
}
285
308
}
0 commit comments