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/locator_getters.hpp"
39 #include "common/random.hpp"
40 #include "instance/instance.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)
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(void)72 void TimeTicker::HandleTimer(void)
73 {
74     mTimer.FireAt(mTimer.GetFireTime() + Random::NonCrypto::AddJitter(kTickInterval, kRestartJitter));
75 
76     if (mReceivers & Mask(kMeshForwarder))
77     {
78         Get<MeshForwarder>().HandleTimeTick();
79     }
80 
81 #if OPENTHREAD_FTD
82     if (mReceivers & Mask(kMleRouter))
83     {
84         Get<Mle::MleRouter>().HandleTimeTick();
85     }
86 
87     if (mReceivers & Mask(kAddressResolver))
88     {
89         Get<AddressResolver>().HandleTimeTick();
90     }
91 
92 #if OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE && OPENTHREAD_CONFIG_BORDER_ROUTER_REQUEST_ROUTER_ROLE
93     if (mReceivers & Mask(kNetworkDataNotifier))
94     {
95         Get<NetworkData::Notifier>().HandleTimeTick();
96     }
97 #endif
98 
99     if (mReceivers & Mask(kChildSupervisor))
100     {
101         Get<ChildSupervisor>().HandleTimeTick();
102     }
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     if (mReceivers & Mask(kIp6Mpl))
127     {
128         Get<Ip6::Mpl>().HandleTimeTick();
129     }
130 
131 #if OPENTHREAD_FTD && OPENTHREAD_CONFIG_BACKBONE_ROUTER_ENABLE
132     if (mReceivers & Mask(kBbrLocal))
133     {
134         Get<BackboneRouter::Local>().HandleTimeTick();
135     }
136 #endif
137 }
138 
139 } // namespace ot
140