Skip to content

Commit be640fd

Browse files
committed
net: coap_client: Document payload callback
Document the usage of the payload callback in the CoAP client documentation page. Signed-off-by: Robert Lubos <[email protected]>
1 parent 1cc31c3 commit be640fd

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

doc/connectivity/networking/api/coap_client.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,52 @@ RFC7959 Figure 3: Block-Wise GET with Early Negotiation).
106106
107107
ret = coap_client_req(&client, sock, &address, &req, -1);
108108
109+
Optionally, the application can register a payload callback instead of providing a payload pointer
110+
for the CoAP upload. In such cases, the CoAP client library will call this callback when preparing
111+
a PUT/POST request, so that the application can provide the payload in blocks, instead of having to
112+
provide a single contiguous buffer with the entire payload. An example callback, providing the
113+
content of the Lorem Ipsum string can look like this:
114+
115+
.. code-block:: c
116+
117+
static int lorem_ipsum_cb(size_t offset, const uint8_t **payload, size_t *len,
118+
bool *last_block, void *user_data)
119+
{
120+
size_t data_left;
121+
122+
if (offset > LOREM_IPSUM_STRLEN) {
123+
return -EINVAL;
124+
}
125+
126+
*payload = LOREM_IPSUM + offset;
127+
128+
data_left = LOREM_IPSUM_STRLEN - offset;
129+
if (data_left <= *len) {
130+
*len = data_left;
131+
*last_block = true;
132+
} else {
133+
*last_block = false;
134+
}
135+
136+
return 0;
137+
}
138+
139+
The callback can then be registered for the PUT/POST request instead of a payload pointer:
140+
141+
.. code-block:: c
142+
143+
struct coap_client_request req = { 0 };
144+
145+
req.method = COAP_METHOD_PUT;
146+
req.confirmable = true;
147+
req.path = "lorem-ipsum";
148+
req.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN;
149+
req.cb = response_cb;
150+
req.payload_cb = lore_ipsum_cb,
151+
152+
ret = coap_client_req(&client, sock, &address, &req, -1);
153+
154+
109155
API Reference
110156
*************
111157

0 commit comments

Comments
 (0)