Contiki-NG
Loading...
Searching...
No Matches
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#include "sys/log.h"
50#define LOG_MODULE "CTimer"
51#define LOG_LEVEL LOG_LEVEL_SYS
52
53LIST(ctimer_list);
54static bool initialized;
55
56/*---------------------------------------------------------------------------*/
57PROCESS(ctimer_process, "Ctimer process");
58PROCESS_THREAD(ctimer_process, ev, data)
59{
60 struct ctimer *c;
62
63 for(c = list_head(ctimer_list); c != NULL; c = c->next) {
64 etimer_set(&c->etimer, c->etimer.timer.interval);
65 }
66 initialized = true;
67
68 while(1) {
69 PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER);
70 for(c = list_head(ctimer_list); c != NULL; c = c->next) {
71 if(&c->etimer == data) {
72 list_remove(ctimer_list, c);
74 if(c->f != NULL) {
75 c->f(c->ptr);
76 }
78 break;
79 }
80 }
81 }
83}
84/*---------------------------------------------------------------------------*/
85void
87{
88 initialized = false;
89 list_init(ctimer_list);
90 process_start(&ctimer_process, NULL);
91}
92/*---------------------------------------------------------------------------*/
93/*---------------------------------------------------------------------------*/
94void
95ctimer_set_with_process(struct ctimer *c, clock_time_t t,
96 void (*f)(void *), void *ptr, struct process *p)
97{
98 LOG_DBG("ctimer_set %p %lu\n", c, (unsigned long)t);
99 c->p = p;
100 c->f = f;
101 c->ptr = ptr;
102 if(initialized) {
103 PROCESS_CONTEXT_BEGIN(&ctimer_process);
104 etimer_set(&c->etimer, t);
105 PROCESS_CONTEXT_END(&ctimer_process);
106 } else {
107 c->etimer.timer.interval = t;
108 }
109
110 list_add(ctimer_list, c);
111}
112/*---------------------------------------------------------------------------*/
113void
114ctimer_reset(struct ctimer *c)
115{
116 if(initialized) {
117 PROCESS_CONTEXT_BEGIN(&ctimer_process);
118 etimer_reset(&c->etimer);
119 PROCESS_CONTEXT_END(&ctimer_process);
120 }
121
122 list_add(ctimer_list, c);
123}
124/*---------------------------------------------------------------------------*/
125void
126ctimer_restart(struct ctimer *c)
127{
128 if(initialized) {
129 PROCESS_CONTEXT_BEGIN(&ctimer_process);
130 etimer_restart(&c->etimer);
131 PROCESS_CONTEXT_END(&ctimer_process);
132 }
133
134 list_add(ctimer_list, c);
135}
136/*---------------------------------------------------------------------------*/
137void
138ctimer_stop(struct ctimer *c)
139{
140 if(initialized) {
141 etimer_stop(&c->etimer);
142 } else {
143 c->etimer.next = NULL;
144 c->etimer.p = PROCESS_NONE;
145 }
146 list_remove(ctimer_list, c);
147}
148/*---------------------------------------------------------------------------*/
149bool
150ctimer_expired(struct ctimer *c)
151{
152 struct ctimer *t;
153 if(initialized) {
154 return etimer_expired(&c->etimer);
155 }
156 for(t = list_head(ctimer_list); t != NULL; t = t->next) {
157 if(t == c) {
158 return false;
159 }
160 }
161 return true;
162}
163/*---------------------------------------------------------------------------*/
164/** @} */
Header file for the callback timer.
void ctimer_init(void)
Initialize the callback timer library.
Definition ctimer.c:86
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
Definition ctimer.c:138
void ctimer_reset(struct ctimer *c)
Reset a callback timer with the same interval as was previously set.
Definition ctimer.c:114
bool ctimer_expired(struct ctimer *c)
Check if a callback timer has expired.
Definition ctimer.c:150
void ctimer_restart(struct ctimer *c)
Restart a callback timer from the current point in time.
Definition ctimer.c:126
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:95
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:237
static bool etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition etimer.h:201
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition etimer.c:177
static void list_init(list_t list)
Initialize a list.
Definition list.h:152
#define LIST(name)
Declare a linked list.
Definition list.h:90
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition list.c:71
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition list.c:134
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
Definition list.h:169
#define PROCESS(name, strname)
Declare a process.
Definition process.h:308
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:121
#define PROCESS_END()
Define the end of a process.
Definition process.h:132
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition process.h:428
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:274
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition process.h:442
#define PROCESS_YIELD_UNTIL(c)
Yield the currently running process until a condition occurs.
Definition process.h:179
Linked list manipulation routines.
Header file for the logging system.