1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
36
37
38
47
48
49
50
51
52
56
57
58
59
60
61
62
63
64
65
66
70
71
72
79
80
81
89
90
91
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
113
117
118
119
120
121
122
123
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
245
246
247
248
249
/* ... */
/* ... */
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include "FreeRTOS_POSIX.h"
#include "FreeRTOS_POSIX/utils.h"6 includes
size_t UTILS_strnlen( const char * const pcString,
size_t xMaxLength )
{
return strnlen( pcString, xMaxLength );
}{ ... }
int UTILS_AbsoluteTimespecToTicks( const struct timespec * const pxAbsoluteTime,
TickType_t * const pxResult )
{
int iStatus = 0;
struct timespec xCurrentTime = { 0 }, xDifference = { 0 };
if( ( pxAbsoluteTime == NULL ) || ( pxResult == NULL ) )
{
iStatus = EINVAL;
}{...}
if( iStatus == 0 )
{
if( clock_gettime( CLOCK_REALTIME, &xCurrentTime ) != 0 )
{
iStatus = errno;
}{...}
}{...}
if( iStatus == 0 )
{
if( UTILS_TimespecSubtract( &xDifference, pxAbsoluteTime, &xCurrentTime ) != 0 )
{
iStatus = ETIMEDOUT;
}{...}
}{...}
if( iStatus == 0 )
{
iStatus = UTILS_TimespecToTicks( &xDifference, pxResult );
}{...}
return iStatus;
}{ ... }
int UTILS_TimespecToTicks( const struct timespec * const pxTimespec,
TickType_t * const pxResult )
{
int iStatus = 0;
uint64_t ullTotalTicks = 0;
long lNanoseconds = 0;
if( ( pxTimespec == NULL ) || ( pxResult == NULL ) )
{
iStatus = EINVAL;
}{...}
else if( ( pxTimespec != NULL ) && ( UTILS_ValidateTimespec( pxTimespec ) == false ) )
{
iStatus = EINVAL;
}{...}
if( iStatus == 0 )
{
ullTotalTicks = ( uint64_t ) configTICK_RATE_HZ * ( uint64_t ) ( pxTimespec->tv_sec );
/* ... */
lNanoseconds = pxTimespec->tv_nsec / ( long ) NANOSECONDS_PER_TICK +
( long ) ( pxTimespec->tv_nsec % ( long ) NANOSECONDS_PER_TICK != 0 );
ullTotalTicks += ( uint64_t ) lNanoseconds;
*pxResult = ( TickType_t ) ullTotalTicks;
}{...}
return iStatus;
}{ ... }
void UTILS_NanosecondsToTimespec( int64_t llSource,
struct timespec * const pxDestination )
{
long lCarrySec = 0;
pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND );
pxDestination->tv_nsec = ( long ) ( llSource % NANOSECONDS_PER_SECOND );
if( pxDestination->tv_nsec < 0L )
{
lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L;
pxDestination->tv_sec -= ( time_t ) ( lCarrySec );
pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND;
}{...}
}{ ... }
int UTILS_TimespecAdd( struct timespec * const pxResult,
const struct timespec * const x,
const struct timespec * const y )
{
int64_t llResult64 = 0;
if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) )
{
return -1;
}{...}
llResult64 = ( ( ( int64_t ) ( x->tv_sec ) * NANOSECONDS_PER_SECOND ) + ( int64_t ) ( x->tv_nsec ) )
+ ( ( ( int64_t ) ( y->tv_sec ) * NANOSECONDS_PER_SECOND ) + ( int64_t ) ( y->tv_nsec ) );
UTILS_NanosecondsToTimespec( llResult64, pxResult );
return ( int ) ( llResult64 < 0LL );
}{ ... }
int UTILS_TimespecAddNanoseconds( struct timespec * const pxResult,
const struct timespec * const x,
int64_t llNanoseconds )
{
struct timespec y = { .tv_sec = ( time_t ) 0, .tv_nsec = 0L };
if( ( pxResult == NULL ) || ( x == NULL ) )
{
return -1;
}{...}
UTILS_NanosecondsToTimespec( llNanoseconds, &y );
return UTILS_TimespecAdd( pxResult, x, &y );
}{ ... }
int UTILS_TimespecSubtract( struct timespec * const pxResult,
const struct timespec * const x,
const struct timespec * const y )
{
int64_t llResult64 = 0;
if( ( pxResult == NULL ) || ( x == NULL ) || ( y == NULL ) )
{
return -1;
}{...}
llResult64 = ( ( ( int64_t ) ( x->tv_sec ) * NANOSECONDS_PER_SECOND ) + ( int64_t ) ( x->tv_nsec ) )
- ( ( ( int64_t ) ( y->tv_sec ) * NANOSECONDS_PER_SECOND ) + ( int64_t ) ( y->tv_nsec ) );
UTILS_NanosecondsToTimespec( llResult64, pxResult );
return ( int ) ( llResult64 < 0LL );
}{ ... }
bool UTILS_ValidateTimespec( const struct timespec * const pxTimespec )
{
bool xReturn = false;
if( pxTimespec != NULL )
{
if( ( pxTimespec->tv_nsec >= 0 ) &&
( pxTimespec->tv_nsec < NANOSECONDS_PER_SECOND ) )
{
xReturn = true;
}{...}
}{...}
return xReturn;
}{ ... }