1 /*
2 * Copyright (c) 2020, 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 a time ticker.
32 */
33
34 #include "time_ticker.hpp"
35
36 #include "common/code_utils.hpp"
37 #include "common/debug.hpp"
38 #include "common/instance.hpp"
39 #include "common/locator_getters.hpp"
40 #include "common/random.hpp"
41 #include "thread/mle_router.hpp"
42
43 namespace ot {
44
TimeTicker(Instance & aInstance)45 TimeTicker::TimeTicker(Instance &aInstance)
46 : InstanceLocator(aInstance)
47 , mReceivers(0)
48 , mTimer(aInstance, HandleTimer)
49 {
50 }
51
RegisterReceiver(Receiver aReceiver)52 void TimeTicker::RegisterReceiver(Receiver aReceiver)
53 {
54 mReceivers |= Mask(aReceiver);
55
56 if (!mTimer.IsRunning())
57 {
58 mTimer.Start(Random::NonCrypto::GetUint32InRange(0, kTickInterval + 1));
59 }
60 }
61
UnregisterReceiver(Receiver aReceiver)62 void TimeTicker::UnregisterReceiver(Receiver aReceiver)
63 {
64 mReceivers &= ~Mask(aReceiver);
65
66 if (mReceivers == 0)
67 {
68 mTimer.Stop();
69 }
70 }
71
HandleTimer(Timer & aTimer)72 void TimeTicker::HandleTimer(Timer &aTimer)
73 {
74 aTimer.Get<TimeTicker>().HandleTimer();
75 }
76
HandleTimer(void)77 void TimeTicker::HandleTimer(void)
78 {
79 mTimer.FireAt(mTimer.GetFireTime() + Random::NonCrypto::AddJitter(kTickInterval, kRestartJitter));
80
81 if (mReceivers & Mask(kMeshForwarder))
82 {
83 Get<MeshForwarder>().HandleTimeTick();
84 }
85
86 #if OPENTHREAD_FTD
87 if (mReceivers & Mask(kMleRouter))
88 {
89 Get<Mle::MleRouter>().HandleTimeTick();
90 }
91
92 if (mReceivers & Mask(kAddressResolver))
93 {
94 Get<AddressResolver>().HandleTimeTick();
95 }
96
97 #if OPENTHREAD_CONFIG_CHILD_SUPERVISION_ENABLE
98 if (mReceivers & Mask(kChildSupervisor))
99 {
100 Get<Utils::ChildSupervisor>().HandleTimeTick();
101 }
102 #endif
103 #endif // OPENTHREAD_FTD
104
105 #if OPENTHREAD_CONFIG_IP6_FRAGMENTATION_ENABLE
106 if (mReceivers & Mask(kIp6FragmentReassembler))
107 {
108 Get<Ip6::Ip6>().HandleTimeTick();
109 }
110 #endif
111
112 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
113 if (mReceivers & Mask(kDuaManager))
114 {
115 Get<DuaManager>().HandleTimeTick();
116 }
117 #endif
118
119 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
120 if (mReceivers & Mask(kMlrManager))
121 {
122 Get<MlrManager>().HandleTimeTick();
123 }
124 #endif
125 }
126
127 } // namespace ot
128