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>
36 namespace DeviceLayer {
38 class ThreadStackManagerImpl;
42 class DeviceNetworkInfo;
45 * Provides a generic implementation of ThreadStackManager features that works in conjunction
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.
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).
56 template <class ImplClass>
57 class GenericThreadStackManagerImpl_OpenThread
60 // ===== Platform-specific methods directly callable by the application.
62 otInstance * OTInstance() const;
63 static void OnOpenThreadStateChange(uint32_t flags, void * context);
66 // ===== Methods that implement the ThreadStackManager abstract interface.
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);
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);
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
101 // ===== Members available to the implementation subclass.
103 CHIP_ERROR DoInit(otInstance * otInst);
104 bool IsThreadAttachedNoLock(void);
105 bool IsThreadInterfaceUpNoLock(void);
106 CHIP_ERROR AdjustPollingInterval(void);
108 CHIP_ERROR _JoinerStart(void);
111 // ===== Private members for use by this class only.
113 otInstance * mOTInst;
114 ConnectivityManager::ThreadPollingConfig mPollingConfig;
116 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
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;
128 otSrpClientService mService;
129 char mInstanceName[kMaxInstanceNameSize];
130 char mName[kMaxNameSize];
133 char mHostName[kMaxHostNameSize];
134 otIp6Address mHostAddress;
135 Service mServices[kMaxServicesNumber];
138 SrpClient mSrpClient;
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);
144 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD_SRP_CLIENT
146 static void OnJoinerComplete(otError aError, void * aContext);
147 void OnJoinerComplete(otError aError);
149 inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }
152 // Instruct the compiler to instantiate the template only when explicitly told to do so.
153 extern template class GenericThreadStackManagerImpl_OpenThread<ThreadStackManagerImpl>;
156 * Returns the underlying OpenThread instance object.
158 template <class ImplClass>
159 inline otInstance * GenericThreadStackManagerImpl_OpenThread<ImplClass>::OTInstance() const
164 template <class ImplClass>
165 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStart(void)
167 // Do nothing by default.
170 template <class ImplClass>
171 inline void GenericThreadStackManagerImpl_OpenThread<ImplClass>::_OnWoBLEAdvertisingStop(void)
173 // Do nothing by default.
176 } // namespace Internal
177 } // namespace DeviceLayer