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);
39#if COAP_Q_BLOCK_SUPPORT
85 if (block->
szx == 7) {
97 if (block->
m && (length % 1024) != 0) {
99 length - (length % 1024), length);
100 length -= length % 1024;
133 block->
m = block_b.
m;
143 unsigned int blk_size,
size_t total) {
145 size_t avail = pdu->
max_size - token_options;
146 unsigned int start = num << (blk_size + 4);
147 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
149 assert(start <= total);
150 memset(block, 0,
sizeof(*block));
152 block->
szx = block->
aszx = blk_size;
153 if (can_use_bert && blk_size == 6 && avail >= 1024 && session !=
NULL &&
158 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
160 block->
chunk_size = (size_t)1 << (blk_size + 4);
161 if (avail < block->chunk_size && (total - start) >= avail) {
167 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
170 new_blk_size =
coap_flsll((
long long)avail) - 5;
173 block->
szx = new_blk_size;
174 block->
num <<= szx - block->
szx;
175 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
186 unsigned char buf[4];
191 start = block->
num << (block->
szx + 4);
192 if (block->
num != 0 && data_length <= start) {
202 block->
szx, data_length))
207 ((block_b.
num << 4) |
220 unsigned char buf[4];
224 start = block->
num << (block->
szx + 4);
225 if (block->
num != 0 && data_length <= start) {
233 block->
szx, data_length))
248 unsigned int block_num,
unsigned char block_szx) {
250 start = block_num << (block_szx + 4);
256 min(len - start, ((
size_t)1 << (block_szx + 4))),
263 unsigned int start = block->
num << (block->
szx + 4);
271 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
273 max_size = (size_t)1 << (block->
szx + 4);
278 min(len - start, max_size),
293 unsigned char buf[4];
295 int block2_requested = 0;
296#if COAP_SERVER_SUPPORT
298 coap_digest_t digest;
299 coap_digest_ctx_t *dctx =
NULL;
302 memset(&block2, 0,
sizeof(block2));
309 block2_requested = 1;
310 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
313 length >> (block2.
szx + 4));
321#if COAP_SERVER_SUPPORT
324 dctx = coap_digest_setup();
327 if (request && request->
session &&
331 if (!coap_digest_update(dctx, data, length))
333 if (!coap_digest_final(dctx, &digest))
336 memcpy(&etag, digest.key,
sizeof(etag));
337#if COAP_ETAG_MAX_BYTES != 8
360 if (block2_requested) {
413#if COAP_SERVER_SUPPORT
414 coap_digest_free(dctx);
423 uint32_t block_mode) {
431 uint32_t block_mode) {
437#if ! COAP_Q_BLOCK_SUPPORT
445 size_t max_block_size) {
456 uint32_t max_blk_szx_plus1;
458 switch (max_block_size) {
469 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
475 max_blk_szx_plus1 =
coap_fls((uint32_t)max_block_size >> 4) & 0x07;
483 const uint8_t *b,
size_t blen) {
484 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
491 uint64_t token_match =
495 LL_FOREACH(session->
lg_xmit, lg_xmit) {
501#if COAP_CLIENT_SUPPORT
532 LL_PREPEND(session->
lg_xmit, lg_xmit);
537#if COAP_CLIENT_SUPPORT
541 lg_crcv = coap_find_lg_crcv(session, pdu);
543 lg_xmit->b.b1.state_token = lg_crcv->state_token;
555#if COAP_CLIENT_SUPPORT
563 ret = coap_cancel_observe_lkd(session, token, type);
579 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
584 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
585 if (lg_crcv->observe_set) {
586 if ((!token && !lg_crcv->app_token->length) || (token &&
592#if COAP_Q_BLOCK_SUPPORT
598 lg_crcv->obs_token[0] ?
599 lg_crcv->obs_token[0] :
609 lg_crcv->observe_set = 0;
619 if (lg_crcv->o_block_option) {
622 lg_crcv->o_blk_size),
625 if (lg_crcv->obs_data && lg_crcv->obs_data->get_func) {
626 coap_add_data_large_request_app_lkd(session, pdu,
627 lg_crcv->obs_data->length,
629 lg_crcv->obs_data->get_func,
630 lg_crcv->obs_data->app_ptr);
631 }
else if (lg_crcv->obs_data) {
632 coap_add_data_large_request_lkd(session, pdu,
633 lg_crcv->obs_data->length,
634 lg_crcv->obs_data->data,
NULL,
NULL);
636 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
647#if COAP_Q_BLOCK_SUPPORT
649 if (using_q_block1) {
650 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
669 uint64_t token_match =
673 LL_FOREACH(session->lg_crcv, lg_crcv) {
691 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
693 if (m_lg_crcv->obs_data) {
694 m_lg_crcv->obs_data->ref++;
695 lg_crcv->obs_data = m_lg_crcv->obs_data;
697 LL_PREPEND(session->lg_crcv, lg_crcv);
703#if COAP_OSCORE_SUPPORT
717 lg_crcv = coap_find_lg_crcv(session, pdu);
724 ++lg_crcv->retry_counter);
736 if (!lg_crcv->obs_token) {
738 if (!lg_crcv->obs_token) {
741 lg_crcv->obs_token_cnt = 1;
746 if (lg_crcv->obs_token[0] ==
NULL)
766#if COAP_SERVER_SUPPORT
783 LL_FOREACH(session->
lg_xmit, lg_xmit) {
800 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
833 size_t offset,
size_t length) {
845 if (get_func(session, length, offset, l_data, &l_length, app_ptr))
867#if COAP_Q_BLOCK_SUPPORT
873 int have_block_defined = 0;
877 size_t token_options;
880#if COAP_Q_BLOCK_SUPPORT
884#if !COAP_SERVER_SUPPORT
890 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
898 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
912#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
913#if UINT_MAX < MAX_BLK_LEN
915#define MAX_BLK_LEN UINT_MAX
923#if COAP_SERVER_SUPPORT
929 coap_digest_t digest;
930 coap_digest_ctx_t *dctx = coap_digest_setup();
936 if (coap_digest_update(dctx, data, length)) {
937 if (coap_digest_final(dctx, &digest)) {
938 memcpy(&etag, digest.key,
sizeof(etag));
939#if COAP_ETAG_MAX_BYTES != 8
945 coap_digest_free(dctx);
957 if (opt && match_request_etag(pdu, opt, &opt_iter)) {
972#if COAP_Q_BLOCK_SUPPORT
988 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
993#if COAP_CLIENT_SUPPORT
996 LL_DELETE(session->
lg_xmit, lg_xmit);
999 if (!is_mcast && !last_all_sent)
1007#if COAP_Q_BLOCK_SUPPORT
1008 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
1021#if COAP_SERVER_SUPPORT
1026 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
1030 LL_DELETE(session->
lg_xmit, lg_xmit);
1038#if COAP_OSCORE_SUPPORT
1039 if (session->oscore_encryption) {
1047 avail = pdu->
max_size - token_options;
1050#if COAP_OSCORE_SUPPORT
1059 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1066 if (max_blk_szx != -1 && blk_size > max_blk_szx)
1067 blk_size = max_blk_szx;
1071 if (block.
szx < blk_size)
1072 blk_size = block.
szx;
1073 have_block_defined = 1;
1075#if COAP_Q_BLOCK_SUPPORT
1078 if (have_block_defined) {
1080 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1084 if (block.
szx < blk_size)
1085 blk_size = block.
szx;
1086 have_block_defined = 1;
1087 option = alt_option;
1092 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1094 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1098 chunk = (size_t)1 << (blk_size + 4);
1099 if ((have_block_defined && block.
num != 0) || single_request ||
1104 if (length >= block.
num * chunk) {
1105#if COAP_SERVER_SUPPORT
1111 (
unsigned int)length),
1125 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1130 if (chunk > length - block.
num * chunk)
1131 rem = length - block.
num * chunk;
1134 block.
num * chunk, rem))
1144 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1169 lg_xmit->blk_size = blk_size;
1170 lg_xmit->option = option;
1172 if (!lg_xmit->data_info)
1174 lg_xmit->data_info->ref = 0;
1175 lg_xmit->data_info->data = data;
1176 lg_xmit->data_info->length = length;
1177#if COAP_Q_BLOCK_SUPPORT
1178 lg_xmit->non_timeout_random_ticks =
1181 lg_xmit->data_info->get_func = get_func;
1182 lg_xmit->data_info->release_func = release_func;
1183 lg_xmit->data_info->app_ptr = app_ptr;
1190 if (!lg_xmit->b.b1.app_token)
1198 lg_xmit->b.b1.count = 1;
1200 lg_xmit->b.b1.count);
1209 (
unsigned int)length),
1222 lg_xmit->b.b2.resource = resource;
1225 if (lg_xmit->b.b2.query) {
1226 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1229 lg_xmit->b.b2.query =
NULL;
1234 sizeof(lg_xmit->b.b2.rtag));
1235 memcpy(lg_xmit->b.b2.rtag,
coap_opt_value(opt), lg_xmit->b.b2.rtag_length);
1236 lg_xmit->b.b2.rtag_set = 1;
1238 lg_xmit->b.b2.rtag_set = 0;
1240 lg_xmit->b.b2.request_method = request_method;
1249 lg_xmit->b.b2.maxage_expire = 0;
1254 (
unsigned int)length),
1259 blk_size, lg_xmit->data_info->length))
1266 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1274 avail = pdu->
max_size - token_options;
1279#if COAP_OSCORE_SUPPORT
1282 if (avail < (ssize_t)chunk) {
1285 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1288 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1289 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1290 lg_xmit->blk_size = blk_size;
1291 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1297 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1300#if COAP_Q_BLOCK_SUPPORT
1302 lg_xmit->send_blocks.used = 1;
1303 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->data_info->length - 1) / chunk);
1307 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1308 rem = lg_xmit->data_info->length - block.
num * chunk;
1310#if COAP_CONSTRAINED_STACK
1312 static uint8_t l_data[1024];
1313 uint8_t *data_buf = l_data;
1315 uint8_t l_data[1024];
1316 uint8_t *data_buf = l_data;
1319 int data_buf_allocated = 0;
1321 if (rem >
sizeof(l_data)) {
1327 data_buf_allocated = 1;
1329 if (get_func(session, rem, block.
num * chunk, data_buf, &l_length,
1330 lg_xmit->data_info->app_ptr)) {
1332 if (data_buf_allocated)
1337 if (data_buf_allocated)
1345 lg_xmit->b.b1.bert_size = rem;
1347 lg_xmit->last_block = -1;
1350 LL_PREPEND(session->
lg_xmit,lg_xmit);
1353 if (have_block_defined) {
1357 (0 << 4) | (0 << 3) | blk_size), buf);
1362 (
unsigned int)length),
1368 (
unsigned int)length),
1390 }
else if (release_func) {
1396#if COAP_CLIENT_SUPPORT
1401 const uint8_t *data,
1408 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1409 release_func, app_ptr);
1418 const uint8_t *data,
1432 length, data, release_func,
NULL, app_ptr, 0, 0);
1445 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1446 release_func, get_func, app_ptr);
1466 length,
NULL, release_func, get_func,
1471#if COAP_SERVER_SUPPORT
1478 uint16_t media_type,
1482 const uint8_t *data,
1488 ret = coap_add_data_large_response_lkd(resource, session, request,
1489 response, query, media_type, maxage, etag,
1490 length, data, release_func, app_ptr);
1501 uint16_t media_type,
1505 const uint8_t *data,
1509 unsigned char buf[4];
1511 int block_requested = 0;
1512 int single_request = 0;
1513#if COAP_Q_BLOCK_SUPPORT
1514 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1520 memset(&block, 0,
sizeof(block));
1527 block_requested = 1;
1528 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1531 length >> (block.
szx + 4));
1536#if COAP_Q_BLOCK_SUPPORT
1538 block_requested = 1;
1539 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1542 length >> (block.
szx + 4));
1546 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1567 if (block_requested) {
1591 query, etag, length, data,
1592 release_func,
NULL, app_ptr, single_request,
1595 goto error_released;
1605#if COAP_ERROR_PHRASE_LENGTH > 0
1623#if COAP_Q_BLOCK_SUPPORT
1633 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1637 LL_DELETE(session->
lg_xmit, lg_xmit);
1641 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1647 if (lg_xmit->
last_sent + partial_timeout <= now) {
1651#if COAP_CLIENT_SUPPORT
1655 LL_DELETE(session->
lg_xmit, lg_xmit);
1658 if (!is_mcast && !last_all_sent)
1662 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1663 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1672#if COAP_CLIENT_SUPPORT
1673#if COAP_Q_BLOCK_SUPPORT
1678 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1690 pdu->
type = lg_crcv->last_type;
1702 int block_payload_set = -1;
1713 if (lg_crcv->rec_blocks.used &&
1714 (lg_crcv->rec_blocks.used < 2 ||
1717 block = lg_crcv->rec_blocks.range[0].end + 1;
1718 block_size = (size_t)1 << (lg_crcv->szx + 4);
1719 sofar = block * block_size;
1720 if (sofar < lg_crcv->total_len) {
1723 pdu = coap_build_missing_pdu(session, lg_crcv);
1729 (block << 4) | (1 << 3) | lg_crcv->szx),
1737 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1738 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1739 lg_crcv->rec_blocks.range[i].begin != 0) {
1742 pdu = coap_build_missing_pdu(session, lg_crcv);
1747 if (block_payload_set == -1)
1749 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1753 (block << 4) | (0 << 3) | lg_crcv->szx),
1757 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1758 block = lg_crcv->rec_blocks.range[i].end;
1761 block_size = (size_t)1 << (lg_crcv->szx + 4);
1762 sofar = (block + 1) * block_size;
1763 if (sofar < lg_crcv->total_len) {
1766 pdu = coap_build_missing_pdu(session, lg_crcv);
1770 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1772 if (block_payload_set == -1)
1774 for (; block < (ssize_t)sofar &&
1778 (block << 4) | (0 << 3) | lg_crcv->szx),
1785 lg_crcv->rec_blocks.retry++;
1786 if (block_payload_set != -1)
1787 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1802#if COAP_Q_BLOCK_SUPPORT
1808#if COAP_Q_BLOCK_SUPPORT
1810 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1816 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1820#if COAP_Q_BLOCK_SUPPORT
1822 size_t scaled_timeout = receive_timeout *
1823 ((size_t)1 << lg_crcv->rec_blocks.retry);
1831 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1834 coap_request_missing_q_block2(session, lg_crcv);
1836 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1837 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1845 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1846 lg_crcv->last_used + partial_timeout <= now) {
1847#if COAP_Q_BLOCK_SUPPORT
1851 LL_DELETE(session->lg_crcv, lg_crcv);
1852 coap_block_delete_lg_crcv(session, lg_crcv);
1853 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1855 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1856 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1865#if COAP_SERVER_SUPPORT
1866#if COAP_Q_BLOCK_SUPPORT
1878 if (lg_srcv->last_token)
1879 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1890add_408_block(
coap_pdu_t *pdu, uint32_t block) {
1894 assert(block < (1U << 20));
1897 if (block >= (1U << 20)) {
1899 }
else if (block < 24) {
1901 val[0] = (uint8_t)block;
1902 }
else if (block < 0x100) {
1905 val[1] = (uint8_t)block;
1906 }
else if (block < 0x10000) {
1909 val[1] = (uint8_t)((block >> 8) & 0xff);
1910 val[2] = (uint8_t)(block & 0xff);
1914 val[1] = (uint8_t)((block >> 24) & 0xff);
1915 val[2] = (uint8_t)((block >> 16) & 0xff);
1916 val[3] = (uint8_t)((block >> 8) & 0xff);
1917 val[4] = (uint8_t)(block & 0xff);
1933 for (i = 0; i < rec_blocks->
used; i++) {
1934 if (block_num < rec_blocks->range[i].begin)
1936 if (block_num <= rec_blocks->range[i].end)
1942#if COAP_SERVER_SUPPORT
1944check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1945 if (rec_blocks->
used == 0) {
1946 return block_num == 0 ? 1 : 0;
1948 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1965 for (i = 0; i < rec_blocks->
used; i++) {
1966 if (block < rec_blocks->range[i].begin)
1968 if (block < rec_blocks->range[i].end)
1974#if COAP_CLIENT_SUPPORT
1975#if COAP_Q_BLOCK_SUPPORT
1979 if (rec_blocks->
used &&
1981 rec_blocks->processing_payload_set)
1989 if (rec_blocks->
used > 1 &&
1991 rec_blocks->processing_payload_set)
1998#if COAP_SERVER_SUPPORT
1999#if COAP_Q_BLOCK_SUPPORT
2029 uint32_t inc_to_block) {
2032 uint32_t block_size = (uint32_t)1 << (lg_srcv->szx + 4);
2033 uint32_t first_missing;
2034 uint32_t missing_payload;
2035 uint32_t last_payload_block;
2038 uint32_t end_block = ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1;
2042 (int32_t)(inc_to_block /
COAP_MAX_PAYLOADS(session)) == lg_srcv->r_m_payload_set)
2045 if (inc_to_block != end_block) {
2049 if (lg_srcv->rec_blocks.range[0].begin != 0) {
2052 first_missing = lg_srcv->rec_blocks.range[0].end + 1;
2054 if (first_missing > end_block)
2058 if (inc_to_block != end_block && inc_to_block > last_payload_block) {
2059 inc_to_block = last_payload_block;
2063 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
2064 if (block < (int32_t)lg_srcv->rec_blocks.range[i].begin &&
2065 lg_srcv->rec_blocks.range[i].begin != 0) {
2068 pdu = pdu_408_build(session, lg_srcv);
2073 for (; block < (int32_t)lg_srcv->rec_blocks.range[i].begin; block++) {
2074 if (!add_408_block(pdu, block)) {
2080 if (block < (int32_t)lg_srcv->rec_blocks.range[i].end) {
2081 block = lg_srcv->rec_blocks.range[i].end;
2084 if (inc_to_block == end_block && block < (int32_t)inc_to_block) {
2087 pdu = pdu_408_build(session, lg_srcv);
2092 for (; block <= (int32_t)inc_to_block && count <
COAP_MAX_PAYLOADS(session); block++) {
2093 if (!add_408_block(pdu, block)) {
2102 lg_srcv->rec_blocks.retry++;
2118#if COAP_Q_BLOCK_SUPPORT
2124#if COAP_Q_BLOCK_SUPPORT
2126 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
2132 if (session->
context->lg_srcv_cnt > 5) {
2137 partial_timeout /= (session->
context->lg_srcv_cnt - 5);
2143 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
2144 if (lg_srcv->dont_timeout) {
2151#if COAP_Q_BLOCK_SUPPORT
2153 size_t scaled_timeout = receive_timeout *
2154 ((size_t)1 << lg_srcv->rec_blocks.retry);
2160 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
2161 uint32_t block_size = (size_t)1 << (lg_srcv->szx + 4);
2164 if (!coap_request_missing_q_block1(session, lg_srcv,
2165 ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1))
2168 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2169 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2176 if (lg_srcv->no_more_seen)
2178 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2179#if COAP_Q_BLOCK_SUPPORT
2196 if (lg_srcv->last_token)
2197 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2199 (
const uint8_t *)
"Missing interim block");
2203 LL_DELETE(session->lg_srcv, lg_srcv);
2204 coap_block_delete_lg_srcv(session, lg_srcv);
2205 }
else if (lg_srcv->last_used) {
2207 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2208 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2217#if COAP_Q_BLOCK_SUPPORT
2223 uint32_t delayqueue_cnt,
2225 const uint8_t *ptoken;
2227 size_t ltoken_length;
2231 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2239 block->
num = lg_xmit->send_blocks.range[0].begin;
2241 offset = block->
num * chunk;
2258 (lg_xmit->send_blocks.used > 1 ||
2271 coap_log_debug(
"Q-Block: %s block %d\n", is_first ?
"First" :
"Next", block->
num);
2292 ((block->
num) << 4) |
2313 coap_send_pdu_t send_pdu) {
2316 uint32_t delayqueue_cnt = 0;
2326 if (send_pdu == COAP_SEND_INC_PDU) {
2332 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2345 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2346 LL_FOREACH(session->
delayqueue, delayqueue) {
2353 if (send_pdu == COAP_SEND_INC_PDU) {
2358 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2374 block_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, pdu, delayqueue_cnt, 1);
2379 while (block_pdu && lg_xmit->send_blocks.used) {
2383#if COAP_CONSTRAINED_STACK
2385 static uint8_t l_data[1024];
2387 uint8_t l_data[1024];
2390 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2393 assert(chunk <= 1024);
2395 block.
num * chunk, l_data, &l_length,
2420 blocks_delete_entry(&lg_xmit->send_blocks, block.
num);
2425 t_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, block_pdu, delayqueue_cnt, 0);
2456#if COAP_CLIENT_SUPPORT
2469 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2470 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2474 if (now <= non_timeout) {
2476 *tim_rem = non_timeout - now;
2481 timed_out = now - non_timeout;
2483 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2487 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2490 coap_log_debug(
"Q-Block: Timeout - start next MAX_PAYLOADS_SET\n");
2491 memset(&block, 0,
sizeof(block));
2492 block.
num = lg_xmit->send_blocks.range[0].begin;
2493 offset = block.
num * chunk;
2496 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2497 if (*tim_rem > non_timeout) {
2498 *tim_rem = non_timeout;
2512 LL_DELETE(session->
lg_xmit, lg_xmit);
2527#if COAP_SERVER_SUPPORT
2540 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2541 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2543 if (now <= non_timeout) {
2545 *tim_rem = non_timeout - now;
2550 timed_out = now - non_timeout;
2552 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2556 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2559 memset(&block, 0,
sizeof(block));
2560 block.
num = lg_xmit->send_blocks.range[0].begin;
2561 offset = block.
num * chunk;
2565 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2566 if (*tim_rem > non_timeout) {
2567 *tim_rem = non_timeout;
2581 LL_DELETE(session->
lg_xmit, lg_xmit);
2585 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2597#if COAP_CLIENT_SUPPORT
2611 if (opt && lg_crcv) {
2612 int observe_action = -1;
2619 if (lg_crcv->obs_token_cnt <= block_num) {
2623 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2626 lg_crcv->obs_token = tmp;
2627 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2628 lg_crcv->obs_token[i] =
NULL;
2633 if (lg_crcv->obs_token_cnt <= block_num)
2634 lg_crcv->obs_token_cnt = block_num + 1;
2637 if (lg_crcv->obs_token[block_num] ==
NULL)
2641 if (block_num < lg_crcv->obs_token_cnt) {
2642 return lg_crcv->obs_token[block_num];
2649#if COAP_Q_BLOCK_SUPPORT
2654 coap_send_pdu_t send_request) {
2657 uint64_t token_match =
2661 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2670 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2675#if COAP_SERVER_SUPPORT
2676#if COAP_Q_BLOCK_SUPPORT
2687 coap_send_pdu_t send_response) {
2692 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2700 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2710 if (data_info->
ref > 0) {
2722#if COAP_CLIENT_SUPPORT
2723#if COAP_Q_BLOCK_SUPPORT
2756 11, (
const uint8_t *)
".well-known");
2758 4, (
const uint8_t *)
"core");
2771 (0 << 4) | (0 << 3) | 0),
2796 if (lg_crcv ==
NULL)
2799 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2803 lg_crcv->initial = 1;
2821 const uint8_t *data;
2824 if (data_len < lg_xmit->data_info->length) {
2835 if (!lg_crcv->app_token) {
2836 coap_block_delete_lg_crcv(session, lg_crcv);
2842 lg_crcv->retry_counter = 1;
2843 lg_crcv->state_token = state_token;
2850 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2858 lg_crcv->o_blk_size = block.
aszx;
2869#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2872 if (lg_crcv ==
NULL)
2875 if (lg_crcv->ref > 0) {
2881 if (lg_crcv->obs_data) {
2883 lg_crcv->obs_data =
NULL;
2889 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2898#if COAP_SERVER_SUPPORT
2902 if (lg_srcv ==
NULL)
2905 if (lg_srcv->ref > 0) {
2912 if (lg_srcv->body_data)
2913 session->
context->cur_bodies_ram -= lg_srcv->body_data->length;
2918 session->
context->lg_srcv_cnt--;
2925 if (lg_xmit ==
NULL)
2928 if (lg_xmit->
ref > 0) {
2945#if COAP_SERVER_SUPPORT
2952add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2953 uint32_t *count, uint32_t max_count) {
2956 for (i = 0; i < *count && *count < max_count; i++) {
2957 if (num == out_blocks[i].num)
2959 else if (num < out_blocks[i].num) {
2960 memmove(&out_blocks[i+1], &out_blocks[i],
2961 (*count - i) *
sizeof(out_blocks[0]));
2962 out_blocks[i].num = num;
2963 out_blocks[i].is_continue = is_continue;
2968 if (*count < max_count) {
2969 out_blocks[i].num = num;
2970 out_blocks[i].is_continue = is_continue;
3003 uint16_t block_opt = 0;
3004 send_track *out_blocks =
NULL;
3005 const char *error_phrase;
3010 uint32_t request_cnt, i;
3013#if COAP_Q_BLOCK_SUPPORT
3018 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
3025 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
3026 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
3027 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
3029 goto skip_app_handler;
3036 if (block.
num == 0) {
3037#if COAP_Q_BLOCK_SUPPORT
3047 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
3048 if (lg_xmit ==
NULL)
3051#if COAP_Q_BLOCK_SUPPORT
3057 goto internal_issue;
3063 if (etag_opt && !match_request_etag(lg_xmit->
sent_pdu, etag_opt, &opt_iter)) {
3071 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3074 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
3075 block.
num, block.
m);
3077 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
3078 1 << (block.
szx + 4), block.
num, block.
m);
3081 if (block.
num == 0) {
3087 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
3088 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3089#if COAP_Q_BLOCK_SUPPORT
3090 lg_xmit->send_blocks.range[0].begin = block.
num;
3091 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->
data_info->
length - 1) / chunk);
3094 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3095 (1 << (block.
szx + 4)), block.
num);
3097 coap_log_debug(
"ignoring request to increase Block size from %u to %u\n",
3101 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
3102 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
3114#if COAP_Q_BLOCK_SUPPORT
3127 sizeof(
"Changing blocksize during request invalid")-1,
3128 (
const uint8_t *)
"Changing blocksize during request invalid");
3130 goto skip_app_handler;
3132#if COAP_Q_BLOCK_SUPPORT
3137 goto call_app_handler;
3141 num + i < max_block; i++) {
3142 add_block_send(num + i, 1, out_blocks, &request_cnt,
3150 num + i < max_block; i++) {
3151 add_block_send(num + i, 0, out_blocks, &request_cnt,
3156 add_block_send(num, 0, out_blocks, &request_cnt,
3159 add_block_send(num, 0, out_blocks, &request_cnt, 1);
3163 if (request_cnt == 0) {
3166 out_blocks[0].num = 0;
3167 out_blocks[0].is_continue = 0;
3171 for (i = 0; i < request_cnt; i++) {
3175 block.
num = out_blocks[i].num;
3177 if (i + 1 < request_cnt) {
3184 if (out_blocks[i].is_continue) {
3196 goto internal_issue;
3199 if (out_blocks[i].is_continue)
3214 goto internal_issue;
3221 offset = block.
num * chunk;
3225 ((out_pdu->
max_size - token_options) /1024) * 1024;
3236 goto internal_issue;
3238 if (!(offset + chunk < lg_xmit->data_info->length)) {
3246 if (!(offset + chunk < lg_xmit->data_info->length)) {
3264 goto internal_issue;
3272 goto internal_issue;
3274 if (i + 1 < request_cnt) {
3285 goto skip_app_handler;
3286#if COAP_Q_BLOCK_SUPPORT
3293 if (out_pdu != response)
3298 (
const uint8_t *)error_phrase);
3309#if COAP_Q_BLOCK_SUPPORT
3311blocks_delete_entry(
coap_rblock_t *rec_blocks, uint32_t block_num) {
3319 for (i = 0; i < rec_blocks->
used; i++) {
3320 if (block_num >= rec_blocks->
range[i].
begin &&
3321 block_num <= rec_blocks->range[i].end) {
3323 if (block_num == rec_blocks->
range[i].
begin) {
3324 if (block_num == rec_blocks->
range[i].
end) {
3326 if (i + 1 < rec_blocks->
used) {
3327 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3328 (rec_blocks->
used - i - 1) *
sizeof(rec_blocks->
range[0]));
3334 }
else if (block_num == rec_blocks->
range[i].
end) {
3339 if (i == rec_blocks->
used)
3341 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3342 sizeof(rec_blocks->
range[i]) * (rec_blocks->
used - i));
3349 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3350 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3351 rec_blocks->
range[i].
end = block_num - 1;
3352 rec_blocks->
range[i+1].
begin = block_num + 1;
3373 rec_blocks->
retry = 0;
3375 for (i = 0; i < rec_blocks->
used; i++) {
3376 if (block_num >= rec_blocks->
range[i].
begin &&
3377 block_num <= rec_blocks->range[i].end)
3380 if (block_num < rec_blocks->range[i].begin) {
3381 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3388 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3389 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3395 if (block_num == rec_blocks->
range[i].
end + 1) {
3396 rec_blocks->
range[i].
end = block_num;
3397 if (i + 1 < rec_blocks->
used) {
3398 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3401 if (i+2 < rec_blocks->
used) {
3402 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3403 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3411 if (i == rec_blocks->
used) {
3426#if COAP_SERVER_SUPPORT
3454 const uint8_t *data =
NULL;
3460 uint16_t block_option = 0;
3467 const uint8_t *rtag;
3470 unsigned int saved_num;
3471 size_t saved_offset;
3472 int lg_srcv_is_refed = 0;
3473#if COAP_Q_BLOCK_SUPPORT
3474 int request_missing = 0;
3479 *pfree_lg_srcv =
NULL;
3486#if COAP_Q_BLOCK_SUPPORT
3489 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3490 (
const uint8_t *)
"Block1 + Q-Block1 together");
3492 goto skip_app_handler;
3496#if COAP_Q_BLOCK_SUPPORT
3502 if (!block_option ||
3516 goto skip_app_handler;
3518 goto call_app_handler;
3533#if COAP_Q_BLOCK_SUPPORT
3536 coap_log_info(
"Q-Block1: Size1 and RTag options required\n");
3538 goto skip_app_handler;
3542 rtag_length =
min(rtag_length, 8);
3553 goto skip_app_handler;
3557 if (total < size1) {
3561 if (total < (block.
num << (block.
szx + 4)) + length + (block.
m ? 1 : 0)) {
3562 total = (block.
num << (block.
szx + 4)) + length + (block.
m ? 1 : 0);
3569 if (max_block_szx == -1 || (
unsigned int)max_block_szx > block.
szx) {
3570 max_block_szx = block.
szx;
3572 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3576 (total > max_body)) {
3581 if (max_body_size == 0 || max_body < max_body_size) {
3582 max_body_size = max_body;
3589 snprintf(buf,
sizeof(buf),
"Max body size %" PRIuS, max_body_size);
3593 goto skip_app_handler;
3596 offset = block.
num << (block.
szx + 4);
3599#
if COAP_Q_BLOCK_SUPPORT
3608 if (max_block_szx == -1 || (
unsigned int)max_block_szx >= block.
szx) {
3630 if (total < (length + offset + (block.
m ? 1 : 0)))
3631 total = length + offset + (block.
m ? 1 : 0);
3633 *added_block = block.
m;
3635 goto call_app_handler;
3643 coap_lg_srcv_ignore_options,
3644 sizeof(coap_lg_srcv_ignore_options)/
sizeof(coap_lg_srcv_ignore_options[0]));
3647 goto skip_app_handler;
3649 LL_FOREACH(session->lg_srcv, lg_srcv) {
3650 if (rtag_opt || lg_srcv->rtag_set == 1) {
3651 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3653 if (lg_srcv->rtag_length != rtag_length ||
3654 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3657#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3663 if (session->recipient_ctx != lg_srcv->recipient_ctx)
3665 if (session->recipient_ctx !=
NULL ||
3669 if (memcmp(cache_key_l, lg_srcv->cache_key,
sizeof(
coap_cache_key_t)) == 0)
3676 (
const uint8_t *)
"Missing block 0");
3678 goto skip_app_handler;
3684 if (lg_srcv ==
NULL) {
3686 (
const uint8_t *)
"Memory issue");
3688 goto skip_app_handler;
3692 session->
context->lg_srcv_cnt++;
3694#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3695 lg_srcv->recipient_ctx = session->recipient_ctx;
3698 lg_srcv->resource = resource;
3699 lg_srcv->cache_key = cache_key_l;
3701 lg_srcv->content_format = fmt;
3702 lg_srcv->total_len = total;
3704 if (!block.
bert && block.
num == 0 && max_block_szx != -1 &&
3705 (
unsigned int)max_block_szx < block.
szx) {
3706 lg_srcv->szx = max_block_szx;
3707 block.
szx = block.
aszx = max_block_szx;
3709 lg_srcv->szx = block.
szx;
3711 lg_srcv->block_option = block_option;
3714 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3715 lg_srcv->observe_set = 1;
3718 lg_srcv->rtag_length = (uint8_t)rtag_length;
3719 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3720 lg_srcv->rtag_set = 1;
3722#if COAP_Q_BLOCK_SUPPORT
3723 lg_srcv->r_m_payload_set = -1;
3725 LL_PREPEND(session->lg_srcv, lg_srcv);
3728 if (block.
szx > lg_srcv->szx || (max_block_szx != -1 && block.
szx != max_block_szx)) {
3731 (
const uint8_t *)
"Change in SZX invalid");
3734 goto skip_app_handler;
3736 lg_srcv->szx = block.
szx;
3739 coap_lg_srcv_reference_lkd(lg_srcv);
3740 lg_srcv_is_refed = 1;
3744 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3746 (
const uint8_t *)
"Missing interim block");
3748 goto skip_app_handler;
3751 if (fmt != lg_srcv->content_format) {
3752 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3753 (
const uint8_t *)
"Content-Format mismatch");
3758#if COAP_Q_BLOCK_SUPPORT
3761 if (total != lg_srcv->total_len ||
3762 (!block.
m && offset + length != total)) {
3764 (
const uint8_t *)
"Size1 mismatch");
3775 if (lg_srcv->total_len_set &&
3776 (offset + length > lg_srcv->total_len ||
3777 (!block.
m && offset + length != lg_srcv->total_len))) {
3779 (
const uint8_t *)
"Block after last block");
3784 lg_srcv->last_type = pdu->
type;
3786 if (context->max_bodies_ram) {
3787 ssize_t increase = total - (lg_srcv->body_data ? lg_srcv->body_data->length : 0);
3789 if (increase > 0 && context->cur_bodies_ram + increase > context->max_bodies_ram) {
3798 (
const uint8_t *)
"Memory limit hit");
3800 goto skip_app_handler;
3805 saved_num = block.
num;
3806 saved_offset = offset;
3808 while (offset < saved_offset + length) {
3809 uint32_t block_m = block.
m;
3817 if (block.
bert && offset + 1024 < saved_offset + length)
3826#if COAP_Q_BLOCK_SUPPORT
3828 request_missing = 1;
3836 offset = block.
num << (block.
szx + 4);
3841#if COAP_Q_BLOCK_SUPPORT
3842 if (request_missing) {
3843 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3844 goto skip_app_handler;
3850#if COAP_Q_BLOCK_SUPPORT
3851 lg_srcv->rec_blocks.processing_payload_set =
3856 lg_srcv->total_len = saved_offset + length;
3857 lg_srcv->total_len_set = 1;
3858 }
else if (lg_srcv->total_len < saved_offset + length) {
3859 lg_srcv->total_len = saved_offset + length;
3862#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3863 !resource->is_proxy_uri && \
3864 !resource->is_reverse_proxy && \
3865 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3866 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3868 if (lg_srcv->body_data)
3869 session->
context->cur_bodies_ram -= lg_srcv->body_data->length;
3870 if (USE_BLOCK_DATA_HANDLER) {
3875 &lg_srcv->body_data,
3876 length, data, saved_offset,
3877 lg_srcv->total_len));
3880 goto skip_app_handler;
3884 saved_offset, lg_srcv->total_len);
3885 if (!lg_srcv->body_data) {
3887 (
const uint8_t *)
"Memory issue");
3889 goto skip_app_handler;
3892 if (lg_srcv->body_data)
3893 context->cur_bodies_ram += lg_srcv->body_data->length;
3895#if COAP_Q_BLOCK_SUPPORT
3897 goto q_block_1_check;
3919 goto skip_app_handler;
3921#if COAP_Q_BLOCK_SUPPORT
3931#if COAP_Q_BLOCK_SUPPORT
3936 if (lg_srcv->rec_blocks.used == 1 &&
3940 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3942 saved_num = block.
num = lg_srcv->rec_blocks.range[0].end;
3944 }
else if (lg_srcv->rec_blocks.used > 1 &&
3946 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].begin <
3947 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end &&
3948 (lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end %
COAP_MAX_PAYLOADS(session)) + 1
3955 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3956 goto skip_app_handler;
3957 }
else if (lg_srcv->rec_blocks.used > 1 &&
3965 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3966 goto skip_app_handler;
3971 goto skip_app_handler;
3975 goto skip_app_handler;
3981 if (!block.
bert && saved_num == 0 && max_block_szx != -1 &&
3982 (
unsigned int)max_block_szx < block.
aszx) {
3983 block.
aszx = max_block_szx;
4014 LL_FOREACH(session->lg_srcv, sg) {
4015 if (lg_srcv == sg) {
4021 goto skip_app_handler;
4024 if (lg_srcv->last_token) {
4025 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
4033#if COAP_Q_BLOCK_SUPPORT
4038 lg_srcv->no_more_seen = 1;
4052#if COAP_Q_BLOCK_SUPPORT
4054 coap_request_missing_q_block1(session, lg_srcv, block.
num);
4058 goto skip_app_handler;
4068 if (lg_srcv->observe_set) {
4070 lg_srcv->observe_length, lg_srcv->observe);
4073 if (lg_srcv->body_data) {
4082 if (USE_BLOCK_DATA_HANDLER) {
4091 lg_srcv->dont_timeout = 1;
4092 *pfree_lg_srcv = lg_srcv;
4096 if (lg_srcv_is_refed)
4097 coap_lg_srcv_release_lkd(session, lg_srcv);
4101 LL_DELETE(session->lg_srcv, lg_srcv);
4102 coap_block_delete_lg_srcv(session, lg_srcv);
4106 if (lg_srcv_is_refed)
4107 coap_lg_srcv_release_lkd(session, lg_srcv);
4112#if COAP_CLIENT_SUPPORT
4113#if COAP_Q_BLOCK_SUPPORT
4115derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
4116 uint32_t value = **bp & 0x1f;
4120 }
else if (value == 24) {
4122 return (uint32_t)-1;
4126 }
else if (value == 25) {
4128 return (uint32_t)-1;
4136 return (uint32_t)-1;
4137 value = (uint32_t)(**bp) << 24;
4139 value |= **bp << 16;
4157 if (sent || lg_xmit || lg_crcv) {
4161 const uint8_t *data;
4167#if COAP_CONSTRAINED_STACK
4169 static uint8_t l_data[1024];
4171 uint8_t l_data[1024];
4177 }
else if (lg_xmit) {
4180 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
4181 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
4183 assert(blk_size <=
sizeof(l_data));
4193 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
4194 lg_xmit->data_info->length - offset;
4198 sent = lg_crcv->sent_pdu;
4207 ++lg_crcv->retry_counter);
4221 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
4222#if COAP_OSCORE_SUPPORT
4223 if (session->oscore_encryption &&
4230 lg_crcv->state_token = token;
4280 int lg_crcv_is_refed = 0;
4285 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
4288 lg_crcv = coap_find_lg_crcv(session, rcvd);
4291 coap_lg_crcv_reference_lkd(lg_crcv);
4292 lg_crcv_is_refed = 1;
4302 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4303 1 << (block.
szx + 4), block.
num);
4309 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
4315 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
4316 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
4317#if COAP_Q_BLOCK_SUPPORT
4318 lg_xmit->send_blocks.range[0].begin = block.
num;
4319 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->
data_info->
length -1) / chunk);
4322 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
4323 coap_log_debug(
"new Block size is %u, block number %u completed\n",
4324 1 << (block.
szx + 4), block.
num);
4330 track_echo(session, rcvd);
4331#if COAP_Q_BLOCK_SUPPORT
4334 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
4335 goto skip_app_handler;
4350 goto skip_app_handler;
4355 if ((block.
num + 1) * chunk < lg_xmit->data_info->length) {
4371 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
4374 assert(len <=
sizeof(buf));
4376 memcpy(buf, new_token->
s, len);
4395 offset = block.
num * chunk;
4397 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
4400 ((pdu->
max_size - token_options) /1024) * 1024;
4414#if COAP_CONSTRAINED_STACK
4416 static uint8_t l_data[1024];
4418 uint8_t l_data[1024];
4422 assert(chunk <= 1024);
4424 block.
num * chunk, l_data, &l_length,
4439#if COAP_Q_BLOCK_SUPPORT
4442 if (coap_send_q_block1(session, block, pdu,
4445 goto skip_app_handler;
4452 goto skip_app_handler;
4462 int observe_action = -1;
4465 goto lg_xmit_finished;
4475 goto lg_xmit_finished;
4481 goto call_app_handler;
4483 goto lg_xmit_finished;
4485 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
4486 goto skip_app_handler;
4487#if COAP_Q_BLOCK_SUPPORT
4492 goto skip_app_handler;
4496 const uint8_t *data;
4501 uint16_t fmt = fmt_opt ?
4511 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
4512 goto skip_app_handler;
4517 const uint8_t *bp = data;
4520 for (i = 0; (bp < data + length) &&
4522 if ((*bp & 0xc0) != 0x00)
4524 block.
num = derive_cbor_value(&bp, data + length - bp);
4526 if (block.
num > (1 << 20) -1)
4528 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4533 if (lg_xmit->send_blocks.used) {
4535 block.
num = lg_xmit->send_blocks.range[0].begin;
4536 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4537 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_MISSING);
4539 goto skip_app_handler;
4542 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4545 goto lg_xmit_finished;
4547 goto call_app_handler;
4559 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4570 lg_crcv->sent_pdu->lg_xmit = 0;
4582 LL_DELETE(session->
lg_xmit, lg_xmit);
4586 if (lg_crcv_is_refed)
4587 coap_lg_crcv_release_lkd(session, lg_crcv);
4591 if (lg_crcv_is_refed)
4592 coap_lg_crcv_release_lkd(session, lg_crcv);
4605 const uint8_t *data,
size_t offset,
size_t total) {
4618 const uint8_t *data,
size_t offset,
size_t total) {
4621 if (body_data ==
NULL && total) {
4624 if (body_data ==
NULL)
4628 if (SIZE_MAX - 8 < length || offset > SIZE_MAX - length - 8) {
4634 if (offset + length <= body_data->length) {
4636 memcpy(&body_data->
s[offset], data, length);
4652 memcpy(&body_data->
s[offset], data, length);
4661#if COAP_CLIENT_SUPPORT
4681#if COAP_Q_BLOCK_SUPPORT
4685 uint16_t block_opt = 0;
4687 int ack_rst_sent = 0;
4691 memset(&block, 0,
sizeof(block));
4692#if COAP_Q_BLOCK_SUPPORT
4693 memset(&qblock, 0,
sizeof(qblock));
4695 lg_crcv = coap_find_lg_crcv(session, rcvd);
4703 const uint8_t *data;
4706 size_t size2 = size_opt ?
4718#if COAP_Q_BLOCK_SUPPORT
4721 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4727 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4732 track_echo(session, rcvd);
4733 if (have_block && (block.
m || length)) {
4737 uint16_t fmt = fmt_opt ?
4744 size_t saved_offset;
4747#if COAP_Q_BLOCK_SUPPORT
4750 coap_log_info(
"Q-Block2: Size2 and ETag options required\n");
4767 goto expire_lg_crcv;
4770 chunk = (size_t)1 << (block.
szx + 4);
4771 offset = block.
num * chunk;
4772 if (size2 < (offset + length)) {
4774 size2 = offset + length + 1;
4776 size2 = offset + length;
4778 saved_offset = offset;
4780 if (lg_crcv->initial) {
4781#if COAP_Q_BLOCK_SUPPORT
4784 lg_crcv->initial = 0;
4785 if (lg_crcv->body_data) {
4787 lg_crcv->body_data =
NULL;
4790 lg_crcv->etag_length = (uint8_t)
min(
coap_opt_length(etag_opt),
sizeof(lg_crcv->etag));
4791 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4792 lg_crcv->etag_set = 1;
4794 lg_crcv->etag_set = 0;
4796 lg_crcv->total_len = size2;
4797 lg_crcv->content_format = fmt;
4798 lg_crcv->szx = block.
szx;
4799 lg_crcv->block_option = block_opt;
4800 lg_crcv->last_type = rcvd->
type;
4801 lg_crcv->rec_blocks.used = 0;
4802 lg_crcv->rec_blocks.total_blocks = 0;
4803 lg_crcv->total_len_set = 0;
4804 lg_crcv->o_blk_size = block.
aszx;
4805#if COAP_Q_BLOCK_SUPPORT
4806 lg_crcv->rec_blocks.processing_payload_set = 0;
4809 if (block.
aszx != lg_crcv->o_blk_size) {
4810 coap_log_info(
"(Q-)Block2: Server block size change error\n");
4818 lg_crcv->total_len < size2)
4819 lg_crcv->total_len = size2;
4826 if (max_block_szx == -1 || (
unsigned int)max_block_szx > block.
szx) {
4827 max_block_szx = block.
szx;
4829 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4833 (size2 > max_body)) {
4836 if (max_body_size == 0 || max_body < max_body_size) {
4837 max_body_size = max_body;
4849 lg_crcv->etag, lg_crcv->etag_length)) {
4853 ++lg_crcv->retry_counter);
4857#if COAP_Q_BLOCK_SUPPORT
4862 coap_log_warn(
"Data body updated during receipt - new request started\n");
4866 lg_crcv->initial = 1;
4868 lg_crcv->body_data =
NULL;
4881 (0 << 4) | (0 << 3) | block.
aszx),
4887 goto skip_app_handler;
4889 }
else if (lg_crcv->etag_set) {
4895 if (fmt != lg_crcv->content_format) {
4899#if COAP_Q_BLOCK_SUPPORT
4902 size2, lg_crcv->total_len);
4907 saved_offset + length != lg_crcv->total_len) {
4909 saved_offset + length, lg_crcv->total_len);
4914 if (lg_crcv->total_len_set &&
4915 (saved_offset + length > lg_crcv->total_len ||
4916 (!block.
m && saved_offset + length != lg_crcv->total_len))) {
4918 block.
num, saved_offset + length, lg_crcv->total_len);
4921 if (block.
num == 0) {
4927 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4928 lg_crcv->observe_set = 1;
4930 lg_crcv->observe_set = 0;
4934 while (offset < saved_offset + length) {
4936#if COAP_Q_BLOCK_SUPPORT
4940 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4941 1 << (block.
szx + 4), block.
num);
4948#if COAP_Q_BLOCK_SUPPORT
4950 if (lg_crcv->rec_blocks.used > 1 &&
4951 (block.
num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end ||
4952 block.
num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end - 1) &&
4953 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].begin <
4954 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end &&
4955 (lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end %
COAP_MAX_PAYLOADS(session)) + 1
4962 coap_request_missing_q_block2(session, lg_crcv);
4963 }
else if (lg_crcv->rec_blocks.used &&
4964 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4965 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4966 coap_request_missing_q_block2(session, lg_crcv);
4969 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4975 offset = block.
num << (block.
szx + 4);
4981 if (updated_block) {
4987 lg_crcv->total_len = saved_offset + length;
4988 lg_crcv->total_len_set = 1;
4992 if (size2 < saved_offset + length) {
4993 size2 = saved_offset + length;
5000 &lg_crcv->body_data,
5002 saved_offset, size2));
5008 saved_offset, size2);
5009 if (lg_crcv->body_data ==
NULL) {
5022#if COAP_Q_BLOCK_SUPPORT
5028 if (check_all_blocks_in_for_payload_set(session,
5029 &lg_crcv->rec_blocks)) {
5030 block.
num = lg_crcv->rec_blocks.range[0].end;
5032 lg_crcv->rec_blocks.processing_payload_set =
5034 if (check_any_blocks_next_payload_set(session,
5035 &lg_crcv->rec_blocks)) {
5037 coap_request_missing_q_block2(session, lg_crcv);
5038 goto skip_app_handler;
5043 goto skip_app_handler;
5047 goto skip_app_handler;
5071 ((block.
num + 1) << 4) |
5072 (block.
m << 3) | block.
aszx),
5077 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, 0, length, data,
NULL,
NULL,
NULL,
5082 goto skip_app_handler;
5085 goto skip_app_handler;
5090#if COAP_Q_BLOCK_SUPPORT
5092 lg_crcv->total_len : size2;
5101 if (lg_crcv->app_token)
5103 lg_crcv->app_token->s);
5106 goto call_app_handler;
5108#if COAP_Q_BLOCK_SUPPORT
5114 if (lg_crcv->observe_set) {
5116 lg_crcv->observe_length, lg_crcv->observe);
5118 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
5119#if COAP_Q_BLOCK_SUPPORT
5128 lg_crcv->total_len : saved_offset + length;
5136#if COAP_Q_BLOCK_SUPPORT
5138 lg_crcv->total_len : size2;
5151 if (lg_crcv->app_token)
5153 lg_crcv->app_token->s);
5156 body_free = lg_crcv->body_data;
5157 lg_crcv->body_data =
NULL;
5161 if (lg_crcv->observe_set == 0) {
5163 LL_DELETE(session->lg_crcv, lg_crcv);
5164 coap_block_delete_lg_crcv(session, lg_crcv);
5165 goto skip_app_handler;
5168 lg_crcv->initial = 1;
5171 goto skip_app_handler;
5186 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
5187 lg_crcv->observe_set = 1;
5189 lg_crcv->observe_set = 0;
5198 goto expire_lg_crcv;
5203#if COAP_OSCORE_SUPPORT
5204 if (check_freshness(session, rcvd,
5205 (session->oscore_encryption == 0) ? sent :
NULL,
5208 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
5210 goto skip_app_handler;
5211 goto expire_lg_crcv;
5220 const uint8_t *data;
5227 ++lg_crcv->retry_counter);
5235#if COAP_PROXY_SUPPORT
5236 coap_proxy_req_t *proxy_req = session->
context->proxy_list_count ?
5237 coap_proxy_map_outgoing_request(session, rcvd,
NULL) :
NULL;
5243 proxy_req->token_used =
new;
5244 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"upd");
5248 coap_log_debug(
"* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
5252 goto skip_app_handler;
5255 goto expire_lg_crcv;
5258 goto expire_lg_crcv;
5260 if (!block.
m && !lg_crcv->observe_set) {
5270 lg_crcv->last_used -= adjust;
5284#
if COAP_Q_BLOCK_SUPPORT
5291 goto skip_app_handler;
5294 const uint8_t *data;
5298#if COAP_Q_BLOCK_SUPPORT
5299 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
5305 if (block.
num != 0) {
5307 size_t chunk = (size_t)1 << (block.
szx + 4);
5312 goto call_app_handler;
5315#if COAP_Q_BLOCK_SUPPORT
5319 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
5325 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5328 LL_PREPEND(session->lg_crcv, lg_crcv);
5329 return coap_handle_response_get_block(context, session, sent, rcvd,
5342 track_echo(session, rcvd);
5344 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5347 LL_PREPEND(session->lg_crcv, lg_crcv);
5348 return coap_handle_response_get_block(context, session, sent, rcvd,
5360 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5363 LL_PREPEND(session->lg_crcv, lg_crcv);
5364 return coap_handle_response_get_block(context, session, sent, rcvd,
5382 if (lg_crcv->app_token)
5384 lg_crcv->app_token->s);
5388 LL_DELETE(session->lg_crcv, lg_crcv);
5389 coap_block_delete_lg_crcv(session, lg_crcv);
5401#if COAP_SERVER_SUPPORT
5410 if (response->
code == 0)
5412 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
5420#if COAP_CLIENT_SUPPORT
5423 uint64_t token_match =
5429 if (session->lg_crcv) {
5430 LL_FOREACH(session->lg_crcv, lg_crcv) {
5435 lg_crcv->app_token->s);
5443 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 add_get_func_data(coap_session_t *session, coap_pdu_t *pdu, coap_get_large_data_t get_func, void *app_ptr, size_t offset, size_t length)
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, 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_cache_key_t coap_cache_key_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
void coap_delete_cache_key(coap_cache_key_t *cache_key)
Delete the cache-key.
coap_cache_key_t * coap_cache_derive_key_w_ignore(const coap_session_t *session, const coap_pdu_t *pdu, coap_cache_session_based_t session_based, const uint16_t *ignore_options, size_t ignore_count)
Calculates a cache-key for the given CoAP PDU.
@ COAP_CACHE_IS_SESSION_BASED_NO_DATA
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.
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.
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.
size_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.
uint64_t maxage_expire
When this entry expires, in seconds.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
coap_resource_t * resource
associated resource
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
application 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.
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.
union coap_lg_xmit_t::@307025105257072175273265221004207100347162040320 b
uint16_t option
large block transmission 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
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
coap_bin_const_t * last_token
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