Apply Upstream code (2021-03-15)
[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 #include <lib/mdns/platform/Mdns.h>
36
37 namespace chip {
38 namespace DeviceLayer {
39
40 class ThreadStackManagerImpl;
41
42 namespace Internal {
43
44 class DeviceNetworkInfo;
45
46 /**
47  * Provides a generic implementation of ThreadStackManager features that works in conjunction
48  * with OpenThread.
49  *
50  * This class contains implementations of select features from the ThreadStackManager abstract
51  * interface that are suitable for use on devices that employ OpenThread.  It is intended to
52  * be inherited, directly or indirectly, by the ThreadStackManagerImpl class, which also appears
53  * as the template's ImplClass parameter.
54  *
55  * The class is designed to be independent of the choice of host OS (e.g. RTOS or posix) and
56  * network stack (e.g. LwIP or other IP stack).
57  */
58 template <class ImplClass>
59 class GenericThreadStackManagerImpl_OpenThread
60 {
61 public:
62     // ===== Platform-specific methods directly callable by the application.
63
64     otInstance * OTInstance() const;
65     static void OnOpenThreadStateChange(uint32_t flags, void * context);
66
67 protected:
68     // ===== Methods that implement the ThreadStackManager abstract interface.
69
70     void _ProcessThreadActivity(void);
71     bool _HaveRouteToAddress(const Inet::IPAddress & destAddr);
72     void _OnPlatformEvent(const ChipDeviceEvent * event);
73     bool _IsThreadEnabled(void);
74     CHIP_ERROR _SetThreadEnabled(bool val);
75
76     bool _IsThreadProvisioned(void);
77     bool _IsThreadAttached(void);
78     CHIP_ERROR _GetThreadProvision(DeviceNetworkInfo & netInfo, bool includeCredentials);
79     CHIP_ERROR _SetThreadProvision(const DeviceNetworkInfo & netInfo);
80     CHIP_ERROR _SetThreadProvision(const uint8_t * operationalDataset, size_t operationalDatasetLen);
81     void _ErasePersistentInfo(void);
82     ConnectivityManager::ThreadDeviceType _GetThreadDeviceType(void);
83     CHIP_ERROR _SetThreadDeviceType(ConnectivityManager::ThreadDeviceType deviceType);
84     void _GetThreadPollingConfig(ConnectivityManager::ThreadPollingConfig & pollingConfig);
85     CHIP_ERROR _SetThreadPollingConfig(const ConnectivityManager::ThreadPollingConfig & pollingConfig);
86     bool _HaveMeshConnectivity(void);
87     void _OnMessageLayerActivityChanged(bool messageLayerIsActive);
88     CHIP_ERROR _GetAndLogThreadStatsCounters(void);
89     CHIP_ERROR _GetAndLogThreadTopologyMinimal(void);
90     CHIP_ERROR _GetAndLogThreadTopologyFull(void);
91     CHIP_ERROR _GetPrimary802154MACAddress(uint8_t * buf);
92     CHIP_ERROR _GetFactoryAssignedEUI64(uint8_t (&buf)[8]);
93     CHIP_ERROR _GetExternalIPv6Address(chip::Inet::IPAddress & addr);
94     void _OnWoBLEAdvertisingStart(void);
95     void _OnWoBLEAdvertisingStop(void);
96
97 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
98     CHIP_ERROR _AddSrpService(const char * aInstanceName, const char * aName, uint16_t aPort, chip::Mdns::TextEntry * aTxtEntries,
99                               size_t aTxtEntiresSize, uint32_t aLeaseInterval, uint32_t aKeyLeaseInterval);
100     CHIP_ERROR _RemoveSrpService(const char * aInstanceName, const char * aName);
101     CHIP_ERROR _SetupSrpHost(const char * aHostName);
102 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
103
104     // ===== Members available to the implementation subclass.
105
106     CHIP_ERROR DoInit(otInstance * otInst);
107     bool IsThreadAttachedNoLock(void);
108     bool IsThreadInterfaceUpNoLock(void);
109     CHIP_ERROR AdjustPollingInterval(void);
110
111     CHIP_ERROR _JoinerStart(void);
112
113 private:
114     // ===== Private members for use by this class only.
115
116     otInstance * mOTInst;
117     ConnectivityManager::ThreadPollingConfig mPollingConfig;
118
119 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
120
121     struct SrpClient
122     {
123         static constexpr uint8_t kServiceId           = 0x5d;
124         static constexpr uint8_t kMaxServicesNumber   = 3;
125         static constexpr uint8_t kMaxInstanceNameSize = 64;
126         static constexpr uint8_t kMaxNameSize         = 16;
127         static constexpr uint8_t kMaxHostNameSize     = 32;
128         static constexpr uint8_t kMaxTxtEntriesNumber = 4;
129         static constexpr uint8_t kMaxTxtValueSize     = 255;
130         static constexpr uint8_t kMaxTxtKeySize       = 16;
131
132         struct Service
133         {
134             otSrpClientService mService;
135             char mInstanceName[kMaxInstanceNameSize];
136             char mName[kMaxNameSize];
137             otDnsTxtEntry mTxtEntries[kMaxTxtEntriesNumber];
138             uint8_t mTxtValueBuffers[kMaxTxtEntriesNumber][kMaxTxtValueSize];
139             char mTxtKeyBuffers[kMaxTxtEntriesNumber][kMaxTxtKeySize];
140         };
141
142         char mHostName[kMaxHostNameSize];
143         otIp6Address mHostAddress;
144         Service mServices[kMaxServicesNumber];
145     };
146
147     SrpClient mSrpClient;
148
149     static void OnSrpClientNotification(otError aError, const otSrpClientHostInfo * aHostInfo, const otSrpClientService * aServices,
150                                         const otSrpClientService * aRemovedServices, void * aContext);
151     static void OnSrpClientStateChange(const otSockAddr * aServerSockAddr, void * aContext);
152
153 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
154
155     static void OnJoinerComplete(otError aError, void * aContext);
156     void OnJoinerComplete(otError aError);
157
158     inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
159 };
160
161 // Instruct the compiler to instantiate the template only when explicitly told to do so.
162 extern template class GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>;
163
164 /**
165  * Returns the underlying OpenThread instance object.
166  */
167 template <class ImplClass>
168 inline otInstance * GenericThreadStackManagerImpl_OpenThread<ImplClass>::OTInstance() const
169 {
170     return mOTInst;
171 }
172
173 template <class ImplClass>
174 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStart(void)
175 {
176     // Do nothing by default.
177 }
178
179 template <class ImplClass>
180 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStop(void)
181 {
182     // Do nothing by default.
183 }
184
185 } // namespace Internal
186 } // namespace DeviceLayer
187 } // namespace chip