1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_pdu_access(3) 5c87c5fbaSopenharmony_ci================= 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_pdu_access 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_pdu_access, 14c87c5fbaSopenharmony_cicoap_check_option, 15c87c5fbaSopenharmony_cicoap_decode_var_bytes, 16c87c5fbaSopenharmony_cicoap_decode_var_bytes8, 17c87c5fbaSopenharmony_cicoap_get_data, 18c87c5fbaSopenharmony_cicoap_opt_length, 19c87c5fbaSopenharmony_cicoap_opt_value, 20c87c5fbaSopenharmony_cicoap_option_filter_clear, 21c87c5fbaSopenharmony_cicoap_option_filter_get, 22c87c5fbaSopenharmony_cicoap_option_filter_set, 23c87c5fbaSopenharmony_cicoap_option_filter_unset, 24c87c5fbaSopenharmony_cicoap_option_iterator_init, 25c87c5fbaSopenharmony_cicoap_option_next, 26c87c5fbaSopenharmony_cicoap_pdu_get_code, 27c87c5fbaSopenharmony_cicoap_pdu_get_mid, 28c87c5fbaSopenharmony_cicoap_pdu_get_token, 29c87c5fbaSopenharmony_cicoap_pdu_get_type, 30c87c5fbaSopenharmony_cicoap_get_uri_path 31c87c5fbaSopenharmony_ci- Accessing CoAP PDUs 32c87c5fbaSopenharmony_ci 33c87c5fbaSopenharmony_ciSYNOPSIS 34c87c5fbaSopenharmony_ci-------- 35c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci*coap_opt_t *coap_check_option(const coap_pdu_t *_pdu_, 38c87c5fbaSopenharmony_cicoap_option_num_t _number_, coap_opt_iterator_t *_oi_);* 39c87c5fbaSopenharmony_ci 40c87c5fbaSopenharmony_ci*unsigned int coap_decode_var_bytes(const uint8_t *_buf_, size_t _length_);* 41c87c5fbaSopenharmony_ci 42c87c5fbaSopenharmony_ci*uint64_t coap_decode_var_bytes8(const uint8_t *_buf_, size_t _length_);* 43c87c5fbaSopenharmony_ci 44c87c5fbaSopenharmony_ci*int coap_get_data(const coap_pdu_t *_pdu_, size_t *_length, 45c87c5fbaSopenharmony_ciconst uint8_t **_data_);* 46c87c5fbaSopenharmony_ci 47c87c5fbaSopenharmony_ci*uint32_t coap_opt_length(const coap_opt_t *_opt_);* 48c87c5fbaSopenharmony_ci 49c87c5fbaSopenharmony_ci*const uint8_t *coap_opt_value(const coap_opt_t *_opt_);* 50c87c5fbaSopenharmony_ci 51c87c5fbaSopenharmony_ci*void coap_option_filter_clear(coap_opt_filter_t *_filter_);* 52c87c5fbaSopenharmony_ci 53c87c5fbaSopenharmony_ci*int coap_option_filter_get(coap_opt_filter_t *_filter_, 54c87c5fbaSopenharmony_cicoap_option_num_t _number_);* 55c87c5fbaSopenharmony_ci 56c87c5fbaSopenharmony_ci*int coap_option_filter_set(coap_opt_filter_t *_filter_, 57c87c5fbaSopenharmony_cicoap_option_num_t _number_)*; 58c87c5fbaSopenharmony_ci 59c87c5fbaSopenharmony_ci*int coap_option_filter_unset(coap_opt_filter_t *_filter_, 60c87c5fbaSopenharmony_cicoap_option_num_t _number_);* 61c87c5fbaSopenharmony_ci 62c87c5fbaSopenharmony_ci*coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *_pdu_, 63c87c5fbaSopenharmony_cicoap_opt_iterator_t *_oi_, const coap_opt_filter_t *_filter_);* 64c87c5fbaSopenharmony_ci 65c87c5fbaSopenharmony_ci*coap_opt_t *coap_option_next(coap_opt_iterator_t *_oi_);* 66c87c5fbaSopenharmony_ci 67c87c5fbaSopenharmony_ci*coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *_pdu_);* 68c87c5fbaSopenharmony_ci 69c87c5fbaSopenharmony_ci*coap_mid_t coap_pdu_get_mid(const coap_pdu_t *_pdu_);* 70c87c5fbaSopenharmony_ci 71c87c5fbaSopenharmony_ci*coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *_pdu_);* 72c87c5fbaSopenharmony_ci 73c87c5fbaSopenharmony_ci*coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *_pdu_);* 74c87c5fbaSopenharmony_ci 75c87c5fbaSopenharmony_ci*coap_string_t *coap_get_uri_path(const coap_pdu_t *_pdu_);* 76c87c5fbaSopenharmony_ci 77c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 78c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 79c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 80c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 81c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ciDESCRIPTION 84c87c5fbaSopenharmony_ci----------- 85c87c5fbaSopenharmony_ciThe CoAP PDU is of the form 86c87c5fbaSopenharmony_ci 87c87c5fbaSopenharmony_ci--header--|--optional token--|--optional options--|--optional payload-- 88c87c5fbaSopenharmony_ci 89c87c5fbaSopenharmony_ciThe terminology used is taken mainly from 90c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252#section-1.2[RFC7252 1.2. Terminology]". 91c87c5fbaSopenharmony_ci 92c87c5fbaSopenharmony_ciThe following functions provide access to the information held within 93c87c5fbaSopenharmony_cidifferent parts of a PDU. 94c87c5fbaSopenharmony_ci 95c87c5fbaSopenharmony_ciPDU HEADER FUNCTIONS 96c87c5fbaSopenharmony_ci-------------------- 97c87c5fbaSopenharmony_ci 98c87c5fbaSopenharmony_ci*Function: coap_pdu_get_type()* 99c87c5fbaSopenharmony_ci 100c87c5fbaSopenharmony_ciThe *coap_pdu_get_type*() function returns one of the messages types below from 101c87c5fbaSopenharmony_cithe PDU _pdu_ header. 102c87c5fbaSopenharmony_ci 103c87c5fbaSopenharmony_ci[source, c] 104c87c5fbaSopenharmony_ci---- 105c87c5fbaSopenharmony_ciCOAP_MESSAGE_CON Type confirmable. 106c87c5fbaSopenharmony_ciCOAP_MESSAGE_NON Type non-confirmable. 107c87c5fbaSopenharmony_ciCOAP_MESSAGE_ACK Type acknowledge 108c87c5fbaSopenharmony_ciCOAP_MESSAGE_RST Type reset. 109c87c5fbaSopenharmony_ci---- 110c87c5fbaSopenharmony_ci 111c87c5fbaSopenharmony_ci*NOTE:* For reliable messages https://rfc-editor.org/rfc/rfc8323[RFC8323], 112c87c5fbaSopenharmony_cithis will always return COAP_MESSAGE_CON. 113c87c5fbaSopenharmony_ci 114c87c5fbaSopenharmony_ci*Function: coap_pdu_get_code()* 115c87c5fbaSopenharmony_ci 116c87c5fbaSopenharmony_ciThe *coap_pdu_get_code*() function returns one of the codes below from the 117c87c5fbaSopenharmony_ciPDU _pdu_ header. It is possible that new codes are added in over time. 118c87c5fbaSopenharmony_ci 119c87c5fbaSopenharmony_ci[source, c] 120c87c5fbaSopenharmony_ci---- 121c87c5fbaSopenharmony_ciCOAP_EMPTY_CODE 0.00 122c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_GET 0.01 123c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_POST 0.02 124c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_PUT 0.03 125c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_DELETE 0.04 126c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_FETCH 0.05 127c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_PATCH 0.06 128c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_IPATCH 0.07 129c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_OK 2.00 130c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CREATED 2.01 131c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_DELETED 2.02 132c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_VALID 2.03 133c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CHANGED 2.04 134c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONTENT 2.05 135c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONTINUE 2.31 136c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_REQUEST 4.00 137c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNAUTHORIZED 4.01 138c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_OPTION 4.02 139c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_FORBIDDEN 4.03 140c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_FOUND 4.04 141c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_ALLOWED 4.05 142c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_ACCEPTABLE 4.06 143c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_INCOMPLETE 4.08 144c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONFLICT 4.09 145c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_PRECONDITION_FAILED 4.12 146c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_REQUEST_TOO_LARGE 4.13 147c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT 4.15 148c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNPROCESSABLE 4.22 149c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_TOO_MANY_REQUESTS 4.29 150c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_INTERNAL_ERROR 5.00 151c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_IMPLEMENTED 5.01 152c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_GATEWAY 5.02 153c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_SERVICE_UNAVAILABLE 5.03 154c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_GATEWAY_TIMEOUT 5.04 155c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED 5.05 156c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_HOP_LIMIT_REACHED 5.08 157c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_CSM 7.01 158c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_PING 7.02 159c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_PONG 7.03 160c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_RELEASE 7.04 161c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_ABORT 7.05 162c87c5fbaSopenharmony_ci---- 163c87c5fbaSopenharmony_ci 164c87c5fbaSopenharmony_ci*NOTE:* For reliable messages "ttps://rfc-editor.org/rfc/rfc8323[RFC8323], 165c87c5fbaSopenharmony_cithis will always return COAP_EMPTY_CODE. 166c87c5fbaSopenharmony_ci 167c87c5fbaSopenharmony_ci*Function: coap_pdu_get_mid()* 168c87c5fbaSopenharmony_ci 169c87c5fbaSopenharmony_ciThe *coap_pdu_get_mid*() returns the message id from the PDU _pdu_ header. 170c87c5fbaSopenharmony_ci 171c87c5fbaSopenharmony_ci*NOTE:* For reliable messages https://rfc-editor.org/rfc/rfc8323[RFC8323], 172c87c5fbaSopenharmony_cithis will always return 0. 173c87c5fbaSopenharmony_ci 174c87c5fbaSopenharmony_ciPDU TOKEN FUNCTIONS 175c87c5fbaSopenharmony_ci------------------- 176c87c5fbaSopenharmony_ci 177c87c5fbaSopenharmony_ci*Function: coap_pdu_get_token()* 178c87c5fbaSopenharmony_ci 179c87c5fbaSopenharmony_ciThe *coap_pdu_get_token*() returns the token information held in the PDU _pdu_ 180c87c5fbaSopenharmony_citoken which may be zero length. 181c87c5fbaSopenharmony_ci 182c87c5fbaSopenharmony_ciPDU OPTIONS FUNCTIONS 183c87c5fbaSopenharmony_ci--------------------- 184c87c5fbaSopenharmony_ci 185c87c5fbaSopenharmony_ciThe following is the current list of options with their numeric value 186c87c5fbaSopenharmony_ci---- 187c87c5fbaSopenharmony_ci/* 188c87c5fbaSopenharmony_ci * The C, U, and N flags indicate the properties 189c87c5fbaSopenharmony_ci * Critical, Unsafe, and NoCacheKey, respectively. 190c87c5fbaSopenharmony_ci * If U is set, then N has no meaning as per 191c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7252#section-5.10 192c87c5fbaSopenharmony_ci * and is set to a -. 193c87c5fbaSopenharmony_ci * Separately, R is for the options that can be repeated 194c87c5fbaSopenharmony_ci * 195c87c5fbaSopenharmony_ci * The least significant byte of the option is set as followed 196c87c5fbaSopenharmony_ci * as per https://rfc-editor.org/rfc/rfc7252#section-5.4.6 197c87c5fbaSopenharmony_ci * 198c87c5fbaSopenharmony_ci * 0 1 2 3 4 5 6 7 199c87c5fbaSopenharmony_ci * --+---+---+---+---+---+---+---+ 200c87c5fbaSopenharmony_ci * | NoCacheKey| U | C | 201c87c5fbaSopenharmony_ci * --+---+---+---+---+---+---+---+ 202c87c5fbaSopenharmony_ci * 203c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc8613#section-4 goes on to define E, I and U 204c87c5fbaSopenharmony_ci * properties Encrypted and Integrity Protected, Integrity Protected Only and 205c87c5fbaSopenharmony_ci * Unprotected respectively. Integrity Protected Only is not currently used. 206c87c5fbaSopenharmony_ci * 207c87c5fbaSopenharmony_ci * An Option is tagged with CUNREIU with any of the letters replaced with _ if 208c87c5fbaSopenharmony_ci * not set, or - for N if U is set (see above) for aiding understanding of the 209c87c5fbaSopenharmony_ci * Option. 210c87c5fbaSopenharmony_ci */ 211c87c5fbaSopenharmony_ci 212c87c5fbaSopenharmony_ciCOAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */ 213c87c5fbaSopenharmony_ciCOAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */ 214c87c5fbaSopenharmony_ciCOAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */ 215c87c5fbaSopenharmony_ciCOAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */ 216c87c5fbaSopenharmony_ciCOAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint, 0 B/0-3 B, RFC7641 */ 217c87c5fbaSopenharmony_ciCOAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */ 218c87c5fbaSopenharmony_ciCOAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */ 219c87c5fbaSopenharmony_ciCOAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */ 220c87c5fbaSopenharmony_ciCOAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */ 221c87c5fbaSopenharmony_ciCOAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */ 222c87c5fbaSopenharmony_ci/* COAP_OPTION_MAXAGE default 60 seconds if not set */ 223c87c5fbaSopenharmony_ciCOAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */ 224c87c5fbaSopenharmony_ciCOAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */ 225c87c5fbaSopenharmony_ciCOAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */ 226c87c5fbaSopenharmony_ciCOAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */ 227c87c5fbaSopenharmony_ciCOAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */ 228c87c5fbaSopenharmony_ciCOAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */ 229c87c5fbaSopenharmony_ciCOAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */ 230c87c5fbaSopenharmony_ciCOAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */ 231c87c5fbaSopenharmony_ciCOAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */ 232c87c5fbaSopenharmony_ciCOAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */ 233c87c5fbaSopenharmony_ciCOAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */ 234c87c5fbaSopenharmony_ciCOAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */ 235c87c5fbaSopenharmony_ci---- 236c87c5fbaSopenharmony_ciSee FURTHER INFORMATION as to how to get the latest list. 237c87c5fbaSopenharmony_ci 238c87c5fbaSopenharmony_ci*Function: coap_check_option()* 239c87c5fbaSopenharmony_ci 240c87c5fbaSopenharmony_ciThe *coap_check_option*() function is used to check whether the specified option 241c87c5fbaSopenharmony_ci_number_ is in the PDU _pdu_ options. The option iterator _oi_ is used as a 242c87c5fbaSopenharmony_ciscratch (does not need to be initialized) internal 243c87c5fbaSopenharmony_cistorage location while iterating through the options looking for the specific 244c87c5fbaSopenharmony_ci_number_. If the _number_ is repeated in the _pdu_, only the first occurrence 245c87c5fbaSopenharmony_ciwill be returned. If the option is not found, NULL is returned. 246c87c5fbaSopenharmony_ci 247c87c5fbaSopenharmony_ci*Function: coap_option_iterator_init()* 248c87c5fbaSopenharmony_ci 249c87c5fbaSopenharmony_ciThe *coap_option_iterator_init*() function can be used to 250c87c5fbaSopenharmony_ciinitialize option iterator _oi_, applying a filter _filter_ to indicate which 251c87c5fbaSopenharmony_cioptions are to be ignored when iterating through the options. The _filter_ can 252c87c5fbaSopenharmony_cibe NULL (or COAP_OPT_ALL) if all of the options are required. 253c87c5fbaSopenharmony_ciTo set up the filter otherwise, the following 4 functions are available. 254c87c5fbaSopenharmony_ci 255c87c5fbaSopenharmony_ci*Function: coap_option_filter_clear()* 256c87c5fbaSopenharmony_ci 257c87c5fbaSopenharmony_ciThe *coap_option_filter_clear*() function initializes _filter_ to have no 258c87c5fbaSopenharmony_cioptions set. 259c87c5fbaSopenharmony_ci 260c87c5fbaSopenharmony_ci*Function: coap_option_filter_get()* 261c87c5fbaSopenharmony_ci 262c87c5fbaSopenharmony_ciThe *coap_option_filter_get*() function is used to check whether option _number_ 263c87c5fbaSopenharmony_ciis set in _filter_. 264c87c5fbaSopenharmony_ci 265c87c5fbaSopenharmony_ci*Function: coap_option_filter_set()* 266c87c5fbaSopenharmony_ci 267c87c5fbaSopenharmony_ciThe *coap_option_filter_set*() function is used to set option _number_ in 268c87c5fbaSopenharmony_ci_filter_. 269c87c5fbaSopenharmony_ci 270c87c5fbaSopenharmony_ci*Function: coap_option_filter_unset()* 271c87c5fbaSopenharmony_ci 272c87c5fbaSopenharmony_ciThe *coap_option_filter_unset*() function is used to remove option _number_ in 273c87c5fbaSopenharmony_ci_filter_. 274c87c5fbaSopenharmony_ci 275c87c5fbaSopenharmony_ci*Function: coap_option_next()* 276c87c5fbaSopenharmony_ci 277c87c5fbaSopenharmony_ciThe *coap_option_next*() function is then used following 278c87c5fbaSopenharmony_ci*coap_option_iterator_init*() in a loop to return all 279c87c5fbaSopenharmony_cithe appropriate options until NULL is returned - indicating the end of 280c87c5fbaSopenharmony_cithe available options. See EXAMPLES below for further information. 281c87c5fbaSopenharmony_ci 282c87c5fbaSopenharmony_ci*Function: coap_opt_length()* 283c87c5fbaSopenharmony_ci 284c87c5fbaSopenharmony_ciThe *coap_opt_length*() function returns the length of the option _opt_ 285c87c5fbaSopenharmony_ci(as returned by *coap_check_option*() or *coap_option_next*()). 286c87c5fbaSopenharmony_ci 287c87c5fbaSopenharmony_ci*Function: coap_opt_value()* 288c87c5fbaSopenharmony_ci 289c87c5fbaSopenharmony_ciThe *coap_opt_value*() function returns a pointer to the start of the data for 290c87c5fbaSopenharmony_cithe option _opt_ (as returned by *coap_check_option*() or *coap_option_next*()). 291c87c5fbaSopenharmony_ci 292c87c5fbaSopenharmony_ci*Function: coap_decode_var_bytes()* 293c87c5fbaSopenharmony_ci 294c87c5fbaSopenharmony_ciThe *coap_decode_var_bytes*() function will decode an option value up to 4 bytes 295c87c5fbaSopenharmony_cilong from _buf_ and _length_ into an unsigned 32bit number. 296c87c5fbaSopenharmony_ci 297c87c5fbaSopenharmony_ci*Function: coap_decode_var_bytes8()* 298c87c5fbaSopenharmony_ci 299c87c5fbaSopenharmony_ciThe *coap_decode_var_bytes8*() function will decode an option value up to 8 300c87c5fbaSopenharmony_cibytes long from _buf_ and _length_ into an unsigned 64bit number. 301c87c5fbaSopenharmony_ci 302c87c5fbaSopenharmony_ci*Function: coap_get_uri_path()* 303c87c5fbaSopenharmony_ci 304c87c5fbaSopenharmony_ciThe *coap_get_uri_path*() function will abstract the uri path from the 305c87c5fbaSopenharmony_cispecified _pdu_ options. The returned uri path will need to be freed off when no longer required. 306c87c5fbaSopenharmony_ci 307c87c5fbaSopenharmony_ciPDU PAYLOAD FUNCTIONS 308c87c5fbaSopenharmony_ci--------------------- 309c87c5fbaSopenharmony_ci 310c87c5fbaSopenharmony_ci*Function: coap_get_data()* 311c87c5fbaSopenharmony_ci 312c87c5fbaSopenharmony_ciThe *coap_get_data*() function is used abstract from the _pdu_ payload 313c87c5fbaSopenharmony_ciinformation about the received data by updating _length_ with the length of 314c87c5fbaSopenharmony_cidata available, and _data_ with a pointer to where the data is located. 315c87c5fbaSopenharmony_ci 316c87c5fbaSopenharmony_ci*NOTE:* This function has been updated by *coap_get_data_large*() when large 317c87c5fbaSopenharmony_citransfers may take place. See coap_block(3). 318c87c5fbaSopenharmony_ci 319c87c5fbaSopenharmony_ciRETURN VALUES 320c87c5fbaSopenharmony_ci------------- 321c87c5fbaSopenharmony_ci 322c87c5fbaSopenharmony_ci*coap_check_option*() and *coap_option_next*() returns a coap_opt_t* or 323c87c5fbaSopenharmony_ciNULL if not found. 324c87c5fbaSopenharmony_ci 325c87c5fbaSopenharmony_ci*coap_decode_var_bytes*() and *coap_decode_var_bytes8*() return the decoded 326c87c5fbaSopenharmony_civalue. 327c87c5fbaSopenharmony_ci 328c87c5fbaSopenharmony_ci*coap_pdu_get_code*(), *coap_pdu_get_mid*(), *coap_pdu_get_type*() return 329c87c5fbaSopenharmony_cithe appropriate value. 330c87c5fbaSopenharmony_ci 331c87c5fbaSopenharmony_ci*coap_option_filter_get*(), *coap_option_filter_set*() and 332c87c5fbaSopenharmony_ci*coap_option_filter_unset*() return 1 on success or 0 on failure. 333c87c5fbaSopenharmony_ci 334c87c5fbaSopenharmony_ci*coap_get_data*() returns 1 if data, else 0. 335c87c5fbaSopenharmony_ci 336c87c5fbaSopenharmony_ci*coap_opt_length*() returns the option length. 337c87c5fbaSopenharmony_ci 338c87c5fbaSopenharmony_ci*coap_opt_value*() returns a pointer to the start of the option value or 339c87c5fbaSopenharmony_ciNULL if error. 340c87c5fbaSopenharmony_ci 341c87c5fbaSopenharmony_ci*coap_option_iterator_init*() returns ap pointer to the provided iterator 342c87c5fbaSopenharmony_cior NULL on error. 343c87c5fbaSopenharmony_ci 344c87c5fbaSopenharmony_ci*coap_pdu_get_token*() returns a pointer to the token in the pdu. 345c87c5fbaSopenharmony_ci 346c87c5fbaSopenharmony_ci*coap_get_uri_path*() returns an allocated pointer to the uri path in the 347c87c5fbaSopenharmony_cipdu or NULL on error. This pointer will need to be freed off. 348c87c5fbaSopenharmony_ci 349c87c5fbaSopenharmony_ciEXAMPLES 350c87c5fbaSopenharmony_ci-------- 351c87c5fbaSopenharmony_ci*Abstract information from PDU* 352c87c5fbaSopenharmony_ci 353c87c5fbaSopenharmony_ci[source, c] 354c87c5fbaSopenharmony_ci---- 355c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 356c87c5fbaSopenharmony_ci 357c87c5fbaSopenharmony_cistatic void 358c87c5fbaSopenharmony_ciget_pdu_information(coap_pdu_t *pdu) { 359c87c5fbaSopenharmony_ci 360c87c5fbaSopenharmony_ci int ret; 361c87c5fbaSopenharmony_ci /* Header variables */ 362c87c5fbaSopenharmony_ci coap_pdu_type_t pdu_type; 363c87c5fbaSopenharmony_ci coap_pdu_code_t pdu_code; 364c87c5fbaSopenharmony_ci coap_mid_t pdu_mid; 365c87c5fbaSopenharmony_ci /* Token variables */ 366c87c5fbaSopenharmony_ci coap_bin_const_t pdu_token; 367c87c5fbaSopenharmony_ci /* Option variables */ 368c87c5fbaSopenharmony_ci coap_opt_iterator_t opt_iter; 369c87c5fbaSopenharmony_ci coap_opt_t *option; 370c87c5fbaSopenharmony_ci coap_opt_filter_t ignore_options; 371c87c5fbaSopenharmony_ci 372c87c5fbaSopenharmony_ci /* Data payload variables */ 373c87c5fbaSopenharmony_ci size_t pdu_data_length; 374c87c5fbaSopenharmony_ci const uint8_t *pdu_data; 375c87c5fbaSopenharmony_ci size_t pdu_data_offset; 376c87c5fbaSopenharmony_ci size_t pdu_data_total_length; 377c87c5fbaSopenharmony_ci 378c87c5fbaSopenharmony_ci /* Pull in the header information */ 379c87c5fbaSopenharmony_ci pdu_type = coap_pdu_get_type(pdu); 380c87c5fbaSopenharmony_ci pdu_code = coap_pdu_get_code(pdu); 381c87c5fbaSopenharmony_ci pdu_mid = coap_pdu_get_mid(pdu); 382c87c5fbaSopenharmony_ci 383c87c5fbaSopenharmony_ci /* Pull in the token information */ 384c87c5fbaSopenharmony_ci pdu_token = coap_pdu_get_token(pdu); 385c87c5fbaSopenharmony_ci 386c87c5fbaSopenharmony_ci /* Pull in the option information */ 387c87c5fbaSopenharmony_ci /* Specific option check */ 388c87c5fbaSopenharmony_ci option = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter); 389c87c5fbaSopenharmony_ci if (option) { 390c87c5fbaSopenharmony_ci uint32_t value = coap_decode_var_bytes(coap_opt_value(option), 391c87c5fbaSopenharmony_ci coap_opt_length(option)); 392c87c5fbaSopenharmony_ci coap_log_info("Option OBSERVE, value %u\n", value); 393c87c5fbaSopenharmony_ci } 394c87c5fbaSopenharmony_ci /* Iterate through all options */ 395c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &opt_iter, COAP_OPT_ALL); 396c87c5fbaSopenharmony_ci while ((option = coap_option_next(&opt_iter))) { 397c87c5fbaSopenharmony_ci coap_log_info("A: Option %d, Length %u\n", 398c87c5fbaSopenharmony_ci opt_iter.number, coap_opt_length(option)); 399c87c5fbaSopenharmony_ci } 400c87c5fbaSopenharmony_ci /* Iterate through options, some ignored */ 401c87c5fbaSopenharmony_ci coap_option_filter_clear(&ignore_options); 402c87c5fbaSopenharmony_ci coap_option_filter_set(&ignore_options, COAP_OPTION_OBSERVE); 403c87c5fbaSopenharmony_ci coap_option_iterator_init(pdu, &opt_iter, &ignore_options); 404c87c5fbaSopenharmony_ci while ((option = coap_option_next(&opt_iter))) { 405c87c5fbaSopenharmony_ci coap_log_info("I: Option %d, Length %u\n", 406c87c5fbaSopenharmony_ci opt_iter.number, coap_opt_length(option)); 407c87c5fbaSopenharmony_ci } 408c87c5fbaSopenharmony_ci 409c87c5fbaSopenharmony_ci /* Pull in the payload information */ 410c87c5fbaSopenharmony_ci ret = coap_get_data(pdu, &pdu_data_length, &pdu_data); 411c87c5fbaSopenharmony_ci /* or */ 412c87c5fbaSopenharmony_ci ret = coap_get_data_large(pdu, &pdu_data_length, &pdu_data, 413c87c5fbaSopenharmony_ci &pdu_data_offset, &pdu_data_total_length); 414c87c5fbaSopenharmony_ci 415c87c5fbaSopenharmony_ci} 416c87c5fbaSopenharmony_ci---- 417c87c5fbaSopenharmony_ci 418c87c5fbaSopenharmony_ciSEE ALSO 419c87c5fbaSopenharmony_ci-------- 420c87c5fbaSopenharmony_ci*coap_block*(3) and *coap_pdu_setup*(3) 421c87c5fbaSopenharmony_ci 422c87c5fbaSopenharmony_ciFURTHER INFORMATION 423c87c5fbaSopenharmony_ci------------------- 424c87c5fbaSopenharmony_ciSee 425c87c5fbaSopenharmony_ci 426c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 427c87c5fbaSopenharmony_ci 428c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc8613[RFC8613: Object Security for Constrained RESTful Environments (OSCORE)]" 429c87c5fbaSopenharmony_ci 430c87c5fbaSopenharmony_cifor further information. 431c87c5fbaSopenharmony_ci 432c87c5fbaSopenharmony_ciSee 433c87c5fbaSopenharmony_cihttps://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers 434c87c5fbaSopenharmony_cifor the current set of defined CoAP Options. 435c87c5fbaSopenharmony_ci 436c87c5fbaSopenharmony_ciBUGS 437c87c5fbaSopenharmony_ci---- 438c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 439c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 440c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 441c87c5fbaSopenharmony_ci 442c87c5fbaSopenharmony_ciAUTHORS 443c87c5fbaSopenharmony_ci------- 444c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 445