Contiki-NG
Loading...
Searching...
No Matches
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/**
40 * \addtogroup coap
41 * @{
42 */
43
44#include "coap-transactions.h"
45#include "coap-observe.h"
46#include "coap-timer.h"
47#include "lib/memb.h"
48#include "lib/list.h"
49#include "lib/random.h"
50#include <stdlib.h>
51
52/* Log configuration */
53#include "coap-log.h"
54#define LOG_MODULE "coap"
55#define LOG_LEVEL LOG_LEVEL_COAP
56
57/*---------------------------------------------------------------------------*/
58MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
59LIST(transactions_list);
60
61/*---------------------------------------------------------------------------*/
62static void
63coap_retransmit_transaction(coap_timer_t *nt)
64{
65 coap_transaction_t *t = coap_timer_get_user_data(nt);
66 if(t == NULL) {
67 LOG_DBG("No retransmission data in coap_timer!\n");
68 return;
69 }
70 ++(t->retrans_counter);
71 LOG_DBG("Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
72 coap_send_transaction(t);
73}
74/*---------------------------------------------------------------------------*/
75
76/*---------------------------------------------------------------------------*/
77/*- Internal API ------------------------------------------------------------*/
78/*---------------------------------------------------------------------------*/
79coap_transaction_t *
80coap_new_transaction(uint16_t mid, const coap_endpoint_t *endpoint)
81{
82 coap_transaction_t *t = memb_alloc(&transactions_memb);
83
84 if(t) {
85 t->mid = mid;
86 t->retrans_counter = 0;
87
88 /* save client address */
89 coap_endpoint_copy(&t->endpoint, endpoint);
90
91 list_add(transactions_list, t); /* list itself makes sure same element is not added twice */
92 }
93
94 return t;
95}
96/*---------------------------------------------------------------------------*/
97void
98coap_send_transaction(coap_transaction_t *t)
99{
100 LOG_DBG("Sending transaction %u\n", t->mid);
101
102 if(COAP_TYPE_CON ==
103 ((COAP_HEADER_TYPE_MASK & t->message[0]) >> COAP_HEADER_TYPE_POSITION)) {
104 if(t->retrans_counter <= COAP_MAX_RETRANSMIT) {
105 /* not timed out yet */
106 coap_sendto(&t->endpoint, t->message, t->message_len);
107 LOG_DBG("Keeping transaction %u\n", t->mid);
108
109 if(t->retrans_counter == 0) {
110 coap_timer_set_callback(&t->retrans_timer, coap_retransmit_transaction);
111 coap_timer_set_user_data(&t->retrans_timer, t);
112 t->retrans_interval =
113 COAP_RESPONSE_TIMEOUT_TICKS + (random_rand() %
114 COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
115 LOG_DBG("Initial interval %lu msec\n",
116 (unsigned long)t->retrans_interval);
117 } else {
118 t->retrans_interval <<= 1; /* double */
119 LOG_DBG("Doubled (%u) interval %lu s\n", t->retrans_counter,
120 (unsigned long)(t->retrans_interval / 1000));
121 }
122
123 /* interval updated above */
124 coap_timer_set(&t->retrans_timer, t->retrans_interval);
125 } else {
126 /* timed out */
127 LOG_DBG("Timeout\n");
128 coap_resource_response_handler_t callback = t->callback;
129 void *callback_data = t->callback_data;
130
131 /* handle observers */
132 coap_remove_observer_by_client(&t->endpoint);
133
134 coap_clear_transaction(t);
135
136 if(callback) {
137 callback(callback_data, NULL);
138 }
139 }
140 } else {
141 coap_sendto(&t->endpoint, t->message, t->message_len);
142 coap_clear_transaction(t);
143 }
144}
145/*---------------------------------------------------------------------------*/
146void
147coap_clear_transaction(coap_transaction_t *t)
148{
149 if(t) {
150 LOG_DBG("Freeing transaction %u: %p\n", t->mid, t);
151
152 coap_timer_stop(&t->retrans_timer);
153 list_remove(transactions_list, t);
154 memb_free(&transactions_memb, t);
155 }
156}
157/*---------------------------------------------------------------------------*/
158coap_transaction_t *
159coap_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 LOG_DBG("Found transaction for MID %u: %p\n", t->mid, t);
166 return t;
167 }
168 }
169 return NULL;
170}
171/*---------------------------------------------------------------------------*/
172/** @} */
Log support for CoAP.
CoAP module for observing resources (draft-ietf-core-observe-11).
CoAP timer API.
CoAP module for reliable transport.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
Definition random.c:58
void coap_timer_stop(coap_timer_t *timer)
Stop a pending CoAP timer.
Definition coap-timer.c:92
static void * coap_timer_get_user_data(coap_timer_t *timer)
Get user data that has been attached to a CoAP timer.
Definition coap-timer.h:118
static void coap_timer_set_user_data(coap_timer_t *timer, void *data)
Attach user data to a CoAP timer.
Definition coap-timer.h:130
void coap_timer_set(coap_timer_t *timer, uint64_t time)
Set a CoAP timer to expire after the specified time.
Definition coap-timer.c:103
static void coap_timer_set_callback(coap_timer_t *timer, void(*callback)(coap_timer_t *))
Set a callback function to be called when a CoAP timer expires.
Definition coap-timer.h:105
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
Send a message to the specified CoAP endpoint.
Definition coap-uip.c:369
void coap_endpoint_copy(coap_endpoint_t *dest, const coap_endpoint_t *src)
Copy a CoAP endpoint from one memory area to another.
Definition coap-uip.c:154
#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
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
Definition memb.c:78
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
Definition memb.c:59
#define MEMB(name, structure, num)
Declare a memory block.
Definition memb.h:91
Linked list manipulation routines.
Memory block allocation routines.