Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2018 Nest Labs, Inc.
5 : *
6 : * Licensed under the Apache License, Version 2.0 (the "License");
7 : * you may not use this file except in compliance with the License.
8 : * You may obtain a copy of the License at
9 : *
10 : * http://www.apache.org/licenses/LICENSE-2.0
11 : *
12 : * Unless required by applicable law or agreed to in writing, software
13 : * distributed under the License is distributed on an "AS IS" BASIS,
14 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 : * See the License for the specific language governing permissions and
16 : * limitations under the License.
17 : */
18 :
19 : /**
20 : * @file
21 : * Defines the public interface for the Device Layer ConnectivityManager object.
22 : */
23 :
24 : #pragma once
25 : #include <memory>
26 :
27 : #include <app/AttributeAccessInterface.h>
28 : #include <app/icd/server/ICDServerConfig.h>
29 : #include <inet/UDPEndPoint.h>
30 : #include <lib/support/CodeUtils.h>
31 : #include <platform/CHIPDeviceConfig.h>
32 : #include <platform/CHIPDeviceEvent.h>
33 :
34 : #include <app/util/basic-types.h>
35 :
36 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
37 : #include <inet/TCPEndPoint.h>
38 : #endif
39 :
40 : namespace chip {
41 :
42 : namespace Ble {
43 : class BleLayer;
44 : class BLEEndPoint;
45 : } // namespace Ble
46 :
47 : namespace DeviceLayer {
48 :
49 : namespace Internal {
50 : template <class>
51 : class GenericPlatformManagerImpl;
52 : template <class>
53 : class GenericPlatformManagerImpl_FreeRTOS;
54 : template <class>
55 : class GenericPlatformManagerImpl_POSIX;
56 : } // namespace Internal
57 :
58 : class ConnectivityManager;
59 : class ConnectivityManagerImpl;
60 :
61 : /**
62 : * Defines the delegate class of Connectivity Manager to notify connectivity updates.
63 : */
64 : class ConnectivityManagerDelegate
65 : {
66 : public:
67 : virtual ~ConnectivityManagerDelegate() {}
68 :
69 : /**
70 : * @brief
71 : * Called when any network interface on the Node is changed
72 : *
73 : */
74 : virtual void OnNetworkInfoChanged() {}
75 : };
76 :
77 : /**
78 : * Provides control of network connectivity for a chip device.
79 : */
80 : class ConnectivityManager
81 : {
82 : using ImplClass = ::chip::DeviceLayer::ConnectivityManagerImpl;
83 :
84 : public:
85 : // ===== Members that define the public interface of the ConnectivityManager
86 :
87 : enum WiFiStationMode
88 : {
89 : kWiFiStationMode_NotSupported = 0,
90 : kWiFiStationMode_ApplicationControlled = 1,
91 : kWiFiStationMode_Disabled = 2,
92 : kWiFiStationMode_Enabled = 3,
93 : };
94 :
95 : enum WiFiAPMode
96 : {
97 : kWiFiAPMode_NotSupported = 0,
98 : kWiFiAPMode_ApplicationControlled = 1,
99 : kWiFiAPMode_Disabled = 2,
100 : kWiFiAPMode_Enabled = 3,
101 : kWiFiAPMode_OnDemand = 4,
102 : kWiFiAPMode_OnDemand_NoStationProvision = 5,
103 : };
104 :
105 : enum ThreadMode
106 : {
107 : kThreadMode_NotSupported = 0,
108 : kThreadMode_ApplicationControlled = 1,
109 : kThreadMode_Disabled = 2,
110 : kThreadMode_Enabled = 3,
111 : };
112 :
113 : enum WiFiStationState
114 : {
115 : kWiFiStationState_NotConnected,
116 : kWiFiStationState_Connecting,
117 : kWiFiStationState_Connecting_Succeeded,
118 : kWiFiStationState_Connecting_Failed,
119 : kWiFiStationState_Connected,
120 : kWiFiStationState_Disconnecting,
121 : };
122 :
123 : enum WiFiAPState
124 : {
125 : kWiFiAPState_NotActive,
126 : kWiFiAPState_Activating,
127 : kWiFiAPState_Active,
128 : kWiFiAPState_Deactivating,
129 : };
130 :
131 : enum CHIPoBLEServiceMode
132 : {
133 : kCHIPoBLEServiceMode_NotSupported = 0,
134 : kCHIPoBLEServiceMode_Enabled = 1,
135 : kCHIPoBLEServiceMode_Disabled = 2,
136 : };
137 :
138 : enum ThreadDeviceType
139 : {
140 : kThreadDeviceType_NotSupported = 0,
141 : kThreadDeviceType_Router = 1,
142 : kThreadDeviceType_FullEndDevice = 2,
143 : kThreadDeviceType_MinimalEndDevice = 3,
144 : kThreadDeviceType_SleepyEndDevice = 4,
145 : kThreadDeviceType_SynchronizedSleepyEndDevice = 5,
146 : };
147 :
148 : enum BLEAdvertisingMode
149 : {
150 : kFastAdvertising = 0,
151 : kSlowAdvertising = 1,
152 : };
153 :
154 : enum class SEDIntervalMode
155 : {
156 : Idle = 0,
157 : Active = 1,
158 : };
159 :
160 : struct SEDIntervalsConfig;
161 :
162 : void SetDelegate(ConnectivityManagerDelegate * delegate) { mDelegate = delegate; }
163 : ConnectivityManagerDelegate * GetDelegate() const { return mDelegate; }
164 :
165 : chip::Inet::EndPointManager<Inet::UDPEndPoint> & UDPEndPointManager();
166 :
167 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
168 : chip::Inet::EndPointManager<Inet::TCPEndPoint> & TCPEndPointManager();
169 : #endif
170 :
171 : // WiFi station methods
172 : WiFiStationMode GetWiFiStationMode();
173 : CHIP_ERROR SetWiFiStationMode(WiFiStationMode val);
174 : bool IsWiFiStationEnabled();
175 : bool IsWiFiStationApplicationControlled();
176 : bool IsWiFiStationConnected();
177 : System::Clock::Timeout GetWiFiStationReconnectInterval();
178 : CHIP_ERROR SetWiFiStationReconnectInterval(System::Clock::Timeout val);
179 : bool IsWiFiStationProvisioned();
180 : void ClearWiFiStationProvision();
181 : CHIP_ERROR GetAndLogWiFiStatsCounters();
182 :
183 : // WiFi AP methods
184 : WiFiAPMode GetWiFiAPMode();
185 : CHIP_ERROR SetWiFiAPMode(WiFiAPMode val);
186 : bool IsWiFiAPActive();
187 : bool IsWiFiAPApplicationControlled();
188 : void DemandStartWiFiAP();
189 : void StopOnDemandWiFiAP();
190 : void MaintainOnDemandWiFiAP();
191 : System::Clock::Timeout GetWiFiAPIdleTimeout();
192 : void SetWiFiAPIdleTimeout(System::Clock::Timeout val);
193 :
194 : // Thread Methods
195 : ThreadMode GetThreadMode();
196 : CHIP_ERROR SetThreadMode(ThreadMode val);
197 : bool IsThreadEnabled();
198 : bool IsThreadApplicationControlled();
199 : ThreadDeviceType GetThreadDeviceType();
200 : CHIP_ERROR SetThreadDeviceType(ThreadDeviceType deviceType);
201 : bool IsThreadAttached();
202 : bool IsThreadProvisioned();
203 : void ErasePersistentInfo();
204 : void ResetThreadNetworkDiagnosticsCounts();
205 :
206 : CHIP_ERROR SetPollingInterval(System::Clock::Milliseconds32 pollingInterval);
207 :
208 : // CHIPoBLE service methods
209 : Ble::BleLayer * GetBleLayer();
210 : bool IsBLEAdvertisingEnabled();
211 : /**
212 : * Enable or disable BLE advertising.
213 : *
214 : * @return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE if BLE advertising is not
215 : * supported or other error on other failures.
216 : */
217 : CHIP_ERROR SetBLEAdvertisingEnabled(bool val);
218 : bool IsBLEAdvertising();
219 : CHIP_ERROR SetBLEAdvertisingMode(BLEAdvertisingMode mode);
220 : CHIP_ERROR GetBLEDeviceName(char * buf, size_t bufSize);
221 : CHIP_ERROR SetBLEDeviceName(const char * deviceName);
222 : uint16_t NumBLEConnections();
223 :
224 : // User selected mode methods
225 : bool IsUserSelectedModeActive();
226 : void SetUserSelectedMode(bool val);
227 : uint16_t GetUserSelectedModeTimeout();
228 : void SetUserSelectedModeTimeout(uint16_t val);
229 :
230 : // Support methods
231 : static const char * WiFiStationModeToStr(WiFiStationMode mode);
232 : static const char * WiFiAPModeToStr(WiFiAPMode mode);
233 : static const char * WiFiStationStateToStr(WiFiStationState state);
234 : static const char * WiFiAPStateToStr(WiFiAPState state);
235 : static const char * CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode);
236 :
237 : private:
238 : ConnectivityManagerDelegate * mDelegate = nullptr;
239 :
240 : // ===== Members for internal use by the following friends.
241 :
242 : friend class PlatformManagerImpl;
243 : template <class>
244 : friend class Internal::GenericPlatformManagerImpl;
245 : template <class>
246 : friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
247 : template <class>
248 : friend class Internal::GenericPlatformManagerImpl_POSIX;
249 :
250 : CHIP_ERROR Init();
251 : void OnPlatformEvent(const ChipDeviceEvent * event);
252 : bool CanStartWiFiScan();
253 : void OnWiFiScanDone();
254 : void OnWiFiStationProvisionChange();
255 :
256 : protected:
257 : // Construction/destruction limited to subclasses.
258 : ConnectivityManager() = default;
259 : ~ConnectivityManager() = default;
260 :
261 : // No copy, move or assignment.
262 : ConnectivityManager(const ConnectivityManager &) = delete;
263 : ConnectivityManager(const ConnectivityManager &&) = delete;
264 : ConnectivityManager & operator=(const ConnectivityManager &) = delete;
265 : };
266 :
267 : /**
268 : * Information describing the desired intervals for a sleepy end device (SED).
269 : */
270 : struct ConnectivityManager::SEDIntervalsConfig
271 : {
272 : /** Interval at which the device is able to communicate with its parent when there are active chip exchanges in progress. Only
273 : * meaningful when the device is acting as a sleepy end node. */
274 : System::Clock::Milliseconds32 ActiveIntervalMS;
275 :
276 : /** Interval at which the device is able to communicate with its parent when there are NO active chip exchanges in progress.
277 : * Only meaningful when the device is acting as a sleepy end node. */
278 : System::Clock::Milliseconds32 IdleIntervalMS;
279 : };
280 :
281 : /**
282 : * Returns a reference to the public interface of the ConnectivityManager singleton object.
283 : *
284 : * chip applications should use this to access features of the ConnectivityManager object
285 : * that are common to all platforms.
286 : */
287 : extern ConnectivityManager & ConnectivityMgr();
288 :
289 : /**
290 : * Returns the platform-specific implementation of the ConnectivityManager singleton object.
291 : *
292 : * chip applications can use this to gain access to features of the ConnectivityManager
293 : * that are specific to the selected platform.
294 : */
295 : extern ConnectivityManagerImpl & ConnectivityMgrImpl();
296 :
297 : } // namespace DeviceLayer
298 : } // namespace chip
299 :
300 : /* Include a header file containing the implementation of the ConfigurationManager
301 : * object for the selected platform.
302 : */
303 : #ifdef EXTERNAL_CONNECTIVITYMANAGERIMPL_HEADER
304 : #include EXTERNAL_CONNECTIVITYMANAGERIMPL_HEADER
305 : #elif defined(CHIP_DEVICE_LAYER_TARGET)
306 : #define CONNECTIVITYMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ConnectivityManagerImpl.h>
307 : #include CONNECTIVITYMANAGERIMPL_HEADER
308 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)
309 :
310 : namespace chip {
311 : namespace DeviceLayer {
312 :
313 0 : inline chip::Inet::EndPointManager<Inet::UDPEndPoint> & ConnectivityManager::UDPEndPointManager()
314 : {
315 0 : return static_cast<ImplClass *>(this)->_UDPEndPointManager();
316 : }
317 :
318 : #if INET_CONFIG_ENABLE_TCP_ENDPOINT
319 0 : inline chip::Inet::EndPointManager<Inet::TCPEndPoint> & ConnectivityManager::TCPEndPointManager()
320 : {
321 0 : return static_cast<ImplClass *>(this)->_TCPEndPointManager();
322 : }
323 : #endif
324 :
325 : inline ConnectivityManager::WiFiStationMode ConnectivityManager::GetWiFiStationMode()
326 : {
327 : return static_cast<ImplClass *>(this)->_GetWiFiStationMode();
328 : }
329 :
330 : inline CHIP_ERROR ConnectivityManager::SetWiFiStationMode(WiFiStationMode val)
331 : {
332 : return static_cast<ImplClass *>(this)->_SetWiFiStationMode(val);
333 : }
334 :
335 : inline bool ConnectivityManager::IsWiFiStationEnabled()
336 : {
337 : return static_cast<ImplClass *>(this)->_IsWiFiStationEnabled();
338 : }
339 :
340 : inline bool ConnectivityManager::IsWiFiStationApplicationControlled()
341 : {
342 : return static_cast<ImplClass *>(this)->_IsWiFiStationApplicationControlled();
343 : }
344 :
345 : inline bool ConnectivityManager::IsWiFiStationConnected()
346 : {
347 : return static_cast<ImplClass *>(this)->_IsWiFiStationConnected();
348 : }
349 :
350 : inline System::Clock::Timeout ConnectivityManager::GetWiFiStationReconnectInterval()
351 : {
352 : return static_cast<ImplClass *>(this)->_GetWiFiStationReconnectInterval();
353 : }
354 :
355 : inline CHIP_ERROR ConnectivityManager::SetWiFiStationReconnectInterval(System::Clock::Timeout val)
356 : {
357 : return static_cast<ImplClass *>(this)->_SetWiFiStationReconnectInterval(val);
358 : }
359 :
360 : inline bool ConnectivityManager::IsWiFiStationProvisioned()
361 : {
362 : return static_cast<ImplClass *>(this)->_IsWiFiStationProvisioned();
363 : }
364 :
365 : inline void ConnectivityManager::ClearWiFiStationProvision()
366 : {
367 : static_cast<ImplClass *>(this)->_ClearWiFiStationProvision();
368 : }
369 :
370 : inline ConnectivityManager::WiFiAPMode ConnectivityManager::GetWiFiAPMode()
371 : {
372 : return static_cast<ImplClass *>(this)->_GetWiFiAPMode();
373 : }
374 :
375 : inline CHIP_ERROR ConnectivityManager::SetWiFiAPMode(WiFiAPMode val)
376 : {
377 : return static_cast<ImplClass *>(this)->_SetWiFiAPMode(val);
378 : }
379 :
380 : inline bool ConnectivityManager::IsWiFiAPActive()
381 : {
382 : return static_cast<ImplClass *>(this)->_IsWiFiAPActive();
383 : }
384 :
385 : inline bool ConnectivityManager::IsWiFiAPApplicationControlled()
386 : {
387 : return static_cast<ImplClass *>(this)->_IsWiFiAPApplicationControlled();
388 : }
389 :
390 : inline void ConnectivityManager::DemandStartWiFiAP()
391 : {
392 : static_cast<ImplClass *>(this)->_DemandStartWiFiAP();
393 : }
394 :
395 : inline void ConnectivityManager::StopOnDemandWiFiAP()
396 : {
397 : static_cast<ImplClass *>(this)->_StopOnDemandWiFiAP();
398 : }
399 :
400 : inline void ConnectivityManager::MaintainOnDemandWiFiAP()
401 : {
402 : static_cast<ImplClass *>(this)->_MaintainOnDemandWiFiAP();
403 : }
404 :
405 : inline System::Clock::Timeout ConnectivityManager::GetWiFiAPIdleTimeout()
406 : {
407 : return static_cast<ImplClass *>(this)->_GetWiFiAPIdleTimeout();
408 : }
409 :
410 : inline void ConnectivityManager::SetWiFiAPIdleTimeout(System::Clock::Timeout val)
411 : {
412 : static_cast<ImplClass *>(this)->_SetWiFiAPIdleTimeout(val);
413 : }
414 :
415 : inline CHIP_ERROR ConnectivityManager::GetAndLogWiFiStatsCounters()
416 : {
417 : return static_cast<ImplClass *>(this)->_GetAndLogWiFiStatsCounters();
418 : }
419 :
420 : inline ConnectivityManager::ThreadMode ConnectivityManager::GetThreadMode()
421 : {
422 : return static_cast<ImplClass *>(this)->_GetThreadMode();
423 : }
424 :
425 : inline CHIP_ERROR ConnectivityManager::SetThreadMode(ThreadMode val)
426 : {
427 : return static_cast<ImplClass *>(this)->_SetThreadMode(val);
428 : }
429 :
430 : inline bool ConnectivityManager::IsThreadEnabled()
431 : {
432 : return static_cast<ImplClass *>(this)->_IsThreadEnabled();
433 : }
434 :
435 : inline bool ConnectivityManager::IsThreadApplicationControlled()
436 : {
437 : return static_cast<ImplClass *>(this)->_IsThreadApplicationControlled();
438 : }
439 :
440 : inline ConnectivityManager::ThreadDeviceType ConnectivityManager::GetThreadDeviceType()
441 : {
442 : return static_cast<ImplClass *>(this)->_GetThreadDeviceType();
443 : }
444 :
445 : inline CHIP_ERROR ConnectivityManager::SetThreadDeviceType(ThreadDeviceType deviceType)
446 : {
447 : return static_cast<ImplClass *>(this)->_SetThreadDeviceType(deviceType);
448 : }
449 :
450 : inline CHIP_ERROR ConnectivityManager::SetPollingInterval(System::Clock::Milliseconds32 pollingInterval)
451 : {
452 : #if CHIP_CONFIG_ENABLE_ICD_SERVER
453 : return static_cast<ImplClass *>(this)->_SetPollingInterval(pollingInterval);
454 : #else
455 : return CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
456 : #endif
457 : }
458 :
459 : inline bool ConnectivityManager::IsThreadAttached()
460 : {
461 : return static_cast<ImplClass *>(this)->_IsThreadAttached();
462 : }
463 :
464 : inline bool ConnectivityManager::IsThreadProvisioned()
465 : {
466 : return static_cast<ImplClass *>(this)->_IsThreadProvisioned();
467 : }
468 :
469 : inline void ConnectivityManager::ErasePersistentInfo()
470 : {
471 : static_cast<ImplClass *>(this)->_ErasePersistentInfo();
472 : }
473 :
474 : inline void ConnectivityManager::ResetThreadNetworkDiagnosticsCounts()
475 : {
476 : static_cast<ImplClass *>(this)->_ResetThreadNetworkDiagnosticsCounts();
477 : }
478 :
479 2 : inline Ble::BleLayer * ConnectivityManager::GetBleLayer()
480 : {
481 2 : return static_cast<ImplClass *>(this)->_GetBleLayer();
482 : }
483 :
484 : inline bool ConnectivityManager::IsBLEAdvertisingEnabled()
485 : {
486 : return static_cast<ImplClass *>(this)->_IsBLEAdvertisingEnabled();
487 : }
488 :
489 2 : inline CHIP_ERROR ConnectivityManager::SetBLEAdvertisingEnabled(bool val)
490 : {
491 2 : return static_cast<ImplClass *>(this)->_SetBLEAdvertisingEnabled(val);
492 : }
493 :
494 : inline bool ConnectivityManager::IsBLEAdvertising()
495 : {
496 : return static_cast<ImplClass *>(this)->_IsBLEAdvertising();
497 : }
498 :
499 : inline CHIP_ERROR ConnectivityManager::SetBLEAdvertisingMode(BLEAdvertisingMode mode)
500 : {
501 : return static_cast<ImplClass *>(this)->_SetBLEAdvertisingMode(mode);
502 : }
503 :
504 : inline CHIP_ERROR ConnectivityManager::GetBLEDeviceName(char * buf, size_t bufSize)
505 : {
506 : return static_cast<ImplClass *>(this)->_GetBLEDeviceName(buf, bufSize);
507 : }
508 :
509 : inline CHIP_ERROR ConnectivityManager::SetBLEDeviceName(const char * deviceName)
510 : {
511 : return static_cast<ImplClass *>(this)->_SetBLEDeviceName(deviceName);
512 : }
513 :
514 : inline uint16_t ConnectivityManager::NumBLEConnections()
515 : {
516 : return static_cast<ImplClass *>(this)->_NumBLEConnections();
517 : }
518 :
519 : inline bool ConnectivityManager::IsUserSelectedModeActive()
520 : {
521 : return static_cast<ImplClass *>(this)->_IsUserSelectedModeActive();
522 : }
523 :
524 : inline void ConnectivityManager::SetUserSelectedMode(bool val)
525 : {
526 : static_cast<ImplClass *>(this)->_SetUserSelectedMode(val);
527 : }
528 :
529 : inline uint16_t ConnectivityManager::GetUserSelectedModeTimeout()
530 : {
531 : return static_cast<ImplClass *>(this)->_GetUserSelectedModeTimeout();
532 : }
533 :
534 : inline void ConnectivityManager::SetUserSelectedModeTimeout(uint16_t val)
535 : {
536 : static_cast<ImplClass *>(this)->_SetUserSelectedModeTimeout(val);
537 : }
538 :
539 : inline const char * ConnectivityManager::WiFiStationModeToStr(WiFiStationMode mode)
540 : {
541 : return ImplClass::_WiFiStationModeToStr(mode);
542 : }
543 :
544 : inline const char * ConnectivityManager::WiFiAPModeToStr(WiFiAPMode mode)
545 : {
546 : return ImplClass::_WiFiAPModeToStr(mode);
547 : }
548 :
549 : inline const char * ConnectivityManager::WiFiStationStateToStr(WiFiStationState state)
550 : {
551 : return ImplClass::_WiFiStationStateToStr(state);
552 : }
553 :
554 : inline const char * ConnectivityManager::WiFiAPStateToStr(WiFiAPState state)
555 : {
556 : return ImplClass::_WiFiAPStateToStr(state);
557 : }
558 :
559 : inline const char * ConnectivityManager::CHIPoBLEServiceModeToStr(CHIPoBLEServiceMode mode)
560 : {
561 : return ImplClass::_CHIPoBLEServiceModeToStr(mode);
562 : }
563 :
564 : inline CHIP_ERROR ConnectivityManager::Init()
565 : {
566 : return static_cast<ImplClass *>(this)->_Init();
567 : }
568 :
569 : inline void ConnectivityManager::OnPlatformEvent(const ChipDeviceEvent * event)
570 : {
571 : static_cast<ImplClass *>(this)->_OnPlatformEvent(event);
572 : }
573 :
574 : inline bool ConnectivityManager::CanStartWiFiScan()
575 : {
576 : return static_cast<ImplClass *>(this)->_CanStartWiFiScan();
577 : }
578 :
579 : inline void ConnectivityManager::OnWiFiScanDone()
580 : {
581 : static_cast<ImplClass *>(this)->_OnWiFiScanDone();
582 : }
583 :
584 : inline void ConnectivityManager::OnWiFiStationProvisionChange()
585 : {
586 : static_cast<ImplClass *>(this)->_OnWiFiStationProvisionChange();
587 : }
588 :
589 : } // namespace DeviceLayer
590 : } // namespace chip
|