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