-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathap_utils.c
332 lines (272 loc) · 8.78 KB
/
ap_utils.c
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/** \file ap_utils.c
* \brief Part of AP's Toolkit. Miscellaneous utility functions module
*/
#define AP_UTILS_C
#include "ap_utils.h"
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*=========================================================*/
/** \brief Compares struct timeval against current time, returning result of then <=> now
*
* \param tv struct timeval *
* \return int - -1 if less than now, 0 if equal, 1 if past current time
*/
int ap_utils_timeval_cmp_to_now(struct timeval *tv)
{
struct timeval now;
gettimeofday(&now, NULL);
if ( timercmp(tv, &now, >) )
return 1;
else if ( timercmp(tv, &now, <) )
return -1;
return 0;
}
/*=========================================================*/
/** \brief Set struct timeval value based on mode flag
*
* \param tv struct timeval *
* \param mode int - AP_UTILS_TIME_*
* \param msec int - milliseconds. Is it absolute value or relative depends on mode
* \return int - true/false
*
* AP_UTILS_TIME_ADD - Adds specified milliseconds to the current tv value
* AP_UTILS_TIME_SUB - Subtracts specified milliseconds from the current tv value
* AP_UTILS_TIME_SET_FROM_NOW - Adds specified milliseconds to the current time and store it in tv
* AP_UTILS_TIME_SET_FROMZERO - Sets tv value exactly to milliseconds value
*
*/
int ap_utils_timeval_set(struct timeval *tv, int mode, int msec)
{
struct timeval tmp;
if ( msec < 1 )
return 0;
switch( mode )
{
case AP_UTILS_TIME_ADD:
tmp.tv_sec = msec / 1000;
tmp.tv_usec = 1000 * (msec % 1000);
timeradd(tv, &tmp, tv);
break;
case AP_UTILS_TIME_SUB:
tmp.tv_sec = msec / 1000;
tmp.tv_usec = 1000 * (msec % 1000);
timersub(tv, &tmp, tv);
break;
case AP_UTILS_TIME_SET_FROM_NOW:
gettimeofday(tv, NULL);
tmp.tv_sec = msec / 1000;
tmp.tv_usec = 1000 * (msec % 1000);
timeradd(tv, &tmp, tv);
break;
case AP_UTILS_TIME_SET_FROMZERO:
tv->tv_sec = msec / 1000;
tv->tv_usec = 1000 * (msec % 1000);
break;
default:
return 0;
}
return 1;
}
#define MAX_NSEC 1000000000l
/*=========================================================*/
/** \brief Compares struct timespec against current time, returning result of then <=> now
*
* \param ts struct timespec *
* \return int - -1 if less than now, 0 if equal, 1 if past current time
*/
int ap_utils_timespec_cmp_to_now(struct timespec *ts)
{
struct timespec now;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
if ( ts->tv_sec > now.tv_sec )
return 1;
if ( ts->tv_sec < now.tv_sec )
return -1;
if ( ts->tv_nsec > now.tv_nsec )
return 1;
if ( ts->tv_nsec < now.tv_nsec )
return -1;
return 0;
}
/*=========================================================*/
/** \brief Set struct timespec value based on mode flag
*
* \param ts struct timespec *
* \param mode int - AP_UTILS_TIME_*
* \param msec int - milliseconds. Is it absolute value or relative depends on mode
* \return int - true/false
*
* AP_UTILS_TIME_ADD - Adds specified milliseconds to the current ts value
* AP_UTILS_TIME_SUB - Subtracts specified milliseconds from the current ts value
* AP_UTILS_TIME_SET_FROM_NOW - Adds specified milliseconds to the current time and store it in ts
* AP_UTILS_TIME_SET_FROMZERO - Sets ts value exactly to milliseconds value
*
*/
int ap_utils_timespec_set(struct timespec *ts, int mode, int msec)
{
long tmp;
if ( msec < 0 )
return 0;
switch( mode )
{
case AP_UTILS_TIME_ADD:
ts->tv_nsec += msec * 1000000l;
break;
case AP_UTILS_TIME_SUB:
ts->tv_nsec -= msec * 1000000l;
break;
case AP_UTILS_TIME_SET_FROM_NOW:
clock_gettime(CLOCK_MONOTONIC_RAW, ts);
ts->tv_nsec += msec * 1000000l;
break;
case AP_UTILS_TIME_SET_FROMZERO:
ts->tv_sec = msec / 1000;
ts->tv_nsec = MAX_NSEC * (msec % 1000l);
return 1;
default:
return 0;
}
if ( ts->tv_nsec >= MAX_NSEC )
{
tmp = ts->tv_nsec / MAX_NSEC;
ts->tv_nsec -= tmp * MAX_NSEC;
ts->tv_sec += tmp;
}
else if ( ts->tv_nsec < 0l )
{
tmp = labs(ts->tv_nsec) / MAX_NSEC;
ts->tv_nsec = MAX_NSEC + ts->tv_nsec + tmp * MAX_NSEC;
ts->tv_sec -= tmp;
}
return 1;
}
/*=========================================================*/
/** \brief Set struct timespec value to zero
*
* \param ts struct timespec *
* \return void
*/
void ap_utils_timespec_clear(struct timespec *ts)
{
ts->tv_nsec = 0l; ts->tv_sec = 0;
}
/*=========================================================*/
/** \brief Check if struct timespec value is non-zero
*
* \param ts struct timespec *
* \return int - true/false
*/
int ap_utils_timespec_is_set(struct timespec *ts)
{
return (ts->tv_sec > 0 || ts->tv_nsec > 0l);
}
/*=========================================================*/
/** \brief Like timeradd() for timespec: adds value of a to b, placing result in destination
*
* \param a struct timespec * - Operand 1
* \param b struct timespec * - Operand 2
* \param destination struct timespec * - Destination for sum a+b
* \return void
*/
void ap_utils_timespec_add(struct timespec *a, struct timespec *b, struct timespec *destination)
{
long tmp;
destination->tv_sec = a->tv_sec + b->tv_sec;
destination->tv_nsec = a->tv_nsec + b->tv_nsec;
if ( destination->tv_nsec >= MAX_NSEC )
{
tmp = destination->tv_nsec / MAX_NSEC;
destination->tv_nsec -= tmp * MAX_NSEC;
destination->tv_sec += tmp;
}
}
/*=========================================================*/
/** \brief Like timersub() for timespec: subtracts value of b from a, placing result in destination
*
* \param a struct timespec * - Minuend
* \param b struct timespec * - Subtrahend
* \param destination struct timespec * - Destination for result of a-b
* \return void
*/
void ap_utils_timespec_sub(struct timespec *a, struct timespec *b, struct timespec *destination)
{
long tmp;
destination->tv_sec = a->tv_sec - b->tv_sec;
destination->tv_nsec = a->tv_nsec - b->tv_nsec;
if ( destination->tv_nsec <= -MAX_NSEC )
{
tmp = destination->tv_nsec / MAX_NSEC;
destination->tv_nsec = destination->tv_nsec + tmp * MAX_NSEC;
destination->tv_sec += tmp;
}
}
/*=========================================================*/
/** \brief Counts elapsed time in various ranges
*
* \param begin struct timespec * - Beginning of time period. Can be NULL to take the current time instead
* \param end struct timespec * - End of time period. Can be NULL to take the current time instead
* \param destination struct timespec * - Destination for result. Can be NULL if user needs only returned milliseconds amount
* \return int Milliseconds amount in given period
*
* If *begin and *end both is not NULL then counting amount of time between begin and end.
* If begin is NULL then counting from current clock to *end
* If end is NULL then counting from *begin to current clock.
*/
long ap_utils_timespec_elapsed(struct timespec *begin, struct timespec *end, struct timespec *destination)
{
struct timespec now;
if ( begin == NULL && end == NULL )
return 0;
if ( begin == NULL )
{
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
begin = &now;
}
else if ( end == NULL )
{
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
end = &now;
}
if ( destination == NULL )
destination = &now;
ap_utils_timespec_sub(end, begin, destination);
return destination->tv_sec * 1000l + destination->tv_nsec / 1000000l;
}
/*=========================================================*/
/** \brief Converts struct timespec value to milliseconds
*
* \param ts struct timespec * - Beginning of time period. Must be set always
* \return int Milliseconds amount
*/
long ap_utils_timespec_to_milliseconds(struct timespec *ts)
{
long retval;
retval = ts->tv_sec * 1000l;
if ( ts->tv_nsec > 0l )
retval += ts->tv_nsec / 1000000l;
return retval;
}
/*=========================================================*/
/** \brief Standard CRC 16
*
* \param mem void * - Memory block to process
* \param len int - length of data
* \return uint16_t counted CRC 16
*/
uint16_t count_crc16(void *mem, int len)
{
uint16_t a, crc16;
uint8_t *pch;
pch = (uint8_t *)mem;
crc16 = 0;
while(len--)
{
crc16 ^= *pch;
a = (crc16 ^ (crc16 << 4)) & 0x00FF;
crc16 = (crc16 >> 8) ^ (a << 8) ^ (a << 3) ^ (a >> 4);
++pch;
}
return(crc16);
}