21#define min(a,b) ((a) < (b) ? (a) : (b))
25#ifndef COAP_ETAG_MAX_BYTES
26#define COAP_ETAG_MAX_BYTES 4
28#if COAP_ETAG_MAX_BYTES < 1 || COAP_ETAG_MAX_BYTES > 8
29#error COAP_ETAG_MAX_BYTES byte size invalid
32#if COAP_Q_BLOCK_SUPPORT
78 if (block->
szx == 7) {
90 if (block->
m && (length % 1024) != 0) {
92 length - (length % 1024), length);
93 length -= length % 1024;
126 block->
m = block_b.
m;
136 unsigned int blk_size,
size_t total) {
138 size_t avail = pdu->
max_size - token_options;
139 unsigned int start = num << (blk_size + 4);
140 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
142 assert(start <= total);
143 memset(block, 0,
sizeof(*block));
145 block->
szx = block->
aszx = blk_size;
146 if (can_use_bert && blk_size == 6 && avail >= 1024 && session !=
NULL &&
151 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
153 block->
chunk_size = (size_t)1 << (blk_size + 4);
154 if (avail < block->chunk_size && (total - start) >= avail) {
160 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
163 new_blk_size =
coap_flsll((
long long)avail) - 5;
166 block->
szx = new_blk_size;
167 block->
num <<= szx - block->
szx;
168 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
179 unsigned char buf[4];
184 start = block->
num << (block->
szx + 4);
185 if (block->
num != 0 && data_length <= start) {
195 block->
szx, data_length))
200 ((block_b.
num << 4) |
213 unsigned char buf[4];
217 start = block->
num << (block->
szx + 4);
218 if (block->
num != 0 && data_length <= start) {
226 block->
szx, data_length))
241 unsigned int block_num,
unsigned char block_szx) {
243 start = block_num << (block_szx + 4);
249 min(len - start, ((
size_t)1 << (block_szx + 4))),
256 unsigned int start = block->
num << (block->
szx + 4);
264 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
266 max_size = (size_t)1 << (block->
szx + 4);
271 min(len - start, max_size),
286 unsigned char buf[4];
288 int block2_requested = 0;
289#if COAP_SERVER_SUPPORT
291 coap_digest_t digest;
292 coap_digest_ctx_t *dctx =
NULL;
295 memset(&block2, 0,
sizeof(block2));
302 block2_requested = 1;
303 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
306 length >> (block2.
szx + 4));
314#if COAP_SERVER_SUPPORT
317 dctx = coap_digest_setup();
320 if (request && request->
session &&
324 if (!coap_digest_update(dctx, data, length))
326 if (!coap_digest_final(dctx, &digest))
329 memcpy(&etag, digest.key,
sizeof(etag));
330#if COAP_ETAG_MAX_BYTES != 8
353 if (block2_requested) {
406#if COAP_SERVER_SUPPORT
407 coap_digest_free(dctx);
416 uint32_t block_mode) {
424 uint32_t block_mode) {
430#if ! COAP_Q_BLOCK_SUPPORT
438 size_t max_block_size) {
449 switch (max_block_size) {
460 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
465 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
466 context->
block_mode &= ~COAP_BLOCK_MAX_SIZE_MASK;
473 const uint8_t *b,
size_t blen) {
474 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
481 uint64_t token_match =
485 LL_FOREACH(session->
lg_xmit, lg_xmit) {
491#if COAP_CLIENT_SUPPORT
522 LL_PREPEND(session->
lg_xmit, lg_xmit);
527#if COAP_CLIENT_SUPPORT
531 lg_crcv = coap_find_lg_crcv(session, pdu);
533 lg_xmit->b.b1.state_token = lg_crcv->state_token;
545#if COAP_CLIENT_SUPPORT
553 ret = coap_cancel_observe_lkd(session, token, type);
569 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
574 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
575 if (lg_crcv->observe_set) {
576 if ((!token && !lg_crcv->app_token->length) || (token &&
582#if COAP_Q_BLOCK_SUPPORT
588 lg_crcv->obs_token[0] ?
589 lg_crcv->obs_token[0] :
599 lg_crcv->observe_set = 0;
609 if (lg_crcv->o_block_option) {
612 lg_crcv->o_blk_size),
615 if (lg_crcv->obs_data) {
616 coap_add_data_large_request_lkd(session, pdu,
617 lg_crcv->obs_data->length,
618 lg_crcv->obs_data->data,
NULL,
NULL);
620 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
631#if COAP_Q_BLOCK_SUPPORT
633 if (using_q_block1) {
634 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
653 uint64_t token_match =
657 LL_FOREACH(session->lg_crcv, lg_crcv) {
675 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
677 if (m_lg_crcv->obs_data) {
678 m_lg_crcv->obs_data->ref++;
679 lg_crcv->obs_data = m_lg_crcv->obs_data;
681 LL_PREPEND(session->lg_crcv, lg_crcv);
687#if COAP_OSCORE_SUPPORT
701 lg_crcv = coap_find_lg_crcv(session, pdu);
705 ++lg_crcv->retry_counter);
733#if COAP_SERVER_SUPPORT
750 LL_FOREACH(session->
lg_xmit, lg_xmit) {
757 lg_xmit->b.b2.query ?
758 lg_xmit->b.b2.query : &empty)) {
767 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
793#if COAP_Q_BLOCK_SUPPORT
799 int have_block_defined = 0;
801 uint8_t max_blk_size;
803 size_t token_options;
806#if COAP_Q_BLOCK_SUPPORT
810#if !COAP_SERVER_SUPPORT
816 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
824 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
838#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
839#if UINT_MAX < MAX_BLK_LEN
841#define MAX_BLK_LEN UINT_MAX
849#if COAP_SERVER_SUPPORT
861 coap_digest_t digest;
862 coap_digest_ctx_t *dctx = coap_digest_setup();
868 if (coap_digest_update(dctx, data, length)) {
869 if (coap_digest_final(dctx, &digest)) {
870 memcpy(&etag, digest.key,
sizeof(etag));
871#if COAP_ETAG_MAX_BYTES != 8
877 coap_digest_free(dctx);
897 if (etag == etag_r) {
912#if COAP_Q_BLOCK_SUPPORT
928 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
932#if COAP_CLIENT_SUPPORT
935 LL_DELETE(session->
lg_xmit, lg_xmit);
946#if COAP_Q_BLOCK_SUPPORT
947 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
960#if COAP_SERVER_SUPPORT
965 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
968 LL_DELETE(session->
lg_xmit, lg_xmit);
975#if COAP_OSCORE_SUPPORT
976 if (session->oscore_encryption) {
984 avail = pdu->
max_size - token_options;
987#if COAP_OSCORE_SUPPORT
996 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1002 if (max_blk_size && blk_size > max_blk_size)
1003 blk_size = max_blk_size;
1007 if (block.
szx < blk_size)
1008 blk_size = block.
szx;
1009 have_block_defined = 1;
1011#if COAP_Q_BLOCK_SUPPORT
1014 if (have_block_defined) {
1016 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1020 if (block.
szx < blk_size)
1021 blk_size = block.
szx;
1022 have_block_defined = 1;
1023 option = alt_option;
1028 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1030 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1034 chunk = (size_t)1 << (blk_size + 4);
1035 if ((have_block_defined && block.
num != 0) || single_request ||
1040 if (length >= block.
num * chunk) {
1041#if COAP_SERVER_SUPPORT
1047 (
unsigned int)length),
1061 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1066 if (chunk > length - block.
num * chunk)
1067 rem = length - block.
num * chunk;
1074 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1099 lg_xmit->blk_size = blk_size;
1100 lg_xmit->option = option;
1102 if (!lg_xmit->data_info)
1104 lg_xmit->data_info->ref = 0;
1105 lg_xmit->data_info->data = data;
1106 lg_xmit->data_info->length = length;
1107#if COAP_Q_BLOCK_SUPPORT
1108 lg_xmit->non_timeout_random_ticks =
1111 lg_xmit->data_info->get_func = get_func;
1112 lg_xmit->data_info->release_func = release_func;
1113 lg_xmit->data_info->app_ptr = app_ptr;
1120 if (!lg_xmit->b.b1.app_token)
1128 lg_xmit->b.b1.count = 1;
1130 lg_xmit->b.b1.count);
1139 (
unsigned int)length),
1152 lg_xmit->b.b2.resource = resource;
1155 if (lg_xmit->b.b2.query) {
1156 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1159 lg_xmit->b.b2.query =
NULL;
1164 sizeof(lg_xmit->b.b2.rtag));
1166 lg_xmit->b.b2.rtag_set = 1;
1168 lg_xmit->b.b2.rtag_set = 0;
1170 lg_xmit->b.b2.etag = etag;
1171 lg_xmit->b.b2.request_method = request_method;
1178 lg_xmit->b.b2.maxage_expire = 0;
1183 (
unsigned int)length),
1188 blk_size, lg_xmit->data_info->length))
1195 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1203 avail = pdu->
max_size - token_options;
1208#if COAP_OSCORE_SUPPORT
1211 if (avail < (ssize_t)chunk) {
1214 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1217 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1218 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1219 lg_xmit->blk_size = blk_size;
1220 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1226 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1231 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1232 rem = lg_xmit->data_info->length - block.
num * chunk;
1234#if COAP_CONSTRAINED_STACK
1236 static uint8_t l_data[1024];
1238 uint8_t l_data[1024];
1242 assert(rem <= 1024);
1243 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1254 lg_xmit->b.b1.bert_size = rem;
1256 lg_xmit->last_block = -1;
1259 LL_PREPEND(session->
lg_xmit,lg_xmit);
1262 if (have_block_defined) {
1266 (0 << 4) | (0 << 3) | blk_size), buf);
1273 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1294 }
else if (release_func) {
1300#if COAP_CLIENT_SUPPORT
1305 const uint8_t *data,
1312 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1313 release_func, app_ptr);
1322 const uint8_t *data,
1336 length, data, release_func,
NULL, app_ptr, 0, 0);
1349 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1350 release_func, get_func, app_ptr);
1370 length,
NULL, release_func, get_func,
1375#if COAP_SERVER_SUPPORT
1382 uint16_t media_type,
1386 const uint8_t *data,
1392 ret = coap_add_data_large_response_lkd(resource, session, request,
1393 response, query, media_type, maxage, etag,
1394 length, data, release_func, app_ptr);
1405 uint16_t media_type,
1409 const uint8_t *data,
1413 unsigned char buf[4];
1415 int block_requested = 0;
1416 int single_request = 0;
1417#if COAP_Q_BLOCK_SUPPORT
1418 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1424 memset(&block, 0,
sizeof(block));
1431 block_requested = 1;
1432 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1435 length >> (block.
szx + 4));
1440#if COAP_Q_BLOCK_SUPPORT
1442 block_requested = 1;
1443 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1446 length >> (block.
szx + 4));
1450 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1471 if (block_requested) {
1495 query, maxage, etag, length, data,
1496 release_func,
NULL, app_ptr, single_request,
1499 goto error_released;
1509#if COAP_ERROR_PHRASE_LENGTH > 0
1527#if COAP_Q_BLOCK_SUPPORT
1537 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1541 LL_DELETE(session->
lg_xmit, lg_xmit);
1545 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1551 if (lg_xmit->
last_sent + partial_timeout <= now) {
1554#if COAP_CLIENT_SUPPORT
1558 LL_DELETE(session->
lg_xmit, lg_xmit);
1565 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1566 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1575#if COAP_CLIENT_SUPPORT
1576#if COAP_Q_BLOCK_SUPPORT
1581 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1593 pdu->
type = lg_crcv->last_type;
1605 int block_payload_set = -1;
1616 if (lg_crcv->rec_blocks.used &&
1617 (lg_crcv->rec_blocks.used < 2 ||
1620 block = lg_crcv->rec_blocks.range[0].end + 1;
1621 block_size = (size_t)1 << (lg_crcv->szx + 4);
1622 sofar = block * block_size;
1623 if (sofar < lg_crcv->total_len) {
1626 pdu = coap_build_missing_pdu(session, lg_crcv);
1632 (block << 4) | (1 << 3) | lg_crcv->szx),
1640 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1641 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1642 lg_crcv->rec_blocks.range[i].begin != 0) {
1645 pdu = coap_build_missing_pdu(session, lg_crcv);
1650 if (block_payload_set == -1)
1652 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1656 (block << 4) | (0 << 3) | lg_crcv->szx),
1660 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1661 block = lg_crcv->rec_blocks.range[i].end;
1664 block_size = (size_t)1 << (lg_crcv->szx + 4);
1665 sofar = (block + 1) * block_size;
1666 if (sofar < lg_crcv->total_len) {
1669 pdu = coap_build_missing_pdu(session, lg_crcv);
1673 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1675 if (block_payload_set == -1)
1677 for (; block < (ssize_t)sofar &&
1681 (block << 4) | (0 << 3) | lg_crcv->szx),
1688 lg_crcv->rec_blocks.retry++;
1689 if (block_payload_set != -1)
1690 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1705#if COAP_Q_BLOCK_SUPPORT
1711#if COAP_Q_BLOCK_SUPPORT
1713 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1719 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1723#if COAP_Q_BLOCK_SUPPORT
1725 size_t scaled_timeout = receive_timeout *
1726 ((size_t)1 << lg_crcv->rec_blocks.retry);
1734 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1735 coap_request_missing_q_block2(session, lg_crcv);
1737 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1738 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1746 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1747 lg_crcv->last_used + partial_timeout <= now) {
1748#if COAP_Q_BLOCK_SUPPORT
1752 LL_DELETE(session->lg_crcv, lg_crcv);
1753 coap_block_delete_lg_crcv(session, lg_crcv);
1754 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1756 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1757 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1766#if COAP_SERVER_SUPPORT
1767#if COAP_Q_BLOCK_SUPPORT
1779 if (lg_srcv->last_token)
1780 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1795 assert(block >= 0 && block < (1 << 20));
1797 if (block < 0 || block >= (1 << 20)) {
1799 }
else if (block < 24) {
1802 }
else if (block < 0x100) {
1806 }
else if (block < 0x10000) {
1809 val[1] = block >> 8;
1810 val[2] = block & 0xff;
1814 val[1] = block >> 16;
1815 val[2] = (block >> 8) & 0xff;
1816 val[3] = block & 0xff;
1832 for (i = 0; i < rec_blocks->
used; i++) {
1833 if (block_num < rec_blocks->range[i].begin)
1835 if (block_num <= rec_blocks->range[i].end)
1841#if COAP_SERVER_SUPPORT
1843check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1844 if (rec_blocks->
used == 0) {
1845 return block_num == 0 ? 1 : 0;
1847 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1864 for (i = 0; i < rec_blocks->
used; i++) {
1865 if (block < rec_blocks->range[i].begin)
1867 if (block < rec_blocks->range[i].end)
1873#if COAP_CLIENT_SUPPORT
1874#if COAP_Q_BLOCK_SUPPORT
1878 if (rec_blocks->
used &&
1880 rec_blocks->processing_payload_set)
1888 if (rec_blocks->
used > 1 &&
1890 rec_blocks->processing_payload_set)
1897#if COAP_SERVER_SUPPORT
1908#if COAP_Q_BLOCK_SUPPORT
1914#if COAP_Q_BLOCK_SUPPORT
1916 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1922 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1923 if (lg_srcv->dont_timeout) {
1930#if COAP_Q_BLOCK_SUPPORT
1932 size_t scaled_timeout = receive_timeout *
1933 ((size_t)1 << lg_srcv->rec_blocks.retry);
1939 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1942 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1943 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1945 size_t last_payload_block;
1947 size_t no_blocks = 0;
1950 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1951 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1952 lg_srcv->rec_blocks.range[i].begin != 0) {
1954 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1956 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1957 block = lg_srcv->rec_blocks.range[i].end;
1960 if (no_blocks == 0 && block == (
int)final_block)
1966 if (final_block > last_payload_block) {
1967 final_block = last_payload_block;
1969 no_blocks += final_block - block;
1970 if (no_blocks == 0) {
1972 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1974 if (final_block > last_payload_block) {
1975 final_block = last_payload_block;
1980 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1981 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1982 lg_srcv->rec_blocks.range[i].begin != 0) {
1985 pdu = pdu_408_build(session, lg_srcv);
1990 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1991 if (!add_408_block(pdu, block)) {
1996 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1997 block = lg_srcv->rec_blocks.range[i].end;
2001 for (; block <= (int)final_block; block++) {
2003 pdu = pdu_408_build(session, lg_srcv);
2007 if (!add_408_block(pdu, block)) {
2013 lg_srcv->rec_blocks.retry++;
2016 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2017 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2024 if (lg_srcv->no_more_seen)
2026 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2027#if COAP_Q_BLOCK_SUPPORT
2044 if (lg_srcv->last_token)
2045 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2047 (
const uint8_t *)
"Missing interim block");
2051 LL_DELETE(session->lg_srcv, lg_srcv);
2052 coap_block_delete_lg_srcv(session, lg_srcv);
2053 }
else if (lg_srcv->last_used) {
2055 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2056 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2065#if COAP_Q_BLOCK_SUPPORT
2074 coap_send_pdu_t send_pdu) {
2079 const uint8_t *ptoken;
2081 size_t ltoken_length;
2082 uint32_t delayqueue_cnt = 0;
2085 if (send_pdu == COAP_SEND_INC_PDU)
2094 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2095 LL_FOREACH(session->
delayqueue, delayqueue) {
2129 if (send_pdu == COAP_SEND_INC_PDU &&
2140 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2166 ltoken_length, ptoken, &drop_options,
2172 ((block.
num) << 4) |
2183#if COAP_CONSTRAINED_STACK
2185 static uint8_t l_data[1024];
2187 uint8_t l_data[1024];
2191 assert(chunk <= 1024);
2193 block.
num * chunk, l_data, &l_length,
2235#if COAP_CLIENT_SUPPORT
2248 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2249 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2251 if (now <= non_timeout) {
2253 *tim_rem = non_timeout - now;
2256 timed_out = now - non_timeout;
2262 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2264 memset(&block, 0,
sizeof(block));
2265 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2268 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2269 if (*tim_rem > non_timeout) {
2270 *tim_rem = non_timeout;
2284 LL_DELETE(session->
lg_xmit, lg_xmit);
2288 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2299#if COAP_SERVER_SUPPORT
2312 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2313 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2315 if (now <= non_timeout) {
2317 *tim_rem = non_timeout - now;
2320 timed_out = now - non_timeout;
2326 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2328 memset(&block, 0,
sizeof(block));
2329 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2333 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2334 if (*tim_rem > non_timeout) {
2335 *tim_rem = non_timeout;
2349 LL_DELETE(session->
lg_xmit, lg_xmit);
2353 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2365#if COAP_CLIENT_SUPPORT
2379 if (opt && lg_crcv) {
2380 int observe_action = -1;
2387 if (lg_crcv->obs_token_cnt <= block_num) {
2391 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2394 lg_crcv->obs_token = tmp;
2395 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2396 lg_crcv->obs_token[i] =
NULL;
2401 if (lg_crcv->obs_token_cnt <= block_num)
2402 lg_crcv->obs_token_cnt = block_num + 1;
2405 if (lg_crcv->obs_token[block_num] ==
NULL)
2409 if (block_num < lg_crcv->obs_token_cnt) {
2410 return lg_crcv->obs_token[block_num];
2417#if COAP_Q_BLOCK_SUPPORT
2422 coap_send_pdu_t send_request) {
2425 uint64_t token_match =
2429 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2438 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2443#if COAP_SERVER_SUPPORT
2444#if COAP_Q_BLOCK_SUPPORT
2455 coap_send_pdu_t send_response) {
2460 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2465 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2468 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2478 if (data_info->
ref > 0) {
2490#if COAP_CLIENT_SUPPORT
2491#if COAP_Q_BLOCK_SUPPORT
2523 11, (
const uint8_t *)
".well-known");
2525 4, (
const uint8_t *)
"core");
2538 (0 << 4) | (0 << 3) | 0),
2558 if (lg_crcv ==
NULL)
2561 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2565 lg_crcv->initial = 1;
2583 const uint8_t *data;
2586 if (data_len < lg_xmit->data_info->length) {
2597 if (!lg_crcv->app_token) {
2598 coap_block_delete_lg_crcv(session, lg_crcv);
2604 lg_crcv->retry_counter = 1;
2605 lg_crcv->state_token = state_token;
2612 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2620 lg_crcv->o_blk_size = block.
aszx;
2631#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2634 if (lg_crcv ==
NULL)
2637 if (lg_crcv->ref > 0) {
2643 if (lg_crcv->obs_data) {
2645 lg_crcv->obs_data =
NULL;
2651 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2660#if COAP_SERVER_SUPPORT
2664#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2667 if (lg_srcv ==
NULL)
2670 if (lg_srcv->ref > 0) {
2687 if (lg_xmit ==
NULL)
2690 if (lg_xmit->
ref > 0) {
2707#if COAP_SERVER_SUPPORT
2714add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2715 uint32_t *count, uint32_t max_count) {
2718 for (i = 0; i < *count && *count < max_count; i++) {
2719 if (num == out_blocks[i].num)
2721 else if (num < out_blocks[i].num) {
2723 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2724 out_blocks[i].num = num;
2725 out_blocks[i].is_continue = is_continue;
2730 if (*count < max_count) {
2731 out_blocks[i].num = num;
2732 out_blocks[i].is_continue = is_continue;
2765 uint16_t block_opt = 0;
2766 send_track *out_blocks =
NULL;
2767 const char *error_phrase;
2772 uint32_t request_cnt, i;
2775#if COAP_Q_BLOCK_SUPPORT
2780 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2787 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2788 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2789 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2791 goto skip_app_handler;
2798 if (block.
num == 0) {
2802 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2803 if (lg_xmit ==
NULL)
2806#if COAP_Q_BLOCK_SUPPORT
2812 goto internal_issue;
2825 if (etag == lg_xmit->
b.
b2.
etag) {
2839 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2842 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2843 block.
num, block.
m);
2845 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2846 1 << (block.
szx + 4), block.
num, block.
m);
2849 if (block.
num == 0) {
2850 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2855 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2857 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2859 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2860 1 << (block.
szx + 4), block.
num);
2863 "next block is not aligned on requested block size "
2864 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2866 (1 << (block.
szx + 4)),
2867 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2870 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2871 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2883#if COAP_Q_BLOCK_SUPPORT
2896 sizeof(
"Changing blocksize during request invalid")-1,
2897 (
const uint8_t *)
"Changing blocksize during request invalid");
2899 goto skip_app_handler;
2901#if COAP_Q_BLOCK_SUPPORT
2906 goto call_app_handler;
2910 num + i < max_block; i++) {
2911 add_block_send(num + i, 1, out_blocks, &request_cnt,
2919 num + i < max_block; i++) {
2920 add_block_send(num + i, 0, out_blocks, &request_cnt,
2925 add_block_send(num, 0, out_blocks, &request_cnt,
2928 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2932 if (request_cnt == 0) {
2936 out_blocks[0].num = 0;
2937 out_blocks[0].is_continue = 0;
2941 for (i = 0; i < request_cnt; i++) {
2944 block.
num = out_blocks[i].num;
2947 if (i + 1 < request_cnt) {
2954 if (out_blocks[i].is_continue) {
2966 goto internal_issue;
2969 if (out_blocks[i].is_continue)
2984 goto internal_issue;
2994 ((out_pdu->
max_size - token_options) /1024) * 1024;
3005 goto internal_issue;
3007 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3015 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3033 goto internal_issue;
3041 goto internal_issue;
3043 if (i + 1 < request_cnt) {
3049 goto skip_app_handler;
3050#if COAP_Q_BLOCK_SUPPORT
3060 (
const uint8_t *)error_phrase);
3081 rec_blocks->
retry = 0;
3083 for (i = 0; i < rec_blocks->
used; i++) {
3084 if (block_num >= rec_blocks->
range[i].
begin &&
3085 block_num <= rec_blocks->range[i].end)
3088 if (block_num < rec_blocks->range[i].begin) {
3089 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3096 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3097 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3103 if (block_num == rec_blocks->
range[i].
end + 1) {
3104 rec_blocks->
range[i].
end = block_num;
3105 if (i + 1 < rec_blocks->
used) {
3106 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3109 if (i+2 < rec_blocks->
used) {
3110 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3111 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3119 if (i == rec_blocks->
used) {
3133#if COAP_SERVER_SUPPORT
3153 const uint8_t *data =
NULL;
3158 uint16_t block_option = 0;
3165 const uint8_t *rtag;
3166 uint32_t max_block_szx;
3168 unsigned int saved_num;
3169 size_t saved_offset;
3170 int lg_srcv_is_refed = 0;
3173 *pfree_lg_srcv =
NULL;
3180#if COAP_Q_BLOCK_SUPPORT
3183 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3184 (
const uint8_t *)
"Block1 + Q-Block1 together");
3186 goto skip_app_handler;
3190#if COAP_Q_BLOCK_SUPPORT
3196 if (!block_option ||
3210 goto skip_app_handler;
3212 goto call_app_handler;
3238 goto skip_app_handler;
3246 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3247 max_block_szx = block.
szx;
3249 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3253 (total > max_body)) {
3258 if (max_body_size == 0 || max_body < max_body_size) {
3259 max_body_size = max_body;
3266 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3270 goto skip_app_handler;
3273 offset = block.
num << (block.
szx + 4);
3276#
if COAP_Q_BLOCK_SUPPORT
3295 if (total < (length + offset + (block.
m ? 1 : 0)))
3296 total = length + offset + (block.m ? 1 : 0);
3298 *added_block = block.
m;
3300 goto call_app_handler;
3306 LL_FOREACH(session->lg_srcv, lg_srcv) {
3307 if (rtag_opt || lg_srcv->rtag_set == 1) {
3308 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3310 if (lg_srcv->rtag_length != rtag_length ||
3311 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3314 if (resource == lg_srcv->resource) {
3317 if ((lg_srcv->resource == context->unknown_resource ||
3318 resource == context->proxy_uri_resource) &&
3325 (
const uint8_t *)
"Missing block 0");
3327 goto skip_app_handler;
3333 if (lg_srcv ==
NULL) {
3335 (
const uint8_t *)
"Memory issue");
3337 goto skip_app_handler;
3342 lg_srcv->resource = resource;
3343 if (resource == context->unknown_resource ||
3344 resource == context->proxy_uri_resource)
3346 lg_srcv->content_format = fmt;
3347 lg_srcv->total_len = total;
3349 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3350 max_block_szx < block.
szx) {
3351 lg_srcv->szx = max_block_szx;
3353 lg_srcv->szx = block.
szx;
3355 lg_srcv->block_option = block_option;
3358 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3359 lg_srcv->observe_set = 1;
3363 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3364 lg_srcv->rtag_set = 1;
3366 lg_srcv->body_data =
NULL;
3367 LL_PREPEND(session->lg_srcv, lg_srcv);
3370 coap_lg_srcv_reference_lkd(lg_srcv);
3371 lg_srcv_is_refed = 1;
3375 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3377 (
const uint8_t *)
"Missing interim block");
3379 goto skip_app_handler;
3382 if (fmt != lg_srcv->content_format) {
3383 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3384 (
const uint8_t *)
"Content-Format mismatch");
3389#if COAP_Q_BLOCK_SUPPORT
3391 if (total != lg_srcv->total_len) {
3393 (
const uint8_t *)
"Size1 mismatch");
3403 lg_srcv->last_mid = pdu->
mid;
3404 lg_srcv->last_type = pdu->
type;
3407 saved_num = block.
num;
3408 saved_offset = offset;
3410 while (offset < saved_offset + length) {
3415 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3416 (
const uint8_t *)
"Too many missing blocks");
3423 offset = block.
num << (block.
szx + 4);
3429#if COAP_Q_BLOCK_SUPPORT
3430 lg_srcv->rec_blocks.processing_payload_set =
3433 if (lg_srcv->total_len < saved_offset + length) {
3434 lg_srcv->total_len = saved_offset + length;
3437#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3438 !resource->is_proxy_uri && \
3439 !resource->is_reverse_proxy && \
3440 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3441 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3443 if (USE_BLOCK_DATA_HANDLER) {
3448 &lg_srcv->body_data,
3449 length, data, saved_offset,
3450 lg_srcv->total_len));
3453 goto skip_app_handler;
3457 saved_offset, lg_srcv->total_len);
3458 if (!lg_srcv->body_data) {
3460 (
const uint8_t *)
"Memory issue");
3462 goto skip_app_handler;
3473#if COAP_Q_BLOCK_SUPPORT
3478 if (lg_srcv->rec_blocks.used == 1 &&
3481 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3483 block.
num = lg_srcv->rec_blocks.range[0].end;
3484 goto skip_app_handler;
3489 goto skip_app_handler;
3493 goto skip_app_handler;
3499 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3500 max_block_szx < block.
aszx) {
3501 block.
aszx = max_block_szx;
3532 LL_FOREACH(session->lg_srcv, sg) {
3533 if (lg_srcv == sg) {
3539 goto skip_app_handler;
3542 if (lg_srcv->last_token) {
3543 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3551#if COAP_Q_BLOCK_SUPPORT
3556 lg_srcv->no_more_seen = 1;
3570#if COAP_Q_BLOCK_SUPPORT
3574 goto skip_app_handler;
3584 if (lg_srcv->observe_set) {
3586 lg_srcv->observe_length, lg_srcv->observe);
3589 if (lg_srcv->body_data) {
3598 if (USE_BLOCK_DATA_HANDLER) {
3607 lg_srcv->dont_timeout = 1;
3608 *pfree_lg_srcv = lg_srcv;
3611 if (lg_srcv_is_refed)
3612 coap_lg_srcv_release_lkd(session, lg_srcv);
3616 LL_DELETE(session->lg_srcv, lg_srcv);
3617 coap_block_delete_lg_srcv(session, lg_srcv);
3620 if (lg_srcv_is_refed)
3621 coap_lg_srcv_release_lkd(session, lg_srcv);
3626#if COAP_CLIENT_SUPPORT
3627#if COAP_Q_BLOCK_SUPPORT
3629derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3630 uint32_t value = **bp & 0x1f;
3634 }
else if (value == 24) {
3636 return (uint32_t)-1;
3640 }
else if (value == 25) {
3642 return (uint32_t)-1;
3650 return (uint32_t)-1;
3651 value = (uint32_t)(**bp) << 24;
3653 value |= **bp << 16;
3671 if (sent || lg_xmit || lg_crcv) {
3675 const uint8_t *data;
3685 }
else if (lg_xmit) {
3688 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3689 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3692 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3693 lg_xmit->data_info->length - offset;
3696 sent = lg_crcv->sent_pdu;
3705 ++lg_crcv->retry_counter);
3719 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3720#if COAP_OSCORE_SUPPORT
3721 if (session->oscore_encryption &&
3728 lg_crcv->state_token = token;
3778 int lg_crcv_is_refed = 0;
3783 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3786 lg_crcv = coap_find_lg_crcv(session, rcvd);
3789 coap_lg_crcv_reference_lkd(lg_crcv);
3790 lg_crcv_is_refed = 1;
3800 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3801 1 << (block.
szx + 4), block.
num);
3807 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3808 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3813 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3815 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3817 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3818 1 << (block.
szx + 4), block.
num);
3823 "next block is not aligned on requested block size boundary. "
3824 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3826 (1 << (block.
szx + 4)),
3827 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3830 track_echo(session, rcvd);
3843 goto skip_app_handler;
3848 lg_xmit->
offset = (block.
num + 1) * chunk;
3864 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3867 assert(len <=
sizeof(buf));
3869 memcpy(buf, new_token->
s, len);
3889 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3892 ((pdu->
max_size - token_options) /1024) * 1024;
3904#if COAP_CONSTRAINED_STACK
3906 static uint8_t l_data[1024];
3908 uint8_t l_data[1024];
3912 assert(chunk <= 1024);
3914 block.
num * chunk, l_data, &l_length,
3929#if COAP_Q_BLOCK_SUPPORT
3932 if (coap_send_q_block1(session, block, pdu,
3935 goto skip_app_handler;
3942 goto skip_app_handler;
3952 int observe_action = -1;
3955 goto lg_xmit_finished;
3965 goto lg_xmit_finished;
3971 goto call_app_handler;
3973 goto lg_xmit_finished;
3975 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
3976 goto skip_app_handler;
3977#if COAP_Q_BLOCK_SUPPORT
3982 goto skip_app_handler;
3986 const uint8_t *data;
3991 uint16_t fmt = fmt_opt ?
4001 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
4002 goto skip_app_handler;
4007 const uint8_t *bp = data;
4013 size_t ltoken_length;
4015 for (i = 0; (bp < data + length) &&
4017 if ((*bp & 0xc0) != 0x00)
4019 block.
num = derive_cbor_value(&bp, data + length - bp);
4021 if (block.
num > (1 << 20) -1)
4023 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4050 goto skip_app_handler;
4053 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4056 goto lg_xmit_finished;
4058 goto call_app_handler;
4070 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4081 lg_crcv->sent_pdu->lg_xmit = 0;
4093 LL_DELETE(session->
lg_xmit, lg_xmit);
4097 if (lg_crcv_is_refed)
4098 coap_lg_crcv_release_lkd(session, lg_crcv);
4102 if (lg_crcv_is_refed)
4103 coap_lg_crcv_release_lkd(session, lg_crcv);
4116 const uint8_t *data,
size_t offset,
size_t total) {
4129 const uint8_t *data,
size_t offset,
size_t total) {
4132 if (body_data ==
NULL && total) {
4135 if (body_data ==
NULL)
4139 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4145 if (offset + length <= total && body_data->length >= total) {
4146 memcpy(&body_data->
s[offset], data, length);
4162 memcpy(&body_data->
s[offset], data, length);
4171#if COAP_CLIENT_SUPPORT
4191#if COAP_Q_BLOCK_SUPPORT
4195 uint16_t block_opt = 0;
4197 int ack_rst_sent = 0;
4201 memset(&block, 0,
sizeof(block));
4202#if COAP_Q_BLOCK_SUPPORT
4203 memset(&qblock, 0,
sizeof(qblock));
4205 lg_crcv = coap_find_lg_crcv(session, rcvd);
4212 const uint8_t *data;
4215 size_t size2 = size_opt ?
4227#if COAP_Q_BLOCK_SUPPORT
4230 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4236 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4241 track_echo(session, rcvd);
4242 if (have_block && (block.
m || length)) {
4246 uint16_t fmt = fmt_opt ?
4253 size_t saved_offset;
4266 goto expire_lg_crcv;
4269 chunk = (size_t)1 << (block.
szx + 4);
4270 offset = block.
num * chunk;
4271 if (size2 < (offset + length)) {
4273 size2 = offset + length + 1;
4275 size2 = offset + length;
4277 saved_offset = offset;
4279 if (lg_crcv->initial) {
4280#if COAP_Q_BLOCK_SUPPORT
4283 lg_crcv->initial = 0;
4284 if (lg_crcv->body_data) {
4286 lg_crcv->body_data =
NULL;
4290 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4291 lg_crcv->etag_set = 1;
4293 lg_crcv->etag_set = 0;
4295 lg_crcv->total_len = size2;
4296 lg_crcv->content_format = fmt;
4297 lg_crcv->szx = block.
szx;
4298 lg_crcv->block_option = block_opt;
4299 lg_crcv->last_type = rcvd->
type;
4300 lg_crcv->rec_blocks.used = 0;
4301 lg_crcv->rec_blocks.total_blocks = 0;
4302#if COAP_Q_BLOCK_SUPPORT
4303 lg_crcv->rec_blocks.processing_payload_set = 0;
4306 if (lg_crcv->total_len < size2)
4307 lg_crcv->total_len = size2;
4311 uint8_t max_block_szx;
4314 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4315 max_block_szx = block.
szx;
4317 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4321 (size2 > max_body)) {
4324 if (max_body_size == 0 || max_body < max_body_size) {
4325 max_body_size = max_body;
4337 lg_crcv->etag, lg_crcv->etag_length)) {
4341 ++lg_crcv->retry_counter);
4345#if COAP_Q_BLOCK_SUPPORT
4350 coap_log_warn(
"Data body updated during receipt - new request started\n");
4354 lg_crcv->initial = 1;
4356 lg_crcv->body_data =
NULL;
4369 (0 << 4) | (0 << 3) | block.
aszx),
4375 goto skip_app_handler;
4377 }
else if (lg_crcv->etag_set) {
4383 if (fmt != lg_crcv->content_format) {
4387#if COAP_Q_BLOCK_SUPPORT
4393 if (block.
num == 0) {
4399 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4400 lg_crcv->observe_set = 1;
4402 lg_crcv->observe_set = 0;
4406 while (offset < saved_offset + length) {
4408#if COAP_Q_BLOCK_SUPPORT
4412 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4413 1 << (block.
szx + 4), block.
num);
4414#if COAP_Q_BLOCK_SUPPORT
4416 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4417 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4418 coap_request_missing_q_block2(session, lg_crcv);
4420 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4430 offset = block.
num << (block.
szx + 4);
4436 if (updated_block) {
4443 if (size2 < saved_offset + length) {
4444 size2 = saved_offset + length;
4451 &lg_crcv->body_data,
4453 saved_offset, size2));
4459 saved_offset, size2);
4460 if (lg_crcv->body_data ==
NULL) {
4473#if COAP_Q_BLOCK_SUPPORT
4479 if (check_all_blocks_in_for_payload_set(session,
4480 &lg_crcv->rec_blocks)) {
4481 block.
num = lg_crcv->rec_blocks.range[0].end;
4483 lg_crcv->rec_blocks.processing_payload_set =
4485 if (check_any_blocks_next_payload_set(session,
4486 &lg_crcv->rec_blocks)) {
4488 coap_request_missing_q_block2(session, lg_crcv);
4489 goto skip_app_handler;
4494 goto skip_app_handler;
4498 goto skip_app_handler;
4522 ((block.
num + 1) << 4) |
4523 (block.
m << 3) | block.
aszx),
4528 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4533 goto skip_app_handler;
4536 goto skip_app_handler;
4541#if COAP_Q_BLOCK_SUPPORT
4543 lg_crcv->total_len : size2;
4552 if (lg_crcv->app_token)
4554 lg_crcv->app_token->s);
4557 goto call_app_handler;
4559#if COAP_Q_BLOCK_SUPPORT
4565 if (lg_crcv->observe_set) {
4567 lg_crcv->observe_length, lg_crcv->observe);
4569 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4570#if COAP_Q_BLOCK_SUPPORT
4579 lg_crcv->total_len : saved_offset + length;
4587#if COAP_Q_BLOCK_SUPPORT
4589 lg_crcv->total_len : size2;
4602 if (lg_crcv->app_token)
4604 lg_crcv->app_token->s);
4607 body_free = lg_crcv->body_data;
4608 lg_crcv->body_data =
NULL;
4612 if (lg_crcv->observe_set == 0) {
4614 LL_DELETE(session->lg_crcv, lg_crcv);
4615 coap_block_delete_lg_crcv(session, lg_crcv);
4616 goto skip_app_handler;
4619 lg_crcv->initial = 1;
4622 goto skip_app_handler;
4637 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4638 lg_crcv->observe_set = 1;
4640 lg_crcv->observe_set = 0;
4649 goto expire_lg_crcv;
4654#if COAP_OSCORE_SUPPORT
4655 if (check_freshness(session, rcvd,
4656 (session->oscore_encryption == 0) ? sent :
NULL,
4659 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4661 goto skip_app_handler;
4662 goto expire_lg_crcv;
4671 const uint8_t *data;
4678 ++lg_crcv->retry_counter);
4686#if COAP_PROXY_SUPPORT
4687 coap_proxy_req_t *proxy_req = session->
context->proxy_list_count ?
4688 coap_proxy_map_outgoing_request(session, rcvd,
NULL) :
NULL;
4694 proxy_req->token_used =
new;
4695 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"upd");
4699 coap_log_debug(
"* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
4703 goto skip_app_handler;
4706 goto expire_lg_crcv;
4709 goto expire_lg_crcv;
4711 if (!block.
m && !lg_crcv->observe_set) {
4730#
if COAP_Q_BLOCK_SUPPORT
4737 goto skip_app_handler;
4740 const uint8_t *data;
4744#if COAP_Q_BLOCK_SUPPORT
4745 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4751 if (block.
num != 0) {
4753 size_t chunk = (size_t)1 << (block.
szx + 4);
4758 goto call_app_handler;
4761#if COAP_Q_BLOCK_SUPPORT
4765 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4771 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4774 LL_PREPEND(session->lg_crcv, lg_crcv);
4775 return coap_handle_response_get_block(context, session, sent, rcvd,
4788 track_echo(session, rcvd);
4790 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4793 LL_PREPEND(session->lg_crcv, lg_crcv);
4794 return coap_handle_response_get_block(context, session, sent, rcvd,
4806 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4809 LL_PREPEND(session->lg_crcv, lg_crcv);
4810 return coap_handle_response_get_block(context, session, sent, rcvd,
4828 if (lg_crcv->app_token)
4830 lg_crcv->app_token->s);
4834 LL_DELETE(session->lg_crcv, lg_crcv);
4835 coap_block_delete_lg_crcv(session, lg_crcv);
4847#if COAP_SERVER_SUPPORT
4856 if (response->
code == 0)
4858 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4866#if COAP_CLIENT_SUPPORT
4869 uint64_t token_match =
4875 if (session->lg_crcv) {
4876 LL_FOREACH(session->lg_crcv, lg_crcv) {
4881 lg_crcv->app_token->s);
4889 LL_FOREACH(session->
lg_xmit, lg_xmit) {
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void coap_block_release_lg_xmit_data(coap_session_t *session, coap_lg_xmit_data_t *data_info)
#define COAP_ETAG_MAX_BYTES
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
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, coap_get_large_data_t get_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
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 check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
int coap_flsll(long long j)
int coap_fls(unsigned int i)
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
@ COAP_NACK_TOO_MANY_RETRIES
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
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().
uint8_t coap_unique_id[8]
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...
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
void coap_call_response_handler(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, void *body_free)
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
#define coap_check_update_token(a, b)
int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
void coap_context_set_block_mode_lkd(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
#define COAP_BLOCK_MAX_SIZE_SET(a)
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_l...
COAP_STATIC_INLINE void coap_lg_xmit_reference_lkd(coap_lg_xmit_t *lg_xmit)
Increment reference counter on a lg_xmit.
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Remove a lg_xmit.
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
coap_binary_t * coap_block_build_body_lkd(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_SET_MASK
COAP_STATIC_INLINE void coap_lg_xmit_release_lkd(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Decrement reference counter on a lg_xmit.
coap_lg_xmit_t * coap_find_lg_xmit(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_xmit for the session that matches the pdu.
#define COAP_BLOCK_MAX_SIZE_GET(a)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
COAP_API 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.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
#define COAP_BLOCK_USE_M_Q_BLOCK
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
#define COAP_BLOCK_STLESS_BLOCK2
COAP_API 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_BLOCK_TRY_Q_BLOCK
#define COAP_BLOCK_STLESS_FETCH
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
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.
COAP_API 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.
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.
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of 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.
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
#define COAP_BLOCK_USE_LIBCOAP
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.
#define COAP_MAX_DELAY_TICKS
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
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, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
void coap_register_block_data_handler(coap_context_t *context, coap_block_data_handler_t block_data_handler)
Sets up a handler that is called for each received block during a block-wise transfer when COAP_BLOCK...
coap_response_t(* coap_block_data_handler_t)(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, coap_binary_t **body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Definition of the block data handler function.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
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_BLOCK_ISSUE
Triggered when a block transfer could not be handled.
@ 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_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#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_info(...)
#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_API 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.
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.
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
#define COAP_PDU_IS_RESPONSE(pdu)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
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.
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
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_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
#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_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_Q_BLOCK1
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_OPTION_URI_PATH
#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_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_CONTENT_TYPE
size_t coap_add_option(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_Q_BLOCK2
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.
#define COAP_OPTION_URI_PATH_ABB
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
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_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(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.
@ COAP_SESSION_TYPE_CLIENT
client-side
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.
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
#define COAP_STATIC_INLINE
coap_address_t remote
remote address and port
coap_address_t local
local address and port
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
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.
coap_block_data_handler_t block_data_cb
Called with each block data during block transfers.
uint32_t max_body_size
Max supported body size or 0 is unlimited.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
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.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
applicaton provided ptr for de-alloc function
uint32_t ref
Reference count.
const uint8_t * data
large data ptr
size_t length
large data length
coap_release_large_data_t release_func
large data de-alloc function
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
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
coap_lg_xmit_data_t * data_info
Pointer to large data information.
int last_block
last acknowledged block number Block1 last transmitted Q-Block2
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 * sent_pdu
The sent pdu with all the data.
uint32_t ref
Reference count.
uint16_t option
large block transmisson CoAP option
struct coap_lg_xmit_t * next
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 * token
first byte of token (or extended length bytes prefix), 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)
coap_bin_const_t actual_token
Actual token in pdu.
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
coap_session_t * session
Session responsible for PDU or NULL.
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
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
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint8_t no_path_abbrev
Set is remote does not support Uri-Path-Abbrev.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
uint32_t tx_rtag
Next Request-Tag number to use.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
size_t length
length of string