1 | /* |
---|
2 | * Timer Manager |
---|
3 | * |
---|
4 | * |
---|
5 | * COPYRIGHT (c) 1989, 1990, 1991, 1992, 1993, 1994. |
---|
6 | * On-Line Applications Research Corporation (OAR). |
---|
7 | * All rights assigned to U.S. Government, 1994. |
---|
8 | * |
---|
9 | * This material may be reproduced by or for the U.S. Government pursuant |
---|
10 | * to the copyright license under the clause at DFARS 252.227-7013. This |
---|
11 | * notice must appear in all copies of this file and its derivatives. |
---|
12 | * |
---|
13 | * $Id$ |
---|
14 | */ |
---|
15 | |
---|
16 | #include <rtems/system.h> |
---|
17 | #include <rtems/object.h> |
---|
18 | #include <rtems/thread.h> |
---|
19 | #include <rtems/timer.h> |
---|
20 | #include <rtems/tod.h> |
---|
21 | #include <rtems/watchdog.h> |
---|
22 | |
---|
23 | /*PAGE |
---|
24 | * |
---|
25 | * _Timer_Manager_initialization |
---|
26 | * |
---|
27 | * This routine initializes all timer manager related data structures. |
---|
28 | * |
---|
29 | * Input parameters: |
---|
30 | * maximum_timers - number of timers to initialize |
---|
31 | * |
---|
32 | * Output parameters: NONE |
---|
33 | */ |
---|
34 | |
---|
35 | void _Timer_Manager_initialization( |
---|
36 | unsigned32 maximum_timers |
---|
37 | ) |
---|
38 | { |
---|
39 | _Objects_Initialize_information( |
---|
40 | &_Timer_Information, |
---|
41 | FALSE, |
---|
42 | maximum_timers, |
---|
43 | sizeof( Timer_Control ) |
---|
44 | ); |
---|
45 | } |
---|
46 | |
---|
47 | /*PAGE |
---|
48 | * |
---|
49 | * rtems_timer_create |
---|
50 | * |
---|
51 | * This directive creates a timer and performs some initialization. |
---|
52 | * |
---|
53 | * Input parameters: |
---|
54 | * name - timer name |
---|
55 | * id - pointer to timer id |
---|
56 | * |
---|
57 | * Output parameters: |
---|
58 | * id - timer id |
---|
59 | * RTEMS_SUCCESSFUL - if successful |
---|
60 | * error code - if unsuccessful |
---|
61 | */ |
---|
62 | |
---|
63 | rtems_status_code rtems_timer_create( |
---|
64 | Objects_Name name, |
---|
65 | Objects_Id *id |
---|
66 | ) |
---|
67 | { |
---|
68 | Timer_Control *the_timer; |
---|
69 | |
---|
70 | if ( !_Objects_Is_name_valid( name ) ) |
---|
71 | return ( RTEMS_INVALID_NAME ); |
---|
72 | |
---|
73 | _Thread_Disable_dispatch(); /* to prevent deletion */ |
---|
74 | |
---|
75 | the_timer = _Timer_Allocate(); |
---|
76 | |
---|
77 | if ( !the_timer ) { |
---|
78 | _Thread_Enable_dispatch(); |
---|
79 | return( RTEMS_TOO_MANY ); |
---|
80 | } |
---|
81 | |
---|
82 | the_timer->the_class = TIMER_DORMANT; |
---|
83 | |
---|
84 | _Objects_Open( &_Timer_Information, &the_timer->Object, name ); |
---|
85 | |
---|
86 | *id = the_timer->Object.id; |
---|
87 | _Thread_Enable_dispatch(); |
---|
88 | return( RTEMS_SUCCESSFUL ); |
---|
89 | } |
---|
90 | |
---|
91 | /*PAGE |
---|
92 | * |
---|
93 | * rtems_timer_ident |
---|
94 | * |
---|
95 | * This directive returns the system ID associated with |
---|
96 | * the timer name. |
---|
97 | * |
---|
98 | * Input parameters: |
---|
99 | * name - user defined message queue name |
---|
100 | * id - pointer to timer id |
---|
101 | * |
---|
102 | * Output parameters: |
---|
103 | * *id - message queue id |
---|
104 | * RTEMS_SUCCESSFUL - if successful |
---|
105 | * error code - if unsuccessful |
---|
106 | */ |
---|
107 | |
---|
108 | rtems_status_code rtems_timer_ident( |
---|
109 | Objects_Name name, |
---|
110 | Objects_Id *id |
---|
111 | ) |
---|
112 | { |
---|
113 | return _Objects_Name_to_id( |
---|
114 | &_Timer_Information, |
---|
115 | name, |
---|
116 | RTEMS_SEARCH_LOCAL_NODE, |
---|
117 | id |
---|
118 | ); |
---|
119 | } |
---|
120 | |
---|
121 | /*PAGE |
---|
122 | * |
---|
123 | * rtems_timer_cancel |
---|
124 | * |
---|
125 | * This directive allows a thread to cancel a timer. |
---|
126 | * |
---|
127 | * Input parameters: |
---|
128 | * id - timer id |
---|
129 | * |
---|
130 | * Output parameters: |
---|
131 | * RTEMS_SUCCESSFUL - if successful |
---|
132 | * error code - if unsuccessful |
---|
133 | */ |
---|
134 | |
---|
135 | rtems_status_code rtems_timer_cancel( |
---|
136 | Objects_Id id |
---|
137 | ) |
---|
138 | { |
---|
139 | Timer_Control *the_timer; |
---|
140 | Objects_Locations location; |
---|
141 | |
---|
142 | the_timer = _Timer_Get( id, &location ); |
---|
143 | switch ( location ) { |
---|
144 | case OBJECTS_ERROR: |
---|
145 | return( RTEMS_INVALID_ID ); |
---|
146 | case OBJECTS_REMOTE: /* should never return this */ |
---|
147 | return( RTEMS_INTERNAL_ERROR ); |
---|
148 | case OBJECTS_LOCAL: |
---|
149 | if ( !_Timer_Is_dormant_class( the_timer->the_class ) ) { |
---|
150 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
151 | _Thread_Enable_dispatch(); |
---|
152 | return( RTEMS_SUCCESSFUL ); |
---|
153 | } |
---|
154 | _Thread_Enable_dispatch(); |
---|
155 | return( RTEMS_INCORRECT_STATE ); |
---|
156 | } |
---|
157 | |
---|
158 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
159 | } |
---|
160 | |
---|
161 | /*PAGE |
---|
162 | * |
---|
163 | * rtems_timer_delete |
---|
164 | * |
---|
165 | * This directive allows a thread to delete a timer. |
---|
166 | * |
---|
167 | * Input parameters: |
---|
168 | * id - timer id |
---|
169 | * |
---|
170 | * Output parameters: |
---|
171 | * RTEMS_SUCCESSFUL - if successful |
---|
172 | * error code - if unsuccessful |
---|
173 | */ |
---|
174 | |
---|
175 | rtems_status_code rtems_timer_delete( |
---|
176 | Objects_Id id |
---|
177 | ) |
---|
178 | { |
---|
179 | Timer_Control *the_timer; |
---|
180 | Objects_Locations location; |
---|
181 | |
---|
182 | the_timer = _Timer_Get( id, &location ); |
---|
183 | switch ( location ) { |
---|
184 | case OBJECTS_ERROR: |
---|
185 | return( RTEMS_INVALID_ID ); |
---|
186 | case OBJECTS_REMOTE: /* should never return this */ |
---|
187 | return( RTEMS_INTERNAL_ERROR ); |
---|
188 | case OBJECTS_LOCAL: |
---|
189 | _Objects_Close( &_Timer_Information, &the_timer->Object ); |
---|
190 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
191 | _Timer_Free( the_timer ); |
---|
192 | _Thread_Enable_dispatch(); |
---|
193 | return( RTEMS_SUCCESSFUL ); |
---|
194 | } |
---|
195 | |
---|
196 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
197 | } |
---|
198 | |
---|
199 | /*PAGE |
---|
200 | * |
---|
201 | * rtems_timer_fire_after |
---|
202 | * |
---|
203 | * This directive allows a thread to start a timer. |
---|
204 | * |
---|
205 | * Input parameters: |
---|
206 | * id - timer id |
---|
207 | * ticks - interval until routine is fired |
---|
208 | * routine - routine to schedule |
---|
209 | * |
---|
210 | * Output parameters: |
---|
211 | * RTEMS_SUCCESSFUL - if successful |
---|
212 | * error code - if unsuccessful |
---|
213 | */ |
---|
214 | |
---|
215 | rtems_status_code rtems_timer_fire_after( |
---|
216 | Objects_Id id, |
---|
217 | rtems_interval ticks, |
---|
218 | Timer_Service routine, |
---|
219 | void *user_data |
---|
220 | ) |
---|
221 | { |
---|
222 | Timer_Control *the_timer; |
---|
223 | Objects_Locations location; |
---|
224 | |
---|
225 | if ( ticks == 0 ) |
---|
226 | return( RTEMS_INVALID_NUMBER ); |
---|
227 | |
---|
228 | the_timer = _Timer_Get( id, &location ); |
---|
229 | switch ( location ) { |
---|
230 | case OBJECTS_ERROR: |
---|
231 | return( RTEMS_INVALID_ID ); |
---|
232 | case OBJECTS_REMOTE: /* should never return this */ |
---|
233 | return( RTEMS_INTERNAL_ERROR ); |
---|
234 | case OBJECTS_LOCAL: |
---|
235 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
236 | the_timer->the_class = TIMER_INTERVAL; |
---|
237 | _Watchdog_Initialize( &the_timer->Ticker, routine, id, user_data ); |
---|
238 | _Watchdog_Insert_ticks( &the_timer->Ticker, |
---|
239 | ticks, WATCHDOG_ACTIVATE_NOW ); |
---|
240 | _Thread_Enable_dispatch(); |
---|
241 | return( RTEMS_SUCCESSFUL ); |
---|
242 | } |
---|
243 | |
---|
244 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
245 | } |
---|
246 | |
---|
247 | /*PAGE |
---|
248 | * |
---|
249 | * rtems_timer_fire_when |
---|
250 | * |
---|
251 | * This directive allows a thread to start a timer. |
---|
252 | * |
---|
253 | * Input parameters: |
---|
254 | * id - timer id |
---|
255 | * wall_time - time of day to fire timer |
---|
256 | * routine - routine to schedule |
---|
257 | * |
---|
258 | * Output parameters: |
---|
259 | * RTEMS_SUCCESSFUL - if successful |
---|
260 | * error code - if unsuccessful |
---|
261 | */ |
---|
262 | |
---|
263 | rtems_status_code rtems_timer_fire_when( |
---|
264 | Objects_Id id, |
---|
265 | rtems_time_of_day *wall_time, |
---|
266 | Timer_Service routine, |
---|
267 | void *user_data |
---|
268 | ) |
---|
269 | { |
---|
270 | Timer_Control *the_timer; |
---|
271 | Objects_Locations location; |
---|
272 | rtems_status_code validate_status; |
---|
273 | rtems_interval seconds; |
---|
274 | |
---|
275 | if ( !_TOD_Is_set() ) |
---|
276 | return( RTEMS_NOT_DEFINED ); |
---|
277 | |
---|
278 | validate_status = _TOD_Validate( wall_time ); |
---|
279 | if ( !rtems_is_status_successful( validate_status ) ) |
---|
280 | return( validate_status ); |
---|
281 | |
---|
282 | seconds = _TOD_To_seconds( wall_time ); |
---|
283 | if ( seconds <= _TOD_Seconds_since_epoch ) |
---|
284 | return( RTEMS_INVALID_CLOCK ); |
---|
285 | |
---|
286 | the_timer = _Timer_Get( id, &location ); |
---|
287 | switch ( location ) { |
---|
288 | case OBJECTS_ERROR: |
---|
289 | return( RTEMS_INVALID_ID ); |
---|
290 | case OBJECTS_REMOTE: /* should never return this */ |
---|
291 | return( RTEMS_INTERNAL_ERROR ); |
---|
292 | case OBJECTS_LOCAL: |
---|
293 | (void) _Watchdog_Remove( &the_timer->Ticker ); |
---|
294 | the_timer->the_class = TIMER_TIME_OF_DAY; |
---|
295 | _Watchdog_Initialize( &the_timer->Ticker, routine, id, user_data ); |
---|
296 | _Watchdog_Insert_seconds( &the_timer->Ticker, |
---|
297 | seconds - _TOD_Seconds_since_epoch, WATCHDOG_ACTIVATE_NOW ); |
---|
298 | _Thread_Enable_dispatch(); |
---|
299 | return( RTEMS_SUCCESSFUL ); |
---|
300 | } |
---|
301 | |
---|
302 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
303 | } |
---|
304 | |
---|
305 | /*PAGE |
---|
306 | * |
---|
307 | * rtems_timer_reset |
---|
308 | * |
---|
309 | * This directive allows a thread to reset a timer. |
---|
310 | * |
---|
311 | * Input parameters: |
---|
312 | * id - timer id |
---|
313 | * |
---|
314 | * Output parameters: |
---|
315 | * RTEMS_SUCCESSFUL - if successful |
---|
316 | * error code - if unsuccessful |
---|
317 | */ |
---|
318 | |
---|
319 | rtems_status_code rtems_timer_reset( |
---|
320 | Objects_Id id |
---|
321 | ) |
---|
322 | { |
---|
323 | Timer_Control *the_timer; |
---|
324 | Objects_Locations location; |
---|
325 | |
---|
326 | the_timer = _Timer_Get( id, &location ); |
---|
327 | switch ( location ) { |
---|
328 | case OBJECTS_ERROR: |
---|
329 | return( RTEMS_INVALID_ID ); |
---|
330 | case OBJECTS_REMOTE: /* should never return this */ |
---|
331 | return( RTEMS_INTERNAL_ERROR ); |
---|
332 | case OBJECTS_LOCAL: |
---|
333 | if ( _Timer_Is_interval_class( the_timer->the_class ) ) { |
---|
334 | _Watchdog_Reset( &the_timer->Ticker ); |
---|
335 | _Thread_Enable_dispatch(); |
---|
336 | return( RTEMS_SUCCESSFUL ); |
---|
337 | } |
---|
338 | _Thread_Enable_dispatch(); |
---|
339 | return( RTEMS_NOT_DEFINED ); |
---|
340 | } |
---|
341 | |
---|
342 | return( RTEMS_INTERNAL_ERROR ); /* unreached - only to remove warnings */ |
---|
343 | } |
---|