2 * Amazon FreeRTOS Platform V1.0.0
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://aws.amazon.com/freertos
\r
23 * http://www.FreeRTOS.org
\r
28 * @brief Time-related functions used by libraries in this SDK.
\r
31 #ifndef IOT_CLOCK_H_
\r
32 #define IOT_CLOCK_H_
\r
34 /* The config header is always included first. */
\r
35 #include "iot_config.h"
\r
37 /* Standard includes. */
\r
38 #include <stdbool.h>
\r
42 /* Platform layer types include. */
\r
43 #include "types/iot_platform_types.h"
\r
46 * @functionspage{platform_clock,platform clock component,Clock}
\r
47 * - @functionname{platform_clock_function_gettimestring}
\r
48 * - @functionname{platform_clock_function_gettimems}
\r
49 * - @functionname{platform_clock_function_sleepms}
\r
50 * - @functionname{platform_clock_function_timercreate}
\r
51 * - @functionname{platform_clock_function_timerdestroy}
\r
52 * - @functionname{platform_clock_function_timerarm}
\r
56 * @functionpage{IotClock_GetTimestring,platform_clock,gettimestring}
\r
57 * @functionpage{IotClock_GetTimeMs,platform_clock,gettimems}
\r
58 * @functionpage{IotClock_SleepMs,platform_clock,sleepms}
\r
59 * @functionpage{IotClock_TimerCreate,platform_clock,timercreate}
\r
60 * @functionpage{IotClock_TimerDestroy,platform_clock,timerdestroy}
\r
61 * @functionpage{IotClock_TimerArm,platform_clock,timerarm}
\r
65 * @brief Generates a human-readable timestring, such as "01 Jan 2018 12:00".
\r
67 * This function uses the system clock to generate a human-readable timestring.
\r
68 * This timestring is printed by the [logging functions](@ref logging_functions).
\r
70 * @param[out] pBuffer A buffer to store the timestring in.
\r
71 * @param[in] bufferSize The size of `pBuffer`.
\r
72 * @param[out] pTimestringLength The actual length of the timestring stored in
\r
75 * @return `true` if a timestring was successfully generated; `false` otherwise.
\r
77 * @warning The implementation of this function must not call any [logging functions]
\r
78 * (@ref logging_functions).
\r
82 * char timestring[ 32 ];
\r
83 * size_t timestringLength = 0;
\r
85 * if( IotClock_GetTimestring( timestring, 32, ×tringLength ) == true )
\r
87 * printf( "Timestring: %.*s", timestringLength, timestring );
\r
91 /* @[declare_platform_clock_gettimestring] */
\r
92 bool IotClock_GetTimestring( char * pBuffer,
\r
94 size_t * pTimestringLength );
\r
95 /* @[declare_platform_clock_gettimestring] */
\r
98 * @brief Returns a nonzero, monotonically-increasing system time in milliseconds.
\r
100 * This function reads a millisecond-resolution system clock. The clock should
\r
101 * always be monotonically-increasing; therefore, real-time clocks that may be
\r
102 * set by another process are not suitable for this function's implementation.
\r
104 * @return The value of the system clock. This function is not expected to fail.
\r
108 * // Get current time.
\r
109 * uint64_t currentTime = IotClock_GetTimeMs();
\r
112 /* @[declare_platform_clock_gettimems] */
\r
113 uint64_t IotClock_GetTimeMs( void );
\r
114 /* @[declare_platform_clock_gettimems] */
\r
117 * @brief Delay for the given number of milliseconds.
\r
119 * This function suspends its calling thread for at least `sleepTimeMs` milliseconds.
\r
121 * @param[in] sleepTimeMs Sleep time (in milliseconds).
\r
123 /* @[declare_platform_clock_sleepms] */
\r
124 void IotClock_SleepMs( uint32_t sleepTimeMs );
\r
125 /* @[declare_platform_clock_sleepms] */
\r
128 * @brief Create a new timer.
\r
130 * This function creates a new, unarmed timer. It must be called on an uninitialized
\r
131 * #IotTimer_t. This function must not be called on an already-initialized #IotTimer_t.
\r
133 * @param[out] pNewTimer Set to a new timer handle on success.
\r
134 * @param[in] expirationRoutine The function to run when this timer expires. This
\r
135 * function should be called in its own <i>detached</i> thread.
\r
136 * @param[in] pArgument The argument to pass to `expirationRoutine`.
\r
138 * @return `true` if the timer is successfully created; `false` otherwise.
\r
140 * @see @ref platform_clock_function_timerdestroy, @ref platform_clock_function_timerarm
\r
142 /* @[declare_platform_clock_timercreate] */
\r
143 bool IotClock_TimerCreate( IotTimer_t * pNewTimer,
\r
144 IotThreadRoutine_t expirationRoutine,
\r
145 void * pArgument );
\r
146 /* @[declare_platform_clock_timercreate] */
\r
149 * @brief Free resources used by a timer.
\r
151 * This function frees resources used by a timer. It must be called on an initialized
\r
152 * #IotTimer_t. No other timer functions should be called on `pTimer` after calling
\r
153 * this function (unless the timer is re-created).
\r
155 * This function will stop the `pTimer` if it is armed.
\r
157 * @param[in] pTimer The timer to destroy.
\r
159 * @see @ref platform_clock_function_timercreate, @ref platform_clock_function_timerarm
\r
161 /* @[declare_platform_clock_timerdestroy] */
\r
162 void IotClock_TimerDestroy( IotTimer_t * pTimer );
\r
163 /* @[declare_platform_clock_timerdestroy] */
\r
166 * @brief Arm a timer to expire at the given relative timeout.
\r
168 * This function arms a timer to run its expiration routine at the given time.
\r
170 * If `periodMs` is nonzero, the timer should expire periodically at intervals
\r
172 * - `relativeTimeoutMs`
\r
173 * - `relativeTimeoutMs + periodMs`
\r
174 * - `relativeTimeoutMs + 2 * periodMs`
\r
175 * - Etc. (subject to some jitter).
\r
177 * Setting `periodMs` to `0` arms a one-shot, non-periodic timer.
\r
179 * @param[in] pTimer The timer to arm.
\r
180 * @param[in] relativeTimeoutMs When the timer should expire, relative to the time
\r
181 * this function is called.
\r
182 * @param[in] periodMs How often the timer should expire again after `relativeTimerMs`.
\r
184 * @return `true` if the timer was successfully armed; `false` otherwise.
\r
186 * @see @ref platform_clock_function_timercreate, @ref platform_clock_function_timerdestroy
\r
191 * void timerExpirationRoutine( void * pArgument );
\r
193 * void timerExample( void )
\r
195 * IotTimer_t timer;
\r
197 * if( IotClock_TimerCreate( &timer, timerExpirationRoutine, NULL ) == true )
\r
199 * // Set the timer to periodically expire every 10 seconds.
\r
200 * if( IotClock_TimerArm( &timer, 10000, 10000 ) == true )
\r
202 * // Wait for timer to expire.
\r
205 * IotClock_TimerDestroy( &timer );
\r
210 /* @[declare_platform_clock_timerarm] */
\r
211 bool IotClock_TimerArm( IotTimer_t * pTimer,
\r
212 uint32_t relativeTimeoutMs,
\r
213 uint32_t periodMs );
\r
214 /* @[declare_platform_clock_timerarm] */
\r
216 #endif /* ifndef IOT_CLOCK_H_ */
\r