Contiki-NG
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
49LIST(ctimer_list);
50
51static 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/*---------------------------------------------------------------------------*/
62PROCESS(ctimer_process, "Ctimer process");
63PROCESS_THREAD(ctimer_process, ev, data)
64{
65 struct ctimer *c;
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 }
83 break;
84 }
85 }
86 }
88}
89/*---------------------------------------------------------------------------*/
90void
92{
93 initialized = 0;
94 list_init(ctimer_list);
95 process_start(&ctimer_process, NULL);
96}
97/*---------------------------------------------------------------------------*/
98void
99ctimer_set(struct ctimer *c, clock_time_t t,
100 void (*f)(void *), void *ptr)
101{
103}
104/*---------------------------------------------------------------------------*/
105void
106ctimer_set_with_process(struct ctimer *c, clock_time_t t,
107 void (*f)(void *), void *ptr, struct process *p)
108{
109 PRINTF("ctimer_set %p %lu\n", c, (unsigned long)t);
110 c->p = p;
111 c->f = f;
112 c->ptr = ptr;
113 if(initialized) {
114 PROCESS_CONTEXT_BEGIN(&ctimer_process);
115 etimer_set(&c->etimer, t);
116 PROCESS_CONTEXT_END(&ctimer_process);
117 } else {
118 c->etimer.timer.interval = t;
119 }
120
121 list_add(ctimer_list, c);
122}
123/*---------------------------------------------------------------------------*/
124void
125ctimer_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_add(ctimer_list, c);
134}
135/*---------------------------------------------------------------------------*/
136void
137ctimer_restart(struct ctimer *c)
138{
139 if(initialized) {
140 PROCESS_CONTEXT_BEGIN(&ctimer_process);
141 etimer_restart(&c->etimer);
142 PROCESS_CONTEXT_END(&ctimer_process);
143 }
144
145 list_add(ctimer_list, c);
146}
147/*---------------------------------------------------------------------------*/
148void
149ctimer_stop(struct ctimer *c)
150{
151 if(initialized) {
152 etimer_stop(&c->etimer);
153 } else {
154 c->etimer.next = NULL;
155 c->etimer.p = PROCESS_NONE;
156 }
157 list_remove(ctimer_list, c);
158}
159/*---------------------------------------------------------------------------*/
160int
161ctimer_expired(struct ctimer *c)
162{
163 struct ctimer *t;
164 if(initialized) {
165 return etimer_expired(&c->etimer);
166 }
167 for(t = list_head(ctimer_list); t != NULL; t = t->next) {
168 if(t == c) {
169 return 0;
170 }
171 }
172 return 1;
173}
174/*---------------------------------------------------------------------------*/
175/** @} */
Header file for the callback timer.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1154
void ctimer_init(void)
Initialize the callback timer library.
Definition: ctimer.c:91
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition: ctimer.c:149
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
Definition: ctimer.c:99
int ctimer_expired(struct ctimer *c)
Check if a callback timer has expired.
Definition: ctimer.c:161
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition: ctimer.c:125
void ctimer_restart(struct ctimer *c)
Restart a callback timer from the current point in time.
Definition: ctimer.c:137
void ctimer_set_with_process(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr, struct process *p)
Set a callback timer.
Definition: ctimer.c:106
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:199
void etimer_reset(struct etimer *et)
Reset an event timer with the same interval as was previously set.
Definition: etimer.c:192
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void list_init(list_t list)
Initialize a list.
Definition: list.c:57
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:89
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:152
void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition: list.c:63
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition: process.h:426
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition: process.h:440
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition: process.h:178
Linked list manipulation routines.