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 GetPrimary802154MACAddress(uint8_t * buf);
107 : CHIP_ERROR GetThreadVersion(uint16_t & version);
108 :
109 : CHIP_ERROR SetThreadProvision(ByteSpan aDataset);
110 : CHIP_ERROR SetThreadEnabled(bool val);
111 : CHIP_ERROR AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
112 : NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback);
113 : CHIP_ERROR StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback);
114 : void OnThreadAttachFinished(void);
115 :
116 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
117 : CHIP_ERROR AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
118 : const Span<const char * const> & aSubTypes, const Span<const Dnssd::TextEntry> & aTxtEntries,
119 : uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval);
120 : CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName);
121 : CHIP_ERROR InvalidateAllSrpServices(); ///< Mark all SRP services as invalid
122 : CHIP_ERROR RemoveInvalidSrpServices(); ///< Remove SRP services marked as invalid
123 :
124 : /*
125 : * @brief Utility function to clear all thread SRP host and services established between the SRP server and client.
126 : * It is expected that a transaction is done between the SRP server and client so the clear request is applied on both ends
127 : *
128 : * A generic implementation is provided in `GenericThreadStackManagerImpl_OpenThread` with the SoC OT stack
129 : */
130 : CHIP_ERROR ClearAllSrpHostAndServices();
131 :
132 : /*
133 : * @brief Used to synchronize on the SRP server response confirming the clearing of the host and service entries
134 : * Should be called in ClearAllSrpHostAndServices once the request is sent.
135 : */
136 : void WaitOnSrpClearAllComplete();
137 :
138 : /*
139 : * @brief Notify that the SRP server confirmed the clearing of the host and service entries
140 : * Should be called in the SRP Client set callback in the removal confirmation.
141 : */
142 : void NotifySrpClearAllComplete();
143 : CHIP_ERROR SetupSrpHost(const char * aHostName);
144 : CHIP_ERROR ClearSrpHost(const char * aHostName);
145 : CHIP_ERROR SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback, DnsAsyncReturnCallback aErrorCallback, void * aContext);
146 :
147 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
148 : CHIP_ERROR DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext);
149 : CHIP_ERROR DnsResolve(const char * aServiceName, const char * aInstanceName, DnsResolveCallback aCallback, void * aContext);
150 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
151 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
152 :
153 : void ResetThreadNetworkDiagnosticsCounts(void);
154 :
155 : private:
156 : // ===== Members for internal use by the following friends.
157 :
158 : friend class PlatformManagerImpl;
159 : friend class ConfigurationManagerImpl;
160 : friend class DeviceControlServer;
161 : #if CHIP_DEVICE_CONFIG_ENABLE_NFC_BASED_COMMISSIONING
162 : friend class Internal::NFCCommissioningManagerImpl;
163 : #endif
164 : #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
165 : friend class Internal::BLEManagerImpl;
166 : #endif
167 : template <class>
168 : friend class Internal::GenericPlatformManagerImpl;
169 : template <class>
170 : friend class Internal::GenericConfigurationManagerImpl;
171 : template <class>
172 : friend class Internal::GenericPlatformManagerImpl_CMSISOS;
173 : template <class>
174 : friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
175 : template <class>
176 : friend class Internal::GenericConnectivityManagerImpl_Thread;
177 : template <class>
178 : friend class Internal::GenericThreadStackManagerImpl_OpenThread;
179 : template <class>
180 : friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
181 : template <class>
182 : friend class Internal::GenericThreadStackManagerImpl_FreeRTOS;
183 :
184 : void OnPlatformEvent(const ChipDeviceEvent * event);
185 : void ErasePersistentInfo();
186 : ConnectivityManager::ThreadDeviceType GetThreadDeviceType();
187 : CHIP_ERROR SetThreadDeviceType(ConnectivityManager::ThreadDeviceType threadRole);
188 :
189 : #if CHIP_CONFIG_ENABLE_ICD_SERVER
190 : CHIP_ERROR SetPollingInterval(System::Clock::Milliseconds32 pollingInterval);
191 : #endif
192 :
193 : protected:
194 : // Construction/destruction limited to subclasses.
195 : ThreadStackManager() = default;
196 : ~ThreadStackManager() = default;
197 :
198 : // No copy, move or assignment.
199 : ThreadStackManager(const ThreadStackManager &) = delete;
200 : ThreadStackManager(const ThreadStackManager &&) = delete;
201 : ThreadStackManager & operator=(const ThreadStackManager &) = delete;
202 : };
203 :
204 : /**
205 : * Returns the public interface of the ThreadStackManager singleton object.
206 : *
207 : * chip applications should use this to access features of the ThreadStackManager object
208 : * that are common to all platforms.
209 : */
210 : extern ThreadStackManager & ThreadStackMgr();
211 :
212 : /**
213 : * Returns the platform-specific implementation of the ThreadStackManager singleton object.
214 : *
215 : * chip applications can use this to gain access to features of the ThreadStackManager
216 : * that are specific to the selected platform.
217 : */
218 : extern ThreadStackManagerImpl & ThreadStackMgrImpl();
219 :
220 : } // namespace DeviceLayer
221 : } // namespace chip
222 :
223 : /* Include a header file containing the implementation of the ThreadStackManager
224 : * object for the selected platform.
225 : */
226 : #ifdef EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
227 : #include EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
228 : #elif defined(CHIP_DEVICE_LAYER_TARGET)
229 : #define THREADSTACKMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ThreadStackManagerImpl.h>
230 : #include THREADSTACKMANAGERIMPL_HEADER
231 : #endif // defined(CHIP_DEVICE_LAYER_TARGET)
232 :
233 : namespace chip {
234 : namespace DeviceLayer {
235 :
236 : inline CHIP_ERROR ThreadStackManager::InitThreadStack()
237 : {
238 : return static_cast<ImplClass *>(this)->_InitThreadStack();
239 : }
240 :
241 : inline void ThreadStackManager::ProcessThreadActivity()
242 : {
243 : static_cast<ImplClass *>(this)->_ProcessThreadActivity();
244 : }
245 :
246 : inline CHIP_ERROR ThreadStackManager::StartThreadTask()
247 : {
248 : return static_cast<ImplClass *>(this)->_StartThreadTask();
249 : }
250 :
251 : inline void ThreadStackManager::LockThreadStack()
252 : {
253 : static_cast<ImplClass *>(this)->_LockThreadStack();
254 : }
255 :
256 : inline bool ThreadStackManager::TryLockThreadStack()
257 : {
258 : return static_cast<ImplClass *>(this)->_TryLockThreadStack();
259 : }
260 :
261 : inline void ThreadStackManager::UnlockThreadStack()
262 : {
263 : static_cast<ImplClass *>(this)->_UnlockThreadStack();
264 : }
265 :
266 : /**
267 : * Determines whether a route exists via the Thread interface to the specified destination address.
268 : */
269 : inline bool ThreadStackManager::HaveRouteToAddress(const chip::Inet::IPAddress & destAddr)
270 : {
271 : return static_cast<ImplClass *>(this)->_HaveRouteToAddress(destAddr);
272 : }
273 :
274 25 : inline void ThreadStackManager::OnPlatformEvent(const ChipDeviceEvent * event)
275 : {
276 25 : static_cast<ImplClass *>(this)->_OnPlatformEvent(event);
277 25 : }
278 :
279 0 : inline bool ThreadStackManager::IsThreadEnabled()
280 : {
281 0 : return static_cast<ImplClass *>(this)->_IsThreadEnabled();
282 : }
283 :
284 0 : inline CHIP_ERROR ThreadStackManager::SetThreadEnabled(bool val)
285 : {
286 0 : return static_cast<ImplClass *>(this)->_SetThreadEnabled(val);
287 : }
288 :
289 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
290 : inline CHIP_ERROR ThreadStackManager::AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
291 : const Span<const char * const> & aSubTypes,
292 : const Span<const Dnssd::TextEntry> & aTxtEntries, uint32_t aLeaseInterval = 0,
293 : uint32_t aKeyLeaseInterval = 0)
294 : {
295 : return static_cast<ImplClass *>(this)->_AddSrpService(aInstanceName, aName, aPort, aSubTypes, aTxtEntries, aLeaseInterval,
296 : aKeyLeaseInterval);
297 : }
298 :
299 : inline CHIP_ERROR ThreadStackManager::RemoveSrpService(const char * aInstanceName, const char * aName)
300 : {
301 : return static_cast<ImplClass *>(this)->_RemoveSrpService(aInstanceName, aName);
302 : }
303 :
304 : inline CHIP_ERROR ThreadStackManager::InvalidateAllSrpServices()
305 : {
306 : return static_cast<ImplClass *>(this)->_InvalidateAllSrpServices();
307 : }
308 :
309 : inline CHIP_ERROR ThreadStackManager::RemoveInvalidSrpServices()
310 : {
311 : return static_cast<ImplClass *>(this)->_RemoveInvalidSrpServices();
312 : }
313 :
314 : inline CHIP_ERROR ThreadStackManager::ClearAllSrpHostAndServices()
315 : {
316 : return static_cast<ImplClass *>(this)->_ClearAllSrpHostAndServices();
317 : }
318 :
319 : inline void ThreadStackManager::WaitOnSrpClearAllComplete()
320 : {
321 : return static_cast<ImplClass *>(this)->_WaitOnSrpClearAllComplete();
322 : }
323 :
324 : inline void ThreadStackManager::NotifySrpClearAllComplete()
325 : {
326 : return static_cast<ImplClass *>(this)->_NotifySrpClearAllComplete();
327 : }
328 :
329 : inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName)
330 : {
331 : return static_cast<ImplClass *>(this)->_SetupSrpHost(aHostName);
332 : }
333 :
334 : inline CHIP_ERROR ThreadStackManager::ClearSrpHost(const char * aHostName)
335 : {
336 : return static_cast<ImplClass *>(this)->_ClearSrpHost(aHostName);
337 : }
338 :
339 : inline CHIP_ERROR ThreadStackManager::SetSrpDnsCallbacks(DnsAsyncReturnCallback aInitCallback,
340 : DnsAsyncReturnCallback aErrorCallback, void * aContext)
341 : {
342 : return static_cast<ImplClass *>(this)->_SetSrpDnsCallbacks(aInitCallback, aErrorCallback, aContext);
343 : }
344 :
345 : #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
346 : inline CHIP_ERROR ThreadStackManager::DnsBrowse(const char * aServiceName, DnsBrowseCallback aCallback, void * aContext)
347 : {
348 : return static_cast<ImplClass *>(this)->_DnsBrowse(aServiceName, aCallback, aContext);
349 : }
350 :
351 : inline CHIP_ERROR ThreadStackManager::DnsResolve(const char * aServiceName, const char * aInstanceName,
352 : DnsResolveCallback aCallback, void * aContext)
353 : {
354 : return static_cast<ImplClass *>(this)->_DnsResolve(aServiceName, aInstanceName, aCallback, aContext);
355 : }
356 :
357 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_DNS_CLIENT
358 : #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
359 :
360 0 : inline bool ThreadStackManager::IsThreadProvisioned()
361 : {
362 0 : return static_cast<ImplClass *>(this)->_IsThreadProvisioned();
363 : }
364 :
365 0 : inline bool ThreadStackManager::IsThreadAttached()
366 : {
367 0 : return static_cast<ImplClass *>(this)->_IsThreadAttached();
368 : }
369 :
370 0 : inline CHIP_ERROR ThreadStackManager::GetThreadProvision(Thread::OperationalDataset & dataset)
371 : {
372 0 : return static_cast<ImplClass *>(this)->_GetThreadProvision(dataset);
373 : }
374 :
375 0 : inline CHIP_ERROR ThreadStackManager::SetThreadProvision(ByteSpan netInfo)
376 : {
377 0 : return static_cast<ImplClass *>(this)->_SetThreadProvision(netInfo);
378 : }
379 :
380 : inline CHIP_ERROR
381 0 : ThreadStackManager::AttachToThreadNetwork(const Thread::OperationalDataset & dataset,
382 : NetworkCommissioning::Internal::WirelessDriver::ConnectCallback * callback)
383 : {
384 0 : return static_cast<ImplClass *>(this)->_AttachToThreadNetwork(dataset, callback);
385 : }
386 :
387 0 : inline void ThreadStackManager::OnThreadAttachFinished(void)
388 : {
389 0 : static_cast<ImplClass *>(this)->_OnThreadAttachFinished();
390 0 : }
391 :
392 0 : inline CHIP_ERROR ThreadStackManager::StartThreadScan(NetworkCommissioning::ThreadDriver::ScanCallback * callback)
393 : {
394 0 : return static_cast<ImplClass *>(this)->_StartThreadScan(callback);
395 : }
396 :
397 1 : inline void ThreadStackManager::ErasePersistentInfo()
398 : {
399 1 : static_cast<ImplClass *>(this)->_ErasePersistentInfo();
400 1 : }
401 :
402 : inline ConnectivityManager::ThreadDeviceType ThreadStackManager::GetThreadDeviceType()
403 : {
404 : return static_cast<ImplClass *>(this)->_GetThreadDeviceType();
405 : }
406 :
407 : inline CHIP_ERROR ThreadStackManager::SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType)
408 : {
409 : return static_cast<ImplClass *>(this)->_SetThreadDeviceType(deviceType);
410 : }
411 :
412 : #if CHIP_CONFIG_ENABLE_ICD_SERVER
413 : inline CHIP_ERROR ThreadStackManager::SetPollingInterval(System::Clock::Milliseconds32 pollingInterval)
414 : {
415 : return static_cast<ImplClass *>(this)->_SetPollingInterval(pollingInterval);
416 : }
417 : #endif // CHIP_CONFIG_ENABLE_ICD_SERVER
418 :
419 10 : inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
420 : {
421 10 : return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
422 : }
423 :
424 0 : inline CHIP_ERROR ThreadStackManager::GetThreadVersion(uint16_t & version)
425 : {
426 0 : return static_cast<ImplClass *>(this)->_GetThreadVersion(version);
427 : }
428 :
429 0 : inline void ThreadStackManager::ResetThreadNetworkDiagnosticsCounts()
430 : {
431 0 : static_cast<ImplClass *>(this)->_ResetThreadNetworkDiagnosticsCounts();
432 0 : }
433 :
434 : } // namespace DeviceLayer
435 : } // namespace chip
|