Contiki-NG
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 <stdlib.h>
50
51/* Log configuration */
52#include "coap-log.h"
53#define LOG_MODULE "coap"
54#define LOG_LEVEL LOG_LEVEL_COAP
55
56/*---------------------------------------------------------------------------*/
57MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
58LIST(transactions_list);
59
60/*---------------------------------------------------------------------------*/
61static void
62coap_retransmit_transaction(coap_timer_t *nt)
63{
64 coap_transaction_t *t = coap_timer_get_user_data(nt);
65 if(t == NULL) {
66 LOG_DBG("No retransmission data in coap_timer!\n");
67 return;
68 }
69 ++(t->retrans_counter);
70 LOG_DBG("Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
71 coap_send_transaction(t);
72}
73/*---------------------------------------------------------------------------*/
74
75/*---------------------------------------------------------------------------*/
76/*- Internal API ------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78coap_transaction_t *
79coap_new_transaction(uint16_t mid, const coap_endpoint_t *endpoint)
80{
81 coap_transaction_t *t = memb_alloc(&transactions_memb);
82
83 if(t) {
84 t->mid = mid;
85 t->retrans_counter = 0;
86
87 /* save client address */
88 coap_endpoint_copy(&t->endpoint, endpoint);
89
90 list_add(transactions_list, t); /* list itself makes sure same element is not added twice */
91 }
92
93 return t;
94}
95/*---------------------------------------------------------------------------*/
96void
97coap_send_transaction(coap_transaction_t *t)
98{
99 LOG_DBG("Sending transaction %u\n", t->mid);
100
101 if(COAP_TYPE_CON ==
102 ((COAP_HEADER_TYPE_MASK & t->message[0]) >> COAP_HEADER_TYPE_POSITION)) {
103 if(t->retrans_counter <= COAP_MAX_RETRANSMIT) {
104 /* not timed out yet */
105 coap_sendto(&t->endpoint, t->message, t->message_len);
106 LOG_DBG("Keeping transaction %u\n", t->mid);
107
108 if(t->retrans_counter == 0) {
109 coap_timer_set_callback(&t->retrans_timer, coap_retransmit_transaction);
110 coap_timer_set_user_data(&t->retrans_timer, t);
111 t->retrans_interval =
112 COAP_RESPONSE_TIMEOUT_TICKS + (rand() %
113 COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
114 LOG_DBG("Initial interval %lu msec\n",
115 (unsigned long)t->retrans_interval);
116 } else {
117 t->retrans_interval <<= 1; /* double */
118 LOG_DBG("Doubled (%u) interval %lu s\n", t->retrans_counter,
119 (unsigned long)(t->retrans_interval / 1000));
120 }
121
122 /* interval updated above */
123 coap_timer_set(&t->retrans_timer, t->retrans_interval);
124 } else {
125 /* timed out */
126 LOG_DBG("Timeout\n");
127 coap_resource_response_handler_t callback = t->callback;
128 void *callback_data = t->callback_data;
129
130 /* handle observers */
131 coap_remove_observer_by_client(&t->endpoint);
132
133 coap_clear_transaction(t);
134
135 if(callback) {
136 callback(callback_data, NULL);
137 }
138 }
139 } else {
140 coap_sendto(&t->endpoint, t->message, t->message_len);
141 coap_clear_transaction(t);
142 }
143}
144/*---------------------------------------------------------------------------*/
145void
146coap_clear_transaction(coap_transaction_t *t)
147{
148 if(t) {
149 LOG_DBG("Freeing transaction %u: %p\n", t->mid, t);
150
151 coap_timer_stop(&t->retrans_timer);
152 list_remove(transactions_list, t);
153 memb_free(&transactions_memb, t);
154 }
155}
156/*---------------------------------------------------------------------------*/
157coap_transaction_t *
158coap_get_transaction_by_mid(uint16_t mid)
159{
160 coap_transaction_t *t = NULL;
161
162 for(t = (coap_transaction_t *)list_head(transactions_list); t; t = t->next) {
163 if(t->mid == mid) {
164 LOG_DBG("Found transaction for MID %u: %p\n", t->mid, t);
165 return t;
166 }
167 }
168 return NULL;
169}
170/*---------------------------------------------------------------------------*/
171/** @} */
Log support for CoAP.
CoAP module for observing resources (draft-ietf-core-observe-11).
CoAP timer API.
CoAP module for reliable transport.
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
void * list_head(const list_t list)
Get a pointer to the first element of a list.
Definition: list.c:82
#define LIST(name)
Declare a linked list.
Definition: list.h:89
void list_add(list_t list, void *item)
Add an item at the end of a list.
Definition: list.c:142
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
Definition: list.c:237
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:90
Linked list manipulation routines.
Memory block allocation routines.