Contiki 3.x
ctimer.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Callback timer implementation
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 /**
41  * \addtogroup ctimer
42  * @{
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_add(ctimer_list, c);
115 }
116 /*---------------------------------------------------------------------------*/
117 void
118 ctimer_reset(struct ctimer *c)
119 {
120  if(initialized) {
121  PROCESS_CONTEXT_BEGIN(&ctimer_process);
122  etimer_reset(&c->etimer);
123  PROCESS_CONTEXT_END(&ctimer_process);
124  }
125 
126  list_add(ctimer_list, c);
127 }
128 /*---------------------------------------------------------------------------*/
129 void
130 ctimer_restart(struct ctimer *c)
131 {
132  if(initialized) {
133  PROCESS_CONTEXT_BEGIN(&ctimer_process);
134  etimer_restart(&c->etimer);
135  PROCESS_CONTEXT_END(&ctimer_process);
136  }
137 
138  list_add(ctimer_list, c);
139 }
140 /*---------------------------------------------------------------------------*/
141 void
142 ctimer_stop(struct ctimer *c)
143 {
144  if(initialized) {
145  etimer_stop(&c->etimer);
146  } else {
147  c->etimer.next = NULL;
148  c->etimer.p = PROCESS_NONE;
149  }
150  list_remove(ctimer_list, c);
151 }
152 /*---------------------------------------------------------------------------*/
153 int
154 ctimer_expired(struct ctimer *c)
155 {
156  struct ctimer *t;
157  if(initialized) {
158  return etimer_expired(&c->etimer);
159  }
160  for(t = list_head(ctimer_list); t != NULL; t = t->next) {
161  if(t == c) {
162  return 0;
163  }
164  }
165  return 1;
166 }
167 /*---------------------------------------------------------------------------*/
168 /** @} */
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
Linked list manipulation routines.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:205
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
int ctimer_expired(struct ctimer *c)
Check if a callback timer has expired.
Definition: ctimer.c:154
#define NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void ctimer_restart(struct ctimer *c)
Restart a callback timer from the current point in time.
Definition: ctimer.c:130
void list_init(list_t list)
Initialize a list.
Definition: list.c:66
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:118
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define LIST(name)
Declare a linked list.
Definition: list.h:86
Header file for the callback timer
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:184
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:235
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition: process.h:426
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:191
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:142
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition: process.h:440