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 
378 #ifndef NET_GCOAP_H
379 #define NET_GCOAP_H
380 
381 #include <stdint.h>
382 
383 #include "event/callback.h"
384 #include "event/timeout.h"
385 #include "net/ipv6/addr.h"
386 #include "net/sock/udp.h"
387 #include "net/nanocoap.h"
388 #include "xtimer.h"
389 
390 #ifdef __cplusplus
391 extern "C" {
392 #endif
393 
403 #ifndef CONFIG_GCOAP_PORT
404 #define CONFIG_GCOAP_PORT (5683)
405 #endif
406 
410 #ifndef CONFIG_GCOAP_PDU_BUF_SIZE
411 #define CONFIG_GCOAP_PDU_BUF_SIZE (128)
412 #endif
413 
417 #ifndef CONFIG_GCOAP_REQ_WAITING_MAX
418 #define CONFIG_GCOAP_REQ_WAITING_MAX (2)
419 #endif
420 
425 #define GCOAP_TOKENLEN_MAX (8)
426 
430 #define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
431 
438 #ifndef CONFIG_GCOAP_TOKENLEN
439 #define CONFIG_GCOAP_TOKENLEN (2)
440 #endif
441 
445 #define GCOAP_PAYLOAD_MARKER (0xFF)
446 
453 #ifndef CONFIG_GCOAP_NO_AUTO_INIT
454 #define CONFIG_GCOAP_NO_AUTO_INIT 0
455 #endif
456 
461 #define GCOAP_MEMO_UNUSED (0)
462 #define GCOAP_MEMO_RETRANSMIT (1)
463 #define GCOAP_MEMO_WAIT (2)
464 #define GCOAP_MEMO_RESP (3)
465 #define GCOAP_MEMO_TIMEOUT (4)
466 #define GCOAP_MEMO_ERR (5)
472 #define GCOAP_SEND_LIMIT_NON (-1)
473 
474 #ifdef DOXYGEN
475 
484 #define CONFIG_GCOAP_NO_RETRANS_BACKOFF
485 #endif
486 
493 #ifndef CONFIG_GCOAP_NON_TIMEOUT
494 #define CONFIG_GCOAP_NON_TIMEOUT (5000000U)
495 #endif
496 
501 #ifndef CONFIG_GCOAP_OBS_CLIENTS_MAX
502 #define CONFIG_GCOAP_OBS_CLIENTS_MAX (2)
503 #endif
504 
509 #ifndef CONFIG_GCOAP_OBS_REGISTRATIONS_MAX
510 #define CONFIG_GCOAP_OBS_REGISTRATIONS_MAX (2)
511 #endif
512 
517 #define GCOAP_OBS_MEMO_UNUSED (0)
518 #define GCOAP_OBS_MEMO_IDLE (1)
519 #define GCOAP_OBS_MEMO_PENDING (2)
542 #ifndef CONFIG_GCOAP_OBS_VALUE_WIDTH
543 #define CONFIG_GCOAP_OBS_VALUE_WIDTH (3)
544 #endif
545 
549 #if (CONFIG_GCOAP_OBS_VALUE_WIDTH == 3)
550 #define GCOAP_OBS_TICK_EXPONENT (5)
551 #elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 2)
552 #define GCOAP_OBS_TICK_EXPONENT (16)
553 #elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 1)
554 #define GCOAP_OBS_TICK_EXPONENT (24)
555 #endif
556 
561 #define GCOAP_OBS_INIT_OK (0)
562 #define GCOAP_OBS_INIT_ERR (-1)
563 #define GCOAP_OBS_INIT_UNUSED (-2)
564 
569 #ifndef GCOAP_STACK_SIZE
570 #define GCOAP_STACK_SIZE (THREAD_STACKSIZE_DEFAULT + DEBUG_EXTRA_STACKSIZE \
571  + sizeof(coap_pkt_t))
572 #endif
573 
578 #ifndef CONFIG_GCOAP_RESEND_BUFS_MAX
579 #define CONFIG_GCOAP_RESEND_BUFS_MAX (1)
580 #endif
581 
587 #define COAP_LINK_FLAG_INIT_RESLIST (1)
594 typedef struct {
595  unsigned content_format;
596  size_t link_pos;
597  uint16_t flags;
600 
612 typedef ssize_t (*gcoap_link_encoder_t)(const coap_resource_t *resource, char *buf,
613  size_t maxlen, coap_link_encoder_ctx_t *context);
614 
619 #define GCOAP_RESOURCE_FOUND (0)
620 #define GCOAP_RESOURCE_WRONG_METHOD (1)
621 #define GCOAP_RESOURCE_NO_PATH (2)
622 #define GCOAP_RESOURCE_ERROR (3)
623 
629 
642 typedef int (*gcoap_request_matcher_t)(gcoap_listener_t *listener,
643  const coap_resource_t **resource,
644  const coap_pkt_t *pdu);
645 
652  size_t resources_len;
667 };
668 
673 
680 typedef void (*gcoap_resp_handler_t)(const gcoap_request_memo_t *memo,
681  coap_pkt_t* pdu,
682  const sock_udp_ep_t *remote);
683 
687 typedef struct {
688  uint8_t *pdu_buf;
689  size_t pdu_len;
691 
696  unsigned state;
699  union {
703  } msg;
707  void *context;
710 };
711 
715 typedef struct {
718  uint8_t token[GCOAP_TOKENLEN_MAX];
719  unsigned token_len;
721 
732 
749 
768 int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
769  unsigned code, const char *path);
770 
783 static inline ssize_t gcoap_request(coap_pkt_t *pdu, uint8_t *buf, size_t len,
784  unsigned code, char *path)
785 {
786  return (gcoap_req_init(pdu, buf, len, code, path) == 0)
788  : -1;
789 }
790 
803 size_t gcoap_req_send(const uint8_t *buf, size_t len,
804  const sock_udp_ep_t *remote,
805  gcoap_resp_handler_t resp_handler, void *context);
806 
820 int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
821 
833 static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
834  size_t len, unsigned code)
835 {
836  return (gcoap_resp_init(pdu, buf, len, code) == 0)
838  : -1;
839 }
840 
856 int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
857  const coap_resource_t *resource);
858 
872 size_t gcoap_obs_send(const uint8_t *buf, size_t len,
873  const coap_resource_t *resource);
874 
882 uint8_t gcoap_op_state(void);
883 
902 int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf);
903 
917 ssize_t gcoap_encode_link(const coap_resource_t *resource, char *buf,
918  size_t maxlen, coap_link_encoder_ctx_t *context);
919 
938 int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val);
939 
940 #ifdef __cplusplus
941 }
942 #endif
943 
944 #endif /* NET_GCOAP_H */
945 
gcoap_listener
A modular collection of resources for a server.
Definition: gcoap.h:649
kernel_pid_t
int16_t kernel_pid_t
Unique process identifier.
Definition: sched.h:125
gcoap_listener::link_encoder
gcoap_link_encoder_t link_encoder
Writes a link for a resource.
Definition: gcoap.h:653
gcoap_request_memo::hdr_buf
uint8_t hdr_buf[GCOAP_HEADER_MAXLEN]
Copy of PDU header, if no resends.
Definition: gcoap.h:700
gcoap_get_resource_list
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.
gcoap_init
kernel_pid_t gcoap_init(void)
Initializes the gcoap thread and device.
gcoap_add_qstring
int gcoap_add_qstring(coap_pkt_t *pdu, const char *key, const char *val)
Adds a single Uri-Query option to a CoAP request.
GCOAP_TOKENLEN_MAX
#define GCOAP_TOKENLEN_MAX
Maximum length in bytes for a token.
Definition: gcoap.h:425
nanocoap.h
nanocoap API
gcoap_request_memo::resp_handler
gcoap_resp_handler_t resp_handler
Callback for the response.
Definition: gcoap.h:706
event_timeout_t
Timeout Event structure.
Definition: timeout.h:50
udp.h
UDP sock definitions.
addr.h
Definitions for IPv6 addresses.
gcoap_resend_t::pdu_len
size_t pdu_len
Length of pdu_buf.
Definition: gcoap.h:689
gcoap_resend_t
Extends request memo for resending a confirmable request.
Definition: gcoap.h:687
timeout.h
Provides functionality to trigger events after timeout.
gcoap_request_memo::remote_ep
sock_udp_ep_t remote_ep
Remote endpoint.
Definition: gcoap.h:705
gcoap_obs_send
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.
callback.h
Provides a callback-with-argument event type.
gcoap_observe_memo_t::observer
sock_udp_ep_t * observer
Client endpoint; unused if null.
Definition: gcoap.h:716
gcoap_request_matcher_t
int(* gcoap_request_matcher_t)(gcoap_listener_t *listener, const coap_resource_t **resource, const coap_pkt_t *pdu)
Handler function for the request matcher strategy.
Definition: gcoap.h:642
event_callback_t
Callback Event structure definition.
Definition: callback.h:48
gcoap_request_memo::send_limit
int send_limit
Remaining resends, 0 if none; GCOAP_SEND_LIMIT_NON if non-confirmable.
Definition: gcoap.h:697
gcoap_link_encoder_t
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:612
gcoap_register_listener
void gcoap_register_listener(gcoap_listener_t *listener)
Starts listening for resource paths.
gcoap_resp_init
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
Initializes a CoAP response packet on a buffer.
gcoap_request_memo
Memo to handle a response for a request.
Definition: gcoap.h:695
gcoap_request_memo::context
void * context
ptr to user defined context data
Definition: gcoap.h:707
gcoap_listener::request_matcher
gcoap_request_matcher_t request_matcher
Function that picks a suitable request handler from a request.
Definition: gcoap.h:666
_sock_tl_ep
Common IP-based transport layer end point.
Definition: sock.h:213
coap_resource_t
Type for CoAP resource entry.
Definition: nanocoap.h:223
gcoap_request_memo::resp_evt_tmout
event_timeout_t resp_evt_tmout
Limits wait for response.
Definition: gcoap.h:708
GCOAP_HEADER_MAXLEN
#define GCOAP_HEADER_MAXLEN
Maximum length in bytes for a header, including the token.
Definition: gcoap.h:430
gcoap_listener::resources_len
size_t resources_len
Length of array.
Definition: gcoap.h:652
gcoap_resp_handler_t
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:680
gcoap_listener::next
struct gcoap_listener * next
Next listener in list.
Definition: gcoap.h:654
gcoap_request_memo::data
gcoap_resend_t data
Endpoint and PDU buffer, for resend.
Definition: gcoap.h:702
gcoap_req_send
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.
COAP_OPT_FINISH_NONE
#define COAP_OPT_FINISH_NONE
no special handling required
Definition: nanocoap.h:160
gcoap_request
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:783
gcoap_observe_memo_t::resource
const coap_resource_t * resource
Entity being observed.
Definition: gcoap.h:717
coap_pkt_t
CoAP PDU parsing context structure.
Definition: nanocoap.h:185
gcoap_resend_t::pdu_buf
uint8_t * pdu_buf
Buffer containing the PDU.
Definition: gcoap.h:688
gcoap_req_init
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.
gcoap_response
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:833
gcoap_request_memo::resp_tmout_cb
event_callback_t resp_tmout_cb
Callback for response timeout.
Definition: gcoap.h:709
gcoap_encode_link
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.
gcoap_request_memo::msg
union gcoap_request_memo::@264 msg
Request message data; if confirmable, supports resending message.
coap_opt_finish
ssize_t coap_opt_finish(coap_pkt_t *pkt, uint16_t flags)
Finalizes options as required and prepares for payload.
gcoap_request_memo::state
unsigned state
State of this memo, a GCOAP_MEMO...
Definition: gcoap.h:696
gcoap_observe_memo_t::token_len
unsigned token_len
Actual length of token attribute.
Definition: gcoap.h:719
xtimer.h
xtimer interface definitions
gcoap_listener::resources
const coap_resource_t * resources
First element in the array of resources; must order alphabetically.
Definition: gcoap.h:650
gcoap_op_state
uint8_t gcoap_op_state(void)
Provides important operational statistics.
gcoap_obs_init
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...
gcoap_observe_memo_t
Memo for Observe registration and notifications.
Definition: gcoap.h:715