nanocoap.h File Reference

nanocoap API More...

Detailed Description

nanocoap API

Author
Kaspar Schleiser kaspa.nosp@m.r@sc.nosp@m.hleis.nosp@m.er.d.nosp@m.e

Definition in file nanocoap.h.

#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include <unistd.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_pkt_t
 CoAP option array entry. More...
 
struct  coap_resource_t
 Type for CoAP resource entry. More...
 

Macros

#define COAP_PORT   (5683)
 CoAP port to use.
 
#define COAP_WELL_KNOWN_CORE_DEFAULT_HANDLER   { "/.well-known/core", COAP_GET, coap_well_known_core_default_handler }
 Resource definition for the default .well-known/core handler.
 
Nanocoap specific maximum values
#define NANOCOAP_URL_MAX   (64)
 
#define NANOCOAP_QS_MAX   (64)
 
CoAP option numbers
#define COAP_OPT_URI_HOST   (3)
 
#define COAP_OPT_OBSERVE   (6)
 
#define COAP_OPT_URI_PATH   (11)
 
#define COAP_OPT_CONTENT_FORMAT   (12)
 
#define COAP_OPT_URI_QUERY   (15)
 
CoAP packet types
#define COAP_REQ   (0)
 
#define COAP_RESP   (2)
 
#define COAP_RST   (3)
 
Message types -- confirmable, non-confirmable, etc.
#define COAP_TYPE_CON   (0)
 
#define COAP_TYPE_NON   (1)
 
#define COAP_TYPE_ACK   (2)
 
#define COAP_TYPE_RST   (3)
 
CoAP method codes used in header
#define COAP_CLASS_REQ   (0)
 
#define COAP_METHOD_GET   (1)
 
#define COAP_METHOD_POST   (2)
 
#define COAP_METHOD_PUT   (3)
 
#define COAP_METHOD_DELETE   (4)
 
CoAP method flags used in coap_handlers array
#define COAP_GET   (0x1)
 
#define COAP_POST   (0x2)
 
#define COAP_PUT   (0x4)
 
#define COAP_DELETE   (0x8)
 
Empty CoAP message code
#define COAP_CODE_EMPTY   (0)
 
Response message codes: success
#define COAP_CLASS_SUCCESS   (2)
 
#define COAP_CODE_CREATED   ((2 << 5) | 1)
 
#define COAP_CODE_DELETED   ((2 << 5) | 2)
 
#define COAP_CODE_VALID   ((2 << 5) | 3)
 
#define COAP_CODE_CHANGED   ((2 << 5) | 4)
 
#define COAP_CODE_204   ((2 << 5) | 4)
 
#define COAP_CODE_CONTENT   ((2 << 5) | 5)
 
#define COAP_CODE_205   ((2 << 5) | 5)
 
#define COAP_CODE_231   ((2 << 5) | 31)
 
Response message codes: client error
#define COAP_CLASS_CLIENT_FAILURE   (4)
 
#define COAP_CODE_BAD_REQUEST   ((4 << 5) | 0)
 
#define COAP_CODE_UNAUTHORIZED   ((4 << 5) | 1)
 
#define COAP_CODE_BAD_OPTION   ((4 << 5) | 2)
 
#define COAP_CODE_FORBIDDEN   ((4 << 5) | 3)
 
#define COAP_CODE_PATH_NOT_FOUND   ((4 << 5) | 4)
 
#define COAP_CODE_404   ((4 << 5) | 4)
 
#define COAP_CODE_METHOD_NOT_ALLOWED   ((4 << 5) | 5)
 
#define COAP_CODE_NOT_ACCEPTABLE   ((4 << 5) | 6)
 
#define COAP_CODE_PRECONDITION_FAILED   ((4 << 5) | 0xC)
 
#define COAP_CODE_REQUEST_ENTITY_TOO_LARGE   ((4 << 5) | 0xD)
 
#define COAP_CODE_UNSUPPORTED_CONTENT_FORMAT   ((4 << 5) | 0xF)
 
Response message codes: server error
#define COAP_CLASS_SERVER_FAILURE   (5)
 
#define COAP_CODE_INTERNAL_SERVER_ERROR   ((5 << 5) | 0)
 
#define COAP_CODE_NOT_IMPLEMENTED   ((5 << 5) | 1)
 
#define COAP_CODE_BAD_GATEWAY   ((5 << 5) | 2)
 
#define COAP_CODE_SERVICE_UNAVAILABLE   ((5 << 5) | 3)
 
#define COAP_CODE_GATEWAY_TIMEOUT   ((5 << 5) | 4)
 
#define COAP_CODE_PROXYING_NOT_SUPPORTED   ((5 << 5) | 5)
 
Content types
#define COAP_CT_LINK_FORMAT   (40)
 
#define COAP_CT_XML   (41)
 
#define COAP_CT_OCTET_STREAM   (42)
 
#define COAP_CT_EXI   (47)
 
#define COAP_CT_JSON   (50)
 
Content-Format option codes
#define COAP_FORMAT_TEXT   (0)
 
#define COAP_FORMAT_LINK   (40)
 
#define COAP_FORMAT_OCTET   (42)
 
#define COAP_FORMAT_JSON   (50)
 
#define COAP_FORMAT_CBOR   (60)
 
#define COAP_FORMAT_NONE   (65535)
 nanocoap-specific value to indicate no format specified. More...
 
Observe (RFC 7641) constants
#define COAP_OBS_REGISTER   (0)
 
#define COAP_OBS_DEREGISTER   (1)
 
Timing parameters
#define COAP_ACK_TIMEOUT   (2U)
 
#define COAP_RANDOM_FACTOR   (1.5)
 
#define COAP_MAX_RETRANSMIT   (4)
 
#define COAP_NSTART   (1)
 
#define COAP_DEFAULT_LEISURE   (5)
 

Typedefs

typedef ssize_t(* coap_handler_t) (coap_pkt_t *pkt, uint8_t *buf, size_t len)
 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...
 
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_put_option_uri (uint8_t *buf, uint16_t lastonum, const char *uri, uint16_t optnum)
 Insert URI encoded option into buffer. 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 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 class, 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)
 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.