Contiki 3.x
er-coap-transactions.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Institute for Pervasive Computing, ETH Zurich
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  * \file
34  * CoAP module for reliable transport
35  * \author
36  * Matthias Kovatsch <kovatsch@inf.ethz.ch>
37  */
38 
39 #include "contiki.h"
40 #include "contiki-net.h"
41 #include "er-coap-transactions.h"
42 #include "er-coap-observe.h"
43 
44 #define DEBUG 0
45 #if DEBUG
46 #include <stdio.h>
47 #define PRINTF(...) printf(__VA_ARGS__)
48 #define PRINT6ADDR(addr) PRINTF("[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x]", ((uint8_t *)addr)[0], ((uint8_t *)addr)[1], ((uint8_t *)addr)[2], ((uint8_t *)addr)[3], ((uint8_t *)addr)[4], ((uint8_t *)addr)[5], ((uint8_t *)addr)[6], ((uint8_t *)addr)[7], ((uint8_t *)addr)[8], ((uint8_t *)addr)[9], ((uint8_t *)addr)[10], ((uint8_t *)addr)[11], ((uint8_t *)addr)[12], ((uint8_t *)addr)[13], ((uint8_t *)addr)[14], ((uint8_t *)addr)[15])
49 #define PRINTLLADDR(lladdr) PRINTF("[%02x:%02x:%02x:%02x:%02x:%02x]", (lladdr)->addr[0], (lladdr)->addr[1], (lladdr)->addr[2], (lladdr)->addr[3], (lladdr)->addr[4], (lladdr)->addr[5])
50 #else
51 #define PRINTF(...)
52 #define PRINT6ADDR(addr)
53 #define PRINTLLADDR(addr)
54 #endif
55 
56 /*---------------------------------------------------------------------------*/
57 MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
58 LIST(transactions_list);
59 
60 static struct process *transaction_handler_process = NULL;
61 
62 /*---------------------------------------------------------------------------*/
63 /*- Internal API ------------------------------------------------------------*/
64 /*---------------------------------------------------------------------------*/
65 void
66 coap_register_as_transaction_handler()
67 {
68  transaction_handler_process = PROCESS_CURRENT();
69 }
70 coap_transaction_t *
71 coap_new_transaction(uint16_t mid, uip_ipaddr_t *addr, uint16_t port)
72 {
73  coap_transaction_t *t = memb_alloc(&transactions_memb);
74 
75  if(t) {
76  t->mid = mid;
77  t->retrans_counter = 0;
78 
79  /* save client address */
80  uip_ipaddr_copy(&t->addr, addr);
81  t->port = port;
82 
83  list_add(transactions_list, t); /* list itself makes sure same element is not added twice */
84  }
85 
86  return t;
87 }
88 /*---------------------------------------------------------------------------*/
89 void
90 coap_send_transaction(coap_transaction_t *t)
91 {
92  PRINTF("Sending transaction %u\n", t->mid);
93 
94  coap_send_message(&t->addr, t->port, t->packet, t->packet_len);
95 
96  if(COAP_TYPE_CON ==
97  ((COAP_HEADER_TYPE_MASK & t->packet[0]) >> COAP_HEADER_TYPE_POSITION)) {
98  if(t->retrans_counter < COAP_MAX_RETRANSMIT) {
99  /* not timed out yet */
100  PRINTF("Keeping transaction %u\n", t->mid);
101 
102  if(t->retrans_counter == 0) {
103  t->retrans_timer.timer.interval =
104  COAP_RESPONSE_TIMEOUT_TICKS + (random_rand()
105  %
106  (clock_time_t)
107  COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
108  PRINTF("Initial interval %f\n",
109  (float)t->retrans_timer.timer.interval / CLOCK_SECOND);
110  } else {
111  t->retrans_timer.timer.interval <<= 1; /* double */
112  PRINTF("Doubled (%u) interval %f\n", t->retrans_counter,
113  (float)t->retrans_timer.timer.interval / CLOCK_SECOND);
114  }
115 
116  /*FIXME
117  * Hack: Setting timer for responsible process.
118  * Maybe there is a better way, but avoid posting everything to the process.
119  */
120  struct process *process_actual = PROCESS_CURRENT();
121 
122  process_current = transaction_handler_process;
123  etimer_restart(&t->retrans_timer); /* interval updated above */
124  process_current = process_actual;
125 
126  t = NULL;
127  } else {
128  /* timed out */
129  PRINTF("Timeout\n");
130  restful_response_handler callback = t->callback;
131  void *callback_data = t->callback_data;
132 
133  /* handle observers */
134  coap_remove_observer_by_client(&t->addr, t->port);
135 
136  coap_clear_transaction(t);
137 
138  if(callback) {
139  callback(callback_data, NULL);
140  }
141  }
142  } else {
143  coap_clear_transaction(t);
144  }
145 }
146 /*---------------------------------------------------------------------------*/
147 void
148 coap_clear_transaction(coap_transaction_t *t)
149 {
150  if(t) {
151  PRINTF("Freeing transaction %u: %p\n", t->mid, t);
152 
153  etimer_stop(&t->retrans_timer);
154  list_remove(transactions_list, t);
155  memb_free(&transactions_memb, t);
156  }
157 }
158 coap_transaction_t *
159 coap_get_transaction_by_mid(uint16_t mid)
160 {
161  coap_transaction_t *t = NULL;
162 
163  for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
164  if(t->mid == mid) {
165  PRINTF("Found transaction for MID %u: %p\n", t->mid, t);
166  return t;
167  }
168  }
169  return NULL;
170 }
171 /*---------------------------------------------------------------------------*/
172 void
173 coap_check_transactions()
174 {
175  coap_transaction_t *t = NULL;
176 
177  for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
178  if(etimer_expired(&t->retrans_timer)) {
179  ++(t->retrans_counter);
180  PRINTF("Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
181  coap_send_transaction(t);
182  }
183  }
184 }
185 /*---------------------------------------------------------------------------*/
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:205
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
CoAP module for reliable transport
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1026
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
#define LIST(name)
Declare a linked list.
Definition: list.h:86
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:235
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:191
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
Definition: random.c:47
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
CoAP module for observing resources (draft-ietf-core-observe-11).