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