pdu.h
Go to the documentation of this file.
/*
* pdu.h -- CoAP message structure
*
* Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef _COAP_PDU_H_
#define _COAP_PDU_H_
#include "uri.h"
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP port */
#define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
#ifndef COAP_MAX_PDU_SIZE
#define COAP_MAX_PDU_SIZE 1400 /* maximum size of a CoAP PDU */
#endif /* COAP_MAX_PDU_SIZE */
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
#define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
#ifdef __COAP_DEFAULT_HASH
/* pre-calculated hash key for the default well-known URI */
#define COAP_DEFAULT_WKC_HASHKEY "\345\130\144\245"
#endif
/* CoAP message types */
#define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */
#define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */
#define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */
#define COAP_MESSAGE_RST 3 /* indicates error in received messages */
/* CoAP request methods */
#define COAP_REQUEST_GET 1
#define COAP_REQUEST_POST 2
#define COAP_REQUEST_PUT 3
#define COAP_REQUEST_DELETE 4
/* CoAP option types (be sure to update check_critical when adding options */
#define COAP_OPTION_IF_MATCH 1 /* C, opaque, 0-8 B, (none) */
#define COAP_OPTION_URI_HOST 3 /* C, String, 1-255 B, destination address */
#define COAP_OPTION_ETAG 4 /* E, opaque, 1-8 B, (none) */
#define COAP_OPTION_IF_NONE_MATCH 5 /* empty, 0 B, (none) */
#define COAP_OPTION_URI_PORT 7 /* C, uint, 0-2 B, destination port */
#define COAP_OPTION_LOCATION_PATH 8 /* E, String, 0-255 B, - */
#define COAP_OPTION_URI_PATH 11 /* C, String, 0-255 B, (none) */
#define COAP_OPTION_CONTENT_FORMAT 12 /* E, uint, 0-2 B, (none) */
#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_MAXAGE 14 /* E, uint, 0--4 B, 60 Seconds */
#define COAP_OPTION_URI_QUERY 15 /* C, String, 1-255 B, (none) */
#define COAP_OPTION_ACCEPT 17 /* C, uint, 0-2 B, (none) */
#define COAP_OPTION_LOCATION_QUERY 20 /* E, String, 0-255 B, (none) */
#define COAP_OPTION_PROXY_URI 35 /* C, String, 1-1034 B, (none) */
#define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */
#define COAP_OPTION_SIZE1 60 /* E, uint, 0-4 B, (none) */
/* option types from RFC 7641 */
#define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-3 B, (none) */
#define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE
/* selected option types from RFC 7959 */
#define COAP_OPTION_BLOCK2 23 /* C, uint, 0--3 B, (none) */
#define COAP_OPTION_BLOCK1 27 /* C, uint, 0--3 B, (none) */
/* selected option types from RFC 7967 */
#define COAP_OPTION_NORESPONSE 258 /* N, uint, 0--1 B, 0 */
#define COAP_MAX_OPT 65535
/* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
/* As of draft-ietf-core-coap-04, response codes are encoded to base
* 32, i.e. the three upper bits determine the response class while
* the remaining five fine-grained information specific to that class.
*/
#define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
/* Determines the class of response code C */
#define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
#ifndef SHORT_ERROR_RESPONSE
char *coap_response_phrase(unsigned char code);
#define COAP_ERROR_PHRASE_LENGTH 32
#else
#define coap_response_phrase(x) ((char *)NULL)
#define COAP_ERROR_PHRASE_LENGTH 0
#endif /* SHORT_ERROR_RESPONSE */
/* The following definitions exist for backwards compatibility */
#if 0 /* this does not exist any more */
#define COAP_RESPONSE_100 40 /* 100 Continue */
#endif
#define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200) /* 2.00 OK */
#define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201) /* 2.01 Created */
#define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203) /* 2.03 Valid */
#define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400) /* 4.00 Bad Request */
#define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404) /* 4.04 Not Found */
#define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405) /* 4.05 Method Not Allowed */
#define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415) /* 4.15 Unsupported Media Type */
#define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500) /* 5.00 Internal Server Error */
#define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501) /* 5.01 Not Implemented */
#define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503) /* 5.03 Service Unavailable */
#define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504) /* 5.04 Gateway Timeout */
#if 0 /* these response codes do not have a valid code any more */
# define COAP_RESPONSE_X_240 240 /* Token Option required by server */
# define COAP_RESPONSE_X_241 241 /* Uri-Authority Option required by server */
#endif
#define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402) /* Critical Option not supported */
/* CoAP media type encoding */
#define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
#define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
#define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
#define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
#define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
#define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
/* Note that identifiers for registered media types are in the range 0-65535. We
* use an unallocated type here and hope for the best. */
#define COAP_MEDIATYPE_ANY 0xff /* any media type */
typedef int coap_tid_t;
#define COAP_INVALID_TID -1
#define COAP_DROPPED_RESPONSE -2
#ifdef WORDS_BIGENDIAN
typedef struct {
unsigned int version:2; /* protocol version */
unsigned int type:2; /* type flag */
unsigned int token_length:4; /* length of Token */
unsigned int code:8; /* request method (value 1--10) or response
code (value 40-255) */
unsigned short id; /* message id */
unsigned char token[]; /* the actual token, if any */
} coap_hdr_t;
#else
typedef struct {
unsigned int token_length:4; /* length of Token */
unsigned int type:2; /* type flag */
unsigned int version:2; /* protocol version */
unsigned int code:8; /* request method (value 1--10) or response
code (value 40-255) */
unsigned short id; /* transaction id (network byte order!) */
unsigned char token[]; /* the actual token, if any */
} coap_hdr_t;
#endif
#define COAP_MESSAGE_IS_EMPTY(MSG) ((MSG)->code == 0)
#define COAP_MESSAGE_IS_REQUEST(MSG) (!COAP_MESSAGE_IS_EMPTY(MSG) \
&& ((MSG)->code < 32))
#define COAP_MESSAGE_IS_RESPONSE(MSG) ((MSG)->code >= 64)
#define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list
* in a CoAP message is limited by 0b11110000
* marker */
#define COAP_OPT_END 0xF0 /* end marker */
#define COAP_PAYLOAD_START 0xFF /* payload marker */
typedef struct {
unsigned short key; /* the option key (no delta coding) */
unsigned int length;
} coap_option;
#define COAP_OPTION_KEY(option) (option).key
#define COAP_OPTION_LENGTH(option) (option).length
#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
typedef struct {
size_t max_size;
coap_hdr_t *hdr;
unsigned short max_delta;
unsigned short length;
unsigned char *data;
#ifdef WITH_LWIP
struct pbuf *pbuf;
#endif
} coap_pdu_t;
#define COAP_OPTION(node) ((coap_option *)(node)->options)
#ifdef WITH_LWIP
coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
#endif
coap_pdu_t *
coap_pdu_init(unsigned char type,
unsigned char code,
unsigned short id,
size_t size);
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
coap_pdu_t *coap_new_pdu(void);
void coap_delete_pdu(coap_pdu_t *);
int coap_pdu_parse(unsigned char *data,
size_t length,
coap_pdu_t *result);
int coap_add_token(coap_pdu_t *pdu,
size_t len,
const unsigned char *data);
size_t coap_add_option(coap_pdu_t *pdu,
unsigned short type,
unsigned int len,
const unsigned char *data);
unsigned char *coap_add_option_later(coap_pdu_t *pdu,
unsigned short type,
unsigned int len);
int coap_add_data(coap_pdu_t *pdu,
unsigned int len,
const unsigned char *data);
int coap_get_data(coap_pdu_t *pdu,
size_t *len,
unsigned char **data);
#endif /* _COAP_PDU_H_ */
coap_option
Structures for more convenient handling of options.
Definition: pdu.h:214
coap_pdu_clear
void coap_pdu_clear(coap_pdu_t *pdu, size_t size)
Clears any contents from pdu and resets version field, length and data pointers.
Definition: pdu.c:29
coap_hdr_t::id
unsigned short id
Definition: pdu.h:191
coap_tid_t
int coap_tid_t
coap_tid_t is used to store CoAP transaction id, i.e.
Definition: pdu.h:163
coap_pdu_t::length
unsigned short length
PDU length (including header, options, data)
Definition: pdu.h:234
coap_hdr_t
Definition: pdu.h:185
coap_option::length
unsigned int length
Definition: pdu.h:216
coap_option::key
unsigned short key
Definition: pdu.h:215
uri.h
coap_new_pdu
coap_pdu_t * coap_new_pdu(void)
Creates a new CoAP PDU.
Definition: pdu.c:119
coap_pdu_t::hdr
coap_hdr_t * hdr
Address of the first byte of the CoAP message.
Definition: pdu.h:229
coap_pdu_t::max_delta
unsigned short max_delta
highest option number
Definition: pdu.h:233
coap_add_token
int coap_add_token(coap_pdu_t *pdu, size_t len, const unsigned char *data)
Adds token of length len to pdu.
Definition: pdu.c:153
coap_pdu_parse
int coap_pdu_parse(unsigned char *data, size_t length, coap_pdu_t *result)
Parses data into the CoAP PDU structure given in result.
Definition: pdu.c:344
coap_hdr_t::code
unsigned int code
Definition: pdu.h:189
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
coap_add_data
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.
Definition: pdu.c:234
coap_pdu_t::max_size
size_t max_size
allocated storage for options and data
Definition: pdu.h:228
coap_response_phrase
char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
Definition: pdu.c:309
coap_delete_pdu
void coap_delete_pdu(coap_pdu_t *)
Definition: pdu.c:136
coap_hdr_t::token_length
unsigned int token_length
Definition: pdu.h:186
coap_get_data
int coap_get_data(coap_pdu_t *pdu, size_t *len, unsigned char **data)
Retrieves the length and data pointer of specified PDU.
Definition: pdu.c:257
coap_hdr_t::version
unsigned int version
Definition: pdu.h:188
coap_add_option
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
Adds option of given type to pdu that is passed as first parameter.
Definition: pdu.c:171
coap_hdr_t::type
unsigned int type
Definition: pdu.h:187
coap_add_option_later
unsigned char * coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len)
Adds option of given type to pdu that is passed as first parameter, but does not write a value...
Definition: pdu.c:203
coap_pdu_init
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...
Definition: pdu.c:75
coap_pdu_t::data
unsigned char * data
payload
Definition: pdu.h:235