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  * @brief
32  *  This file defines the OpenThread Network Time Synchronization Service API.
33  */
34 
35 #ifndef OPENTHREAD_NETWORK_TIME_H_
36 #define OPENTHREAD_NETWORK_TIME_H_
37 
38 #include <openthread/error.h>
39 #include <openthread/instance.h>
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 /**
46  * @addtogroup api-network-time
47  *
48  * @brief
49  *   This module includes functions that control network time synchronization service.
50  *
51  * @{
52  */
53 
54 /**
55  * Represents OpenThread time synchronization status.
56  */
57 typedef enum otNetworkTimeStatus
58 {
59     OT_NETWORK_TIME_UNSYNCHRONIZED = -1, ///< The device hasn't attached to a network.
60     OT_NETWORK_TIME_RESYNC_NEEDED  = 0,  ///< The device hasn’t received time sync for more than two periods time.
61     OT_NETWORK_TIME_SYNCHRONIZED   = 1,  ///< The device network time is synchronized.
62 } otNetworkTimeStatus;
63 
64 /**
65  * Pointer is called when a network time sync or status change occurs.
66  */
67 typedef void (*otNetworkTimeSyncCallbackFn)(void *aCallbackContext);
68 
69 /**
70  * zero is considered as invalid time synchronization sequence.
71  */
72 #define OT_TIME_SYNC_INVALID_SEQ 0
73 
74 /**
75  * Get the Thread network time.
76  *
77  * @param[in]     aInstance     The OpenThread instance structure.
78  * @param[in,out] aNetworkTime  The Thread network time in microseconds.
79  *
80  * @returns The time synchronization status.
81  */
82 otNetworkTimeStatus otNetworkTimeGet(otInstance *aInstance, uint64_t *aNetworkTime);
83 
84 /**
85  * Set the time synchronization period.
86  *
87  * Can only be called while Thread protocols are disabled.
88  *
89  * @param[in] aInstance         The OpenThread instance structure.
90  * @param[in] aTimeSyncPeriod   The time synchronization period, in seconds.
91  *
92  * @retval OT_ERROR_NONE           Successfully set the time sync period.
93  * @retval OT_ERROR_INVALID_STATE  Thread protocols are enabled.
94  */
95 otError otNetworkTimeSetSyncPeriod(otInstance *aInstance, uint16_t aTimeSyncPeriod);
96 
97 /**
98  * Get the time synchronization period.
99  *
100  * @param[in] aInstance  The OpenThread instance structure.
101  *
102  * @returns The time synchronization period.
103  */
104 uint16_t otNetworkTimeGetSyncPeriod(otInstance *aInstance);
105 
106 /**
107  * Set the time synchronization XTAL accuracy threshold for Router-Capable device.
108  *
109  * Can only be called while Thread protocols are disabled.
110  *
111  * @param[in] aInstance        The OpenThread instance structure.
112  * @param[in] aXTALThreshold   The XTAL accuracy threshold for Router, in PPM.
113  *
114  * @retval OT_ERROR_NONE           Successfully set the time sync period.
115  * @retval OT_ERROR_INVALID_STATE  Thread protocols are enabled.
116  */
117 otError otNetworkTimeSetXtalThreshold(otInstance *aInstance, uint16_t aXTALThreshold);
118 
119 /**
120  * Get the time synchronization XTAL accuracy threshold for Router.
121  *
122  * @param[in] aInstance  The OpenThread instance structure.
123  *
124  * @returns The XTAL accuracy threshold for Router, in PPM.
125  */
126 uint16_t otNetworkTimeGetXtalThreshold(otInstance *aInstance);
127 
128 /**
129  * Set a callback to be called when a network time sync or status change occurs
130  *
131  * This callback shall be called only when the network time offset jumps by
132  * OPENTHREAD_CONFIG_TIME_SYNC_JUMP_NOTIF_MIN_US or when the status changes.
133  *
134  * @param[in] aInstance The OpenThread instance structure.
135  * @param[in] aCallbackFn The callback function to be called
136  * @param[in] aCallbackContext The context to be passed to the callback function upon invocation
137  */
138 void otNetworkTimeSyncSetCallback(otInstance                 *aInstance,
139                                   otNetworkTimeSyncCallbackFn aCallbackFn,
140                                   void                       *aCallbackContext);
141 
142 /**
143  * @}
144  */
145 
146 #ifdef __cplusplus
147 } // extern "C"
148 #endif
149 
150 #endif // OPENTHREAD_NETWORK_TIME_H_
151