1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_pdu_setup(3) 5c87c5fbaSopenharmony_ci================= 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_pdu_setup 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_pdu_setup, 14c87c5fbaSopenharmony_cicoap_new_pdu, 15c87c5fbaSopenharmony_cicoap_pdu_init, 16c87c5fbaSopenharmony_cicoap_new_message_id, 17c87c5fbaSopenharmony_cicoap_session_init_token, 18c87c5fbaSopenharmony_cicoap_session_new_token, 19c87c5fbaSopenharmony_cicoap_add_token, 20c87c5fbaSopenharmony_cicoap_new_optlist, 21c87c5fbaSopenharmony_cicoap_insert_optlist, 22c87c5fbaSopenharmony_cicoap_delete_optlist, 23c87c5fbaSopenharmony_cicoap_encode_var_safe, 24c87c5fbaSopenharmony_cicoap_encode_var_safe8, 25c87c5fbaSopenharmony_cicoap_add_optlist_pdu, 26c87c5fbaSopenharmony_cicoap_add_option, 27c87c5fbaSopenharmony_cicoap_add_data, 28c87c5fbaSopenharmony_cicoap_add_data_blocked_response, 29c87c5fbaSopenharmony_cicoap_send, 30c87c5fbaSopenharmony_cicoap_split_path, 31c87c5fbaSopenharmony_cicoap_split_query, 32c87c5fbaSopenharmony_cicoap_pdu_set_mid, 33c87c5fbaSopenharmony_cicoap_pdu_set_code, 34c87c5fbaSopenharmony_cicoap_pdu_set_type 35c87c5fbaSopenharmony_ci- Setting up CoAP PDUs 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ciSYNOPSIS 38c87c5fbaSopenharmony_ci-------- 39c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 40c87c5fbaSopenharmony_ci 41c87c5fbaSopenharmony_ci*coap_pdu_t *coap_new_pdu(coap_pdu_type_t _type_, coap_pdu_code_t _code_, 42c87c5fbaSopenharmony_cicoap_session_t *_session_);* 43c87c5fbaSopenharmony_ci 44c87c5fbaSopenharmony_ci*coap_pdu_t *coap_pdu_init(coap_pdu_type_t _type_, coap_pdu_code_t _code_, 45c87c5fbaSopenharmony_cicoap_mid_t _message_id_, size_t _max_size_);* 46c87c5fbaSopenharmony_ci 47c87c5fbaSopenharmony_ci*uint16_t coap_new_message_id(coap_session_t *_session_);* 48c87c5fbaSopenharmony_ci 49c87c5fbaSopenharmony_ci*void coap_session_init_token(coap_session_t *_session_, size_t _length_, 50c87c5fbaSopenharmony_ciconst uint8_t *_token_);* 51c87c5fbaSopenharmony_ci 52c87c5fbaSopenharmony_ci*void coap_session_new_token(coap_session_t *_session_, size_t *_length_, 53c87c5fbaSopenharmony_ciuint8_t *_token_);* 54c87c5fbaSopenharmony_ci 55c87c5fbaSopenharmony_ci*int coap_add_token(coap_pdu_t *_pdu_, size_t _length_, 56c87c5fbaSopenharmony_ciconst uint8_t *_data_);* 57c87c5fbaSopenharmony_ci 58c87c5fbaSopenharmony_ci*coap_optlist_t *coap_new_optlist(uint16_t _number_, size_t _length_, 59c87c5fbaSopenharmony_ciconst uint8_t *_data_);* 60c87c5fbaSopenharmony_ci 61c87c5fbaSopenharmony_ci*int coap_insert_optlist(coap_optlist_t **_optlist_chain_, 62c87c5fbaSopenharmony_cicoap_optlist_t *_optlist_);* 63c87c5fbaSopenharmony_ci 64c87c5fbaSopenharmony_ci*void coap_delete_optlist(coap_optlist_t *_optlist_chain_);* 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ci*unsigned int coap_encode_var_safe(uint8_t *_buffer_, size_t _size_, 67c87c5fbaSopenharmony_ciunsigned int _value_);* 68c87c5fbaSopenharmony_ci 69c87c5fbaSopenharmony_ci*unsigned int coap_encode_var_safe8(uint8_t *_buffer_, size_t _size_, 70c87c5fbaSopenharmony_ciuint64_t _value_);* 71c87c5fbaSopenharmony_ci 72c87c5fbaSopenharmony_ci*int coap_add_optlist_pdu(coap_pdu_t *_pdu_, coap_optlist_t **_optlist_chain_);* 73c87c5fbaSopenharmony_ci 74c87c5fbaSopenharmony_ci*size_t coap_add_option(coap_pdu_t *_pdu_, uint16_t _number_, size_t _length_, 75c87c5fbaSopenharmony_ciconst uint8_t *_data_);* 76c87c5fbaSopenharmony_ci 77c87c5fbaSopenharmony_ci*int coap_add_data(coap_pdu_t *_pdu_, size_t _length_, const uint8_t *_data_);* 78c87c5fbaSopenharmony_ci 79c87c5fbaSopenharmony_ci*void coap_add_data_blocked_response(const coap_pdu_t *_request_, 80c87c5fbaSopenharmony_cicoap_pdu_t *_response_, uint16_t _media_type_, int _maxage_, size_t _length_, 81c87c5fbaSopenharmony_ciconst uint8_t *_data_);* 82c87c5fbaSopenharmony_ci 83c87c5fbaSopenharmony_ci*coap_mid_t coap_send(coap_session_t *_session_, coap_pdu_t *_pdu_);* 84c87c5fbaSopenharmony_ci 85c87c5fbaSopenharmony_ci*int coap_split_path(const uint8_t *_path_, size_t _length_, uint8_t *_buffer_, 86c87c5fbaSopenharmony_cisize_t *_buflen_);* 87c87c5fbaSopenharmony_ci 88c87c5fbaSopenharmony_ci*int coap_split_query(const uint8_t *_query_, size_t _length_, 89c87c5fbaSopenharmony_ciuint8_t *_buffer_, size_t *_buflen_);* 90c87c5fbaSopenharmony_ci 91c87c5fbaSopenharmony_ci*void coap_pdu_set_mid(coap_pdu_t *_pdu_, coap_mid_t _mid_);* 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ci*void coap_pdu_set_code(coap_pdu_t *_pdu_, coap_pdu_code_t _code_);* 94c87c5fbaSopenharmony_ci 95c87c5fbaSopenharmony_ci*void coap_pdu_set_type(coap_pdu_t *_pdu_, coap_pdu_type_t _type_);* 96c87c5fbaSopenharmony_ci 97c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 98c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 99c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 100c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 101c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 102c87c5fbaSopenharmony_ci 103c87c5fbaSopenharmony_ciDESCRIPTION 104c87c5fbaSopenharmony_ci----------- 105c87c5fbaSopenharmony_ciThe CoAP PDU is of the form 106c87c5fbaSopenharmony_ci 107c87c5fbaSopenharmony_ci--header--|--optional token--|--optional options--|--optional payload-- 108c87c5fbaSopenharmony_ci 109c87c5fbaSopenharmony_ciThe terminology used is taken mainly from 110c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252#section-1.2[RFC7252 1.2. Terminology]". 111c87c5fbaSopenharmony_ci 112c87c5fbaSopenharmony_ciThe PDU must be built in the correct order, from left to right. In particular, 113c87c5fbaSopenharmony_cithe options need to be added in the correct numerical option order as they are 114c87c5fbaSopenharmony_cistored in the PDU using relative numeric offsets from the previous option 115c87c5fbaSopenharmony_cinumber. 116c87c5fbaSopenharmony_ci 117c87c5fbaSopenharmony_ciThere are option list functions available where options can be added to a 118c87c5fbaSopenharmony_cichained list of options and then the chain list is sorted and then be added to 119c87c5fbaSopenharmony_cithe PDU. 120c87c5fbaSopenharmony_ci 121c87c5fbaSopenharmony_ciTypically for clients, when creating a request, the PDU needs to be created 122c87c5fbaSopenharmony_cibefore filling it with the appropriate information. 123c87c5fbaSopenharmony_ci 124c87c5fbaSopenharmony_ciTypically with a server, the response PDU, with the optional token already added 125c87c5fbaSopenharmony_ciin, will already be created before the response handler is called, and the 126c87c5fbaSopenharmony_ciresponse PDU will need to be updated as appropriate starting with the optional 127c87c5fbaSopenharmony_cioptions. Note that updating the response pdu's code variable will cause the 128c87c5fbaSopenharmony_ciresponse pdu to get transmitted. If code does not get updated, and the PDU is 129c87c5fbaSopenharmony_ciof type CONFIRMABLE, then the response PDU is transmitted as an empty ACK 130c87c5fbaSopenharmony_cipacket. The response pdu is always freed off by the underlying library. 131c87c5fbaSopenharmony_ci 132c87c5fbaSopenharmony_ciFor handling situations where the data to be transmitted does not fit into a 133c87c5fbaSopenharmony_cisingle packet, see *coap_block*(3). 134c87c5fbaSopenharmony_ci 135c87c5fbaSopenharmony_ciPDU CREATE AND HEADER FUNCTIONS 136c87c5fbaSopenharmony_ci------------------------------- 137c87c5fbaSopenharmony_ci 138c87c5fbaSopenharmony_ci*Function: coap_new_pdu()* 139c87c5fbaSopenharmony_ci 140c87c5fbaSopenharmony_ciThe *coap_new_pdu*() function returns a newly created PDU of type 141c87c5fbaSopenharmony_ci_coap_pdu_t_. 142c87c5fbaSopenharmony_ci 143c87c5fbaSopenharmony_ciThe _type_ is one of the following 144c87c5fbaSopenharmony_ci 145c87c5fbaSopenharmony_ci[source, c] 146c87c5fbaSopenharmony_ci---- 147c87c5fbaSopenharmony_ciCOAP_MESSAGE_CON Set the _PDU_ to be of type confirmable. 148c87c5fbaSopenharmony_ciCOAP_MESSAGE_NON Set the _PDU_ to be of type non-confirmable. 149c87c5fbaSopenharmony_ciCOAP_MESSAGE_ACK Set the _PDU_ to be of type acknowledge (for internal use). 150c87c5fbaSopenharmony_ciCOAP_MESSAGE_RST Set the _PDU_ to be of type reset. 151c87c5fbaSopenharmony_ci---- 152c87c5fbaSopenharmony_ci 153c87c5fbaSopenharmony_ciThe _code_ is one of the following 154c87c5fbaSopenharmony_ci 155c87c5fbaSopenharmony_ci[source, c] 156c87c5fbaSopenharmony_ci---- 157c87c5fbaSopenharmony_ciCOAP_EMPTY_CODE 0.00 158c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_GET 0.01 159c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_POST 0.02 160c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_PUT 0.03 161c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_DELETE 0.04 162c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_FETCH 0.05 163c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_PATCH 0.06 164c87c5fbaSopenharmony_ciCOAP_REQUEST_CODE_IPATCH 0.07 165c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_OK 2.00 166c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CREATED 2.01 167c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_DELETED 2.02 168c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_VALID 2.03 169c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CHANGED 2.04 170c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONTENT 2.05 171c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONTINUE 2.31 172c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_REQUEST 4.00 173c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNAUTHORIZED 4.01 174c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_OPTION 4.02 175c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_FORBIDDEN 4.03 176c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_FOUND 4.04 177c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_ALLOWED 4.05 178c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_ACCEPTABLE 4.06 179c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_INCOMPLETE 4.08 180c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_CONFLICT 4.09 181c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_PRECONDITION_FAILED 4.12 182c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_REQUEST_TOO_LARGE 4.13 183c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT 4.15 184c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_UNPROCESSABLE 4.22 185c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_TOO_MANY_REQUESTS 4.29 186c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_INTERNAL_ERROR 5.00 187c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_NOT_IMPLEMENTED 5.01 188c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_BAD_GATEWAY 5.02 189c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_SERVICE_UNAVAILABLE 5.03 190c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_GATEWAY_TIMEOUT 5.04 191c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED 5.05 192c87c5fbaSopenharmony_ciCOAP_RESPONSE_CODE_HOP_LIMIT_REACHED 5.08 193c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_CSM 7.01 194c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_PING 7.02 195c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_PONG 7.03 196c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_RELEASE 7.04 197c87c5fbaSopenharmony_ciCOAP_SIGNALING_CODE_ABORT 7.05 198c87c5fbaSopenharmony_ci---- 199c87c5fbaSopenharmony_ci 200c87c5fbaSopenharmony_ciand _session_ is used to set up other default values. 201c87c5fbaSopenharmony_ci 202c87c5fbaSopenharmony_ci*Function: coap_pdu_init()* 203c87c5fbaSopenharmony_ci 204c87c5fbaSopenharmony_ciThe *coap_pdu_init*() function does the same work as *coap_new_pdu*() but gives 205c87c5fbaSopenharmony_cithe additional ability to define the default values for _message_id_ and 206c87c5fbaSopenharmony_ci_max_size_ that *coap_new_pdu*() creates. 207c87c5fbaSopenharmony_ci 208c87c5fbaSopenharmony_ciThe _message_id_ must be unique per request (which is not the same as the 209c87c5fbaSopenharmony_citoken), and must not be reused within EXCHANGE_LIFETIME (usually 247 seconds). 210c87c5fbaSopenharmony_ciTo automate this, the function *coap_new_message_id*(_session_) should be 211c87c5fbaSopenharmony_cicalled. 212c87c5fbaSopenharmony_ci 213c87c5fbaSopenharmony_ciAt the CoAP protocol level, requests and responses are matched by _message_id_ 214c87c5fbaSopenharmony_ciwhich is why it needs to be unique. At the application level, for "separate" 215c87c5fbaSopenharmony_ciresponses, the initial empty ACK response matches the _message_id_ of the 216c87c5fbaSopenharmony_cirequest (handled by libcoap) but the actual response has the same token as the 217c87c5fbaSopenharmony_cirequest and this must be used for the match. For "piggybacked" responses the 218c87c5fbaSopenharmony_citoken must still be used as the valid match for request and response. and the 219c87c5fbaSopenharmony_ci_message_id_ just happens to match (but unsafe in case the server is sending 220c87c5fbaSopenharmony_ciback a "separate" response). 221c87c5fbaSopenharmony_ci 222c87c5fbaSopenharmony_ciThe _max_size_ parameter defines the maximum size of a _PDU_ and is usually 223c87c5fbaSopenharmony_cidetermined by calling *coap_session_max_pdu_size*(session); 224c87c5fbaSopenharmony_ci 225c87c5fbaSopenharmony_ci*Function: coap_new_message_id()* 226c87c5fbaSopenharmony_ci 227c87c5fbaSopenharmony_ciThe *coap_new_message_id*() function returns the next message id to use for 228c87c5fbaSopenharmony_cisending a new request message. 229c87c5fbaSopenharmony_ci 230c87c5fbaSopenharmony_ci*NOTE:* For reliable messages https://rfc-editor.org/rfc/rfc8323[RFC8323], 231c87c5fbaSopenharmony_cithis will always return 0. 232c87c5fbaSopenharmony_ci 233c87c5fbaSopenharmony_ci*Function: coap_pdu_set_mid()* 234c87c5fbaSopenharmony_ci 235c87c5fbaSopenharmony_ciThe *coap_pdu_set_mid*() function is used to set the message id _mid_ in the 236c87c5fbaSopenharmony_ciPDU _pdu_. 237c87c5fbaSopenharmony_ci 238c87c5fbaSopenharmony_ci*Function: coap_pdu_set_code()* 239c87c5fbaSopenharmony_ci 240c87c5fbaSopenharmony_ciThe *coap_pdu_set_code*() function is used to set the code _code_ in the PDU 241c87c5fbaSopenharmony_ci_pdu_. 242c87c5fbaSopenharmony_ci 243c87c5fbaSopenharmony_ci*Function: coap_pdu_set_type()* 244c87c5fbaSopenharmony_ci 245c87c5fbaSopenharmony_ciThe *coap_pdu_set_type*() function is used to set the _type_ of the PDU _pdu_. 246c87c5fbaSopenharmony_ci 247c87c5fbaSopenharmony_ci*NOTE:* A PDU does not need to be created by the server application to send 248c87c5fbaSopenharmony_ciback a response. The libcoap server logic creates the initial PDU with 249c87c5fbaSopenharmony_ciCOAP_EMPTY_CODE, appropriate message_id, matching token and potentially some 250c87c5fbaSopenharmony_ciother options before calling the appropriate request handler (See 251c87c5fbaSopenharmony_ci*coap_register_request_handler*(3)). 252c87c5fbaSopenharmony_ci 253c87c5fbaSopenharmony_ciPDU TOKEN FUNCTIONS 254c87c5fbaSopenharmony_ci------------------- 255c87c5fbaSopenharmony_ci 256c87c5fbaSopenharmony_ci*Function: coap_session_init_token()* 257c87c5fbaSopenharmony_ci 258c87c5fbaSopenharmony_ciThe *coap_session_init_token*() function is used to initialize the starting 259c87c5fbaSopenharmony_ci_token_ of _length_ for the _session_. 260c87c5fbaSopenharmony_ci 261c87c5fbaSopenharmony_ci*NOTE:* this only takes the first 8 bytes of the token if extended tokens 262c87c5fbaSopenharmony_ciare being used (https://rfc-editor.org/rfc/rfc8974[RFC8974]). 263c87c5fbaSopenharmony_ci 264c87c5fbaSopenharmony_ci*Function: coap_session_new_token()* 265c87c5fbaSopenharmony_ci 266c87c5fbaSopenharmony_ciThe *coap_session_new_token*() function is used to obtain the next available 267c87c5fbaSopenharmony_ci_token_ of _length_ for the _session_. Note that the same token must be used 268c87c5fbaSopenharmony_cifor doing an observe cancellation that was used for doing the observe 269c87c5fbaSopenharmony_ciregistration. Otherwise tokens should be unique for each request/response so 270c87c5fbaSopenharmony_cithat they can be correctly matched. 271c87c5fbaSopenharmony_ci 272c87c5fbaSopenharmony_ci*NOTE:* Only a token of up to 8 bytes is returned. 273c87c5fbaSopenharmony_ci 274c87c5fbaSopenharmony_ci*Function: coap_add_token()* 275c87c5fbaSopenharmony_ci 276c87c5fbaSopenharmony_ciThe *coap_add_token*() function adds in the specified token's _data_ of length 277c87c5fbaSopenharmony_ci_length_ to the PDU _pdu_. The maximum (but impractical due to PDU sizes) 278c87c5fbaSopenharmony_cilength of the token is 65804 bytes. If anything more than 8, the remote peer 279c87c5fbaSopenharmony_cineeds to support extended tokens for this to work. 280c87c5fbaSopenharmony_ciAdding the token must be done before any options or data are added. 281c87c5fbaSopenharmony_ciThis function must only be called once per _pdu_, and must not be called 282c87c5fbaSopenharmony_ciin the appropriate request handler as the token has already been added into 283c87c5fbaSopenharmony_cithe skeletal response PDU. 284c87c5fbaSopenharmony_ci 285c87c5fbaSopenharmony_ciIf a token is not added, then the token in the PDU is zero length, but still a 286c87c5fbaSopenharmony_civalid token which is used for matching. The exception is an empty ACK packet. 287c87c5fbaSopenharmony_ci 288c87c5fbaSopenharmony_ci*NOTE:* The token provided by the client application may not be the same as 289c87c5fbaSopenharmony_ciused internally by libcoap - for example when doing data transmission where 290c87c5fbaSopenharmony_cithe body of data is spread over multiple payloads (see *coap_block*(3)). 291c87c5fbaSopenharmony_ciHowever, when the data transfers complete, the application will receive the 292c87c5fbaSopenharmony_cicorrected token in the response PDU. 293c87c5fbaSopenharmony_ci 294c87c5fbaSopenharmony_ciPDU OPTIONS FUNCTIONS 295c87c5fbaSopenharmony_ci--------------------- 296c87c5fbaSopenharmony_ci 297c87c5fbaSopenharmony_ciThe following is the current list of options with their numeric value 298c87c5fbaSopenharmony_ci---- 299c87c5fbaSopenharmony_ci/* 300c87c5fbaSopenharmony_ci * The C, U, and N flags indicate the properties 301c87c5fbaSopenharmony_ci * Critical, Unsafe, and NoCacheKey, respectively. 302c87c5fbaSopenharmony_ci * If U is set, then N has no meaning as per 303c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc7252#section-5.10 304c87c5fbaSopenharmony_ci * and is set to a -. 305c87c5fbaSopenharmony_ci * Separately, R is for the options that can be repeated 306c87c5fbaSopenharmony_ci * 307c87c5fbaSopenharmony_ci * The least significant byte of the option is set as followed 308c87c5fbaSopenharmony_ci * as per https://rfc-editor.org/rfc/rfc7252#section-5.4.6 309c87c5fbaSopenharmony_ci * 310c87c5fbaSopenharmony_ci * 0 1 2 3 4 5 6 7 311c87c5fbaSopenharmony_ci * --+---+---+---+---+---+---+---+ 312c87c5fbaSopenharmony_ci * | NoCacheKey| U | C | 313c87c5fbaSopenharmony_ci * --+---+---+---+---+---+---+---+ 314c87c5fbaSopenharmony_ci * 315c87c5fbaSopenharmony_ci * https://rfc-editor.org/rfc/rfc8613#section-4 goes on to define E, I and U 316c87c5fbaSopenharmony_ci * properties Encrypted and Integrity Protected, Integrity Protected Only and 317c87c5fbaSopenharmony_ci * Unprotected respectively. Integrity Protected Only is not currently used. 318c87c5fbaSopenharmony_ci * 319c87c5fbaSopenharmony_ci * An Option is tagged with CUNREIU with any of the letters replaced with _ if 320c87c5fbaSopenharmony_ci * not set, or - for N if U is set (see above) for aiding understanding of the 321c87c5fbaSopenharmony_ci * Option. 322c87c5fbaSopenharmony_ci */ 323c87c5fbaSopenharmony_ci 324c87c5fbaSopenharmony_ciCOAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */ 325c87c5fbaSopenharmony_ciCOAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */ 326c87c5fbaSopenharmony_ciCOAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */ 327c87c5fbaSopenharmony_ciCOAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */ 328c87c5fbaSopenharmony_ciCOAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint, 0 B/0-3 B, RFC7641 */ 329c87c5fbaSopenharmony_ciCOAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */ 330c87c5fbaSopenharmony_ciCOAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */ 331c87c5fbaSopenharmony_ciCOAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */ 332c87c5fbaSopenharmony_ciCOAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */ 333c87c5fbaSopenharmony_ciCOAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */ 334c87c5fbaSopenharmony_ci/* COAP_OPTION_MAXAGE default 60 seconds if not set */ 335c87c5fbaSopenharmony_ciCOAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */ 336c87c5fbaSopenharmony_ciCOAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */ 337c87c5fbaSopenharmony_ciCOAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */ 338c87c5fbaSopenharmony_ciCOAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */ 339c87c5fbaSopenharmony_ciCOAP_OPTION_Q_BLOCK1 19 /* CU__E_U, uint, 0-3 B, RFC8177 */ 340c87c5fbaSopenharmony_ciCOAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */ 341c87c5fbaSopenharmony_ciCOAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */ 342c87c5fbaSopenharmony_ciCOAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */ 343c87c5fbaSopenharmony_ciCOAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */ 344c87c5fbaSopenharmony_ciCOAP_OPTION_Q_BLOCK2 31 /* CU_RE_U, uint, 0-3 B, RFC9177 */ 345c87c5fbaSopenharmony_ciCOAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */ 346c87c5fbaSopenharmony_ciCOAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */ 347c87c5fbaSopenharmony_ciCOAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */ 348c87c5fbaSopenharmony_ciCOAP_OPTION_ECHO 252 /* _N__E_U, opaque, 0-40 B, RFC9175 */ 349c87c5fbaSopenharmony_ciCOAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */ 350c87c5fbaSopenharmony_ciCOAP_OPTION_RTAG 292 /* ___RE_U, opaque, 0-8 B, RFC9175 */ 351c87c5fbaSopenharmony_ci---- 352c87c5fbaSopenharmony_ciSee FURTHER INFORMATION as to how to get the latest list. 353c87c5fbaSopenharmony_ci 354c87c5fbaSopenharmony_ci*Function: coap_new_optlist()* 355c87c5fbaSopenharmony_ci 356c87c5fbaSopenharmony_ciThe *coap_new_optlist*() function returns a newly created _optlist_ entry of 357c87c5fbaSopenharmony_citype _coap_optlist_t_*. The _number_ specifies which CoAP option is to be 358c87c5fbaSopenharmony_ciused, and is one of the COAP_OPTION_* definitions. The _length_ is the length 359c87c5fbaSopenharmony_ciof the data of the option, and _data_ points to the content of the option. 360c87c5fbaSopenharmony_ci 361c87c5fbaSopenharmony_ci*NOTE:* Where possible, the option data needs to be stripped of leading zeros 362c87c5fbaSopenharmony_ci(big endian) to reduce the amount of data needed in the PDU, as well as in 363c87c5fbaSopenharmony_cisome cases the maximum data size of an option can be exceeded if not stripped 364c87c5fbaSopenharmony_ciand hence be illegal. This is done by using *coap_encode_var_safe*() or 365c87c5fbaSopenharmony_ci*coap_encode_var_safe8*(). 366c87c5fbaSopenharmony_ci 367c87c5fbaSopenharmony_ci*Function: coap_insert_optlist()* 368c87c5fbaSopenharmony_ci 369c87c5fbaSopenharmony_ciThe *coap_insert_optlist*() function adds the _optlist_ entry onto the 370c87c5fbaSopenharmony_ciend of the _optlist_chain_. 371c87c5fbaSopenharmony_ciThe initial _optlist_chain_ entry needs to be set to NULL before this function 372c87c5fbaSopenharmony_ciis first called. The *coap_delete_optlist*() function has to be called to free 373c87c5fbaSopenharmony_cioff all the _optlist_chain_ entries. 374c87c5fbaSopenharmony_ci 375c87c5fbaSopenharmony_ci*Function: coap_delete_optlist()* 376c87c5fbaSopenharmony_ci 377c87c5fbaSopenharmony_ciThe *coap_delete_optlist*() function deletes and frees off all the optlist 378c87c5fbaSopenharmony_cientries in the _optlist_chain_. 379c87c5fbaSopenharmony_ci 380c87c5fbaSopenharmony_ci*Function: coap_add_optlist_pdu()* 381c87c5fbaSopenharmony_ci 382c87c5fbaSopenharmony_ciThe *coap_add_optlist_pdu*() function sorts all of the entries in 383c87c5fbaSopenharmony_ci_optlist_chain_ into ascending option numeric order and adds all the entries 384c87c5fbaSopenharmony_cito the _pdu_. This function does not free off the entries in _optlist_chain_. 385c87c5fbaSopenharmony_ciThis function must be called after adding any token and before adding in the 386c87c5fbaSopenharmony_cipayload data. 387c87c5fbaSopenharmony_ci 388c87c5fbaSopenharmony_ci*Function: coap_add_option()* 389c87c5fbaSopenharmony_ci 390c87c5fbaSopenharmony_ciThe *coap_add_option*() function adds in the specified option of type _number_ 391c87c5fbaSopenharmony_ciwith _data_ of length _length_ to the PDU _pdu_. 392c87c5fbaSopenharmony_ciIt is important that options are added to the _pdu_ with _number_ either 393c87c5fbaSopenharmony_cibeing the same or greater than the previous option _number_ that was added. 394c87c5fbaSopenharmony_ci 395c87c5fbaSopenharmony_ci*NOTE:* Where possible, the option data needs to be stripped of leading zeros 396c87c5fbaSopenharmony_ci(big endian) to reduce the amount of data needed in the PDU, as well as in 397c87c5fbaSopenharmony_cisome cases the maximum data size of an option can be exceeded if not stripped 398c87c5fbaSopenharmony_ciand hence be illegal. This is done by using *coap_encode_var_safe*() or 399c87c5fbaSopenharmony_ci*coap_encode_var_safe8*(). 400c87c5fbaSopenharmony_ci 401c87c5fbaSopenharmony_ci*Function: coap_encode_var_safe()* 402c87c5fbaSopenharmony_ci 403c87c5fbaSopenharmony_ciThe *coap_encode_var_safe*() function encodes _value_ into _buffer_ which has 404c87c5fbaSopenharmony_cia size of _size_ in bytes. Normally, the _buffer_ size should be at least 405c87c5fbaSopenharmony_cithe sizeof(int) bytes unless you definitely know less space is required. 406c87c5fbaSopenharmony_ci 407c87c5fbaSopenharmony_ci*Function: coap_encode_var_safe8()* 408c87c5fbaSopenharmony_ci 409c87c5fbaSopenharmony_ciThe *coap_encode_var_safe8*() function encodes 8 byte _value_ into _buffer_ 410c87c5fbaSopenharmony_ciwhich has a size of _size_ in bytes. Normally, the _buffer_ size should be at 411c87c5fbaSopenharmony_cileast 8 bytes unless you definitely know less space is required. 412c87c5fbaSopenharmony_ci 413c87c5fbaSopenharmony_ci*Function: coap_split_path()* 414c87c5fbaSopenharmony_ci 415c87c5fbaSopenharmony_ciThe *coap_split_path*() function splits up _path_ of length _length_ and 416c87c5fbaSopenharmony_ciplaces the result in _buffer_ which has a size of _buflen_ with the nul 417c87c5fbaSopenharmony_cicharacter separating each path component. _buflen_ needs 418c87c5fbaSopenharmony_cito be preset with the size of _buffer_ before the function call, and then 419c87c5fbaSopenharmony_ci_buflen_ is updated with the actual size of _buffer_ used. The return 420c87c5fbaSopenharmony_civalue indicates the number of components that individual COAP_OPTION_URI_PATH 421c87c5fbaSopenharmony_cioptions need to be created for. 422c87c5fbaSopenharmony_ci 423c87c5fbaSopenharmony_ci*Function: coap_split_query()* 424c87c5fbaSopenharmony_ci 425c87c5fbaSopenharmony_ciThe *coap_split_query*() function splits up _query_ of length _length_ and 426c87c5fbaSopenharmony_ciplaces the result in _buffer_ which has a size of _buflen_ with the nul 427c87c5fbaSopenharmony_cicharacter separating each path component. _buflen_ needs 428c87c5fbaSopenharmony_cito be preset with the size of _buffer_ before the function call, and then 429c87c5fbaSopenharmony_ci_buflen_ is updated with the actual size of _buffer_ used. The return 430c87c5fbaSopenharmony_civalue indicates the number of components that individual COAP_OPTION_URI_QUERY 431c87c5fbaSopenharmony_cioptions need to be created for. 432c87c5fbaSopenharmony_ci 433c87c5fbaSopenharmony_ciPDU OPTIONS - LIBCOAP HANDLING 434c87c5fbaSopenharmony_ci------------------------------ 435c87c5fbaSopenharmony_ciMost of the options are under the control of the applications, but the 436c87c5fbaSopenharmony_cifollowing are primarily used internally by libcoap. 437c87c5fbaSopenharmony_ci 438c87c5fbaSopenharmony_ci*COAP_OPTION_BLOCK1* and *COAP_OPTION_BLOCK2* 439c87c5fbaSopenharmony_ci 440c87c5fbaSopenharmony_ciThese Block options are used when a large body needs to be split up into 441c87c5fbaSopenharmony_cimultiple payloads. Following the introduction of 442c87c5fbaSopenharmony_ci*coap_context_set_block_mode*(3), libcoap can internally handle the setting 443c87c5fbaSopenharmony_ciof these options (see *coap_block*(3)). Applications can continue to include 444c87c5fbaSopenharmony_cithese options to set hint block size values. 445c87c5fbaSopenharmony_ci 446c87c5fbaSopenharmony_ciIt is recommended that 447c87c5fbaSopenharmony_ci*coap_context_set_block_mode(context, COAP_BLOCK_USE_LIBCOAP|COAP_BLOCK_SINGLE_BODY)* 448c87c5fbaSopenharmony_ciis used to reduce the programming requirements for block handling within 449c87c5fbaSopenharmony_cithe applications. 450c87c5fbaSopenharmony_ci 451c87c5fbaSopenharmony_ci*COAP_OPTION_ECHO* 452c87c5fbaSopenharmony_ci 453c87c5fbaSopenharmony_ciThis option can be set by the server application to indicate that the state of 454c87c5fbaSopenharmony_cithe client's freshness is confirmed. The libcoap client logic will detect the 455c87c5fbaSopenharmony_ciuse of the Echo option by the server and reflect back the Echo value in the 456c87c5fbaSopenharmony_cinext request without involving the client application. The opaque 457c87c5fbaSopenharmony_cioption Echo may be seen by the client application. 458c87c5fbaSopenharmony_ci 459c87c5fbaSopenharmony_ci*COAP_OPTION_ETAG* 460c87c5fbaSopenharmony_ci 461c87c5fbaSopenharmony_ciThis option is normally set by the server libcoap logic when sending back 462c87c5fbaSopenharmony_cimultiple payloads so that the (libcoap logic) client can re-assemble the 463c87c5fbaSopenharmony_cicorrect body. 464c87c5fbaSopenharmony_ci 465c87c5fbaSopenharmony_ci*COAP_OPTION_HOP_LIMIT* 466c87c5fbaSopenharmony_ci 467c87c5fbaSopenharmony_ciWhen using proxy logic, the value of the Hop-Limit option is decremented by 468c87c5fbaSopenharmony_cione for each proxy hop. If the count decrements to zero, then a 5.08 (Hop 469c87c5fbaSopenharmony_ciLimit Reached) error code is returned to the sender. The initial count is 470c87c5fbaSopenharmony_ci16, unless the client application sets its own limit using the Hop-Limit option. 471c87c5fbaSopenharmony_ci 472c87c5fbaSopenharmony_ci*COAP_OPTION_RTAG* 473c87c5fbaSopenharmony_ci 474c87c5fbaSopenharmony_ciThis option is set by the libcoap client logic when transmitting multiple 475c87c5fbaSopenharmony_cibodies with multiple payloads so that the (libcoap logic) server can 476c87c5fbaSopenharmony_cidifferentiate and re-assemble the correct body. 477c87c5fbaSopenharmony_ci 478c87c5fbaSopenharmony_ci*COAP_OPTION_SIZE1* and *COAP_OPTION_SIZE2* 479c87c5fbaSopenharmony_ci 480c87c5fbaSopenharmony_ciThese options are added by the libcoap logic to provide a size (Size1 by 481c87c5fbaSopenharmony_cilibcoap client logic, Size2 by libcoap server logic) indication to the 482c87c5fbaSopenharmony_cirecipient of the size of the large body that is to be transferred. See 483c87c5fbaSopenharmony_ci*coap_block*(3). 484c87c5fbaSopenharmony_ci 485c87c5fbaSopenharmony_ci*COAP_OPTION_OSCORE* 486c87c5fbaSopenharmony_ci 487c87c5fbaSopenharmony_ciThis option is used during OSCORE enabled communications. It should not be 488c87c5fbaSopenharmony_ciset by any application, but is used internally. See *coap_oscore*(3) for 489c87c5fbaSopenharmony_cifurther information on how to set up OSCORE. 490c87c5fbaSopenharmony_ci 491c87c5fbaSopenharmony_ciPDU PAYLOAD FUNCTIONS 492c87c5fbaSopenharmony_ci--------------------- 493c87c5fbaSopenharmony_ci 494c87c5fbaSopenharmony_ci*Function: coap_add_data()* 495c87c5fbaSopenharmony_ci 496c87c5fbaSopenharmony_ciThe *coap_add_data*() function adds in the specified payload _data_ of length 497c87c5fbaSopenharmony_ci_length_ to the PDU _pdu_. Adding the payload data must be done after any 498c87c5fbaSopenharmony_citoken or options are added. This function must only be called once per _pdu_. 499c87c5fbaSopenharmony_ci 500c87c5fbaSopenharmony_ci*Function: coap_add_data_blocked_response()* 501c87c5fbaSopenharmony_ci 502c87c5fbaSopenharmony_ciThe *coap_add_data_blocked_response*() function adds in the appropriate part 503c87c5fbaSopenharmony_ciof the payload _data_ of length _length_ to the PDU _pdu_. It should be used 504c87c5fbaSopenharmony_cias a direct replacement for *coap_add_data*() if it is possible that the data 505c87c5fbaSopenharmony_ciwill not fit into a single pdu. It also adds in the appropriate 506c87c5fbaSopenharmony_ciCoAP options to handle Block-Wise transfer. This function is usually used for 507c87c5fbaSopenharmony_cia server's GET / FETCH response. The _request_ and _response_ are the same 508c87c5fbaSopenharmony_ciparameters for the registered GET / FETCH resource handler. 509c87c5fbaSopenharmony_ciThe _media_type_ is for the format of the _data_ and _maxage_ defines the 510c87c5fbaSopenharmony_cilifetime of the response. If set to -1, then the Max-Age option does not get 511c87c5fbaSopenharmony_ciincluded. This function must only be called once per _pdu_. 512c87c5fbaSopenharmony_ciIt is the responsibility of the client to recognize that it has only 513c87c5fbaSopenharmony_cireceived a part of the data and request the next block (with the appropriate 514c87c5fbaSopenharmony_ciBlock options) from the server. Returning the next requested block is handled 515c87c5fbaSopenharmony_ciby this function. 516c87c5fbaSopenharmony_ci 517c87c5fbaSopenharmony_ci*NOTE:* This function has been superseded by *coap_add_data_large_response*(). 518c87c5fbaSopenharmony_ciSee *coap_block*(3). 519c87c5fbaSopenharmony_ci 520c87c5fbaSopenharmony_ciPDU TRANSMIT FUNCTIONS 521c87c5fbaSopenharmony_ci---------------------- 522c87c5fbaSopenharmony_ci 523c87c5fbaSopenharmony_ci*Function: coap_send()* 524c87c5fbaSopenharmony_ci 525c87c5fbaSopenharmony_ciThe *coap_send*() function is used to initiate the transmission of the _pdu_ 526c87c5fbaSopenharmony_ciassociated with the _session_. The caller must not access or delete _pdu_ 527c87c5fbaSopenharmony_ciafter calling *coap_send*() - even if there is a return error. 528c87c5fbaSopenharmony_ci 529c87c5fbaSopenharmony_ci*NOTE:* For request handlers, returning from the request handler will cause 530c87c5fbaSopenharmony_cithe response PDU to be transmitted as appropriate and there is no need to call 531c87c5fbaSopenharmony_ci*coap_send*() to do this. 532c87c5fbaSopenharmony_ci 533c87c5fbaSopenharmony_ciRETURN VALUES 534c87c5fbaSopenharmony_ci------------- 535c87c5fbaSopenharmony_ci*coap_new_pdu*() and *coap_pdu_init*() return a newly created 536c87c5fbaSopenharmony_ci_PDU_ or NULL if there is a malloc or parameter failure. 537c87c5fbaSopenharmony_ci 538c87c5fbaSopenharmony_ci*coap_new_optlist*() returns a newly created _optlist_ or NULL 539c87c5fbaSopenharmony_ciif there is a malloc failure. 540c87c5fbaSopenharmony_ci 541c87c5fbaSopenharmony_ci*coap_add_token*(), *coap_insert_optlist*(), *coap_add_optlist_pdu*() 542c87c5fbaSopenharmony_ciand *coap_add_data*() return 0 on failure, 1 on success. 543c87c5fbaSopenharmony_ci 544c87c5fbaSopenharmony_ci*coap_encode_var_safe*() returns either the length of bytes 545c87c5fbaSopenharmony_ciencoded (which can be 0 when encoding 0) or 0 on failure. 546c87c5fbaSopenharmony_ci 547c87c5fbaSopenharmony_ci*coap_encode_var_safe8*() returns either the length of bytes 548c87c5fbaSopenharmony_ciencoded (which can be 0 when encoding 0) or 0 on failure. 549c87c5fbaSopenharmony_ci 550c87c5fbaSopenharmony_ci*coap_add_option*() returns the size of option added, or 0 on 551c87c5fbaSopenharmony_cifailure. 552c87c5fbaSopenharmony_ci 553c87c5fbaSopenharmony_ci*coap_send*() returns the CoAP message ID on success or 554c87c5fbaSopenharmony_ciCOAP_INVALID_MID on failure. 555c87c5fbaSopenharmony_ci 556c87c5fbaSopenharmony_ci*coap_split_path*() and *coap_split_query*() return the number 557c87c5fbaSopenharmony_ciof components found. 558c87c5fbaSopenharmony_ci 559c87c5fbaSopenharmony_ci*coap_new_message_id*() returns the next CoAP message ID to use. 560c87c5fbaSopenharmony_ci 561c87c5fbaSopenharmony_ciEXAMPLES 562c87c5fbaSopenharmony_ci-------- 563c87c5fbaSopenharmony_ci*Setup PDU and Transmit* 564c87c5fbaSopenharmony_ci 565c87c5fbaSopenharmony_ci[source, c] 566c87c5fbaSopenharmony_ci---- 567c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 568c87c5fbaSopenharmony_ci 569c87c5fbaSopenharmony_cistatic int 570c87c5fbaSopenharmony_cibuild_send_pdu(coap_context_t *context, coap_session_t *session, 571c87c5fbaSopenharmony_ciuint8_t msgtype, uint8_t request_code, const char *uri, const char *query, 572c87c5fbaSopenharmony_ciunsigned char *data, size_t length, int observe) { 573c87c5fbaSopenharmony_ci 574c87c5fbaSopenharmony_ci coap_pdu_t *pdu; 575c87c5fbaSopenharmony_ci uint8_t buf[1024]; 576c87c5fbaSopenharmony_ci size_t buflen; 577c87c5fbaSopenharmony_ci uint8_t *sbuf = buf; 578c87c5fbaSopenharmony_ci int res; 579c87c5fbaSopenharmony_ci coap_optlist_t *optlist_chain = NULL; 580c87c5fbaSopenharmony_ci /* Remove (void) definition if variable is used */ 581c87c5fbaSopenharmony_ci (void)context; 582c87c5fbaSopenharmony_ci 583c87c5fbaSopenharmony_ci /* Create the pdu with the appropriate options */ 584c87c5fbaSopenharmony_ci pdu = coap_pdu_init(msgtype, request_code, coap_new_message_id(session), 585c87c5fbaSopenharmony_ci coap_session_max_pdu_size(session)); 586c87c5fbaSopenharmony_ci if (!pdu) 587c87c5fbaSopenharmony_ci return 0; 588c87c5fbaSopenharmony_ci 589c87c5fbaSopenharmony_ci /* 590c87c5fbaSopenharmony_ci * Create unique token for this request for handling unsolicited / 591c87c5fbaSopenharmony_ci * delayed responses 592c87c5fbaSopenharmony_ci */ 593c87c5fbaSopenharmony_ci coap_session_new_token(session, &buflen, buf); 594c87c5fbaSopenharmony_ci if (!coap_add_token(pdu, buflen, buf)) { 595c87c5fbaSopenharmony_ci coap_log_debug("cannot add token to request\n"); 596c87c5fbaSopenharmony_ci goto error; 597c87c5fbaSopenharmony_ci } 598c87c5fbaSopenharmony_ci 599c87c5fbaSopenharmony_ci if (uri) { 600c87c5fbaSopenharmony_ci /* Add in the URI options */ 601c87c5fbaSopenharmony_ci buflen = sizeof(buf); 602c87c5fbaSopenharmony_ci res = coap_split_path((const uint8_t*)uri, strlen(uri), sbuf, &buflen); 603c87c5fbaSopenharmony_ci while (res--) { 604c87c5fbaSopenharmony_ci if (!coap_insert_optlist(&optlist_chain, 605c87c5fbaSopenharmony_ci coap_new_optlist(COAP_OPTION_URI_PATH, 606c87c5fbaSopenharmony_ci coap_opt_length(sbuf), coap_opt_value(sbuf)))) 607c87c5fbaSopenharmony_ci goto error; 608c87c5fbaSopenharmony_ci sbuf += coap_opt_size(sbuf); 609c87c5fbaSopenharmony_ci } 610c87c5fbaSopenharmony_ci } 611c87c5fbaSopenharmony_ci 612c87c5fbaSopenharmony_ci if (query) { 613c87c5fbaSopenharmony_ci /* Add in the QUERY options */ 614c87c5fbaSopenharmony_ci buflen = sizeof(buf); 615c87c5fbaSopenharmony_ci res = coap_split_query((const uint8_t*)query, strlen(query), sbuf, &buflen); 616c87c5fbaSopenharmony_ci while (res--) { 617c87c5fbaSopenharmony_ci if (!coap_insert_optlist(&optlist_chain, 618c87c5fbaSopenharmony_ci coap_new_optlist(COAP_OPTION_URI_QUERY, 619c87c5fbaSopenharmony_ci coap_opt_length(sbuf), coap_opt_value(sbuf)))) 620c87c5fbaSopenharmony_ci goto error; 621c87c5fbaSopenharmony_ci sbuf += coap_opt_size(sbuf); 622c87c5fbaSopenharmony_ci } 623c87c5fbaSopenharmony_ci } 624c87c5fbaSopenharmony_ci 625c87c5fbaSopenharmony_ci if (request_code == COAP_REQUEST_GET && observe) { 626c87c5fbaSopenharmony_ci /* Indicate that we want to observe this resource */ 627c87c5fbaSopenharmony_ci if (!coap_insert_optlist(&optlist_chain, 628c87c5fbaSopenharmony_ci coap_new_optlist(COAP_OPTION_OBSERVE, 629c87c5fbaSopenharmony_ci coap_encode_var_safe(buf, sizeof(buf), 630c87c5fbaSopenharmony_ci COAP_OBSERVE_ESTABLISH), buf) 631c87c5fbaSopenharmony_ci )) 632c87c5fbaSopenharmony_ci goto error; 633c87c5fbaSopenharmony_ci } 634c87c5fbaSopenharmony_ci 635c87c5fbaSopenharmony_ci /* ... Other code / options etc. ... */ 636c87c5fbaSopenharmony_ci 637c87c5fbaSopenharmony_ci /* Add in all the options (after internal sorting) to the pdu */ 638c87c5fbaSopenharmony_ci if (!coap_add_optlist_pdu(pdu, &optlist_chain)) 639c87c5fbaSopenharmony_ci goto error; 640c87c5fbaSopenharmony_ci 641c87c5fbaSopenharmony_ci if (data && length) { 642c87c5fbaSopenharmony_ci /* Add in the specified data */ 643c87c5fbaSopenharmony_ci if (!coap_add_data(pdu, length, data)) 644c87c5fbaSopenharmony_ci goto error; 645c87c5fbaSopenharmony_ci } 646c87c5fbaSopenharmony_ci 647c87c5fbaSopenharmony_ci if (coap_send(session, pdu) == COAP_INVALID_MID) 648c87c5fbaSopenharmony_ci goto error; 649c87c5fbaSopenharmony_ci return 1; 650c87c5fbaSopenharmony_ci 651c87c5fbaSopenharmony_cierror: 652c87c5fbaSopenharmony_ci 653c87c5fbaSopenharmony_ci if (pdu) 654c87c5fbaSopenharmony_ci coap_delete_pdu(pdu); 655c87c5fbaSopenharmony_ci return 0; 656c87c5fbaSopenharmony_ci 657c87c5fbaSopenharmony_ci} 658c87c5fbaSopenharmony_ci---- 659c87c5fbaSopenharmony_ci 660c87c5fbaSopenharmony_ci*Resource Request Handler Response PDU Update* 661c87c5fbaSopenharmony_ci 662c87c5fbaSopenharmony_ci[source, c] 663c87c5fbaSopenharmony_ci---- 664c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 665c87c5fbaSopenharmony_ci 666c87c5fbaSopenharmony_ci#include <stdio.h> 667c87c5fbaSopenharmony_ci 668c87c5fbaSopenharmony_cistatic void 669c87c5fbaSopenharmony_cihnd_get_time(coap_resource_t *resource, coap_session_t *session, 670c87c5fbaSopenharmony_ciconst coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) { 671c87c5fbaSopenharmony_ci 672c87c5fbaSopenharmony_ci unsigned char buf[40]; 673c87c5fbaSopenharmony_ci size_t len; 674c87c5fbaSopenharmony_ci time_t now; 675c87c5fbaSopenharmony_ci 676c87c5fbaSopenharmony_ci /* ... Additional analysis code for resource, request pdu etc. ... */ 677c87c5fbaSopenharmony_ci 678c87c5fbaSopenharmony_ci /* After analysis, generate a suitable response */ 679c87c5fbaSopenharmony_ci 680c87c5fbaSopenharmony_ci now = time(NULL); 681c87c5fbaSopenharmony_ci 682c87c5fbaSopenharmony_ci if (query != NULL && coap_string_equal(query, coap_make_str_const("secs"))) { 683c87c5fbaSopenharmony_ci /* Output secs since Jan 1 1970 */ 684c87c5fbaSopenharmony_ci len = snprintf((char *)buf, sizeof(buf), "%lu", now); 685c87c5fbaSopenharmony_ci } 686c87c5fbaSopenharmony_ci else { 687c87c5fbaSopenharmony_ci /* Output human-readable time */ 688c87c5fbaSopenharmony_ci struct tm *tmp; 689c87c5fbaSopenharmony_ci tmp = gmtime(&now); 690c87c5fbaSopenharmony_ci if (!tmp) { 691c87c5fbaSopenharmony_ci /* If 'now' is not valid */ 692c87c5fbaSopenharmony_ci coap_pdu_set_code(response, COAP_RESPONSE_CODE_NOT_FOUND); 693c87c5fbaSopenharmony_ci return; 694c87c5fbaSopenharmony_ci } 695c87c5fbaSopenharmony_ci len = strftime((char *)buf, sizeof(buf), "%b %d %H:%M:%S", tmp); 696c87c5fbaSopenharmony_ci } 697c87c5fbaSopenharmony_ci coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); 698c87c5fbaSopenharmony_ci /* 699c87c5fbaSopenharmony_ci * Invoke coap_add_data_large_response() to do all the hard work. 700c87c5fbaSopenharmony_ci * 701c87c5fbaSopenharmony_ci * Define the format - COAP_MEDIATYPE_TEXT_PLAIN - to add in 702c87c5fbaSopenharmony_ci * Define how long this response is valid for (secs) - 1 - to add in. 703c87c5fbaSopenharmony_ci * Etag Option added internally with unique value as param set to 0 704c87c5fbaSopenharmony_ci * 705c87c5fbaSopenharmony_ci * Observe Option added internally if needed within the function 706c87c5fbaSopenharmony_ci * Block2 Option added internally if output too large 707c87c5fbaSopenharmony_ci * Size2 Option added internally 708c87c5fbaSopenharmony_ci */ 709c87c5fbaSopenharmony_ci coap_add_data_large_response(resource, session, request, response, 710c87c5fbaSopenharmony_ci query, COAP_MEDIATYPE_TEXT_PLAIN, 1, 0, 711c87c5fbaSopenharmony_ci len, 712c87c5fbaSopenharmony_ci buf, NULL, NULL); 713c87c5fbaSopenharmony_ci 714c87c5fbaSopenharmony_ci /* 715c87c5fbaSopenharmony_ci * Returning from the request handler will cause the response to be 716c87c5fbaSopenharmony_ci * sent off (assuming coap_pdu_set_code() has been called), unless 717c87c5fbaSopenharmony_ci * the response is confirmable and the code is COAP_EMPTY_CODE which 718c87c5fbaSopenharmony_ci * will cause an empty ACK packet to be returned). 719c87c5fbaSopenharmony_ci */ 720c87c5fbaSopenharmony_ci 721c87c5fbaSopenharmony_ci} 722c87c5fbaSopenharmony_ci---- 723c87c5fbaSopenharmony_ci 724c87c5fbaSopenharmony_ciSEE ALSO 725c87c5fbaSopenharmony_ci-------- 726c87c5fbaSopenharmony_ci*coap_block*(3), *coap_observe*(3), *coap_oscore*(3), *coap_pdu_access*(3) 727c87c5fbaSopenharmony_ciand *coap_resource*(3) 728c87c5fbaSopenharmony_ci 729c87c5fbaSopenharmony_ciFURTHER INFORMATION 730c87c5fbaSopenharmony_ci------------------- 731c87c5fbaSopenharmony_ciSee 732c87c5fbaSopenharmony_ci 733c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 734c87c5fbaSopenharmony_ci 735c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7959[RFC7959: Block-Wise Transfers in the Constrained Application Protocol (CoAP)]" 736c87c5fbaSopenharmony_ci 737c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc8613[RFC8613: Object Security for Constrained RESTful Environments (OSCORE)]" 738c87c5fbaSopenharmony_ci 739c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc8974[RFC8974: Extended Tokens and Stateless Clients in the Constrained Application Protocol (CoAP)]" 740c87c5fbaSopenharmony_ci 741c87c5fbaSopenharmony_cifor further information. 742c87c5fbaSopenharmony_ci 743c87c5fbaSopenharmony_ciSee https://www.iana.org/assignments/core-parameters/core-parameters.xhtml#option-numbers 744c87c5fbaSopenharmony_cifor the current set of defined CoAP Options. 745c87c5fbaSopenharmony_ci 746c87c5fbaSopenharmony_ciBUGS 747c87c5fbaSopenharmony_ci---- 748c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 749c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 750c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 751c87c5fbaSopenharmony_ci 752c87c5fbaSopenharmony_ciAUTHORS 753c87c5fbaSopenharmony_ci------- 754c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 755