Apply Upstream code (2021-03-15)
[platform/upstream/connectedhomeip.git] / src / include / platform / ThreadStackManager.h
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 namespace chip {
27
28 namespace Mdns {
29 struct TextEntry;
30 }
31
32 namespace DeviceLayer {
33
34 class PlatformManagerImpl;
35 class ThreadStackManagerImpl;
36 class ConfigurationManagerImpl;
37
38 namespace Internal {
39 class DeviceNetworkInfo;
40 class DeviceControlServer;
41 class BLEManagerImpl;
42 template <class>
43 class GenericPlatformManagerImpl;
44 template <class>
45 class GenericConfigurationManagerImpl;
46 template <class>
47 class GenericPlatformManagerImpl_FreeRTOS;
48 template <class>
49 class GenericConnectivityManagerImpl_Thread;
50 template <class>
51 class GenericThreadStackManagerImpl_OpenThread;
52 template <class>
53 class GenericThreadStackManagerImpl_OpenThread_LwIP;
54 template <class>
55 class GenericThreadStackManagerImpl_FreeRTOS;
56 template <class>
57 class GenericNetworkProvisioningServerImpl;
58 } // namespace Internal
59
60 /**
61  * Provides features for initializing and interacting with the Thread stack on
62  * a chip-enabled device.
63  */
64 class ThreadStackManager
65 {
66     using ImplClass = ThreadStackManagerImpl;
67
68 public:
69     // ===== Members that define the public interface of the ThreadStackManager
70
71     CHIP_ERROR InitThreadStack();
72     void ProcessThreadActivity();
73     CHIP_ERROR StartThreadTask();
74     void LockThreadStack();
75     bool TryLockThreadStack();
76     void UnlockThreadStack();
77     bool HaveRouteToAddress(const chip::Inet::IPAddress & destAddr);
78     CHIP_ERROR GetAndLogThreadStatsCounters();
79     CHIP_ERROR GetAndLogThreadTopologyMinimal();
80     CHIP_ERROR GetAndLogThreadTopologyFull();
81     CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf);
82     CHIP_ERROR GetFactoryAssignedEUI64(uint8_t (&buf)[8]);
83     CHIP_ERROR GetExternalIPv6Address(chip::Inet::IPAddress & addr);
84
85     CHIP_ERROR JoinerStart();
86     CHIP_ERROR SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo);
87     CHIP_ERROR SetThreadProvision(const uint8_t * operationalDataset, size_t operationalDatasetLen);
88     CHIP_ERROR SetThreadEnabled(bool val);
89
90 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
91     CHIP_ERROR AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort, chip::Mdns::TextEntry * aTxtEntries,
92                              size_t aTxtEntiresSize, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval);
93     CHIP_ERROR RemoveSrpService(const char * aInstanceName, const char * aName);
94     CHIP_ERROR SetupSrpHost(const char * aHostName);
95 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
96
97 private:
98     // ===== Members for internal use by the following friends.
99
100     friend class PlatformManagerImpl;
101     friend class ConfigurationManagerImpl;
102 #if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
103     friend class Internal::BLEManagerImpl;
104 #endif
105     friend class Internal::DeviceControlServer;
106     template <class>
107     friend class Internal::GenericPlatformManagerImpl;
108     template <class>
109     friend class Internal::GenericConfigurationManagerImpl;
110     template <class>
111     friend class Internal::GenericPlatformManagerImpl_FreeRTOS;
112     template <class>
113     friend class Internal::GenericConnectivityManagerImpl_Thread;
114     template <class>
115     friend class Internal::GenericThreadStackManagerImpl_OpenThread;
116     template <class>
117     friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
118     template <class>
119     friend class Internal::GenericThreadStackManagerImpl_FreeRTOS;
120     template <class>
121     friend class Internal::GenericNetworkProvisioningServerImpl;
122
123     void OnPlatformEvent(const ChipDeviceEvent * event);
124     bool IsThreadEnabled();
125     bool IsThreadProvisioned();
126     bool IsThreadAttached();
127     CHIP_ERROR GetThreadProvision(Internal::DeviceNetworkInfo & netInfo, bool includeCredentials);
128     void ErasePersistentInfo();
129     ConnectivityManager::ThreadDeviceType GetThreadDeviceType();
130     CHIP_ERROR SetThreadDeviceType(ConnectivityManager::ThreadDeviceType threadRole);
131     void GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig);
132     CHIP_ERROR SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig);
133     bool HaveMeshConnectivity();
134     void OnMessageLayerActivityChanged(bool messageLayerIsActive);
135     void OnCHIPoBLEAdvertisingStart();
136     void OnCHIPoBLEAdvertisingStop();
137
138 protected:
139     // Construction/destruction limited to subclasses.
140     ThreadStackManager()  = default;
141     ~ThreadStackManager() = default;
142
143     // No copy, move or assignment.
144     ThreadStackManager(const ThreadStackManager &)  = delete;
145     ThreadStackManager(const ThreadStackManager &&) = delete;
146     ThreadStackManager & operator=(const ThreadStackManager &) = delete;
147 };
148
149 /**
150  * Returns the public interface of the ThreadStackManager singleton object.
151  *
152  * chip applications should use this to access features of the ThreadStackManager object
153  * that are common to all platforms.
154  */
155 extern ThreadStackManager & ThreadStackMgr();
156
157 /**
158  * Returns the platform-specific implementation of the ThreadStackManager singleton object.
159  *
160  * chip applications can use this to gain access to features of the ThreadStackManager
161  * that are specific to the selected platform.
162  */
163 extern ThreadStackManagerImpl & ThreadStackMgrImpl();
164
165 } // namespace DeviceLayer
166 } // namespace chip
167
168 /* Include a header file containing the implementation of the ThreadStackManager
169  * object for the selected platform.
170  */
171 #ifdef EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
172 #include EXTERNAL_THREADSTACKMANAGERIMPL_HEADER
173 #elif defined(CHIP_DEVICE_LAYER_TARGET)
174 #define THREADSTACKMANAGERIMPL_HEADER <platform/CHIP_DEVICE_LAYER_TARGET/ThreadStackManagerImpl.h>
175 #include THREADSTACKMANAGERIMPL_HEADER
176 #endif // defined(CHIP_DEVICE_LAYER_TARGET)
177
178 namespace chip {
179 namespace DeviceLayer {
180
181 inline CHIP_ERROR ThreadStackManager::InitThreadStack()
182 {
183     return static_cast<ImplClass *>(this)->_InitThreadStack();
184 }
185
186 inline void ThreadStackManager::ProcessThreadActivity()
187 {
188     static_cast<ImplClass *>(this)->_ProcessThreadActivity();
189 }
190
191 inline CHIP_ERROR ThreadStackManager::StartThreadTask()
192 {
193     return static_cast<ImplClass *>(this)->_StartThreadTask();
194 }
195
196 inline void ThreadStackManager::LockThreadStack()
197 {
198     static_cast<ImplClass *>(this)->_LockThreadStack();
199 }
200
201 inline bool ThreadStackManager::TryLockThreadStack()
202 {
203     return static_cast<ImplClass *>(this)->_TryLockThreadStack();
204 }
205
206 inline void ThreadStackManager::UnlockThreadStack()
207 {
208     static_cast<ImplClass *>(this)->_UnlockThreadStack();
209 }
210
211 /**
212  * Determines whether a route exists via the Thread interface to the specified destination address.
213  */
214 inline bool ThreadStackManager::HaveRouteToAddress(const chip::Inet::IPAddress & destAddr)
215 {
216     return static_cast<ImplClass *>(this)->_HaveRouteToAddress(destAddr);
217 }
218
219 inline void ThreadStackManager::OnPlatformEvent(const ChipDeviceEvent * event)
220 {
221     static_cast<ImplClass *>(this)->_OnPlatformEvent(event);
222 }
223
224 inline bool ThreadStackManager::IsThreadEnabled()
225 {
226     return static_cast<ImplClass *>(this)->_IsThreadEnabled();
227 }
228
229 inline CHIP_ERROR ThreadStackManager::SetThreadEnabled(bool val)
230 {
231     return static_cast<ImplClass *>(this)->_SetThreadEnabled(val);
232 }
233
234 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
235 inline CHIP_ERROR ThreadStackManager::AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort,
236                                                     chip::Mdns::TextEntry * aTxtEntries, size_t aTxtEntiresSize,
237                                                     uint32_t aLeaseInterval = 0, uint32_t aKeyLeaseInterval = 0)
238 {
239     return static_cast<ImplClass *>(this)->_AddSrpService(aInstanceName, aName, aPort, aTxtEntries, aTxtEntiresSize, aLeaseInterval,
240                                                           aKeyLeaseInterval);
241 }
242
243 inline CHIP_ERROR ThreadStackManager::RemoveSrpService(const char * aInstanceName, const char * aName)
244 {
245     return static_cast<ImplClass *>(this)->_RemoveSrpService(aInstanceName, aName);
246 }
247
248 inline CHIP_ERROR ThreadStackManager::SetupSrpHost(const char * aHostName)
249 {
250     return static_cast<ImplClass *>(this)->_SetupSrpHost(aHostName);
251 }
252 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
253
254 inline bool ThreadStackManager::IsThreadProvisioned()
255 {
256     return static_cast<ImplClass *>(this)->_IsThreadProvisioned();
257 }
258
259 inline bool ThreadStackManager::IsThreadAttached()
260 {
261     return static_cast<ImplClass *>(this)->_IsThreadAttached();
262 }
263
264 inline CHIP_ERROR ThreadStackManager::GetThreadProvision(Internal::DeviceNetworkInfo & netInfo, bool includeCredentials)
265 {
266     return static_cast<ImplClass *>(this)->_GetThreadProvision(netInfo, includeCredentials);
267 }
268
269 inline CHIP_ERROR ThreadStackManager::SetThreadProvision(const Internal::DeviceNetworkInfo & netInfo)
270 {
271     return static_cast<ImplClass *>(this)->_SetThreadProvision(netInfo);
272 }
273
274 inline CHIP_ERROR ThreadStackManager::SetThreadProvision(const uint8_t * operationalDataset, size_t operationalDatasetLen)
275 {
276     return static_cast<ImplClass *>(this)->_SetThreadProvision(operationalDataset, operationalDatasetLen);
277 }
278
279 inline void ThreadStackManager::ErasePersistentInfo()
280 {
281     static_cast<ImplClass *>(this)->_ErasePersistentInfo();
282 }
283
284 inline ConnectivityManager::ThreadDeviceType ThreadStackManager::GetThreadDeviceType()
285 {
286     return static_cast<ImplClass *>(this)->_GetThreadDeviceType();
287 }
288
289 inline CHIP_ERROR ThreadStackManager::SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType)
290 {
291     return static_cast<ImplClass *>(this)->_SetThreadDeviceType(deviceType);
292 }
293
294 inline void ThreadStackManager::GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig)
295 {
296     static_cast<ImplClass *>(this)->_GetThreadPollingConfig(pollingConfig);
297 }
298
299 inline CHIP_ERROR ThreadStackManager::SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig)
300 {
301     return static_cast<ImplClass *>(this)->_SetThreadPollingConfig(pollingConfig);
302 }
303
304 inline bool ThreadStackManager::HaveMeshConnectivity()
305 {
306     return static_cast<ImplClass *>(this)->_HaveMeshConnectivity();
307 }
308
309 inline void ThreadStackManager::OnMessageLayerActivityChanged(bool messageLayerIsActive)
310 {
311     return static_cast<ImplClass *>(this)->_OnMessageLayerActivityChanged(messageLayerIsActive);
312 }
313
314 inline void ThreadStackManager::OnCHIPoBLEAdvertisingStart()
315 {
316     static_cast<ImplClass *>(this)->_OnCHIPoBLEAdvertisingStart();
317 }
318
319 inline void ThreadStackManager::OnCHIPoBLEAdvertisingStop()
320 {
321     static_cast<ImplClass *>(this)->_OnCHIPoBLEAdvertisingStop();
322 }
323
324 inline CHIP_ERROR ThreadStackManager::GetAndLogThreadStatsCounters()
325 {
326     return static_cast<ImplClass *>(this)->_GetAndLogThreadStatsCounters();
327 }
328
329 inline CHIP_ERROR ThreadStackManager::GetAndLogThreadTopologyMinimal()
330 {
331     return static_cast<ImplClass *>(this)->_GetAndLogThreadTopologyMinimal();
332 }
333
334 inline CHIP_ERROR ThreadStackManager::GetAndLogThreadTopologyFull()
335 {
336     return static_cast<ImplClass *>(this)->_GetAndLogThreadTopologyFull();
337 }
338
339 inline CHIP_ERROR ThreadStackManager::GetPrimary802154MACAddress(uint8_t * buf)
340 {
341     return static_cast<ImplClass *>(this)->_GetPrimary802154MACAddress(buf);
342 }
343
344 inline CHIP_ERROR ThreadStackManager::GetFactoryAssignedEUI64(uint8_t (&buf)[8])
345 {
346     return static_cast<ImplClass *>(this)->_GetFactoryAssignedEUI64(buf);
347 }
348
349 inline CHIP_ERROR ThreadStackManager::GetExternalIPv6Address(chip::Inet::IPAddress & addr)
350 {
351     return static_cast<ImplClass *>(this)->_GetExternalIPv6Address(addr);
352 }
353
354 inline CHIP_ERROR ThreadStackManager::JoinerStart()
355 {
356     return static_cast<ImplClass *>(this)->_JoinerStart();
357 }
358
359 } // namespace DeviceLayer
360 } // namespace chip