net: coap_client: allow application to add block2 option to requests

Allow an application to add a Block2 option to an initial request for a
resource. For any subsequent requests as part of a blockwise transfer,
drop the application-added Block2 option since the coap_client must
append a Block2 option with updated NUM and SZX fields based on the
server response.

Signed-off-by: Matt Rodgers <mrodgers@witekio.com>
This commit is contained in:
Matt Rodgers 2024-07-29 11:55:27 +01:00 committed by Fabio Baltieri
commit 47fbb8512f
3 changed files with 60 additions and 0 deletions

View file

@ -79,6 +79,34 @@ The following is an example of a very simple response handling function:
}
}
CoAP options may also be added to the request by the application. The following is an example of
the application adding a Block2 option to the initial request, to suggest a maximum block size to
the server for a resource that it expects to be large enough to require a blockwise transfer (see
RFC7959 Figure 3: Block-Wise GET with Early Negotiation).
.. code-block:: c
static struct coap_client;
struct coap_client_request req = { 0 };
/* static, since options must remain valid throughout the whole execution of the request */
static struct coap_client_option block2_option;
coap_client_init(&client, NULL);
block2_option = coap_client_option_initial_block2();
req.method = COAP_METHOD_GET;
req.confirmable = true;
req.path = "test";
req.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN;
req.cb = response_cb;
req.options = &block2_option;
req.num_options = 1;
req.payload = NULL;
req.len = 0;
ret = coap_client_req(&client, sock, &address, &req, -1);
API Reference
*************