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  PROCESS_CONTEXT_BEGIN(transaction_handler_process);
117  etimer_restart(&t->retrans_timer); /* interval updated above */
118  PROCESS_CONTEXT_END(transaction_handler_process);
119 
120  t = NULL;
121  } else {
122  /* timed out */
123  PRINTF("Timeout\n");
124  restful_response_handler callback = t->callback;
125  void *callback_data = t->callback_data;
126 
127  /* handle observers */
128  coap_remove_observer_by_client(&t->addr, t->port);
129 
130  coap_clear_transaction(t);
131 
132  if(callback) {
133  callback(callback_data, NULL);
134  }
135  }
136  } else {
137  coap_clear_transaction(t);
138  }
139 }
140 /*---------------------------------------------------------------------------*/
141 void
142 coap_clear_transaction(coap_transaction_t *t)
143 {
144  if(t) {
145  PRINTF("Freeing transaction %u: %p\n", t->mid, t);
146 
147  etimer_stop(&t->retrans_timer);
148  list_remove(transactions_list, t);
149  memb_free(&transactions_memb, t);
150  }
151 }
152 coap_transaction_t *
153 coap_get_transaction_by_mid(uint16_t mid)
154 {
155  coap_transaction_t *t = NULL;
156 
157  for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
158  if(t->mid == mid) {
159  PRINTF("Found transaction for MID %u: %p\n", t->mid, t);
160  return t;
161  }
162  }
163  return NULL;
164 }
165 /*---------------------------------------------------------------------------*/
166 void
167 coap_check_transactions()
168 {
169  coap_transaction_t *t = NULL;
170 
171  for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
172  if(etimer_expired(&t->retrans_timer)) {
173  ++(t->retrans_counter);
174  PRINTF("Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
175  coap_send_transaction(t);
176  }
177  }
178 }
179 /*---------------------------------------------------------------------------*/
void list_remove(list_t list, void *item)
Remove a specific element from a list.
Definition: list.c:240
#define LIST(name)
Declare a linked list.
Definition: list.h:86
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
CoAP module for observing resources (draft-ietf-core-observe-11).
#define MEMB(name, structure, num)
Declare a memory block.
Definition: memb.h:89
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
Definition: etimer.c:199
#define PROCESS_CONTEXT_END(p)
End a context switch.
Definition: process.h:440
CoAP module for reliable transport
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
Definition: uip.h:1027
void etimer_stop(struct etimer *et)
Stop a pending event timer.
Definition: etimer.c:243
#define PROCESS_CONTEXT_BEGIN(p)
Switch context to another process.
Definition: process.h:426
void * list_head(list_t list)
Get a pointer to the first element of a list.
Definition: list.c:83
#define NULL
The null pointer.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition: random.c:47
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:143
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition: memb.c:59
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
Definition: process.h:402
char memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition: memb.c:79