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