3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2019 Nest Labs, Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 * Provides a generic implementation of ThreadStackManager features
23 * for use on platforms that use OpenThread.
28 #include <openthread/instance.h>
29 #include <openthread/netdata.h>
31 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
32 #include <openthread/srp_client.h>
35 #include <lib/mdns/platform/Mdns.h>
38 namespace DeviceLayer {
40 class ThreadStackManagerImpl;
44 class DeviceNetworkInfo;
47 * Provides a generic implementation of ThreadStackManager features that works in conjunction
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.
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).
58 template <class ImplClass>
59 class GenericThreadStackManagerImpl_OpenThread
62 // ===== Platform-specific methods directly callable by the application.
64 otInstance * OTInstance() const;
65 static void OnOpenThreadStateChange(uint32_t flags, void * context);
68 // ===== Methods that implement the ThreadStackManager abstract interface.
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);
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);
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
104 // ===== Members available to the implementation subclass.
106 CHIP_ERROR DoInit(otInstance * otInst);
107 bool IsThreadAttachedNoLock(void);
108 bool IsThreadInterfaceUpNoLock(void);
109 CHIP_ERROR AdjustPollingInterval(void);
111 CHIP_ERROR _JoinerStart(void);
114 // ===== Private members for use by this class only.
116 otInstance * mOTInst;
117 ConnectivityManager::ThreadPollingConfig mPollingConfig;
119 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
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;
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];
142 char mHostName[kMaxHostNameSize];
143 otIp6Address mHostAddress;
144 Service mServices[kMaxServicesNumber];
147 SrpClient mSrpClient;
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);
153 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
155 static void OnJoinerComplete(otError aError, void * aContext);
156 void OnJoinerComplete(otError aError);
158 inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
161 // Instruct the compiler to instantiate the template only when explicitly told to do so.
162 extern template class GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>;
165 * Returns the underlying OpenThread instance object.
167 template <class ImplClass>
168 inline otInstance * GenericThreadStackManagerImpl_OpenThread<ImplClass>::OTInstance() const
173 template <class ImplClass>
174 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStart(void)
176 // Do nothing by default.
179 template <class ImplClass>
180 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStop(void)
182 // Do nothing by default.
185 } // namespace Internal
186 } // namespace DeviceLayer