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