49#include "lib/random.h"
54#define LOG_MODULE "coap"
55#define LOG_LEVEL LOG_LEVEL_COAP
58MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
59LIST(transactions_list);
63coap_retransmit_transaction(coap_timer_t *nt)
67 LOG_DBG(
"No retransmission data in coap_timer!\n");
70 ++(t->retrans_counter);
71 LOG_DBG(
"Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
72 coap_send_transaction(t);
80coap_new_transaction(uint16_t mid,
const coap_endpoint_t *endpoint)
82 coap_transaction_t *t =
memb_alloc(&transactions_memb);
86 t->retrans_counter = 0;
98coap_send_transaction(coap_transaction_t *t)
100 LOG_DBG(
"Sending transaction %u\n", t->mid);
103 ((COAP_HEADER_TYPE_MASK & t->message[0]) >> COAP_HEADER_TYPE_POSITION)) {
104 if(t->retrans_counter <= COAP_MAX_RETRANSMIT) {
106 coap_sendto(&t->endpoint, t->message, t->message_len);
107 LOG_DBG(
"Keeping transaction %u\n", t->mid);
109 if(t->retrans_counter == 0) {
112 t->retrans_interval =
114 COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
115 LOG_DBG(
"Initial interval %lu msec\n",
116 (
unsigned long)t->retrans_interval);
118 t->retrans_interval <<= 1;
119 LOG_DBG(
"Doubled (%u) interval %lu s\n", t->retrans_counter,
120 (
unsigned long)(t->retrans_interval / 1000));
127 LOG_DBG(
"Timeout\n");
128 coap_resource_response_handler_t callback = t->callback;
129 void *callback_data = t->callback_data;
132 coap_remove_observer_by_client(&t->endpoint);
134 coap_clear_transaction(t);
137 callback(callback_data, NULL);
141 coap_sendto(&t->endpoint, t->message, t->message_len);
142 coap_clear_transaction(t);
147coap_clear_transaction(coap_transaction_t *t)
150 LOG_DBG(
"Freeing transaction %u: %p\n", t->mid, t);
159coap_get_transaction_by_mid(uint16_t mid)
161 coap_transaction_t *t = NULL;
163 for(t = (coap_transaction_t *)
list_head(transactions_list); t; t = t->next) {
165 LOG_DBG(
"Found transaction for MID %u: %p\n", t->mid, t);
CoAP module for observing resources (draft-ietf-core-observe-11).
CoAP module for reliable transport.
unsigned short random_rand(void)
Generates a new random number using the cc2538 RNG.
void coap_timer_stop(coap_timer_t *timer)
Stop a pending CoAP timer.
static void * coap_timer_get_user_data(coap_timer_t *timer)
Get user data that has been attached to a CoAP timer.
static void coap_timer_set_user_data(coap_timer_t *timer, void *data)
Attach user data to a CoAP timer.
void coap_timer_set(coap_timer_t *timer, uint64_t time)
Set a CoAP timer to expire after the specified time.
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.
int coap_sendto(const coap_endpoint_t *ep, const uint8_t *data, uint16_t len)
Send a message to the specified CoAP endpoint.
void coap_endpoint_copy(coap_endpoint_t *dest, const coap_endpoint_t *src)
Copy a CoAP endpoint from one memory area to another.
#define LIST(name)
Declare a linked list.
void list_add(list_t list, void *item)
Add an item at the end of a list.
void list_remove(list_t list, const void *item)
Remove a specific element from a list.
static void * list_head(const_list_t list)
Get a pointer to the first element of a list.
int memb_free(struct memb *m, void *ptr)
Deallocate a memory block from a memory block previously declared with MEMB().
void * memb_alloc(struct memb *m)
Allocate a memory block from a block of memory declared with MEMB().
#define MEMB(name, structure, num)
Declare a memory block.
Linked list manipulation routines.
Memory block allocation routines.