forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectivityManagerImpl_WIFI.cpp
414 lines (351 loc) · 15.3 KB
/
ConnectivityManagerImpl_WIFI.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
*
* Copyright (c) 2020 Project CHIP Authors
* Copyright (c) 2019 Nest Labs, Inc.
*
* 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 <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/ConnectivityManager.h>
#include <platform/internal/BLEManager.h>
#include <platform/silabs/NetworkCommissioningWiFiDriver.h>
#include <lwip/dns.h>
#include <lwip/ip_addr.h>
#include <lwip/nd6.h>
#include <lwip/netif.h>
#include <platform/internal/GenericConnectivityManagerImpl_UDP.ipp>
#if INET_CONFIG_ENABLE_TCP_ENDPOINT
#include <platform/internal/GenericConnectivityManagerImpl_TCP.ipp>
#endif
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
#include <platform/internal/GenericConnectivityManagerImpl_BLE.ipp>
#endif
#include "CHIPDevicePlatformConfig.h"
#include <platform/silabs/wifi/WifiInterface.h>
using namespace ::chip;
using namespace ::chip::Inet;
using namespace ::chip::System;
using namespace ::chip::DeviceLayer::Internal;
using namespace ::chip::DeviceLayer::Silabs;
namespace chip {
namespace DeviceLayer {
ConnectivityManagerImpl ConnectivityManagerImpl::sInstance;
CHIP_ERROR ConnectivityManagerImpl::_Init()
{
CHIP_ERROR err;
// Queue work items to bootstrap the AP and station state machines once the Chip event loop is running.
mWiFiStationMode = kWiFiStationMode_Disabled;
mWiFiStationState = kWiFiStationState_NotConnected;
mLastStationConnectFailTime = System::Clock::kZero;
mWiFiStationReconnectInterval = System::Clock::Milliseconds32(CHIP_DEVICE_CONFIG_WIFI_STATION_RECONNECT_INTERVAL);
mFlags.ClearAll();
// TODO Initialize the Chip Addressing and Routing Module.
// Ensure that station mode is enabled.
WifiInterface::GetInstance().ConfigureStationMode();
err = DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
SuccessOrExit(err);
exit:
return err;
}
void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
// Forward the event to the generic base classes as needed.
// Handle Wfx wifi events...
if (event->Type == DeviceEventType::kWFXSystemEvent)
{
switch (event->Platform.WFXSystemEvent.data.genericMsgEvent.header.id)
{
case to_underlying(WifiInterface::WifiEvent::kStartUp):
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_START");
DriveStationState();
break;
case to_underlying(WifiInterface::WifiEvent::kConnect):
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_CONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
{
if (event->Platform.WFXSystemEvent.data.connectEvent.body.status == 0)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Succeeded);
}
else
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
}
}
DriveStationState();
break;
case to_underlying(WifiInterface::WifiEvent::kDisconnect):
ChipLogProgress(DeviceLayer, "WIFI_EVENT_STA_DISCONNECTED");
if (mWiFiStationState == kWiFiStationState_Connecting)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
}
DriveStationState();
break;
case to_underlying(WifiInterface::WifiEvent::kGotIPv4):
case to_underlying(WifiInterface::WifiEvent::kLostIP):
case to_underlying(WifiInterface::WifiEvent::kGotIPv6):
ChipLogProgress(DeviceLayer, "IP Change Event");
UpdateInternetConnectivityState();
break;
default:
break;
}
}
}
ConnectivityManager::WiFiStationMode ConnectivityManagerImpl::_GetWiFiStationMode(void)
{
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
if (WifiInterface::GetInstance().IsStationModeEnabled())
{
mWiFiStationMode = kWiFiStationMode_Enabled;
}
else
{
mWiFiStationMode = kWiFiStationMode_Disabled;
}
}
return mWiFiStationMode;
}
bool ConnectivityManagerImpl::_IsWiFiStationProvisioned(void)
{
return WifiInterface::GetInstance().IsWifiProvisioned();
}
bool ConnectivityManagerImpl::_IsWiFiStationEnabled(void)
{
return WifiInterface::GetInstance().IsStationModeEnabled();
}
CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationMode(ConnectivityManager::WiFiStationMode val)
{
DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
if (mWiFiStationMode != val)
{
ChipLogProgress(DeviceLayer, "WiFi station mode change: %s -> %s", WiFiStationModeToStr(mWiFiStationMode),
WiFiStationModeToStr(val));
}
mWiFiStationMode = val;
return CHIP_NO_ERROR;
}
CHIP_ERROR ConnectivityManagerImpl::_SetWiFiStationReconnectInterval(System::Clock::Timeout val)
{
mWiFiStationReconnectInterval = val;
return CHIP_NO_ERROR;
}
void ConnectivityManagerImpl::_ClearWiFiStationProvision(void)
{
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
WifiInterface::GetInstance().ClearWifiCredentials();
DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
}
}
CHIP_ERROR ConnectivityManagerImpl::_GetAndLogWifiStatsCounters(void)
{
return CHIP_ERROR_NOT_IMPLEMENTED;
}
void ConnectivityManagerImpl::_OnWiFiScanDone()
{
// CHIP_ERROR_NOT_IMPLEMENTED
}
void ConnectivityManagerImpl::_OnWiFiStationProvisionChange()
{
// Schedule a call to the DriveStationState method to adjust the station state as needed.
ChipLogProgress(DeviceLayer, "_ON WIFI PROVISION CHANGE");
DeviceLayer::SystemLayer().ScheduleWork(DriveStationState, NULL);
}
#if CHIP_CONFIG_ENABLE_ICD_SERVER
CHIP_ERROR ConnectivityManagerImpl::_SetPollingInterval(System::Clock::Milliseconds32 pollingInterval)
{
// TODO ICD
(void) pollingInterval;
ChipLogError(DeviceLayer, "Set ICD Fast Polling on Silabs Wifi platform");
return CHIP_ERROR_NOT_IMPLEMENTED;
}
#endif /* CHIP_CONFIG_ENABLE_ICD_SERVER */
// == == == == == == == == == == ConnectivityManager Private Methods == == == == == == == == == ==
void ConnectivityManagerImpl::DriveStationState()
{
bool stationConnected = false;
// Refresh the current station mode.
GetWiFiStationMode();
// If the station interface is NOT under application control...
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled)
{
// Ensure that the Wifi task is started.
CHIP_ERROR error = WifiInterface::GetInstance().StartWifiTask();
VerifyOrReturn(error == CHIP_NO_ERROR, ChipLogError(DeviceLayer, "StartWifiTask() failed: %s", ErrorStr(error)));
// Ensure that station mode is enabled in the WiFi layer.
WifiInterface::GetInstance().ConfigureStationMode();
}
stationConnected = WifiInterface::GetInstance().IsStationConnected();
// If the station interface is currently connected ...
if (stationConnected)
{
// Advance the station state to Connected if it was previously NotConnected or
// a previously initiated connect attempt succeeded.
if (mWiFiStationState == kWiFiStationState_NotConnected || mWiFiStationState == kWiFiStationState_Connecting_Succeeded)
{
ChangeWiFiStationState(kWiFiStationState_Connected);
ChipLogProgress(DeviceLayer, "WiFi station interface connected");
mLastStationConnectFailTime = System::Clock::kZero;
OnStationConnected();
}
// If the WiFi station interface is no longer enabled, or no longer provisioned,
// disconnect the station from the AP, unless the WiFi station mode is currently
// under application control.
#ifndef SL_ONNETWORK_PAIRING
// Incase of station interface disabled & provisioned, wifi_station should not be disconnected.
// Device will try to reconnect.
if (mWiFiStationMode != kWiFiStationMode_ApplicationControlled &&
(mWiFiStationMode != kWiFiStationMode_Enabled && !IsWiFiStationProvisioned()))
{
ChipLogProgress(DeviceLayer, "Disconnecting WiFi station interface");
CHIP_ERROR error = WifiInterface::GetInstance().TriggerDisconnection();
SuccessOrExitAction(error, ChipLogError(DeviceLayer, "TriggerDisconnection() failed: %s", ErrorStr(error)));
ChangeWiFiStationState(kWiFiStationState_Disconnecting);
}
#endif
}
// Otherwise the station interface is NOT connected to an AP, so...
else
{
System::Clock::Timestamp now = System::SystemClock().GetMonotonicTimestamp();
// Advance the station state to NotConnected if it was previously Connected or Disconnecting,
// or if a previous initiated connect attempt failed.
if (mWiFiStationState == kWiFiStationState_Connected || mWiFiStationState == kWiFiStationState_Disconnecting ||
mWiFiStationState == kWiFiStationState_Connecting_Failed)
{
WiFiStationState prevState = mWiFiStationState;
ChangeWiFiStationState(kWiFiStationState_NotConnected);
if (prevState != kWiFiStationState_Connecting_Failed)
{
ChipLogProgress(DeviceLayer, "WiFi station interface disconnected");
mLastStationConnectFailTime = System::Clock::kZero;
OnStationDisconnected();
}
else
{
mLastStationConnectFailTime = now;
}
}
// If the WiFi station interface is now enabled and provisioned (and by implication,
// not presently under application control), AND the system is not in the process of
// scanning, then...
if (mWiFiStationMode == kWiFiStationMode_Enabled && IsWiFiStationProvisioned())
{
// Initiate a connection to the AP if we haven't done so before, or if enough
// time has passed since the last attempt.
if (mLastStationConnectFailTime == System::Clock::kZero ||
now >= mLastStationConnectFailTime + mWiFiStationReconnectInterval)
{
if (mWiFiStationState != kWiFiStationState_Connecting)
{
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi");
SuccessOrExitAction(WifiInterface::GetInstance().ConnectToAccessPoint(),
ChipLogError(DeviceLayer, "ConnectToAccessPoint() failed"));
ChangeWiFiStationState(kWiFiStationState_Connecting);
}
}
// Otherwise arrange another connection attempt at a suitable point in the future.
else
{
System::Clock::Timestamp timeToNextConnect = (mLastStationConnectFailTime + mWiFiStationReconnectInterval) - now;
ChipLogProgress(DeviceLayer, "Next WiFi station reconnect in %" PRIu32 " ms",
System::Clock::Milliseconds32(timeToNextConnect).count());
ReturnOnFailure(DeviceLayer::SystemLayer().StartTimer(timeToNextConnect, DriveStationState, NULL));
}
}
}
exit:
ChipLogProgress(DeviceLayer, "Done driving station state, nothing else to do...");
// Kick-off any pending network scan that might have been deferred due to the activity
// of the WiFi station.
}
void ConnectivityManagerImpl::OnStationConnected()
{
NetworkCommissioning::SlWiFiDriver::GetInstance().OnConnectWiFiNetwork();
UpdateInternetConnectivityState();
// Alert other components of the new state.
ChipDeviceEvent event;
event.Type = DeviceEventType::kWiFiConnectivityChange;
event.WiFiConnectivityChange.Result = kConnectivity_Established;
(void) PlatformMgr().PostEvent(&event);
}
void ConnectivityManagerImpl::OnStationDisconnected()
{
// TODO: Invoke WARM to perform actions that occur when the WiFi station interface goes down.
UpdateInternetConnectivityState();
// Alert other components of the new state.
ChipDeviceEvent event;
event.Type = DeviceEventType::kWiFiConnectivityChange;
event.WiFiConnectivityChange.Result = kConnectivity_Lost;
(void) PlatformMgr().PostEvent(&event);
}
void ConnectivityManagerImpl::DriveStationState(::chip::System::Layer * aLayer, void * aAppState)
{
sInstance.DriveStationState();
}
void ConnectivityManagerImpl::ChangeWiFiStationState(WiFiStationState newState)
{
if (mWiFiStationState != newState)
{
ChipLogProgress(DeviceLayer, "WiFi station state change: %s -> %s", WiFiStationStateToStr(mWiFiStationState),
WiFiStationStateToStr(newState));
mWiFiStationState = newState;
NetworkCommissioning::SlWiFiDriver::GetInstance().UpdateNetworkingStatus();
}
}
void ConnectivityManagerImpl::UpdateInternetConnectivityState(void)
{
bool haveIPv4Conn = false;
bool haveIPv6Conn = false;
bool hadIPv4Conn = mFlags.Has(ConnectivityFlags::kHaveIPv4InternetConnectivity);
bool hadIPv6Conn = mFlags.Has(ConnectivityFlags::kHaveIPv6InternetConnectivity);
IPAddress addr;
// If the WiFi station is currently in the connected state...
if (mWiFiStationState == kWiFiStationState_Connected)
{
#if CHIP_DEVICE_CONFIG_ENABLE_IPV4
haveIPv4Conn = WifiInterface::GetIstance().HasAnIPv4Address();
#endif /* CHIP_DEVICE_CONFIG_ENABLE_IPV4 */
haveIPv6Conn = WifiInterface::GetInstance().HasAnIPv6Address();
}
// If the internet connectivity state has changed...
if (haveIPv4Conn != hadIPv4Conn || haveIPv6Conn != hadIPv6Conn)
{
// Update the current state.
mFlags.Set(ConnectivityFlags::kHaveIPv4InternetConnectivity, haveIPv4Conn)
.Set(ConnectivityFlags::kHaveIPv6InternetConnectivity, haveIPv6Conn);
// Alert other components of the state change.
ChipDeviceEvent event;
event.Type = DeviceEventType::kInternetConnectivityChange;
event.InternetConnectivityChange.IPv4 = GetConnectivityChange(hadIPv4Conn, haveIPv4Conn);
event.InternetConnectivityChange.IPv6 = GetConnectivityChange(hadIPv6Conn, haveIPv6Conn);
event.InternetConnectivityChange.ipAddress = addr;
if (haveIPv4Conn != hadIPv4Conn)
{
ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv4", (haveIPv4Conn) ? "ESTABLISHED" : "LOST");
}
if (haveIPv6Conn != hadIPv6Conn)
{
ChipLogProgress(DeviceLayer, "%s Internet connectivity %s", "IPv6", (haveIPv6Conn) ? "ESTABLISHED" : "LOST");
}
(void) PlatformMgr().PostEvent(&event);
}
}
} // namespace DeviceLayer
} // namespace chip