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
519 LL_PREPEND(session->
lg_xmit, lg_xmit);
524#if COAP_CLIENT_SUPPORT
528 lg_crcv = coap_find_lg_crcv(session, pdu);
530 lg_xmit->b.b1.state_token = lg_crcv->state_token;
542#if COAP_CLIENT_SUPPORT
550 ret = coap_cancel_observe_lkd(session, token, type);
566 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
571 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
572 if (lg_crcv->observe_set) {
573 if ((!token && !lg_crcv->app_token->length) || (token &&
579#if COAP_Q_BLOCK_SUPPORT
585 lg_crcv->obs_token[0] ?
586 lg_crcv->obs_token[0] :
595 lg_crcv->observe_set = 0;
605 if (lg_crcv->o_block_option) {
608 lg_crcv->o_blk_size),
611 if (lg_crcv->obs_data) {
612 coap_add_data_large_request_lkd(session, pdu,
613 lg_crcv->obs_data->length,
614 lg_crcv->obs_data->data,
NULL,
NULL);
616 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
627#if COAP_Q_BLOCK_SUPPORT
629 if (using_q_block1) {
630 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
649 uint64_t token_match =
653 LL_FOREACH(session->lg_crcv, lg_crcv) {
671 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
673 if (m_lg_crcv->obs_data) {
674 m_lg_crcv->obs_data->ref++;
675 lg_crcv->obs_data = m_lg_crcv->obs_data;
677 LL_PREPEND(session->lg_crcv, lg_crcv);
683#if COAP_OSCORE_SUPPORT
697 lg_crcv = coap_find_lg_crcv(session, pdu);
701 ++lg_crcv->retry_counter);
729#if COAP_SERVER_SUPPORT
746 LL_FOREACH(session->
lg_xmit, lg_xmit) {
753 lg_xmit->b.b2.query ?
754 lg_xmit->b.b2.query : &empty)) {
763 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
789#if COAP_Q_BLOCK_SUPPORT
795 int have_block_defined = 0;
797 uint8_t max_blk_size;
799 size_t token_options;
802#if COAP_Q_BLOCK_SUPPORT
806#if !COAP_SERVER_SUPPORT
812 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
820 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
834#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
835#if UINT_MAX < MAX_BLK_LEN
837#define MAX_BLK_LEN UINT_MAX
845#if COAP_SERVER_SUPPORT
857 coap_digest_t digest;
858 coap_digest_ctx_t *dctx = coap_digest_setup();
864 if (coap_digest_update(dctx, data, length)) {
865 if (coap_digest_final(dctx, &digest)) {
866 memcpy(&etag, digest.key,
sizeof(etag));
867#if COAP_ETAG_MAX_BYTES != 8
873 coap_digest_free(dctx);
893 if (etag == etag_r) {
908#if COAP_Q_BLOCK_SUPPORT
924 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
928#if COAP_CLIENT_SUPPORT
931 LL_DELETE(session->
lg_xmit, lg_xmit);
942#if COAP_Q_BLOCK_SUPPORT
943 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
956#if COAP_SERVER_SUPPORT
961 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
964 LL_DELETE(session->
lg_xmit, lg_xmit);
971#if COAP_OSCORE_SUPPORT
972 if (session->oscore_encryption) {
980 avail = pdu->
max_size - token_options;
983#if COAP_OSCORE_SUPPORT
992 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
998 if (max_blk_size && blk_size > max_blk_size)
999 blk_size = max_blk_size;
1003 if (block.
szx < blk_size)
1004 blk_size = block.
szx;
1005 have_block_defined = 1;
1007#if COAP_Q_BLOCK_SUPPORT
1010 if (have_block_defined) {
1012 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1016 if (block.
szx < blk_size)
1017 blk_size = block.
szx;
1018 have_block_defined = 1;
1019 option = alt_option;
1024 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1026 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1030 chunk = (size_t)1 << (blk_size + 4);
1031 if ((have_block_defined && block.
num != 0) || single_request ||
1036 if (length >= block.
num * chunk) {
1037#if COAP_SERVER_SUPPORT
1043 (
unsigned int)length),
1057 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1062 if (chunk > length - block.
num * chunk)
1063 rem = length - block.
num * chunk;
1070 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1095 lg_xmit->blk_size = blk_size;
1096 lg_xmit->option = option;
1098 if (!lg_xmit->data_info)
1100 lg_xmit->data_info->ref = 0;
1101 lg_xmit->data_info->data = data;
1102 lg_xmit->data_info->length = length;
1103#if COAP_Q_BLOCK_SUPPORT
1104 lg_xmit->non_timeout_random_ticks =
1107 lg_xmit->data_info->get_func = get_func;
1108 lg_xmit->data_info->release_func = release_func;
1109 lg_xmit->data_info->app_ptr = app_ptr;
1116 if (!lg_xmit->b.b1.app_token)
1124 lg_xmit->b.b1.count = 1;
1126 lg_xmit->b.b1.count);
1135 (
unsigned int)length),
1148 lg_xmit->b.b2.resource = resource;
1151 if (lg_xmit->b.b2.query) {
1152 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1155 lg_xmit->b.b2.query =
NULL;
1160 sizeof(lg_xmit->b.b2.rtag));
1162 lg_xmit->b.b2.rtag_set = 1;
1164 lg_xmit->b.b2.rtag_set = 0;
1166 lg_xmit->b.b2.etag = etag;
1167 lg_xmit->b.b2.request_method = request_method;
1174 lg_xmit->b.b2.maxage_expire = 0;
1179 (
unsigned int)length),
1184 blk_size, lg_xmit->data_info->length))
1191 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1199 avail = pdu->
max_size - token_options;
1204#if COAP_OSCORE_SUPPORT
1207 if (avail < (ssize_t)chunk) {
1210 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1213 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1214 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1215 lg_xmit->blk_size = blk_size;
1216 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1222 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1227 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1228 rem = lg_xmit->data_info->length - block.
num * chunk;
1230#if COAP_CONSTRAINED_STACK
1232 static uint8_t l_data[1024];
1234 uint8_t l_data[1024];
1238 assert(rem <= 1024);
1239 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1250 lg_xmit->b.b1.bert_size = rem;
1252 lg_xmit->last_block = -1;
1255 LL_PREPEND(session->
lg_xmit,lg_xmit);
1258 if (have_block_defined) {
1262 (0 << 4) | (0 << 3) | blk_size), buf);
1269 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1290 }
else if (release_func) {
1296#if COAP_CLIENT_SUPPORT
1301 const uint8_t *data,
1308 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1309 release_func, app_ptr);
1318 const uint8_t *data,
1332 length, data, release_func,
NULL, app_ptr, 0, 0);
1345 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1346 release_func, get_func, app_ptr);
1366 length,
NULL, release_func, get_func,
1371#if COAP_SERVER_SUPPORT
1378 uint16_t media_type,
1382 const uint8_t *data,
1388 ret = coap_add_data_large_response_lkd(resource, session, request,
1389 response, query, media_type, maxage, etag,
1390 length, data, release_func, app_ptr);
1401 uint16_t media_type,
1405 const uint8_t *data,
1409 unsigned char buf[4];
1411 int block_requested = 0;
1412 int single_request = 0;
1413#if COAP_Q_BLOCK_SUPPORT
1414 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1420 memset(&block, 0,
sizeof(block));
1427 block_requested = 1;
1428 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1431 length >> (block.
szx + 4));
1436#if COAP_Q_BLOCK_SUPPORT
1438 block_requested = 1;
1439 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1442 length >> (block.
szx + 4));
1446 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1467 if (block_requested) {
1491 query, maxage, etag, length, data,
1492 release_func,
NULL, app_ptr, single_request,
1495 goto error_released;
1505#if COAP_ERROR_PHRASE_LENGTH > 0
1523#if COAP_Q_BLOCK_SUPPORT
1533 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1537 LL_DELETE(session->
lg_xmit, lg_xmit);
1541 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1547 if (lg_xmit->
last_sent + partial_timeout <= now) {
1550#if COAP_CLIENT_SUPPORT
1554 LL_DELETE(session->
lg_xmit, lg_xmit);
1561 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1562 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1571#if COAP_CLIENT_SUPPORT
1572#if COAP_Q_BLOCK_SUPPORT
1577 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1589 pdu->
type = lg_crcv->last_type;
1601 int block_payload_set = -1;
1612 if (lg_crcv->rec_blocks.used &&
1613 (lg_crcv->rec_blocks.used < 2 ||
1616 block = lg_crcv->rec_blocks.range[0].end + 1;
1617 block_size = (size_t)1 << (lg_crcv->szx + 4);
1618 sofar = block * block_size;
1619 if (sofar < lg_crcv->total_len) {
1622 pdu = coap_build_missing_pdu(session, lg_crcv);
1628 (block << 4) | (1 << 3) | lg_crcv->szx),
1636 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1637 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1638 lg_crcv->rec_blocks.range[i].begin != 0) {
1641 pdu = coap_build_missing_pdu(session, lg_crcv);
1646 if (block_payload_set == -1)
1648 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1652 (block << 4) | (0 << 3) | lg_crcv->szx),
1656 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1657 block = lg_crcv->rec_blocks.range[i].end;
1660 block_size = (size_t)1 << (lg_crcv->szx + 4);
1661 sofar = (block + 1) * block_size;
1662 if (sofar < lg_crcv->total_len) {
1665 pdu = coap_build_missing_pdu(session, lg_crcv);
1669 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1671 if (block_payload_set == -1)
1673 for (; block < (ssize_t)sofar &&
1677 (block << 4) | (0 << 3) | lg_crcv->szx),
1684 lg_crcv->rec_blocks.retry++;
1685 if (block_payload_set != -1)
1686 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1701#if COAP_Q_BLOCK_SUPPORT
1707#if COAP_Q_BLOCK_SUPPORT
1709 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1715 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1719#if COAP_Q_BLOCK_SUPPORT
1721 size_t scaled_timeout = receive_timeout *
1722 ((size_t)1 << lg_crcv->rec_blocks.retry);
1730 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1731 coap_request_missing_q_block2(session, lg_crcv);
1733 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1734 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1742 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1743 lg_crcv->last_used + partial_timeout <= now) {
1744#if COAP_Q_BLOCK_SUPPORT
1748 LL_DELETE(session->lg_crcv, lg_crcv);
1749 coap_block_delete_lg_crcv(session, lg_crcv);
1750 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1752 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1753 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1762#if COAP_SERVER_SUPPORT
1763#if COAP_Q_BLOCK_SUPPORT
1775 if (lg_srcv->last_token)
1776 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1791 assert(block >= 0 && block < (1 << 20));
1793 if (block < 0 || block >= (1 << 20)) {
1795 }
else if (block < 24) {
1798 }
else if (block < 0x100) {
1802 }
else if (block < 0x10000) {
1805 val[1] = block >> 8;
1806 val[2] = block & 0xff;
1810 val[1] = block >> 16;
1811 val[2] = (block >> 8) & 0xff;
1812 val[3] = block & 0xff;
1828 for (i = 0; i < rec_blocks->
used; i++) {
1829 if (block_num < rec_blocks->range[i].begin)
1831 if (block_num <= rec_blocks->range[i].end)
1837#if COAP_SERVER_SUPPORT
1839check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1840 if (rec_blocks->
used == 0) {
1841 return block_num == 0 ? 1 : 0;
1843 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1860 for (i = 0; i < rec_blocks->
used; i++) {
1861 if (block < rec_blocks->range[i].begin)
1863 if (block < rec_blocks->range[i].end)
1869#if COAP_CLIENT_SUPPORT
1870#if COAP_Q_BLOCK_SUPPORT
1874 if (rec_blocks->
used &&
1876 rec_blocks->processing_payload_set)
1884 if (rec_blocks->
used > 1 &&
1886 rec_blocks->processing_payload_set)
1893#if COAP_SERVER_SUPPORT
1904#if COAP_Q_BLOCK_SUPPORT
1910#if COAP_Q_BLOCK_SUPPORT
1912 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1918 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1919 if (lg_srcv->dont_timeout) {
1926#if COAP_Q_BLOCK_SUPPORT
1928 size_t scaled_timeout = receive_timeout *
1929 ((size_t)1 << lg_srcv->rec_blocks.retry);
1935 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1938 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1939 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1941 size_t last_payload_block;
1943 size_t no_blocks = 0;
1946 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1947 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1948 lg_srcv->rec_blocks.range[i].begin != 0) {
1950 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1952 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1953 block = lg_srcv->rec_blocks.range[i].end;
1956 if (no_blocks == 0 && block == (
int)final_block)
1962 if (final_block > last_payload_block) {
1963 final_block = last_payload_block;
1965 no_blocks += final_block - block;
1966 if (no_blocks == 0) {
1968 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1970 if (final_block > last_payload_block) {
1971 final_block = last_payload_block;
1976 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1977 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1978 lg_srcv->rec_blocks.range[i].begin != 0) {
1981 pdu = pdu_408_build(session, lg_srcv);
1986 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1987 if (!add_408_block(pdu, block)) {
1992 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1993 block = lg_srcv->rec_blocks.range[i].end;
1997 for (; block <= (int)final_block; block++) {
1999 pdu = pdu_408_build(session, lg_srcv);
2003 if (!add_408_block(pdu, block)) {
2009 lg_srcv->rec_blocks.retry++;
2012 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2013 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2020 if (lg_srcv->no_more_seen)
2022 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2023#if COAP_Q_BLOCK_SUPPORT
2040 if (lg_srcv->last_token)
2041 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2043 (
const uint8_t *)
"Missing interim block");
2047 LL_DELETE(session->lg_srcv, lg_srcv);
2048 coap_block_delete_lg_srcv(session, lg_srcv);
2049 }
else if (lg_srcv->last_used) {
2051 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2052 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2061#if COAP_Q_BLOCK_SUPPORT
2070 coap_send_pdu_t send_pdu) {
2075 const uint8_t *ptoken;
2077 size_t ltoken_length;
2078 uint32_t delayqueue_cnt = 0;
2081 if (send_pdu == COAP_SEND_INC_PDU)
2090 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2091 LL_FOREACH(session->
delayqueue, delayqueue) {
2119 ptoken, &drop_options);
2125 if (send_pdu == COAP_SEND_INC_PDU &&
2135 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2161 ltoken_length, ptoken, &drop_options);
2166 ((block.
num) << 4) |
2177#if COAP_CONSTRAINED_STACK
2179 static uint8_t l_data[1024];
2181 uint8_t l_data[1024];
2185 assert(chunk <= 1024);
2187 block.
num * chunk, l_data, &l_length,
2227#if COAP_CLIENT_SUPPORT
2240 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2241 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2243 if (now <= non_timeout) {
2245 *tim_rem = non_timeout - now;
2248 timed_out = now - non_timeout;
2254 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2256 memset(&block, 0,
sizeof(block));
2257 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2260 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2261 if (*tim_rem > non_timeout) {
2262 *tim_rem = non_timeout;
2276 LL_DELETE(session->
lg_xmit, lg_xmit);
2280 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2291#if COAP_SERVER_SUPPORT
2304 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2305 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2307 if (now <= non_timeout) {
2309 *tim_rem = non_timeout - now;
2312 timed_out = now - non_timeout;
2318 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2320 memset(&block, 0,
sizeof(block));
2321 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2325 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2326 if (*tim_rem > non_timeout) {
2327 *tim_rem = non_timeout;
2341 LL_DELETE(session->
lg_xmit, lg_xmit);
2345 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2357#if COAP_CLIENT_SUPPORT
2371 if (opt && lg_crcv) {
2372 int observe_action = -1;
2379 if (lg_crcv->obs_token_cnt <= block_num) {
2383 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2386 lg_crcv->obs_token = tmp;
2387 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2388 lg_crcv->obs_token[i] =
NULL;
2393 lg_crcv->obs_token_cnt = block_num + 1;
2396 if (lg_crcv->obs_token[block_num] ==
NULL)
2400 if (block_num < lg_crcv->obs_token_cnt) {
2401 return lg_crcv->obs_token[block_num];
2408#if COAP_Q_BLOCK_SUPPORT
2413 coap_send_pdu_t send_request) {
2416 uint64_t token_match =
2420 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2429 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2434#if COAP_SERVER_SUPPORT
2435#if COAP_Q_BLOCK_SUPPORT
2446 coap_send_pdu_t send_response) {
2451 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2456 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2459 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2469 if (data_info->
ref > 0) {
2481#if COAP_CLIENT_SUPPORT
2482#if COAP_Q_BLOCK_SUPPORT
2514 11, (
const uint8_t *)
".well-known");
2516 4, (
const uint8_t *)
"core");
2529 (0 << 4) | (0 << 3) | 0),
2549 if (lg_crcv ==
NULL)
2552 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2556 lg_crcv->initial = 1;
2574 const uint8_t *data;
2577 if (data_len < lg_xmit->data_info->length) {
2588 if (!lg_crcv->app_token) {
2589 coap_block_delete_lg_crcv(session, lg_crcv);
2595 lg_crcv->retry_counter = 1;
2596 lg_crcv->state_token = state_token;
2603 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2611 lg_crcv->o_blk_size = block.
aszx;
2622#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2625 if (lg_crcv ==
NULL)
2629 if (lg_crcv->obs_data) {
2631 lg_crcv->obs_data =
NULL;
2637 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2646#if COAP_SERVER_SUPPORT
2650#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2653 if (lg_srcv ==
NULL)
2668 if (lg_xmit ==
NULL)
2683#if COAP_SERVER_SUPPORT
2690add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2691 uint32_t *count, uint32_t max_count) {
2694 for (i = 0; i < *count && *count < max_count; i++) {
2695 if (num == out_blocks[i].num)
2697 else if (num < out_blocks[i].num) {
2699 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2700 out_blocks[i].num = num;
2701 out_blocks[i].is_continue = is_continue;
2706 if (*count < max_count) {
2707 out_blocks[i].num = num;
2708 out_blocks[i].is_continue = is_continue;
2741 uint16_t block_opt = 0;
2742 send_track *out_blocks =
NULL;
2743 const char *error_phrase;
2748 uint32_t request_cnt, i;
2751#if COAP_Q_BLOCK_SUPPORT
2756 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2763 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2764 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2765 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2767 goto skip_app_handler;
2774 if (block.
num == 0) {
2778 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2779 if (lg_xmit ==
NULL)
2782#if COAP_Q_BLOCK_SUPPORT
2788 goto internal_issue;
2801 if (etag == lg_xmit->
b.
b2.
etag) {
2815 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2818 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2819 block.
num, block.
m);
2821 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2822 1 << (block.
szx + 4), block.
num, block.
m);
2825 if (block.
num == 0) {
2826 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2831 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2833 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2835 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2836 1 << (block.
szx + 4), block.
num);
2839 "next block is not aligned on requested block size "
2840 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2842 (1 << (block.
szx + 4)),
2843 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2846 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2847 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2859#if COAP_Q_BLOCK_SUPPORT
2872 sizeof(
"Changing blocksize during request invalid")-1,
2873 (
const uint8_t *)
"Changing blocksize during request invalid");
2875 goto skip_app_handler;
2877#if COAP_Q_BLOCK_SUPPORT
2882 goto call_app_handler;
2886 num + i < max_block; i++) {
2887 add_block_send(num + i, 1, out_blocks, &request_cnt,
2895 num + i < max_block; i++) {
2896 add_block_send(num + i, 0, out_blocks, &request_cnt,
2901 add_block_send(num, 0, out_blocks, &request_cnt,
2904 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2908 if (request_cnt == 0) {
2912 out_blocks[0].num = 0;
2913 out_blocks[0].is_continue = 0;
2917 for (i = 0; i < request_cnt; i++) {
2920 block.
num = out_blocks[i].num;
2923 if (i + 1 < request_cnt) {
2930 if (out_blocks[i].is_continue) {
2939 goto internal_issue;
2942 if (out_blocks[i].is_continue)
2957 goto internal_issue;
2967 ((out_pdu->
max_size - token_options) /1024) * 1024;
2978 goto internal_issue;
2980 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
2988 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3006 goto internal_issue;
3014 goto internal_issue;
3016 if (i + 1 < request_cnt) {
3022 goto skip_app_handler;
3023#if COAP_Q_BLOCK_SUPPORT
3033 (
const uint8_t *)error_phrase);
3054 rec_blocks->
retry = 0;
3056 for (i = 0; i < rec_blocks->
used; i++) {
3057 if (block_num >= rec_blocks->
range[i].
begin &&
3058 block_num <= rec_blocks->range[i].end)
3061 if (block_num < rec_blocks->range[i].begin) {
3062 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3069 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3070 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3076 if (block_num == rec_blocks->
range[i].
end + 1) {
3077 rec_blocks->
range[i].
end = block_num;
3078 if (i + 1 < rec_blocks->
used) {
3079 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3082 if (i+2 < rec_blocks->
used) {
3083 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3084 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3092 if (i == rec_blocks->
used) {
3106#if COAP_SERVER_SUPPORT
3126 const uint8_t *data =
NULL;
3131 uint16_t block_option = 0;
3138 const uint8_t *rtag;
3139 uint32_t max_block_szx;
3141 unsigned int saved_num;
3142 size_t saved_offset;
3145 *pfree_lg_srcv =
NULL;
3152#if COAP_Q_BLOCK_SUPPORT
3155 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3156 (
const uint8_t *)
"Block1 + Q-Block1 together");
3158 goto skip_app_handler;
3162#if COAP_Q_BLOCK_SUPPORT
3168 if (!block_option ||
3182 goto skip_app_handler;
3184 goto call_app_handler;
3210 goto skip_app_handler;
3218 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3219 max_block_szx = block.
szx;
3221 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3225 (total > max_body)) {
3230 if (max_body_size == 0 || max_body < max_body_size) {
3231 max_body_size = max_body;
3238 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3242 goto skip_app_handler;
3245 offset = block.
num << (block.
szx + 4);
3248#
if COAP_Q_BLOCK_SUPPORT
3267 if (total < (length + offset + (block.
m ? 1 : 0)))
3268 total = length + offset + (block.m ? 1 : 0);
3270 *added_block = block.
m;
3272 goto call_app_handler;
3278 LL_FOREACH(session->lg_srcv, lg_srcv) {
3279 if (rtag_opt || lg_srcv->rtag_set == 1) {
3280 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3282 if (lg_srcv->rtag_length != rtag_length ||
3283 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3286 if (resource == lg_srcv->resource) {
3289 if ((lg_srcv->resource == context->unknown_resource ||
3290 resource == context->proxy_uri_resource) &&
3297 (
const uint8_t *)
"Missing block 0");
3299 goto skip_app_handler;
3305 if (lg_srcv ==
NULL) {
3307 (
const uint8_t *)
"Memory issue");
3309 goto skip_app_handler;
3314 lg_srcv->resource = resource;
3315 if (resource == context->unknown_resource ||
3316 resource == context->proxy_uri_resource)
3318 lg_srcv->content_format = fmt;
3319 lg_srcv->total_len = total;
3321 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3322 max_block_szx < block.
szx) {
3323 lg_srcv->szx = max_block_szx;
3325 lg_srcv->szx = block.
szx;
3327 lg_srcv->block_option = block_option;
3330 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3331 lg_srcv->observe_set = 1;
3335 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3336 lg_srcv->rtag_set = 1;
3338 lg_srcv->body_data =
NULL;
3339 LL_PREPEND(session->lg_srcv, lg_srcv);
3345 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3347 (
const uint8_t *)
"Missing interim block");
3349 goto skip_app_handler;
3352 if (fmt != lg_srcv->content_format) {
3353 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3354 (
const uint8_t *)
"Content-Format mismatch");
3359#if COAP_Q_BLOCK_SUPPORT
3361 if (total != lg_srcv->total_len) {
3363 (
const uint8_t *)
"Size1 mismatch");
3373 lg_srcv->last_mid = pdu->
mid;
3374 lg_srcv->last_type = pdu->
type;
3377 saved_num = block.
num;
3378 saved_offset = offset;
3380 while (offset < saved_offset + length) {
3385 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3386 (
const uint8_t *)
"Too many missing blocks");
3393 offset = block.
num << (block.
szx + 4);
3399#if COAP_Q_BLOCK_SUPPORT
3400 lg_srcv->rec_blocks.processing_payload_set =
3403 if (lg_srcv->total_len < saved_offset + length) {
3404 lg_srcv->total_len = saved_offset + length;
3407#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3408 !resource->is_proxy_uri && \
3409 !resource->is_reverse_proxy && \
3410 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3411 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3413 if (USE_BLOCK_DATA_HANDLER) {
3418 &lg_srcv->body_data,
3419 length, data, saved_offset,
3420 lg_srcv->total_len));
3423 goto skip_app_handler;
3427 saved_offset, lg_srcv->total_len);
3428 if (!lg_srcv->body_data) {
3430 (
const uint8_t *)
"Memory issue");
3432 goto skip_app_handler;
3443#if COAP_Q_BLOCK_SUPPORT
3448 if (lg_srcv->rec_blocks.used == 1 &&
3452 block.
num = lg_srcv->rec_blocks.range[0].end;
3456 goto skip_app_handler;
3460 goto skip_app_handler;
3466 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3467 max_block_szx < block.
aszx) {
3468 block.
aszx = max_block_szx;
3499 LL_FOREACH(session->lg_srcv, sg) {
3500 if (lg_srcv == sg) {
3506 goto skip_app_handler;
3509 if (lg_srcv->last_token) {
3510 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3518#if COAP_Q_BLOCK_SUPPORT
3523 lg_srcv->no_more_seen = 1;
3537#if COAP_Q_BLOCK_SUPPORT
3541 goto skip_app_handler;
3551 if (lg_srcv->observe_set) {
3553 lg_srcv->observe_length, lg_srcv->observe);
3556 if (lg_srcv->body_data) {
3565 if (USE_BLOCK_DATA_HANDLER) {
3574 lg_srcv->dont_timeout = 1;
3575 *pfree_lg_srcv = lg_srcv;
3581 LL_DELETE(session->lg_srcv, lg_srcv);
3582 coap_block_delete_lg_srcv(session, lg_srcv);
3589#if COAP_CLIENT_SUPPORT
3590#if COAP_Q_BLOCK_SUPPORT
3592derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3593 uint32_t value = **bp & 0x1f;
3597 }
else if (value == 24) {
3599 return (uint32_t)-1;
3603 }
else if (value == 25) {
3605 return (uint32_t)-1;
3613 return (uint32_t)-1;
3616 value |= **bp << 16;
3634 if (sent || lg_xmit || lg_crcv) {
3638 const uint8_t *data;
3648 }
else if (lg_xmit) {
3651 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3652 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3655 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3656 lg_xmit->data_info->length - offset;
3659 sent = lg_crcv->sent_pdu;
3668 ++lg_crcv->retry_counter);
3681 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3682#if COAP_OSCORE_SUPPORT
3683 if (session->oscore_encryption &&
3690 lg_crcv->state_token = token;
3744 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3747 lg_crcv = coap_find_lg_crcv(session, rcvd);
3758 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3759 1 << (block.
szx + 4), block.
num);
3765 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3766 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3771 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3773 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3775 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3776 1 << (block.
szx + 4), block.
num);
3781 "next block is not aligned on requested block size boundary. "
3782 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3784 (1 << (block.
szx + 4)),
3785 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3788 track_echo(session, rcvd);
3806 lg_xmit->
offset = (block.
num + 1) * chunk;
3822 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3825 assert(len <=
sizeof(buf));
3827 memcpy(buf, new_token->
s, len);
3846 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3849 ((pdu->
max_size - token_options) /1024) * 1024;
3861#if COAP_CONSTRAINED_STACK
3863 static uint8_t l_data[1024];
3865 uint8_t l_data[1024];
3869 assert(chunk <= 1024);
3871 block.
num * chunk, l_data, &l_length,
3886#if COAP_Q_BLOCK_SUPPORT
3889 if (coap_send_q_block1(session, block, pdu,
3909 int observe_action = -1;
3912 goto lg_xmit_finished;
3922 goto lg_xmit_finished;
3930 goto lg_xmit_finished;
3932 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
3934#if COAP_Q_BLOCK_SUPPORT
3943 const uint8_t *data;
3948 uint16_t fmt = fmt_opt ?
3958 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3964 const uint8_t *bp = data;
3970 size_t ltoken_length;
3972 for (i = 0; (bp < data + length) &&
3974 if ((*bp & 0xc0) != 0x00)
3976 block.
num = derive_cbor_value(&bp, data + length - bp);
3978 if (block.
num > (1 << 20) -1)
3980 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4010 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4013 goto lg_xmit_finished;
4027 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4038 lg_crcv->sent_pdu->lg_xmit = 0;
4050 LL_DELETE(session->
lg_xmit, lg_xmit);
4067 const uint8_t *data,
size_t offset,
size_t total) {
4070 if (body_data ==
NULL && total) {
4073 if (body_data ==
NULL)
4077 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4082 if (offset + length <= total && body_data->length >= total) {
4083 memcpy(&body_data->
s[offset], data, length);
4099 memcpy(&body_data->
s[offset], data, length);
4108#if COAP_CLIENT_SUPPORT
4128#if COAP_Q_BLOCK_SUPPORT
4132 uint16_t block_opt = 0;
4134 int ack_rst_sent = 0;
4137 memset(&block, 0,
sizeof(block));
4138#if COAP_Q_BLOCK_SUPPORT
4139 memset(&qblock, 0,
sizeof(qblock));
4141 lg_crcv = coap_find_lg_crcv(session, rcvd);
4149 const uint8_t *data;
4152 size_t size2 = size_opt ?
4164#if COAP_Q_BLOCK_SUPPORT
4167 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4173 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4178 track_echo(session, rcvd);
4179 if (have_block && (block.
m || length)) {
4183 uint16_t fmt = fmt_opt ?
4190 size_t saved_offset;
4203 goto expire_lg_crcv;
4206 chunk = (size_t)1 << (block.
szx + 4);
4207 offset = block.
num * chunk;
4208 if (size2 < (offset + length)) {
4210 size2 = offset + length + 1;
4212 size2 = offset + length;
4214 saved_offset = offset;
4216 if (lg_crcv->initial) {
4217#if COAP_Q_BLOCK_SUPPORT
4220 lg_crcv->initial = 0;
4221 if (lg_crcv->body_data) {
4223 lg_crcv->body_data =
NULL;
4227 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4228 lg_crcv->etag_set = 1;
4230 lg_crcv->etag_set = 0;
4232 lg_crcv->total_len = size2;
4233 lg_crcv->content_format = fmt;
4234 lg_crcv->szx = block.
szx;
4235 lg_crcv->block_option = block_opt;
4236 lg_crcv->last_type = rcvd->
type;
4237 lg_crcv->rec_blocks.used = 0;
4238 lg_crcv->rec_blocks.total_blocks = 0;
4239#if COAP_Q_BLOCK_SUPPORT
4240 lg_crcv->rec_blocks.processing_payload_set = 0;
4243 if (lg_crcv->total_len < size2)
4244 lg_crcv->total_len = size2;
4248 uint8_t max_block_szx;
4251 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4252 max_block_szx = block.
szx;
4254 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4258 (size2 > max_body)) {
4261 if (max_body_size == 0 || max_body < max_body_size) {
4262 max_body_size = max_body;
4274 lg_crcv->etag, lg_crcv->etag_length)) {
4278 ++lg_crcv->retry_counter);
4282#if COAP_Q_BLOCK_SUPPORT
4287 coap_log_warn(
"Data body updated during receipt - new request started\n");
4291 lg_crcv->initial = 1;
4293 lg_crcv->body_data =
NULL;
4305 (0 << 4) | (0 << 3) | block.
aszx),
4311 goto skip_app_handler;
4313 }
else if (lg_crcv->etag_set) {
4319 if (fmt != lg_crcv->content_format) {
4323#if COAP_Q_BLOCK_SUPPORT
4329 if (block.
num == 0) {
4335 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4336 lg_crcv->observe_set = 1;
4338 lg_crcv->observe_set = 0;
4342 while (offset < saved_offset + length) {
4344#if COAP_Q_BLOCK_SUPPORT
4348 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4349 1 << (block.
szx + 4), block.
num);
4350#if COAP_Q_BLOCK_SUPPORT
4352 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4353 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4354 coap_request_missing_q_block2(session, lg_crcv);
4356 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4366 offset = block.
num << (block.
szx + 4);
4372 if (updated_block) {
4379 if (size2 < saved_offset + length) {
4380 size2 = saved_offset + length;
4387 &lg_crcv->body_data,
4389 saved_offset, size2));
4395 saved_offset, size2);
4396 if (lg_crcv->body_data ==
NULL) {
4409#if COAP_Q_BLOCK_SUPPORT
4415 if (check_all_blocks_in_for_payload_set(session,
4416 &lg_crcv->rec_blocks)) {
4417 block.
num = lg_crcv->rec_blocks.range[0].end;
4419 lg_crcv->rec_blocks.processing_payload_set =
4421 if (check_any_blocks_next_payload_set(session,
4422 &lg_crcv->rec_blocks)) {
4424 coap_request_missing_q_block2(session, lg_crcv);
4425 goto skip_app_handler;
4430 goto skip_app_handler;
4434 goto skip_app_handler;
4457 ((block.
num + 1) << 4) |
4458 (block.
m << 3) | block.
aszx),
4463 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4468 goto skip_app_handler;
4471 goto skip_app_handler;
4476#if COAP_Q_BLOCK_SUPPORT
4478 lg_crcv->total_len : size2;
4487 if (lg_crcv->app_token)
4489 lg_crcv->app_token->s);
4492 goto call_app_handler;
4494#if COAP_Q_BLOCK_SUPPORT
4500 if (lg_crcv->observe_set) {
4502 lg_crcv->observe_length, lg_crcv->observe);
4504 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4505#if COAP_Q_BLOCK_SUPPORT
4514 lg_crcv->total_len : saved_offset + length;
4522#if COAP_Q_BLOCK_SUPPORT
4524 lg_crcv->total_len : size2;
4537 if (lg_crcv->app_token)
4539 lg_crcv->app_token->s);
4542 body_free = lg_crcv->body_data;
4543 lg_crcv->body_data =
NULL;
4547 if (lg_crcv->observe_set == 0) {
4549 LL_DELETE(session->lg_crcv, lg_crcv);
4550 coap_block_delete_lg_crcv(session, lg_crcv);
4551 goto skip_app_handler;
4554 lg_crcv->initial = 1;
4557 goto skip_app_handler;
4572 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4573 lg_crcv->observe_set = 1;
4575 lg_crcv->observe_set = 0;
4584 goto expire_lg_crcv;
4589#if COAP_OSCORE_SUPPORT
4590 if (check_freshness(session, rcvd,
4591 (session->oscore_encryption == 0) ? sent :
NULL,
4594 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4596 goto skip_app_handler;
4597 goto expire_lg_crcv;
4600 goto expire_lg_crcv;
4602 if (!block.
m && !lg_crcv->observe_set) {
4621#
if COAP_Q_BLOCK_SUPPORT
4628 goto skip_app_handler;
4631 const uint8_t *data;
4635#if COAP_Q_BLOCK_SUPPORT
4636 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4642 if (block.
num != 0) {
4644 size_t chunk = (size_t)1 << (block.
szx + 4);
4649 goto call_app_handler;
4652#if COAP_Q_BLOCK_SUPPORT
4656 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4662 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4665 LL_PREPEND(session->lg_crcv, lg_crcv);
4666 return coap_handle_response_get_block(context, session, sent, rcvd,
4679 track_echo(session, rcvd);
4681 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4684 LL_PREPEND(session->lg_crcv, lg_crcv);
4685 return coap_handle_response_get_block(context, session, sent, rcvd,
4702 if (lg_crcv->app_token)
4704 lg_crcv->app_token->s);
4708 LL_DELETE(session->lg_crcv, lg_crcv);
4709 coap_block_delete_lg_crcv(session, lg_crcv);
4721#if COAP_SERVER_SUPPORT
4730 if (response->
code == 0)
4732 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4740#if COAP_CLIENT_SUPPORT
4743 uint64_t token_match =
4749 if (session->lg_crcv) {
4750 LL_FOREACH(session->lg_crcv, lg_crcv) {
4755 lg_crcv->app_token->s);
4763 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...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
#define COAP_BLOCK_SET_MASK
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.
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
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
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.
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.
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)
Duplicate an existing PDU.
#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.
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.
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 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