2 * Amazon FreeRTOS POSIX V1.1.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 Utility functions used by FreeRTOS+POSIX.
\r
31 #ifndef _FREERTOS_POSIX_UTILS_
\r
32 #define _FREERTOS_POSIX_UTILS_
\r
34 /* C standard library includes. */
\r
35 #include <stdbool.h>
\r
38 /* FreeRTOS+POSIX includes. */
\r
39 #include "FreeRTOS_POSIX/time.h"
\r
42 * @brief Calculates the length of pcString, up to xMaxLength.
\r
44 * @param[in] pcString The string to find the length of.
\r
45 * @param[in] xMaxLength The limit when searching for the end of pcString.
\r
47 * @return 0 if pcString is NULL; otherwise, the length of pcString or xMaxLength,
\r
48 * whichever is smaller.
\r
50 size_t UTILS_strnlen( const char * const pcString,
\r
51 size_t xMaxLength );
\r
54 * @brief Calculates the number of ticks between now and a given timespec.
\r
56 * @param[in] pxAbsoluteTime A time in the future, specified as seconds and
\r
57 * nanoseconds since CLOCK_REALTIME's 0.
\r
58 * @param[in] pxCurrentTime current time, specified as seconds and
\r
60 * @param[out] pxResult Where the result of the conversion is stored. The result
\r
61 * is rounded up for fractional ticks.
\r
63 * @return 0 on success. Otherwise, ETIMEDOUT if pxAbsoluteTime is in the past,
\r
64 * or EINVAL for invalid parameters.
\r
66 int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime,
\r
67 const struct timespec * const pxCurrentTime,
\r
68 TickType_t * const pxResult );
\r
71 * @brief Converts a struct timespec to FreeRTOS ticks.
\r
73 * @param[in] pxTimespec The timespec to convert.
\r
74 * @param[out] Where the result of the conversion is stored. The result is rounded
\r
75 * up for fractional ticks.
\r
77 * @return 0 on success. Otherwise, EINVAL for invalid parameters.
\r
79 int UTILS_TimespecToTicks( const struct timespec * const pxTimespec,
\r
80 TickType_t * const pxResult );
\r
83 * @brief Converts an integer value to a timespec.
\r
85 * @param[in] llSource The value to convert.
\r
86 * @param[out] pxDestination Where to store the converted value.
\r
88 * @return No return value.
\r
90 void UTILS_NanosecondsToTimespec( int64_t llSource,
\r
91 struct timespec * const pxDestination );
\r
94 * @brief Calculates pxResult = x + y.
\r
96 * @param[in] x The first argument for addition.
\r
97 * @param[in] y The second argument for addition.
\r
98 * @param[out] pxResult Where the result of the calculation is stored.
\r
100 * @return -1 if any argument was NULL; 1 if result is negative (overflow); otherwise, 0.
\r
102 int UTILS_TimespecAdd( const struct timespec * const x,
\r
103 const struct timespec * const y,
\r
104 struct timespec * const pxResult );
\r
107 * @brief Calculates pxResult = x + ( struct timespec ) nanosec.
\r
109 * @param[in] x The first argument for addition.
\r
110 * @param[in] llNanoseconds The second argument for addition.
\r
111 * @param[out] pxResult Where the result of the calculation is stored.
\r
113 * @return -1 if pxResult or x was NULL; 1 if result is negative; otherwise, 0.
\r
115 int UTILS_TimespecAddNanoseconds( const struct timespec * const x,
\r
116 int64_t llNanoseconds,
\r
117 struct timespec * const pxResult );
\r
120 * @brief Calculates pxResult = x - y. If the result is negative contents of
\r
121 * pResult are undefined
\r
123 * @param[in] x The first argument for subtraction.
\r
124 * @param[in] y The second argument for subtraction.
\r
125 * @param[out] pxResult Where the result of the calculation is stored.
\r
127 * @return -1 if any argument was NULL; 1 if result is negative; otherwise, 0.
\r
129 int UTILS_TimespecSubtract( const struct timespec * const x,
\r
130 const struct timespec * const y,
\r
131 struct timespec * const pxResult );
\r
134 * @brief Compare x == y.
\r
136 * @param[in] x The first argument for comparison.
\r
137 * @param[in] y The second argument for comparison.
\r
139 * @return 0 if x == y; 1 if x > y; -1 if x < y or any argument was NULL
\r
141 int UTILS_TimespecCompare( const struct timespec * const x,
\r
142 const struct timespec * const y );
\r
145 * @brief Checks that a timespec conforms to POSIX.
\r
147 * A valid timespec must have 0 <= tv_nsec < 1000000000.
\r
149 * @param[in] pxTimespec The timespec to validate.
\r
151 * @return true if the pxTimespec is valid, false otherwise.
\r
153 bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec );
\r
155 #endif /* ifndef _FREERTOS_POSIX_UTILS_ */
\r