@@ -106,6 +106,52 @@ RFC7959 Figure 3: Block-Wise GET with Early Negotiation).
106
106
107
107
ret = coap_client_req(&client, sock, &address, &req, -1);
108
108
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
+
109
155
API Reference
110
156
*************
111
157
0 commit comments