EVE 1.0
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 <stddef.h>
46 #include "sys/ctimer.h"
47 #include "contiki.h"
48 #include "lib/list.h"
49 
50 LIST(ctimer_list);
51 
52 static char initialized;
53 
54 #ifndef DEBUG
55  #define DEBUG 0
56 #endif
57 #if DEBUG
58 #include <stdio.h>
59 #define PRINTF(...) printf(__VA_ARGS__)
60 #else
61 #define PRINTF(...)
62 #endif
63 
64 /*---------------------------------------------------------------------------*/
65 PROCESS(ctimer_process, "Ctimer process");
66 PROCESS_THREAD(ctimer_process, ev, data)
67 {
68  struct ctimer *c;
69  PROCESS_BEGIN();
70 
71  for(c = list_head(ctimer_list); c != NULL; c = c->next) {
72  etimer_set(&c->etimer, c->etimer.timer.interval);
73  }
74  initialized = 1;
75 
76  while(1) {
77  PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER);
78  for(c = list_head(ctimer_list); c != NULL; c = c->next) {
79  if(&c->etimer == data) {
80  list_remove(ctimer_list, c);
82  if(c->f != NULL) {
83  c->f(c->ptr);
84  }
85  PROCESS_CONTEXT_END(c->p);
86  break;
87  }
88  }
89  }
90  PROCESS_END();
91 #ifdef ECLIPSE_STUB_CODE_ANALYSE
92  return PT_ENDED;
93 #endif /* ECLIPSE_STUB_CODE_ANALYSE */
94 }
95 /*---------------------------------------------------------------------------*/
96 void
98 {
99  initialized = 0;
100  list_init(ctimer_list);
101  process_start(&ctimer_process, NULL);
102 }
103 /*---------------------------------------------------------------------------*/
104 void
105 ctimer_set(struct ctimer *c, clock_time_t t,
106  void (*f)(void *), void *ptr)
107 {
108  PRINTF("ctimer_set %p %u\n", c, (unsigned)t);
109  c->p = PROCESS_CURRENT();
110  c->f = f;
111  c->ptr = ptr;
112  if(initialized) {
113  PROCESS_CONTEXT_BEGIN(&ctimer_process);
114  etimer_set(&c->etimer, t);
115  PROCESS_CONTEXT_END(&ctimer_process);
116  } else {
117  c->etimer.timer.interval = t;
118  }
119 
120  list_remove(ctimer_list, c);
121  list_add(ctimer_list, c);
122 }
123 /*---------------------------------------------------------------------------*/
124 void
125 ctimer_reset(struct ctimer *c)
126 {
127  if(initialized) {
128  PROCESS_CONTEXT_BEGIN(&ctimer_process);
129  etimer_reset(&c->etimer);
130  PROCESS_CONTEXT_END(&ctimer_process);
131  }
132 
133  list_remove(ctimer_list, c);
134  list_add(ctimer_list, c);
135 }
136 /*---------------------------------------------------------------------------*/
137 void
138 ctimer_restart(struct ctimer *c)
139 {
140  if(initialized) {
141  PROCESS_CONTEXT_BEGIN(&ctimer_process);
142  etimer_restart(&c->etimer);
143  PROCESS_CONTEXT_END(&ctimer_process);
144  }
145 
146  list_remove(ctimer_list, c);
147  list_add(ctimer_list, c);
148 }
149 /*---------------------------------------------------------------------------*/
150 void
151 ctimer_stop(struct ctimer *c)
152 {
153  if(initialized) {
154  etimer_stop(&c->etimer);
155  } else {
156  c->etimer.next = NULL;
157  c->etimer.p = PROCESS_NONE;
158  }
159  list_remove(ctimer_list, c);
160 }
161 /*---------------------------------------------------------------------------*/
162 int
163 ctimer_expired(struct ctimer *c)
164 {
165  struct ctimer *t;
166  if(initialized) {
167  return etimer_expired(&c->etimer);
168  }
169  for(t = list_head(ctimer_list); t != NULL; t = t->next) {
170  if(t == c) {
171  return 0;
172  }
173  }
174  return 1;
175 }
176 /*---------------------------------------------------------------------------*/
177 /** @} */
#define PROCESS_END()
Definition: process.h:140
int ctimer_expired(struct ctimer *c)
Check if a callback timer has expired.
Definition: ctimer.c:163
#define PROCESS(name, strname)
Definition: process.h:316
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:151
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:263
void process_start(struct process *p, const char *arg)
Definition: process.c:100
#define PROCESS_CONTEXT_END(p)
Definition: process.h:475
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:219
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:212
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:97
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
#define PROCESS_YIELD_UNTIL(c)
Definition: process.h:187
#define PROCESS_CURRENT()
Definition: process.h:437
#define PROCESS_BEGIN()
Definition: process.h:129
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:233
void * list_head(list_t list)
Definition: list.c:82
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:105
void ctimer_restart(struct ctimer *c)
Restart a callback timer from the current point in time.
Definition: ctimer.c:138
void list_add(list_t list, void *item)
Definition: list.c:142
void list_init(list_t list)
Definition: list.c:65
uint8_t data[USBNET_RX_BUF_SIZE]
Definition: usbnet.h:140
#define LIST(name)
Definition: list.h:88
#define PROCESS_CONTEXT_BEGIN(p)
Definition: process.h:461
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:205
#define PROCESS_THREAD(name, ev, data)
Definition: process.h:282
void list_remove(list_t list, void *item)
Definition: list.c:239