1 | /* |
---|
2 | * ptimer.c,v 1.1 1996/06/03 16:29:58 joel Exp |
---|
3 | */ |
---|
4 | |
---|
5 | #include <assert.h> |
---|
6 | #include <time.h> |
---|
7 | #include <errno.h> |
---|
8 | |
---|
9 | #include <rtems/system.h> |
---|
10 | #include <rtems/score/isr.h> |
---|
11 | #include <rtems/score/thread.h> |
---|
12 | #include <rtems/score/tod.h> |
---|
13 | |
---|
14 | #include <rtems/posix/time.h> |
---|
15 | |
---|
16 | /************************************/ |
---|
17 | /* These includes are now necessary */ |
---|
18 | /************************************/ |
---|
19 | |
---|
20 | #include <sys/features.h> |
---|
21 | #include <rtems/rtems/status.h> |
---|
22 | #include <rtems/rtems/types.h> |
---|
23 | #include <rtems/rtems/timer.h> |
---|
24 | #include <rtems/rtems/clock.h> |
---|
25 | #include <rtems/posix/psignal.h> |
---|
26 | #include <rtems/score/wkspace.h> |
---|
27 | #include <pthread.h> |
---|
28 | #include <stdio.h> |
---|
29 | #include <signal.h> |
---|
30 | |
---|
31 | /*****************************/ |
---|
32 | /* End of necessary includes */ |
---|
33 | /*****************************/ |
---|
34 | |
---|
35 | /* ************ |
---|
36 | * Constants |
---|
37 | * ************/ |
---|
38 | |
---|
39 | #define STATE_FREE_C 0x01 /* Free position of the table of timers */ |
---|
40 | #define STATE_CREATE_NEW_C 0x02 /* Created timer but not running */ |
---|
41 | #define STATE_CREATE_RUN_C 0x03 /* Created timer and running */ |
---|
42 | #define STATE_CREATE_STOP_C 0x04 /* Created, ran and stopped timer */ |
---|
43 | #define MAX_NSEC_C 1000000000 /* Maximum number of nsec allowed */ |
---|
44 | #define MIN_NSEC_C 0 /* Minimum number of nsec allowew */ |
---|
45 | #define TIMER_RELATIVE_C 0 /* Indicates that the fire time is |
---|
46 | * relative to the current one */ |
---|
47 | #define SEC_TO_TICKS_C _TOD_Ticks_per_second /* Number of ticks in a second*/ |
---|
48 | #define NSEC_PER_SEC_C 1000000000 /* Nanoseconds in a second */ |
---|
49 | |
---|
50 | #define NO_MORE_TIMERS_C 11 /* There is not available timers */ |
---|
51 | #define BAD_TIMER_C 11 /* The timer does not exist in the table */ |
---|
52 | |
---|
53 | #define SECONDS_PER_YEAR_C ( 360 * 24 * 60 * 60 ) |
---|
54 | #define SECONDS_PER_MONTH_C ( 30 * 24 * 60 * 60 ) |
---|
55 | #define SECONDS_PER_DAY_C ( 24 * 60 * 60 ) |
---|
56 | #define SECONDS_PER_HOUR_C ( 60 * 60 ) |
---|
57 | #define SECONDS_PER_MINUTE_C ( 60 ) |
---|
58 | |
---|
59 | /* |
---|
60 | #define DEBUG_MESSAGES |
---|
61 | */ |
---|
62 | |
---|
63 | /* ********************************************************* |
---|
64 | * Types that will store the created timers and their data |
---|
65 | * *********************************************************/ |
---|
66 | |
---|
67 | /* |
---|
68 | * Data for a timer |
---|
69 | */ |
---|
70 | |
---|
71 | typedef struct { |
---|
72 | pthread_t thread_id; /* Thread identifier */ |
---|
73 | char state; /* State of the timer */ |
---|
74 | struct sigevent inf; /* Information associated to the timer */ |
---|
75 | timer_t timer_id; /* Created timer identifier */ |
---|
76 | struct itimerspec timer_data; /* Timing data of the timer */ |
---|
77 | unsigned32 ticks; /* Number of ticks of the initialization */ |
---|
78 | unsigned32 overrun; /* Number of expirations of the timer */ |
---|
79 | rtems_time_of_day time; /* Time in which the timer was started */ |
---|
80 | } timer_alive_t; |
---|
81 | |
---|
82 | /* |
---|
83 | * Array of Timers |
---|
84 | */ |
---|
85 | |
---|
86 | int timer_max; |
---|
87 | timer_alive_t *timer_struct; |
---|
88 | |
---|
89 | /* |
---|
90 | * Data for the signals |
---|
91 | */ |
---|
92 | |
---|
93 | struct sigaction signal_inf[SIGRTMAX]; |
---|
94 | |
---|
95 | /*********************************** |
---|
96 | * Definition of Internal Functions |
---|
97 | ***********************************/ |
---|
98 | |
---|
99 | /* *************************************************************************** |
---|
100 | * PRINT_MSG_S |
---|
101 | * |
---|
102 | * Description: This function write a message in the display. |
---|
103 | * It is used for debugging and all the calls must be deleted |
---|
104 | * when the tests finish |
---|
105 | * ***************************************************************************/ |
---|
106 | |
---|
107 | static void PRINT_MSG_S ( char *msg ) |
---|
108 | { |
---|
109 | |
---|
110 | #ifdef DEBUG_MESSAGES |
---|
111 | printf("%s\n", msg); |
---|
112 | #endif |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | /* *************************************************************************** |
---|
117 | * PRINT_ERRNO_S |
---|
118 | * |
---|
119 | * Description: Print the value of the global variable errno in the display |
---|
120 | * ***************************************************************************/ |
---|
121 | |
---|
122 | static void PRINT_ERRNO_S () |
---|
123 | { |
---|
124 | #ifdef DEBUG_MESSAGES |
---|
125 | switch (errno) |
---|
126 | { |
---|
127 | case EINVAL: |
---|
128 | PRINT_MSG_S ( "errno EINVAL"); break; |
---|
129 | case EPERM: |
---|
130 | PRINT_MSG_S ( "errno EPERM"); break; |
---|
131 | case ESRCH: |
---|
132 | PRINT_MSG_S ( "errno ESRCH"); break; |
---|
133 | case EAGAIN: |
---|
134 | PRINT_MSG_S ( "errno EAGAIN"); break; |
---|
135 | default : |
---|
136 | printf ("errno: %d\n", errno); |
---|
137 | break; |
---|
138 | } |
---|
139 | #endif |
---|
140 | } |
---|
141 | |
---|
142 | /* *************************************************************************** |
---|
143 | * TIMER_INITIALIZE_S |
---|
144 | * |
---|
145 | * Description: Initialize the data of a timer |
---|
146 | * ***************************************************************************/ |
---|
147 | |
---|
148 | void TIMER_INITIALIZE_S ( int timer_pos ) |
---|
149 | { |
---|
150 | |
---|
151 | /* |
---|
152 | * Indicates that the position in the table is free |
---|
153 | */ |
---|
154 | |
---|
155 | timer_struct[timer_pos].state = STATE_FREE_C; |
---|
156 | |
---|
157 | /* |
---|
158 | * The initial data of timing are set with null value |
---|
159 | */ |
---|
160 | |
---|
161 | timer_struct[timer_pos].timer_data.it_value.tv_sec = 0; |
---|
162 | timer_struct[timer_pos].timer_data.it_value.tv_nsec = 0; |
---|
163 | timer_struct[timer_pos].timer_data.it_interval.tv_sec = 0; |
---|
164 | timer_struct[timer_pos].timer_data.it_interval.tv_nsec = 0; |
---|
165 | |
---|
166 | /* |
---|
167 | * The count of expirations is 0 |
---|
168 | */ |
---|
169 | |
---|
170 | timer_struct[timer_pos].overrun = 0; |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | /* *************************************************************************** |
---|
175 | * _POSIX_Timer_Manager_initialization |
---|
176 | * |
---|
177 | * Description: Initialize the internal structure in which the data of all |
---|
178 | * the timers are stored |
---|
179 | * ***************************************************************************/ |
---|
180 | |
---|
181 | void _POSIX_Timer_Manager_initialization ( int max_timers ) |
---|
182 | { |
---|
183 | int index; |
---|
184 | |
---|
185 | timer_struct = _Workspace_Allocate_or_fatal_error( |
---|
186 | max_timers * sizeof(timer_alive_t) ); |
---|
187 | |
---|
188 | /* |
---|
189 | * Initialize all the timers |
---|
190 | */ |
---|
191 | |
---|
192 | for (index=0; index<max_timers; index++) |
---|
193 | TIMER_INITIALIZE_S( index ); |
---|
194 | |
---|
195 | timer_max = max_timers; |
---|
196 | } |
---|
197 | |
---|
198 | /* *************************************************************************** |
---|
199 | * FIRST_FREE_POSITION_F |
---|
200 | * |
---|
201 | * Description: Returns the first free position in the table of timers. |
---|
202 | * If there is not a free position, it returns NO_MORE_TIMERS_C |
---|
203 | * ***************************************************************************/ |
---|
204 | |
---|
205 | int FIRST_FREE_POSITION_F () |
---|
206 | { |
---|
207 | int index; |
---|
208 | |
---|
209 | for (index=0; index<timer_max; index++) { |
---|
210 | if ( timer_struct[index].state == STATE_FREE_C ) { |
---|
211 | return index; |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | /* The function reaches this point only if all the position are occupied */ |
---|
216 | |
---|
217 | return NO_MORE_TIMERS_C; |
---|
218 | } |
---|
219 | |
---|
220 | /* *************************************************************************** |
---|
221 | * TIMER_POSITION_F |
---|
222 | * |
---|
223 | * Description: Returns the position in the table of timers in which the |
---|
224 | * data of the timer are stored. |
---|
225 | * If the timer identifier does not exist, it returns |
---|
226 | * BAD_TIMER_C |
---|
227 | * ***************************************************************************/ |
---|
228 | |
---|
229 | int TIMER_POSITION_F ( timer_t timer_id ) |
---|
230 | { |
---|
231 | int index; |
---|
232 | |
---|
233 | for (index=0; index<timer_max; index++ ) { |
---|
234 | |
---|
235 | /* Looks for the position of the timer. The timer must exist and the |
---|
236 | * position can not be free */ |
---|
237 | if ( ( timer_struct[index].timer_id == timer_id ) && |
---|
238 | ( timer_struct[index].state != STATE_FREE_C ) ) { |
---|
239 | return index; |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | /* If the function reaches this point is because the timer identifier |
---|
244 | * is not correct */ |
---|
245 | |
---|
246 | return BAD_TIMER_C; |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | /* *************************************************************************** |
---|
251 | * COPY_ITIMERSPEC_S |
---|
252 | * |
---|
253 | * Description: Does a copy of a variable of type struct itimerspec |
---|
254 | * ***************************************************************************/ |
---|
255 | |
---|
256 | void COPY_ITIMERSPEC_S ( const struct itimerspec *source, |
---|
257 | struct itimerspec *target ) |
---|
258 | { |
---|
259 | |
---|
260 | target->it_value.tv_sec = source->it_value.tv_sec; |
---|
261 | target->it_value.tv_nsec = source->it_value.tv_nsec; |
---|
262 | target->it_interval.tv_sec = source->it_interval.tv_sec; |
---|
263 | target->it_interval.tv_nsec = source->it_interval.tv_nsec; |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | /* *************************************************************************** |
---|
268 | * ITIMERSPEC_TO_RTEMS_TIME_OF_DAY_S |
---|
269 | * |
---|
270 | * Description: This function converts the data of a structure itimerspec |
---|
271 | * into structure rtems_time_of_day |
---|
272 | * ***************************************************************************/ |
---|
273 | |
---|
274 | void ITIMERSPEC_TO_RTEMS_TIME_OF_DAY_S |
---|
275 | ( const struct itimerspec *itimer, rtems_time_of_day *rtems_time ) |
---|
276 | { |
---|
277 | unsigned long int seconds; |
---|
278 | |
---|
279 | /* The leap years and the months with 28, 29 or 31 days have not been |
---|
280 | * considerated. It will be made in the future */ |
---|
281 | |
---|
282 | seconds = itimer->it_value.tv_sec; |
---|
283 | |
---|
284 | rtems_time->year = seconds / SECONDS_PER_YEAR_C; |
---|
285 | seconds = seconds % SECONDS_PER_YEAR_C; |
---|
286 | |
---|
287 | rtems_time->month = seconds / SECONDS_PER_MONTH_C; |
---|
288 | seconds = seconds % SECONDS_PER_MONTH_C; |
---|
289 | |
---|
290 | rtems_time->day = seconds / SECONDS_PER_DAY_C; |
---|
291 | seconds = seconds % SECONDS_PER_DAY_C; |
---|
292 | |
---|
293 | rtems_time->hour = seconds / SECONDS_PER_HOUR_C; |
---|
294 | seconds = seconds % SECONDS_PER_HOUR_C; |
---|
295 | |
---|
296 | rtems_time->minute = seconds / SECONDS_PER_MINUTE_C; |
---|
297 | seconds = seconds % SECONDS_PER_MINUTE_C; |
---|
298 | |
---|
299 | rtems_time->second = seconds; |
---|
300 | |
---|
301 | rtems_time->ticks = ( itimer->it_value.tv_nsec * SEC_TO_TICKS_C ) / |
---|
302 | NSEC_PER_SEC_C; |
---|
303 | |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | /* *************************************************************************** |
---|
308 | * FIRE_TIMER_S |
---|
309 | * |
---|
310 | * Description: This is the operation that is ran when a timer expires |
---|
311 | * ***************************************************************************/ |
---|
312 | |
---|
313 | |
---|
314 | rtems_timer_service_routine FIRE_TIMER_S (rtems_id timer, void *data) |
---|
315 | { |
---|
316 | int timer_pos; /* Position in the table of the timer that |
---|
317 | * has expirated */ |
---|
318 | rtems_status_code return_v; /* Return value of rtems_timer_fire_after */ |
---|
319 | int sig_number; /* Number of the signal to send */ |
---|
320 | |
---|
321 | |
---|
322 | /* The position of the table of timers that contains the data of the |
---|
323 | * expired timer will be stored in "timer_pos". In theory a timer can not |
---|
324 | * expire if it has not been created or has been deleted */ |
---|
325 | |
---|
326 | PRINT_MSG_S ("FIRE_TIMER_S"); |
---|
327 | |
---|
328 | timer_pos = TIMER_POSITION_F(timer); |
---|
329 | |
---|
330 | /* Increases the number of expiration of the timer in one unit. */ |
---|
331 | timer_struct[timer_pos].overrun = timer_struct[timer_pos].overrun + 1; |
---|
332 | |
---|
333 | |
---|
334 | if ( ( timer_struct[timer_pos].timer_data.it_interval.tv_sec != 0 ) || |
---|
335 | ( timer_struct[timer_pos].timer_data.it_interval.tv_nsec != 0 ) ) { |
---|
336 | |
---|
337 | /* The timer must be reprogrammed */ |
---|
338 | |
---|
339 | return_v = rtems_timer_fire_after ( timer, |
---|
340 | timer_struct[timer_pos].ticks, |
---|
341 | FIRE_TIMER_S, |
---|
342 | NULL ); |
---|
343 | |
---|
344 | /* Stores the time when the timer was started again */ |
---|
345 | |
---|
346 | return_v = rtems_clock_get ( RTEMS_CLOCK_GET_TOD, |
---|
347 | &timer_struct[timer_pos].time ); |
---|
348 | |
---|
349 | /* The state has not to be actualized, because nothing modifies it */ |
---|
350 | |
---|
351 | timer_struct[timer_pos].state = STATE_CREATE_RUN_C; |
---|
352 | |
---|
353 | } else { |
---|
354 | /* Indicates that the timer is stopped */ |
---|
355 | |
---|
356 | timer_struct[timer_pos].state = STATE_CREATE_STOP_C; |
---|
357 | |
---|
358 | } |
---|
359 | |
---|
360 | /* |
---|
361 | * The sending of the signal to the process running the handling function |
---|
362 | * specified for that signal is simulated |
---|
363 | */ |
---|
364 | |
---|
365 | sig_number = timer_struct[timer_pos].inf.sigev_signo; |
---|
366 | |
---|
367 | if( pthread_kill ( timer_struct[timer_pos].thread_id , |
---|
368 | timer_struct[timer_pos].inf.sigev_signo ) ) { |
---|
369 | PRINT_MSG_S ("ERROR_PTHREAD_KILL"); |
---|
370 | } else { |
---|
371 | PRINT_MSG_S ("SUCCESS_PTHREAD_KILL"); |
---|
372 | } |
---|
373 | |
---|
374 | /* |
---|
375 | * After the signal handler returns, the count of expirations of the |
---|
376 | * timer must be set to 0. |
---|
377 | */ |
---|
378 | |
---|
379 | timer_struct[timer_pos].overrun = 0; |
---|
380 | |
---|
381 | } |
---|
382 | |
---|
383 | /* ********************************************************************* |
---|
384 | * 14.2.2 Create a Per-Process Timer, P1003.1b-1993, p. 264 |
---|
385 | * ********************************************************************/ |
---|
386 | |
---|
387 | /* ************** |
---|
388 | * timer_create |
---|
389 | * **************/ |
---|
390 | |
---|
391 | int timer_create( |
---|
392 | clockid_t clock_id, |
---|
393 | struct sigevent *evp, |
---|
394 | timer_t *timerid |
---|
395 | ) |
---|
396 | { |
---|
397 | |
---|
398 | rtems_status_code return_v; /* return value of the operation */ |
---|
399 | rtems_id timer_id; /* created timer identifier */ |
---|
400 | int timer_pos; /* Position in the table of timers */ |
---|
401 | |
---|
402 | /* |
---|
403 | * The data of the structure evp are checked in order to verify if they |
---|
404 | * are coherent. |
---|
405 | */ |
---|
406 | |
---|
407 | if (evp != NULL) { |
---|
408 | /* The structure has data */ |
---|
409 | |
---|
410 | if ( ( evp->sigev_notify != SIGEV_NONE ) && |
---|
411 | ( evp->sigev_notify != SIGEV_SIGNAL ) ) { |
---|
412 | /* The value of the field sigev_notify is not valid */ |
---|
413 | |
---|
414 | return (-1); |
---|
415 | |
---|
416 | } |
---|
417 | } |
---|
418 | |
---|
419 | /* |
---|
420 | * A timer is created using the primitive rtems_timer_create |
---|
421 | */ |
---|
422 | |
---|
423 | return_v = rtems_timer_create ( clock_id, &timer_id ); |
---|
424 | |
---|
425 | switch (return_v) { |
---|
426 | case RTEMS_SUCCESSFUL : |
---|
427 | |
---|
428 | PRINT_MSG_S("SUCCESS: rtems create timer RTEMS_SUCCESSFUL"); |
---|
429 | |
---|
430 | /* |
---|
431 | * The timer has been created properly |
---|
432 | */ |
---|
433 | |
---|
434 | /* Obtains the first free position in the table of timers */ |
---|
435 | |
---|
436 | timer_pos = FIRST_FREE_POSITION_F(); |
---|
437 | |
---|
438 | if ( timer_pos == NO_MORE_TIMERS_C ) { |
---|
439 | /* There is not position for another timers in spite of RTEMS |
---|
440 | * supports it. It will necessaty to increase the structure used */ |
---|
441 | |
---|
442 | errno = EAGAIN; |
---|
443 | |
---|
444 | return -1; |
---|
445 | } |
---|
446 | |
---|
447 | /* Exit parameter */ |
---|
448 | |
---|
449 | *timerid = timer_id; |
---|
450 | |
---|
451 | /* The data of the created timer are stored to use them later */ |
---|
452 | |
---|
453 | timer_struct[timer_pos].state = STATE_CREATE_NEW_C; |
---|
454 | |
---|
455 | /* NEW VERSION*/ |
---|
456 | timer_struct[timer_pos].thread_id = pthread_self (); |
---|
457 | |
---|
458 | |
---|
459 | if ( evp != NULL ) { |
---|
460 | |
---|
461 | timer_struct[timer_pos].inf.sigev_notify = evp->sigev_notify; |
---|
462 | timer_struct[timer_pos].inf.sigev_signo = evp->sigev_signo; |
---|
463 | timer_struct[timer_pos].inf.sigev_value = evp->sigev_value; |
---|
464 | |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | timer_struct[timer_pos].timer_id = timer_id; |
---|
469 | |
---|
470 | timer_struct[timer_pos].overrun = 0; |
---|
471 | |
---|
472 | timer_struct[timer_pos].timer_data.it_value.tv_sec = 0; |
---|
473 | timer_struct[timer_pos].timer_data.it_value.tv_nsec = 0; |
---|
474 | timer_struct[timer_pos].timer_data.it_interval.tv_sec = 0; |
---|
475 | timer_struct[timer_pos].timer_data.it_interval.tv_nsec = 0; |
---|
476 | |
---|
477 | return 0; |
---|
478 | |
---|
479 | |
---|
480 | case RTEMS_INVALID_NAME : /* The assigned name is not valid*/ |
---|
481 | |
---|
482 | PRINT_MSG_S ("ERROR: rtems create timer RTEMS_INVALID_NAME"); |
---|
483 | |
---|
484 | errno = EINVAL; |
---|
485 | |
---|
486 | return (-1); |
---|
487 | |
---|
488 | |
---|
489 | case RTEMS_TOO_MANY : |
---|
490 | |
---|
491 | PRINT_MSG_S ("ERROR: rtems create timer RTEMS_TOO_MANY "); |
---|
492 | |
---|
493 | /* There has been created too much timers for the same process */ |
---|
494 | |
---|
495 | errno = EAGAIN; |
---|
496 | |
---|
497 | return (-1); |
---|
498 | |
---|
499 | default : |
---|
500 | |
---|
501 | /* |
---|
502 | * Does nothing. It only returns the error without assigning a value |
---|
503 | * to errno. In theory, it can not happen because the call to |
---|
504 | * rtems_timer_create can not return other different value. |
---|
505 | */ |
---|
506 | |
---|
507 | return (-1); |
---|
508 | |
---|
509 | } |
---|
510 | |
---|
511 | /* |
---|
512 | * The next sentence is used to avoid singular situations |
---|
513 | */ |
---|
514 | |
---|
515 | return (-1); |
---|
516 | |
---|
517 | } |
---|
518 | |
---|
519 | /* |
---|
520 | * 14.2.3 Delete a Per_process Timer, P1003.1b-1993, p. 266 |
---|
521 | */ |
---|
522 | |
---|
523 | int timer_delete( |
---|
524 | timer_t timerid |
---|
525 | ) |
---|
526 | { |
---|
527 | |
---|
528 | /* |
---|
529 | * IDEA: This function must probably stop the timer first and then delete it |
---|
530 | * |
---|
531 | * It will have to do a call to rtems_timer_cancel and then another |
---|
532 | * call to rtems_timer_delete. |
---|
533 | * The call to rtems_timer_delete will be probably unnecessary, |
---|
534 | * because rtems_timer_delete stops the timer before deleting it. |
---|
535 | */ |
---|
536 | |
---|
537 | int timer_pos; |
---|
538 | rtems_status_code status; |
---|
539 | |
---|
540 | |
---|
541 | /* First the position in the table of timers is obtained */ |
---|
542 | |
---|
543 | timer_pos = TIMER_POSITION_F ( timerid ); |
---|
544 | |
---|
545 | if ( timer_pos == BAD_TIMER_C ) { |
---|
546 | /* The timer identifier is erroneus */ |
---|
547 | |
---|
548 | errno = EINVAL; |
---|
549 | return -1; |
---|
550 | } |
---|
551 | |
---|
552 | /* The timer is deleted */ |
---|
553 | |
---|
554 | status = rtems_timer_delete ( timerid ); |
---|
555 | |
---|
556 | if ( status == RTEMS_INVALID_ID ) { |
---|
557 | /* The timer identifier is erroneus */ |
---|
558 | |
---|
559 | errno = EINVAL; |
---|
560 | return -1; |
---|
561 | } |
---|
562 | |
---|
563 | /* Initializes the data of the timer */ |
---|
564 | |
---|
565 | TIMER_INITIALIZE_S ( timer_pos ); |
---|
566 | |
---|
567 | return 0; |
---|
568 | } |
---|
569 | |
---|
570 | /* |
---|
571 | * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267 |
---|
572 | */ |
---|
573 | |
---|
574 | /* ************** |
---|
575 | * timer_settime |
---|
576 | * **************/ |
---|
577 | |
---|
578 | |
---|
579 | int timer_settime( |
---|
580 | timer_t timerid, |
---|
581 | int flags, |
---|
582 | const struct itimerspec *value, |
---|
583 | struct itimerspec *ovalue |
---|
584 | ) |
---|
585 | { |
---|
586 | |
---|
587 | rtems_status_code return_v; /* Return of the calls to RTEMS */ |
---|
588 | int timer_pos; /* Position of the timer in the table */ |
---|
589 | rtems_time_of_day rtems_time; /* Time in RTEMS */ |
---|
590 | |
---|
591 | /* First the position in the table of timers is obtained */ |
---|
592 | |
---|
593 | timer_pos = TIMER_POSITION_F ( timerid ); |
---|
594 | |
---|
595 | if ( timer_pos == BAD_TIMER_C ) { |
---|
596 | /* The timer identifier is erroneus */ |
---|
597 | |
---|
598 | errno = EINVAL; |
---|
599 | return -1; |
---|
600 | } |
---|
601 | |
---|
602 | if ( value == NULL ) { |
---|
603 | /* The stucture of times of the timer is free, and then returns an |
---|
604 | error but the variable errno is not actualized */ |
---|
605 | |
---|
606 | /* errno = ?????? */ |
---|
607 | |
---|
608 | return -1; |
---|
609 | } |
---|
610 | |
---|
611 | /* If the function reaches this point, then it will be necessary to do |
---|
612 | * something with the structure of times of the timer: to stop, start |
---|
613 | * or start it again */ |
---|
614 | |
---|
615 | /* First, it verifies if the timer must be stopped */ |
---|
616 | |
---|
617 | if ( value->it_value.tv_sec == 0 && value->it_value.tv_nsec == 0 ) { |
---|
618 | /* The timer is stopped */ |
---|
619 | |
---|
620 | return_v = rtems_timer_cancel ( timerid ); |
---|
621 | |
---|
622 | /* The old data of the timer are returned */ |
---|
623 | |
---|
624 | COPY_ITIMERSPEC_S ( &timer_struct[timer_pos].timer_data, ovalue ); |
---|
625 | |
---|
626 | /* The new data are set */ |
---|
627 | |
---|
628 | COPY_ITIMERSPEC_S ( value, &timer_struct[timer_pos].timer_data ); |
---|
629 | |
---|
630 | /* Indicates that the timer is created and stopped */ |
---|
631 | |
---|
632 | timer_struct[timer_pos].state = STATE_CREATE_STOP_C; |
---|
633 | |
---|
634 | /* Returns with success */ |
---|
635 | |
---|
636 | return 0; |
---|
637 | } |
---|
638 | |
---|
639 | /* |
---|
640 | * If the function reaches this point, then the timer will have to be |
---|
641 | * initialized with new values: to start it or start it again |
---|
642 | */ |
---|
643 | |
---|
644 | /* First, it verifies if the structure "value" is correct */ |
---|
645 | |
---|
646 | if ( ( value->it_value.tv_nsec > MAX_NSEC_C ) || |
---|
647 | ( value->it_value.tv_nsec < MIN_NSEC_C ) ) { |
---|
648 | /* The number of nanoseconds is not correct */ |
---|
649 | |
---|
650 | errno = EINVAL; |
---|
651 | |
---|
652 | return -1; |
---|
653 | } |
---|
654 | |
---|
655 | /* Then, "value" must be converted from seconds and nanoseconds to clock |
---|
656 | * ticks, to use it in the calls to RTEMS */ |
---|
657 | |
---|
658 | /* It is also necessary to take in account if the time is absolute |
---|
659 | * or relative */ |
---|
660 | |
---|
661 | switch (flags) { |
---|
662 | case TIMER_ABSTIME: |
---|
663 | |
---|
664 | /* The fire time is absolute: |
---|
665 | * It has to use "rtems_time_fire_when" */ |
---|
666 | |
---|
667 | /* First, it converts from struct itimerspec to rtems_time_of_day */ |
---|
668 | |
---|
669 | ITIMERSPEC_TO_RTEMS_TIME_OF_DAY_S ( value, &rtems_time ); |
---|
670 | |
---|
671 | return_v = rtems_timer_fire_when ( timerid, &rtems_time, FIRE_TIMER_S, NULL); |
---|
672 | |
---|
673 | switch ( return_v ) { |
---|
674 | case RTEMS_SUCCESSFUL: |
---|
675 | |
---|
676 | PRINT_MSG_S ("SUCCESS: timer_settime RTEMS_SUCCESSFUL"); |
---|
677 | |
---|
678 | /* The timer has been started and is running */ |
---|
679 | |
---|
680 | /* Actualizes the data of the structure and |
---|
681 | * returns the old ones in "ovalue" */ |
---|
682 | |
---|
683 | COPY_ITIMERSPEC_S ( &timer_struct[timer_pos].timer_data, ovalue ); |
---|
684 | |
---|
685 | COPY_ITIMERSPEC_S ( value, &timer_struct[timer_pos].timer_data ); |
---|
686 | |
---|
687 | /* It indicates that the time is running */ |
---|
688 | |
---|
689 | timer_struct[timer_pos].state = STATE_CREATE_RUN_C; |
---|
690 | |
---|
691 | /* Stores the time in which the timer was started again */ |
---|
692 | |
---|
693 | return_v = rtems_clock_get ( RTEMS_CLOCK_GET_TOD, |
---|
694 | &timer_struct[timer_pos].time ); |
---|
695 | |
---|
696 | return 0; |
---|
697 | |
---|
698 | break; |
---|
699 | |
---|
700 | case RTEMS_INVALID_ID: |
---|
701 | |
---|
702 | PRINT_MSG_S ("ERROR: timer_settime RTEMS_INVALID_ID"); |
---|
703 | break; |
---|
704 | |
---|
705 | case RTEMS_NOT_DEFINED: |
---|
706 | |
---|
707 | PRINT_MSG_S ("ERROR: timer_settime RTEMS_NOT_DEFINED"); |
---|
708 | break; |
---|
709 | |
---|
710 | case RTEMS_INVALID_CLOCK: |
---|
711 | |
---|
712 | PRINT_MSG_S ("ERROR: timer_settime RTEMS_INVALID_CLOCK"); |
---|
713 | break; |
---|
714 | |
---|
715 | default: |
---|
716 | |
---|
717 | |
---|
718 | } |
---|
719 | |
---|
720 | break; |
---|
721 | |
---|
722 | case TIMER_RELATIVE_C: |
---|
723 | |
---|
724 | /* The fire time is relative: |
---|
725 | * It has to use "rtems_time_fire_after" */ |
---|
726 | |
---|
727 | /* First, it converts from seconds and nanoseconds to ticks */ |
---|
728 | |
---|
729 | /* The form in which this operation is done can produce a lost |
---|
730 | * of precision of 1 second */ |
---|
731 | |
---|
732 | /* This is the process to convert from nanoseconds to ticks |
---|
733 | * |
---|
734 | * There is a tick every 10 miliseconds, then the nanoseconds are |
---|
735 | * divided between 10**7. The result of this operation will be the |
---|
736 | * number of ticks |
---|
737 | */ |
---|
738 | |
---|
739 | timer_struct[timer_pos].ticks = |
---|
740 | ( SEC_TO_TICKS_C * value->it_value.tv_sec ) + |
---|
741 | ( value->it_value.tv_nsec / ( 1000 * 1000 * 10 ) ); |
---|
742 | |
---|
743 | return_v = rtems_timer_fire_after ( timerid, |
---|
744 | timer_struct[timer_pos].ticks, |
---|
745 | FIRE_TIMER_S, |
---|
746 | NULL ); |
---|
747 | |
---|
748 | switch (return_v) { |
---|
749 | case RTEMS_SUCCESSFUL: |
---|
750 | |
---|
751 | PRINT_MSG_S ( "SUCCESS: timer_settime RTEMS_SUCCESSFUL"); |
---|
752 | |
---|
753 | /* The timer has been started and is running */ |
---|
754 | |
---|
755 | /* Actualizes the data of the structure and |
---|
756 | * returns the old ones in "ovalue" */ |
---|
757 | |
---|
758 | COPY_ITIMERSPEC_S ( &timer_struct[timer_pos].timer_data, ovalue ); |
---|
759 | |
---|
760 | COPY_ITIMERSPEC_S ( value, &timer_struct[timer_pos].timer_data ); |
---|
761 | |
---|
762 | /* It indicates that the time is running */ |
---|
763 | |
---|
764 | timer_struct[timer_pos].state = STATE_CREATE_RUN_C; |
---|
765 | |
---|
766 | /* Stores the time in which the timer was started again */ |
---|
767 | |
---|
768 | return_v = rtems_clock_get ( RTEMS_CLOCK_GET_TOD, |
---|
769 | &timer_struct[timer_pos].time ); |
---|
770 | |
---|
771 | return 0; |
---|
772 | |
---|
773 | break; |
---|
774 | |
---|
775 | case RTEMS_INVALID_ID: |
---|
776 | |
---|
777 | PRINT_MSG_S ( "ERROR: timer_settime RTEMS_INVALID_ID"); |
---|
778 | |
---|
779 | /* The timer identifier is not correct. In theory, this |
---|
780 | * situation can not occur, but the solution is easy */ |
---|
781 | |
---|
782 | errno = EINVAL; |
---|
783 | |
---|
784 | return -1; |
---|
785 | |
---|
786 | break; |
---|
787 | |
---|
788 | case RTEMS_INVALID_NUMBER: |
---|
789 | |
---|
790 | PRINT_MSG_S ( "ERROR: timer_settime RTEMS_INVALID_NUMBER"); |
---|
791 | |
---|
792 | /* In this case, RTEMS fails because the values of timing |
---|
793 | * are incorrect */ |
---|
794 | |
---|
795 | /* |
---|
796 | * I do not know if errno must be actualized |
---|
797 | * |
---|
798 | * errno = EINVAL; |
---|
799 | */ |
---|
800 | |
---|
801 | return -1; |
---|
802 | |
---|
803 | break; |
---|
804 | |
---|
805 | default: |
---|
806 | } |
---|
807 | |
---|
808 | break; |
---|
809 | |
---|
810 | default: |
---|
811 | |
---|
812 | /* It does nothing, although it will be probably necessary to |
---|
813 | * return an error */ |
---|
814 | |
---|
815 | } |
---|
816 | |
---|
817 | /* To avoid problems */ |
---|
818 | |
---|
819 | return 0; |
---|
820 | |
---|
821 | } |
---|
822 | |
---|
823 | |
---|
824 | /* |
---|
825 | * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267 |
---|
826 | */ |
---|
827 | |
---|
828 | /* ************** |
---|
829 | * timer_gettime |
---|
830 | * **************/ |
---|
831 | |
---|
832 | int timer_gettime( |
---|
833 | timer_t timerid, |
---|
834 | struct itimerspec *value |
---|
835 | ) |
---|
836 | { |
---|
837 | |
---|
838 | /* |
---|
839 | * IDEA: This function does not use functions of RTEMS to the handle |
---|
840 | * of timers. It uses some functions for managing the time. |
---|
841 | * |
---|
842 | * A possible form to do this is the following: |
---|
843 | * |
---|
844 | * - When a timer is initialized, the value of the time in |
---|
845 | * that moment is stored. |
---|
846 | * - When this function is called, it returns the difference |
---|
847 | * between the current time and the initialization time. |
---|
848 | */ |
---|
849 | |
---|
850 | rtems_time_of_day current_time; |
---|
851 | rtems_status_code return_v; |
---|
852 | int timer_pos; |
---|
853 | unsigned32 hours; |
---|
854 | unsigned32 minutes; |
---|
855 | unsigned32 seconds; |
---|
856 | unsigned32 ticks; |
---|
857 | unsigned32 nanosec; |
---|
858 | |
---|
859 | |
---|
860 | /* Reads the current time */ |
---|
861 | |
---|
862 | return_v = rtems_clock_get ( RTEMS_CLOCK_GET_TOD, ¤t_time ); |
---|
863 | |
---|
864 | timer_pos = TIMER_POSITION_F ( timerid ); |
---|
865 | |
---|
866 | if ( timer_pos == BAD_TIMER_C ) { |
---|
867 | |
---|
868 | /* The timer identifier is erroneus */ |
---|
869 | |
---|
870 | errno = EINVAL; |
---|
871 | |
---|
872 | return (-1); |
---|
873 | |
---|
874 | } |
---|
875 | |
---|
876 | /* Calculates the difference between the start time of the timer and |
---|
877 | * the current one */ |
---|
878 | |
---|
879 | hours = current_time.hour - timer_struct[timer_pos].time.hour; |
---|
880 | |
---|
881 | if ( current_time.minute < timer_struct[timer_pos].time.minute ) { |
---|
882 | minutes = 60 - timer_struct[timer_pos].time.minute + current_time.minute; |
---|
883 | hours--; |
---|
884 | } else { |
---|
885 | minutes = current_time.minute - timer_struct[timer_pos].time.minute; |
---|
886 | } |
---|
887 | |
---|
888 | if ( current_time.second < timer_struct[timer_pos].time.second ) { |
---|
889 | seconds = 60 - timer_struct[timer_pos].time.second + current_time.second; |
---|
890 | minutes--; |
---|
891 | } else { |
---|
892 | seconds = current_time.second - timer_struct[timer_pos].time.second; |
---|
893 | } |
---|
894 | |
---|
895 | if ( current_time.ticks < timer_struct[timer_pos].time.ticks ) { |
---|
896 | ticks = 100 - timer_struct[timer_pos].time.ticks + current_time.ticks; |
---|
897 | seconds--; |
---|
898 | } else { |
---|
899 | ticks = current_time.ticks - timer_struct[timer_pos].time.ticks; |
---|
900 | } |
---|
901 | |
---|
902 | /* The time that the timer is running is calculated */ |
---|
903 | seconds = hours * 60 * 60 + |
---|
904 | minutes * 60 + |
---|
905 | seconds; |
---|
906 | |
---|
907 | nanosec = ticks * 10 * /* msec */ |
---|
908 | 1000 * /* microsec */ |
---|
909 | 1000; /* nanosec */ |
---|
910 | |
---|
911 | |
---|
912 | /* Calculates the time left before the timer finishes */ |
---|
913 | |
---|
914 | value->it_value.tv_sec = |
---|
915 | timer_struct[timer_pos].timer_data.it_value.tv_sec - seconds; |
---|
916 | |
---|
917 | value->it_value.tv_nsec = |
---|
918 | timer_struct[timer_pos].timer_data.it_value.tv_nsec - nanosec; |
---|
919 | |
---|
920 | |
---|
921 | value->it_interval.tv_sec = |
---|
922 | timer_struct[timer_pos].timer_data.it_interval.tv_sec; |
---|
923 | value->it_interval.tv_nsec = |
---|
924 | timer_struct[timer_pos].timer_data.it_interval.tv_nsec; |
---|
925 | |
---|
926 | |
---|
927 | return 0; |
---|
928 | |
---|
929 | } |
---|
930 | |
---|
931 | /* |
---|
932 | * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267 |
---|
933 | */ |
---|
934 | |
---|
935 | /* ***************** |
---|
936 | * timer_getoverrun |
---|
937 | * *****************/ |
---|
938 | |
---|
939 | int timer_getoverrun( |
---|
940 | timer_t timerid |
---|
941 | ) |
---|
942 | { |
---|
943 | |
---|
944 | /* |
---|
945 | * IDEA: This function must count the times the timer expires. |
---|
946 | * |
---|
947 | * The expiration of a timer must increase by one a counter. |
---|
948 | * After the signal handler associated to the timer finishs |
---|
949 | * its execution, FIRE_TIMER_S will have to set this counter to 0. |
---|
950 | */ |
---|
951 | |
---|
952 | int timer_pos; /* Position of the timer in the structure */ |
---|
953 | int overrun; /* Overflow count */ |
---|
954 | |
---|
955 | |
---|
956 | timer_pos = TIMER_POSITION_F ( timerid ); |
---|
957 | |
---|
958 | if ( timer_pos == BAD_TIMER_C ) { |
---|
959 | /* The timer identifier is erroneus */ |
---|
960 | |
---|
961 | errno = EINVAL; |
---|
962 | |
---|
963 | return -1; |
---|
964 | } |
---|
965 | |
---|
966 | /* The overflow count of the timer is stored in "overrun" */ |
---|
967 | |
---|
968 | overrun = timer_struct[timer_pos].overrun; |
---|
969 | |
---|
970 | /* It is set to 0 */ |
---|
971 | |
---|
972 | timer_struct[timer_pos].overrun = 0; |
---|
973 | |
---|
974 | return overrun; |
---|
975 | |
---|
976 | } |
---|