Line data Source code
1 : /*
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : * Copyright (c) 2019 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 ThreadStackManager object.
22 : */
23 :
24 : #pragma once
25 :
26 : #include <app/icd/server/ICDServerConfig.h>
27 :
28 : #include <app/util/basic-types.h>
29 : #include <inet/IPAddress.h>
30 : #include <lib/support/Span.h>
31 : #include <platform/NetworkCommissioning.h>
32 :
33 : namespace chip {
34 :
35 : namespace Dnssd {
36 : struct TextEntry;
37 : struct DnssdService;
38 : } // namespace Dnssd
39 :
40 : namespace Thread {
41 : class OperationalDataset;
42 : } // namespace Thread
43 :
44 : namespace DeviceLayer {
45 :
46 : class PlatformManagerImpl;
47 : class ThreadStackManagerImpl;
48 : class ConfigurationManagerImpl;
49 : class DeviceControlServer;
50 :
51 : namespace Internal {
52 : class NFCCommissioningManagerImpl;
53 : class BLEManagerImpl;
54 : template <class>
55 : class GenericPlatformManagerImpl;
56 : template <class>
57 : class GenericConfigurationManagerImpl;
58 : template <class>
59 : class GenericPlatformManagerImpl_CMSISOS;
60 : template <class>
61 : class GenericPlatformManagerImpl_FreeRTOS;
62 : template <class>
63 : class GenericConnectivityManagerImpl_Thread;
64 : template <class>
65 : class GenericThreadStackManagerImpl_OpenThread;
66 : template <class>
67 : class GenericThreadStackManagerImpl_OpenThread_LwIP;
68 : template <class>
69 : class GenericThreadStackManagerImpl_FreeRTOS;
70 : } // namespace Internal
71 :
72 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
73 : // Declaration of callback types corresponding to DnssdResolveCallback and DnssdBrowseCallback to avoid circular including.
74 : using DnsResolveCallback = void (*)(void * context, chip::Dnssd::DnssdService * result, const Span<Inet::IPAddress> & addresses,
75 : CHIP_ERROR error);
76 : using DnsBrowseCallback = void (*)(void * context, chip::Dnssd::DnssdService * services, size_t servicesSize, bool finalBrowse,
77 : CHIP_ERROR error);
78 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
79 :
80 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
81 : using DnsAsyncReturnCallback = void (*)(void * context, CHIP_ERROR error);
82 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
83 :
84 : /**
85 : * Provides features for initializing and interacting with the Thread stack on
86 : * a chip-enabled device.
87 : */
88 : class ThreadStackManager
89 : {
90 : using ImplClass = ThreadStackManagerImpl;
91 :
92 : public:
93 : // ===== Members that define the public interface of the ThreadStackManager
94 :
95 : CHIP_ERROR InitThreadStack();
96 : void ProcessThreadActivity();
97 : CHIP_ERROR StartThreadTask();
98 : void LockThreadStack();
99 : bool TryLockThreadStack();
100 : void UnlockThreadStack();
101 : bool HaveRouteToAddress(const chip::Inet::IPAddress & destAddr);
102 : bool IsThreadEnabled();
103 : bool IsThreadProvisioned();
104 : bool IsThreadAttached();
105 : CHIP_ERROR GetThreadProvision(Thread::OperationalDataset & dataset);
106 : CHIP_ERROR GetAndLogThreadStatsCounters();
107 : CHIP_ERROR GetAndLogThreadTopologyMinimal();
108 : CHIP_ERROR GetAndLogThreadTopologyFull();
109 : CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf);
110 : CHIP_ERROR GetExternalIPv6Address(chip::Inet::IPAddress & addr);
111 : CHIP_ERROR GetThreadVersion(uint16_t & version);
112 : CHIP_ERROR GetPollPeriod(uint32_t & buf);
113 :
114 : CHIP_ERROR SetThreadProvision(ByteSpan aDataset);
115 : CHIP_ERROR SetThreadEnabled(bool val);
116 : CHIP_ERROR AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
117 : NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback);
118 : CHIP_ERROR StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback);
119 : void OnThreadAttachFinished(void);
120 :
121 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
122 : CHIP_ERROR AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
123 : const Span<const char * const> & aSubTypes, const Span<const Dnssd::TextEntry> & aTxtEntries,
124 : uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval);
125 : CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName);
126 : CHIP_ERROR InvalidateAllSrpServices(); ///< Mark all SRP services as invalid
127 : CHIP_ERROR RemoveInvalidSrpServices(); ///< Remove SRP services marked as invalid
128 :
129 : /*
130 : * @brief Utility function to clear all thread SRP host and services established between the SRP server and client.
131 : * It is expected that a transaction is done between the SRP server and client so the clear request is applied on both ends
132 : *
133 : * A generic implementation is provided in `GenericThreadStackManagerImpl_OpenThread` with the SoC OT stack
134 : */
135 : CHIP_ERROR ClearAllSrpHostAndServices();
136 :
137 : /*
138 : * @brief Used to synchronize on the SRP server response confirming the clearing of the host and service entries
139 : * Should be called in ClearAllSrpHostAndServices once the request is sent.
140 : */
141 : void WaitOnSrpClearAllComplete();
142 :
143 : /*
144 : * @brief Notify that the SRP server confirmed the clearing of the host and service entries
145 : * Should be called in the SRP Client set callback in the removal confirmation.
146 : */
147 : void NotifySrpClearAllComplete();
148 : CHIP_ERROR SetupSrpHost(const char * aHostName);
149 : CHIP_ERROR ClearSrpHost(const char * aHostName);
150 : CHIP_ERROR SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback, DnsAsyncReturnCallback aErrorCallback, void * aContext);
151 :
152 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
153 : CHIP_ERROR DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext);
154 : CHIP_ERROR DnsResolve(const char * aServiceName, const char * aInstanceName, DnsResolveCallback aCallback, void * aContext);
155 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
156 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
157 :
158 : void ResetThreadNetworkDiagnosticsCounts(void);
159 :
160 : private:
161 : // ===== Members for internal use by the following friends.
162 :
163 : friend class PlatformManagerImpl;
164 : friend class ConfigurationManagerImpl;
165 : friend class DeviceControlServer;
166 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
167 : friend class Internal::NFCCommissioningManagerImpl;
168 : #endif
169 : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
170 : friend class Internal::BLEManagerImpl;
171 : #endif
172 : template <class>
173 : friend class Internal::GenericPlatformManagerImpl;
174 : template <class>
175 : friend class Internal::GenericConfigurationManagerImpl;
176 : template <class>
177 : friend class Internal::GenericPlatformManagerImpl_CMSISOS;
178 : template <class>
179 : friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
180 : template <class>
181 : friend class Internal::GenericConnectivityManagerImpl_Thread;
182 : template <class>
183 : friend class Internal::GenericThreadStackManagerImpl_OpenThread;
184 : template <class>
185 : friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
186 : template <class>
187 : friend class Internal::GenericThreadStackManagerImpl_FreeRTOS;
188 :
189 : void OnPlatformEvent(const ChipDeviceEvent * event);
190 : void ErasePersistentInfo();
191 : ConnectivityManager::ThreadDeviceType GetThreadDeviceType();
192 : CHIP_ERROR SetThreadDeviceType(ConnectivityManager::ThreadDeviceType threadRole);
193 :
194 : #if CHIP_CONFIG_ENABLE_ICD_SERVER
195 : CHIP_ERROR SetPollingInterval(System::Clock::Milliseconds32 pollingInterval);
196 : #endif
197 :
198 : bool HaveMeshConnectivity();
199 :
200 : protected:
201 : // Construction/destruction limited to subclasses.
202 : ThreadStackManager() = default;
203 : ~ThreadStackManager() = default;
204 :
205 : // No copy, move or assignment.
206 : ThreadStackManager(const ThreadStackManager &) = delete;
207 : ThreadStackManager(const ThreadStackManager &&) = delete;
208 : ThreadStackManager & operator=(const ThreadStackManager &) = delete;
209 : };
210 :
211 : /**
212 : * Returns the public interface of the ThreadStackManager singleton object.
213 : *
214 : * chip applications should use this to access features of the ThreadStackManager object
215 : * that are common to all platforms.
216 : */
217 : extern ThreadStackManager & ThreadStackMgr();
218 :
219 : /**
220 : * Returns the platform-specific implementation of the ThreadStackManager singleton object.
221 : *
222 : * chip applications can use this to gain access to features of the ThreadStackManager
223 : * that are specific to the selected platform.
224 : */
225 : extern ThreadStackManagerImpl & ThreadStackMgrImpl();
226 :
227 : } // namespace DeviceLayer
228 : } // namespace chip
229 :
230 : /* Include a header file containing the implementation of the ThreadStackManager
231 : * object for the selected platform.
232 : */
233 : #ifdef EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
234 : #include EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
235 : #elif defined(CHIP_DEVICE_LAYER_TARGET)
236 : #define THREADSTACKMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ThreadStackManagerImpl.h>
237 : #include THREADSTACKMANAGERIMPL_HEADER
238 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)
239 :
240 : namespace chip {
241 : namespace DeviceLayer {
242 :
243 : inline CHIP_ERROR ThreadStackManager::InitThreadStack()
244 : {
245 : return static_cast<ImplClass *>(this)->_InitThreadStack();
246 : }
247 :
248 : inline void ThreadStackManager::ProcessThreadActivity()
249 : {
250 : static_cast<ImplClass *>(this)->_ProcessThreadActivity();
251 : }
252 :
253 : inline CHIP_ERROR ThreadStackManager::StartThreadTask()
254 : {
255 : return static_cast<ImplClass *>(this)->_StartThreadTask();
256 : }
257 :
258 : inline void ThreadStackManager::LockThreadStack()
259 : {
260 : static_cast<ImplClass *>(this)->_LockThreadStack();
261 : }
262 :
263 : inline bool ThreadStackManager::TryLockThreadStack()
264 : {
265 : return static_cast<ImplClass *>(this)->_TryLockThreadStack();
266 : }
267 :
268 : inline void ThreadStackManager::UnlockThreadStack()
269 : {
270 : static_cast<ImplClass *>(this)->_UnlockThreadStack();
271 : }
272 :
273 : /**
274 : * Determines whether a route exists via the Thread interface to the specified destination address.
275 : */
276 : inline bool ThreadStackManager::HaveRouteToAddress(const chip::Inet::IPAddress & destAddr)
277 : {
278 : return static_cast<ImplClass *>(this)->_HaveRouteToAddress(destAddr);
279 : }
280 :
281 19 : inline void ThreadStackManager::OnPlatformEvent(const ChipDeviceEvent * event)
282 : {
283 19 : static_cast<ImplClass *>(this)->_OnPlatformEvent(event);
284 19 : }
285 :
286 0 : inline bool ThreadStackManager::IsThreadEnabled()
287 : {
288 0 : return static_cast<ImplClass *>(this)->_IsThreadEnabled();
289 : }
290 :
291 0 : inline CHIP_ERROR ThreadStackManager::SetThreadEnabled(bool val)
292 : {
293 0 : return static_cast<ImplClass *>(this)->_SetThreadEnabled(val);
294 : }
295 :
296 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
297 : inline CHIP_ERROR ThreadStackManager::AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
298 : const Span<const char * const> & aSubTypes,
299 : const Span<const Dnssd::TextEntry> & aTxtEntries, uint32_t aLeaseInterval = 0,
300 : uint32_t aKeyLeaseInterval = 0)
301 : {
302 : return static_cast<ImplClass *>(this)->_AddSrpService(aInstanceName, aName, aPort, aSubTypes, aTxtEntries, aLeaseInterval,
303 : aKeyLeaseInterval);
304 : }
305 :
306 : inline CHIP_ERROR ThreadStackManager::RemoveSrpService(const char * aInstanceName, const char * aName)
307 : {
308 : return static_cast<ImplClass *>(this)->_RemoveSrpService(aInstanceName, aName);
309 : }
310 :
311 : inline CHIP_ERROR ThreadStackManager::InvalidateAllSrpServices()
312 : {
313 : return static_cast<ImplClass *>(this)->_InvalidateAllSrpServices();
314 : }
315 :
316 : inline CHIP_ERROR ThreadStackManager::RemoveInvalidSrpServices()
317 : {
318 : return static_cast<ImplClass *>(this)->_RemoveInvalidSrpServices();
319 : }
320 :
321 : inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices()
322 : {
323 : return static_cast<ImplClass *>(this)->_ClearAllSrpHostAndServices();
324 : }
325 :
326 : inline void ThreadStackManager::WaitOnSrpClearAllComplete()
327 : {
328 : return static_cast<ImplClass *>(this)->_WaitOnSrpClearAllComplete();
329 : }
330 :
331 : inline void ThreadStackManager::NotifySrpClearAllComplete()
332 : {
333 : return static_cast<ImplClass *>(this)->_NotifySrpClearAllComplete();
334 : }
335 :
336 : inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName)
337 : {
338 : return static_cast<ImplClass *>(this)->_SetupSrpHost(aHostName);
339 : }
340 :
341 : inline CHIP_ERROR ThreadStackManager::ClearSrpHost(const char * aHostName)
342 : {
343 : return static_cast<ImplClass *>(this)->_ClearSrpHost(aHostName);
344 : }
345 :
346 : inline CHIP_ERROR ThreadStackManager::SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback,
347 : DnsAsyncReturnCallback aErrorCallback, void * aContext)
348 : {
349 : return static_cast<ImplClass *>(this)->_SetSrpDnsCallbacks(aInitCallback, aErrorCallback, aContext);
350 : }
351 :
352 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
353 : inline CHIP_ERROR ThreadStackManager::DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext)
354 : {
355 : return static_cast<ImplClass *>(this)->_DnsBrowse(aServiceName, aCallback, aContext);
356 : }
357 :
358 : inline CHIP_ERROR ThreadStackManager::DnsResolve(const char * aServiceName, const char * aInstanceName,
359 : DnsResolveCallback aCallback, void * aContext)
360 : {
361 : return static_cast<ImplClass *>(this)->_DnsResolve(aServiceName, aInstanceName, aCallback, aContext);
362 : }
363 :
364 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
365 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
366 :
367 0 : inline bool ThreadStackManager::IsThreadProvisioned()
368 : {
369 0 : return static_cast<ImplClass *>(this)->_IsThreadProvisioned();
370 : }
371 :
372 0 : inline bool ThreadStackManager::IsThreadAttached()
373 : {
374 0 : return static_cast<ImplClass *>(this)->_IsThreadAttached();
375 : }
376 :
377 0 : inline CHIP_ERROR ThreadStackManager::GetThreadProvision(Thread::OperationalDataset & dataset)
378 : {
379 0 : return static_cast<ImplClass *>(this)->_GetThreadProvision(dataset);
380 : }
381 :
382 0 : inline CHIP_ERROR ThreadStackManager::SetThreadProvision(ByteSpan netInfo)
383 : {
384 0 : return static_cast<ImplClass *>(this)->_SetThreadProvision(netInfo);
385 : }
386 :
387 : inline CHIP_ERROR
388 0 : ThreadStackManager::AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
389 : NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback)
390 : {
391 0 : return static_cast<ImplClass *>(this)->_AttachToThreadNetwork(dataset, callback);
392 : }
393 :
394 0 : inline void ThreadStackManager::OnThreadAttachFinished(void)
395 : {
396 0 : static_cast<ImplClass *>(this)->_OnThreadAttachFinished();
397 0 : }
398 :
399 0 : inline CHIP_ERROR ThreadStackManager::StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback)
400 : {
401 0 : return static_cast<ImplClass *>(this)->_StartThreadScan(callback);
402 : }
403 :
404 1 : inline void ThreadStackManager::ErasePersistentInfo()
405 : {
406 1 : static_cast<ImplClass *>(this)->_ErasePersistentInfo();
407 1 : }
408 :
409 : inline ConnectivityManager::ThreadDeviceType ThreadStackManager::GetThreadDeviceType()
410 : {
411 : return static_cast<ImplClass *>(this)->_GetThreadDeviceType();
412 : }
413 :
414 : inline CHIP_ERROR ThreadStackManager::SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType)
415 : {
416 : return static_cast<ImplClass *>(this)->_SetThreadDeviceType(deviceType);
417 : }
418 :
419 : #if CHIP_CONFIG_ENABLE_ICD_SERVER
420 : inline CHIP_ERROR ThreadStackManager::SetPollingInterval(System::Clock::Milliseconds32 pollingInterval)
421 : {
422 : return static_cast<ImplClass *>(this)->_SetPollingInterval(pollingInterval);
423 : }
424 : #endif // CHIP_CONFIG_ENABLE_ICD_SERVER
425 :
426 : inline bool ThreadStackManager::HaveMeshConnectivity()
427 : {
428 : return static_cast<ImplClass *>(this)->_HaveMeshConnectivity();
429 : }
430 :
431 : inline CHIP_ERROR ThreadStackManager::GetAndLogThreadStatsCounters()
432 : {
433 : return static_cast<ImplClass *>(this)->_GetAndLogThreadStatsCounters();
434 : }
435 :
436 : inline CHIP_ERROR ThreadStackManager::GetAndLogThreadTopologyMinimal()
437 : {
438 : return static_cast<ImplClass *>(this)->_GetAndLogThreadTopologyMinimal();
439 : }
440 :
441 : inline CHIP_ERROR ThreadStackManager::GetAndLogThreadTopologyFull()
442 : {
443 : return static_cast<ImplClass *>(this)->_GetAndLogThreadTopologyFull();
444 : }
445 :
446 8 : inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
447 : {
448 8 : return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
449 : }
450 :
451 : inline CHIP_ERROR ThreadStackManager::GetExternalIPv6Address(chip::Inet::IPAddress & addr)
452 : {
453 : return static_cast<ImplClass *>(this)->_GetExternalIPv6Address(addr);
454 : }
455 :
456 0 : inline CHIP_ERROR ThreadStackManager::GetThreadVersion(uint16_t & version)
457 : {
458 0 : return static_cast<ImplClass *>(this)->_GetThreadVersion(version);
459 : }
460 :
461 : inline CHIP_ERROR ThreadStackManager::GetPollPeriod(uint32_t & buf)
462 : {
463 : return static_cast<ImplClass *>(this)->_GetPollPeriod(buf);
464 : }
465 :
466 : inline void ThreadStackManager::ResetThreadNetworkDiagnosticsCounts()
467 : {
468 : static_cast<ImplClass *>(this)->_ResetThreadNetworkDiagnosticsCounts();
469 : }
470 :
471 : } // namespace DeviceLayer
472 : } // namespace chip
|