nanocoap.h File Reference

nanocoap API More...

Detailed Description

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.h>
#include "coap.h"
#include <arpa/inet.h>
+ Include dependency graph for nanocoap.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  coap_hdr_t
 Raw CoAP PDU header structure. More...
 
struct  coap_optpos_t
 CoAP option array entry. More...
 
struct  coap_pkt_t
 CoAP PDU parsing context structure. More...
 
struct  coap_resource_t
 Type for CoAP resource entry. More...
 
struct  coap_block1_t
 Block1 helper struct. More...
 
struct  coap_block_slicer_t
 Blockwise transfer helper struct. More...
 

Macros

#define COAP_FORMAT_NONE   (UINT16_MAX)
 Nanocoap-specific value to indicate no format specified.
 
#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER
 Resource definition for the default .well-known/core handler. More...
 

Typedefs

typedef ssize_t(* coap_handler_t) (coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
 Resource handler type.
 

Functions

int coap_parse (coap_pkt_t *pkt, uint8_t *buf, size_t len)
 Parse a CoAP PDU. More...
 
ssize_t coap_build_reply (coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len)
 Build reply to CoAP request. More...
 
ssize_t coap_reply_simple (coap_pkt_t *pkt, unsigned code, uint8_t *buf, size_t len, unsigned ct, const uint8_t *payload, uint8_t payload_len)
 Create CoAP reply (convenience function) More...
 
ssize_t coap_handle_req (coap_pkt_t *pkt, uint8_t *resp_buf, unsigned resp_buf_len)
 Handle incoming CoAP request. More...
 
ssize_t coap_build_hdr (coap_hdr_t *hdr, unsigned type, uint8_t *token, size_t token_len, unsigned code, uint16_t id)
 Builds a CoAP header. More...
 
void coap_pkt_init (coap_pkt_t *pkt, uint8_t *buf, size_t len, size_t header_len)
 Initialize a packet struct, to build a message buffer. More...
 
size_t coap_put_option (uint8_t *buf, uint16_t lastonum, uint16_t onum, uint8_t *odata, size_t olen)
 Insert a CoAP option into buffer. More...
 
size_t coap_put_option_ct (uint8_t *buf, uint16_t lastonum, uint16_t content_type)
 Insert content type option into buffer. More...
 
size_t coap_opt_put_string (uint8_t *buf, uint16_t lastonum, uint16_t optnum, const char *string, char separator)
 Encode the given string as multi-part option into buffer. More...
 
static size_t coap_opt_put_uri_path (uint8_t *buf, uint16_t lastonum, const char *uri)
 Convenience function for inserting URI_PATH option into buffer. More...
 
static size_t coap_opt_put_uri_query (uint8_t *buf, uint16_t lastonum, const char *uri)
 Convenience function for inserting URI_QUERY option into buffer. More...
 
static size_t coap_opt_put_location_path (uint8_t *buf, uint16_t lastonum, const char *location)
 Convenience function for inserting LOCATION_PATH option into buffer. More...
 
static size_t coap_opt_put_location_query (uint8_t *buf, uint16_t lastonum, const char *location)
 Convenience function for inserting LOCATION_QUERY option into buffer. More...
 
int coap_get_blockopt (coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsigned *szx)
 Generic block option getter. More...
 
int coap_get_block1 (coap_pkt_t *pkt, coap_block1_t *block1)
 Block1 option getter. More...
 
int coap_get_block2 (coap_pkt_t *pkt, coap_block1_t *block2)
 Block2 option getter. More...
 
size_t coap_put_option_block1 (uint8_t *buf, uint16_t lastonum, unsigned blknum, unsigned szx, int more)
 Insert block1 option into buffer. More...
 
size_t coap_put_block1_ok (uint8_t *pkt_pos, coap_block1_t *block1, uint16_t lastonum)
 Insert block1 option into buffer (from coap_block1_t) More...
 
ssize_t coap_opt_add_string (coap_pkt_t *pkt, uint16_t optnum, const char *string, char separator)
 Encode the given string as option(s) into pkt. More...
 
ssize_t coap_opt_add_uint (coap_pkt_t *pkt, uint16_t optnum, uint32_t value)
 Encode the given uint option into pkt. More...
 
ssize_t coap_opt_finish (coap_pkt_t *pkt, uint16_t flags)
 Finalizes options as required and prepares for payload. More...
 
size_t coap_opt_put_block2 (uint8_t *buf, uint16_t lastonum, coap_block_slicer_t *slicer, bool more)
 Insert block2 option into buffer. More...
 
unsigned coap_get_content_type (coap_pkt_t *pkt)
 Get content type from packet. More...
 
ssize_t coap_opt_get_string (const coap_pkt_t *pkt, uint16_t optnum, uint8_t *target, size_t max_len, char separator)
 Read a full option as null terminated string into the target buffer. More...
 
static ssize_t coap_get_uri_path (const coap_pkt_t *pkt, uint8_t *target)
 Convenience function for getting the packet's URI_PATH. More...
 
static ssize_t coap_get_uri_query (const coap_pkt_t *pkt, uint8_t *target)
 Convenience function for getting the packet's URI_QUERY option. More...
 
static ssize_t coap_get_location_path (const coap_pkt_t *pkt, uint8_t *target, size_t max_len)
 Convenience function for getting the packet's LOCATION_PATH option. More...
 
static ssize_t coap_get_location_query (const coap_pkt_t *pkt, uint8_t *target, size_t max_len)
 Convenience function for getting the packet's LOCATION_QUERY option. More...
 
void coap_block2_init (coap_pkt_t *pkt, coap_block_slicer_t *slicer)
 Initialize a block2 slicer struct for writing the payload. More...
 
void coap_block2_finish (coap_block_slicer_t *slicer)
 Finish a block2 response. More...
 
ssize_t coap_block2_build_reply (coap_pkt_t *pkt, unsigned code, uint8_t *rbuf, unsigned rlen, unsigned payload_len, coap_block_slicer_t *slicer)
 Build reply to CoAP block2 request. More...
 
size_t coap_blockwise_put_char (coap_block_slicer_t *slicer, uint8_t *bufpos, char c)
 Add a single character to a block2 reply. More...
 
size_t coap_blockwise_put_bytes (coap_block_slicer_t *slicer, uint8_t *bufpos, const uint8_t *c, size_t len)
 Add a byte array to a block2 reply. More...
 
static unsigned coap_szx2size (unsigned szx)
 Helper to decode SZX value to size in bytes. More...
 
static unsigned coap_get_ver (coap_pkt_t *pkt)
 Get the CoAP version number. More...
 
static unsigned coap_get_type (coap_pkt_t *pkt)
 Get the message type. More...
 
static unsigned coap_get_token_len (coap_pkt_t *pkt)
 Get a message's token length [in byte]. More...
 
static unsigned coap_get_code_class (coap_pkt_t *pkt)
 Get a message's code class (3 most significant bits of code) More...
 
static unsigned coap_get_code_detail (coap_pkt_t *pkt)
 Get a message's code detail (5 least significant bits of code) More...
 
static unsigned coap_get_code_raw (coap_pkt_t *pkt)
 Get a message's raw code (class + detail) More...
 
static unsigned coap_get_code (coap_pkt_t *pkt)
 Get a message's code in decimal format ((class * 100) + detail) More...
 
static unsigned coap_get_id (coap_pkt_t *pkt)
 Get the message ID of the given CoAP packet. More...
 
static uint8_t * coap_hdr_data_ptr (coap_hdr_t *hdr)
 Get the start of data after the header. More...
 
static unsigned coap_get_total_hdr_len (coap_pkt_t *pkt)
 Get the total header length (4-byte header + token length) More...
 
static uint8_t coap_code (unsigned cls, unsigned detail)
 Encode given code class and code detail to raw code. More...
 
static void coap_hdr_set_code (coap_hdr_t *hdr, uint8_t code)
 Write the given raw message code to given CoAP header. More...
 
static void coap_hdr_set_type (coap_hdr_t *hdr, unsigned type)
 Set the message type for the given CoAP header. More...
 
static unsigned coap_method2flag (unsigned code)
 Convert message code (request method) into a corresponding bit field. More...
 
static bool coap_has_observe (coap_pkt_t *pkt)
 Identifies a packet containing an observe option. More...
 
static void coap_clear_observe (coap_pkt_t *pkt)
 Clears the observe option value from a packet. More...
 
static uint32_t coap_get_observe (coap_pkt_t *pkt)
 Get the value of the observe option from the given packet. More...
 
ssize_t coap_well_known_core_default_handler (coap_pkt_t *pkt, uint8_t *buf, size_t len, void *context)
 Reference to the default .well-known/core handler defined by the application.
 

Variables

const coap_resource_t coap_resources []
 Global CoAP resource list.
 
const unsigned coap_resources_numof
 Number of entries in global CoAP resource list.
 

Nanocoap specific CoAP method flags used in coap_handlers array

#define COAP_GET   (0x1)
 
#define COAP_POST   (0x2)
 
#define COAP_PUT   (0x4)
 
#define COAP_DELETE   (0x8)
 

Nanocoap specific maximum values

#define NANOCOAP_NOPTS_MAX   (16)
 
#define NANOCOAP_URI_MAX   (64)
 
#define NANOCOAP_BLOCK_SIZE_EXP_MAX   (6)
 Maximum size for a blockwise transfer as power of 2.
 

coap_opt_finish() flag parameter values

Directs packet/buffer updates when user finishes adding options

#define COAP_OPT_FINISH_NONE   (0x0000)
 no special handling required
 
#define COAP_OPT_FINISH_PAYLOAD   (0x0001)
 expect a payload to follow