libcoap  4.1.2
net.h
Go to the documentation of this file.
1 /*
2  * net.h -- CoAP network interface
3  *
4  * Copyright (C) 2010-2015 Olaf Bergmann <bergmann@tzi.org>
5  *
6  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef _COAP_NET_H_
11 #define _COAP_NET_H_
12 
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/time.h>
17 #include <time.h>
18 
19 #ifdef WITH_LWIP
20 #include <lwip/ip_addr.h>
21 #endif
22 
23 #include "coap_io.h"
24 #include "coap_time.h"
25 #include "option.h"
26 #include "pdu.h"
27 #include "prng.h"
28 
29 struct coap_queue_t;
30 
31 typedef struct coap_queue_t {
32  struct coap_queue_t *next;
34  unsigned char retransmit_cnt;
36  unsigned int timeout;
41 } coap_queue_t;
42 
44 int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
45 
48 
50 void coap_delete_all(coap_queue_t *queue);
51 
54 
55 struct coap_resource_t;
56 struct coap_context_t;
57 #ifndef WITHOUT_ASYNC
58 struct coap_async_state_t;
59 #endif
60 
62 typedef void (*coap_response_handler_t)(struct coap_context_t *,
63  const coap_endpoint_t *local_interface,
64  const coap_address_t *remote,
65  coap_pdu_t *sent,
66  coap_pdu_t *received,
67  const coap_tid_t id);
68 
69 #define COAP_MID_CACHE_SIZE 3
70 typedef struct {
71  unsigned char flags[COAP_MID_CACHE_SIZE];
74 
77 typedef struct coap_context_t {
82 
83 #ifndef WITHOUT_ASYNC
84 
87 #endif /* WITHOUT_ASYNC */
88 
96 #ifdef WITH_POSIX
97  int sockfd;
98 #endif /* WITH_POSIX */
99 
100 #ifdef WITH_CONTIKI
101  struct uip_udp_conn *conn;
102  struct etimer retransmit_timer;
103  struct etimer notify_timer;
104 #endif /* WITH_CONTIKI */
105 
106 #ifdef WITH_LWIP
107  uint8_t timer_configured;
110 #endif /* WITH_LWIP */
111 
117  unsigned short message_id;
118 
123  unsigned int observe;
124 
126 
127  ssize_t (*network_send)(struct coap_context_t *context,
128  const coap_endpoint_t *local_interface,
129  const coap_address_t *dst,
130  unsigned char *data, size_t datalen);
131 
132  ssize_t (*network_read)(coap_endpoint_t *ep, coap_packet_t **packet);
133 
135 
143 static inline void
145  coap_response_handler_t handler) {
146  context->response_handler = handler;
147 }
148 
155 inline static void
156 coap_register_option(coap_context_t *ctx, unsigned char type) {
157  coap_option_setb(ctx->known_options, type);
158 }
159 
165 unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
166 
171 
176 
180 coap_context_t *coap_new_context(const coap_address_t *listen_addr);
181 
191 static inline unsigned short
193  context->message_id++;
194 #ifndef WITH_CONTIKI
195  return htons(context->message_id);
196 #else /* WITH_CONTIKI */
197  return uip_htons(context->message_id);
198 #endif
199 }
200 
207 void coap_free_context(coap_context_t *context);
208 
209 
225  const coap_endpoint_t *local_interface,
226  const coap_address_t *dst,
227  coap_pdu_t *pdu);
228 
247  unsigned char code,
248  coap_opt_filter_t opts);
249 
265  const coap_endpoint_t *local_interface,
266  const coap_address_t *dst,
267  coap_pdu_t *pdu);
268 
288  coap_pdu_t *request,
289  const coap_endpoint_t *local_interface,
290  const coap_address_t *dst,
291  unsigned char code,
292  coap_opt_filter_t opts);
293 
310  const coap_endpoint_t *local_interface,
311  const coap_address_t *dst,
312  coap_pdu_t *request,
313  unsigned char type);
314 
330  const coap_endpoint_t *local_interface,
331  const coap_address_t *dst,
332  coap_pdu_t *request);
333 
348 static inline coap_tid_t
350  const coap_endpoint_t *local_interface,
351  const coap_address_t *dst,
352  coap_pdu_t *request) {
353  return coap_send_message_type(context,
354  local_interface,
355  dst, request,
357 }
358 
363 
369 int coap_read(coap_context_t *context);
370 
383  coap_packet_t *packet);
384 
393 void coap_transaction_id(const coap_address_t *peer,
394  const coap_pdu_t *pdu,
395  coap_tid_t *id);
396 
413  coap_tid_t id,
414  coap_queue_t **node);
415 
426 inline static int
428  coap_queue_t *node;
429  if (!coap_remove_from_queue(queue, id, &node))
430  return 0;
431 
432  coap_delete_node(node);
433  return 1;
434 }
435 
445 
456  const coap_address_t *dst,
457  const unsigned char *token,
458  size_t token_length);
459 
463 void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd);
464 
468 int coap_can_exit(coap_context_t *context);
469 
474 void coap_ticks(coap_tick_t *);
475 
508  coap_pdu_t *pdu,
509  coap_opt_filter_t unknown);
510 
522  coap_pdu_t *request);
523 
524 #endif /* _COAP_NET_H_ */
coap_queue_t * sendqueue
Definition: net.h:93
unsigned int observe
The next value to be used for Observe.
Definition: net.h:123
unsigned char coap_key_t[4]
Definition: hashkey.h:20
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:298
void coap_dispatch(coap_context_t *context, coap_queue_t *rcvd)
Dispatches the PDUs from the receive queue in given context.
Definition: net.c:1539
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:496
void(* coap_response_handler_t)(struct coap_context_t *, const coap_endpoint_t *local_interface, 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:62
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
Definition: net.h:34
ssize_t(* network_read)(coap_endpoint_t *ep, coap_packet_t **packet)
Definition: net.h:132
#define COAP_MESSAGE_RST
Definition: pdu.h:46
coap_endpoint_t * endpoint
The endpoint used for listening.
Definition: net.h:94
multi-purpose address abstraction
Definition: address.h:57
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
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:444
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:156
coap_endpoint_t local_if
the local address interface
Definition: net.h:37
coap_tid_t coap_send_confirmed(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a confirmed CoAP message to given destination.
Definition: net.c:702
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:427
static coap_tid_t coap_send_rst(coap_context_t *context, const coap_endpoint_t *local_interface, 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:349
coap_address_t remote
remote address
Definition: net.h:38
struct coap_context_t coap_context_t
The CoAP stack&#39;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 the queue.
Definition: net.c:1024
struct coap_async_state_t * async_state
The list of asynchronous transactions.
Definition: net.h:86
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:199
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:333
unsigned short message_id
The last message id that was used is stored in this field.
Definition: net.h:117
unsigned long coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition: coap_time.h:84
coap_tid_t coap_send_error(coap_context_t *context, coap_pdu_t *request, const coap_endpoint_t *local_interface, 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:624
Abstraction of virtual endpoint that can be attached to coap_context_t.
Definition: coap_io.h:39
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
Definition: net.c:774
coap_tid_t id
unique transaction id
Definition: net.h:39
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:144
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
Definition: net.h:92
coap_pdu_t * pdu
the CoAP PDU to send
Definition: net.h:40
coap_tick_t t
when to send PDU for the next time
Definition: net.h:33
#define COAP_MID_CACHE_SIZE
Definition: net.h:69
Header structure for CoAP PDUs.
Definition: pdu.h:227
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:180
coap_response_handler_t response_handler
Definition: net.h:125
coap_pdu_t * coap_wellknown_response(coap_context_t *context, coap_pdu_t *request)
Creates a new response for given request with the contents of .well-known/core.
Definition: net.c:1147
int coap_can_exit(coap_context_t *context)
Returns 1 if there are no messages to send or to dispatch in the context&#39;s queues.
Definition: net.c:1646
struct coap_queue_t * next
Definition: net.h:32
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
Definition: coap_time.c:49
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:1032
unsigned int timeout
the randomized timeout value
Definition: net.h:36
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:984
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:934
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
Definition: net.c:254
int coap_handle_message(coap_context_t *ctx, coap_packet_t *packet)
Parses and interprets a CoAP message with context ctx.
Definition: net.c:849
Pre-defined constants that reflect defaults for CoAP.
uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE]
Fixed-size vector we use for option filtering.
Definition: option.h:135
coap_tid_t coap_send_message_type(coap_context_t *context, const coap_endpoint_t *local_interface, 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:646
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:192
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
Definition: net.c:290
int sockfd
send/receive socket
Definition: net.h:97
int coap_read(coap_context_t *context)
Reads data from the network and tries to parse as CoAP PDU.
Definition: net.c:823
coap_tid_t coap_send(coap_context_t *context, const coap_endpoint_t *local_interface, const coap_address_t *dst, coap_pdu_t *pdu)
Sends a non-confirmed CoAP message to given destination.
Definition: net.c:616
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
Definition: net.c:265
Pseudo Random Numbers.
coap_queue_t * coap_new_node(void)
Creates a new node suitable for adding to the CoAP sendqueue.
Definition: net.c:274
struct coap_queue_t coap_queue_t
void coap_free_context(coap_context_t *context)
CoAP stack context must be released with coap_free_context().
Definition: net.c:419
coap_tid_t coap_send_ack(coap_context_t *context, const coap_endpoint_t *local_interface, 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
The CoAP stack&#39;s global state is stored in a coap_context_t object.
Definition: net.h:77
struct coap_resource_t * resources
< The hash table or list of known resources.
Definition: net.h:81
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by node->t.
Definition: net.c:217
coap_opt_filter_t known_options
Definition: net.h:78