1 /* 2 * pdu.h -- CoAP message structure 3 * 4 * Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org> 5 * Copyright (c) 2021 Huawei Device Co., Ltd. All rights reserved. 6 * 7 * This file is part of the CoAP library libcoap. Please see README for terms 8 * of use. 9 */ 10 11 /** 12 * @file pdu.h 13 * @brief Pre-defined constants that reflect defaults for CoAP 14 */ 15 16 #ifndef COAP_PDU_H_ 17 #define COAP_PDU_H_ 18 19 #include "coap_config.h" 20 #include "uri.h" 21 22 struct coap_session_t; 23 24 #ifdef WITH_LWIP 25 #include <lwip/pbuf.h> 26 #endif 27 28 #include <stdint.h> 29 #ifndef COAP_USER_DEFAULT_PORT 30 #define COAP_USER_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */ 31 #endif 32 33 #define COAP_NSTACKX_PORT COAP_USER_DEFAULT_PORT 34 35 #define COAP_DEFAULT_PORT COAP_USER_DEFAULT_PORT 36 37 #define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */ 38 #define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */ 39 #ifndef COAP_DEFAULT_MTU 40 #define COAP_DEFAULT_MTU 1152 41 #endif /* COAP_DEFAULT_MTU */ 42 43 /* TCP Message format constants, do not modify */ 44 #define COAP_MESSAGE_SIZE_OFFSET_TCP8 13 45 #define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */ 46 #define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */ 47 48 /* Derived message size limits */ 49 #define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */ 50 #define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */ 51 #define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */ 52 #define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF) 53 54 #ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE 55 #if defined(WITH_CONTIKI) || defined(WITH_LWIP) 56 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4) 57 #else 58 /* 8 MiB max-message-size plus some space for options */ 59 #define COAP_DEFAULT_MAX_PDU_RX_SIZE (8*1024*1024+256) 60 #endif 61 #endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */ 62 63 #ifndef COAP_DEBUG_BUF_SIZE 64 #if defined(WITH_CONTIKI) || defined(WITH_LWIP) 65 #define COAP_DEBUG_BUF_SIZE 128 66 #else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */ 67 /* 1024 derived from RFC7252 4.6. Message Size max payload */ 68 #define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2) 69 #endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */ 70 #endif /* COAP_DEBUG_BUF_SIZE */ 71 72 #define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */ 73 #define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */ 74 75 /** well-known resources URI */ 76 #define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core" 77 78 /* CoAP message types */ 79 80 #define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */ 81 #define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */ 82 #define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */ 83 #define COAP_MESSAGE_RST 3 /* indicates error in received messages */ 84 85 /* CoAP request methods */ 86 87 #define COAP_REQUEST_GET 1 88 #define COAP_REQUEST_POST 2 89 #define COAP_REQUEST_PUT 3 90 #define COAP_REQUEST_DELETE 4 91 #define COAP_REQUEST_FETCH 5 /* RFC 8132 */ 92 #define COAP_REQUEST_PATCH 6 /* RFC 8132 */ 93 #define COAP_REQUEST_IPATCH 7 /* RFC 8132 */ 94 95 /* 96 * CoAP option types (be sure to update coap_option_check_critical() when 97 * adding options 98 */ 99 100 #define COAP_OPTION_IF_MATCH 1 /* C, opaque, 0-8 B, (none) */ 101 #define COAP_OPTION_URI_HOST 3 /* C, String, 1-255 B, destination address */ 102 #define COAP_OPTION_ETAG 4 /* E, opaque, 1-8 B, (none) */ 103 #define COAP_OPTION_IF_NONE_MATCH 5 /* empty, 0 B, (none) */ 104 #define COAP_OPTION_URI_PORT 7 /* C, uint, 0-2 B, destination port */ 105 #define COAP_OPTION_LOCATION_PATH 8 /* E, String, 0-255 B, - */ 106 #define COAP_OPTION_URI_PATH 11 /* C, String, 0-255 B, (none) */ 107 #define COAP_OPTION_CONTENT_FORMAT 12 /* E, uint, 0-2 B, (none) */ 108 #define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT 109 #define COAP_OPTION_MAXAGE 14 /* E, uint, 0--4 B, 60 Seconds */ 110 #define COAP_OPTION_URI_QUERY 15 /* C, String, 1-255 B, (none) */ 111 #define COAP_OPTION_ACCEPT 17 /* C, uint, 0-2 B, (none) */ 112 #define COAP_OPTION_LOCATION_QUERY 20 /* E, String, 0-255 B, (none) */ 113 #define COAP_OPTION_SIZE2 28 /* E, uint, 0-4 B, (none) */ 114 #define COAP_OPTION_PROXY_URI 35 /* C, String, 1-1034 B, (none) */ 115 #define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */ 116 #define COAP_OPTION_SIZE1 60 /* E, uint, 0-4 B, (none) */ 117 118 /* option types from RFC 7641 */ 119 120 #define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-3 B, (none) */ 121 #define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE 122 123 /* selected option types from RFC 7959 */ 124 125 #define COAP_OPTION_BLOCK2 23 /* C, uint, 0--3 B, (none) */ 126 #define COAP_OPTION_BLOCK1 27 /* C, uint, 0--3 B, (none) */ 127 128 /* selected option types from RFC 7967 */ 129 130 #define COAP_OPTION_NORESPONSE 258 /* N, uint, 0--1 B, 0 */ 131 132 #define COAP_MAX_OPT 65535 /**< the highest option number we know */ 133 134 /* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */ 135 136 /* As of draft-ietf-core-coap-04, response codes are encoded to base 137 * 32, i.e. the three upper bits determine the response class while 138 * the remaining five fine-grained information specific to that class. 139 */ 140 #define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100) 141 142 /* Determines the class of response code C */ 143 #define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF) 144 145 #ifndef SHORT_ERROR_RESPONSE 146 /** 147 * Returns a human-readable response phrase for the specified CoAP response @p 148 * code. This function returns @c NULL if not found. 149 * 150 * @param code The response code for which the literal phrase should be 151 * retrieved. 152 * 153 * @return A zero-terminated string describing the error, or @c NULL if not 154 * found. 155 */ 156 const char *coap_response_phrase(unsigned char code); 157 158 #define COAP_ERROR_PHRASE_LENGTH 32 /**< maximum length of error phrase */ 159 160 #else 161 #define coap_response_phrase(x) ((char *)NULL) 162 163 #define COAP_ERROR_PHRASE_LENGTH 0 /**< maximum length of error phrase */ 164 #endif /* SHORT_ERROR_RESPONSE */ 165 166 /* The following definitions exist for backwards compatibility */ 167 #if 0 /* this does not exist any more */ 168 #define COAP_RESPONSE_100 40 /* 100 Continue */ 169 #endif 170 #define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200) /* 2.00 OK */ 171 #define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201) /* 2.01 Created */ 172 #define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203) /* 2.03 Valid */ 173 #define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400) /* 4.00 Bad Request */ 174 #define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404) /* 4.04 Not Found */ 175 #define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405) /* 4.05 Method Not Allowed */ 176 #define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415) /* 4.15 Unsupported Media Type */ 177 #define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500) /* 5.00 Internal Server Error */ 178 #define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501) /* 5.01 Not Implemented */ 179 #define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503) /* 5.03 Service Unavailable */ 180 #define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504) /* 5.04 Gateway Timeout */ 181 #if 0 /* these response codes do not have a valid code any more */ 182 # define COAP_RESPONSE_X_240 240 /* Token Option required by server */ 183 # define COAP_RESPONSE_X_241 241 /* Uri-Authority Option required by server */ 184 #endif 185 #define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402) /* Critical Option not supported */ 186 187 #define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100) 188 #define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701) 189 #define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702) 190 #define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703) 191 #define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704) 192 #define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705) 193 194 /* Applies to COAP_SIGNALING_CSM */ 195 #define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2 196 #define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4 197 /* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */ 198 #define COAP_SIGNALING_OPTION_CUSTODY 2 199 /* Applies to COAP_SIGNALING_RELEASE */ 200 #define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2 201 #define COAP_SIGNALING_OPTION_HOLD_OFF 4 202 /* Applies to COAP_SIGNALING_ABORT */ 203 #define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2 204 205 /* CoAP media type encoding */ 206 207 #define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */ 208 #define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */ 209 #define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */ 210 #define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */ 211 #define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */ 212 #define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */ 213 #define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */ 214 #define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */ 215 216 /* Content formats from RFC 8152 */ 217 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */ 218 #define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */ 219 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */ 220 #define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */ 221 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */ 222 #define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */ 223 224 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */ 225 #define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */ 226 227 /* Content formats from RFC 8428 */ 228 #define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */ 229 #define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */ 230 #define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */ 231 #define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */ 232 #define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */ 233 #define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */ 234 #define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */ 235 #define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */ 236 237 /* Note that identifiers for registered media types are in the range 0-65535. We 238 * use an unallocated type here and hope for the best. */ 239 #define COAP_MEDIATYPE_ANY 0xff /* any media type */ 240 241 /** 242 * coap_tid_t is used to store CoAP transaction id, i.e. a hash value 243 * built from the remote transport address and the message id of a 244 * CoAP PDU. Valid transaction ids are greater or equal zero. 245 */ 246 typedef int coap_tid_t; 247 248 /** Indicates an invalid transaction id. */ 249 #define COAP_INVALID_TID -1 250 251 /** 252 * Indicates that a response is suppressed. This will occur for error 253 * responses if the request was received via IP multicast. 254 */ 255 #define COAP_DROPPED_RESPONSE -2 256 257 #define COAP_PDU_DELAYED -3 258 259 #define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list 260 * in a CoAP message is limited by 0b11110000 261 * marker */ 262 263 #define COAP_OPT_END 0xF0 /* end marker */ 264 265 #define COAP_PAYLOAD_START 0xFF /* payload marker */ 266 267 /** 268 * @deprecated Use coap_optlist_t instead. 269 * 270 * Structures for more convenient handling of options. (To be used with ordered 271 * coap_list_t.) The option's data will be added to the end of the coap_option 272 * structure (see macro COAP_OPTION_DATA). 273 */ 274 COAP_DEPRECATED typedef struct { 275 uint16_t key; /* the option key (no delta coding) */ 276 unsigned int length; 277 } coap_option; 278 279 #define COAP_OPTION_KEY(option) (option).key 280 #define COAP_OPTION_LENGTH(option) (option).length 281 #define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option)) 282 283 /** 284 * structure for CoAP PDUs 285 * token, if any, follows the fixed size header, then options until 286 * payload marker (0xff), then the payload if stored inline. 287 * Memory layout is: 288 * <---header--->|<---token---><---options--->0xff<---payload---> 289 * header is addressed with a negative offset to token, its maximum size is 290 * max_hdr_size. 291 * options starts at token + token_length 292 * payload starts at data, its length is used_size - (data - token) 293 */ 294 295 typedef struct coap_pdu_t { 296 uint8_t type; /**< message type */ 297 uint8_t code; /**< request method (value 1--10) or response code (value 40-255) */ 298 uint8_t max_hdr_size; /**< space reserved for protocol-specific header */ 299 uint8_t hdr_size; /**< actaul size used for protocol-specific header */ 300 uint8_t token_length; /**< length of Token */ 301 uint16_t tid; /**< transaction id, if any, in regular host byte order */ 302 uint16_t max_delta; /**< highest option number */ 303 size_t alloc_size; /**< allocated storage for token, options and payload */ 304 size_t used_size; /**< used bytes of storage for token, options and payload */ 305 size_t max_size; /**< maximum size for token, options and payload, or zero for variable size pdu */ 306 uint8_t *token; /**< first byte of token, if any, or options */ 307 uint8_t *data; /**< first byte of payload, if any */ 308 #ifdef WITH_LWIP 309 struct pbuf *pbuf; /**< lwIP PBUF. The package data will always reside 310 * inside the pbuf's payload, but this pointer 311 * has to be kept because no exact offset can be 312 * given. This field must not be accessed from 313 * outside, because the pbuf's reference count 314 * is checked to be 1 when the pbuf is assigned 315 * to the pdu, and the pbuf stays exclusive to 316 * this pdu. */ 317 #endif 318 } coap_pdu_t; 319 320 #define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0) 321 #define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32) 322 #define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224) 323 #define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224) 324 325 #define COAP_PDU_MAX_UDP_HEADER_SIZE 4 326 #define COAP_PDU_MAX_TCP_HEADER_SIZE 6 327 328 #ifdef WITH_LWIP 329 /** 330 * Creates a CoAP PDU from an lwIP @p pbuf, whose reference is passed on to this 331 * function. 332 * 333 * The pbuf is checked for being contiguous, and for having only one reference. 334 * The reference is stored in the PDU and will be freed when the PDU is freed. 335 * 336 * (For now, these are fatal errors; in future, a new pbuf might be allocated, 337 * the data copied and the passed pbuf freed). 338 * 339 * This behaves like coap_pdu_init(0, 0, 0, pbuf->tot_len), and afterwards 340 * copying the contents of the pbuf to the pdu. 341 * 342 * @return A pointer to the new PDU object or @c NULL on error. 343 */ 344 coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf); 345 #endif 346 347 typedef uint8_t coap_proto_t; 348 /** 349 * coap_proto_t values 350 */ 351 #define COAP_PROTO_NONE 0 352 #define COAP_PROTO_UDP 1 353 #define COAP_PROTO_DTLS 2 354 #define COAP_PROTO_TCP 3 355 #define COAP_PROTO_TLS 4 356 357 /** 358 * Creates a new CoAP PDU with at least enough storage space for the given 359 * @p size maximum message size. The function returns a pointer to the 360 * node coap_pdu_t object on success, or @c NULL on error. The storage allocated 361 * for the result must be released with coap_delete_pdu() if coap_send() is not 362 * called. 363 * 364 * @param type The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON, 365 * COAP_MESSAGE_ACK, COAP_MESSAGE_RST). 366 * @param code The message code. 367 * @param tid The transcation id to set or 0 if unknown / not applicable. 368 * @param size The maximum allowed number of byte for the message. 369 * @return A pointer to the new PDU object or @c NULL on error. 370 */ 371 coap_pdu_t * 372 coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size); 373 374 /** 375 * Dynamically grows the size of @p pdu to @p new_size. The new size 376 * must not exceed the PDU's configure maximum size. On success, this 377 * function returns 1, otherwise 0. 378 * 379 * @param pdu The PDU to resize. 380 * @param new_size The new size in bytes. 381 * @return 1 if the operation succeeded, 0 otherwise. 382 */ 383 int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size); 384 385 /** 386 * Clears any contents from @p pdu and resets @c used_size, 387 * and @c data pointers. @c max_size is set to @p size, any 388 * other field is set to @c 0. Note that @p pdu must be a valid 389 * pointer to a coap_pdu_t object created e.g. by coap_pdu_init(). 390 */ 391 void coap_pdu_clear(coap_pdu_t *pdu, size_t size); 392 393 /** 394 * Creates a new CoAP PDU. 395 */ 396 coap_pdu_t *coap_new_pdu(const struct coap_session_t *session); 397 398 /** 399 * Dispose of an CoAP PDU and frees associated storage. 400 * Not that in general you should not call this function directly. 401 * When a PDU is sent with coap_send(), coap_delete_pdu() will be 402 * called automatically for you. 403 */ 404 405 void coap_delete_pdu(coap_pdu_t *); 406 407 /** 408 * Interprets @p data to determine the number of bytes in the header. 409 * This function returns @c 0 on error or a number greater than zero on success. 410 * 411 * @param proto Session's protocol 412 * @param data The first byte of raw data to parse as CoAP PDU. 413 * 414 * @return A value greater than zero on success or @c 0 on error. 415 */ 416 size_t coap_pdu_parse_header_size(coap_proto_t proto, 417 const uint8_t *data); 418 419 /** 420 * Parses @p data to extract the message size. 421 * @p length must be at least coap_pdu_parse_header_size(proto, data). 422 * This function returns @c 0 on error or a number greater than zero on success. 423 * 424 * @param proto Session's protocol 425 * @param data The raw data to parse as CoAP PDU. 426 * @param length The actual size of @p data. 427 * 428 * @return A value greater than zero on success or @c 0 on error. 429 */ 430 size_t coap_pdu_parse_size(coap_proto_t proto, 431 const uint8_t *data, 432 size_t length); 433 434 /** 435 * Decode the protocol specific header for the specified PDU. 436 * @param pdu A newly received PDU. 437 * @param proto The target wire protocol. 438 * @return 1 for success or 0 on error. 439 */ 440 441 int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto); 442 443 /** 444 * Verify consistency in the given CoAP PDU structure and locate the data. 445 * This function returns @c 0 on error or a number greater than zero on 446 * success. 447 * This function only parses the token and options, up to the payload start 448 * marker. 449 * 450 * @param pdu The PDU structure to. 451 * 452 * @return 1 on success or @c 0 on error. 453 */ 454 int coap_pdu_parse_opt(coap_pdu_t *pdu); 455 456 /** 457 * Parses @p data into the CoAP PDU structure given in @p result. 458 * The target pdu must be large enough to 459 * This function returns @c 0 on error or a number greater than zero on success. 460 * 461 * @param proto Session's protocol 462 * @param data The raw data to parse as CoAP PDU. 463 * @param length The actual size of @p data. 464 * @param pdu The PDU structure to fill. Note that the structure must 465 * provide space to hold at least the token and options 466 * part of the message. 467 * 468 * @return 1 on success or @c 0 on error. 469 */ 470 int coap_pdu_parse(coap_proto_t proto, 471 const uint8_t *data, 472 size_t length, 473 coap_pdu_t *pdu); 474 /** 475 * Adds token of length @p len to @p pdu. 476 * Adding the token destroys any following contents of the pdu. Hence options 477 * and data must be added after coap_add_token() has been called. In @p pdu, 478 * length is set to @p len + @c 4, and max_delta is set to @c 0. This function 479 * returns @c 0 on error or a value greater than zero on success. 480 * 481 * @param pdu The PDU where the token is to be added. 482 * @param len The length of the new token. 483 * @param data The token to add. 484 * 485 * @return A value greater than zero on success, or @c 0 on error. 486 */ 487 int coap_add_token(coap_pdu_t *pdu, 488 size_t len, 489 const uint8_t *data); 490 491 /** 492 * Adds option of given type to pdu that is passed as first 493 * parameter. 494 * coap_add_option() destroys the PDU's data, so coap_add_data() must be called 495 * after all options have been added. As coap_add_token() destroys the options 496 * following the token, the token must be added before coap_add_option() is 497 * called. This function returns the number of bytes written or @c 0 on error. 498 */ 499 size_t coap_add_option(coap_pdu_t *pdu, 500 uint16_t type, 501 size_t len, 502 const uint8_t *data); 503 504 /** 505 * Adds option of given type to pdu that is passed as first parameter, but does 506 * not write a value. It works like coap_add_option with respect to calling 507 * sequence (i.e. after token and before data). This function returns a memory 508 * address to which the option data has to be written before the PDU can be 509 * sent, or @c NULL on error. 510 */ 511 uint8_t *coap_add_option_later(coap_pdu_t *pdu, 512 uint16_t type, 513 size_t len); 514 515 /** 516 * Adds given data to the pdu that is passed as first parameter. Note that the 517 * PDU's data is destroyed by coap_add_option(). coap_add_data() must be called 518 * only once per PDU, otherwise the result is undefined. 519 */ 520 int coap_add_data(coap_pdu_t *pdu, 521 size_t len, 522 const uint8_t *data); 523 524 /** 525 * Adds given data to the pdu that is passed as first parameter but does not 526 * copyt it. Note that the PDU's data is destroyed by coap_add_option(). 527 * coap_add_data() must be have been called once for this PDU, otherwise the 528 * result is undefined. 529 * The actual data must be copied at the returned location. 530 */ 531 uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len); 532 533 /** 534 * Retrieves the length and data pointer of specified PDU. Returns 0 on error or 535 * 1 if *len and *data have correct values. Note that these values are destroyed 536 * with the pdu. 537 */ 538 int coap_get_data(const coap_pdu_t *pdu, 539 size_t *len, 540 uint8_t **data); 541 542 /** 543 * Compose the protocol specific header for the specified PDU. 544 * @param pdu A newly composed PDU. 545 * @param proto The target wire protocol. 546 * @return Number of header bytes prepended before pdu->token or 0 on error. 547 */ 548 549 size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto); 550 551 #endif /* COAP_PDU_H_ */ 552