---
name: "coap_async"
section: "3"
platform: "linux"
package: "libcoap3t64"
version: "4.3.4-1.1+deb13u3"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/coap_async"
---

# coap\_async(3)

Work with CoAP async support

## NAME

coap\_async, coap\_async\_is\_supported, coap\_register\_async, coap\_async\_trigger, coap\_async\_set\_delay, coap\_find\_async, coap\_free\_async, coap\_async\_set\_app\_data, coap\_async\_get\_app\_data - Work with CoAP async support

## SYNOPSIS

**#include \<coap3/coap.h\>**

**int coap\_async\_is\_supported(void);**

**coap\_async\_t \*coap\_register\_async(coap\_session\_t \****session***, const coap\_pdu\_t \****request***, coap\_tick\_t** *delay***);**

**void coap\_async\_trigger(coap\_async\_t \****async***);**

**void coap\_async\_set\_delay(coap\_async\_t \****async***, coap\_tick\_t** *delay***);**

**void coap\_free\_async(coap\_session\_t \****session***, coap\_async\_t \****async***);**

**coap\_async\_t \*coap\_find\_async(coap\_session\_t \****session***, coap\_bin\_const\_t** *token***);**

**void coap\_async\_set\_app\_data(coap\_async\_t \****async***, void \****app\_data***);**

**void \*coap\_async\_get\_app\_data(const coap\_async\_t \****async***);**

For specific (D)TLS library support, link with **-lcoap-3-notls**, **-lcoap-3-gnutls**, **-lcoap-3-openssl**, **-lcoap-3-mbedtls** or **-lcoap-3-tinydtls**. Otherwise, link with **-lcoap-3** to get the default (D)TLS library support.

## DESCRIPTION

CoAP server responses can be piggybacked ("RFC7252 5.2.1. Piggybacked") or separate ("RFC7252 5.2.2. Separate").

For piggybacked responses, the response packet contains both the status and any data.

For separate responses, there is an initial empty ACK response (Confirmable only - to stop the client re-transmitting the request) followed at a later time by a packet containing the status and any data.

Usually responses are piggybacked, but this man page focuses on a mechanism for providing separate (async) support.

**NOTE:** If a server is providing Proxy support, then the server code should return from the request handler with no response code set (i.e. respond with empty ACK) and then send back the response as provided by the upstream server when received, so no need to use the async support.

## FUNCTIONS

**Function: coap\_async\_is\_supported()**

The **coap\_async\_is\_supported**() function is used to determine if there is async support or not compiled into libcoap.

**Function: coap\_register\_async()**

The **coap\_register\_async**() function is used to set up an asynchronous delayed request for the *request* PDU associated with the *session*. The application request handler will get called with a copy of *request* after *delay* ticks which will then cause a response to be sent. If *delay* is 0, then the application request handler will not get called until **coap\_async\_trigger**() or **coap\_async\_set\_delay**() is called.

**Function: coap\_async\_trigger()**

The **coap\_async\_trigger**() function is used to expire the delay for the *async* definition, so the application request handler is almost immediately called.

**Function: coap\_async\_set\_delay()**

The **coap\_async\_set\_delay**() function is used to update the remaining *delay* before the application request handler is called for the *async* definition. If *delay* is set to 0, then the application request handler will not get called.

An example of usage here is **coap\_register\_async**() sets *delay* to 0, and then when the response is ready at an indeterminate point in the future, **coap\_async\_set\_delay**() is called setting *delay* to 1. Alternatively, **coap\_async\_trigger**() can be called.

**Function: coap\_free\_async()**

The **coap\_free\_async**() function is used to delete an *async* definition.

**Function: coap\_find\_async()**

The **coap\_find\_async**() function is used to determine if there is an async definition based on the *session* and token *token*.

**Function: coap\_async\_set\_app\_data()**

The **coap\_async\_set\_app\_data**() function is used to add in user defined *app\_data* to the *async* definition. It is the responsibility of the application to release this data if appropriate. This would usually be done when the application request handler is called under *async* control.

**Function: coap\_async\_get\_app\_data()**

The **coap\_async\_get\_app\_data**() function is used to retrieve any defined application data from the *async* definition.

## RETURN VALUES

**coap\_async\_is\_supported**() returns 1 if support is available, 0 otherwise.

**coap\_register\_async**() and **coap\_find\_async**() return a pointer to an async definition or NULL if there is an error.

**coap\_async\_get\_app\_data**() returns a pointer to the user defined data.

## EXAMPLES

**CoAP Server Non-Encrypted Setup**

```
#include <coap3/coap.h>
/*
 * This example is used to demonstrate how to set up and use a "separate"
 * response (empty ACK followed by data response at a later stage).
 */
static void
hnd_get_with_delay(coap_session_t *session,
              coap_resource_t *resource,
              coap_pdu_t *request,
              coap_string_t *query,
              coap_pdu_t *response) {
  unsigned long delay = 5;
  size_t size;
  coap_async_t *async;
  coap_bin_const_t token = coap_pdu_get_token(request);
  /*
   * See if this is the initial, or delayed request
   */
  async = coap_find_async(session, token);
  if (!async) {
    /* Set up an async request to trigger delay in the future */
    if (query) {
      const uint8_t *p = query->s;
      delay = 0;
      for (size = query->length; size; --size, ++p)
        delay = delay * 10 + (*p - '0');
      if (delay == 0) {
        coap_log_info("async: delay of 0 not supported\n");
        coap_pdu_set_code(response, COAP_RESPONSE_CODE_BAD_REQUEST);
        return;
      }
    }
    async = coap_register_async(session,
                                request,
                                COAP_TICKS_PER_SECOND * delay);
    if (async == NULL) {
      coap_pdu_set_code(response, COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE);
      return;
    }
    /*
     * Not setting response code will cause empty ACK to be sent
     * if Confirmable
     */
    return;
  }
  /* async is set up, so this is the delayed request */
  /* remove any stored app data associated with 'async' here */
  /* Send back the appropriate data */
  coap_add_data_large_response(resource, session, request, response,
                               query, COAP_MEDIATYPE_TEXT_PLAIN, -1, 0, 4,
                               (const uint8_t *)"done", NULL, NULL);
  coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT);
  /* async is automatically removed by libcoap on return from this handler */
}
```

## SEE ALSO

[**coap\_handler**(3)](/coap_handler/3.md)

## FURTHER INFORMATION

See

"RFC7252: The Constrained Application Protocol (CoAP)"

for further information.

## BUGS

Please report bugs on the mailing list for libcoap: libcoap-developers@lists.sourceforge.net or raise an issue on GitHub at https://github.com/obgm/libcoap/issues

## AUTHORS

The libcoap project \<libcoap-developers@lists.sourceforge.net\>
