Amazon FreeRTOS: POSIX
Return to main page ↑
utils.h File Reference

Utility functions used by FreeRTOS+POSIX. More...

#include <stdbool.h>
#include <stdint.h>
#include "FreeRTOS_POSIX/time.h"

Go to the source code of this file.

Functions

size_t UTILS_strnlen (const char *const pcString, size_t xMaxLength)
 Calculates the length of pcString, up to xMaxLength. More...
 
int UTILS_AbsoluteTimespecToDeltaTicks (const struct timespec *const pxAbsoluteTime, const struct timespec *const pxCurrentTime, TickType_t *const pxResult)
 Calculates the number of ticks between now and a given timespec. More...
 
int UTILS_TimespecToTicks (const struct timespec *const pxTimespec, TickType_t *const pxResult)
 Converts a struct timespec to FreeRTOS ticks. More...
 
void UTILS_NanosecondsToTimespec (int64_t llSource, struct timespec *const pxDestination)
 Converts an integer value to a timespec. More...
 
int UTILS_TimespecAdd (const struct timespec *const x, const struct timespec *const y, struct timespec *const pxResult)
 Calculates pxResult = x + y. More...
 
int UTILS_TimespecAddNanoseconds (const struct timespec *const x, int64_t llNanoseconds, struct timespec *const pxResult)
 Calculates pxResult = x + ( struct timespec ) nanosec. More...
 
int UTILS_TimespecSubtract (const struct timespec *const x, const struct timespec *const y, struct timespec *const pxResult)
 Calculates pxResult = x - y. If the result is negative contents of pResult are undefined. More...
 
int UTILS_TimespecCompare (const struct timespec *const x, const struct timespec *const y)
 Compare x == y. More...
 
bool UTILS_ValidateTimespec (const struct timespec *const pxTimespec)
 Checks that a timespec conforms to POSIX. More...
 

Detailed Description

Utility functions used by FreeRTOS+POSIX.

Function Documentation

◆ UTILS_strnlen()

size_t UTILS_strnlen ( const char *const  pcString,
size_t  xMaxLength 
)

Calculates the length of pcString, up to xMaxLength.

Parameters
[in]pcStringThe string to find the length of.
[in]xMaxLengthThe limit when searching for the end of pcString.
Returns
0 if pcString is NULL; otherwise, the length of pcString or xMaxLength, whichever is smaller.

◆ UTILS_AbsoluteTimespecToDeltaTicks()

int UTILS_AbsoluteTimespecToDeltaTicks ( const struct timespec *const  pxAbsoluteTime,
const struct timespec *const  pxCurrentTime,
TickType_t *const  pxResult 
)

Calculates the number of ticks between now and a given timespec.

Parameters
[in]pxAbsoluteTimeA time in the future, specified as seconds and nanoseconds since CLOCK_REALTIME's 0.
[in]pxCurrentTimecurrent time, specified as seconds and nanoseconds.
[out]pxResultWhere the result of the conversion is stored. The result is rounded up for fractional ticks.
Returns
0 on success. Otherwise, ETIMEDOUT if pxAbsoluteTime is in the past, or EINVAL for invalid parameters.

◆ UTILS_TimespecToTicks()

int UTILS_TimespecToTicks ( const struct timespec *const  pxTimespec,
TickType_t *const  pxResult 
)

Converts a struct timespec to FreeRTOS ticks.

Parameters
[in]pxTimespecThe timespec to convert.
[out]Wherethe result of the conversion is stored. The result is rounded up for fractional ticks.
Returns
0 on success. Otherwise, EINVAL for invalid parameters.

◆ UTILS_NanosecondsToTimespec()

void UTILS_NanosecondsToTimespec ( int64_t  llSource,
struct timespec *const  pxDestination 
)

Converts an integer value to a timespec.

Parameters
[in]llSourceThe value to convert.
[out]pxDestinationWhere to store the converted value.
Returns
No return value.

◆ UTILS_TimespecAdd()

int UTILS_TimespecAdd ( const struct timespec *const  x,
const struct timespec *const  y,
struct timespec *const  pxResult 
)

Calculates pxResult = x + y.

Parameters
[in]xThe first argument for addition.
[in]yThe second argument for addition.
[out]pxResultWhere the result of the calculation is stored.
Returns
-1 if any argument was NULL; 1 if result is negative (overflow); otherwise, 0.

◆ UTILS_TimespecAddNanoseconds()

int UTILS_TimespecAddNanoseconds ( const struct timespec *const  x,
int64_t  llNanoseconds,
struct timespec *const  pxResult 
)

Calculates pxResult = x + ( struct timespec ) nanosec.

Parameters
[in]xThe first argument for addition.
[in]llNanosecondsThe second argument for addition.
[out]pxResultWhere the result of the calculation is stored.
Returns
-1 if pxResult or x was NULL; 1 if result is negative; otherwise, 0.

◆ UTILS_TimespecSubtract()

int UTILS_TimespecSubtract ( const struct timespec *const  x,
const struct timespec *const  y,
struct timespec *const  pxResult 
)

Calculates pxResult = x - y. If the result is negative contents of pResult are undefined.

Parameters
[in]xThe first argument for subtraction.
[in]yThe second argument for subtraction.
[out]pxResultWhere the result of the calculation is stored.
Returns
-1 if any argument was NULL; 1 if result is negative; otherwise, 0.

◆ UTILS_TimespecCompare()

int UTILS_TimespecCompare ( const struct timespec *const  x,
const struct timespec *const  y 
)

Compare x == y.

Parameters
[in]xThe first argument for comparison.
[in]yThe second argument for comparison.
Returns
0 if x == y; 1 if x > y; -1 if x < y or any argument was NULL

◆ UTILS_ValidateTimespec()

bool UTILS_ValidateTimespec ( const struct timespec *const  pxTimespec)

Checks that a timespec conforms to POSIX.

A valid timespec must have 0 <= tv_nsec < 1000000000.

Parameters
[in]pxTimespecThe timespec to validate.
Returns
true if the pxTimespec is valid, false otherwise.