19#define min(a,b) ((a) < (b) ? (a) : (b))
22#define STATE_TOKEN_BASE(t) ((t) & 0xffffffffffffULL)
23#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> 48)
24#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << 48))
58 if (block->
szx == 7) {
103 block->
m = block_b.
m;
113 unsigned int blk_size,
size_t total) {
115 size_t avail = pdu->
max_size - token_options;
116 unsigned int start = num << (blk_size + 4);
117 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
119 assert(start <= total);
120 memset(block, 0,
sizeof(*block));
122 block->
szx = block->
aszx = blk_size;
123 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
128 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
130 block->
chunk_size = (size_t)1 << (blk_size + 4);
131 if (avail < block->chunk_size && (total - start) >= avail) {
138 "not enough space, even the smallest block does not fit\n");
141 new_blk_size =
coap_flsll((
long long)avail) - 5;
143 "decrease block size for %zu to %d\n", avail, new_blk_size);
145 block->
szx = new_blk_size;
146 block->
num <<= szx - block->
szx;
147 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
158 unsigned char buf[4];
163 start = block->
num << (block->
szx + 4);
164 if (block->
num != 0 && data_length <= start) {
174 block->
szx, data_length))
179 ((block_b.
num << 4) |
192 unsigned char buf[4];
196 start = block->
num << (block->
szx + 4);
197 if (block->
num != 0 && data_length <= start) {
205 block->
szx, data_length))
220 unsigned int block_num,
unsigned char block_szx) {
222 start = block_num << (block_szx + 4);
228 min(len - start, ((
size_t)1 << (block_szx + 4))),
235 unsigned int start = block->
num << (block->
szx + 4);
243 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
245 max_size = (size_t)1 << (block->
szx + 4);
250 min(len - start, max_size),
266 unsigned char buf[4];
268 int block2_requested = 0;
270 memset(&block2, 0,
sizeof(block2));
277 block2_requested = 1;
278 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
281 length >> (block2.
szx + 4));
290 memset(etag, 0,
sizeof(etag));
305 if (block2_requested) {
365 uint8_t block_mode) {
375 const uint8_t *b,
size_t blen) {
376 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
379#if COAP_CLIENT_SUPPORT
392 "** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
397 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
447#if COAP_SERVER_SUPPORT
465 LL_FOREACH(session->
lg_xmit, lg_xmit) {
482 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
510 int have_block_defined = 0;
513 size_t token_options;
519 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
521 release_func(session, app_ptr);
527 "** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
541#define MAX_BLK_LEN (((1 << 20) - 1) * (1 << (6 + 4)))
545 "Size of large buffer restricted to 0x%x bytes\n",
MAX_BLK_LEN);
555 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
560 LL_DELETE(session->
lg_xmit, lg_xmit);
572#if COAP_SERVER_SUPPORT
580 LL_DELETE(session->
lg_xmit, lg_xmit);
589 avail = pdu->
max_size - token_options;
594 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
600 if (block.
szx < blk_size)
601 blk_size = block.
szx;
602 have_block_defined = 1;
605 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
608 "not enough space, even the smallest block does not fit\n");
612 chunk = (size_t)1 << (blk_size + 4);
613 if (have_block_defined && block.
num != 0) {
617 if (length >= block.
num * chunk) {
619 if (chunk > length - block.
num * chunk)
620 rem = length - block.
num * chunk;
625 release_func(session, app_ptr);
627 else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
647 lg_xmit->blk_size = blk_size;
648 lg_xmit->option = option;
649 lg_xmit->last_block = 0;
650 lg_xmit->data = data;
651 lg_xmit->length = length;
652 lg_xmit->release_func = release_func;
653 lg_xmit->app_ptr = app_ptr;
660 if (!lg_xmit->b.b1.app_token)
666 lg_xmit->b.b1.count = 1;
668 lg_xmit->b.b1.count);
676 (
unsigned int)length),
689 lg_xmit->b.b2.resource = resource;
692 if (lg_xmit->b.b2.query) {
693 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
696 lg_xmit->b.b2.query = NULL;
701 sizeof(lg_xmit->b.b2.rtag));
703 lg_xmit->b.b2.rtag_set = 1;
706 lg_xmit->b.b2.rtag_set = 0;
708 lg_xmit->b.b2.etag = etag;
709 lg_xmit->b.b2.request_method = request_method;
717 lg_xmit->b.b2.maxage_expire = 0;
722 (
unsigned int)length),
736 blk_size, lg_xmit->length))
743 (block.
num << 4) | (block.
m << 3) | block.
aszx),
747 memcpy(&lg_xmit->pdu, pdu,
sizeof(lg_xmit->pdu));
749 lg_xmit->pdu.used_size + lg_xmit->pdu.max_hdr_size);
750 if (!lg_xmit->pdu.token)
753 lg_xmit->pdu.alloc_size = lg_xmit->pdu.used_size;
754 lg_xmit->pdu.token += lg_xmit->pdu.max_hdr_size;
755 memcpy(lg_xmit->pdu.token, pdu->
token, lg_xmit->pdu.used_size);
757 lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->
data - pdu->
token);
761 avail = pdu->
max_size - token_options;
766 if (avail < (ssize_t)chunk) {
770 "not enough space, even the smallest block does not fit\n");
773 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
774 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
775 lg_xmit->blk_size = blk_size;
776 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
782 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
787 if (rem > lg_xmit->length - block.
num * chunk)
788 rem = lg_xmit->length - block.
num * chunk;
793 lg_xmit->b.b1.bert_size = rem;
795 lg_xmit->last_block = -1;
798 LL_PREPEND(session->
lg_xmit,lg_xmit);
802 if (have_block_defined) {
806 (0 << 4) | (0 << 3) | blk_size), buf);
813 release_func(session, app_ptr);
820 }
else if (release_func) {
821 release_func(session, app_ptr);
826#if COAP_CLIENT_SUPPORT
840 release_func(session, app_ptr);
844 length, data, release_func, app_ptr, 0);
848#if COAP_SERVER_SUPPORT
863 unsigned char buf[4];
865 int block_requested = 0;
868 memset(&block, 0,
sizeof(block));
876 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
879 length >> (block.
szx + 4));
898 if (block_requested) {
922 query, maxage, etag, length, data,
923 release_func, app_ptr, request->
code)) {
932 release_func(session, app_ptr);
934#if COAP_ERROR_PHRASE_LENGTH > 0
958 LL_FOREACH_SAFE(session->
lg_xmit, p, q) {
962 LL_DELETE(session->
lg_xmit, p);
974 if (p->
last_sent + partial_timeout <= now) {
976 LL_DELETE(session->
lg_xmit, p);
982 if (*tim_rem > p->
last_sent + partial_timeout - now) {
983 *tim_rem = p->
last_sent + partial_timeout - now;
992#if COAP_CLIENT_SUPPORT
1007 LL_FOREACH_SAFE(session->
lg_crcv, p, q) {
1009 p->
last_used + partial_timeout <= now) {
1011 LL_DELETE(session->
lg_crcv, p);
1016 if (*tim_rem > p->
last_used + partial_timeout - now) {
1017 *tim_rem = p->
last_used + partial_timeout - now;
1030 for (i = 0; i < rec_blocks->
used; i++) {
1031 if (block_num < rec_blocks->range[i].begin)
1033 if (block_num <= rec_blocks->range[i].end)
1044 for (i = 0; i < rec_blocks->
used; i++) {
1045 if (block < rec_blocks->range[i].begin)
1047 if (block < rec_blocks->range[i].end)
1051 if (block + 1 < total_blocks)
1057#if COAP_SERVER_SUPPORT
1072 LL_FOREACH_SAFE(session->
lg_srcv, p, q) {
1075 LL_DELETE(session->
lg_srcv, p);
1080 if (*tim_rem > p->
last_used + partial_timeout - now) {
1081 *tim_rem = p->
last_used + partial_timeout - now;
1090#if COAP_CLIENT_SUPPORT
1093 uint32_t block_num, uint8_t *buf,
size_t len) {
1100 int observe_action = -1;
1108 (block_num + 1) *
sizeof(lg_crcv->
obs_token[0]));
1117 if (lg_crcv->
obs_token[block_num] == NULL)
1119 memcpy(lg_crcv->
obs_token[block_num]->
s, buf, len);
1122 if (block_num < lg_crcv->obs_token_cnt) {
1124 memcpy(buf, lg_crcv->
obs_token[block_num]->
s, len);
1136 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
1138 size_t data_len = lg_xmit ? lg_xmit->
length :
1144 if (lg_crcv == NULL)
1148 "** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
1152 lg_crcv->initial = 1;
1155 memcpy(&lg_crcv->pdu, pdu,
sizeof(lg_crcv->pdu));
1157 lg_crcv->pdu.max_size = token_options + data_len + 9;
1158 lg_crcv->pdu.used_size = token_options + data_len;
1160 token_options + data_len + lg_crcv->pdu.max_hdr_size);
1161 if (!lg_crcv->pdu.token) {
1165 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
1166 memcpy(lg_crcv->pdu.token, pdu->
token, token_options);
1167 if (lg_crcv->pdu.data) {
1168 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
1169 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->
data : pdu->
data, data_len);
1174 if (!lg_crcv->app_token) {
1181 lg_crcv->retry_counter = 1;
1182 lg_crcv->state_token = state_token;
1200 if (lg_crcv == NULL)
1217#if COAP_SERVER_SUPPORT
1221 if (lg_srcv == NULL)
1235 if (lg_xmit == NULL)
1254#if COAP_SERVER_SUPPORT
1256add_block_send(uint32_t num, uint32_t *out_blocks,
1257 uint32_t *count, uint32_t max_count) {
1260 for (i = 0; i < *count && *count < max_count; i++) {
1261 if (num == out_blocks[i])
1263 else if (num < out_blocks[i]) {
1265 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
1266 out_blocks[i] = num;
1271 if (*count < max_count) {
1272 out_blocks[i] = num;
1302 uint16_t block_opt = 0;
1303 uint32_t out_blocks[1];
1304 const char *error_phrase;
1309 uint32_t request_cnt, i;
1315 if (block.
num == 0) {
1336 goto skip_app_handler;
1344 chunk = (size_t)1 << (p->
blk_size + 4);
1348 "found Block option, block is BERT, block nr. %u, M %d\n",
1349 block.
num, block.
m);
1352 "found Block option, block size is %u, block nr. %u, M %d\n",
1353 1 << (block.
szx + 4), block.
num, block.
m);
1356 if ((p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
1361 block.
num = (uint32_t)(((p->
offset + chunk) >> (block.
szx + 4)) - 1);
1363 chunk = (size_t)1 << (p->
blk_size + 4);
1366 "new Block size is %u, block number %u completed\n",
1367 1 << (block.
szx + 4), block.
num);
1370 "ignoring request to increase Block size, "
1371 "next block is not aligned on requested block size "
1372 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
1374 (1 << (block.
szx + 4)),
1375 (p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
1391 sizeof(
"Changing blocksize during request invalid")-1,
1392 (
const uint8_t *)
"Changing blocksize during request invalid");
1396 add_block_send(num, out_blocks, &request_cnt, 1);
1399 if (request_cnt == 0) {
1407 for (i = 0; i < request_cnt; i++) {
1410 block.
num = out_blocks[i];
1413 if (i + 1 < request_cnt) {
1421 pdu->
token, &drop_options);
1423 goto internal_issue;
1439 goto internal_issue;
1449 ((out_pdu->
max_size - token_options) /1024) * 1024;
1460 goto internal_issue;
1462 if (!(p->
offset + chunk < p->length)) {
1475 if (!(p->
offset + chunk < p->length)) {
1495 goto internal_issue;
1504 goto internal_issue;
1506 if (i + 1 < request_cnt) {
1512 goto skip_app_handler;
1518 (
const uint8_t *)error_phrase);
1533 rec_blocks->
retry = 0;
1535 for (i = 0; i < rec_blocks->
used; i++) {
1536 if (block_num >= rec_blocks->
range[i].
begin &&
1537 block_num <= rec_blocks->range[i].end)
1540 if (block_num < rec_blocks->range[i].begin) {
1541 if (block_num + 1 == rec_blocks->
range[i].
begin) {
1549 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
1550 (rec_blocks->
used - i) * sizeof (rec_blocks->
range[0]));
1556 if (block_num == rec_blocks->
range[i].
end + 1) {
1557 rec_blocks->
range[i].
end = block_num;
1558 if (i + 1 < rec_blocks->
used) {
1559 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
1562 if (i+2 < rec_blocks->
used) {
1563 memmove (&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
1564 (rec_blocks->
used - (i+2)) * sizeof (rec_blocks->
range[0]));
1572 if (i == rec_blocks->
used) {
1583#if COAP_SERVER_SUPPORT
1603 const uint8_t *data = NULL;
1608 uint16_t block_option = 0;
1611 *pfree_lg_srcv = NULL;
1634 const uint8_t *rtag = rtag_opt ?
coap_opt_value(rtag_opt) : NULL;
1638 offset = block.
num << (block.
szx + 4);
1640 LL_FOREACH(session->
lg_srcv, p) {
1641 if (rtag_opt || p->
rtag_set == 1) {
1642 if (!(rtag_opt && p->
rtag_set == 1))
1645 memcmp(p->
rtag, rtag, rtag_length) != 0)
1656 if (!p && block.
num != 0) {
1661 pdu->
body_total = length + offset + (block.
m ? 1 : 0);
1664 else if (!p && !(offset == 0 && block.
m == 0)) {
1668 (
const uint8_t *)
"Memory issue");
1670 goto skip_app_handler;
1692 memcpy(p->
rtag, rtag, rtag_length);
1696 LL_PREPEND(session->
lg_srcv, p);
1700 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
1701 (
const uint8_t *)
"Content-Format mismatch");
1710 size_t chunk = (size_t)1 << (block.
szx + 4);
1711 int update_data = 0;
1712 unsigned int saved_num = block.
num;
1713 size_t saved_offset = offset;
1715 while (offset < saved_offset + length) {
1720 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
1721 (
const uint8_t *)
"Too many missing blocks");
1728 offset = block.
num << (block.
szx + 4);
1736 goto call_app_handler;
1741 (uint32_t)(p->
total_len + chunk -1)/chunk)) {
1754 goto skip_app_handler;
1756 goto skip_app_handler;
1776 goto call_app_handler;
1785 if (total > offset + length + block.
m)
1797 goto call_app_handler;
1808 goto call_app_handler;
1811 LL_DELETE(session->
lg_srcv, p);
1813 goto skip_app_handler;
1824#if COAP_CLIENT_SUPPORT
1834 if (sent || lg_xmit || lg_crcv) {
1838 const uint8_t *data;
1848 }
else if (lg_xmit) {
1849 sent = &lg_xmit->
pdu;
1851 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
1852 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
1854 data = &lg_xmit->
data[offset];
1855 data_len = (lg_xmit->
length - offset) > blk_size ? blk_size :
1856 lg_xmit->
length - offset;
1859 sent = &lg_crcv->
pdu;
1932 LL_FOREACH_SAFE(session->
lg_xmit, p, q) {
1942 size_t chunk = (size_t)1 << (p->
blk_size + 4);
1950 "found Block option, block is BERT, block nr. %u (%zu)\n",
1954 "found Block option, block size is %u, block nr. %u\n",
1955 1 << (block.
szx + 4), block.
num);
1958 if ((p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
1963 block.
num = (uint32_t)(((p->
offset + chunk) >> (block.
szx + 4)) - 1);
1965 chunk = (size_t)1 << (p->
blk_size + 4);
1968 "new Block size is %u, block number %u completed\n",
1969 1 << (block.
szx + 4), block.
num);
1974 "next block is not aligned on requested block size boundary. "
1975 "(%zu x %u mod %u = %zu != 0)\n",
1977 (1 << (block.
szx + 4)),
1978 (p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
1981 track_echo(session, rcvd);
2008 LL_FOREACH(session->
lg_crcv, lg_crcv) {
2011 track_fetch_observe(&p->
pdu, lg_crcv, block.
num + 1,
2023 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
2026 ((pdu->
max_size - token_options) /1024) * 1024;
2049 if (check_freshness(session, rcvd, sent, p, NULL))
2052 goto lg_xmit_finished;
2062 LL_FOREACH(session->
lg_crcv, lg_crcv) {
2080 LL_DELETE(session->
lg_xmit, p);
2091 const uint8_t *data,
size_t offset,
size_t total) {
2094 if (body_data == NULL && total) {
2097 if (body_data == NULL)
2101 if (offset + length <= total && body_data->length >= total) {
2102 memcpy(&body_data->
s[offset], data, length);
2119 memcpy(&body_data->
s[offset], data, length);
2129#if COAP_CLIENT_SUPPORT
2150 uint16_t block_opt = 0;
2152 int ack_rst_sent = 0;
2156 memset(&block, 0,
sizeof(block));
2157 LL_FOREACH(session->
lg_crcv, p) {
2173 const uint8_t *data;
2176 size_t size2 = size_opt ?
2188 track_echo(session, rcvd);
2189 if (have_block && (block.
m || length)) {
2193 uint16_t fmt = fmt_opt ?
2200 size_t saved_offset;
2204 chunk = (size_t)1 << (block.
szx + 4);
2205 offset = block.
num * chunk;
2206 if (size2 < (offset + length)) {
2208 size2 = offset + length + 1;
2210 size2 = offset + length;
2212 saved_offset = offset;
2250 "Data body updated during receipt - new request started\n");
2267 (0 << 4) | (0 << 3) | block.
aszx),
2273 goto skip_app_handler;
2286 if (block.
num == 0) {
2300 while (offset < saved_offset + length) {
2310 offset = block.
num << (block.
szx + 4);
2316 if (updated_block) {
2319 saved_offset, size2);
2325 (size2 + chunk -1) / chunk)) {
2349 ((block.
num + 1) << 4) |
2350 (block.
m << 3) | block.
aszx),
2357 goto skip_app_handler;
2363 goto call_app_handler;
2384 if (block.
m != 0 || block.
num != 0) {
2400 LL_DELETE(session->
lg_crcv, p);
2402 goto skip_app_handler;
2412 goto skip_app_handler;
2424 goto expire_lg_crcv;
2429 if (check_freshness(session, rcvd, sent, NULL, p))
2430 goto skip_app_handler;
2431 goto expire_lg_crcv;
2434 goto expire_lg_crcv;
2459 goto skip_app_handler;
2466 if (block.
num != 0) {
2469 const uint8_t *data;
2470 size_t chunk = (size_t)1 << (block.
szx + 4);
2475 goto call_app_handler;
2482 LL_PREPEND(session->
lg_crcv, lg_crcv);
2487 track_echo(session, rcvd);
2492 LL_PREPEND(session->
lg_crcv, lg_crcv);
2509 LL_DELETE(session->
lg_crcv, p);
2522#if COAP_SERVER_SUPPORT
2531 if (response->
code == 0)
2534 if (lg_xmit && lg_xmit->
pdu.
code == 0) {
2541#if COAP_CLIENT_SUPPORT
2550 LL_FOREACH(session->
lg_crcv, lg_crcv) {
2564 LL_FOREACH(session->
lg_xmit, lg_xmit) {
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
#define STATE_TOKEN_FULL(t, r)
static int check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks)
#define STATE_TOKEN_BASE(t)
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num)
static int setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, unsigned int num, unsigned int blk_size, size_t total)
static int coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr, coap_pdu_code_t request_method)
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
unsigned char coap_key_t[4]
#define coap_hash(String, Length, Result)
Pulls together all the internal only header files.
uint16_t coap_option_num_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
int coap_flsll(long long i)
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
int coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
int coap_handle_response_get_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive)
void coap_check_code_lg_xmit(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response()...
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
void coap_check_update_token(coap_session_t *session, coap_pdu_t *pdu)
The function checks if the token needs to be updated before PDU is presented to the application (only...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, int *added_block, coap_lg_srcv_t **free_lg_srcv)
coap_lg_xmit_t * coap_find_lg_xmit_response(const coap_session_t *session, const coap_pdu_t *request, const coap_resource_t *resource, const coap_string_t *query)
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, coap_lg_xmit_t *lg_xmit)
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
void coap_context_set_block_mode(coap_context_t *context, uint8_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, coap_block_b_t *block)
Adds the appropriate payload data of the body to the pdu.
#define COAP_BLOCK_SINGLE_BODY
int coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
int coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, unsigned int block_num, unsigned char block_szx)
Adds the block_num block of size 1 << (block_szx + 4) from source data to pdu.
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
void coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, uint16_t media_type, int maxage, size_t length, const uint8_t *data)
Adds the appropriate part of data to the response pdu.
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
#define COAP_BLOCK_NO_PREEMPTIVE_RTAG
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_LAST(opt)
Returns the value of the least significant byte of a Block option opt.
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_USE_LIBCOAP
void coap_ticks(coap_tick_t *t)
Sets t to the internal time with COAP_TICKS_PER_SECOND resolution.
time_t coap_time_t
CoAP time in seconds since epoch.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu)
Sends a CoAP message to given peer.
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
COAP_STATIC_INLINE coap_mid_t coap_send_rst(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
#define coap_log_debug(...)
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_warn(...)
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
#define COAP_PDU_IS_REQUEST(pdu)
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_BLOCK2
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_OPTION_SIZE1
coap_pdu_type_t
CoAP PDU message type definitions.
#define COAP_MEDIATYPE_TEXT_PLAIN
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
coap_pdu_t * coap_pdu_duplicate(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_OBSERVE
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
@ COAP_REQUEST_CODE_FETCH
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
#define COAP_STATIC_INLINE
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
COAP_STATIC_INLINE int token_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options with BERT support.
unsigned int num
block number
uint32_t chunk_size
1024 if BERT
unsigned int bert
Operating as BERT.
unsigned int aszx
block size (0-7 including BERT
unsigned int defined
Set if block found.
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size (0-6)
Structure of Block options.
unsigned int num
block number
unsigned int szx
block size
unsigned int m
1 if more blocks follow, 0 otherwise
The CoAP stack's global state is stored in a coap_context_t object.
uint64_t etag
Next ETag to use.
coap_response_handler_t response_handler
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * unknown_resource
can be used for handling unknown resources
uint64_t state_token
state token
size_t bert_size
size of last BERT block
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
Structure to hold large body (many blocks) client receive information.
uint16_t block_option
Block option in use.
uint8_t etag[8]
ETag for block checking.
uint8_t etag_length
ETag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t observe_length
Length of observe data.
uint8_t observe[3]
Observe data (if observe_set) (only 24 bits)
uint8_t etag_set
Set if ETag is in receive PDU.
uint8_t initial
If set, has not been used yet.
uint8_t szx
size of individual blocks
coap_binary_t ** obs_token
Tokens used in setting up Observe (to handle large FETCH)
uint16_t content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
coap_tick_t last_used
< list of received blocks
uint64_t state_token
state token
coap_binary_t * app_token
app requesting PDU token
uint16_t retry_counter
Retry counter (part of state token)
coap_binary_t * body_data
Used for re-assembling entire body.
size_t obs_token_cnt
number of tokens used to set up Observe
uint8_t observe_set
Set if this is an observe receive PDU.
size_t total_len
Length as indicated by SIZE2 option.
Structure to hold large body (many blocks) server receive information.
uint8_t rtag[8]
RTag for block checking.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t last_token[8]
< list of received blocks
uint8_t rtag_set
Set if RTag is in receive PDU.
uint16_t block_option
Block option in use.
size_t total_len
Length as indicated by SIZE1 option.
uint8_t observe_length
Length of observe data.
coap_rblock_t rec_blocks
set to uri_path if unknown resource
coap_binary_t * body_data
Used for re-assembling entire body.
coap_resource_t * resource
associated resource
size_t last_token_length
length of token
uint8_t observe_set
Set if this is an observe receive PDU.
uint8_t rtag_length
RTag length.
uint8_t last_type
Last request type (CON/NON)
uint8_t szx
size of individual blocks
size_t amount_so_far
Amount of data seen so far.
coap_tick_t last_used
Last time data sent or 0.
uint8_t observe[3]
Observe data (if set) (only 24 bits)
uint16_t content_format
Content format for the set of blocks.
coap_str_const_t * uri_path
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_release_large_data_t release_func
large data de-alloc function
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
union coap_lg_xmit_t::@1 b
const uint8_t * data
large data ptr
int last_block
last acknowledged block number
coap_tick_t last_payload
Last time MAX_PAYLOAD was sent or 0.
size_t offset
large data next offset to transmit
coap_pdu_t pdu
skeletal PDU
size_t length
large data length
uint16_t option
large block transmisson CoAP option
void * app_ptr
applicaton provided ptr for de-alloc function
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
uint8_t max_hdr_size
space reserved for protocol-specific header
uint8_t * token
first byte of token, if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
uint8_t token_length
length of Token
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Structure to keep track of received blocks.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of resource that can be attached to coap_context_t.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_proto_t proto
protocol used
uint32_t tx_rtag
Next Request-Tag number to use.
coap_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
CoAP string data definition.
size_t length
length of string