1c87c5fbaSopenharmony_ci// -*- mode:doc; -*- 2c87c5fbaSopenharmony_ci// vim: set syntax=asciidoc tw=0 3c87c5fbaSopenharmony_ci 4c87c5fbaSopenharmony_cicoap_async(3) 5c87c5fbaSopenharmony_ci============= 6c87c5fbaSopenharmony_ci:doctype: manpage 7c87c5fbaSopenharmony_ci:man source: coap_async 8c87c5fbaSopenharmony_ci:man version: @PACKAGE_VERSION@ 9c87c5fbaSopenharmony_ci:man manual: libcoap Manual 10c87c5fbaSopenharmony_ci 11c87c5fbaSopenharmony_ciNAME 12c87c5fbaSopenharmony_ci---- 13c87c5fbaSopenharmony_cicoap_async, 14c87c5fbaSopenharmony_cicoap_async_is_supported, 15c87c5fbaSopenharmony_cicoap_register_async, 16c87c5fbaSopenharmony_cicoap_async_trigger, 17c87c5fbaSopenharmony_cicoap_async_set_delay, 18c87c5fbaSopenharmony_cicoap_find_async, 19c87c5fbaSopenharmony_cicoap_free_async, 20c87c5fbaSopenharmony_cicoap_async_set_app_data, 21c87c5fbaSopenharmony_cicoap_async_get_app_data 22c87c5fbaSopenharmony_ci- Work with CoAP async support 23c87c5fbaSopenharmony_ci 24c87c5fbaSopenharmony_ciSYNOPSIS 25c87c5fbaSopenharmony_ci-------- 26c87c5fbaSopenharmony_ci*#include <coap@LIBCOAP_API_VERSION@/coap.h>* 27c87c5fbaSopenharmony_ci 28c87c5fbaSopenharmony_ci*int coap_async_is_supported(void);* 29c87c5fbaSopenharmony_ci 30c87c5fbaSopenharmony_ci*coap_async_t *coap_register_async(coap_session_t *_session_, 31c87c5fbaSopenharmony_ciconst coap_pdu_t *_request_, coap_tick_t _delay_);* 32c87c5fbaSopenharmony_ci 33c87c5fbaSopenharmony_ci*void coap_async_trigger(coap_async_t *_async_);* 34c87c5fbaSopenharmony_ci 35c87c5fbaSopenharmony_ci*void coap_async_set_delay(coap_async_t *_async_, coap_tick_t _delay_);* 36c87c5fbaSopenharmony_ci 37c87c5fbaSopenharmony_ci*void coap_free_async(coap_session_t *_session_, coap_async_t *_async_);* 38c87c5fbaSopenharmony_ci 39c87c5fbaSopenharmony_ci*coap_async_t *coap_find_async(coap_session_t *_session_, 40c87c5fbaSopenharmony_cicoap_bin_const_t _token_);* 41c87c5fbaSopenharmony_ci 42c87c5fbaSopenharmony_ci*void coap_async_set_app_data(coap_async_t *_async_, void *_app_data_);* 43c87c5fbaSopenharmony_ci 44c87c5fbaSopenharmony_ci*void *coap_async_get_app_data(const coap_async_t *_async_);* 45c87c5fbaSopenharmony_ci 46c87c5fbaSopenharmony_ciFor specific (D)TLS library support, link with 47c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*, 48c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls* 49c87c5fbaSopenharmony_cior *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with 50c87c5fbaSopenharmony_ci*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support. 51c87c5fbaSopenharmony_ci 52c87c5fbaSopenharmony_ciDESCRIPTION 53c87c5fbaSopenharmony_ci----------- 54c87c5fbaSopenharmony_ciCoAP server responses can be piggybacked 55c87c5fbaSopenharmony_ci("https://rfc-editor.org/rfc/rfc7252#section-5.2.1[RFC7252 5.2.1. Piggybacked]") 56c87c5fbaSopenharmony_cior separate 57c87c5fbaSopenharmony_ci("https://rfc-editor.org/rfc/rfc7252#section-5.2.2[RFC7252 5.2.2. Separate]"). 58c87c5fbaSopenharmony_ci 59c87c5fbaSopenharmony_ciFor piggybacked responses, the response packet contains both the status and 60c87c5fbaSopenharmony_ciany data. 61c87c5fbaSopenharmony_ci 62c87c5fbaSopenharmony_ciFor separate responses, there is an initial empty ACK response (Confirmable 63c87c5fbaSopenharmony_cionly - to stop the client re-transmitting the request) followed at a later time 64c87c5fbaSopenharmony_ciby a packet containing the status and any data. 65c87c5fbaSopenharmony_ci 66c87c5fbaSopenharmony_ciUsually responses are piggybacked, but this man page focuses on a mechanism 67c87c5fbaSopenharmony_cifor providing separate (async) support. 68c87c5fbaSopenharmony_ci 69c87c5fbaSopenharmony_ci*NOTE:* If a server is providing Proxy support, then the server code should 70c87c5fbaSopenharmony_cireturn from the request handler with no response code set (i.e. respond with 71c87c5fbaSopenharmony_ciempty ACK) and then send back the response as provided by the upstream server 72c87c5fbaSopenharmony_ciwhen received, so no need to use the async support. 73c87c5fbaSopenharmony_ci 74c87c5fbaSopenharmony_ciFUNCTIONS 75c87c5fbaSopenharmony_ci--------- 76c87c5fbaSopenharmony_ci 77c87c5fbaSopenharmony_ci*Function: coap_async_is_supported()* 78c87c5fbaSopenharmony_ci 79c87c5fbaSopenharmony_ciThe *coap_async_is_supported*() function is used to determine if there is 80c87c5fbaSopenharmony_ciasync support or not compiled into libcoap. 81c87c5fbaSopenharmony_ci 82c87c5fbaSopenharmony_ci*Function: coap_register_async()* 83c87c5fbaSopenharmony_ci 84c87c5fbaSopenharmony_ciThe *coap_register_async*() function is used to set up an asynchronous delayed 85c87c5fbaSopenharmony_cirequest for the _request_ PDU associated with the _session_. The 86c87c5fbaSopenharmony_ciapplication request handler will get called with a copy of _request_ after 87c87c5fbaSopenharmony_ci_delay_ ticks which will then cause a response to be sent. If _delay_ is 0, 88c87c5fbaSopenharmony_cithen the application request handler will not get called until 89c87c5fbaSopenharmony_ci*coap_async_trigger*() or *coap_async_set_delay*() is called. 90c87c5fbaSopenharmony_ci 91c87c5fbaSopenharmony_ci*Function: coap_async_trigger()* 92c87c5fbaSopenharmony_ci 93c87c5fbaSopenharmony_ciThe *coap_async_trigger*() function is used to expire the delay for the 94c87c5fbaSopenharmony_ci_async_ definition, so the application request handler is almost 95c87c5fbaSopenharmony_ciimmediately called. 96c87c5fbaSopenharmony_ci 97c87c5fbaSopenharmony_ci*Function: coap_async_set_delay()* 98c87c5fbaSopenharmony_ci 99c87c5fbaSopenharmony_ciThe *coap_async_set_delay*() function is used to update the remaining _delay_ 100c87c5fbaSopenharmony_cibefore the application request handler is called for the _async_ definition. If 101c87c5fbaSopenharmony_ci_delay_ is set to 0, then the application request handler will not get called. 102c87c5fbaSopenharmony_ci 103c87c5fbaSopenharmony_ciAn example of usage here is *coap_register_async*() sets _delay_ to 0, and 104c87c5fbaSopenharmony_cithen when the response is ready at an indeterminate point in the future, 105c87c5fbaSopenharmony_ci*coap_async_set_delay*() is called setting _delay_ to 1. Alternatively, 106c87c5fbaSopenharmony_ci*coap_async_trigger*() can be called. 107c87c5fbaSopenharmony_ci 108c87c5fbaSopenharmony_ci*Function: coap_free_async()* 109c87c5fbaSopenharmony_ci 110c87c5fbaSopenharmony_ciThe *coap_free_async*() function is used to delete an _async_ definition. 111c87c5fbaSopenharmony_ci 112c87c5fbaSopenharmony_ci*Function: coap_find_async()* 113c87c5fbaSopenharmony_ci 114c87c5fbaSopenharmony_ciThe *coap_find_async*() function is used to determine if there is an async 115c87c5fbaSopenharmony_cidefinition based on the _session_ and token _token_. 116c87c5fbaSopenharmony_ci 117c87c5fbaSopenharmony_ci*Function: coap_async_set_app_data()* 118c87c5fbaSopenharmony_ci 119c87c5fbaSopenharmony_ciThe *coap_async_set_app_data*() function is used to add in user defined 120c87c5fbaSopenharmony_ci_app_data_ to the _async_ definition. It is the responsibility of the 121c87c5fbaSopenharmony_ciapplication to release this data if appropriate. This would usually be done 122c87c5fbaSopenharmony_ciwhen the application request handler is called under 'async' control. 123c87c5fbaSopenharmony_ci 124c87c5fbaSopenharmony_ci*Function: coap_async_get_app_data()* 125c87c5fbaSopenharmony_ci 126c87c5fbaSopenharmony_ciThe *coap_async_get_app_data*() function is used to retrieve any defined 127c87c5fbaSopenharmony_ciapplication data from the _async_ definition. 128c87c5fbaSopenharmony_ci 129c87c5fbaSopenharmony_ciRETURN VALUES 130c87c5fbaSopenharmony_ci------------- 131c87c5fbaSopenharmony_ci 132c87c5fbaSopenharmony_ci*coap_async_is_supported*() returns 1 if support is available, 0 otherwise. 133c87c5fbaSopenharmony_ci 134c87c5fbaSopenharmony_ci*coap_register_async*() and *coap_find_async*() return a pointer to an async 135c87c5fbaSopenharmony_cidefinition or NULL if there is an error. 136c87c5fbaSopenharmony_ci 137c87c5fbaSopenharmony_ci*coap_async_get_app_data*() returns a pointer to the user defined data. 138c87c5fbaSopenharmony_ci 139c87c5fbaSopenharmony_ciEXAMPLES 140c87c5fbaSopenharmony_ci-------- 141c87c5fbaSopenharmony_ci*CoAP Server Non-Encrypted Setup* 142c87c5fbaSopenharmony_ci 143c87c5fbaSopenharmony_ci[source, c] 144c87c5fbaSopenharmony_ci---- 145c87c5fbaSopenharmony_ci#include <coap@LIBCOAP_API_VERSION@/coap.h> 146c87c5fbaSopenharmony_ci 147c87c5fbaSopenharmony_ci/* 148c87c5fbaSopenharmony_ci * This example is used to demonstrate how to set up and use a "separate" 149c87c5fbaSopenharmony_ci * response (empty ACK followed by data response at a later stage). 150c87c5fbaSopenharmony_ci */ 151c87c5fbaSopenharmony_cistatic void 152c87c5fbaSopenharmony_cihnd_get_with_delay(coap_session_t *session, 153c87c5fbaSopenharmony_ci coap_resource_t *resource, 154c87c5fbaSopenharmony_ci coap_pdu_t *request, 155c87c5fbaSopenharmony_ci coap_string_t *query, 156c87c5fbaSopenharmony_ci coap_pdu_t *response) { 157c87c5fbaSopenharmony_ci unsigned long delay = 5; 158c87c5fbaSopenharmony_ci size_t size; 159c87c5fbaSopenharmony_ci coap_async_t *async; 160c87c5fbaSopenharmony_ci coap_bin_const_t token = coap_pdu_get_token(request); 161c87c5fbaSopenharmony_ci 162c87c5fbaSopenharmony_ci /* 163c87c5fbaSopenharmony_ci * See if this is the initial, or delayed request 164c87c5fbaSopenharmony_ci */ 165c87c5fbaSopenharmony_ci 166c87c5fbaSopenharmony_ci async = coap_find_async(session, token); 167c87c5fbaSopenharmony_ci if (!async) { 168c87c5fbaSopenharmony_ci /* Set up an async request to trigger delay in the future */ 169c87c5fbaSopenharmony_ci if (query) { 170c87c5fbaSopenharmony_ci const uint8_t *p = query->s; 171c87c5fbaSopenharmony_ci 172c87c5fbaSopenharmony_ci delay = 0; 173c87c5fbaSopenharmony_ci for (size = query->length; size; --size, ++p) 174c87c5fbaSopenharmony_ci delay = delay * 10 + (*p - '0'); 175c87c5fbaSopenharmony_ci if (delay == 0) { 176c87c5fbaSopenharmony_ci coap_log_info("async: delay of 0 not supported\n"); 177c87c5fbaSopenharmony_ci coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST); 178c87c5fbaSopenharmony_ci return; 179c87c5fbaSopenharmony_ci } 180c87c5fbaSopenharmony_ci } 181c87c5fbaSopenharmony_ci async = coap_register_async(session, 182c87c5fbaSopenharmony_ci request, 183c87c5fbaSopenharmony_ci COAP_TICKS_PER_SECOND * delay); 184c87c5fbaSopenharmony_ci if (async == NULL) { 185c87c5fbaSopenharmony_ci coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE); 186c87c5fbaSopenharmony_ci return; 187c87c5fbaSopenharmony_ci } 188c87c5fbaSopenharmony_ci /* 189c87c5fbaSopenharmony_ci * Not setting response code will cause empty ACK to be sent 190c87c5fbaSopenharmony_ci * if Confirmable 191c87c5fbaSopenharmony_ci */ 192c87c5fbaSopenharmony_ci return; 193c87c5fbaSopenharmony_ci } 194c87c5fbaSopenharmony_ci /* async is set up, so this is the delayed request */ 195c87c5fbaSopenharmony_ci 196c87c5fbaSopenharmony_ci /* remove any stored app data associated with 'async' here */ 197c87c5fbaSopenharmony_ci 198c87c5fbaSopenharmony_ci /* Send back the appropriate data */ 199c87c5fbaSopenharmony_ci coap_add_data_large_response(resource, session, request, response, 200c87c5fbaSopenharmony_ci query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4, 201c87c5fbaSopenharmony_ci (const uint8_t *)"done", NULL, NULL); 202c87c5fbaSopenharmony_ci coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); 203c87c5fbaSopenharmony_ci 204c87c5fbaSopenharmony_ci /* async is automatically removed by libcoap on return from this handler */ 205c87c5fbaSopenharmony_ci} 206c87c5fbaSopenharmony_ci 207c87c5fbaSopenharmony_ci---- 208c87c5fbaSopenharmony_ci 209c87c5fbaSopenharmony_ciSEE ALSO 210c87c5fbaSopenharmony_ci-------- 211c87c5fbaSopenharmony_ci*coap_handler*(3) 212c87c5fbaSopenharmony_ci 213c87c5fbaSopenharmony_ciFURTHER INFORMATION 214c87c5fbaSopenharmony_ci------------------- 215c87c5fbaSopenharmony_ciSee 216c87c5fbaSopenharmony_ci 217c87c5fbaSopenharmony_ci"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]" 218c87c5fbaSopenharmony_ci 219c87c5fbaSopenharmony_cifor further information. 220c87c5fbaSopenharmony_ci 221c87c5fbaSopenharmony_ciBUGS 222c87c5fbaSopenharmony_ci---- 223c87c5fbaSopenharmony_ciPlease report bugs on the mailing list for libcoap: 224c87c5fbaSopenharmony_cilibcoap-developers@lists.sourceforge.net or raise an issue on GitHub at 225c87c5fbaSopenharmony_cihttps://github.com/obgm/libcoap/issues 226c87c5fbaSopenharmony_ci 227c87c5fbaSopenharmony_ciAUTHORS 228c87c5fbaSopenharmony_ci------- 229c87c5fbaSopenharmony_ciThe libcoap project <libcoap-developers@lists.sourceforge.net> 230