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#define COAP_LG_XMIT_TXT_SCALAR (8)
34#if COAP_Q_BLOCK_SUPPORT
35static int blocks_delete_entry(
coap_rblock_t *rec_blocks, uint32_t block_num);
38#if COAP_Q_BLOCK_SUPPORT
84 if (block->
szx == 7) {
96 if (block->
m && (length % 1024) != 0) {
98 length - (length % 1024), length);
99 length -= length % 1024;
132 block->
m = block_b.
m;
142 unsigned int blk_size,
size_t total) {
144 size_t avail = pdu->
max_size - token_options;
145 unsigned int start = num << (blk_size + 4);
146 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
148 assert(start <= total);
149 memset(block, 0,
sizeof(*block));
151 block->
szx = block->
aszx = blk_size;
152 if (can_use_bert && blk_size == 6 && avail >= 1024 && session !=
NULL &&
157 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
159 block->
chunk_size = (size_t)1 << (blk_size + 4);
160 if (avail < block->chunk_size && (total - start) >= avail) {
166 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
169 new_blk_size =
coap_flsll((
long long)avail) - 5;
172 block->
szx = new_blk_size;
173 block->
num <<= szx - block->
szx;
174 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
185 unsigned char buf[4];
190 start = block->
num << (block->
szx + 4);
191 if (block->
num != 0 && data_length <= start) {
201 block->
szx, data_length))
206 ((block_b.
num << 4) |
219 unsigned char buf[4];
223 start = block->
num << (block->
szx + 4);
224 if (block->
num != 0 && data_length <= start) {
232 block->
szx, data_length))
247 unsigned int block_num,
unsigned char block_szx) {
249 start = block_num << (block_szx + 4);
255 min(len - start, ((
size_t)1 << (block_szx + 4))),
262 unsigned int start = block->
num << (block->
szx + 4);
270 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
272 max_size = (size_t)1 << (block->
szx + 4);
277 min(len - start, max_size),
292 unsigned char buf[4];
294 int block2_requested = 0;
295#if COAP_SERVER_SUPPORT
297 coap_digest_t digest;
298 coap_digest_ctx_t *dctx =
NULL;
301 memset(&block2, 0,
sizeof(block2));
308 block2_requested = 1;
309 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
312 length >> (block2.
szx + 4));
320#if COAP_SERVER_SUPPORT
323 dctx = coap_digest_setup();
326 if (request && request->
session &&
330 if (!coap_digest_update(dctx, data, length))
332 if (!coap_digest_final(dctx, &digest))
335 memcpy(&etag, digest.key,
sizeof(etag));
336#if COAP_ETAG_MAX_BYTES != 8
359 if (block2_requested) {
412#if COAP_SERVER_SUPPORT
413 coap_digest_free(dctx);
422 uint32_t block_mode) {
430 uint32_t block_mode) {
436#if ! COAP_Q_BLOCK_SUPPORT
444 size_t max_block_size) {
455 switch (max_block_size) {
466 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
471 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
479 const uint8_t *b,
size_t blen) {
480 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
487 uint64_t token_match =
491 LL_FOREACH(session->
lg_xmit, lg_xmit) {
497#if COAP_CLIENT_SUPPORT
528 LL_PREPEND(session->
lg_xmit, lg_xmit);
533#if COAP_CLIENT_SUPPORT
537 lg_crcv = coap_find_lg_crcv(session, pdu);
539 lg_xmit->b.b1.state_token = lg_crcv->state_token;
551#if COAP_CLIENT_SUPPORT
559 ret = coap_cancel_observe_lkd(session, token, type);
575 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
580 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
581 if (lg_crcv->observe_set) {
582 if ((!token && !lg_crcv->app_token->length) || (token &&
588#if COAP_Q_BLOCK_SUPPORT
594 lg_crcv->obs_token[0] ?
595 lg_crcv->obs_token[0] :
605 lg_crcv->observe_set = 0;
615 if (lg_crcv->o_block_option) {
618 lg_crcv->o_blk_size),
621 if (lg_crcv->obs_data) {
622 coap_add_data_large_request_lkd(session, pdu,
623 lg_crcv->obs_data->length,
624 lg_crcv->obs_data->data,
NULL,
NULL);
626 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
637#if COAP_Q_BLOCK_SUPPORT
639 if (using_q_block1) {
640 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
659 uint64_t token_match =
663 LL_FOREACH(session->lg_crcv, lg_crcv) {
681 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
683 if (m_lg_crcv->obs_data) {
684 m_lg_crcv->obs_data->ref++;
685 lg_crcv->obs_data = m_lg_crcv->obs_data;
687 LL_PREPEND(session->lg_crcv, lg_crcv);
693#if COAP_OSCORE_SUPPORT
707 lg_crcv = coap_find_lg_crcv(session, pdu);
711 ++lg_crcv->retry_counter);
739#if COAP_SERVER_SUPPORT
756 LL_FOREACH(session->
lg_xmit, lg_xmit) {
773 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
799#if COAP_Q_BLOCK_SUPPORT
805 int have_block_defined = 0;
807 uint8_t max_blk_size;
809 size_t token_options;
812#if COAP_Q_BLOCK_SUPPORT
816#if !COAP_SERVER_SUPPORT
822 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
830 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
844#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
845#if UINT_MAX < MAX_BLK_LEN
847#define MAX_BLK_LEN UINT_MAX
855#if COAP_SERVER_SUPPORT
867 coap_digest_t digest;
868 coap_digest_ctx_t *dctx = coap_digest_setup();
874 if (coap_digest_update(dctx, data, length)) {
875 if (coap_digest_final(dctx, &digest)) {
876 memcpy(&etag, digest.key,
sizeof(etag));
877#if COAP_ETAG_MAX_BYTES != 8
883 coap_digest_free(dctx);
903 if (etag == etag_r) {
918#if COAP_Q_BLOCK_SUPPORT
934 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
938#if COAP_CLIENT_SUPPORT
941 LL_DELETE(session->
lg_xmit, lg_xmit);
952#if COAP_Q_BLOCK_SUPPORT
953 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
966#if COAP_SERVER_SUPPORT
971 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
974 LL_DELETE(session->
lg_xmit, lg_xmit);
981#if COAP_OSCORE_SUPPORT
982 if (session->oscore_encryption) {
990 avail = pdu->
max_size - token_options;
993#if COAP_OSCORE_SUPPORT
1002 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1008 if (max_blk_size && blk_size > max_blk_size)
1009 blk_size = max_blk_size;
1013 if (block.
szx < blk_size)
1014 blk_size = block.
szx;
1015 have_block_defined = 1;
1017#if COAP_Q_BLOCK_SUPPORT
1020 if (have_block_defined) {
1022 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1026 if (block.
szx < blk_size)
1027 blk_size = block.
szx;
1028 have_block_defined = 1;
1029 option = alt_option;
1034 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1036 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1040 chunk = (size_t)1 << (blk_size + 4);
1041 if ((have_block_defined && block.
num != 0) || single_request ||
1046 if (length >= block.
num * chunk) {
1047#if COAP_SERVER_SUPPORT
1053 (
unsigned int)length),
1067 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1072 if (chunk > length - block.
num * chunk)
1073 rem = length - block.
num * chunk;
1080 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1105 lg_xmit->blk_size = blk_size;
1106 lg_xmit->option = option;
1108 if (!lg_xmit->data_info)
1110 lg_xmit->data_info->ref = 0;
1111 lg_xmit->data_info->data = data;
1112 lg_xmit->data_info->length = length;
1113#if COAP_Q_BLOCK_SUPPORT
1114 lg_xmit->non_timeout_random_ticks =
1117 lg_xmit->data_info->get_func = get_func;
1118 lg_xmit->data_info->release_func = release_func;
1119 lg_xmit->data_info->app_ptr = app_ptr;
1126 if (!lg_xmit->b.b1.app_token)
1134 lg_xmit->b.b1.count = 1;
1136 lg_xmit->b.b1.count);
1145 (
unsigned int)length),
1158 lg_xmit->b.b2.resource = resource;
1161 if (lg_xmit->b.b2.query) {
1162 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1165 lg_xmit->b.b2.query =
NULL;
1170 sizeof(lg_xmit->b.b2.rtag));
1172 lg_xmit->b.b2.rtag_set = 1;
1174 lg_xmit->b.b2.rtag_set = 0;
1176 lg_xmit->b.b2.etag = etag;
1177 lg_xmit->b.b2.request_method = request_method;
1184 lg_xmit->b.b2.maxage_expire = 0;
1189 (
unsigned int)length),
1194 blk_size, lg_xmit->data_info->length))
1201 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1209 avail = pdu->
max_size - token_options;
1214#if COAP_OSCORE_SUPPORT
1217 if (avail < (ssize_t)chunk) {
1220 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1223 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1224 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1225 lg_xmit->blk_size = blk_size;
1226 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1232 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1235#if COAP_Q_BLOCK_SUPPORT
1237 lg_xmit->send_blocks.used = 1;
1238 lg_xmit->send_blocks.range[0].end = (uint32_t)(lg_xmit->data_info->length / chunk);
1242 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1243 rem = lg_xmit->data_info->length - block.
num * chunk;
1245#if COAP_CONSTRAINED_STACK
1247 static uint8_t l_data[1024];
1249 uint8_t l_data[1024];
1253 assert(rem <= 1024);
1254 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1265 lg_xmit->b.b1.bert_size = rem;
1267 lg_xmit->last_block = -1;
1270 LL_PREPEND(session->
lg_xmit,lg_xmit);
1273 if (have_block_defined) {
1277 (0 << 4) | (0 << 3) | blk_size), buf);
1284 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1305 }
else if (release_func) {
1311#if COAP_CLIENT_SUPPORT
1316 const uint8_t *data,
1323 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1324 release_func, app_ptr);
1333 const uint8_t *data,
1347 length, data, release_func,
NULL, app_ptr, 0, 0);
1360 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1361 release_func, get_func, app_ptr);
1381 length,
NULL, release_func, get_func,
1386#if COAP_SERVER_SUPPORT
1393 uint16_t media_type,
1397 const uint8_t *data,
1403 ret = coap_add_data_large_response_lkd(resource, session, request,
1404 response, query, media_type, maxage, etag,
1405 length, data, release_func, app_ptr);
1416 uint16_t media_type,
1420 const uint8_t *data,
1424 unsigned char buf[4];
1426 int block_requested = 0;
1427 int single_request = 0;
1428#if COAP_Q_BLOCK_SUPPORT
1429 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1435 memset(&block, 0,
sizeof(block));
1442 block_requested = 1;
1443 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1446 length >> (block.
szx + 4));
1451#if COAP_Q_BLOCK_SUPPORT
1453 block_requested = 1;
1454 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1457 length >> (block.
szx + 4));
1461 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1482 if (block_requested) {
1506 query, maxage, etag, length, data,
1507 release_func,
NULL, app_ptr, single_request,
1510 goto error_released;
1520#if COAP_ERROR_PHRASE_LENGTH > 0
1538#if COAP_Q_BLOCK_SUPPORT
1548 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1552 LL_DELETE(session->
lg_xmit, lg_xmit);
1556 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1562 if (lg_xmit->
last_sent + partial_timeout <= now) {
1565#if COAP_CLIENT_SUPPORT
1569 LL_DELETE(session->
lg_xmit, lg_xmit);
1576 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1577 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1586#if COAP_CLIENT_SUPPORT
1587#if COAP_Q_BLOCK_SUPPORT
1592 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1604 pdu->
type = lg_crcv->last_type;
1616 int block_payload_set = -1;
1627 if (lg_crcv->rec_blocks.used &&
1628 (lg_crcv->rec_blocks.used < 2 ||
1631 block = lg_crcv->rec_blocks.range[0].end + 1;
1632 block_size = (size_t)1 << (lg_crcv->szx + 4);
1633 sofar = block * block_size;
1634 if (sofar < lg_crcv->total_len) {
1637 pdu = coap_build_missing_pdu(session, lg_crcv);
1643 (block << 4) | (1 << 3) | lg_crcv->szx),
1651 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1652 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1653 lg_crcv->rec_blocks.range[i].begin != 0) {
1656 pdu = coap_build_missing_pdu(session, lg_crcv);
1661 if (block_payload_set == -1)
1663 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1667 (block << 4) | (0 << 3) | lg_crcv->szx),
1671 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1672 block = lg_crcv->rec_blocks.range[i].end;
1675 block_size = (size_t)1 << (lg_crcv->szx + 4);
1676 sofar = (block + 1) * block_size;
1677 if (sofar < lg_crcv->total_len) {
1680 pdu = coap_build_missing_pdu(session, lg_crcv);
1684 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1686 if (block_payload_set == -1)
1688 for (; block < (ssize_t)sofar &&
1692 (block << 4) | (0 << 3) | lg_crcv->szx),
1699 lg_crcv->rec_blocks.retry++;
1700 if (block_payload_set != -1)
1701 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1716#if COAP_Q_BLOCK_SUPPORT
1722#if COAP_Q_BLOCK_SUPPORT
1724 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1730 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1734#if COAP_Q_BLOCK_SUPPORT
1736 size_t scaled_timeout = receive_timeout *
1737 ((size_t)1 << lg_crcv->rec_blocks.retry);
1745 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1746 coap_request_missing_q_block2(session, lg_crcv);
1748 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1749 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1757 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1758 lg_crcv->last_used + partial_timeout <= now) {
1759#if COAP_Q_BLOCK_SUPPORT
1763 LL_DELETE(session->lg_crcv, lg_crcv);
1764 coap_block_delete_lg_crcv(session, lg_crcv);
1765 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1767 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1768 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1777#if COAP_SERVER_SUPPORT
1778#if COAP_Q_BLOCK_SUPPORT
1790 if (lg_srcv->last_token)
1791 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1806 assert(block >= 0 && block < (1 << 20));
1808 if (block < 0 || block >= (1 << 20)) {
1810 }
else if (block < 24) {
1813 }
else if (block < 0x100) {
1817 }
else if (block < 0x10000) {
1820 val[1] = block >> 8;
1821 val[2] = block & 0xff;
1825 val[1] = block >> 16;
1826 val[2] = (block >> 8) & 0xff;
1827 val[3] = block & 0xff;
1843 for (i = 0; i < rec_blocks->
used; i++) {
1844 if (block_num < rec_blocks->range[i].begin)
1846 if (block_num <= rec_blocks->range[i].end)
1852#if COAP_SERVER_SUPPORT
1854check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1855 if (rec_blocks->
used == 0) {
1856 return block_num == 0 ? 1 : 0;
1858 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1875 for (i = 0; i < rec_blocks->
used; i++) {
1876 if (block < rec_blocks->range[i].begin)
1878 if (block < rec_blocks->range[i].end)
1884#if COAP_CLIENT_SUPPORT
1885#if COAP_Q_BLOCK_SUPPORT
1889 if (rec_blocks->
used &&
1891 rec_blocks->processing_payload_set)
1899 if (rec_blocks->
used > 1 &&
1901 rec_blocks->processing_payload_set)
1908#if COAP_SERVER_SUPPORT
1909#if COAP_Q_BLOCK_SUPPORT
1914 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1916 size_t last_payload_block;
1918 size_t no_blocks = 0;
1921 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1922 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1923 lg_srcv->rec_blocks.range[i].begin != 0) {
1925 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1927 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1928 block = lg_srcv->rec_blocks.range[i].end;
1931 if (no_blocks == 0 && block == (
int)final_block)
1937 if (final_block > last_payload_block) {
1938 final_block = last_payload_block;
1940 no_blocks += final_block - block;
1941 if (no_blocks == 0) {
1943 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1945 if (final_block > last_payload_block) {
1946 final_block = last_payload_block;
1951 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1952 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1953 lg_srcv->rec_blocks.range[i].begin != 0) {
1956 pdu = pdu_408_build(session, lg_srcv);
1961 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1962 if (!add_408_block(pdu, block)) {
1967 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1968 block = lg_srcv->rec_blocks.range[i].end;
1972 for (; block <= (int)final_block; block++) {
1974 pdu = pdu_408_build(session, lg_srcv);
1978 if (!add_408_block(pdu, block)) {
1984 lg_srcv->rec_blocks.retry++;
2000#if COAP_Q_BLOCK_SUPPORT
2006#if COAP_Q_BLOCK_SUPPORT
2008 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
2014 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
2015 if (lg_srcv->dont_timeout) {
2022#if COAP_Q_BLOCK_SUPPORT
2024 size_t scaled_timeout = receive_timeout *
2025 ((size_t)1 << lg_srcv->rec_blocks.retry);
2031 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
2032 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
2033 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
2035 if (lg_srcv->rec_blocks.used > 1 ||
2036 final_block != lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used - 1].end) {
2038 if (final_block > lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used - 1].end + 1)
2039 final_block = lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used - 1].end + 1;
2043 if (!request_missing_blocks(session, lg_srcv, final_block))
2046 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2047 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2054 if (lg_srcv->no_more_seen)
2056 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2057#if COAP_Q_BLOCK_SUPPORT
2074 if (lg_srcv->last_token)
2075 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2077 (
const uint8_t *)
"Missing interim block");
2081 LL_DELETE(session->lg_srcv, lg_srcv);
2082 coap_block_delete_lg_srcv(session, lg_srcv);
2083 }
else if (lg_srcv->last_used) {
2085 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2086 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2095#if COAP_Q_BLOCK_SUPPORT
2104 coap_send_pdu_t send_pdu) {
2109 const uint8_t *ptoken;
2111 size_t ltoken_length;
2112 uint32_t delayqueue_cnt = 0;
2116 if (send_pdu == COAP_SEND_INC_PDU) {
2122 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2135 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2136 LL_FOREACH(session->
delayqueue, delayqueue) {
2141 if ((block.
m || lg_xmit->send_blocks.used) &&
2144 (lg_xmit->send_blocks.used > 1 ||
2177 if (send_pdu == COAP_SEND_INC_PDU) {
2182 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2196 while (block_pdu && lg_xmit->send_blocks.used) {
2199 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2203 block.
num = lg_xmit->send_blocks.range[0].begin;
2205 offset = block.
num * chunk;
2209 (lg_xmit->send_blocks.used > 1 ||
2228 ltoken_length, ptoken, &drop_options,
2234 ((block.
num) << 4) |
2245#if COAP_CONSTRAINED_STACK
2247 static uint8_t l_data[1024];
2249 uint8_t l_data[1024];
2253 assert(chunk <= 1024);
2255 block.
num * chunk, l_data, &l_length,
2287 blocks_delete_entry(&lg_xmit->send_blocks, block.
num);
2300#if COAP_CLIENT_SUPPORT
2313 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2314 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2316 if (now <= non_timeout) {
2318 *tim_rem = non_timeout - now;
2321 timed_out = now - non_timeout;
2323 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2327 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2330 memset(&block, 0,
sizeof(block));
2331 block.
num = lg_xmit->send_blocks.range[0].begin;
2332 offset = block.
num * chunk;
2335 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2336 if (*tim_rem > non_timeout) {
2337 *tim_rem = non_timeout;
2351 LL_DELETE(session->
lg_xmit, lg_xmit);
2366#if COAP_SERVER_SUPPORT
2379 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2380 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2382 if (now <= non_timeout) {
2384 *tim_rem = non_timeout - now;
2387 timed_out = now - non_timeout;
2389 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2393 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2396 memset(&block, 0,
sizeof(block));
2397 block.
num = lg_xmit->send_blocks.range[0].begin;
2398 offset = block.
num * chunk;
2402 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2403 if (*tim_rem > non_timeout) {
2404 *tim_rem = non_timeout;
2418 LL_DELETE(session->
lg_xmit, lg_xmit);
2422 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2434#if COAP_CLIENT_SUPPORT
2448 if (opt && lg_crcv) {
2449 int observe_action = -1;
2456 if (lg_crcv->obs_token_cnt <= block_num) {
2460 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2463 lg_crcv->obs_token = tmp;
2464 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2465 lg_crcv->obs_token[i] =
NULL;
2470 if (lg_crcv->obs_token_cnt <= block_num)
2471 lg_crcv->obs_token_cnt = block_num + 1;
2474 if (lg_crcv->obs_token[block_num] ==
NULL)
2478 if (block_num < lg_crcv->obs_token_cnt) {
2479 return lg_crcv->obs_token[block_num];
2486#if COAP_Q_BLOCK_SUPPORT
2491 coap_send_pdu_t send_request) {
2494 uint64_t token_match =
2498 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2507 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2512#if COAP_SERVER_SUPPORT
2513#if COAP_Q_BLOCK_SUPPORT
2524 coap_send_pdu_t send_response) {
2529 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2537 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2547 if (data_info->
ref > 0) {
2559#if COAP_CLIENT_SUPPORT
2560#if COAP_Q_BLOCK_SUPPORT
2592 11, (
const uint8_t *)
".well-known");
2594 4, (
const uint8_t *)
"core");
2607 (0 << 4) | (0 << 3) | 0),
2627 if (lg_crcv ==
NULL)
2630 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2634 lg_crcv->initial = 1;
2652 const uint8_t *data;
2655 if (data_len < lg_xmit->data_info->length) {
2666 if (!lg_crcv->app_token) {
2667 coap_block_delete_lg_crcv(session, lg_crcv);
2673 lg_crcv->retry_counter = 1;
2674 lg_crcv->state_token = state_token;
2681 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2689 lg_crcv->o_blk_size = block.
aszx;
2700#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2703 if (lg_crcv ==
NULL)
2706 if (lg_crcv->ref > 0) {
2712 if (lg_crcv->obs_data) {
2714 lg_crcv->obs_data =
NULL;
2720 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2729#if COAP_SERVER_SUPPORT
2733#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2736 if (lg_srcv ==
NULL)
2739 if (lg_srcv->ref > 0) {
2756 if (lg_xmit ==
NULL)
2759 if (lg_xmit->
ref > 0) {
2776#if COAP_SERVER_SUPPORT
2783add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2784 uint32_t *count, uint32_t max_count) {
2787 for (i = 0; i < *count && *count < max_count; i++) {
2788 if (num == out_blocks[i].num)
2790 else if (num < out_blocks[i].num) {
2792 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2793 out_blocks[i].num = num;
2794 out_blocks[i].is_continue = is_continue;
2799 if (*count < max_count) {
2800 out_blocks[i].num = num;
2801 out_blocks[i].is_continue = is_continue;
2834 uint16_t block_opt = 0;
2835 send_track *out_blocks =
NULL;
2836 const char *error_phrase;
2841 uint32_t request_cnt, i;
2844#if COAP_Q_BLOCK_SUPPORT
2849 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2856 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2857 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2858 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2860 goto skip_app_handler;
2867 if (block.
num == 0) {
2871 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2872 if (lg_xmit ==
NULL)
2875#if COAP_Q_BLOCK_SUPPORT
2881 goto internal_issue;
2894 if (etag == lg_xmit->
b.
b2.
etag) {
2908 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2911 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2912 block.
num, block.
m);
2914 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2915 1 << (block.
szx + 4), block.
num, block.
m);
2918 if (block.
num == 0) {
2924 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
2925 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2926#if COAP_Q_BLOCK_SUPPORT
2927 lg_xmit->send_blocks.range[0].begin = block.
num;
2928 lg_xmit->send_blocks.range[0].end = (uint32_t)(lg_xmit->
data_info->
length / chunk);
2931 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2932 (1 << (block.
szx + 4)), block.
num);
2934 coap_log_debug(
"ignoring request to increase Block size from %u to %u\n",
2938 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2939 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2951#if COAP_Q_BLOCK_SUPPORT
2964 sizeof(
"Changing blocksize during request invalid")-1,
2965 (
const uint8_t *)
"Changing blocksize during request invalid");
2967 goto skip_app_handler;
2969#if COAP_Q_BLOCK_SUPPORT
2974 goto call_app_handler;
2978 num + i < max_block; i++) {
2979 add_block_send(num + i, 1, out_blocks, &request_cnt,
2987 num + i < max_block; i++) {
2988 add_block_send(num + i, 0, out_blocks, &request_cnt,
2993 add_block_send(num, 0, out_blocks, &request_cnt,
2996 add_block_send(num, 0, out_blocks, &request_cnt, 1);
3000 if (request_cnt == 0) {
3003 out_blocks[0].num = 0;
3004 out_blocks[0].is_continue = 0;
3008 for (i = 0; i < request_cnt; i++) {
3012 block.
num = out_blocks[i].num;
3014 if (i + 1 < request_cnt) {
3021 if (out_blocks[i].is_continue) {
3033 goto internal_issue;
3036 if (out_blocks[i].is_continue)
3051 goto internal_issue;
3058 offset = block.
num * chunk;
3062 ((out_pdu->
max_size - token_options) /1024) * 1024;
3073 goto internal_issue;
3075 if (!(offset + chunk < lg_xmit->data_info->length)) {
3083 if (!(offset + chunk < lg_xmit->data_info->length)) {
3101 goto internal_issue;
3109 goto internal_issue;
3111 if (i + 1 < request_cnt) {
3117 goto skip_app_handler;
3118#if COAP_Q_BLOCK_SUPPORT
3128 (
const uint8_t *)error_phrase);
3139#if COAP_Q_BLOCK_SUPPORT
3141blocks_delete_entry(
coap_rblock_t *rec_blocks, uint32_t block_num) {
3149 for (i = 0; i < rec_blocks->
used; i++) {
3150 if (block_num >= rec_blocks->
range[i].
begin &&
3151 block_num <= rec_blocks->range[i].end) {
3153 if (block_num == rec_blocks->
range[i].
begin) {
3154 if (block_num == rec_blocks->
range[i].
end) {
3156 if (i + 1 < rec_blocks->
used) {
3157 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3158 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3164 }
else if (block_num == rec_blocks->
range[i].
end) {
3169 if (i == rec_blocks->
used)
3171 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3172 sizeof(rec_blocks->
range[i]) * (rec_blocks->
used - i));
3179 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3180 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3181 rec_blocks->
range[i].
end = block_num - 1;
3182 rec_blocks->
range[i+1].
begin = block_num + 1;
3203 rec_blocks->
retry = 0;
3205 for (i = 0; i < rec_blocks->
used; i++) {
3206 if (block_num >= rec_blocks->
range[i].
begin &&
3207 block_num <= rec_blocks->range[i].end)
3210 if (block_num < rec_blocks->range[i].begin) {
3211 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3218 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3219 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3225 if (block_num == rec_blocks->
range[i].
end + 1) {
3226 rec_blocks->
range[i].
end = block_num;
3227 if (i + 1 < rec_blocks->
used) {
3228 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3231 if (i+2 < rec_blocks->
used) {
3232 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3233 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3241 if (i == rec_blocks->
used) {
3255#if COAP_SERVER_SUPPORT
3275 const uint8_t *data =
NULL;
3280 uint16_t block_option = 0;
3287 const uint8_t *rtag;
3288 uint32_t max_block_szx;
3290 unsigned int saved_num;
3291 size_t saved_offset;
3292 int lg_srcv_is_refed = 0;
3295 *pfree_lg_srcv =
NULL;
3302#if COAP_Q_BLOCK_SUPPORT
3305 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3306 (
const uint8_t *)
"Block1 + Q-Block1 together");
3308 goto skip_app_handler;
3312#if COAP_Q_BLOCK_SUPPORT
3318 if (!block_option ||
3332 goto skip_app_handler;
3334 goto call_app_handler;
3360 goto skip_app_handler;
3368 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3369 max_block_szx = block.
szx;
3371 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3375 (total > max_body)) {
3380 if (max_body_size == 0 || max_body < max_body_size) {
3381 max_body_size = max_body;
3388 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3392 goto skip_app_handler;
3395 offset = block.
num << (block.
szx + 4);
3398#
if COAP_Q_BLOCK_SUPPORT
3417 if (total < (length + offset + (block.
m ? 1 : 0)))
3418 total = length + offset + (block.
m ? 1 : 0);
3420 *added_block = block.
m;
3422 goto call_app_handler;
3428 LL_FOREACH(session->lg_srcv, lg_srcv) {
3429 if (rtag_opt || lg_srcv->rtag_set == 1) {
3430 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3432 if (lg_srcv->rtag_length != rtag_length ||
3433 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3436 if (resource == lg_srcv->resource) {
3439 if ((lg_srcv->resource == context->unknown_resource ||
3440 resource == context->proxy_uri_resource) &&
3447 (
const uint8_t *)
"Missing block 0");
3449 goto skip_app_handler;
3455 if (lg_srcv ==
NULL) {
3457 (
const uint8_t *)
"Memory issue");
3459 goto skip_app_handler;
3464 lg_srcv->resource = resource;
3465 if (resource == context->unknown_resource ||
3466 resource == context->proxy_uri_resource)
3468 lg_srcv->content_format = fmt;
3469 lg_srcv->total_len = total;
3471 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3472 max_block_szx < block.
szx) {
3473 lg_srcv->szx = max_block_szx;
3475 lg_srcv->szx = block.
szx;
3477 lg_srcv->block_option = block_option;
3480 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3481 lg_srcv->observe_set = 1;
3485 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3486 lg_srcv->rtag_set = 1;
3488 lg_srcv->body_data =
NULL;
3489 LL_PREPEND(session->lg_srcv, lg_srcv);
3492 coap_lg_srcv_reference_lkd(lg_srcv);
3493 lg_srcv_is_refed = 1;
3497 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3499 (
const uint8_t *)
"Missing interim block");
3501 goto skip_app_handler;
3504 if (fmt != lg_srcv->content_format) {
3505 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3506 (
const uint8_t *)
"Content-Format mismatch");
3511#if COAP_Q_BLOCK_SUPPORT
3513 if (total != lg_srcv->total_len) {
3515 (
const uint8_t *)
"Size1 mismatch");
3525 lg_srcv->last_mid = pdu->
mid;
3526 lg_srcv->last_type = pdu->
type;
3529 saved_num = block.
num;
3530 saved_offset = offset;
3532 while (offset < saved_offset + length) {
3544 offset = block.
num << (block.
szx + 4);
3550#if COAP_Q_BLOCK_SUPPORT
3551 lg_srcv->rec_blocks.processing_payload_set =
3554 if (lg_srcv->total_len < saved_offset + length) {
3555 lg_srcv->total_len = saved_offset + length;
3558#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3559 !resource->is_proxy_uri && \
3560 !resource->is_reverse_proxy && \
3561 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3562 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3564 if (USE_BLOCK_DATA_HANDLER) {
3569 &lg_srcv->body_data,
3570 length, data, saved_offset,
3571 lg_srcv->total_len));
3574 goto skip_app_handler;
3578 saved_offset, lg_srcv->total_len);
3579 if (!lg_srcv->body_data) {
3581 (
const uint8_t *)
"Memory issue");
3583 goto skip_app_handler;
3587 goto skip_app_handler;
3596#if COAP_Q_BLOCK_SUPPORT
3601 if (lg_srcv->rec_blocks.used == 1 &&
3604 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3606 block.
num = lg_srcv->rec_blocks.range[0].end;
3607 goto skip_app_handler;
3609 }
else if (lg_srcv->rec_blocks.used > 1 &&
3618 request_missing_blocks(session, lg_srcv, block.
num);
3619 goto skip_app_handler;
3623 goto skip_app_handler;
3627 goto skip_app_handler;
3633 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3634 max_block_szx < block.
aszx) {
3635 block.
aszx = max_block_szx;
3666 LL_FOREACH(session->lg_srcv, sg) {
3667 if (lg_srcv == sg) {
3673 goto skip_app_handler;
3676 if (lg_srcv->last_token) {
3677 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3685#if COAP_Q_BLOCK_SUPPORT
3690 lg_srcv->no_more_seen = 1;
3704#if COAP_Q_BLOCK_SUPPORT
3706 request_missing_blocks(session, lg_srcv, block.
num);
3710 goto skip_app_handler;
3720 if (lg_srcv->observe_set) {
3722 lg_srcv->observe_length, lg_srcv->observe);
3725 if (lg_srcv->body_data) {
3734 if (USE_BLOCK_DATA_HANDLER) {
3743 lg_srcv->dont_timeout = 1;
3744 *pfree_lg_srcv = lg_srcv;
3747 if (lg_srcv_is_refed)
3748 coap_lg_srcv_release_lkd(session, lg_srcv);
3752 LL_DELETE(session->lg_srcv, lg_srcv);
3753 coap_block_delete_lg_srcv(session, lg_srcv);
3756 if (lg_srcv_is_refed)
3757 coap_lg_srcv_release_lkd(session, lg_srcv);
3762#if COAP_CLIENT_SUPPORT
3763#if COAP_Q_BLOCK_SUPPORT
3765derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3766 uint32_t value = **bp & 0x1f;
3770 }
else if (value == 24) {
3772 return (uint32_t)-1;
3776 }
else if (value == 25) {
3778 return (uint32_t)-1;
3786 return (uint32_t)-1;
3787 value = (uint32_t)(**bp) << 24;
3789 value |= **bp << 16;
3807 if (sent || lg_xmit || lg_crcv) {
3811 const uint8_t *data;
3821 }
else if (lg_xmit) {
3824 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3825 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3828 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3829 lg_xmit->data_info->length - offset;
3832 sent = lg_crcv->sent_pdu;
3841 ++lg_crcv->retry_counter);
3855 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3856#if COAP_OSCORE_SUPPORT
3857 if (session->oscore_encryption &&
3864 lg_crcv->state_token = token;
3914 int lg_crcv_is_refed = 0;
3919 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3922 lg_crcv = coap_find_lg_crcv(session, rcvd);
3925 coap_lg_crcv_reference_lkd(lg_crcv);
3926 lg_crcv_is_refed = 1;
3936 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3937 1 << (block.
szx + 4), block.
num);
3943 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3949 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
3950 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3951#if COAP_Q_BLOCK_SUPPORT
3952 lg_xmit->send_blocks.range[0].begin = block.
num;
3953 lg_xmit->send_blocks.range[0].end = (uint32_t)(lg_xmit->
data_info->
length / chunk);
3956 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3957 1 << (block.
szx + 4), block.
num);
3962 track_echo(session, rcvd);
3963#if COAP_Q_BLOCK_SUPPORT
3966 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
3967 goto skip_app_handler;
3982 goto skip_app_handler;
3987 if ((block.
num + 1) * chunk < lg_xmit->data_info->length) {
4003 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
4006 assert(len <=
sizeof(buf));
4008 memcpy(buf, new_token->
s, len);
4027 offset = block.
num * chunk;
4029 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
4032 ((pdu->
max_size - token_options) /1024) * 1024;
4044#if COAP_CONSTRAINED_STACK
4046 static uint8_t l_data[1024];
4048 uint8_t l_data[1024];
4052 assert(chunk <= 1024);
4054 block.
num * chunk, l_data, &l_length,
4069#if COAP_Q_BLOCK_SUPPORT
4072 if (coap_send_q_block1(session, block, pdu,
4075 goto skip_app_handler;
4082 goto skip_app_handler;
4092 int observe_action = -1;
4095 goto lg_xmit_finished;
4105 goto lg_xmit_finished;
4111 goto call_app_handler;
4113 goto lg_xmit_finished;
4115 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
4116 goto skip_app_handler;
4117#if COAP_Q_BLOCK_SUPPORT
4122 goto skip_app_handler;
4126 const uint8_t *data;
4131 uint16_t fmt = fmt_opt ?
4141 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
4142 goto skip_app_handler;
4147 const uint8_t *bp = data;
4150 for (i = 0; (bp < data + length) &&
4152 if ((*bp & 0xc0) != 0x00)
4154 block.
num = derive_cbor_value(&bp, data + length - bp);
4156 if (block.
num > (1 << 20) -1)
4158 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4163 if (lg_xmit->send_blocks.used) {
4165 block.
num = lg_xmit->send_blocks.range[0].begin;
4166 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4167 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
4169 goto skip_app_handler;
4172 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4175 goto lg_xmit_finished;
4177 goto call_app_handler;
4189 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4200 lg_crcv->sent_pdu->lg_xmit = 0;
4212 LL_DELETE(session->
lg_xmit, lg_xmit);
4216 if (lg_crcv_is_refed)
4217 coap_lg_crcv_release_lkd(session, lg_crcv);
4221 if (lg_crcv_is_refed)
4222 coap_lg_crcv_release_lkd(session, lg_crcv);
4235 const uint8_t *data,
size_t offset,
size_t total) {
4248 const uint8_t *data,
size_t offset,
size_t total) {
4251 if (body_data ==
NULL && total) {
4254 if (body_data ==
NULL)
4258 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4264 if (offset + length <= total && body_data->length >= total) {
4265 memcpy(&body_data->
s[offset], data, length);
4281 memcpy(&body_data->
s[offset], data, length);
4290#if COAP_CLIENT_SUPPORT
4310#if COAP_Q_BLOCK_SUPPORT
4314 uint16_t block_opt = 0;
4316 int ack_rst_sent = 0;
4320 memset(&block, 0,
sizeof(block));
4321#if COAP_Q_BLOCK_SUPPORT
4322 memset(&qblock, 0,
sizeof(qblock));
4324 lg_crcv = coap_find_lg_crcv(session, rcvd);
4332 const uint8_t *data;
4335 size_t size2 = size_opt ?
4347#if COAP_Q_BLOCK_SUPPORT
4350 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4356 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4361 track_echo(session, rcvd);
4362 if (have_block && (block.
m || length)) {
4366 uint16_t fmt = fmt_opt ?
4373 size_t saved_offset;
4386 goto expire_lg_crcv;
4389 chunk = (size_t)1 << (block.
szx + 4);
4390 offset = block.
num * chunk;
4391 if (size2 < (offset + length)) {
4393 size2 = offset + length + 1;
4395 size2 = offset + length;
4397 saved_offset = offset;
4399 if (lg_crcv->initial) {
4400#if COAP_Q_BLOCK_SUPPORT
4403 lg_crcv->initial = 0;
4404 if (lg_crcv->body_data) {
4406 lg_crcv->body_data =
NULL;
4410 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4411 lg_crcv->etag_set = 1;
4413 lg_crcv->etag_set = 0;
4415 lg_crcv->total_len = size2;
4416 lg_crcv->content_format = fmt;
4417 lg_crcv->szx = block.
szx;
4418 lg_crcv->block_option = block_opt;
4419 lg_crcv->last_type = rcvd->
type;
4420 lg_crcv->rec_blocks.used = 0;
4421 lg_crcv->rec_blocks.total_blocks = 0;
4422#if COAP_Q_BLOCK_SUPPORT
4423 lg_crcv->rec_blocks.processing_payload_set = 0;
4426 if (lg_crcv->total_len < size2)
4427 lg_crcv->total_len = size2;
4431 uint8_t max_block_szx;
4434 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4435 max_block_szx = block.
szx;
4437 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4441 (size2 > max_body)) {
4444 if (max_body_size == 0 || max_body < max_body_size) {
4445 max_body_size = max_body;
4457 lg_crcv->etag, lg_crcv->etag_length)) {
4461 ++lg_crcv->retry_counter);
4465#if COAP_Q_BLOCK_SUPPORT
4470 coap_log_warn(
"Data body updated during receipt - new request started\n");
4474 lg_crcv->initial = 1;
4476 lg_crcv->body_data =
NULL;
4489 (0 << 4) | (0 << 3) | block.
aszx),
4495 goto skip_app_handler;
4497 }
else if (lg_crcv->etag_set) {
4503 if (fmt != lg_crcv->content_format) {
4507#if COAP_Q_BLOCK_SUPPORT
4513 if (block.
num == 0) {
4519 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4520 lg_crcv->observe_set = 1;
4522 lg_crcv->observe_set = 0;
4526 while (offset < saved_offset + length) {
4528#if COAP_Q_BLOCK_SUPPORT
4532 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4533 1 << (block.
szx + 4), block.
num);
4534#if COAP_Q_BLOCK_SUPPORT
4536 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4537 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4538 coap_request_missing_q_block2(session, lg_crcv);
4540 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4552 offset = block.
num << (block.
szx + 4);
4558 if (updated_block) {
4565 if (size2 < saved_offset + length) {
4566 size2 = saved_offset + length;
4573 &lg_crcv->body_data,
4575 saved_offset, size2));
4581 saved_offset, size2);
4582 if (lg_crcv->body_data ==
NULL) {
4595#if COAP_Q_BLOCK_SUPPORT
4601 if (check_all_blocks_in_for_payload_set(session,
4602 &lg_crcv->rec_blocks)) {
4603 block.
num = lg_crcv->rec_blocks.range[0].end;
4605 lg_crcv->rec_blocks.processing_payload_set =
4607 if (check_any_blocks_next_payload_set(session,
4608 &lg_crcv->rec_blocks)) {
4610 coap_request_missing_q_block2(session, lg_crcv);
4611 goto skip_app_handler;
4616 goto skip_app_handler;
4620 goto skip_app_handler;
4644 ((block.
num + 1) << 4) |
4645 (block.
m << 3) | block.
aszx),
4650 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4655 goto skip_app_handler;
4658 goto skip_app_handler;
4663#if COAP_Q_BLOCK_SUPPORT
4665 lg_crcv->total_len : size2;
4674 if (lg_crcv->app_token)
4676 lg_crcv->app_token->s);
4679 goto call_app_handler;
4681#if COAP_Q_BLOCK_SUPPORT
4687 if (lg_crcv->observe_set) {
4689 lg_crcv->observe_length, lg_crcv->observe);
4691 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4692#if COAP_Q_BLOCK_SUPPORT
4701 lg_crcv->total_len : saved_offset + length;
4709#if COAP_Q_BLOCK_SUPPORT
4711 lg_crcv->total_len : size2;
4724 if (lg_crcv->app_token)
4726 lg_crcv->app_token->s);
4729 body_free = lg_crcv->body_data;
4730 lg_crcv->body_data =
NULL;
4734 if (lg_crcv->observe_set == 0) {
4736 LL_DELETE(session->lg_crcv, lg_crcv);
4737 coap_block_delete_lg_crcv(session, lg_crcv);
4738 goto skip_app_handler;
4741 lg_crcv->initial = 1;
4744 goto skip_app_handler;
4759 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4760 lg_crcv->observe_set = 1;
4762 lg_crcv->observe_set = 0;
4771 goto expire_lg_crcv;
4776#if COAP_OSCORE_SUPPORT
4777 if (check_freshness(session, rcvd,
4778 (session->oscore_encryption == 0) ? sent :
NULL,
4781 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4783 goto skip_app_handler;
4784 goto expire_lg_crcv;
4793 const uint8_t *data;
4800 ++lg_crcv->retry_counter);
4808#if COAP_PROXY_SUPPORT
4809 coap_proxy_req_t *proxy_req = session->
context->proxy_list_count ?
4810 coap_proxy_map_outgoing_request(session, rcvd,
NULL) :
NULL;
4816 proxy_req->token_used =
new;
4817 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"upd");
4821 coap_log_debug(
"* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
4825 goto skip_app_handler;
4828 goto expire_lg_crcv;
4831 goto expire_lg_crcv;
4833 if (!block.
m && !lg_crcv->observe_set) {
4843 lg_crcv->last_used -= adjust;
4857#
if COAP_Q_BLOCK_SUPPORT
4864 goto skip_app_handler;
4867 const uint8_t *data;
4871#if COAP_Q_BLOCK_SUPPORT
4872 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4878 if (block.
num != 0) {
4880 size_t chunk = (size_t)1 << (block.
szx + 4);
4885 goto call_app_handler;
4888#if COAP_Q_BLOCK_SUPPORT
4892 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4898 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4901 LL_PREPEND(session->lg_crcv, lg_crcv);
4902 return coap_handle_response_get_block(context, session, sent, rcvd,
4915 track_echo(session, rcvd);
4917 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4920 LL_PREPEND(session->lg_crcv, lg_crcv);
4921 return coap_handle_response_get_block(context, session, sent, rcvd,
4933 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4936 LL_PREPEND(session->lg_crcv, lg_crcv);
4937 return coap_handle_response_get_block(context, session, sent, rcvd,
4955 if (lg_crcv->app_token)
4957 lg_crcv->app_token->s);
4961 LL_DELETE(session->lg_crcv, lg_crcv);
4962 coap_block_delete_lg_crcv(session, lg_crcv);
4974#if COAP_SERVER_SUPPORT
4983 if (response->
code == 0)
4985 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4993#if COAP_CLIENT_SUPPORT
4996 uint64_t token_match =
5002 if (session->lg_crcv) {
5003 LL_FOREACH(session->lg_crcv, lg_crcv) {
5008 lg_crcv->app_token->s);
5016 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 blocks_add_entry(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)
#define COAP_LG_XMIT_TXT_SCALAR
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...
#define COAP_OPTION_CONTENT_TYPE
@ COAP_OPTION_CONTENT_FORMAT
@ COAP_OPTION_URI_PATH_ABB
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_BLOCK_MAX_SIZE_MASK
#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.
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
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_pdu_code_t
Set of codes available for a PDU.
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.
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.
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.
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
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.
coap_lg_xmit_data_t * data_info
Pointer to large data information.
union coap_lg_xmit_t::@025301310216224123377060072066277301055061031376 b
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.
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.
uint8_t is_rate_limiting
Currently NON rate limiting.
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