• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2018, 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 Thread network time synchronization service.
32  */
33 
34 #ifndef TIME_SYNC_HPP_
35 #define TIME_SYNC_HPP_
36 
37 #include "openthread-core-config.h"
38 
39 #if OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
40 
41 #include <openthread/network_time.h>
42 
43 #include "common/locator.hpp"
44 #include "common/message.hpp"
45 #include "common/non_copyable.hpp"
46 #include "common/notifier.hpp"
47 #include "common/timer.hpp"
48 
49 namespace ot {
50 
51 /**
52  * This class implements OpenThread Time Synchronization Service.
53  *
54  */
55 class TimeSync : public InstanceLocator, private NonCopyable
56 {
57     friend class ot::Notifier;
58 
59 public:
60     /**
61      * This constructor initializes the object.
62      *
63      */
64     TimeSync(Instance &aInstance);
65 
66     /**
67      * Get the Thread network time.
68      *
69      * @param[in,out] aNetworkTime  The Thread network time in microseconds.
70      *
71      * @returns The time synchronization status.
72      *
73      */
74     otNetworkTimeStatus GetTime(uint64_t &aNetworkTime) const;
75 
76     /**
77      * Handle the message which includes time synchronization information.
78      *
79      * @param[in] aMessage  The message which includes time synchronization information.
80      *
81      */
82     void HandleTimeSyncMessage(const Message &aMessage);
83 
84 #if OPENTHREAD_FTD
85     /**
86      * Send a time synchronization message when it is required.
87      *
88      * Note: A time synchronization message is required in following cases:
89      *       1. Leader send time sync message periodically;
90      *       2. Router(except Leader) received a time sync message with newer sequence;
91      *       3. Router received a time sync message with older sequence.
92      *
93      */
94     void ProcessTimeSync(void);
95 #endif
96 
97     /**
98      * This method gets the time synchronization sequence.
99      *
100      * @returns The time synchronization sequence.
101      *
102      */
GetTimeSyncSeq(void) const103     uint8_t GetTimeSyncSeq(void) const { return mTimeSyncSeq; }
104 
105     /**
106      * This method gets the time offset to the Thread network time.
107      *
108      * @returns The time offset to the Thread network time, in microseconds.
109      *
110      */
GetNetworkTimeOffset(void) const111     int64_t GetNetworkTimeOffset(void) const { return mNetworkTimeOffset; }
112 
113     /**
114      * Set the time synchronization period.
115      *
116      * @param[in] aTimeSyncPeriod   The time synchronization period, in seconds.
117      *
118      */
SetTimeSyncPeriod(uint16_t aTimeSyncPeriod)119     void SetTimeSyncPeriod(uint16_t aTimeSyncPeriod) { mTimeSyncPeriod = aTimeSyncPeriod; }
120 
121     /**
122      * Get the time synchronization period.
123      *
124      * @returns The time synchronization period, in seconds.
125      *
126      */
GetTimeSyncPeriod(void) const127     uint16_t GetTimeSyncPeriod(void) const { return mTimeSyncPeriod; }
128 
129     /**
130      * Set the time synchronization XTAL accuracy threshold for Router.
131      *
132      * @param[in] aXtalThreshold   The XTAL accuracy threshold for Router, in PPM.
133      *
134      */
SetXtalThreshold(uint16_t aXtalThreshold)135     void SetXtalThreshold(uint16_t aXtalThreshold) { mXtalThreshold = aXtalThreshold; }
136 
137     /**
138      * Get the time synchronization XTAL accuracy threshold for Router.
139      *
140      * @returns The XTAL accuracy threshold for Router, in PPM.
141      *
142      */
GetXtalThreshold(void) const143     uint16_t GetXtalThreshold(void) const { return mXtalThreshold; }
144 
145     /**
146      * Set the time sync callback to be notified of a network time update.
147      *
148      * @param[in] aCallback The callback to be called when time sync is handled.
149      * @param[in] aCallbackContext The context to be passed to callback.
150      *
151      */
SetTimeSyncCallback(otNetworkTimeSyncCallbackFn aCallback,void * aCallbackContext)152     void SetTimeSyncCallback(otNetworkTimeSyncCallbackFn aCallback, void *aCallbackContext)
153     {
154         mTimeSyncCallback        = aCallback;
155         mTimeSyncCallbackContext = aCallbackContext;
156     }
157 
158     /**
159      * Callback to be called when timer expires.
160      *
161      */
162     void HandleTimeout(void);
163 
164 private:
165     /**
166      * Callback to be called when thread state changes.
167      *
168      * @param[in] aFlags Flags that denote the state change events.
169      *
170      */
171     void HandleNotifierEvents(Events aEvents);
172 
173     /**
174      * Callback to be called when timer expires.
175      *
176      * @param[in] aTimer The corresponding timer.
177      *
178      */
179     static void HandleTimeout(Timer &aTimer);
180 
181     /**
182      * Check and handle any status change, and notify observers if applicable.
183      *
184      * @param[in] aNotifyTimeUpdated True to denote that observers should be notified due to a time change, false
185      * otherwise.
186      *
187      */
188     void CheckAndHandleChanges(bool aNotifyTimeUpdated);
189 
190     /**
191      * Increase the time synchronization sequence.
192      *
193      */
194     void IncrementTimeSyncSeq(void);
195 
196     /**
197      * Notify any listener of a network time sync update event.
198      *
199      */
200     void NotifyTimeSyncCallback(void);
201 
202     bool     mTimeSyncRequired; ///< Indicate whether or not a time synchronization message is required.
203     uint8_t  mTimeSyncSeq;      ///< The time synchronization sequence.
204     uint16_t mTimeSyncPeriod;   ///< The time synchronization period.
205     uint16_t mXtalThreshold;    ///< The XTAL accuracy threshold for a device to become a Router, in PPM.
206 #if OPENTHREAD_FTD
207     TimeMilli mLastTimeSyncSent; ///< The time when the last time synchronization message was sent.
208 #endif
209     TimeMilli mLastTimeSyncReceived; ///< The time when the last time synchronization message was received.
210     int64_t   mNetworkTimeOffset;    ///< The time offset to the Thread Network time
211     otNetworkTimeSyncCallbackFn
212                         mTimeSyncCallback; ///< The callback to be called when time sync is handled or status updated.
213     void *              mTimeSyncCallbackContext; ///< The context to be passed to callback.
214     TimerMilli          mTimer;                   ///< Timer for checking if a resync is required.
215     otNetworkTimeStatus mCurrentStatus;           ///< Current network time status.
216 };
217 
218 /**
219  * @}
220  */
221 
222 } // namespace ot
223 
224 #endif // OPENTHREAD_CONFIG_TIME_SYNC_ENABLE
225 
226 #endif // TIME_SYNC_HPP_
227