1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief Function Arms or Disarms the Timer Identified by timerid |
---|
5 | * @ingroup POSIXAPI |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * 14.2.4 Per-Process Timers, P1003.1b-1993, p. 267 |
---|
10 | * |
---|
11 | * COPYRIGHT (c) 1989-2008. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * The license and distribution terms for this file may be |
---|
15 | * found in the file LICENSE in this distribution or at |
---|
16 | * http://www.rtems.com/license/LICENSE. |
---|
17 | */ |
---|
18 | |
---|
19 | #if HAVE_CONFIG_H |
---|
20 | #include "config.h" |
---|
21 | #endif |
---|
22 | |
---|
23 | #include <time.h> |
---|
24 | #include <errno.h> |
---|
25 | |
---|
26 | #include <rtems/system.h> |
---|
27 | #include <rtems/seterr.h> |
---|
28 | #include <rtems/score/thread.h> |
---|
29 | #include <rtems/score/tod.h> |
---|
30 | #include <rtems/posix/time.h> |
---|
31 | #include <rtems/posix/ptimer.h> |
---|
32 | #include <rtems/posix/timer.h> |
---|
33 | |
---|
34 | int timer_settime( |
---|
35 | timer_t timerid, |
---|
36 | int flags, |
---|
37 | const struct itimerspec *value, |
---|
38 | struct itimerspec *ovalue |
---|
39 | ) |
---|
40 | { |
---|
41 | POSIX_Timer_Control *ptimer; |
---|
42 | Objects_Locations location; |
---|
43 | bool activated; |
---|
44 | uint32_t initial_period; |
---|
45 | struct itimerspec normalize; |
---|
46 | |
---|
47 | if ( !value ) |
---|
48 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
49 | |
---|
50 | /* |
---|
51 | * First, it verifies if the structure "value" is correct |
---|
52 | * if the number of nanoseconds is not correct return EINVAL |
---|
53 | */ |
---|
54 | if ( !_Timespec_Is_valid( &(value->it_value) ) ) { |
---|
55 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
56 | } |
---|
57 | if ( !_Timespec_Is_valid( &(value->it_interval) ) ) { |
---|
58 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
59 | } |
---|
60 | |
---|
61 | if ( flags != TIMER_ABSTIME && flags != POSIX_TIMER_RELATIVE ) { |
---|
62 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
63 | } |
---|
64 | |
---|
65 | normalize = *value; |
---|
66 | |
---|
67 | /* Convert absolute to relative time */ |
---|
68 | if (flags == TIMER_ABSTIME) { |
---|
69 | struct timespec now; |
---|
70 | _TOD_Get( &now ); |
---|
71 | /* Check for seconds in the past */ |
---|
72 | if ( _Timespec_Greater_than( &now, &normalize.it_value ) ) |
---|
73 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
74 | _Timespec_Subtract( &now, &normalize.it_value, &normalize.it_value ); |
---|
75 | } |
---|
76 | |
---|
77 | /* If the function reaches this point, then it will be necessary to do |
---|
78 | * something with the structure of times of the timer: to stop, start |
---|
79 | * or start it again |
---|
80 | */ |
---|
81 | |
---|
82 | ptimer = _POSIX_Timer_Get( timerid, &location ); |
---|
83 | switch ( location ) { |
---|
84 | |
---|
85 | case OBJECTS_LOCAL: |
---|
86 | /* First, it verifies if the timer must be stopped */ |
---|
87 | if ( normalize.it_value.tv_sec == 0 && normalize.it_value.tv_nsec == 0 ) { |
---|
88 | /* Stop the timer */ |
---|
89 | (void) _Watchdog_Remove( &ptimer->Timer ); |
---|
90 | /* The old data of the timer are returned */ |
---|
91 | if ( ovalue ) |
---|
92 | *ovalue = ptimer->timer_data; |
---|
93 | /* The new data are set */ |
---|
94 | ptimer->timer_data = normalize; |
---|
95 | /* Indicates that the timer is created and stopped */ |
---|
96 | ptimer->state = POSIX_TIMER_STATE_CREATE_STOP; |
---|
97 | /* Returns with success */ |
---|
98 | _Objects_Put( &ptimer->Object ); |
---|
99 | return 0; |
---|
100 | } |
---|
101 | |
---|
102 | /* Convert from seconds and nanoseconds to ticks */ |
---|
103 | ptimer->ticks = _Timespec_To_ticks( &value->it_interval ); |
---|
104 | initial_period = _Timespec_To_ticks( &normalize.it_value ); |
---|
105 | |
---|
106 | |
---|
107 | activated = _POSIX_Timer_Insert_helper( |
---|
108 | &ptimer->Timer, |
---|
109 | initial_period, |
---|
110 | ptimer->Object.id, |
---|
111 | _POSIX_Timer_TSR, |
---|
112 | ptimer |
---|
113 | ); |
---|
114 | if ( !activated ) { |
---|
115 | _Objects_Put( &ptimer->Object ); |
---|
116 | return 0; |
---|
117 | } |
---|
118 | |
---|
119 | /* |
---|
120 | * The timer has been started and is running. So we return the |
---|
121 | * old ones in "ovalue" |
---|
122 | */ |
---|
123 | if ( ovalue ) |
---|
124 | *ovalue = ptimer->timer_data; |
---|
125 | ptimer->timer_data = normalize; |
---|
126 | |
---|
127 | /* Indicate that the time is running */ |
---|
128 | ptimer->state = POSIX_TIMER_STATE_CREATE_RUN; |
---|
129 | _TOD_Get( &ptimer->time ); |
---|
130 | _Objects_Put( &ptimer->Object ); |
---|
131 | return 0; |
---|
132 | |
---|
133 | #if defined(RTEMS_MULTIPROCESSING) |
---|
134 | case OBJECTS_REMOTE: |
---|
135 | #endif |
---|
136 | case OBJECTS_ERROR: |
---|
137 | break; |
---|
138 | } |
---|
139 | |
---|
140 | rtems_set_errno_and_return_minus_one( EINVAL ); |
---|
141 | } |
---|