• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 includes definitions for a time ticker.
32  */
33 
34 #ifndef TIME_TICKER_HPP_
35 #define TIME_TICKER_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #include <limits.h>
40 
41 #include "common/locator.hpp"
42 #include "common/non_copyable.hpp"
43 #include "common/time.hpp"
44 #include "common/timer.hpp"
45 
46 namespace ot {
47 
48 /**
49  * This class represents a time ticker.
50  *
51  * The time ticker emits periodic ticks (with 1 second period interval) to a set of registered tick receiver modules.
52  * The tick receivers (OpenThread objects) are identified by the `Receiver` enumeration. The receiver objects
53  * must provide `HandleTimeTick()` method which would be invoked by `TimeTicker` periodically.
54  *
55  */
56 class TimeTicker : public InstanceLocator, private NonCopyable
57 {
58 public:
59     /**
60      * This enumeration represents time tick receivers.
61      *
62      * This enumeration contains the list of all OpenThread modules that can be registered as time tick receivers.
63      *
64      */
65     enum Receiver : uint8_t
66     {
67         kMeshForwarder,          ///< `MeshForwarder`
68         kMleRouter,              ///< `Mle::MleRouter`
69         kAddressResolver,        ///< `AddressResolver`
70         kChildSupervisor,        ///< `Utils::ChildSupervisor`
71         kIp6FragmentReassembler, ///< `Ip6::Ip6` (handling of fragmented messages)
72         kDuaManager,             ///< `DuaManager`
73         kMlrManager,             ///< `MlrManager`
74         kNetworkDataNotifier,    ///< `NetworkData::Notifier`
75 
76         kNumReceivers, ///< Number of receivers.
77     };
78 
79     /**
80      * This constructor initializes the `TimeTicker` instance.
81      *
82      */
83     explicit TimeTicker(Instance &aInstance);
84 
85     /**
86      * This method registers a receiver with `TimeTicker` to receive periodic ticks.
87      *
88      * @param[in] aReceiver   A tick receiver identifier.
89      *
90      */
91     void RegisterReceiver(Receiver aReceiver);
92 
93     /**
94      * This method unregisters a receiver with `TimeTicker` to receive periodic ticks.
95      *
96      * @param[in] aReceiver   A tick receiver identifier.
97      *
98      */
99     void UnregisterReceiver(Receiver aReceiver);
100 
101     /**
102      * This method indicates whether a receiver is registered with `TimeTicker` to receive periodic ticks.
103      *
104      * @param[in] aReceiver   A tick receiver identifier.
105      *
106      * @retval TRUE   If @p aReceiver is registered with `TimeTicker`.
107      * @retval FALSE  If @p aReceiver is not registered with `TimeTicker`.
108      *
109      */
IsReceiverRegistered(Receiver aReceiver) const110     bool IsReceiverRegistered(Receiver aReceiver) const { return (mReceivers & Mask(aReceiver)) != 0; }
111 
112 private:
113     static constexpr uint32_t kTickInterval  = Time::kOneSecondInMsec;
114     static constexpr uint32_t kRestartJitter = 4; // in msec, jitter added when restarting the timer [-4,+4] ms.
115 
Mask(Receiver aReceiver)116     constexpr static uint32_t Mask(Receiver aReceiver) { return static_cast<uint32_t>(1U) << aReceiver; }
117 
118     static void HandleTimer(Timer &aTimer);
119     void        HandleTimer(void);
120 
121     uint32_t   mReceivers;
122     TimerMilli mTimer;
123 
124     static_assert(kNumReceivers < sizeof(mReceivers) * CHAR_BIT, "Too many `Receiver`s - does not fit in a bit mask");
125 };
126 
127 } // namespace ot
128 
129 #endif // TIMER_HPP_
130