--- /dev/null
+/*\r
+ * Amazon FreeRTOS POSIX V1.1.0\r
+ * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://aws.amazon.com/freertos\r
+ * http://www.FreeRTOS.org\r
+ */\r
+\r
+/**\r
+ * @file FreeRTOS_POSIX_clock.c\r
+ * @brief Implementation of clock functions in time.h\r
+ */\r
+\r
+/* C standard library includes. */\r
+#include <stddef.h>\r
+#include <string.h>\r
+\r
+/* FreeRTOS+POSIX includes. */\r
+#include "FreeRTOS_POSIX.h"\r
+#include "FreeRTOS_POSIX/errno.h"\r
+#include "FreeRTOS_POSIX/time.h"\r
+#include "FreeRTOS_POSIX/utils.h"\r
+\r
+/* Declaration of snprintf. The header stdio.h is not included because it\r
+ * includes conflicting symbols on some platforms. */\r
+extern int snprintf( char * s,\r
+ size_t n,\r
+ const char * format,\r
+ ... );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+clock_t clock( void )\r
+{\r
+ /* This function is currently unsupported. It will always return -1. */\r
+\r
+ return ( clock_t ) -1;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int clock_getcpuclockid( pid_t pid,\r
+ clockid_t * clock_id )\r
+{\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) pid;\r
+ ( void ) clock_id;\r
+\r
+ /* This function is currently unsupported. It will always return EPERM. */\r
+ return EPERM;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int clock_getres( clockid_t clock_id,\r
+ struct timespec * res )\r
+{\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) clock_id;\r
+\r
+ /* Convert FreeRTOS tick resolution as timespec. */\r
+ if( res != NULL )\r
+ {\r
+ res->tv_sec = 0;\r
+ res->tv_nsec = NANOSECONDS_PER_TICK;\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int clock_gettime( clockid_t clock_id,\r
+ struct timespec * tp )\r
+{\r
+ TimeOut_t xCurrentTime = { 0 };\r
+\r
+ /* Intermediate variable used to convert TimeOut_t to struct timespec.\r
+ * Also used to detect overflow issues. It must be unsigned because the\r
+ * behavior of signed integer overflow is undefined. */\r
+ uint64_t ullTickCount = 0ULL;\r
+\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) clock_id;\r
+\r
+ /* Get the current tick count and overflow count. vTaskSetTimeOutState()\r
+ * is used to get these values because they are both static in tasks.c. */\r
+ vTaskSetTimeOutState( &xCurrentTime );\r
+\r
+ /* Adjust the tick count for the number of times a TickType_t has overflowed.\r
+ * portMAX_DELAY should be the maximum value of a TickType_t. */\r
+ ullTickCount = ( uint64_t ) ( xCurrentTime.xOverflowCount ) << ( sizeof( TickType_t ) * 8 );\r
+\r
+ /* Add the current tick count. */\r
+ ullTickCount += xCurrentTime.xTimeOnEntering;\r
+\r
+ /* Convert ullTickCount to timespec. */\r
+ UTILS_NanosecondsToTimespec( ( int64_t ) ullTickCount * NANOSECONDS_PER_TICK, tp );\r
+\r
+ return 0;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int clock_nanosleep( clockid_t clock_id,\r
+ int flags,\r
+ const struct timespec * rqtp,\r
+ struct timespec * rmtp )\r
+{\r
+ int iStatus = 0;\r
+ TickType_t xSleepTime = 0;\r
+ struct timespec xCurrentTime = { 0 };\r
+\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) clock_id;\r
+ ( void ) rmtp;\r
+ ( void ) flags; /* This is only ignored if INCLUDE_vTaskDelayUntil is 0. */\r
+\r
+ /* Check rqtp. */\r
+ if( UTILS_ValidateTimespec( rqtp ) == false )\r
+ {\r
+ iStatus = EINVAL;\r
+ }\r
+\r
+ /* Get current time */\r
+ if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) )\r
+ {\r
+ iStatus = EINVAL;\r
+ }\r
+\r
+ if( iStatus == 0 )\r
+ {\r
+ /* Check for absolute time sleep. */\r
+ if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME )\r
+ {\r
+ /* Get current time */\r
+ if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )\r
+ {\r
+ iStatus = EINVAL;\r
+ }\r
+\r
+ /* Get number of ticks until absolute time. */\r
+ if( ( iStatus == 0 ) && ( UTILS_AbsoluteTimespecToDeltaTicks( rqtp, &xCurrentTime, &xSleepTime ) == 0 ) )\r
+ {\r
+ /* Delay until absolute time if vTaskDelayUntil is available. */\r
+ #if ( INCLUDE_vTaskDelayUntil == 1 )\r
+\r
+ /* Get the current tick count. This variable isn't declared\r
+ * at the top of the function because it's only used and needed\r
+ * if vTaskDelayUntil is available. */\r
+ TickType_t xCurrentTicks = xTaskGetTickCount();\r
+\r
+ /* Delay until absolute time. */\r
+ vTaskDelayUntil( &xCurrentTicks, xSleepTime );\r
+ #else\r
+\r
+ /* If vTaskDelayUntil isn't available, ignore the TIMER_ABSTIME flag\r
+ * and sleep for a relative time. */\r
+ vTaskDelay( xSleepTime );\r
+ #endif\r
+ }\r
+ }\r
+ else\r
+ {\r
+ /* If TIMER_ABSTIME isn't specified, convert rqtp to ticks and\r
+ * sleep for a relative time. */\r
+ if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )\r
+ {\r
+ vTaskDelay( xSleepTime );\r
+ }\r
+ }\r
+ }\r
+\r
+ return iStatus;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int clock_settime( clockid_t clock_id,\r
+ const struct timespec * tp )\r
+{\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) clock_id;\r
+ ( void ) tp;\r
+\r
+ /* This function is currently unsupported. It will always return -1 and\r
+ * set errno to EPERM. */\r
+ errno = EPERM;\r
+\r
+ return -1;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+int nanosleep( const struct timespec * rqtp,\r
+ struct timespec * rmtp )\r
+{\r
+ int iStatus = 0;\r
+ TickType_t xSleepTime = 0;\r
+\r
+ /* Silence warnings about unused parameters. */\r
+ ( void ) rmtp;\r
+\r
+ /* Check rqtp. */\r
+ if( UTILS_ValidateTimespec( rqtp ) == false )\r
+ {\r
+ errno = EINVAL;\r
+ iStatus = -1;\r
+ }\r
+\r
+ if( iStatus == 0 )\r
+ {\r
+ /* Convert rqtp to ticks and delay. */\r
+ if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )\r
+ {\r
+ vTaskDelay( xSleepTime );\r
+ }\r
+ }\r
+\r
+ return iStatus;\r
+}\r
+\r
+/*-----------------------------------------------------------*/\r