1 /*
2 * Copyright (c) 2016-2017, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the OpenThread Instance class.
32 */
33
34 #include "instance.hpp"
35
36 #include <openthread/platform/misc.h>
37
38 #include "common/new.hpp"
39 #include "radio/trel_link.hpp"
40 #include "utils/heap.hpp"
41
42 namespace ot {
43
44 #if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
45
46 // Define the raw storage used for OpenThread instance (in single-instance case).
47 OT_DEFINE_ALIGNED_VAR(gInstanceRaw, sizeof(Instance), uint64_t);
48
49 #endif
50
51 #if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
52
53 #define INSTANCE_SIZE_ALIGNED OT_ALIGNED_VAR_SIZE(sizeof(ot::Instance), uint64_t)
54 #define MULTI_INSTANCE_SIZE (OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_NUM * INSTANCE_SIZE_ALIGNED)
55
56 // Define the raw storage used for OpenThread instance (in multi-instance case).
57 static uint64_t gMultiInstanceRaw[MULTI_INSTANCE_SIZE];
58
59 #endif
60
61 #if OPENTHREAD_MTD || OPENTHREAD_FTD
62 #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
63 OT_DEFINE_ALIGNED_VAR(sHeapRaw, sizeof(Utils::Heap), uint64_t);
64 Utils::Heap *Instance::sHeap{nullptr};
65 #endif
66 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
67 bool Instance::sDnsNameCompressionEnabled = true;
68 #endif
69 #endif
70
71 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
72 LogLevel Instance::sLogLevel = static_cast<LogLevel>(OPENTHREAD_CONFIG_LOG_LEVEL_INIT);
73 #endif
74
Instance(void)75 Instance::Instance(void)
76 : mTimerMilliScheduler(*this)
77 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
78 , mTimerMicroScheduler(*this)
79 #endif
80 , mRadio(*this)
81 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
82 , mUptime(*this)
83 #endif
84 #if OPENTHREAD_MTD || OPENTHREAD_FTD
85 , mNotifier(*this)
86 , mTimeTicker(*this)
87 , mSettings(*this)
88 , mSettingsDriver(*this)
89 , mMessagePool(*this)
90 #if OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
91 // DNS-SD (mDNS) platform is initialized early to
92 // allow other modules to use it.
93 , mDnssd(*this)
94 #endif
95 , mIp6(*this)
96 , mThreadNetif(*this)
97 , mTmfAgent(*this)
98 #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE
99 , mDhcp6Client(*this)
100 #endif
101 #if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE
102 , mDhcp6Server(*this)
103 #endif
104 #if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
105 , mNeighborDiscoveryAgent(*this)
106 #endif
107 #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
108 , mSlaac(*this)
109 #endif
110 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
111 , mDnsClient(*this)
112 #endif
113 #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
114 , mSrpClient(*this)
115 #endif
116 #if OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_ENABLE
117 , mSrpClientBuffers(*this)
118 #endif
119 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
120 , mDnssdServer(*this)
121 #endif
122 #if OPENTHREAD_CONFIG_DNS_DSO_ENABLE
123 , mDnsDso(*this)
124 #endif
125 #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE
126 , mSntpClient(*this)
127 #endif
128 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
129 , mBackboneRouterLocal(*this)
130 #endif
131 , mActiveDataset(*this)
132 , mPendingDataset(*this)
133 , mExtendedPanIdManager(*this)
134 , mNetworkNameManager(*this)
135 , mIp6Filter(*this)
136 , mKeyManager(*this)
137 , mLowpan(*this)
138 , mMac(*this)
139 , mMeshForwarder(*this)
140 , mMleRouter(*this)
141 , mDiscoverScanner(*this)
142 , mAddressResolver(*this)
143 #if OPENTHREAD_CONFIG_MULTI_RADIO
144 , mRadioSelector(*this)
145 #endif
146 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
147 , mNetworkDataLocal(*this)
148 #endif
149 , mNetworkDataLeader(*this)
150 #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
151 , mNetworkDataNotifier(*this)
152 #endif
153 #if OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE
154 , mNetworkDataPublisher(*this)
155 #endif
156 , mNetworkDataServiceManager(*this)
157 , mNetworkDiagnosticServer(*this)
158 #if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE
159 , mNetworkDiagnosticClient(*this)
160 #endif
161 #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
162 , mBorderAgent(*this)
163 #endif
164 #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
165 , mCommissioner(*this)
166 #endif
167 #if OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE
168 , mTmfSecureAgent(*this)
169 #endif
170 #if OPENTHREAD_CONFIG_JOINER_ENABLE
171 , mJoiner(*this)
172 #endif
173 #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE
174 , mJamDetector(*this)
175 #endif
176 #if OPENTHREAD_FTD
177 , mJoinerRouter(*this)
178 , mLeader(*this)
179 #endif
180 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
181 , mBackboneRouterLeader(*this)
182 #endif
183 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
184 , mBackboneRouterManager(*this)
185 #endif
186 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
187 , mMlrManager(*this)
188 #endif
189
190 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
191 , mDuaManager(*this)
192 #endif
193 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
194 , mSrpServer(*this)
195 #if OPENTHREAD_CONFIG_SRP_SERVER_ADVERTISING_PROXY_ENABLE
196 , mSrpAdvertisingProxy(*this)
197 #endif
198 #endif
199 #if OPENTHREAD_FTD
200 , mChildSupervisor(*this)
201 #endif
202 , mSupervisionListener(*this)
203 , mAnnounceBegin(*this)
204 , mPanIdQuery(*this)
205 , mEnergyScan(*this)
206 #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE
207 , mAnycastLocator(*this)
208 #endif
209 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
210 , mTimeSync(*this)
211 #endif
212 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
213 , mInitiator(*this)
214 #endif
215 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
216 , mSubject(*this)
217 #endif
218 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
219 , mApplicationCoap(*this)
220 #endif
221 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
222 , mApplicationCoapSecure(*this, /* aLayerTwoSecurity */ true)
223 #endif
224 #if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE
225 , mApplicationBleSecure(*this)
226 #endif
227 #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE
228 , mPingSender(*this)
229 #endif
230 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
231 , mChannelMonitor(*this)
232 #endif
233 #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && \
234 (OPENTHREAD_FTD || \
235 (OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE && OPENTHREAD_CONFIG_CHANNEL_MANAGER_CSL_CHANNEL_SELECT_ENABLE))
236 , mChannelManager(*this)
237 #endif
238 #if OPENTHREAD_CONFIG_MESH_DIAG_ENABLE && OPENTHREAD_FTD
239 , mMeshDiag(*this)
240 #endif
241 #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE
242 , mHistoryTracker(*this)
243 #endif
244 #if OPENTHREAD_CONFIG_LINK_METRICS_MANAGER_ENABLE
245 , mLinkMetricsManager(*this)
246 #endif
247 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
248 , mDatasetUpdater(*this)
249 #endif
250 #if OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
251 , mAnnounceSender(*this)
252 #endif
253 #if OPENTHREAD_CONFIG_OTNS_ENABLE
254 , mOtns(*this)
255 #endif
256 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
257 , mRoutingManager(*this)
258 #endif
259 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
260 , mNat64Translator(*this)
261 #endif
262 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
263 #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE
264 , mLinkRaw(*this)
265 #endif
266 #if OPENTHREAD_ENABLE_VENDOR_EXTENSION
267 , mExtension(Extension::ExtensionBase::Init(*this))
268 #endif
269 #if OPENTHREAD_CONFIG_DIAG_ENABLE
270 , mDiags(*this)
271 #endif
272 #if OPENTHREAD_CONFIG_POWER_CALIBRATION_ENABLE && OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
273 , mPowerCalibration(*this)
274 #endif
275 , mIsInitialized(false)
276 , mId(Random::NonCrypto::GetUint32())
277 {
278 }
279
280 #if (OPENTHREAD_MTD || OPENTHREAD_FTD) && !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
GetHeap(void)281 Utils::Heap &Instance::GetHeap(void)
282 {
283 if (nullptr == sHeap)
284 {
285 sHeap = new (&sHeapRaw) Utils::Heap();
286 }
287
288 return *sHeap;
289 }
290 #endif
291
292 #if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
293
InitSingle(void)294 Instance &Instance::InitSingle(void)
295 {
296 Instance *instance = &Get();
297
298 VerifyOrExit(!instance->mIsInitialized);
299
300 instance = new (&gInstanceRaw) Instance();
301
302 instance->AfterInit();
303
304 exit:
305 return *instance;
306 }
307
Get(void)308 Instance &Instance::Get(void)
309 {
310 void *instance = &gInstanceRaw;
311
312 return *static_cast<Instance *>(instance);
313 }
314
315 #else // #if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
316 #if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
317
InitMultiple(uint8_t aIdx)318 Instance *Instance::InitMultiple(uint8_t aIdx)
319 {
320 size_t bufferSize;
321 uint64_t *instanceBuffer = gMultiInstanceRaw + aIdx * INSTANCE_SIZE_ALIGNED;
322 Instance *instance = reinterpret_cast<Instance *>(instanceBuffer);
323
324 VerifyOrExit(aIdx < OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_NUM);
325 VerifyOrExit(!instance->mIsInitialized);
326
327 bufferSize = (&gMultiInstanceRaw[MULTI_INSTANCE_SIZE] - instanceBuffer) * sizeof(uint64_t);
328 instance = Instance::Init(instanceBuffer, &bufferSize);
329
330 exit:
331 return instance;
332 }
333
334 #endif // #if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE
335
Init(void * aBuffer,size_t * aBufferSize)336 Instance *Instance::Init(void *aBuffer, size_t *aBufferSize)
337 {
338 Instance *instance = nullptr;
339
340 VerifyOrExit(aBufferSize != nullptr);
341
342 // Make sure the input buffer is big enough
343 VerifyOrExit(sizeof(Instance) <= *aBufferSize, *aBufferSize = sizeof(Instance));
344
345 VerifyOrExit(aBuffer != nullptr);
346
347 instance = new (aBuffer) Instance();
348
349 instance->AfterInit();
350
351 exit:
352 return instance;
353 }
354
355 #endif // OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
356
Reset(void)357 void Instance::Reset(void) { otPlatReset(this); }
358
359 #if OPENTHREAD_CONFIG_PLATFORM_BOOTLOADER_MODE_ENABLE
ResetToBootloader(void)360 Error Instance::ResetToBootloader(void) { return otPlatResetToBootloader(this); }
361 #endif
362
363 #if OPENTHREAD_RADIO
ResetRadioStack(void)364 void Instance::ResetRadioStack(void)
365 {
366 mRadio.Init();
367 mLinkRaw.Init();
368 }
369 #endif
370
AfterInit(void)371 void Instance::AfterInit(void)
372 {
373 mIsInitialized = true;
374 #if OPENTHREAD_MTD || OPENTHREAD_FTD
375
376 // Restore datasets and network information
377
378 Get<Settings>().Init();
379 Get<Mle::MleRouter>().Restore();
380
381 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
382 Get<Trel::Link>().AfterInit();
383 #endif
384
385 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
386
387 #if OPENTHREAD_ENABLE_VENDOR_EXTENSION
388 Get<Extension::ExtensionBase>().SignalInstanceInit();
389 #endif
390 }
391
Finalize(void)392 void Instance::Finalize(void)
393 {
394 VerifyOrExit(mIsInitialized);
395
396 mIsInitialized = false;
397
398 #if OPENTHREAD_MTD || OPENTHREAD_FTD
399 IgnoreError(otThreadSetEnabled(this, false));
400 IgnoreError(otIp6SetEnabled(this, false));
401 IgnoreError(otLinkSetEnabled(this, false));
402
403 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
404 Get<KeyManager>().DestroyTemporaryKeys();
405 #endif
406
407 Get<Settings>().Deinit();
408 #endif
409
410 IgnoreError(Get<Mac::SubMac>().Disable());
411
412 #if !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
413
414 /**
415 * Object was created on buffer, so instead of deleting
416 * the object we call destructor explicitly.
417 */
418 this->~Instance();
419
420 #endif // !OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
421
422 exit:
423 return;
424 }
425
426 #if OPENTHREAD_MTD || OPENTHREAD_FTD
427
FactoryReset(void)428 void Instance::FactoryReset(void)
429 {
430 Get<Settings>().Wipe();
431 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
432 Get<KeyManager>().DestroyTemporaryKeys();
433 Get<KeyManager>().DestroyPersistentKeys();
434 #endif
435 otPlatReset(this);
436 }
437
ErasePersistentInfo(void)438 Error Instance::ErasePersistentInfo(void)
439 {
440 Error error = kErrorNone;
441
442 VerifyOrExit(Get<Mle::MleRouter>().IsDisabled(), error = kErrorInvalidState);
443 Get<Settings>().Wipe();
444 #if OPENTHREAD_CONFIG_PLATFORM_KEY_REFERENCES_ENABLE
445 Get<KeyManager>().DestroyTemporaryKeys();
446 Get<KeyManager>().DestroyPersistentKeys();
447 #endif
448
449 exit:
450 return error;
451 }
452
GetBufferInfo(BufferInfo & aInfo)453 void Instance::GetBufferInfo(BufferInfo &aInfo)
454 {
455 aInfo.Clear();
456
457 aInfo.mTotalBuffers = Get<MessagePool>().GetTotalBufferCount();
458 aInfo.mFreeBuffers = Get<MessagePool>().GetFreeBufferCount();
459 aInfo.mMaxUsedBuffers = Get<MessagePool>().GetMaxUsedBufferCount();
460
461 Get<MeshForwarder>().GetSendQueue().GetInfo(aInfo.m6loSendQueue);
462 Get<MeshForwarder>().GetReassemblyQueue().GetInfo(aInfo.m6loReassemblyQueue);
463 Get<Ip6::Ip6>().GetSendQueue().GetInfo(aInfo.mIp6Queue);
464
465 #if OPENTHREAD_FTD
466 Get<Ip6::Mpl>().GetBufferedMessageSet().GetInfo(aInfo.mMplQueue);
467 #endif
468
469 Get<Mle::MleRouter>().GetMessageQueue().GetInfo(aInfo.mMleQueue);
470
471 Get<Tmf::Agent>().GetRequestMessages().GetInfo(aInfo.mCoapQueue);
472 Get<Tmf::Agent>().GetCachedResponses().GetInfo(aInfo.mCoapQueue);
473
474 #if OPENTHREAD_CONFIG_SECURE_TRANSPORT_ENABLE
475 Get<Tmf::SecureAgent>().GetRequestMessages().GetInfo(aInfo.mCoapSecureQueue);
476 Get<Tmf::SecureAgent>().GetCachedResponses().GetInfo(aInfo.mCoapSecureQueue);
477 #endif
478
479 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
480 GetApplicationCoap().GetRequestMessages().GetInfo(aInfo.mApplicationCoapQueue);
481 GetApplicationCoap().GetCachedResponses().GetInfo(aInfo.mApplicationCoapQueue);
482 #endif
483 }
484
ResetBufferInfo(void)485 void Instance::ResetBufferInfo(void) { Get<MessagePool>().ResetMaxUsedBufferCount(); }
486
487 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
488
489 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
490
SetLogLevel(LogLevel aLogLevel)491 void Instance::SetLogLevel(LogLevel aLogLevel)
492 {
493 if (aLogLevel != sLogLevel)
494 {
495 sLogLevel = aLogLevel;
496 otPlatLogHandleLevelChanged(sLogLevel);
497 }
498 }
499
otPlatLogHandleLevelChanged(otLogLevel aLogLevel)500 extern "C" OT_TOOL_WEAK void otPlatLogHandleLevelChanged(otLogLevel aLogLevel) { OT_UNUSED_VARIABLE(aLogLevel); }
501
502 #endif
503
504 } // namespace ot
505