Contiki-Inga 3.x
ctimer.c
Go to the documentation of this file.
1 /**
2  * \addtogroup ctimer
3  * @{
4  */
5 
6 /*
7  * Copyright (c) 2006, Swedish Institute of Computer Science.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in the
17  * documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the Institute nor the names of its contributors
19  * may be used to endorse or promote products derived from this software
20  * without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * This file is part of the Contiki operating system.
35  *
36  */
37 
38 /**
39  * \file
40  * Callback timer implementation
41  * \author
42  * Adam Dunkels <adam@sics.se>
43  */
44 
45 #include "sys/ctimer.h"
46 #include "contiki.h"
47 #include "lib/list.h"
48 
49 LIST(ctimer_list);
50 
51 static char initialized;
52 
53 #define DEBUG 0
54 #if DEBUG
55 #include <stdio.h>
56 #define PRINTF(...) printf(__VA_ARGS__)
57 #else
58 #define PRINTF(...)
59 #endif
60 
61 /*---------------------------------------------------------------------------*/
62 PROCESS(ctimer_process, "Ctimer process");
63 PROCESS_THREAD(ctimer_process, ev, data)
64 {
65  struct ctimer *c;
66  PROCESS_BEGIN();
67 
68  for(c = list_head(ctimer_list); c != NULL; c = c->next) {
69  etimer_set(&c->etimer, c->etimer.timer.interval);
70  }
71  initialized = 1;
72 
73  while(1) {
74  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER);
75  for(c = list_head(ctimer_list); c != NULL; c = c->next) {
76  if(&c->etimer == data) {
77  list_remove(ctimer_list, c);
79  if(c->f != NULL) {
80  c->f(c->ptr);
81  }
82  PROCESS_CONTEXT_END(c->p);
83  break;
84  }
85  }
86  }
87  PROCESS_END();
88 }
89 /*---------------------------------------------------------------------------*/
90 void
92 {
93  initialized = 0;
94  list_init(ctimer_list);
95  process_start(&ctimer_process, NULL);
96 }
97 /*---------------------------------------------------------------------------*/
98 void
99 ctimer_set(struct ctimer *c, clock_time_t t,
100  void (*f)(void *), void *ptr)
101 {
102  PRINTF("ctimer_set %p %u\n", c, (unsigned)t);
103  c->p = PROCESS_CURRENT();
104  c->f = f;
105  c->ptr = ptr;
106  if(initialized) {
107  PROCESS_CONTEXT_BEGIN(&ctimer_process);
108  etimer_set(&c->etimer, t);
109  PROCESS_CONTEXT_END(&ctimer_process);
110  } else {
111  c->etimer.timer.interval = t;
112  }
113 
114  list_remove(ctimer_list, c);
115  list_add(ctimer_list, c);
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 ctimer_reset(struct ctimer *c)
120 {
121  if(initialized) {
122  PROCESS_CONTEXT_BEGIN(&ctimer_process);
123  etimer_reset(&c->etimer);
124  PROCESS_CONTEXT_END(&ctimer_process);
125  }
126 
127  list_remove(ctimer_list, c);
128  list_add(ctimer_list, c);
129 }
130 /*---------------------------------------------------------------------------*/
131 void
132 ctimer_restart(struct ctimer *c)
133 {
134  if(initialized) {
135  PROCESS_CONTEXT_BEGIN(&ctimer_process);
136  etimer_restart(&c->etimer);
137  PROCESS_CONTEXT_END(&ctimer_process);
138  }
139 
140  list_remove(ctimer_list, c);
141  list_add(ctimer_list, c);
142 }
143 /*---------------------------------------------------------------------------*/
144 void
145 ctimer_stop(struct ctimer *c)
146 {
147  if(initialized) {
148  etimer_stop(&c->etimer);
149  } else {
150  c->etimer.next = NULL;
151  c->etimer.p = PROCESS_NONE;
152  }
153  list_remove(ctimer_list, c);
154 }
155 /*---------------------------------------------------------------------------*/
156 int
157 ctimer_expired(struct ctimer *c)
158 {
159  struct ctimer *t;
160  if(initialized) {
161  return etimer_expired(&c->etimer);
162  }
163  for(t = list_head(ctimer_list); t != NULL; t = t->next) {
164  if(t == c) {
165  return 0;
166  }
167  }
168  return 1;
169 }
170 /*---------------------------------------------------------------------------*/
171 /** @} */