18 #elif HAVE_SYS_UNISTD_H
19 #include <sys/unistd.h>
21 #include <sys/types.h>
22 #ifdef HAVE_SYS_SOCKET_H
23 #include <sys/socket.h>
25 #ifdef HAVE_NETINET_IN_H
26 #include <netinet/in.h>
28 #ifdef HAVE_ARPA_INET_H
29 #include <arpa/inet.h>
33 #include <lwip/pbuf.h>
35 #include <lwip/timers.h>
48 #if defined(WITH_POSIX)
64 #include <lwip/memp.h>
66 static void coap_retransmittimer_execute(
void *arg);
76 memp_free(MEMP_COAP_NODE, node);
82 # define DEBUG DEBUG_PRINT
86 #include "net/uip-debug.h"
88 clock_time_t clock_offset;
90 #define UIP_IP_BUF ((struct uip_ip_hdr *)&uip_buf[UIP_LLH_LEN])
91 #define UIP_UDP_BUF ((struct uip_udp_hdr *)&uip_buf[UIP_LLIPH_LEN])
93 void coap_resources_init();
94 void coap_pdu_resources_init();
96 unsigned char initialized = 0;
101 PROCESS(coap_retransmit_process,
"message retransmit process");
110 memb_free(&node_storage, node);
122 static void received_package(
void *arg,
struct udp_pcb *upcb,
struct pbuf *p, ip_addr_t *addr, u16_t port)
126 LWIP_ASSERT(
"pending_package was not cleared.", context->pending_package == NULL);
128 context->pending_package = p;
129 context->pending_address.addr = addr->addr;
130 context->pending_port = port;
144 unsigned int result = 0;
159 while (q && (t + q->
t < (coap_tick_t)delta)) {
168 q->
t = (coap_tick_t)delta - t;
182 if ( !queue || !node )
193 if (node->
t < q->
t) {
205 }
while (q && q->
t <= node->
t);
222 coap_free_node(node);
239 node = coap_malloc_node();
243 coap_log(LOG_WARNING,
"coap_new_node: malloc\n");
248 memset(node, 0,
sizeof *node );
276 #ifdef COAP_DEFAULT_WKC_HASHKEY
278 #define is_wkc(Key) \
279 (memcmp((Key), COAP_DEFAULT_WKC_HASHKEY, sizeof(coap_key_t)) == 0)
286 static unsigned char _initialized = 0;
291 return memcmp(k, wkc,
sizeof(
coap_key_t)) == 0;
297 const coap_address_t *listen_addr) {
313 coap_log(LOG_EMERG,
"no listen address specified\n");
321 prng_init((
unsigned long)listen_addr ^ clock_offset);
327 coap_log(LOG_EMERG,
"coap_init: malloc:\n");
333 coap_resources_init();
334 coap_pdu_resources_init();
336 c = &the_coap_context;
359 c->sockfd = socket(listen_addr->addr.sa.sa_family, SOCK_DGRAM, 0);
360 if ( c->sockfd < 0 ) {
362 coap_log(LOG_EMERG,
"coap_new_context: socket\n");
367 if ( setsockopt( c->sockfd, SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse) ) < 0 ) {
369 coap_log(LOG_WARNING,
"setsockopt SO_REUSEADDR\n");
373 if (bind(c->sockfd, &listen_addr->addr.sa, listen_addr->size) < 0) {
375 coap_log(LOG_EMERG,
"coap_new_context: bind\n");
383 if ( c->sockfd >= 0 )
390 c->conn = udp_new(NULL, 0, NULL);
391 udp_bind(c->conn, listen_addr->port);
393 process_start(&coap_retransmit_process, (
char *)c);
395 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
396 #ifndef WITHOUT_OBSERVE
400 etimer_set(&the_coap_context.retransmit_timer, 0xFFFF);
401 PROCESS_CONTEXT_END(&coap_retransmit_process);
407 LWIP_ASSERT(
"Failed to allocate PCB for CoAP", c->pcb != NULL);
409 udp_recv(c->pcb, received_package, (
void*)c);
410 udp_bind(c->pcb, &listen_addr->addr, listen_addr->port);
412 c->timer_configured = 0;
420 #if defined(WITH_POSIX) || defined(WITH_LWIP)
422 #ifndef COAP_RESOURCES_NOHASH
434 coap_retransmittimer_restart(context);
437 #if defined(WITH_POSIX) || defined(WITH_LWIP)
438 #ifdef COAP_RESOURCES_NOHASH
449 close( context->sockfd );
453 udp_remove(context->pcb);
454 memp_free(MEMP_COAP_CONTEXT, context);
480 if (opt_iter.
type & 0x01 &&
482 debug(
"unknown critical option %d\n", opt_iter.
type);
508 switch (peer->addr.sa.sa_family) {
510 coap_hash((
const unsigned char *)&peer->addr.sa, peer->size, h);
513 coap_hash((
const unsigned char *)&peer->addr.sin6.sin6_port,
514 sizeof(peer->addr.sin6.sin6_port), h);
515 coap_hash((
const unsigned char *)&peer->addr.sin6.sin6_addr,
516 sizeof(peer->addr.sin6.sin6_addr), h);
522 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
524 coap_hash((
const unsigned char *)&peer->port,
sizeof(peer->port), h);
525 coap_hash((
const unsigned char *)&peer->addr,
sizeof(peer->addr), h);
528 coap_hash((
const unsigned char *)&pdu->
hdr->
id,
sizeof(
unsigned short), h);
530 *
id = ((h[0] << 8) | h[1]) ^ ((h[2] << 8) | h[3]);
535 const coap_address_t *dst,
544 result =
coap_send(context, dst, response);
555 const coap_address_t *dst,
557 ssize_t bytes_written;
560 if ( !context || !dst || !pdu )
563 bytes_written = sendto( context->sockfd, pdu->
hdr, pdu->
length, 0,
564 &dst->addr.sa, dst->size);
566 if (bytes_written >= 0) {
569 coap_log(LOG_CRIT,
"coap_send: sendto\n");
579 const coap_address_t *dst,
583 if ( !context || !dst || !pdu )
587 uip_udp_packet_sendto(context->conn, pdu->
hdr, pdu->
length,
588 &dst->addr, dst->port);
598 const coap_address_t *dst,
605 if ( !context || !dst || !pdu )
610 data_backup = pdu->
data;
617 LWIP_ASSERT(
"The PDU header is not where it is expected", pdu->
hdr == p->payload +
sizeof(
coap_pdu_t));
622 debug(
"coap_send_impl: pbuf_header failed\n");
629 pbuf_realloc(p, pdu->
length);
631 udp_sendto(context->pcb, p,
632 &dst->addr, dst->port);
634 pbuf_header(p, -(ptrdiff_t)((uint8_t*)pdu - (uint8_t*)p->payload) -
sizeof(
coap_pdu_t));
637 LWIP_ASSERT(
"Cannot undo pbuf_header", err == 0);
640 LWIP_ASSERT(
"PDU not restored", p->payload == pdu);
645 pdu->
data = data_backup;
654 const coap_address_t *dst,
656 return coap_send_impl(context, dst, pdu);
662 const coap_address_t *dst,
673 result =
coap_send(context, dst, response);
682 const coap_address_t *dst,
684 unsigned char type) {
691 result =
coap_send(context, dst, response);
700 const coap_address_t *dst,
708 debug(
"coap_send_confirmed: insufficient memory\n");
712 node->
id = coap_send_impl(context, dst, pdu);
714 debug(
"coap_send_confirmed: error sending pdu\n");
715 coap_free_node(node);
719 prng((
unsigned char *)&r,
sizeof(r));
724 ((COAP_TICKS_PER_SECOND * (r & 0xFF)) >> 8);
726 memcpy(&node->
remote, dst,
sizeof(coap_address_t));
750 coap_retransmittimer_restart(context);
761 PROCESS_CONTEXT_BEGIN(&coap_retransmit_process);
762 etimer_set(&context->retransmit_timer, nextpdu->
t);
763 PROCESS_CONTEXT_END(&coap_retransmit_process);
772 if ( !context || !node )
782 coap_retransmittimer_restart(context);
785 debug(
"** retransmission #%d of transaction %d\n",
788 node->
id = coap_send_impl(context, &node->
remote, node->
pdu);
795 debug(
"** removed transaction %d\n", ntohs(node->
id));
798 #ifndef WITHOUT_OBSERVE
802 str token = { 0, NULL };
823 if (opt && opt < maxpos) {
824 if (((*opt & 0x0f) < 0x0f) || (opt + 1 < maxpos))
835 #if defined(WITH_LWIP) || defined(WITH_CONTIKI)
839 ssize_t bytes_read = -1;
840 coap_address_t src, dst;
847 LWIP_ASSERT(
"No package pending", ctx->pending_package != NULL);
848 LWIP_ASSERT(
"Can only deal with contiguous PBUFs to read the initial details", ctx->pending_package->tot_len == ctx->pending_package->len);
849 buf = ctx->pending_package->payload;
857 bytes_read = recvfrom(ctx->sockfd, buf,
sizeof(buf), 0,
858 &src.addr.sa, &src.size);
862 uip_ipaddr_copy(&src.addr, &UIP_IP_BUF->srcipaddr);
863 src.port = UIP_UDP_BUF->srcport;
864 uip_ipaddr_copy(&dst.addr, &UIP_IP_BUF->destipaddr);
865 dst.port = UIP_UDP_BUF->destport;
867 bytes_read = uip_datalen();
868 ((
char *)uip_appdata)[bytes_read] = 0;
869 PRINTF(
"Server received %d bytes from [", (
int)bytes_read);
870 PRINT6ADDR(&src.addr);
871 PRINTF(
"]:%d\n", uip_ntohs(src.port));
876 src.addr.addr = ctx->pending_address.addr;
877 src.port = ctx->pending_port;
878 bytes_read = ctx->pending_package->tot_len;
881 if ( bytes_read < 0 ) {
882 warn(
"coap_read: recvfrom");
886 if ( (
size_t)bytes_read <
sizeof(
coap_hdr_t) ) {
887 debug(
"coap_read: discarded invalid frame\n" );
892 debug(
"coap_read: unknown protocol version\n" );
901 node->
pdu = coap_pdu_from_pbuf(ctx->pending_package);
902 ctx->pending_package = NULL;
910 memcpy(&node->
local, &dst,
sizeof(coap_address_t));
911 memcpy(&node->
remote, &src,
sizeof(coap_address_t));
914 warn(
"discard malformed PDU");
924 #ifndef INET6_ADDRSTRLEN
925 #define INET6_ADDRSTRLEN 40
930 debug(
"** received %d bytes from %s:\n", (
int)bytes_read, addr);
945 pbuf_free(ctx->pending_package);
946 ctx->pending_package = NULL;
955 if ( !queue || !*queue)
960 if (
id == (*queue)->id ) {
962 *queue = (*queue)->
next;
964 (*queue)->
t += (*node)->t;
966 (*node)->next = NULL;
968 debug(
"*** removed transaction %u\n",
id);
977 }
while ( q &&
id != q->
id );
987 debug(
"*** removed transaction %u\n",
id);
997 const unsigned char *b,
size_t blen) {
998 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
1003 const unsigned char *token,
size_t token_length) {
1008 debug(
"cancel_all_messages\n");
1016 debug(
"**** removed transaction %d\n", ntohs(q->
pdu->
hdr->
id));
1032 debug(
"**** removed transaction %d\n", ntohs(q->
pdu->
hdr->
id));
1044 while (queue && queue->
id !=
id)
1045 queue = queue->
next;
1058 unsigned short opt_type = 0;
1065 size += strlen(phrase) + 1;
1087 unsigned short delta = opt_iter.
type - opt_type;
1091 }
else if (delta < 269) {
1101 switch (*option & 0x0f) {
1112 opt_type = opt_iter.
type;
1121 debug(
"cannot add token to error response\n");
1133 #if COAP_ERROR_PHRASE_LENGTH > 0
1136 coap_add_data(response, strlen(phrase), (
unsigned char *)phrase);
1147 static inline size_t
1149 unsigned char buf[1];
1154 warn(
"cannot determine length of /.well-known/core\n");
1158 debug(
"get_wkc_len: print_wellknown() returned %zu\n", len);
1163 #define SZX_TO_BYTES(SZX) ((size_t)(1 << ((SZX) + 4)))
1169 size_t len, wkc_len;
1170 unsigned char buf[2];
1172 int need_block2 = 0;
1183 debug(
"wellknown_response: cannot create PDU\n");
1188 debug(
"wellknown_response: cannot add token\n");
1197 offset = block.
num << (block.
szx + 4);
1198 if (block.
szx > 6) {
1203 block.
num = offset >> (block.
szx + 4);
1214 debug(
"wellknown_response: insufficient storage space\n");
1227 if (!need_block2 && (resp->
max_size - (
size_t)resp->
length < wkc_len)) {
1235 if (block.
szx == 0) {
1236 debug(
"wellknown_response: message to small even for szx == 0\n");
1249 debug(
"wellknown_response: cannot add Block2 option\n");
1265 debug(
"print_wellknown failed\n");
1279 #define WANT_WKC(Pdu,Key) \
1280 (((Pdu)->hdr->code == COAP_REQUEST_GET) && is_wkc(Key))
1310 debug(
"GET for unknown resource 0x%02x%02x%02x%02x, return 4.04\n",
1311 key[0], key[1], key[2], key[3]);
1320 debug(
"unhandled request for unknown resource 0x%02x%02x%02x%02x\r\n",
1321 key[0], key[1], key[2], key[3]);
1328 warn(
"cannot send response for transaction %u\n", node->
id);
1341 debug(
"call custom handler for resource 0x%02x%02x%02x%02x\n",
1342 key[0], key[1], key[2], key[3]);
1354 h(context, resource, &node->
remote,
1355 node->
pdu, &token, response);
1356 if (response->
hdr->
type != COAP_MESSAGE_NON ||
1360 debug(
"cannot send response for message %d\n", node->
pdu->
hdr->
id);
1366 warn(
"cannot generate response\r\n");
1378 debug(
"cannot send response for transaction %u\n", node->
id);
1393 rcvd->
pdu, rcvd->
id);
1417 #ifndef WITHOUT_OBSERVE
1419 #ifndef COAP_RESOURCES_NOHASH
1422 str token = { 0, NULL };
1430 #ifndef WITH_CONTIKI
1431 #ifdef COAP_RESOURCES_NOHASH
1441 for (i = 0; i < resource_storage.num; ++i, ++r) {
1442 if (resource_storage.count[i]) {
1487 { sent->pdu->hdr->token_length, sent->pdu->hdr->token };
1497 coap_log(LOG_ALERT,
"got RST for message %u\n", ntohs(rcvd->
pdu->
hdr->
id));
1520 warn(
"coap_dispatch: cannot create error reponse\n");
1524 warn(
"coap_dispatch: error sending reponse\n");
1542 debug(
"dropped message with invalid code\n");
1564 PROCESS_THREAD(coap_retransmit_process, ev, data)
1571 debug(
"Started retransmit process\r\n");
1575 if (ev == PROCESS_EVENT_TIMER) {
1576 if (etimer_expired(&the_coap_context.retransmit_timer)) {
1581 while (nextpdu && nextpdu->
t <= now) {
1587 etimer_set(&the_coap_context.retransmit_timer,
1588 nextpdu ? nextpdu->
t - now : 0xFFFF);
1590 #ifndef WITHOUT_OBSERVE
1591 if (etimer_expired(&the_coap_context.notify_timer)) {
1593 etimer_reset(&the_coap_context.notify_timer);
1619 static void coap_retransmittimer_execute(
void *arg)
1623 coap_tick_t elapsed;
1626 ctx->timer_configured = 0;
1633 while (nextinqueue != NULL)
1635 if (nextinqueue->
t > elapsed) {
1636 nextinqueue->
t -= elapsed;
1639 elapsed -= nextinqueue->
t;
1647 coap_retransmittimer_restart(ctx);
1652 coap_tick_t now, elapsed, delay;
1654 if (ctx->timer_configured)
1656 printf(
"clearing\n");
1657 sys_untimeout(coap_retransmittimer_execute, (
void*)ctx);
1658 ctx->timer_configured = 0;
1681 printf(
"scheduling for %d ticks\n", delay);
1682 sys_timeout(delay, coap_retransmittimer_execute, (
void*)ctx);
1683 ctx->timer_configured = 1;
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
#define LL_FOREACH(head, el)
#define COAP_OPTION_IF_MATCH
#define COAP_DEFAULT_RESPONSE_TIMEOUT
void coap_check_notify(coap_context_t *context)
Checks for all known resources, if they are dirty and notifies subscribed observers.
int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
unsigned char coap_key_t[4]
#define COAP_RESPONSE_CODE(N)
#define COAP_DEFAULT_URI_WELLKNOWN
well-known resources URI
void coap_dispatch(coap_context_t *context)
Dispatches the PDUs from the receive queue in given context.
#define COAP_OPTION_PROXY_URI
#define COAP_OPTION_CONTENT_FORMAT
static void coap_handle_rst(coap_context_t *context, const coap_queue_t *sent)
This function handles RST messages received for the message passed in sent.
unsigned char retransmit_cnt
retransmission counter, will be removed when zero
int coap_delete_resource(coap_context_t *context, coap_key_t key)
Deletes a resource identified by key.
coap_address_t local
local address
void coap_show_pdu(const coap_pdu_t *pdu)
#define COAP_PRINT_OUTPUT_LENGTH(v)
state management for asynchronous messages
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.
#define HASH_ITER(hh, head, el, tmp)
unsigned short length
PDU length (including header, options, data)
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned char type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
void handle_request(coap_context_t *context, coap_queue_t *node)
static void coap_register_option(coap_context_t *ctx, unsigned char type)
Registers the option type type with the given context object ctx.
static int check_opt_size(coap_opt_t *opt, unsigned char *maxpos)
Checks if opt fits into the message that ends with maxpos.
#define COAP_OPTION_BLOCK1
int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *pdu)
Parses data into the CoAP PDU structure given in result.
coap_address_t remote
remote address
#define coap_malloc(size)
void coap_handle_failed_notify(coap_context_t *, const coap_address_t *, const str *)
#define prng_init(Value)
Called to set the PRNG seed.
static int coap_option_getb(const coap_opt_filter_t filter, unsigned short type)
Gets the corresponding bit for type in filter.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
coap_log_t coap_get_log_level()
Returns the current log level.
#define COAP_OPTION_CONTENT_TYPE
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.
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.
#define WANT_WKC(Pdu, Key)
unsigned short message_id
The last message id that was used is stored in this field.
unsigned short max_delta
highest option number
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.
coap_queue_t * coap_peek_next(coap_context_t *context)
Returns the next pdu to send without removing from sendqeue.
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.
#define COAP_OPTION_PROXY_SCHEME
#define COAP_RESOURCE_CHECK_TIME
The interval in seconds to check if resources have changed.
#define COAP_SET_STR(st, l, v)
static int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_hash_request_uri(const coap_pdu_t *request, coap_key_t key)
Calculates the hash key for the resource requested by the Uri-Options of request. ...
#define SZX_TO_BYTES(SZX)
coap_queue_t * coap_find_transaction(coap_queue_t *queue, coap_tid_t id)
Retrieves transaction from queue.
coap_tid_t id
unique transaction id
static size_t get_wkc_len(coap_context_t *context, coap_opt_t *query_filter)
Quick hack to determine the size of the resource description for .well-known/core.
coap_tick_t sendqueue_basetime
The time stamp in the first element of the sendqeue is relative to sendqueue_basetime.
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.
size_t coap_print_addr(const struct coap_address_t *addr, unsigned char *buf, size_t len)
coap_pdu_t * pdu
the CoAP PDU to send
coap_tick_t t
when to send PDU for the next time
#define COAP_DEFAULT_VERSION
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.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
Header structure for CoAP PDUs.
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node)
Adds node to given queue, ordered by node->t.
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.
coap_opt_iterator_t * coap_option_iterator_init(coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list...
coap_key_t key
the actual key bytes for this resource
#define COAP_MAX_BLOCK_SZX
The largest value for the SZX component in a Block option.
coap_response_handler_t response_handler
#define COAP_OPTION_URI_PORT
#define COAP_DEFAULT_MAX_RETRANSMIT
int print_wellknown(coap_context_t *, unsigned char *, size_t *, size_t, coap_opt_t *)
Prints the names of all known resources to buf.
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
size_t max_size
allocated storage for options and data
struct coap_queue_t * next
#define COAP_RESPONSE_CLASS(C)
#define COAP_OPTION_IF_NONE_MATCH
Iterator to run through PDU options.
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
de-duplicate code with coap_add_option_later
unsigned int timeout
the randomized timeout value
generic resource handling
#define COAP_MAX_PDU_SIZE
#define COAP_OPT_SIZE(opt)
unsigned char coap_opt_filter_t[(COAP_MAX_OPT >> 3)+1]
Fixed-size bit-vector we use for option filtering.
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
#define coap_hash(String, Length, Result)
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.
coap_pdu_t * wellknown_response(coap_context_t *context, coap_pdu_t *request)
unsigned short type
decoded option type
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.
#define COAP_OPTION_BLOCK2
void coap_delete_pdu(coap_pdu_t *pdu)
static void coap_address_init(coap_address_t *addr)
Resets the given coap_address_t object addr to its default values.
void coap_delete_all(coap_queue_t *queue)
Removes all items from given queue and frees the allocated storage.
coap_pdu_t * coap_pdu_init(unsigned char type, unsigned char code, unsigned short id, size_t size)
Creates a new CoAP PDU of given size (must be large enough to hold the basic CoAP message header (coa...
Structure of Block options.
static int handle_locally(coap_context_t *context, coap_queue_t *node)
static void coap_option_filter_clear(coap_opt_filter_t f)
Clears filter f.
int coap_hash_path(const unsigned char *path, size_t len, coap_key_t key)
Calculates a hash over the given path and stores the result in key.
#define COAP_PAYLOAD_START
void coap_free_context(coap_context_t *context)
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.
#define COAP_OPT_LENGTH(opt)
unsigned int token_length
#define COAP_OPTION_URI_PATH
void coap_touch_observer(coap_context_t *context, const coap_address_t *observer, const str *token)
Marks an observer as alive.
#define COAP_ERROR_PHRASE_LENGTH
maximum length of error phrase
#define COAP_PRINT_STATUS_ERROR
#define COAP_OPTION_URI_QUERY
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
#define COAP_MESSAGE_IS_REQUEST(MSG)
#define COAP_OPTION_ACCEPT
coap_queue_t * coap_new_node()
Creates a new node suitable for adding to the CoAP sendqueue.
static int token_match(const unsigned char *a, size_t alen, const unsigned char *b, size_t blen)
void coap_delete_observer(coap_resource_t *resource, const coap_address_t *observer, const str *token)
Removes any subscription for observer from resource and releases the allocated storage.
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
int coap_read(coap_context_t *ctx)
Reads data from the network and tries to parse as CoAP PDU.
void(* coap_method_handler_t)(coap_context_t *, struct coap_resource_t *, coap_address_t *, coap_pdu_t *, str *, coap_pdu_t *)
Definition of message handler function (.
#define prng(Buf, Length)
Fills Buf with Length bytes of random data.
static int coap_option_clrb(coap_opt_filter_t filter, unsigned short type)
Clears the corresponding bit for type in filter.
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.
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).
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node)
Handles retransmissions of confirmable messages.
#define COAP_OPTION_URI_HOST
coap_queue_t * coap_pop_next(coap_context_t *context)
Returns the next pdu to send and removes it from the sendqeue.
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
unsigned char * data
payload
#define COAP_OPT_VALUE(opt)
The CoAP stack's global state is stored in a coap_context_t object.
static int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
#define COAP_MESSAGE_IS_RESPONSE(MSG)
int coap_delete_node(coap_queue_t *node)
Destroys specified node.
coap_resource_t * coap_get_resource_from_key(coap_context_t *context, coap_key_t key)
Returns the resource identified by the unique string key.
struct coap_resource_t * resources
hash table or list of known resources
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now)
Set sendqueue_basetime in the given context object ctx to now.
coap_method_handler_t handler[4]
Used to store handlers for the four coap methods GET, POST, PUT, and DELETE.
static void handle_response(coap_context_t *context, coap_queue_t *sent, coap_queue_t *rcvd)
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size
#define is_wkc(Key)
Checks if Key is equal to the pre-defined hash key for.well-known/core.
unsigned int num
block number
coap_opt_filter_t known_options