1 /*
2 * Copyright (c) 2016, The OpenThread Authors.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Neither the name of the copyright holder nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /**
30 * @file
31 * This file implements the Thread network interface.
32 */
33
34 #include "thread_netif.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/encoding.hpp"
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/message.hpp"
41 #include "net/ip6.hpp"
42 #include "net/netif.hpp"
43 #include "net/udp6.hpp"
44 #include "thread/mle.hpp"
45 #include "thread/thread_tlvs.hpp"
46 #include "thread/uri_paths.hpp"
47
48 namespace ot {
49
ThreadNetif(Instance & aInstance)50 ThreadNetif::ThreadNetif(Instance &aInstance)
51 : Netif(aInstance)
52 , mIsUp(false)
53 {
54 }
55
Up(void)56 void ThreadNetif::Up(void)
57 {
58 VerifyOrExit(!mIsUp);
59
60 // Enable the MAC just in case it was disabled while the Interface was down.
61 Get<Mac::Mac>().SetEnabled(true);
62 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
63 IgnoreError(Get<Utils::ChannelMonitor>().Start());
64 #endif
65 Get<MeshForwarder>().Start();
66
67 mIsUp = true;
68
69 SubscribeAllNodesMulticast();
70 IgnoreError(Get<Mle::MleRouter>().Enable());
71 IgnoreError(Get<Tmf::Agent>().Start());
72 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
73 IgnoreError(Get<Dns::ServiceDiscovery::Server>().Start());
74 #endif
75 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
76 IgnoreError(Get<Dns::Client>().Start());
77 #endif
78 #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE
79 IgnoreError(Get<Sntp::Client>().Start());
80 #endif
81 Get<Notifier>().Signal(kEventThreadNetifStateChanged);
82
83 exit:
84 return;
85 }
86
Down(void)87 void ThreadNetif::Down(void)
88 {
89 VerifyOrExit(mIsUp);
90
91 #if OPENTHREAD_CONFIG_DNS_CLIENT_ENABLE
92 Get<Dns::Client>().Stop();
93 #endif
94 #if OPENTHREAD_CONFIG_SNTP_CLIENT_ENABLE
95 IgnoreError(Get<Sntp::Client>().Stop());
96 #endif
97 #if OPENTHREAD_CONFIG_DNSSD_SERVER_ENABLE
98 Get<Dns::ServiceDiscovery::Server>().Stop();
99 #endif
100 #if OPENTHREAD_CONFIG_DTLS_ENABLE
101 Get<Tmf::SecureAgent>().Stop();
102 #endif
103 IgnoreError(Get<Tmf::Agent>().Stop());
104 IgnoreError(Get<Mle::MleRouter>().Disable());
105 RemoveAllExternalUnicastAddresses();
106 UnsubscribeAllExternalMulticastAddresses();
107 UnsubscribeAllRoutersMulticast();
108 UnsubscribeAllNodesMulticast();
109
110 mIsUp = false;
111 Get<MeshForwarder>().Stop();
112 #if OPENTHREAD_CONFIG_CHANNEL_MONITOR_ENABLE
113 IgnoreError(Get<Utils::ChannelMonitor>().Stop());
114 #endif
115 Get<Notifier>().Signal(kEventThreadNetifStateChanged);
116
117 exit:
118 return;
119 }
120
121 } // namespace ot
122