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 defines OpenThread instance class.
32  */
33 
34 #ifndef INSTANCE_HPP_
35 #define INSTANCE_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <stdbool.h>
40 #include <stdint.h>
41 
42 #include <openthread/heap.h>
43 #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
44 #include <openthread/platform/memory.h>
45 #endif
46 
47 #include "common/array.hpp"
48 #include "common/as_core_type.hpp"
49 #include "common/error.hpp"
50 #include "common/extension.hpp"
51 #include "common/log.hpp"
52 #include "common/message.hpp"
53 #include "common/non_copyable.hpp"
54 #include "common/random.hpp"
55 #include "common/tasklet.hpp"
56 #include "common/time_ticker.hpp"
57 #include "common/timer.hpp"
58 #include "common/uptime.hpp"
59 #include "diags/factory_diags.hpp"
60 #include "mac/link_raw.hpp"
61 #include "radio/radio.hpp"
62 #include "utils/otns.hpp"
63 #include "utils/power_calibration.hpp"
64 
65 #if OPENTHREAD_FTD || OPENTHREAD_MTD
66 #include "backbone_router/backbone_tmf.hpp"
67 #include "backbone_router/bbr_leader.hpp"
68 #include "backbone_router/bbr_local.hpp"
69 #include "backbone_router/bbr_manager.hpp"
70 #include "border_router/routing_manager.hpp"
71 #include "coap/coap_secure.hpp"
72 #include "common/code_utils.hpp"
73 #include "common/notifier.hpp"
74 #include "common/settings.hpp"
75 #include "crypto/mbedtls.hpp"
76 #include "mac/mac.hpp"
77 #include "meshcop/border_agent.hpp"
78 #include "meshcop/commissioner.hpp"
79 #include "meshcop/dataset_manager.hpp"
80 #include "meshcop/dataset_updater.hpp"
81 #include "meshcop/extended_panid.hpp"
82 #include "meshcop/joiner.hpp"
83 #include "meshcop/joiner_router.hpp"
84 #include "meshcop/meshcop_leader.hpp"
85 #include "meshcop/network_name.hpp"
86 #include "net/dhcp6.hpp"
87 #include "net/dhcp6_client.hpp"
88 #include "net/dhcp6_server.hpp"
89 #include "net/dns_client.hpp"
90 #include "net/dns_dso.hpp"
91 #include "net/dnssd_server.hpp"
92 #include "net/ip6.hpp"
93 #include "net/ip6_filter.hpp"
94 #include "net/nat64_translator.hpp"
95 #include "net/nd_agent.hpp"
96 #include "net/netif.hpp"
97 #include "net/sntp_client.hpp"
98 #include "net/srp_client.hpp"
99 #include "net/srp_server.hpp"
100 #include "thread/address_resolver.hpp"
101 #include "thread/announce_begin_server.hpp"
102 #include "thread/announce_sender.hpp"
103 #include "thread/anycast_locator.hpp"
104 #include "thread/child_supervision.hpp"
105 #include "thread/discover_scanner.hpp"
106 #include "thread/dua_manager.hpp"
107 #include "thread/energy_scan_server.hpp"
108 #include "thread/key_manager.hpp"
109 #include "thread/link_metrics.hpp"
110 #include "thread/link_quality.hpp"
111 #include "thread/mesh_forwarder.hpp"
112 #include "thread/mle.hpp"
113 #include "thread/mle_router.hpp"
114 #include "thread/mlr_manager.hpp"
115 #include "thread/network_data_local.hpp"
116 #include "thread/network_data_notifier.hpp"
117 #include "thread/network_data_publisher.hpp"
118 #include "thread/network_data_service.hpp"
119 #include "thread/network_diagnostic.hpp"
120 #include "thread/panid_query_server.hpp"
121 #include "thread/radio_selector.hpp"
122 #include "thread/thread_netif.hpp"
123 #include "thread/time_sync_service.hpp"
124 #include "thread/tmf.hpp"
125 #include "utils/channel_manager.hpp"
126 #include "utils/channel_monitor.hpp"
127 #include "utils/heap.hpp"
128 #include "utils/history_tracker.hpp"
129 #include "utils/jam_detector.hpp"
130 #include "utils/link_metrics_manager.hpp"
131 #include "utils/mesh_diag.hpp"
132 #include "utils/ping_sender.hpp"
133 #include "utils/slaac_address.hpp"
134 #include "utils/srp_client_buffers.hpp"
135 #endif // OPENTHREAD_FTD || OPENTHREAD_MTD
136 
137 /**
138  * @addtogroup core-instance
139  *
140  * @brief
141  *   This module includes definitions for OpenThread instance.
142  *
143  * @{
144  *
145  */
146 
147 /**
148  * Represents an opaque (and empty) type corresponding to an OpenThread instance object.
149  *
150  */
151 typedef struct otInstance
152 {
153 } otInstance;
154 
155 namespace ot {
156 
157 /**
158  * Represents an OpenThread instance.
159  *
160  * Contains all the components used by OpenThread.
161  *
162  */
163 class Instance : public otInstance, private NonCopyable
164 {
165 public:
166     /**
167      * Represents the message buffer information (number of messages/buffers in all OT stack message queues).
168      *
169      */
170     class BufferInfo : public otBufferInfo, public Clearable<BufferInfo>
171     {
172     };
173 
174 #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
175     /**
176       * Initializes the OpenThread instance.
177       *
178       * Must be called before any other calls on OpenThread instance.
179       *
180       * @param[in]     aBuffer      The buffer for OpenThread to use for allocating the Instance.
181       * @param[in,out] aBufferSize  On input, the size of `aBuffer`. On output, if not enough space for `Instance`, the
182                                     number of bytes required for `Instance`.
183       *
184       * @returns  A pointer to the new OpenThread instance.
185       *
186       */
187     static Instance *Init(void *aBuffer, size_t *aBufferSize);
188 
189 #else // OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
190 
191     /**
192      * Initializes the single OpenThread instance.
193      *
194      * Initializes OpenThread and prepares it for subsequent OpenThread API calls. This function must be
195      * called before any other calls to OpenThread.
196      *
197      * @returns A reference to the single OpenThread instance.
198      *
199      */
200     static Instance &InitSingle(void);
201 
202     /**
203      * Returns a reference to the single OpenThread instance.
204      *
205      * @returns A reference to the single OpenThread instance.
206      *
207      */
208     static Instance &Get(void);
209 #endif
210 
211     /**
212      * Gets the instance identifier.
213      *
214      * The instance identifier is set to a random value when the instance is constructed, and then its value will not
215      * change after initialization.
216      *
217      * @returns The instance identifier.
218      *
219      */
GetId(void) const220     uint32_t GetId(void) const { return mId; }
221 
222     /**
223      * Indicates whether or not the instance is valid/initialized and not yet finalized.
224      *
225      * @returns TRUE if the instance is valid/initialized, FALSE otherwise.
226      *
227      */
IsInitialized(void) const228     bool IsInitialized(void) const { return mIsInitialized; }
229 
230     /**
231      * Triggers a platform reset.
232      *
233      * The reset process ensures that all the OpenThread state/info (stored in volatile memory) is erased. Note that
234      * this method does not erase any persistent state/info saved in non-volatile memory.
235      *
236      */
237     void Reset(void);
238 
239 #if OPENTHREAD_RADIO
240     /**
241      * Resets the internal states of the radio.
242      *
243      */
244     void ResetRadioStack(void);
245 #endif
246 
247     /**
248      * Returns the active log level.
249      *
250      * @returns The log level.
251      *
252      */
GetLogLevel(void)253     static LogLevel GetLogLevel(void)
254 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
255     {
256         return sLogLevel;
257     }
258 #else
259     {
260         return static_cast<LogLevel>(OPENTHREAD_CONFIG_LOG_LEVEL);
261     }
262 #endif
263 
264 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
265     /**
266      * Sets the log level.
267      *
268      * @param[in] aLogLevel  A log level.
269      *
270      */
271     static void SetLogLevel(LogLevel aLogLevel);
272 #endif
273 
274     /**
275      * Finalizes the OpenThread instance.
276      *
277      * Should be called when OpenThread instance is no longer in use.
278      *
279      */
280     void Finalize(void);
281 
282 #if OPENTHREAD_MTD || OPENTHREAD_FTD
283     /**
284      * Deletes all the settings stored in non-volatile memory, and then triggers a platform reset.
285      *
286      */
287     void FactoryReset(void);
288 
289     /**
290      * Erases all the OpenThread persistent info (network settings) stored in non-volatile memory.
291      *
292      * Erase is successful/allowed only if the device is in `disabled` state/role.
293      *
294      * @retval kErrorNone          All persistent info/state was erased successfully.
295      * @retval kErrorInvalidState  Device is not in `disabled` state/role.
296      *
297      */
298     Error ErasePersistentInfo(void);
299 
300 #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
301     /**
302      * Returns a reference to the Heap object.
303      *
304      * @returns A reference to the Heap object.
305      *
306      */
307     static Utils::Heap &GetHeap(void);
308 #endif
309 
310 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
311     /**
312      * Returns a reference to application COAP object.
313      *
314      * @returns A reference to the application COAP object.
315      *
316      */
GetApplicationCoap(void)317     Coap::Coap &GetApplicationCoap(void) { return mApplicationCoap; }
318 #endif
319 
320 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
321     /**
322      * Returns a reference to application COAP Secure object.
323      *
324      * @returns A reference to the application COAP Secure object.
325      *
326      */
GetApplicationCoapSecure(void)327     Coap::CoapSecure &GetApplicationCoapSecure(void) { return mApplicationCoapSecure; }
328 #endif
329 
330 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
331     /**
332      * Enables/disables the "DNS name compressions" mode.
333      *
334      * By default DNS name compression is enabled. When disabled, DNS names are appended as full and never compressed.
335      * This is applicable to OpenThread's DNS and SRP client/server modules.
336      *
337      * This is intended for testing only and available under a `REFERENCE_DEVICE` config.
338      *
339      * @param[in] aEnabled   TRUE to enable the "DNS name compression" mode, FALSE to disable.
340      *
341      */
SetDnsNameCompressionEnabled(bool aEnabled)342     static void SetDnsNameCompressionEnabled(bool aEnabled) { sDnsNameCompressionEnabled = aEnabled; }
343 
344     /**
345      * Indicates whether the "DNS name compression" mode is enabled or not.
346      *
347      * @returns TRUE if the "DNS name compressions" mode is enabled, FALSE otherwise.
348      *
349      */
IsDnsNameCompressionEnabled(void)350     static bool IsDnsNameCompressionEnabled(void) { return sDnsNameCompressionEnabled; }
351 #endif
352 
353     /**
354      * Retrieves the the Message Buffer information.
355      *
356      * @param[out]  aInfo  A `BufferInfo` where information is written.
357      *
358      */
359     void GetBufferInfo(BufferInfo &aInfo);
360 
361     /**
362      * Resets the Message Buffer information counter tracking maximum number buffers in use at the same
363      * time.
364      *
365      * Resets `mMaxUsedBuffers` in `BufferInfo`.
366      *
367      */
368     void ResetBufferInfo(void);
369 
370 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
371 
372     /**
373      * Returns a reference to a given `Type` object belonging to the OpenThread instance.
374      *
375      * For example, `Get<MeshForwarder>()` returns a reference to the `MeshForwarder` object of the instance.
376      *
377      * Note that any `Type` for which the `Get<Type>` is defined MUST be uniquely accessible from the OpenThread
378      * `Instance` through the member variable property hierarchy.
379      *
380      * Specializations of the `Get<Type>()` method are defined in this file after the `Instance` class definition.
381      *
382      * @returns A reference to the `Type` object of the instance.
383      *
384      */
385     template <typename Type> inline Type &Get(void);
386 
387 private:
388     Instance(void);
389     void AfterInit(void);
390 
391     // Order of variables (their initialization in `Instance`)
392     // is important.
393     //
394     // Tasklet and Timer Schedulers are first to ensure other
395     // objects/classes can use them from their constructors.
396 
397     Tasklet::Scheduler    mTaskletScheduler;
398     TimerMilli::Scheduler mTimerMilliScheduler;
399 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
400     TimerMicro::Scheduler mTimerMicroScheduler;
401 #endif
402 
403 #if OPENTHREAD_MTD || OPENTHREAD_FTD
404     // Random::Manager is initialized before other objects. Note that it
405     // requires MbedTls which itself may use Heap.
406 #if !OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
407     static Utils::Heap *sHeap;
408 #endif
409     Crypto::MbedTls mMbedTls;
410 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
411 
412     Random::Manager mRandomManager;
413 
414     // Radio is initialized before other member variables
415     // (particularly, SubMac and Mac) to allow them to use its methods
416     // from their constructor.
417     Radio mRadio;
418 
419 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
420     Uptime mUptime;
421 #endif
422 
423 #if OPENTHREAD_MTD || OPENTHREAD_FTD
424     // Notifier, TimeTicker, Settings, and MessagePool are initialized
425     // before other member variables since other classes/objects from
426     // their constructor may use them.
427     Notifier       mNotifier;
428     TimeTicker     mTimeTicker;
429     Settings       mSettings;
430     SettingsDriver mSettingsDriver;
431     MessagePool    mMessagePool;
432 
433     Ip6::Ip6    mIp6;
434     ThreadNetif mThreadNetif;
435     Tmf::Agent  mTmfAgent;
436 
437 #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE
438     Dhcp6::Client mDhcp6Client;
439 #endif
440 
441 #if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE
442     Dhcp6::Server mDhcp6Server;
443 #endif
444 
445 #if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
446     NeighborDiscovery::Agent mNeighborDiscoveryAgent;
447 #endif
448 
449 #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
450     Utils::Slaac mSlaac;
451 #endif
452 
453 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
454     Dns::Client mDnsClient;
455 #endif
456 
457 #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
458     Srp::Client mSrpClient;
459 #endif
460 
461 #if OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_ENABLE
462     Utils::SrpClientBuffers mSrpClientBuffers;
463 #endif
464 
465 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
466     Dns::ServiceDiscovery::Server mDnssdServer;
467 #endif
468 
469 #if OPENTHREAD_CONFIG_DNS_DSO_ENABLE
470     Dns::Dso mDnsDso;
471 #endif
472 
473 #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE
474     Sntp::Client mSntpClient;
475 #endif
476 
477     MeshCoP::ActiveDatasetManager  mActiveDataset;
478     MeshCoP::PendingDatasetManager mPendingDataset;
479     MeshCoP::ExtendedPanIdManager  mExtendedPanIdManager;
480     MeshCoP::NetworkNameManager    mNetworkNameManager;
481     Ip6::Filter                    mIp6Filter;
482     KeyManager                     mKeyManager;
483     Lowpan::Lowpan                 mLowpan;
484     Mac::Mac                       mMac;
485     MeshForwarder                  mMeshForwarder;
486     Mle::MleRouter                 mMleRouter;
487     Mle::DiscoverScanner           mDiscoverScanner;
488     AddressResolver                mAddressResolver;
489 
490 #if OPENTHREAD_CONFIG_MULTI_RADIO
491     RadioSelector mRadioSelector;
492 #endif
493 
494 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
495     NetworkData::Local mNetworkDataLocal;
496 #endif
497 
498     NetworkData::Leader mNetworkDataLeader;
499 
500 #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
501     NetworkData::Notifier mNetworkDataNotifier;
502 #endif
503 
504 #if OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE
505     NetworkData::Publisher mNetworkDataPublisher;
506 #endif
507 
508     NetworkData::Service::Manager mNetworkDataServiceManager;
509 
510     NetworkDiagnostic::Server mNetworkDiagnosticServer;
511 #if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE
512     NetworkDiagnostic::Client mNetworkDiagnosticClient;
513 #endif
514 
515 #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
516     MeshCoP::BorderAgent mBorderAgent;
517 #endif
518 
519 #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
520     MeshCoP::Commissioner mCommissioner;
521 #endif
522 
523 #if OPENTHREAD_CONFIG_DTLS_ENABLE
524     Tmf::SecureAgent mTmfSecureAgent;
525 #endif
526 
527 #if OPENTHREAD_CONFIG_JOINER_ENABLE
528     MeshCoP::Joiner mJoiner;
529 #endif
530 
531 #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE
532     Utils::JamDetector mJamDetector;
533 #endif
534 
535 #if OPENTHREAD_FTD
536     MeshCoP::JoinerRouter mJoinerRouter;
537     MeshCoP::Leader       mLeader;
538 #endif
539 
540 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
541     BackboneRouter::Leader mBackboneRouterLeader;
542 #endif
543 
544 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
545     BackboneRouter::Local   mBackboneRouterLocal;
546     BackboneRouter::Manager mBackboneRouterManager;
547 #endif
548 
549 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
550     MlrManager mMlrManager;
551 #endif
552 
553 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
554     DuaManager mDuaManager;
555 #endif
556 
557 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
558     Srp::Server mSrpServer;
559 #endif
560 
561 #if OPENTHREAD_FTD
562     ChildSupervisor mChildSupervisor;
563 #endif
564     SupervisionListener mSupervisionListener;
565 
566     AnnounceBeginServer mAnnounceBegin;
567     PanIdQueryServer    mPanIdQuery;
568     EnergyScanServer    mEnergyScan;
569 
570 #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE
571     AnycastLocator mAnycastLocator;
572 #endif
573 
574 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
575     TimeSync mTimeSync;
576 #endif
577 
578 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
579     LinkMetrics::Initiator mInitiator;
580 #endif
581 
582 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
583     LinkMetrics::Subject mSubject;
584 #endif
585 
586 #if OPENTHREAD_CONFIG_COAP_API_ENABLE
587     Coap::Coap mApplicationCoap;
588 #endif
589 
590 #if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
591     Coap::CoapSecure mApplicationCoapSecure;
592 #endif
593 
594 #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE
595     Utils::PingSender mPingSender;
596 #endif
597 
598 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
599     Utils::ChannelMonitor mChannelMonitor;
600 #endif
601 
602 #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD
603     Utils::ChannelManager mChannelManager;
604 #endif
605 
606 #if OPENTHREAD_CONFIG_MESH_DIAG_ENABLE && OPENTHREAD_FTD
607     Utils::MeshDiag mMeshDiag;
608 #endif
609 
610 #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE
611     Utils::HistoryTracker mHistoryTracker;
612 #endif
613 
614 #if OPENTHREAD_CONFIG_LINK_METRICS_MANAGER_ENABLE
615     Utils::LinkMetricsManager mLinkMetricsManager;
616 #endif
617 
618 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
619     MeshCoP::DatasetUpdater mDatasetUpdater;
620 #endif
621 
622 #if OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
623     AnnounceSender mAnnounceSender;
624 #endif
625 
626 #if OPENTHREAD_CONFIG_OTNS_ENABLE
627     Utils::Otns mOtns;
628 #endif
629 
630 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
631     BorderRouter::RoutingManager mRoutingManager;
632 #endif
633 
634 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
635     Nat64::Translator mNat64Translator;
636 #endif
637 
638 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
639 
640 #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE
641     Mac::LinkRaw mLinkRaw;
642 #endif
643 
644 #if OPENTHREAD_CONFIG_LOG_LEVEL_DYNAMIC_ENABLE
645     static LogLevel sLogLevel;
646 #endif
647 
648 #if OPENTHREAD_ENABLE_VENDOR_EXTENSION
649     Extension::ExtensionBase &mExtension;
650 #endif
651 
652 #if OPENTHREAD_CONFIG_DIAG_ENABLE
653     FactoryDiags::Diags mDiags;
654 #endif
655 #if OPENTHREAD_CONFIG_POWER_CALIBRATION_ENABLE && OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
656     Utils::PowerCalibration mPowerCalibration;
657 #endif
658 
659     bool mIsInitialized;
660 
661 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE && (OPENTHREAD_FTD || OPENTHREAD_MTD)
662     static bool sDnsNameCompressionEnabled;
663 #endif
664 
665     uint32_t mId;
666 };
667 
668 DefineCoreType(otInstance, Instance);
669 DefineCoreType(otBufferInfo, Instance::BufferInfo);
670 
671 // Specializations of the `Get<Type>()` method.
672 
Get(void)673 template <> inline Instance &Instance::Get(void) { return *this; }
674 
Get(void)675 template <> inline Radio &Instance::Get(void) { return mRadio; }
676 
Get(void)677 template <> inline Radio::Callbacks &Instance::Get(void) { return mRadio.mCallbacks; }
678 
679 #if OPENTHREAD_CONFIG_RADIO_STATS_ENABLE && (OPENTHREAD_FTD || OPENTHREAD_MTD)
Get(void)680 template <> inline RadioStatistics &Instance::Get(void) { return mRadio.mRadioStatistics; }
681 #endif
682 
683 #if OPENTHREAD_CONFIG_UPTIME_ENABLE
Get(void)684 template <> inline Uptime &Instance::Get(void) { return mUptime; }
685 #endif
686 
687 #if OPENTHREAD_MTD || OPENTHREAD_FTD
Get(void)688 template <> inline Notifier &Instance::Get(void) { return mNotifier; }
689 
Get(void)690 template <> inline TimeTicker &Instance::Get(void) { return mTimeTicker; }
691 
Get(void)692 template <> inline Settings &Instance::Get(void) { return mSettings; }
693 
Get(void)694 template <> inline SettingsDriver &Instance::Get(void) { return mSettingsDriver; }
695 
Get(void)696 template <> inline MeshForwarder &Instance::Get(void) { return mMeshForwarder; }
697 
698 #if OPENTHREAD_CONFIG_MULTI_RADIO
Get(void)699 template <> inline RadioSelector &Instance::Get(void) { return mRadioSelector; }
700 #endif
701 
Get(void)702 template <> inline Mle::Mle &Instance::Get(void) { return mMleRouter; }
703 
Get(void)704 template <> inline Mle::MleRouter &Instance::Get(void) { return mMleRouter; }
705 
Get(void)706 template <> inline Mle::DiscoverScanner &Instance::Get(void) { return mDiscoverScanner; }
707 
Get(void)708 template <> inline NeighborTable &Instance::Get(void) { return mMleRouter.mNeighborTable; }
709 
710 #if OPENTHREAD_FTD
Get(void)711 template <> inline ChildTable &Instance::Get(void) { return mMleRouter.mChildTable; }
712 
Get(void)713 template <> inline RouterTable &Instance::Get(void) { return mMleRouter.mRouterTable; }
714 #endif
715 
Get(void)716 template <> inline Ip6::Netif &Instance::Get(void) { return mThreadNetif; }
717 
Get(void)718 template <> inline ThreadNetif &Instance::Get(void) { return mThreadNetif; }
719 
Get(void)720 template <> inline Ip6::Ip6 &Instance::Get(void) { return mIp6; }
721 
Get(void)722 template <> inline Mac::Mac &Instance::Get(void) { return mMac; }
723 
Get(void)724 template <> inline Mac::SubMac &Instance::Get(void) { return mMac.mLinks.mSubMac; }
725 
726 #if OPENTHREAD_CONFIG_RADIO_LINK_TREL_ENABLE
Get(void)727 template <> inline Trel::Link &Instance::Get(void) { return mMac.mLinks.mTrel; }
728 
Get(void)729 template <> inline Trel::Interface &Instance::Get(void) { return mMac.mLinks.mTrel.mInterface; }
730 #endif
731 
732 #if OPENTHREAD_CONFIG_MAC_FILTER_ENABLE
Get(void)733 template <> inline Mac::Filter &Instance::Get(void) { return mMac.mFilter; }
734 #endif
735 
Get(void)736 template <> inline Lowpan::Lowpan &Instance::Get(void) { return mLowpan; }
737 
Get(void)738 template <> inline KeyManager &Instance::Get(void) { return mKeyManager; }
739 
Get(void)740 template <> inline Ip6::Filter &Instance::Get(void) { return mIp6Filter; }
741 
Get(void)742 template <> inline AddressResolver &Instance::Get(void) { return mAddressResolver; }
743 
744 #if OPENTHREAD_FTD
745 
Get(void)746 template <> inline IndirectSender &Instance::Get(void) { return mMeshForwarder.mIndirectSender; }
747 
Get(void)748 template <> inline SourceMatchController &Instance::Get(void)
749 {
750     return mMeshForwarder.mIndirectSender.mSourceMatchController;
751 }
752 
Get(void)753 template <> inline DataPollHandler &Instance::Get(void) { return mMeshForwarder.mIndirectSender.mDataPollHandler; }
754 
755 #if OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE
Get(void)756 template <> inline CslTxScheduler &Instance::Get(void) { return mMeshForwarder.mIndirectSender.mCslTxScheduler; }
757 #endif
758 
Get(void)759 template <> inline MeshCoP::Leader &Instance::Get(void) { return mLeader; }
760 
Get(void)761 template <> inline MeshCoP::JoinerRouter &Instance::Get(void) { return mJoinerRouter; }
762 #endif // OPENTHREAD_FTD
763 
Get(void)764 template <> inline AnnounceBeginServer &Instance::Get(void) { return mAnnounceBegin; }
765 
Get(void)766 template <> inline DataPollSender &Instance::Get(void) { return mMeshForwarder.mDataPollSender; }
767 
Get(void)768 template <> inline EnergyScanServer &Instance::Get(void) { return mEnergyScan; }
769 
Get(void)770 template <> inline PanIdQueryServer &Instance::Get(void) { return mPanIdQuery; }
771 
772 #if OPENTHREAD_CONFIG_TMF_ANYCAST_LOCATOR_ENABLE
Get(void)773 template <> inline AnycastLocator &Instance::Get(void) { return mAnycastLocator; }
774 #endif
775 
776 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
Get(void)777 template <> inline NetworkData::Local &Instance::Get(void) { return mNetworkDataLocal; }
778 #endif
779 
Get(void)780 template <> inline NetworkData::Leader &Instance::Get(void) { return mNetworkDataLeader; }
781 
782 #if OPENTHREAD_FTD || OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE || OPENTHREAD_CONFIG_TMF_NETDATA_SERVICE_ENABLE
Get(void)783 template <> inline NetworkData::Notifier &Instance::Get(void) { return mNetworkDataNotifier; }
784 #endif
785 
786 #if OPENTHREAD_CONFIG_NETDATA_PUBLISHER_ENABLE
Get(void)787 template <> inline NetworkData::Publisher &Instance::Get(void) { return mNetworkDataPublisher; }
788 #endif
789 
Get(void)790 template <> inline NetworkData::Service::Manager &Instance::Get(void) { return mNetworkDataServiceManager; }
791 
792 #if OPENTHREAD_CONFIG_TCP_ENABLE
Get(void)793 template <> inline Ip6::Tcp &Instance::Get(void) { return mIp6.mTcp; }
794 #endif
795 
Get(void)796 template <> inline Ip6::Udp &Instance::Get(void) { return mIp6.mUdp; }
797 
Get(void)798 template <> inline Ip6::Icmp &Instance::Get(void) { return mIp6.mIcmp; }
799 
Get(void)800 template <> inline Ip6::Mpl &Instance::Get(void) { return mIp6.mMpl; }
801 
Get(void)802 template <> inline Tmf::Agent &Instance::Get(void) { return mTmfAgent; }
803 
804 #if OPENTHREAD_CONFIG_DTLS_ENABLE
Get(void)805 template <> inline Tmf::SecureAgent &Instance::Get(void) { return mTmfSecureAgent; }
806 #endif
807 
Get(void)808 template <> inline MeshCoP::ExtendedPanIdManager &Instance::Get(void) { return mExtendedPanIdManager; }
809 
Get(void)810 template <> inline MeshCoP::NetworkNameManager &Instance::Get(void) { return mNetworkNameManager; }
811 
Get(void)812 template <> inline MeshCoP::ActiveDatasetManager &Instance::Get(void) { return mActiveDataset; }
813 
Get(void)814 template <> inline MeshCoP::PendingDatasetManager &Instance::Get(void) { return mPendingDataset; }
815 
816 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
Get(void)817 template <> inline TimeSync &Instance::Get(void) { return mTimeSync; }
818 #endif
819 
820 #if OPENTHREAD_CONFIG_COMMISSIONER_ENABLE && OPENTHREAD_FTD
Get(void)821 template <> inline MeshCoP::Commissioner &Instance::Get(void) { return mCommissioner; }
822 
Get(void)823 template <> inline AnnounceBeginClient &Instance::Get(void) { return mCommissioner.GetAnnounceBeginClient(); }
824 
Get(void)825 template <> inline EnergyScanClient &Instance::Get(void) { return mCommissioner.GetEnergyScanClient(); }
826 
Get(void)827 template <> inline PanIdQueryClient &Instance::Get(void) { return mCommissioner.GetPanIdQueryClient(); }
828 #endif
829 
830 #if OPENTHREAD_CONFIG_JOINER_ENABLE
Get(void)831 template <> inline MeshCoP::Joiner &Instance::Get(void) { return mJoiner; }
832 #endif
833 
834 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
Get(void)835 template <> inline Dns::Client &Instance::Get(void) { return mDnsClient; }
836 #endif
837 
838 #if OPENTHREAD_CONFIG_SRP_CLIENT_ENABLE
Get(void)839 template <> inline Srp::Client &Instance::Get(void) { return mSrpClient; }
840 #endif
841 
842 #if OPENTHREAD_CONFIG_SRP_CLIENT_BUFFERS_ENABLE
Get(void)843 template <> inline Utils::SrpClientBuffers &Instance::Get(void) { return mSrpClientBuffers; }
844 #endif
845 
846 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
Get(void)847 template <> inline Dns::ServiceDiscovery::Server &Instance::Get(void) { return mDnssdServer; }
848 #endif
849 
850 #if OPENTHREAD_CONFIG_DNS_DSO_ENABLE
Get(void)851 template <> inline Dns::Dso &Instance::Get(void) { return mDnsDso; }
852 #endif
853 
Get(void)854 template <> inline NetworkDiagnostic::Server &Instance::Get(void) { return mNetworkDiagnosticServer; }
855 
856 #if OPENTHREAD_CONFIG_TMF_NETDIAG_CLIENT_ENABLE
Get(void)857 template <> inline NetworkDiagnostic::Client &Instance::Get(void) { return mNetworkDiagnosticClient; }
858 #endif
859 
860 #if OPENTHREAD_CONFIG_DHCP6_CLIENT_ENABLE
Get(void)861 template <> inline Dhcp6::Client &Instance::Get(void) { return mDhcp6Client; }
862 #endif
863 
864 #if OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE
Get(void)865 template <> inline Dhcp6::Server &Instance::Get(void) { return mDhcp6Server; }
866 #endif
867 
868 #if OPENTHREAD_CONFIG_NEIGHBOR_DISCOVERY_AGENT_ENABLE
Get(void)869 template <> inline NeighborDiscovery::Agent &Instance::Get(void) { return mNeighborDiscoveryAgent; }
870 #endif
871 
872 #if OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE
Get(void)873 template <> inline Utils::Slaac &Instance::Get(void) { return mSlaac; }
874 #endif
875 
876 #if OPENTHREAD_CONFIG_JAM_DETECTION_ENABLE
Get(void)877 template <> inline Utils::JamDetector &Instance::Get(void) { return mJamDetector; }
878 #endif
879 
880 #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE
Get(void)881 template <> inline Sntp::Client &Instance::Get(void) { return mSntpClient; }
882 #endif
883 
884 #if OPENTHREAD_FTD
Get(void)885 template <> inline ChildSupervisor &Instance::Get(void) { return mChildSupervisor; }
886 #endif
Get(void)887 template <> inline SupervisionListener &Instance::Get(void) { return mSupervisionListener; }
888 
889 #if OPENTHREAD_CONFIG_PING_SENDER_ENABLE
Get(void)890 template <> inline Utils::PingSender &Instance::Get(void) { return mPingSender; }
891 #endif
892 
893 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
Get(void)894 template <> inline Utils::ChannelMonitor &Instance::Get(void) { return mChannelMonitor; }
895 #endif
896 
897 #if OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE && OPENTHREAD_FTD
Get(void)898 template <> inline Utils::ChannelManager &Instance::Get(void) { return mChannelManager; }
899 #endif
900 
901 #if OPENTHREAD_CONFIG_MESH_DIAG_ENABLE && OPENTHREAD_FTD
Get(void)902 template <> inline Utils::MeshDiag &Instance::Get(void) { return mMeshDiag; }
903 #endif
904 
905 #if OPENTHREAD_CONFIG_HISTORY_TRACKER_ENABLE
Get(void)906 template <> inline Utils::HistoryTracker &Instance::Get(void) { return mHistoryTracker; }
907 #endif
908 
909 #if OPENTHREAD_CONFIG_LINK_METRICS_MANAGER_ENABLE
Get(void)910 template <> inline Utils::LinkMetricsManager &Instance::Get(void) { return mLinkMetricsManager; }
911 #endif
912 
913 #if (OPENTHREAD_CONFIG_DATASET_UPDATER_ENABLE || OPENTHREAD_CONFIG_CHANNEL_MANAGER_ENABLE) && OPENTHREAD_FTD
Get(void)914 template <> inline MeshCoP::DatasetUpdater &Instance::Get(void) { return mDatasetUpdater; }
915 #endif
916 
917 #if OPENTHREAD_CONFIG_BORDER_AGENT_ENABLE
Get(void)918 template <> inline MeshCoP::BorderAgent &Instance::Get(void) { return mBorderAgent; }
919 #endif
920 
921 #if OPENTHREAD_CONFIG_ANNOUNCE_SENDER_ENABLE
Get(void)922 template <> inline AnnounceSender &Instance::Get(void) { return mAnnounceSender; }
923 #endif
924 
Get(void)925 template <> inline MessagePool &Instance::Get(void) { return mMessagePool; }
926 
927 #if (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
928 
Get(void)929 template <> inline BackboneRouter::Leader &Instance::Get(void) { return mBackboneRouterLeader; }
930 
931 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
Get(void)932 template <> inline BackboneRouter::Local   &Instance::Get(void) { return mBackboneRouterLocal; }
Get(void)933 template <> inline BackboneRouter::Manager &Instance::Get(void) { return mBackboneRouterManager; }
934 
935 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_MULTICAST_ROUTING_ENABLE
Get(void)936 template <> inline BackboneRouter::MulticastListenersTable &Instance::Get(void)
937 {
938     return mBackboneRouterManager.GetMulticastListenersTable();
939 }
940 #endif
941 
942 #if OPENTHREAD_CONFIG_BACKBONE_ROUTER_DUA_NDPROXYING_ENABLE
Get(void)943 template <> inline BackboneRouter::NdProxyTable &Instance::Get(void)
944 {
945     return mBackboneRouterManager.GetNdProxyTable();
946 }
947 #endif
948 
Get(void)949 template <> inline BackboneRouter::BackboneTmfAgent &Instance::Get(void)
950 {
951     return mBackboneRouterManager.GetBackboneTmfAgent();
952 }
953 #endif
954 
955 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
Get(void)956 template <> inline MlrManager &Instance::Get(void) { return mMlrManager; }
957 #endif
958 
959 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
Get(void)960 template <> inline DuaManager &Instance::Get(void) { return mDuaManager; }
961 #endif
962 
963 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_INITIATOR_ENABLE
Get(void)964 template <> inline LinkMetrics::Initiator &Instance::Get(void) { return mInitiator; }
965 #endif
966 
967 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
Get(void)968 template <> inline LinkMetrics::Subject &Instance::Get(void) { return mSubject; }
969 #endif
970 
971 #endif // (OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2)
972 
973 #if OPENTHREAD_CONFIG_OTNS_ENABLE
Get(void)974 template <> inline Utils::Otns &Instance::Get(void) { return mOtns; }
975 #endif
976 
977 #if OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
Get(void)978 template <> inline BorderRouter::RoutingManager &Instance::Get(void) { return mRoutingManager; }
979 
Get(void)980 template <> inline BorderRouter::InfraIf &Instance::Get(void) { return mRoutingManager.mInfraIf; }
981 #endif // OPENTHREAD_CONFIG_BORDER_ROUTING_ENABLE
982 
983 #if OPENTHREAD_CONFIG_NAT64_TRANSLATOR_ENABLE
Get(void)984 template <> inline Nat64::Translator &Instance::Get(void) { return mNat64Translator; }
985 #endif
986 
987 #if OPENTHREAD_CONFIG_SRP_SERVER_ENABLE
Get(void)988 template <> inline Srp::Server &Instance::Get(void) { return mSrpServer; }
989 #endif
990 
991 #endif // OPENTHREAD_MTD || OPENTHREAD_FTD
992 
993 #if OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE
Get(void)994 template <> inline Mac::LinkRaw &Instance::Get(void) { return mLinkRaw; }
995 
996 #if OPENTHREAD_RADIO
Get(void)997 template <> inline Mac::SubMac &Instance::Get(void) { return mLinkRaw.mSubMac; }
998 #endif
999 
1000 #endif // OPENTHREAD_RADIO || OPENTHREAD_CONFIG_LINK_RAW_ENABLE
1001 
Get(void)1002 template <> inline Tasklet::Scheduler &Instance::Get(void) { return mTaskletScheduler; }
1003 
Get(void)1004 template <> inline TimerMilli::Scheduler &Instance::Get(void) { return mTimerMilliScheduler; }
1005 
1006 #if OPENTHREAD_CONFIG_PLATFORM_USEC_TIMER_ENABLE
Get(void)1007 template <> inline TimerMicro::Scheduler &Instance::Get(void) { return mTimerMicroScheduler; }
1008 #endif
1009 
1010 #if OPENTHREAD_ENABLE_VENDOR_EXTENSION
Get(void)1011 template <> inline Extension::ExtensionBase &Instance::Get(void) { return mExtension; }
1012 #endif
1013 
1014 #if OPENTHREAD_CONFIG_DIAG_ENABLE
Get(void)1015 template <> inline FactoryDiags::Diags &Instance::Get(void) { return mDiags; }
1016 #endif
1017 
1018 #if OPENTHREAD_CONFIG_POWER_CALIBRATION_ENABLE && OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
Get(void)1019 template <> inline Utils::PowerCalibration &Instance::Get(void) { return mPowerCalibration; }
1020 #endif
1021 
1022 /**
1023  * @}
1024  *
1025  */
1026 
1027 } // namespace ot
1028 
1029 #endif // INSTANCE_HPP_
1030