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