gcoap.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2017 Ken Bannister. All rights reserved.
3  * 2017 Freie Universit├Ąt Berlin
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser
6  * General Public License v2.1. See the file LICENSE in the top level
7  * directory for more details.
8  */
9 
345 #ifndef NET_GCOAP_H
346 #define NET_GCOAP_H
347 
348 #include <stdint.h>
349 
350 #include "net/ipv6/addr.h"
351 #include "net/sock/udp.h"
352 #include "net/nanocoap.h"
353 #include "xtimer.h"
354 
355 #ifdef __cplusplus
356 extern "C" {
357 #endif
358 
368 #ifndef CONFIG_GCOAP_MSG_QUEUE_SIZE
369 #define CONFIG_GCOAP_MSG_QUEUE_SIZE (4)
370 #endif
371 
375 #ifndef CONFIG_GCOAP_PORT
376 #define CONFIG_GCOAP_PORT (5683)
377 #endif
378 
382 #ifndef CONFIG_GCOAP_PDU_BUF_SIZE
383 #define CONFIG_GCOAP_PDU_BUF_SIZE (128)
384 #endif
385 
395 #ifndef CONFIG_GCOAP_REQ_OPTIONS_BUF
396 #define CONFIG_GCOAP_REQ_OPTIONS_BUF (4)
397 #endif
398 
408 #ifndef CONFIG_GCOAP_RESP_OPTIONS_BUF
409 #define CONFIG_GCOAP_RESP_OPTIONS_BUF (4)
410 #endif
411 
421 #ifndef CONFIG_GCOAP_OBS_OPTIONS_BUF
422 #define CONFIG_GCOAP_OBS_OPTIONS_BUF (4)
423 #endif
424 
428 #ifndef CONFIG_GCOAP_REQ_WAITING_MAX
429 #define CONFIG_GCOAP_REQ_WAITING_MAX (2)
430 #endif
431 
436 #define GCOAP_TOKENLEN_MAX (8)
437 
441 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
442 
449 #ifndef CONFIG_GCOAP_TOKENLEN
450 #define CONFIG_GCOAP_TOKENLEN (2)
451 #endif
452 
456 #define GCOAP_PAYLOAD_MARKER (0xFF)
457 
464 #ifndef CONFIG_GCOAP_NO_AUTO_INIT
465 #define CONFIG_GCOAP_NO_AUTO_INIT 0
466 #endif
467 
472 #define GCOAP_MEMO_UNUSED (0)
473 #define GCOAP_MEMO_WAIT (1)
474 #define GCOAP_MEMO_RESP (2)
475 #define GCOAP_MEMO_TIMEOUT (3)
476 #define GCOAP_MEMO_ERR (4)
482 #define GCOAP_SEND_LIMIT_NON (-1)
483 
488 #ifndef CONFIG_GCOAP_RECV_TIMEOUT
489 #define CONFIG_GCOAP_RECV_TIMEOUT (1 * US_PER_SEC)
490 #endif
491 
492 #ifdef DOXYGEN
493 
502 #define CONFIG_GCOAP_NO_RETRANS_BACKOFF
503 #endif
504 
511 #ifndef CONFIG_GCOAP_NON_TIMEOUT
512 #define CONFIG_GCOAP_NON_TIMEOUT (5000000U)
513 #endif
514 
518 #define GCOAP_MSG_TYPE_TIMEOUT (0x1501)
519 
526 #define GCOAP_MSG_TYPE_INTR (0x1502)
527 
532 #ifndef CONFIG_GCOAP_OBS_CLIENTS_MAX
533 #define CONFIG_GCOAP_OBS_CLIENTS_MAX (2)
534 #endif
535 
540 #ifndef CONFIG_GCOAP_OBS_REGISTRATIONS_MAX
541 #define CONFIG_GCOAP_OBS_REGISTRATIONS_MAX (2)
542 #endif
543 
548 #define GCOAP_OBS_MEMO_UNUSED (0)
549 #define GCOAP_OBS_MEMO_IDLE (1)
550 #define GCOAP_OBS_MEMO_PENDING (2)
573 #ifndef CONFIG_GCOAP_OBS_VALUE_WIDTH
574 #define CONFIG_GCOAP_OBS_VALUE_WIDTH (3)
575 #endif
576 
580 #if (CONFIG_GCOAP_OBS_VALUE_WIDTH == 3)
581 #define GCOAP_OBS_TICK_EXPONENT (5)
582 #elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 2)
583 #define GCOAP_OBS_TICK_EXPONENT (16)
584 #elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 1)
585 #define GCOAP_OBS_TICK_EXPONENT (24)
586 #endif
587 
592 #define GCOAP_OBS_INIT_OK (0)
593 #define GCOAP_OBS_INIT_ERR (-1)
594 #define GCOAP_OBS_INIT_UNUSED (-2)
595 
600 #ifndef GCOAP_STACK_SIZE
601 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE \
602  + sizeof(coap_pkt_t))
603 #endif
604 
609 #ifndef CONFIG_GCOAP_RESEND_BUFS_MAX
610 #define CONFIG_GCOAP_RESEND_BUFS_MAX (1)
611 #endif
612 
617 #define COAP_LINK_FLAG_INIT_RESLIST (1)
624 typedef struct {
625  unsigned content_format;
626  size_t link_pos;
627  uint16_t flags;
630 
642 typedef ssize_t (*gcoap_link_encoder_t)(const coap_resource_t *resource, char *buf,
643  size_t maxlen, coap_link_encoder_ctx_t *context);
644 
648 typedef struct gcoap_listener {
651  size_t resources_len;
655 
667 typedef void (*gcoap_resp_handler_t)(const gcoap_request_memo_t *memo,
669  const sock_udp_ep_t *remote);
670 
674 typedef struct {
675  uint8_t *pdu_buf;
676  size_t pdu_len;
678 
682 struct gcoap_request_memo {
683  unsigned state;
684  int send_limit;
686  union {
687  uint8_t hdr_buf[GCOAP_HEADER_MAXLEN];
689  gcoap_resend_t data;
690  } msg;
692  sock_udp_ep_t remote_ep;
693  gcoap_resp_handler_t resp_handler;
694  void *context;
695  xtimer_t response_timer;
696  msg_t timeout_msg;
697 };
698 
702 typedef struct {
703  sock_udp_ep_t *observer;
704  const coap_resource_t *resource;
705  uint8_t token[GCOAP_TOKENLEN_MAX];
706  unsigned token_len;
708 
719 
726 
741 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
742  unsigned code, const char *path);
743 
767 ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format);
768 
781 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
782  unsigned code, char *path)
783 {
784  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
786  : -1;
787 }
788 
801 size_t gcoap_req_send(const uint8_t *buf, size_t len,
802  const sock_udp_ep_t *remote,
803  gcoap_resp_handler_t resp_handler, void *context);
804 
818 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
819 
831 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
832  size_t len, unsigned code)
833 {
834  return (gcoap_resp_init(pdu, buf, len, code) == 0)
836  : -1;
837 }
838 
854 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
855  const coap_resource_t *resource);
856 
870 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
871  const coap_resource_t *resource);
872 
880 uint8_t gcoap_op_state(void);
881 
900 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
901 
915 ssize_t gcoap_encode_link(const coap_resource_t *resource, char *buf,
916  size_t maxlen, coap_link_encoder_ctx_t *context);
917 
933 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
934 
935 #ifdef __cplusplus
936 }
937 #endif
938 
939 #endif /* NET_GCOAP_H */
940 
int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, const coap_resource_t *resource)
Initializes a CoAP Observe notification packet on a buffer, for the observer registered for a resourc...
ssize_t(* gcoap_link_encoder_t)(const coap_resource_t *resource, char *buf, size_t maxlen, coap_link_encoder_ctx_t *context)
Handler function to write a resource link.
Definition: gcoap.h:643
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:654
Definitions for IPv6 addresses.
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:441
#define COAP_OPT_FINISH_NONE
no special handling required
Definition: nanocoap.h:159
Memo for Observe registration and notifications.
Definition: gcoap.h:703
UDP sock definitions.
uint8_t gcoap_op_state(void)
Provides important operational statistics.
size_t resources_len
Length of array.
Definition: gcoap.h:652
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:436
static ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code, char *path)
Writes a complete CoAP request PDU when there is not a payload.
Definition: gcoap.h:782
ssize_t gcoap_finish(coap_pkt_t *pdu, size_t payload_len, unsigned format)
Finishes formatting a CoAP PDU after the payload has been written.
Memo to handle a response for a request.
Definition: gcoap.h:683
int16_t kernel_pid_t
Unique process identifier.
Definition: kernel_types.h:83
Type for CoAP resource entry.
Definition: nanocoap.h:222
void gcoap_register_listener(gcoap_listener_t *listener)
Starts listening for resource paths.
A modular collection of resources for a server.
Definition: gcoap.h:649
signed int ssize_t
Used for a count of bytes or an error indication.
Definition: msp430_types.h:89
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Initializes a CoAP response packet on a buffer.
void(* gcoap_resp_handler_t)(const gcoap_request_memo_t *memo, coap_pkt_t *pdu, const sock_udp_ep_t *remote)
Handler function for a server response, including the state for the originating request.
Definition: gcoap.h:668
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
nanocoap API
gcoap_link_encoder_t link_encoder
Writes a link for a resource.
Definition: gcoap.h:653
Common IP-based transport layer end point.
Definition: sock.h:213
int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code, const char *path)
Initializes a CoAP request PDU on a buffer.
xtimer interface definitions
Describes a message object which can be sent between threads.
Definition: msg.h:184
const coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:650
ssize_t gcoap_encode_link(const coap_resource_t *resource, char *buf, size_t maxlen, coap_link_encoder_ctx_t *context)
Writes a resource in CoRE Link Format to a provided buffer.
Extends request memo for resending a confirmable request.
Definition: gcoap.h:675
struct gcoap_listener gcoap_listener_t
A modular collection of resources for a server.
static ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Writes a complete CoAP response PDU when there is no payload.
Definition: gcoap.h:832
size_t gcoap_obs_send(const uint8_t *buf, size_t len, const coap_resource_t *resource)
Sends a buffer containing a CoAP Observe notification to the observer registered for a resource...
xtimer timer structure
Definition: xtimer.h:74
size_t gcoap_req_send(const uint8_t *buf, size_t len, const sock_udp_ep_t *remote, gcoap_resp_handler_t resp_handler, void *context)
Sends a buffer containing a CoAP request to the provided endpoint.
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf)
Get the resource list, currently only CoRE Link Format (COAP_FORMAT_LINK) supported.
CoAP PDU parsing context structure.
Definition: nanocoap.h:184
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.