]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator/lib/FreeRTOS-Plus-POSIX/source/FreeRTOS_POSIX_clock.c
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_Plus_POSIX_with_actor_Windows_Simulator / lib / FreeRTOS-Plus-POSIX / source / FreeRTOS_POSIX_clock.c
1 /*\r
2  * Amazon FreeRTOS POSIX V1.1.0\r
3  * Copyright (C) 2018 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\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
11  *\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
14  *\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
21  *\r
22  * http://aws.amazon.com/freertos\r
23  * http://www.FreeRTOS.org\r
24  */\r
25 \r
26 /**\r
27  * @file FreeRTOS_POSIX_clock.c\r
28  * @brief Implementation of clock functions in time.h\r
29  */\r
30 \r
31 /* C standard library includes. */\r
32 #include <stddef.h>\r
33 #include <string.h>\r
34 \r
35 /* FreeRTOS+POSIX includes. */\r
36 #include "FreeRTOS_POSIX.h"\r
37 #include "FreeRTOS_POSIX/errno.h"\r
38 #include "FreeRTOS_POSIX/time.h"\r
39 #include "FreeRTOS_POSIX/utils.h"\r
40 \r
41 /* Declaration of snprintf. The header stdio.h is not included because it\r
42  * includes conflicting symbols on some platforms. */\r
43 extern int snprintf( char * s,\r
44                      size_t n,\r
45                      const char * format,\r
46                      ... );\r
47 \r
48 /*-----------------------------------------------------------*/\r
49 \r
50 clock_t clock( void )\r
51 {\r
52     /* This function is currently unsupported. It will always return -1. */\r
53 \r
54     return ( clock_t ) -1;\r
55 }\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 int clock_getcpuclockid( pid_t pid,\r
60                          clockid_t * clock_id )\r
61 {\r
62     /* Silence warnings about unused parameters. */\r
63     ( void ) pid;\r
64     ( void ) clock_id;\r
65 \r
66     /* This function is currently unsupported. It will always return EPERM. */\r
67     return EPERM;\r
68 }\r
69 \r
70 /*-----------------------------------------------------------*/\r
71 \r
72 int clock_getres( clockid_t clock_id,\r
73                   struct timespec * res )\r
74 {\r
75     /* Silence warnings about unused parameters. */\r
76     ( void ) clock_id;\r
77 \r
78     /* Convert FreeRTOS tick resolution as timespec. */\r
79     if( res != NULL )\r
80     {\r
81         res->tv_sec = 0;\r
82         res->tv_nsec = NANOSECONDS_PER_TICK;\r
83     }\r
84 \r
85     return 0;\r
86 }\r
87 \r
88 /*-----------------------------------------------------------*/\r
89 \r
90 int clock_gettime( clockid_t clock_id,\r
91                    struct timespec * tp )\r
92 {\r
93     TimeOut_t xCurrentTime = { 0 };\r
94 \r
95     /* Intermediate variable used to convert TimeOut_t to struct timespec.\r
96      * Also used to detect overflow issues. It must be unsigned because the\r
97      * behavior of signed integer overflow is undefined. */\r
98     uint64_t ullTickCount = 0ULL;\r
99 \r
100     /* Silence warnings about unused parameters. */\r
101     ( void ) clock_id;\r
102 \r
103     /* Get the current tick count and overflow count. vTaskSetTimeOutState()\r
104      * is used to get these values because they are both static in tasks.c. */\r
105     vTaskSetTimeOutState( &xCurrentTime );\r
106 \r
107     /* Adjust the tick count for the number of times a TickType_t has overflowed.\r
108      * portMAX_DELAY should be the maximum value of a TickType_t. */\r
109     ullTickCount = ( uint64_t ) ( xCurrentTime.xOverflowCount ) << ( sizeof( TickType_t ) * 8 );\r
110 \r
111     /* Add the current tick count. */\r
112     ullTickCount += xCurrentTime.xTimeOnEntering;\r
113 \r
114     /* Convert ullTickCount to timespec. */\r
115     UTILS_NanosecondsToTimespec( ( int64_t ) ullTickCount * NANOSECONDS_PER_TICK, tp );\r
116 \r
117     return 0;\r
118 }\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 int clock_nanosleep( clockid_t clock_id,\r
123                      int flags,\r
124                      const struct timespec * rqtp,\r
125                      struct timespec * rmtp )\r
126 {\r
127     int iStatus = 0;\r
128     TickType_t xSleepTime = 0;\r
129     struct timespec xCurrentTime = { 0 };\r
130 \r
131     /* Silence warnings about unused parameters. */\r
132     ( void ) clock_id;\r
133     ( void ) rmtp;\r
134     ( void ) flags; /* This is only ignored if INCLUDE_vTaskDelayUntil is 0. */\r
135 \r
136     /* Check rqtp. */\r
137     if( UTILS_ValidateTimespec( rqtp ) == false )\r
138     {\r
139         iStatus = EINVAL;\r
140     }\r
141 \r
142     /* Get current time */\r
143     if( ( iStatus == 0 ) && ( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 ) )\r
144     {\r
145         iStatus = EINVAL;\r
146     }\r
147 \r
148     if( iStatus == 0 )\r
149     {\r
150         /* Check for absolute time sleep. */\r
151         if( ( flags & TIMER_ABSTIME ) == TIMER_ABSTIME )\r
152         {\r
153             /* Get current time */\r
154             if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )\r
155             {\r
156                 iStatus = EINVAL;\r
157             }\r
158 \r
159             /* Get number of ticks until absolute time. */\r
160             if( ( iStatus == 0 ) && ( UTILS_AbsoluteTimespecToDeltaTicks( rqtp, &xCurrentTime, &xSleepTime ) == 0 ) )\r
161             {\r
162                 /* Delay until absolute time if vTaskDelayUntil is available. */\r
163                 #if ( INCLUDE_vTaskDelayUntil == 1 )\r
164 \r
165                     /* Get the current tick count. This variable isn't declared\r
166                      * at the top of the function because it's only used and needed\r
167                      * if vTaskDelayUntil is available. */\r
168                     TickType_t xCurrentTicks = xTaskGetTickCount();\r
169 \r
170                     /* Delay until absolute time. */\r
171                     vTaskDelayUntil( &xCurrentTicks, xSleepTime );\r
172                 #else\r
173 \r
174                     /* If vTaskDelayUntil isn't available, ignore the TIMER_ABSTIME flag\r
175                      * and sleep for a relative time. */\r
176                     vTaskDelay( xSleepTime );\r
177                 #endif\r
178             }\r
179         }\r
180         else\r
181         {\r
182             /* If TIMER_ABSTIME isn't specified, convert rqtp to ticks and\r
183              * sleep for a relative time. */\r
184             if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )\r
185             {\r
186                 vTaskDelay( xSleepTime );\r
187             }\r
188         }\r
189     }\r
190 \r
191     return iStatus;\r
192 }\r
193 \r
194 /*-----------------------------------------------------------*/\r
195 \r
196 int clock_settime( clockid_t clock_id,\r
197                    const struct timespec * tp )\r
198 {\r
199     /* Silence warnings about unused parameters. */\r
200     ( void ) clock_id;\r
201     ( void ) tp;\r
202 \r
203     /* This function is currently unsupported. It will always return -1 and\r
204      * set errno to EPERM. */\r
205     errno = EPERM;\r
206 \r
207     return -1;\r
208 }\r
209 \r
210 /*-----------------------------------------------------------*/\r
211 \r
212 int nanosleep( const struct timespec * rqtp,\r
213                struct timespec * rmtp )\r
214 {\r
215     int iStatus = 0;\r
216     TickType_t xSleepTime = 0;\r
217 \r
218     /* Silence warnings about unused parameters. */\r
219     ( void ) rmtp;\r
220 \r
221     /* Check rqtp. */\r
222     if( UTILS_ValidateTimespec( rqtp ) == false )\r
223     {\r
224         errno = EINVAL;\r
225         iStatus = -1;\r
226     }\r
227 \r
228     if( iStatus == 0 )\r
229     {\r
230         /* Convert rqtp to ticks and delay. */\r
231         if( UTILS_TimespecToTicks( rqtp, &xSleepTime ) == 0 )\r
232         {\r
233             vTaskDelay( xSleepTime );\r
234         }\r
235     }\r
236 \r
237     return iStatus;\r
238 }\r
239 \r
240 /*-----------------------------------------------------------*/\r