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 "instance/instance.hpp"
37
38 namespace ot {
39
TimeTicker(Instance & aInstance)40 TimeTicker::TimeTicker(Instance &aInstance)
41 : InstanceLocator(aInstance)
42 , mReceivers(0)
43 , mTimer(aInstance)
44 {
45 }
46
RegisterReceiver(Receiver aReceiver)47 void TimeTicker::RegisterReceiver(Receiver aReceiver)
48 {
49 mReceivers |= Mask(aReceiver);
50
51 if (!mTimer.IsRunning())
52 {
53 mTimer.Start(Random::NonCrypto::GetUint32InRange(0, kTickInterval + 1));
54 }
55 }
56
UnregisterReceiver(Receiver aReceiver)57 void TimeTicker::UnregisterReceiver(Receiver aReceiver)
58 {
59 mReceivers &= ~Mask(aReceiver);
60
61 if (mReceivers == 0)
62 {
63 mTimer.Stop();
64 }
65 }
66
HandleTimer(void)67 void TimeTicker::HandleTimer(void)
68 {
69 mTimer.FireAt(mTimer.GetFireTime() + Random::NonCrypto::AddJitter(kTickInterval, kRestartJitter));
70
71 if (mReceivers & Mask(kMeshForwarder))
72 {
73 Get<MeshForwarder>().HandleTimeTick();
74 }
75
76 #if OPENTHREAD_FTD
77 if (mReceivers & Mask(kMleRouter))
78 {
79 Get<Mle::MleRouter>().HandleTimeTick();
80 }
81
82 if (mReceivers & Mask(kAddressResolver))
83 {
84 Get<AddressResolver>().HandleTimeTick();
85 }
86
87 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE && OPENTHREAD_CONFIG_BORDER_ROUTER_REQUEST_ROUTER_ROLE
88 if (mReceivers & Mask(kNetworkDataNotifier))
89 {
90 Get<NetworkData::Notifier>().HandleTimeTick();
91 }
92 #endif
93
94 if (mReceivers & Mask(kChildSupervisor))
95 {
96 Get<ChildSupervisor>().HandleTimeTick();
97 }
98 #endif // OPENTHREAD_FTD
99
100 #if OPENTHREAD_CONFIG_IP6_FRAGMENTATION_ENABLE
101 if (mReceivers & Mask(kIp6FragmentReassembler))
102 {
103 Get<Ip6::Ip6>().HandleTimeTick();
104 }
105 #endif
106
107 #if OPENTHREAD_CONFIG_DUA_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_DUA_ENABLE)
108 if (mReceivers & Mask(kDuaManager))
109 {
110 Get<DuaManager>().HandleTimeTick();
111 }
112 #endif
113
114 #if OPENTHREAD_CONFIG_MLR_ENABLE || (OPENTHREAD_FTD && OPENTHREAD_CONFIG_TMF_PROXY_MLR_ENABLE)
115 if (mReceivers & Mask(kMlrManager))
116 {
117 Get<MlrManager>().HandleTimeTick();
118 }
119 #endif
120
121 if (mReceivers & Mask(kIp6Mpl))
122 {
123 Get<Ip6::Mpl>().HandleTimeTick();
124 }
125
126 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
127 if (mReceivers & Mask(kBbrLocal))
128 {
129 Get<BackboneRouter::Local>().HandleTimeTick();
130 }
131 #endif
132 }
133
134 } // namespace ot
135