b6b7972a25e7afbd9b621bbd8419653029b688f5
[platform/upstream/connectedhomeip.git] / src / platform / OpenThread / GenericThreadStackManagerImpl_OpenThread.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    Copyright (c) 2019 Nest Labs, Inc.
5  *    All rights reserved.
6  *
7  *    Licensed under the Apache License, Version 2.0 (the "License");
8  *    you may not use this file except in compliance with the License.
9  *    You may obtain a copy of the License at
10  *
11  *        http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *    Unless required by applicable law or agreed to in writing, software
14  *    distributed under the License is distributed on an "AS IS" BASIS,
15  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *    See the License for the specific language governing permissions and
17  *    limitations under the License.
18  */
19
20 /**
21  *    @file
22  *          Provides a generic implementation of ThreadStackManager features
23  *          for use on platforms that use OpenThread.
24  */
25
26 #pragma once
27
28 #include <openthread/instance.h>
29 #include <openthread/netdata.h>
30
31 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
32 #include <openthread/srp_client.h>
33 #endif
34
35 namespace chip {
36 namespace DeviceLayer {
37
38 class ThreadStackManagerImpl;
39
40 namespace Internal {
41
42 class DeviceNetworkInfo;
43
44 /**
45  * Provides a generic implementation of ThreadStackManager features that works in conjunction
46  * with OpenThread.
47  *
48  * This class contains implementations of select features from the ThreadStackManager abstract
49  * interface that are suitable for use on devices that employ OpenThread.  It is intended to
50  * be inherited, directly or indirectly, by the ThreadStackManagerImpl class, which also appears
51  * as the template's ImplClass parameter.
52  *
53  * The class is designed to be independent of the choice of host OS (e.g. RTOS or posix) and
54  * network stack (e.g. LwIP or other IP stack).
55  */
56 template <class ImplClass>
57 class GenericThreadStackManagerImpl_OpenThread
58 {
59 public:
60     // ===== Platform-specific methods directly callable by the application.
61
62     otInstance * OTInstance() const;
63     static void OnOpenThreadStateChange(uint32_t flags, void * context);
64
65 protected:
66     // ===== Methods that implement the ThreadStackManager abstract interface.
67
68     void _ProcessThreadActivity(void);
69     bool _HaveRouteToAddress(const Inet::IPAddress & destAddr);
70     void _OnPlatformEvent(const ChipDeviceEvent * event);
71     bool _IsThreadEnabled(void);
72     CHIP_ERROR _SetThreadEnabled(bool val);
73
74     bool _IsThreadProvisioned(void);
75     bool _IsThreadAttached(void);
76     CHIP_ERROR _GetThreadProvision(DeviceNetworkInfo & netInfo, bool includeCredentials);
77     CHIP_ERROR _SetThreadProvision(const DeviceNetworkInfo & netInfo);
78     CHIP_ERROR _SetThreadProvision(const uint8_t * operationalDataset, size_t operationalDatasetLen);
79     void _ErasePersistentInfo(void);
80     ConnectivityManager::ThreadDeviceType _GetThreadDeviceType(void);
81     CHIP_ERROR _SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType);
82     void _GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig);
83     CHIP_ERROR _SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig);
84     bool _HaveMeshConnectivity(void);
85     void _OnMessageLayerActivityChanged(bool messageLayerIsActive);
86     CHIP_ERROR _GetAndLogThreadStatsCounters(void);
87     CHIP_ERROR _GetAndLogThreadTopologyMinimal(void);
88     CHIP_ERROR _GetAndLogThreadTopologyFull(void);
89     CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);
90     CHIP_ERROR _GetExternalIPv6Address(chip::Inet::IPAddress & addr);
91     void _OnWoBLEAdvertisingStart(void);
92     void _OnWoBLEAdvertisingStop(void);
93
94 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
95     CHIP_ERROR _AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort, uint32_t aLeaseInterval,
96                               uint32_t aKeyLeaseInterval);
97     CHIP_ERROR _RemoveSrpService(const char * aInstanceName, const char * aName);
98     CHIP_ERROR _SetupSrpHost(const char * aHostName);
99 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
100
101     // ===== Members available to the implementation subclass.
102
103     CHIP_ERROR DoInit(otInstance * otInst);
104     bool IsThreadAttachedNoLock(void);
105     bool IsThreadInterfaceUpNoLock(void);
106     CHIP_ERROR AdjustPollingInterval(void);
107
108     CHIP_ERROR _JoinerStart(void);
109
110 private:
111     // ===== Private members for use by this class only.
112
113     otInstance * mOTInst;
114     ConnectivityManager::ThreadPollingConfig mPollingConfig;
115
116 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
117
118     struct SrpClient
119     {
120         static constexpr uint8_t kServiceId           = 0x5d;
121         static constexpr uint8_t kMaxServicesNumber   = 3;
122         static constexpr uint8_t kMaxInstanceNameSize = 64;
123         static constexpr uint8_t kMaxNameSize         = 16;
124         static constexpr uint8_t kMaxHostNameSize     = 32;
125
126         struct Service
127         {
128             otSrpClientService mService;
129             char mInstanceName[kMaxInstanceNameSize];
130             char mName[kMaxNameSize];
131         };
132
133         char mHostName[kMaxHostNameSize];
134         otIp6Address mHostAddress;
135         Service mServices[kMaxServicesNumber];
136     };
137
138     SrpClient mSrpClient;
139
140     static void OnSrpClientNotification(otError aError, const otSrpClientHostInfo * aHostInfo, const otSrpClientService * aServices,
141                                         const otSrpClientService * aRemovedServices, void * aContext);
142     static void OnSrpClientStateChange(const otSockAddr * aServerSockAddr, void * aContext);
143
144 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
145
146     static void OnJoinerComplete(otError aError, void * aContext);
147     void OnJoinerComplete(otError aError);
148
149     inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
150 };
151
152 // Instruct the compiler to instantiate the template only when explicitly told to do so.
153 extern template class GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>;
154
155 /**
156  * Returns the underlying OpenThread instance object.
157  */
158 template <class ImplClass>
159 inline otInstance * GenericThreadStackManagerImpl_OpenThread<ImplClass>::OTInstance() const
160 {
161     return mOTInst;
162 }
163
164 template <class ImplClass>
165 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStart(void)
166 {
167     // Do nothing by default.
168 }
169
170 template <class ImplClass>
171 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStop(void)
172 {
173     // Do nothing by default.
174 }
175
176 } // namespace Internal
177 } // namespace DeviceLayer
178 } // namespace chip