libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
net.h
Go to the documentation of this file.
1 /* net.h -- CoAP network interface
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #ifndef _COAP_NET_H_
10 #define _COAP_NET_H_
11 
12 #include "config.h"
13 
14 #ifdef HAVE_ASSERT_H
15 #include <assert.h>
16 #else
17 #ifndef assert
18 #warning "assertions are disabled"
19 # define assert(x)
20 #endif
21 #endif
22 
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
27 #endif
28 #ifdef HAVE_TIME_H
29 #include <time.h>
30 #endif
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34 
35 #include "option.h"
36 #include "address.h"
37 #include "prng.h"
38 #include "pdu.h"
39 #include "coap_time.h"
40 
41 struct coap_queue_t;
42 
43 typedef struct coap_queue_t {
44  struct coap_queue_t *next;
45 
46  coap_tick_t t; /* when to send PDU for the next time */
47  unsigned char retransmit_cnt; /* retransmission counter, will be removed when zero */
48  unsigned int timeout; /* the randomized timeout value */
49 
55 } coap_queue_t;
56 
57 /* adds node to given queue, ordered by specified order function */
58 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node,
59  int (*order)(coap_queue_t *, coap_queue_t *node));
60 
61 /* destroys specified node */
63 
64 /* removes all items from given queue and frees the allocated storage */
65 void coap_delete_all(coap_queue_t *queue);
66 
67 /* creates a new node suitable for adding to the CoAP sendqueue */
69 
70 struct coap_resource_t;
71 struct coap_context_t;
72 #ifndef WITHOUT_ASYNC
73 struct coap_async_state_t;
74 #endif
75 
77 typedef void (*coap_response_handler_t)(struct coap_context_t *,
78  const coap_address_t *remote,
79  coap_pdu_t *sent,
80  coap_pdu_t *received,
81  const coap_tid_t id);
82 
83 #define COAP_MID_CACHE_SIZE 3
84 typedef struct {
85  unsigned char flags[COAP_MID_CACHE_SIZE];
88 
90 typedef struct coap_context_t {
92 #ifndef WITH_CONTIKI
94 #endif /* WITH_CONTIKI */
95 #ifndef WITHOUT_ASYNC
96 
98 #endif /* WITHOUT_ASYNC */
100 #ifndef WITH_CONTIKI
101  int sockfd;
102 #else /* WITH_CONTIKI */
103  struct uip_udp_conn *conn;
105  struct etimer retransmit_timer;
106  struct etimer notify_timer;
107 #endif /* WITH_CONTIKI */
108 
115  unsigned short message_id;
116 
121  unsigned int observe;
122 
125 
133 static inline void
135  coap_response_handler_t handler) {
136  context->response_handler = handler;
137 }
138 
146 inline static void
147 coap_register_option(coap_context_t *ctx, unsigned char type) {
148  coap_option_setb(ctx->known_options, type);
149 }
150 
151 /* Returns the next pdu to send without removing from sendqeue. */
153 
154 /* Returns the next pdu to send and removes it from the sendqeue. */
156 
157 /* Creates a new coap_context_t object that will hold the CoAP stack status. */
158 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
159 
168 static inline unsigned short
170 #ifndef WITH_CONTIKI
171  return htons(++(context->message_id));
172 #else /* WITH_CONTIKI */
173  return uip_htons(++context->message_id);
174 #endif
175 }
176 
177 /* CoAP stack context must be released with coap_free_context() */
178 void coap_free_context( coap_context_t *context );
179 
180 
192  const coap_address_t *dst,
193  coap_pdu_t *pdu);
194 
213  unsigned char code,
214  coap_opt_filter_t opts);
226  const coap_address_t *dst,
227  coap_pdu_t *pdu);
228 
247  coap_pdu_t *request,
248  const coap_address_t *dst,
249  unsigned char code,
250  coap_opt_filter_t opts);
251 
265  const coap_address_t *dst,
266  coap_pdu_t *request,
267  unsigned char type);
281  const coap_address_t *dst,
282  coap_pdu_t *request);
283 
296 static inline coap_tid_t
298  const coap_address_t *dst,
299  coap_pdu_t *request) {
300  return coap_send_message_type(context, dst, request, COAP_MESSAGE_RST);
301 }
302 
305 
311 int coap_read( coap_context_t *context );
312 
321 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu,
322  coap_tid_t *id);
323 
342  coap_tid_t id,
343  coap_queue_t **node);
344 
355 inline static int
357  coap_queue_t *node;
358  if (!coap_remove_from_queue(queue, id, &node))
359  return 0;
360 
361  coap_delete_node(node);
362  return 1;
363 }
364 
372 
383  const coap_address_t *dst,
384  const unsigned char *token,
385  size_t token_length);
386 
388 void coap_dispatch( coap_context_t *context );
389 
391 int coap_can_exit( coap_context_t *context );
392 
397 void coap_ticks(coap_tick_t *);
398 
432  coap_pdu_t *pdu,
433  coap_opt_filter_t unknown);
434 
435 #endif /* _COAP_NET_H_ */