40 #include "contiki-net.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])
52 #define PRINT6ADDR(addr)
53 #define PRINTLLADDR(addr)
57 MEMB(transactions_memb, coap_transaction_t, COAP_MAX_OPEN_TRANSACTIONS);
58 LIST(transactions_list);
60 static struct process *transaction_handler_process =
NULL;
66 coap_register_as_transaction_handler()
71 coap_new_transaction(uint16_t mid, uip_ipaddr_t *addr, uint16_t port)
73 coap_transaction_t *t =
memb_alloc(&transactions_memb);
77 t->retrans_counter = 0;
90 coap_send_transaction(coap_transaction_t *t)
92 PRINTF(
"Sending transaction %u\n", t->mid);
94 coap_send_message(&t->addr, t->port, t->packet, t->packet_len);
97 ((COAP_HEADER_TYPE_MASK & t->packet[0]) >> COAP_HEADER_TYPE_POSITION)) {
98 if(t->retrans_counter < COAP_MAX_RETRANSMIT) {
100 PRINTF(
"Keeping transaction %u\n", t->mid);
102 if(t->retrans_counter == 0) {
103 t->retrans_timer.timer.interval =
107 COAP_RESPONSE_TIMEOUT_BACKOFF_MASK);
108 PRINTF(
"Initial interval %f\n",
111 t->retrans_timer.timer.interval <<= 1;
112 PRINTF(
"Doubled (%u) interval %f\n", t->retrans_counter,
122 process_current = transaction_handler_process;
124 process_current = process_actual;
130 restful_response_handler callback = t->callback;
131 void *callback_data = t->callback_data;
134 coap_remove_observer_by_client(&t->addr, t->port);
136 coap_clear_transaction(t);
139 callback(callback_data,
NULL);
143 coap_clear_transaction(t);
148 coap_clear_transaction(coap_transaction_t *t)
151 PRINTF(
"Freeing transaction %u: %p\n", t->mid, t);
159 coap_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 PRINTF(
"Found transaction for MID %u: %p\n", t->mid, t);
173 coap_check_transactions()
175 coap_transaction_t *t =
NULL;
177 for(t = (coap_transaction_t *)
list_head(transactions_list); t; t = t->next) {
179 ++(t->retrans_counter);
180 PRINTF(
"Retransmitting %u (%u)\n", t->mid, t->retrans_counter);
181 coap_send_transaction(t);
#define PROCESS_CURRENT()
Get a pointer to the currently running process.
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
char 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 NULL
The null pointer.
void list_remove(list_t list, void *item)
Remove a specific element from a list.
CoAP module for reliable transport
#define uip_ipaddr_copy(dest, src)
Copy an IP address from one place to another.
void * list_head(list_t list)
Get a pointer to the first element of a list.
#define MEMB(name, structure, num)
Declare a memory block.
void list_add(list_t list, void *item)
Add an item at the end of a list.
#define LIST(name)
Declare a linked list.
void etimer_stop(struct etimer *et)
Stop a pending event timer.
void etimer_restart(struct etimer *et)
Restart an event timer from the current point in time.
unsigned short random_rand(void)
Generate the next state and return the upper part of it.
#define CLOCK_SECOND
A second, measured in system clock time.
CoAP module for observing resources (draft-ietf-core-observe-11).