#ifndef COAP_PDU_H_
#define COAP_PDU_H_
#include "uri.h"
#include "coap_option.h"
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#include <stdint.h>
#define COAP_DEFAULT_PORT 5683
#define COAPS_DEFAULT_PORT 5684
#define COAP_DEFAULT_MAX_AGE 60
#ifndef COAP_DEFAULT_MTU
#define COAP_DEFAULT_MTU 1152
#endif
#define COAP_BERT_BASE 1152
#ifndef COAP_DEFAULT_HOP_LIMIT
#define COAP_DEFAULT_HOP_LIMIT 16
#endif
#define COAP_DEFAULT_SCHEME "coap"
#define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
typedef enum coap_pdu_type_t {
COAP_MESSAGE_CON,
COAP_MESSAGE_NON,
COAP_MESSAGE_ACK,
COAP_MESSAGE_RST
} coap_pdu_type_t;
typedef enum coap_request_t {
COAP_REQUEST_GET = 1,
COAP_REQUEST_POST,
COAP_REQUEST_PUT,
COAP_REQUEST_DELETE,
COAP_REQUEST_FETCH,
COAP_REQUEST_PATCH,
COAP_REQUEST_IPATCH,
} coap_request_t;
#define COAP_OPTION_IF_MATCH 1
#define COAP_OPTION_URI_HOST 3
#define COAP_OPTION_ETAG 4
#define COAP_OPTION_IF_NONE_MATCH 5
#define COAP_OPTION_OBSERVE 6
#define COAP_OPTION_URI_PORT 7
#define COAP_OPTION_LOCATION_PATH 8
#define COAP_OPTION_OSCORE 9
#define COAP_OPTION_URI_PATH 11
#define COAP_OPTION_CONTENT_FORMAT 12
#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_MAXAGE 14
#define COAP_OPTION_URI_QUERY 15
#define COAP_OPTION_HOP_LIMIT 16
#define COAP_OPTION_ACCEPT 17
#define COAP_OPTION_LOCATION_QUERY 20
#define COAP_OPTION_BLOCK2 23
#define COAP_OPTION_BLOCK1 27
#define COAP_OPTION_SIZE2 28
#define COAP_OPTION_PROXY_URI 35
#define COAP_OPTION_PROXY_SCHEME 39
#define COAP_OPTION_SIZE1 60
#define COAP_OPTION_ECHO 252
#define COAP_OPTION_NORESPONSE 258
#define COAP_OPTION_RTAG 292
#define COAP_MAX_OPT 65535
#define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
#define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
#ifndef SHORT_ERROR_RESPONSE
const 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
#define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
typedef enum coap_pdu_signaling_proto_t {
COAP_SIGNALING_CSM = COAP_SIGNALING_CODE(701),
COAP_SIGNALING_PING = COAP_SIGNALING_CODE(702),
COAP_SIGNALING_PONG = COAP_SIGNALING_CODE(703),
COAP_SIGNALING_RELEASE = COAP_SIGNALING_CODE(704),
COAP_SIGNALING_ABORT = COAP_SIGNALING_CODE(705),
} coap_pdu_signaling_proto_t;
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
#define COAP_SIGNALING_OPTION_CUSTODY 2
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
#define COAP_SIGNALING_OPTION_HOLD_OFF 4
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
#define COAP_MEDIATYPE_TEXT_PLAIN 0
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40
#define COAP_MEDIATYPE_APPLICATION_XML 41
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42
#define COAP_MEDIATYPE_APPLICATION_RDF_XML 43
#define COAP_MEDIATYPE_APPLICATION_EXI 47
#define COAP_MEDIATYPE_APPLICATION_JSON 50
#define COAP_MEDIATYPE_APPLICATION_CBOR 60
#define COAP_MEDIATYPE_APPLICATION_CWT 61
#define COAP_MEDIATYPE_APPLICATION_COAP_GROUP_JSON 256
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102
#define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110
#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111
#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112
#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113
#define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114
#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115
#define COAP_MEDIATYPE_APPLICATION_SENML_XML 310
#define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311
#define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271
#define COAP_MEDIATYPE_APPLICATION_ACE_CBOR 19
#define COAP_MEDIATYPE_ANY 0xff
typedef int coap_mid_t;
#define COAP_INVALID_MID -1
#define COAP_INVALID_TID COAP_INVALID_MID
COAP_DEPRECATED typedef struct {
uint16_t key;
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))
#ifdef WITH_LWIP
coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
#endif
typedef enum coap_proto_t {
COAP_PROTO_NONE = 0,
COAP_PROTO_UDP,
COAP_PROTO_DTLS,
COAP_PROTO_TCP,
COAP_PROTO_TLS,
} coap_proto_t;
typedef enum coap_pdu_code_t {
COAP_EMPTY_CODE = 0,
COAP_REQUEST_CODE_GET = COAP_REQUEST_GET,
COAP_REQUEST_CODE_POST = COAP_REQUEST_POST,
COAP_REQUEST_CODE_PUT = COAP_REQUEST_PUT,
COAP_REQUEST_CODE_DELETE = COAP_REQUEST_DELETE,
COAP_REQUEST_CODE_FETCH = COAP_REQUEST_FETCH,
COAP_REQUEST_CODE_PATCH = COAP_REQUEST_PATCH,
COAP_REQUEST_CODE_IPATCH = COAP_REQUEST_IPATCH,
COAP_RESPONSE_CODE_CREATED = COAP_RESPONSE_CODE(201),
COAP_RESPONSE_CODE_DELETED = COAP_RESPONSE_CODE(202),
COAP_RESPONSE_CODE_VALID = COAP_RESPONSE_CODE(203),
COAP_RESPONSE_CODE_CHANGED = COAP_RESPONSE_CODE(204),
COAP_RESPONSE_CODE_CONTENT = COAP_RESPONSE_CODE(205),
COAP_RESPONSE_CODE_CONTINUE = COAP_RESPONSE_CODE(231),
COAP_RESPONSE_CODE_BAD_REQUEST = COAP_RESPONSE_CODE(400),
COAP_RESPONSE_CODE_UNAUTHORIZED = COAP_RESPONSE_CODE(401),
COAP_RESPONSE_CODE_BAD_OPTION = COAP_RESPONSE_CODE(402),
COAP_RESPONSE_CODE_FORBIDDEN = COAP_RESPONSE_CODE(403),
COAP_RESPONSE_CODE_NOT_FOUND = COAP_RESPONSE_CODE(404),
COAP_RESPONSE_CODE_NOT_ALLOWED = COAP_RESPONSE_CODE(405),
COAP_RESPONSE_CODE_NOT_ACCEPTABLE = COAP_RESPONSE_CODE(406),
COAP_RESPONSE_CODE_INCOMPLETE = COAP_RESPONSE_CODE(408),
COAP_RESPONSE_CODE_CONFLICT = COAP_RESPONSE_CODE(409),
COAP_RESPONSE_CODE_PRECONDITION_FAILED = COAP_RESPONSE_CODE(412),
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = COAP_RESPONSE_CODE(413),
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT = COAP_RESPONSE_CODE(415),
COAP_RESPONSE_CODE_UNPROCESSABLE = COAP_RESPONSE_CODE(422),
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS = COAP_RESPONSE_CODE(429),
COAP_RESPONSE_CODE_INTERNAL_ERROR = COAP_RESPONSE_CODE(500),
COAP_RESPONSE_CODE_NOT_IMPLEMENTED = COAP_RESPONSE_CODE(501),
COAP_RESPONSE_CODE_BAD_GATEWAY = COAP_RESPONSE_CODE(502),
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = COAP_RESPONSE_CODE(503),
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = COAP_RESPONSE_CODE(504),
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED = COAP_RESPONSE_CODE(505),
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED = COAP_RESPONSE_CODE(508),
COAP_SIGNALING_CODE_CSM = COAP_SIGNALING_CSM,
COAP_SIGNALING_CODE_PING = COAP_SIGNALING_PING,
COAP_SIGNALING_CODE_PONG = COAP_SIGNALING_PONG,
COAP_SIGNALING_CODE_RELEASE = COAP_SIGNALING_RELEASE,
COAP_SIGNALING_CODE_ABORT = COAP_SIGNALING_ABORT
} coap_pdu_code_t;
coap_pdu_t *coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code,
coap_mid_t mid, size_t size);
coap_pdu_t *coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code,
coap_session_t *session);
void coap_delete_pdu(coap_pdu_t *pdu);
coap_pdu_t *
coap_pdu_duplicate(const coap_pdu_t *old_pdu,
coap_session_t *session,
size_t token_length,
const uint8_t *token,
coap_opt_filter_t *drop_options);
int coap_pdu_parse(coap_proto_t proto,
const uint8_t *data,
size_t length,
coap_pdu_t *pdu);
int coap_add_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
size_t coap_add_option(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const uint8_t *data);
int coap_add_data(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
int coap_get_data(const coap_pdu_t *pdu,
size_t *len,
const uint8_t **data);
int coap_get_data_large(const coap_pdu_t *pdu,
size_t *len,
const uint8_t **data,
size_t *offset,
size_t *total);
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu);
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code);
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu);
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type);
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu);
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu);
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid);
#endif