Contiki-NG
coap-timer-default.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016, SICS, Swedish ICT AB.
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 
30 /**
31  * \file
32  * CoAP timer driver implementation based on Contiki etimers
33  * \author
34  * Niclas Finne <nfi@sics.se>
35  * Joakim Eriksson <joakime@sics.se>
36  */
37 
38 /**
39  * \addtogroup coap-timer
40  * @{
41  *
42  * \defgroup coap-timer-default CoAP timer for Contiki-NG
43  * @{
44  *
45  * This is an implementation of CoAP timer for Contiki-NG.
46  */
47 
48 #include "coap-timer.h"
49 #include "sys/clock.h"
50 #include "sys/etimer.h"
51 #include "sys/process.h"
52 
53 /* Log configuration */
54 #include "coap-log.h"
55 #define LOG_MODULE "coap-timer"
56 #define LOG_LEVEL LOG_LEVEL_NONE
57 
58 PROCESS(coap_timer_process, "coap timer process");
59 
60 static uint64_t current_time;
61 static struct etimer timer;
62 /*---------------------------------------------------------------------------*/
63 static void
64 update_timer(void)
65 {
66  uint64_t remaining;
68  LOG_DBG("remaining %lu msec\n", (unsigned long)remaining);
69  if(remaining == 0) {
70  /* Run as soon as possible */
71  process_poll(&coap_timer_process);
72  } else {
73  remaining *= CLOCK_SECOND;
74  remaining /= 1000;
75  if(remaining > CLOCK_SECOND * 60) {
76  /* Make sure the CoAP timer clock is updated at least once per minute */
77  remaining = CLOCK_SECOND * 60;
78  } else if(remaining < 1) {
79  /* Wait minimum one system clock tick */
80  remaining = 1;
81  }
82  etimer_set(&timer, (clock_time_t)remaining);
83  }
84 }
85 /*---------------------------------------------------------------------------*/
86 PROCESS_THREAD(coap_timer_process, ev, data)
87 {
88  PROCESS_BEGIN();
89 
91  while(1) {
92  PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER ||
93  ev == PROCESS_EVENT_POLL);
94 
95  if(coap_timer_run()) {
96  /* Needs to run again */
97  process_poll(&coap_timer_process);
98  } else {
99  update_timer();
100  }
101  }
102  PROCESS_END();
103 }
104 /*---------------------------------------------------------------------------*/
105 static uint64_t
106 uptime(void)
107 {
108  static clock_time_t last;
109  clock_time_t now;
110  uint64_t diff;
111 
112  now = clock_time();
113  diff = (clock_time_t)(now - last);
114  if(diff > 0) {
115  current_time += (diff * 1000) / CLOCK_SECOND;
116  last = now;
117  }
118  return current_time;
119 }
120 /*---------------------------------------------------------------------------*/
121 static void
122 init(void)
123 {
124  process_start(&coap_timer_process, NULL);
125 }
126 /*---------------------------------------------------------------------------*/
127 static void
128 update(void)
129 {
130  process_poll(&coap_timer_process);
131 }
132 /*---------------------------------------------------------------------------*/
133 const coap_timer_driver_t coap_timer_default_driver = {
134  .init = init,
135  .uptime = uptime,
136  .update = update,
137 };
138 /*---------------------------------------------------------------------------*/
139 /** @} */
140 /** @} */
Log support for CoAP
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
CoAP timer API.
#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
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
A timer.
Definition: timer.h:82
uint64_t coap_timer_time_to_next_expiration(void)
Get the time until next CoAP timer expires or 0 if there already exists expired timers that have not ...
Definition: coap-timer.c:117
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
Event timer header file.
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
Header file for the Contiki process interface.
clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:118
A timer.
Definition: etimer.h:76
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
int coap_timer_run(void)
This function must be called periodically by the CoAP timer driver to process any expired CoAP timers...
Definition: coap-timer.c:137
void process_start(struct process *p, process_data_t data)
Start a process.
Definition: process.c:99