utils.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Amazon FreeRTOS POSIX V1.1.0
  3. * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  6. * this software and associated documentation files (the "Software"), to deal in
  7. * the Software without restriction, including without limitation the rights to
  8. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  9. * the Software, and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in all
  13. * copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  17. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  18. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  19. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * http://aws.amazon.com/freertos
  23. * http://www.FreeRTOS.org
  24. */
  25. /**
  26. * @file utils.h
  27. * @brief Utility functions used by FreeRTOS+POSIX.
  28. */
  29. #ifndef _FREERTOS_POSIX_UTILS_
  30. #define _FREERTOS_POSIX_UTILS_
  31. /* C standard library includes. */
  32. #include <stdbool.h>
  33. #include <stdint.h>
  34. /* FreeRTOS+POSIX includes. */
  35. #include "FreeRTOS_POSIX/time.h"
  36. /**
  37. * @brief Calculates the length of pcString, up to xMaxLength.
  38. *
  39. * @param[in] pcString The string to find the length of.
  40. * @param[in] xMaxLength The limit when searching for the end of pcString.
  41. *
  42. * @return 0 if pcString is NULL; otherwise, the length of pcString or xMaxLength,
  43. * whichever is smaller.
  44. */
  45. size_t UTILS_strnlen( const char * const pcString,
  46. size_t xMaxLength );
  47. /**
  48. * @brief Calculates the number of ticks between now and a given timespec.
  49. *
  50. * @param[in] pxAbsoluteTime A time in the future, specified as seconds and
  51. * nanoseconds since CLOCK_REALTIME's 0.
  52. * @param[in] pxCurrentTime current time, specified as seconds and
  53. * nanoseconds.
  54. * @param[out] pxResult Where the result of the conversion is stored. The result
  55. * is rounded up for fractional ticks.
  56. *
  57. * @return 0 on success. Otherwise, ETIMEDOUT if pxAbsoluteTime is in the past,
  58. * or EINVAL for invalid parameters.
  59. */
  60. int UTILS_AbsoluteTimespecToDeltaTicks( const struct timespec * const pxAbsoluteTime,
  61. const struct timespec * const pxCurrentTime,
  62. TickType_t * const pxResult );
  63. /**
  64. * @brief Converts a struct timespec to FreeRTOS ticks.
  65. *
  66. * @param[in] pxTimespec The timespec to convert.
  67. * @param[out] Where the result of the conversion is stored. The result is rounded
  68. * up for fractional ticks.
  69. *
  70. * @return 0 on success. Otherwise, EINVAL for invalid parameters.
  71. */
  72. int UTILS_TimespecToTicks( const struct timespec * const pxTimespec,
  73. TickType_t * const pxResult );
  74. /**
  75. * @brief Converts an integer value to a timespec.
  76. *
  77. * @param[in] llSource The value to convert.
  78. * @param[out] pxDestination Where to store the converted value.
  79. *
  80. * @return No return value.
  81. */
  82. void UTILS_NanosecondsToTimespec( int64_t llSource,
  83. struct timespec * const pxDestination );
  84. /**
  85. * @brief Calculates pxResult = x + y.
  86. *
  87. * @param[in] x The first argument for addition.
  88. * @param[in] y The second argument for addition.
  89. * @param[out] pxResult Where the result of the calculation is stored.
  90. *
  91. * @return -1 if any argument was NULL; 1 if result is negative (overflow); otherwise, 0.
  92. */
  93. int UTILS_TimespecAdd( const struct timespec * const x,
  94. const struct timespec * const y,
  95. struct timespec * const pxResult );
  96. /**
  97. * @brief Calculates pxResult = x + ( struct timespec ) nanosec.
  98. *
  99. * @param[in] x The first argument for addition.
  100. * @param[in] llNanoseconds The second argument for addition.
  101. * @param[out] pxResult Where the result of the calculation is stored.
  102. *
  103. * @return -1 if pxResult or x was NULL; 1 if result is negative; otherwise, 0.
  104. */
  105. int UTILS_TimespecAddNanoseconds( const struct timespec * const x,
  106. int64_t llNanoseconds,
  107. struct timespec * const pxResult );
  108. /**
  109. * @brief Calculates pxResult = x - y. If the result is negative contents of
  110. * pResult are undefined
  111. *
  112. * @param[in] x The first argument for subtraction.
  113. * @param[in] y The second argument for subtraction.
  114. * @param[out] pxResult Where the result of the calculation is stored.
  115. *
  116. * @return -1 if any argument was NULL; 1 if result is negative; otherwise, 0.
  117. */
  118. int UTILS_TimespecSubtract( const struct timespec * const x,
  119. const struct timespec * const y,
  120. struct timespec * const pxResult );
  121. /**
  122. * @brief Compare x == y.
  123. *
  124. * @param[in] x The first argument for comparison.
  125. * @param[in] y The second argument for comparison.
  126. *
  127. * @return 0 if x == y; 1 if x > y; -1 if x < y or any argument was NULL
  128. */
  129. int UTILS_TimespecCompare( const struct timespec * const x,
  130. const struct timespec * const y );
  131. /**
  132. * @brief Checks that a timespec conforms to POSIX.
  133. *
  134. * A valid timespec must have 0 <= tv_nsec < 1000000000.
  135. *
  136. * @param[in] pxTimespec The timespec to validate.
  137. *
  138. * @return true if the pxTimespec is valid, false otherwise.
  139. */
  140. bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec );
  141. #endif /* ifndef _FREERTOS_POSIX_UTILS_ */