libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
net.h
Go to the documentation of this file.
1 /* net.h -- CoAP network interface
2  *
3  * Copyright (C) 2010--2013 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 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #include "config.h"
17 
18 #ifdef HAVE_ASSERT_H
19 #include <assert.h>
20 #else
21 #ifndef assert
22 #warning "assertions are disabled"
23 # define assert(x)
24 #endif
25 #endif
26 
27 #include <stdlib.h>
28 #include <string.h>
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32 #ifdef HAVE_TIME_H
33 #include <time.h>
34 #endif
35 #ifdef HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif
38 
39 #ifdef WITH_LWIP
40 #include <lwip/ip_addr.h>
41 #endif
42 
43 #include "option.h"
44 #include "address.h"
45 #include "prng.h"
46 #include "pdu.h"
47 #include "coap_time.h"
48 
49 struct coap_queue_t;
50 
51 typedef struct coap_queue_t {
52  struct coap_queue_t *next;
53 
54  coap_tick_t t;
55  unsigned char retransmit_cnt;
56  unsigned int timeout;
58  coap_address_t local;
59  coap_address_t remote;
63 } coap_queue_t;
64 
66 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
67 
70 
72 void coap_delete_all(coap_queue_t *queue);
73 
76 
77 struct coap_resource_t;
78 struct coap_context_t;
79 #ifndef WITHOUT_ASYNC
80 struct coap_async_state_t;
81 #endif
82 
84 typedef void (*coap_response_handler_t)(struct coap_context_t *,
85  const coap_address_t *remote,
86  coap_pdu_t *sent,
87  coap_pdu_t *received,
88  const coap_tid_t id);
89 
90 #define COAP_MID_CACHE_SIZE 3
91 typedef struct {
92  unsigned char flags[COAP_MID_CACHE_SIZE];
95 
97 typedef struct coap_context_t {
99 #ifndef WITH_CONTIKI
101 #endif /* WITH_CONTIKI */
102 #ifndef WITHOUT_ASYNC
103 
105 #endif /* WITHOUT_ASYNC */
106 
109  coap_tick_t sendqueue_basetime;
111 #if WITH_POSIX
112  int sockfd;
113 #endif /* WITH_POSIX */
114 #ifdef WITH_CONTIKI
115  struct uip_udp_conn *conn;
117  struct etimer retransmit_timer;
118  struct etimer notify_timer;
119 #endif /* WITH_CONTIKI */
120 #ifdef WITH_LWIP
121  struct udp_pcb *pcb;
122  struct pbuf *pending_package;
123  ip_addr_t pending_address;
124  u16_t pending_port;
126  uint8_t timer_configured;
127 #endif /* WITH_LWIP */
128 
135  unsigned short message_id;
136 
141  unsigned int observe;
142 
145 
153 static inline void
155  coap_response_handler_t handler) {
156  context->response_handler = handler;
157 }
158 
166 inline static void
167 coap_register_option(coap_context_t *ctx, unsigned char type) {
168  coap_option_setb(ctx->known_options, type);
169 }
170 
171 
177 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
178 
181 
184 
186 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
187 
196 static inline unsigned short
198 #ifndef WITH_CONTIKI
199  return htons(++(context->message_id));
200 #else /* WITH_CONTIKI */
201  return uip_htons(++context->message_id);
202 #endif
203 }
204 
205 /* CoAP stack context must be released with coap_free_context() */
206 void coap_free_context( coap_context_t *context );
207 
208 
220  const coap_address_t *dst,
221  coap_pdu_t *pdu);
222 
241  unsigned char code,
242  coap_opt_filter_t opts);
254  const coap_address_t *dst,
255  coap_pdu_t *pdu);
256 
275  coap_pdu_t *request,
276  const coap_address_t *dst,
277  unsigned char code,
278  coap_opt_filter_t opts);
279 
293  const coap_address_t *dst,
294  coap_pdu_t *request,
295  unsigned char type);
309  const coap_address_t *dst,
310  coap_pdu_t *request);
311 
324 static inline coap_tid_t
326  const coap_address_t *dst,
327  coap_pdu_t *request) {
328  return coap_send_message_type(context, dst, request, COAP_MESSAGE_RST);
329 }
330 
333 
339 int coap_read( coap_context_t *context );
340 
349 void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu,
350  coap_tid_t *id);
351 
370  coap_tid_t id,
371  coap_queue_t **node);
372 
383 inline static int
385  coap_queue_t *node;
386  if (!coap_remove_from_queue(queue, id, &node))
387  return 0;
388 
389  coap_delete_node(node);
390  return 1;
391 }
392 
400 
411  const coap_address_t *dst,
412  const unsigned char *token,
413  size_t token_length);
414 
416 void coap_dispatch( coap_context_t *context );
417 
419 int coap_can_exit( coap_context_t *context );
420 
425 void coap_ticks(coap_tick_t *);
426 
460  coap_pdu_t *pdu,
461  coap_opt_filter_t unknown);
462 
463 #ifdef __cplusplus
464 }
465 #endif
466 
467 #endif /* _COAP_NET_H_ */
coap_queue_t * sendqueue
Definition: net.h:110
unsigned int observe
The next value to be used for Observe.
Definition: net.h:141
unsigned char coap_key_t[4]
Definition: hashkey.h:19
coap_tid_t coap_send(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:653
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
Definition: net.c:261
void coap_transaction_id(const coap_address_t *peer, const coap_pdu_t *pdu, coap_tid_t *id)
Calculates a unique transaction id from given arguments peer and pdu.
Definition: net.c:498
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:55
#define COAP_MESSAGE_RST
Definition: pdu.h:46
coap_address_t local
local address
Definition: net.h:58
int coap_tid_t
Definition: pdu.h:155
int coap_option_check_critical(coap_context_t *ctx, coap_pdu_t *pdu, coap_opt_filter_t unknown)
Verifies that pdu contains no unknown critical options.
Definition: net.c:463
Clock Handling.
static void coap_register_option(coap_context_t *ctx, unsigned char type)
Registers the option type type with the given context object ctx.
Definition: net.h:167
static int coap_remove_transaction(coap_queue_t **queue, coap_tid_t id)
Removes the transaction identified by id from given queue.
Definition: net.h:384
coap_address_t remote
remote address
Definition: net.h:59
struct coap_context_t coap_context_t
The CoAP stack's global state is stored in a coap_context_t object.
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from queue.
Definition: net.c:1043
struct coap_async_state_t * async_state
list of asynchronous transactions
Definition: net.h:104
helpers for handling options in CoAP PDUs
static int coap_option_setb(coap_opt_filter_t filter, unsigned short type)
Sets the corresponding bit for type in filter.
Definition: option.h:119
coap_context_t * coap_new_context(const coap_address_t *listen_addr)
Creates a new coap_context_t object that will hold the CoAP stack status.
Definition: net.c:296
unsigned short message_id
The last message id that was used is stored in this field.
Definition: net.h:135
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:699
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:771
coap_tid_t id
unique transaction id
Definition: net.h:60
static void coap_register_response_handler(coap_context_t *context, coap_response_handler_t handler)
Registers a new message handler that is called whenever a response was received that matches an ongoi...
Definition: net.h:154
coap_tid_t coap_send_ack(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
Definition: net.c:534
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:109
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:62
coap_tick_t t
when to send PDU for the next time
Definition: net.h:54
#define COAP_MID_CACHE_SIZE
Definition: net.h:90
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
Header structure for CoAP PDUs.
Definition: pdu.h:206
coap_tid_t coap_send_message_type(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request, unsigned char type)
Helper funktion to create and send a message with type (usually ACK or RST).
Definition: net.c:681
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
Definition: net.c:143
coap_response_handler_t response_handler
Definition: net.h:143
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context's queues.
Definition: net.c:1555
static coap_tid_t id
Definition: tiny.c:23
representation of network addresses
struct coap_queue_t * next
Definition: net.h:52
coap_pdu_t * coap_new_error_response(coap_pdu_t *request, unsigned char code, coap_opt_filter_t opts)
Creates a new ACK PDU with specified error code.
Definition: net.c:1051
unsigned int timeout
the randomized timeout value
Definition: net.h:56
void coap_cancel_all_messages(coap_context_t *context, const coap_address_t *dst, const unsigned char *token, size_t token_length)
Cancels all outstanding messages for peer dst that have the specified token.
Definition: net.c:1002
unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3)+1]
Fixed-size bit-vector we use for option filtering.
Definition: option.h:93
int coap_remove_from_queue(coap_queue_t **queue, coap_tid_t id, coap_queue_t **node)
This function removes the element with given id from the list given list.
Definition: net.c:952
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:217
int flags
Definition: client.c:26
static coap_tid_t coap_send_rst(coap_context_t *context, const coap_address_t *dst, coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
Definition: net.h:325
coap_queue_t * coap_new_node()
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:237
coap_queue_t * recvqueue
Definition: net.h:110
static unsigned short coap_new_message_id(coap_context_t *context)
Returns a new message id and updates context->message_id accordingly.
Definition: net.h:197
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:253
void coap_dispatch(coap_context_t *context)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1452
int coap_read(coap_context_t *context)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:831
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:228
coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_address_t *dst, unsigned char code, coap_opt_filter_t opts)
Sends an error response with code code for request request to dst.
Definition: net.c:660
Pseudo Random Numbers.
struct coap_queue_t coap_queue_t
void coap_free_context(coap_context_t *context)
Definition: net.c:419
void(* coap_response_handler_t)(struct coap_context_t *, const coap_address_t *remote, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id)
Message handler that is used as call-back in coap_context_t.
Definition: net.h:84
The CoAP stack's global state is stored in a coap_context_t object.
Definition: net.h:97
struct coap_resource_t * resources
hash table or list of known resources
Definition: net.h:100
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by node->t.
Definition: net.c:180
coap_opt_filter_t known_options
Definition: net.h:98