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 switch (max_block_size) {
467 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
472 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
480 const uint8_t *b,
size_t blen) {
481 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
488 uint64_t token_match =
492 LL_FOREACH(session->
lg_xmit, lg_xmit) {
498#if COAP_CLIENT_SUPPORT
529 LL_PREPEND(session->
lg_xmit, lg_xmit);
534#if COAP_CLIENT_SUPPORT
538 lg_crcv = coap_find_lg_crcv(session, pdu);
540 lg_xmit->b.b1.state_token = lg_crcv->state_token;
552#if COAP_CLIENT_SUPPORT
560 ret = coap_cancel_observe_lkd(session, token, type);
576 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
581 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
582 if (lg_crcv->observe_set) {
583 if ((!token && !lg_crcv->app_token->length) || (token &&
589#if COAP_Q_BLOCK_SUPPORT
595 lg_crcv->obs_token[0] ?
596 lg_crcv->obs_token[0] :
606 lg_crcv->observe_set = 0;
616 if (lg_crcv->o_block_option) {
619 lg_crcv->o_blk_size),
622 if (lg_crcv->obs_data) {
623 coap_add_data_large_request_lkd(session, pdu,
624 lg_crcv->obs_data->length,
625 lg_crcv->obs_data->data,
NULL,
NULL);
627 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
638#if COAP_Q_BLOCK_SUPPORT
640 if (using_q_block1) {
641 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
660 uint64_t token_match =
664 LL_FOREACH(session->lg_crcv, lg_crcv) {
682 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
684 if (m_lg_crcv->obs_data) {
685 m_lg_crcv->obs_data->ref++;
686 lg_crcv->obs_data = m_lg_crcv->obs_data;
688 LL_PREPEND(session->lg_crcv, lg_crcv);
694#if COAP_OSCORE_SUPPORT
708 lg_crcv = coap_find_lg_crcv(session, pdu);
715 ++lg_crcv->retry_counter);
727 if (!lg_crcv->obs_token) {
729 if (!lg_crcv->obs_token) {
732 lg_crcv->obs_token_cnt = 1;
737 if (lg_crcv->obs_token[0] ==
NULL)
757#if COAP_SERVER_SUPPORT
774 LL_FOREACH(session->
lg_xmit, lg_xmit) {
791 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
817#if COAP_Q_BLOCK_SUPPORT
823 int have_block_defined = 0;
825 uint8_t max_blk_size;
827 size_t token_options;
830#if COAP_Q_BLOCK_SUPPORT
834#if !COAP_SERVER_SUPPORT
840 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
848 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
862#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
863#if UINT_MAX < MAX_BLK_LEN
865#define MAX_BLK_LEN UINT_MAX
873#if COAP_SERVER_SUPPORT
885 coap_digest_t digest;
886 coap_digest_ctx_t *dctx = coap_digest_setup();
892 if (coap_digest_update(dctx, data, length)) {
893 if (coap_digest_final(dctx, &digest)) {
894 memcpy(&etag, digest.key,
sizeof(etag));
895#if COAP_ETAG_MAX_BYTES != 8
901 coap_digest_free(dctx);
921 if (etag == etag_r) {
936#if COAP_Q_BLOCK_SUPPORT
952 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
957#if COAP_CLIENT_SUPPORT
960 LL_DELETE(session->
lg_xmit, lg_xmit);
963 if (!is_mcast && !last_all_sent)
971#if COAP_Q_BLOCK_SUPPORT
972 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
985#if COAP_SERVER_SUPPORT
990 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
994 LL_DELETE(session->
lg_xmit, lg_xmit);
1002#if COAP_OSCORE_SUPPORT
1003 if (session->oscore_encryption) {
1011 avail = pdu->
max_size - token_options;
1014#if COAP_OSCORE_SUPPORT
1023 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1029 if (max_blk_size && blk_size > max_blk_size)
1030 blk_size = max_blk_size;
1034 if (block.
szx < blk_size)
1035 blk_size = block.
szx;
1036 have_block_defined = 1;
1038#if COAP_Q_BLOCK_SUPPORT
1041 if (have_block_defined) {
1043 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1047 if (block.
szx < blk_size)
1048 blk_size = block.
szx;
1049 have_block_defined = 1;
1050 option = alt_option;
1055 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1057 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1061 chunk = (size_t)1 << (blk_size + 4);
1062 if ((have_block_defined && block.
num != 0) || single_request ||
1067 if (length >= block.
num * chunk) {
1068#if COAP_SERVER_SUPPORT
1074 (
unsigned int)length),
1088 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1093 if (chunk > length - block.
num * chunk)
1094 rem = length - block.
num * chunk;
1101 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1126 lg_xmit->blk_size = blk_size;
1127 lg_xmit->option = option;
1129 if (!lg_xmit->data_info)
1131 lg_xmit->data_info->ref = 0;
1132 lg_xmit->data_info->data = data;
1133 lg_xmit->data_info->length = length;
1134#if COAP_Q_BLOCK_SUPPORT
1135 lg_xmit->non_timeout_random_ticks =
1138 lg_xmit->data_info->get_func = get_func;
1139 lg_xmit->data_info->release_func = release_func;
1140 lg_xmit->data_info->app_ptr = app_ptr;
1147 if (!lg_xmit->b.b1.app_token)
1155 lg_xmit->b.b1.count = 1;
1157 lg_xmit->b.b1.count);
1166 (
unsigned int)length),
1179 lg_xmit->b.b2.resource = resource;
1182 if (lg_xmit->b.b2.query) {
1183 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1186 lg_xmit->b.b2.query =
NULL;
1191 sizeof(lg_xmit->b.b2.rtag));
1192 memcpy(lg_xmit->b.b2.rtag,
coap_opt_value(opt), lg_xmit->b.b2.rtag_length);
1193 lg_xmit->b.b2.rtag_set = 1;
1195 lg_xmit->b.b2.rtag_set = 0;
1197 lg_xmit->b.b2.etag = etag;
1198 lg_xmit->b.b2.request_method = request_method;
1205 lg_xmit->b.b2.maxage_expire = 0;
1210 (
unsigned int)length),
1215 blk_size, lg_xmit->data_info->length))
1222 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1230 avail = pdu->
max_size - token_options;
1235#if COAP_OSCORE_SUPPORT
1238 if (avail < (ssize_t)chunk) {
1241 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1244 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1245 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1246 lg_xmit->blk_size = blk_size;
1247 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1253 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1256#if COAP_Q_BLOCK_SUPPORT
1258 lg_xmit->send_blocks.used = 1;
1259 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->data_info->length - 1) / chunk);
1263 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1264 rem = lg_xmit->data_info->length - block.
num * chunk;
1266#if COAP_CONSTRAINED_STACK
1268 static uint8_t l_data[1024];
1269 uint8_t *data_buf = l_data;
1271 uint8_t l_data[1024];
1272 uint8_t *data_buf = l_data;
1275 int data_buf_allocated = 0;
1277 if (rem >
sizeof(l_data)) {
1283 data_buf_allocated = 1;
1285 if (get_func(session, rem, block.
num * chunk, data_buf, &l_length,
1286 lg_xmit->data_info->app_ptr)) {
1288 if (data_buf_allocated)
1293 if (data_buf_allocated)
1301 lg_xmit->b.b1.bert_size = rem;
1303 lg_xmit->last_block = -1;
1306 LL_PREPEND(session->
lg_xmit,lg_xmit);
1309 if (have_block_defined) {
1313 (0 << 4) | (0 << 3) | blk_size), buf);
1318 (
unsigned int)length),
1324 (
unsigned int)length),
1333 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1354 }
else if (release_func) {
1360#if COAP_CLIENT_SUPPORT
1365 const uint8_t *data,
1372 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1373 release_func, app_ptr);
1382 const uint8_t *data,
1396 length, data, release_func,
NULL, app_ptr, 0, 0);
1409 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1410 release_func, get_func, app_ptr);
1430 length,
NULL, release_func, get_func,
1435#if COAP_SERVER_SUPPORT
1442 uint16_t media_type,
1446 const uint8_t *data,
1452 ret = coap_add_data_large_response_lkd(resource, session, request,
1453 response, query, media_type, maxage, etag,
1454 length, data, release_func, app_ptr);
1465 uint16_t media_type,
1469 const uint8_t *data,
1473 unsigned char buf[4];
1475 int block_requested = 0;
1476 int single_request = 0;
1477#if COAP_Q_BLOCK_SUPPORT
1478 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1484 memset(&block, 0,
sizeof(block));
1491 block_requested = 1;
1492 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1495 length >> (block.
szx + 4));
1500#if COAP_Q_BLOCK_SUPPORT
1502 block_requested = 1;
1503 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1506 length >> (block.
szx + 4));
1510 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1531 if (block_requested) {
1555 query, maxage, etag, length, data,
1556 release_func,
NULL, app_ptr, single_request,
1559 goto error_released;
1569#if COAP_ERROR_PHRASE_LENGTH > 0
1587#if COAP_Q_BLOCK_SUPPORT
1597 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1601 LL_DELETE(session->
lg_xmit, lg_xmit);
1605 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1611 if (lg_xmit->
last_sent + partial_timeout <= now) {
1615#if COAP_CLIENT_SUPPORT
1619 LL_DELETE(session->
lg_xmit, lg_xmit);
1622 if (!is_mcast && !last_all_sent)
1626 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1627 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1636#if COAP_CLIENT_SUPPORT
1637#if COAP_Q_BLOCK_SUPPORT
1642 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1654 pdu->
type = lg_crcv->last_type;
1666 int block_payload_set = -1;
1677 if (lg_crcv->rec_blocks.used &&
1678 (lg_crcv->rec_blocks.used < 2 ||
1681 block = lg_crcv->rec_blocks.range[0].end + 1;
1682 block_size = (size_t)1 << (lg_crcv->szx + 4);
1683 sofar = block * block_size;
1684 if (sofar < lg_crcv->total_len) {
1687 pdu = coap_build_missing_pdu(session, lg_crcv);
1693 (block << 4) | (1 << 3) | lg_crcv->szx),
1701 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1702 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1703 lg_crcv->rec_blocks.range[i].begin != 0) {
1706 pdu = coap_build_missing_pdu(session, lg_crcv);
1711 if (block_payload_set == -1)
1713 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1717 (block << 4) | (0 << 3) | lg_crcv->szx),
1721 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1722 block = lg_crcv->rec_blocks.range[i].end;
1725 block_size = (size_t)1 << (lg_crcv->szx + 4);
1726 sofar = (block + 1) * block_size;
1727 if (sofar < lg_crcv->total_len) {
1730 pdu = coap_build_missing_pdu(session, lg_crcv);
1734 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1736 if (block_payload_set == -1)
1738 for (; block < (ssize_t)sofar &&
1742 (block << 4) | (0 << 3) | lg_crcv->szx),
1749 lg_crcv->rec_blocks.retry++;
1750 if (block_payload_set != -1)
1751 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1766#if COAP_Q_BLOCK_SUPPORT
1772#if COAP_Q_BLOCK_SUPPORT
1774 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1780 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1784#if COAP_Q_BLOCK_SUPPORT
1786 size_t scaled_timeout = receive_timeout *
1787 ((size_t)1 << lg_crcv->rec_blocks.retry);
1795 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1798 coap_request_missing_q_block2(session, lg_crcv);
1800 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1801 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1809 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1810 lg_crcv->last_used + partial_timeout <= now) {
1811#if COAP_Q_BLOCK_SUPPORT
1815 LL_DELETE(session->lg_crcv, lg_crcv);
1816 coap_block_delete_lg_crcv(session, lg_crcv);
1817 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1819 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1820 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1829#if COAP_SERVER_SUPPORT
1830#if COAP_Q_BLOCK_SUPPORT
1842 if (lg_srcv->last_token)
1843 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1854add_408_block(
coap_pdu_t *pdu, uint32_t block) {
1858 assert(block < (1U << 20));
1861 if (block >= (1U << 20)) {
1863 }
else if (block < 24) {
1865 val[0] = (uint8_t)block;
1866 }
else if (block < 0x100) {
1869 val[1] = (uint8_t)block;
1870 }
else if (block < 0x10000) {
1873 val[1] = (uint8_t)((block >> 8) & 0xff);
1874 val[2] = (uint8_t)(block & 0xff);
1878 val[1] = (uint8_t)((block >> 24) & 0xff);
1879 val[2] = (uint8_t)((block >> 16) & 0xff);
1880 val[3] = (uint8_t)((block >> 8) & 0xff);
1881 val[4] = (uint8_t)(block & 0xff);
1897 for (i = 0; i < rec_blocks->
used; i++) {
1898 if (block_num < rec_blocks->range[i].begin)
1900 if (block_num <= rec_blocks->range[i].end)
1906#if COAP_SERVER_SUPPORT
1908check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1909 if (rec_blocks->
used == 0) {
1910 return block_num == 0 ? 1 : 0;
1912 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1929 for (i = 0; i < rec_blocks->
used; i++) {
1930 if (block < rec_blocks->range[i].begin)
1932 if (block < rec_blocks->range[i].end)
1938#if COAP_CLIENT_SUPPORT
1939#if COAP_Q_BLOCK_SUPPORT
1943 if (rec_blocks->
used &&
1945 rec_blocks->processing_payload_set)
1953 if (rec_blocks->
used > 1 &&
1955 rec_blocks->processing_payload_set)
1962#if COAP_SERVER_SUPPORT
1963#if COAP_Q_BLOCK_SUPPORT
1993 uint32_t inc_to_block) {
1996 uint32_t block_size = (uint32_t)1 << (lg_srcv->szx + 4);
1997 uint32_t first_missing;
1998 uint32_t missing_payload;
1999 uint32_t last_payload_block;
2002 uint32_t end_block = ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1;
2006 (int32_t)(inc_to_block /
COAP_MAX_PAYLOADS(session)) == lg_srcv->r_m_payload_set)
2009 if (inc_to_block != end_block) {
2013 if (lg_srcv->rec_blocks.range[0].begin != 0) {
2016 first_missing = lg_srcv->rec_blocks.range[0].end + 1;
2018 if (first_missing > end_block)
2022 if (inc_to_block != end_block && inc_to_block > last_payload_block) {
2023 inc_to_block = last_payload_block;
2027 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
2028 if (block < (int32_t)lg_srcv->rec_blocks.range[i].begin &&
2029 lg_srcv->rec_blocks.range[i].begin != 0) {
2032 pdu = pdu_408_build(session, lg_srcv);
2037 for (; block < (int32_t)lg_srcv->rec_blocks.range[i].begin; block++) {
2038 if (!add_408_block(pdu, block)) {
2044 if (block < (int32_t)lg_srcv->rec_blocks.range[i].end) {
2045 block = lg_srcv->rec_blocks.range[i].end;
2048 if (inc_to_block == end_block && block < (int32_t)inc_to_block) {
2051 pdu = pdu_408_build(session, lg_srcv);
2056 for (; block <= (int32_t)inc_to_block && count <
COAP_MAX_PAYLOADS(session); block++) {
2057 if (!add_408_block(pdu, block)) {
2066 lg_srcv->rec_blocks.retry++;
2082#if COAP_Q_BLOCK_SUPPORT
2088#if COAP_Q_BLOCK_SUPPORT
2090 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
2096 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
2097 if (lg_srcv->dont_timeout) {
2104#if COAP_Q_BLOCK_SUPPORT
2106 size_t scaled_timeout = receive_timeout *
2107 ((size_t)1 << lg_srcv->rec_blocks.retry);
2113 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
2114 uint32_t block_size = (size_t)1 << (lg_srcv->szx + 4);
2117 if (!coap_request_missing_q_block1(session, lg_srcv,
2118 ((uint32_t)lg_srcv->total_len + block_size - 1) / block_size - 1))
2121 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2122 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2129 if (lg_srcv->no_more_seen)
2131 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2132#if COAP_Q_BLOCK_SUPPORT
2149 if (lg_srcv->last_token)
2150 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2152 (
const uint8_t *)
"Missing interim block");
2156 LL_DELETE(session->lg_srcv, lg_srcv);
2157 coap_block_delete_lg_srcv(session, lg_srcv);
2158 }
else if (lg_srcv->last_used) {
2160 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2161 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2170#if COAP_Q_BLOCK_SUPPORT
2176 uint32_t delayqueue_cnt,
2178 const uint8_t *ptoken;
2180 size_t ltoken_length;
2184 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2192 block->
num = lg_xmit->send_blocks.range[0].begin;
2194 offset = block->
num * chunk;
2211 (lg_xmit->send_blocks.used > 1 ||
2224 coap_log_debug(
"Q-Block: %s block %d\n", is_first ?
"First" :
"Next", block->
num);
2245 ((block->
num) << 4) |
2266 coap_send_pdu_t send_pdu) {
2269 uint32_t delayqueue_cnt = 0;
2279 if (send_pdu == COAP_SEND_INC_PDU) {
2285 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2298 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2299 LL_FOREACH(session->
delayqueue, delayqueue) {
2306 if (send_pdu == COAP_SEND_INC_PDU) {
2311 blocks_delete_entry(&lg_xmit->send_blocks, l_block.
num);
2327 block_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, pdu, delayqueue_cnt, 1);
2332 while (block_pdu && lg_xmit->send_blocks.used) {
2336#if COAP_CONSTRAINED_STACK
2338 static uint8_t l_data[1024];
2340 uint8_t l_data[1024];
2343 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2346 assert(chunk <= 1024);
2348 block.
num * chunk, l_data, &l_length,
2373 blocks_delete_entry(&lg_xmit->send_blocks, block.
num);
2378 t_pdu = coap_next_block_to_send(session, lg_xmit, &l_block, block_pdu, delayqueue_cnt, 0);
2409#if COAP_CLIENT_SUPPORT
2422 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2423 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2427 if (now <= non_timeout) {
2429 *tim_rem = non_timeout - now;
2432 timed_out = now - non_timeout;
2434 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2438 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2441 coap_log_debug(
"Q-Block: Timeout - start next MAX_PAYLOADS_SET\n");
2442 memset(&block, 0,
sizeof(block));
2443 block.
num = lg_xmit->send_blocks.range[0].begin;
2444 offset = block.
num * chunk;
2447 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2448 if (*tim_rem > non_timeout) {
2449 *tim_rem = non_timeout;
2463 LL_DELETE(session->
lg_xmit, lg_xmit);
2478#if COAP_SERVER_SUPPORT
2491 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2492 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2494 if (now <= non_timeout) {
2496 *tim_rem = non_timeout - now;
2499 timed_out = now - non_timeout;
2501 if (lg_xmit->
last_payload && lg_xmit->send_blocks.used) {
2505 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2508 memset(&block, 0,
sizeof(block));
2509 block.
num = lg_xmit->send_blocks.range[0].begin;
2510 offset = block.
num * chunk;
2514 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2515 if (*tim_rem > non_timeout) {
2516 *tim_rem = non_timeout;
2530 LL_DELETE(session->
lg_xmit, lg_xmit);
2534 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2546#if COAP_CLIENT_SUPPORT
2560 if (opt && lg_crcv) {
2561 int observe_action = -1;
2568 if (lg_crcv->obs_token_cnt <= block_num) {
2572 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2575 lg_crcv->obs_token = tmp;
2576 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2577 lg_crcv->obs_token[i] =
NULL;
2582 if (lg_crcv->obs_token_cnt <= block_num)
2583 lg_crcv->obs_token_cnt = block_num + 1;
2586 if (lg_crcv->obs_token[block_num] ==
NULL)
2590 if (block_num < lg_crcv->obs_token_cnt) {
2591 return lg_crcv->obs_token[block_num];
2598#if COAP_Q_BLOCK_SUPPORT
2603 coap_send_pdu_t send_request) {
2606 uint64_t token_match =
2610 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2619 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2624#if COAP_SERVER_SUPPORT
2625#if COAP_Q_BLOCK_SUPPORT
2636 coap_send_pdu_t send_response) {
2641 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2649 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2659 if (data_info->
ref > 0) {
2671#if COAP_CLIENT_SUPPORT
2672#if COAP_Q_BLOCK_SUPPORT
2705 11, (
const uint8_t *)
".well-known");
2707 4, (
const uint8_t *)
"core");
2720 (0 << 4) | (0 << 3) | 0),
2745 if (lg_crcv ==
NULL)
2748 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2752 lg_crcv->initial = 1;
2770 const uint8_t *data;
2773 if (data_len < lg_xmit->data_info->length) {
2784 if (!lg_crcv->app_token) {
2785 coap_block_delete_lg_crcv(session, lg_crcv);
2791 lg_crcv->retry_counter = 1;
2792 lg_crcv->state_token = state_token;
2799 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2807 lg_crcv->o_blk_size = block.
aszx;
2818#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2821 if (lg_crcv ==
NULL)
2824 if (lg_crcv->ref > 0) {
2830 if (lg_crcv->obs_data) {
2832 lg_crcv->obs_data =
NULL;
2838 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2847#if COAP_SERVER_SUPPORT
2851#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2854 if (lg_srcv ==
NULL)
2857 if (lg_srcv->ref > 0) {
2875 if (lg_xmit ==
NULL)
2878 if (lg_xmit->
ref > 0) {
2895#if COAP_SERVER_SUPPORT
2902add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2903 uint32_t *count, uint32_t max_count) {
2906 for (i = 0; i < *count && *count < max_count; i++) {
2907 if (num == out_blocks[i].num)
2909 else if (num < out_blocks[i].num) {
2911 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2912 out_blocks[i].num = num;
2913 out_blocks[i].is_continue = is_continue;
2918 if (*count < max_count) {
2919 out_blocks[i].num = num;
2920 out_blocks[i].is_continue = is_continue;
2953 uint16_t block_opt = 0;
2954 send_track *out_blocks =
NULL;
2955 const char *error_phrase;
2960 uint32_t request_cnt, i;
2963#if COAP_Q_BLOCK_SUPPORT
2968 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2975 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2976 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2977 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2979 goto skip_app_handler;
2986 if (block.
num == 0) {
2987#if COAP_Q_BLOCK_SUPPORT
2997 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2998 if (lg_xmit ==
NULL)
3001#if COAP_Q_BLOCK_SUPPORT
3007 goto internal_issue;
3020 if (etag == lg_xmit->
b.
b2.
etag) {
3033 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3036 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
3037 block.
num, block.
m);
3039 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
3040 1 << (block.
szx + 4), block.
num, block.
m);
3043 if (block.
num == 0) {
3049 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
3050 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3051#if COAP_Q_BLOCK_SUPPORT
3052 lg_xmit->send_blocks.range[0].begin = block.
num;
3053 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->
data_info->
length - 1) / chunk);
3056 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3057 (1 << (block.
szx + 4)), block.
num);
3059 coap_log_debug(
"ignoring request to increase Block size from %u to %u\n",
3063 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
3064 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
3076#if COAP_Q_BLOCK_SUPPORT
3089 sizeof(
"Changing blocksize during request invalid")-1,
3090 (
const uint8_t *)
"Changing blocksize during request invalid");
3092 goto skip_app_handler;
3094#if COAP_Q_BLOCK_SUPPORT
3099 goto call_app_handler;
3103 num + i < max_block; i++) {
3104 add_block_send(num + i, 1, out_blocks, &request_cnt,
3112 num + i < max_block; i++) {
3113 add_block_send(num + i, 0, out_blocks, &request_cnt,
3118 add_block_send(num, 0, out_blocks, &request_cnt,
3121 add_block_send(num, 0, out_blocks, &request_cnt, 1);
3125 if (request_cnt == 0) {
3128 out_blocks[0].num = 0;
3129 out_blocks[0].is_continue = 0;
3133 for (i = 0; i < request_cnt; i++) {
3137 block.
num = out_blocks[i].num;
3139 if (i + 1 < request_cnt) {
3146 if (out_blocks[i].is_continue) {
3158 goto internal_issue;
3161 if (out_blocks[i].is_continue)
3176 goto internal_issue;
3183 offset = block.
num * chunk;
3187 ((out_pdu->
max_size - token_options) /1024) * 1024;
3198 goto internal_issue;
3200 if (!(offset + chunk < lg_xmit->data_info->length)) {
3208 if (!(offset + chunk < lg_xmit->data_info->length)) {
3226 goto internal_issue;
3234 goto internal_issue;
3236 if (i + 1 < request_cnt) {
3246 goto skip_app_handler;
3247#if COAP_Q_BLOCK_SUPPORT
3257 (
const uint8_t *)error_phrase);
3268#if COAP_Q_BLOCK_SUPPORT
3270blocks_delete_entry(
coap_rblock_t *rec_blocks, uint32_t block_num) {
3278 for (i = 0; i < rec_blocks->
used; i++) {
3279 if (block_num >= rec_blocks->
range[i].
begin &&
3280 block_num <= rec_blocks->range[i].end) {
3282 if (block_num == rec_blocks->
range[i].
begin) {
3283 if (block_num == rec_blocks->
range[i].
end) {
3285 if (i + 1 < rec_blocks->
used) {
3286 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3287 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3293 }
else if (block_num == rec_blocks->
range[i].
end) {
3298 if (i == rec_blocks->
used)
3300 memmove(&rec_blocks->
range[i], &rec_blocks->
range[i+1],
3301 sizeof(rec_blocks->
range[i]) * (rec_blocks->
used - i));
3308 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3309 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3310 rec_blocks->
range[i].
end = block_num - 1;
3311 rec_blocks->
range[i+1].
begin = block_num + 1;
3332 rec_blocks->
retry = 0;
3334 for (i = 0; i < rec_blocks->
used; i++) {
3335 if (block_num >= rec_blocks->
range[i].
begin &&
3336 block_num <= rec_blocks->range[i].end)
3339 if (block_num < rec_blocks->range[i].begin) {
3340 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3347 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3348 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3354 if (block_num == rec_blocks->
range[i].
end + 1) {
3355 rec_blocks->
range[i].
end = block_num;
3356 if (i + 1 < rec_blocks->
used) {
3357 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3360 if (i+2 < rec_blocks->
used) {
3361 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3362 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3370 if (i == rec_blocks->
used) {
3385#if COAP_SERVER_SUPPORT
3414 const uint8_t *data =
NULL;
3419 uint16_t block_option = 0;
3426 const uint8_t *rtag;
3427 uint32_t max_block_szx;
3429 unsigned int saved_num;
3430 size_t saved_offset;
3431 int lg_srcv_is_refed = 0;
3432#if COAP_Q_BLOCK_SUPPORT
3433 int request_missing = 0;
3438 *pfree_lg_srcv =
NULL;
3445#if COAP_Q_BLOCK_SUPPORT
3448 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3449 (
const uint8_t *)
"Block1 + Q-Block1 together");
3451 goto skip_app_handler;
3455#if COAP_Q_BLOCK_SUPPORT
3461 if (!block_option ||
3475 goto skip_app_handler;
3477 goto call_app_handler;
3492#if COAP_Q_BLOCK_SUPPORT
3495 coap_log_info(
"Q-Block1: Size1 and RTag options required\n");
3497 goto skip_app_handler;
3501 rtag_length =
min(rtag_length, 8);
3512 goto skip_app_handler;
3520 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3521 max_block_szx = block.
szx;
3523 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3527 (total > max_body)) {
3532 if (max_body_size == 0 || max_body < max_body_size) {
3533 max_body_size = max_body;
3540 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3544 goto skip_app_handler;
3547 offset = block.
num << (block.
szx + 4);
3550#
if COAP_Q_BLOCK_SUPPORT
3569 if (total < (length + offset + (block.
m ? 1 : 0)))
3570 total = length + offset + (block.
m ? 1 : 0);
3572 *added_block = block.
m;
3574 goto call_app_handler;
3582 coap_lg_srcv_ignore_options,
3583 sizeof(coap_lg_srcv_ignore_options)/
sizeof(coap_lg_srcv_ignore_options[0]));
3586 goto skip_app_handler;
3588 LL_FOREACH(session->lg_srcv, lg_srcv) {
3589 if (rtag_opt || lg_srcv->rtag_set == 1) {
3590 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3592 if (lg_srcv->rtag_length != rtag_length ||
3593 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3596#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3597 if (session->recipient_ctx == lg_srcv->recipient_ctx ||
3601 if (memcmp(cache_key_l, lg_srcv->cache_key,
sizeof(
coap_cache_key_t)) == 0)
3608 (
const uint8_t *)
"Missing block 0");
3610 goto skip_app_handler;
3616 if (lg_srcv ==
NULL) {
3618 (
const uint8_t *)
"Memory issue");
3620 goto skip_app_handler;
3625#if COAP_OSCORE_SUPPORT && COAP_SERVER_SUPPORT
3626 lg_srcv->recipient_ctx = session->recipient_ctx;
3628 lg_srcv->resource = resource;
3629 lg_srcv->cache_key = cache_key_l;
3631 if (resource == context->unknown_resource ||
3632 resource == context->proxy_uri_resource)
3634 lg_srcv->content_format = fmt;
3635 lg_srcv->total_len = total;
3637 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3638 max_block_szx < block.
szx) {
3639 lg_srcv->szx = max_block_szx;
3641 lg_srcv->szx = block.
szx;
3643 lg_srcv->block_option = block_option;
3646 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3647 lg_srcv->observe_set = 1;
3650 lg_srcv->rtag_length = (uint8_t)rtag_length;
3651 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3652 lg_srcv->rtag_set = 1;
3654 lg_srcv->body_data =
NULL;
3655#if COAP_Q_BLOCK_SUPPORT
3656 lg_srcv->r_m_payload_set = -1;
3658 LL_PREPEND(session->lg_srcv, lg_srcv);
3661 coap_lg_srcv_reference_lkd(lg_srcv);
3662 lg_srcv_is_refed = 1;
3666 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3668 (
const uint8_t *)
"Missing interim block");
3670 goto skip_app_handler;
3673 if (fmt != lg_srcv->content_format) {
3674 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3675 (
const uint8_t *)
"Content-Format mismatch");
3680#if COAP_Q_BLOCK_SUPPORT
3682 if (total != lg_srcv->total_len) {
3684 (
const uint8_t *)
"Size1 mismatch");
3694 lg_srcv->last_type = pdu->
type;
3697 saved_num = block.
num;
3698 saved_offset = offset;
3700 while (offset < saved_offset + length) {
3701 uint32_t block_m = block.
m;
3709 if (block.
bert && offset + 1024 < saved_offset + length)
3718#if COAP_Q_BLOCK_SUPPORT
3720 request_missing = 1;
3728 offset = block.
num << (block.
szx + 4);
3733#if COAP_Q_BLOCK_SUPPORT
3734 if (request_missing) {
3735 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3736 goto skip_app_handler;
3742#if COAP_Q_BLOCK_SUPPORT
3743 lg_srcv->rec_blocks.processing_payload_set =
3746 if (lg_srcv->total_len < saved_offset + length) {
3747 lg_srcv->total_len = saved_offset + length;
3750#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3751 !resource->is_proxy_uri && \
3752 !resource->is_reverse_proxy && \
3753 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3754 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3756 if (USE_BLOCK_DATA_HANDLER) {
3761 &lg_srcv->body_data,
3762 length, data, saved_offset,
3763 lg_srcv->total_len));
3766 goto skip_app_handler;
3770 saved_offset, lg_srcv->total_len);
3771 if (!lg_srcv->body_data) {
3773 (
const uint8_t *)
"Memory issue");
3775 goto skip_app_handler;
3779#if COAP_Q_BLOCK_SUPPORT
3781 goto q_block_1_check;
3803 goto skip_app_handler;
3805#if COAP_Q_BLOCK_SUPPORT
3815#if COAP_Q_BLOCK_SUPPORT
3820 if (lg_srcv->rec_blocks.used == 1 &&
3824 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3826 saved_num = block.
num = lg_srcv->rec_blocks.range[0].end;
3828 }
else if (lg_srcv->rec_blocks.used > 1 &&
3830 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].begin <
3831 lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end &&
3832 (lg_srcv->rec_blocks.range[lg_srcv->rec_blocks.used-1].end %
COAP_MAX_PAYLOADS(session)) + 1
3839 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3840 goto skip_app_handler;
3841 }
else if (lg_srcv->rec_blocks.used > 1 &&
3849 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3850 goto skip_app_handler;
3855 goto skip_app_handler;
3859 goto skip_app_handler;
3865 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3866 max_block_szx < block.
aszx) {
3867 block.
aszx = max_block_szx;
3898 LL_FOREACH(session->lg_srcv, sg) {
3899 if (lg_srcv == sg) {
3905 goto skip_app_handler;
3908 if (lg_srcv->last_token) {
3909 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3917#if COAP_Q_BLOCK_SUPPORT
3922 lg_srcv->no_more_seen = 1;
3936#if COAP_Q_BLOCK_SUPPORT
3938 coap_request_missing_q_block1(session, lg_srcv, block.
num);
3942 goto skip_app_handler;
3952 if (lg_srcv->observe_set) {
3954 lg_srcv->observe_length, lg_srcv->observe);
3957 if (lg_srcv->body_data) {
3966 if (USE_BLOCK_DATA_HANDLER) {
3975 lg_srcv->dont_timeout = 1;
3976 *pfree_lg_srcv = lg_srcv;
3980 if (lg_srcv_is_refed)
3981 coap_lg_srcv_release_lkd(session, lg_srcv);
3985 LL_DELETE(session->lg_srcv, lg_srcv);
3986 coap_block_delete_lg_srcv(session, lg_srcv);
3990 if (lg_srcv_is_refed)
3991 coap_lg_srcv_release_lkd(session, lg_srcv);
3996#if COAP_CLIENT_SUPPORT
3997#if COAP_Q_BLOCK_SUPPORT
3999derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
4000 uint32_t value = **bp & 0x1f;
4004 }
else if (value == 24) {
4006 return (uint32_t)-1;
4010 }
else if (value == 25) {
4012 return (uint32_t)-1;
4020 return (uint32_t)-1;
4021 value = (uint32_t)(**bp) << 24;
4023 value |= **bp << 16;
4041 if (sent || lg_xmit || lg_crcv) {
4045 const uint8_t *data;
4055 }
else if (lg_xmit) {
4058 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
4059 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
4062 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
4063 lg_xmit->data_info->length - offset;
4066 sent = lg_crcv->sent_pdu;
4075 ++lg_crcv->retry_counter);
4089 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
4090#if COAP_OSCORE_SUPPORT
4091 if (session->oscore_encryption &&
4098 lg_crcv->state_token = token;
4148 int lg_crcv_is_refed = 0;
4153 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
4156 lg_crcv = coap_find_lg_crcv(session, rcvd);
4159 coap_lg_crcv_reference_lkd(lg_crcv);
4160 lg_crcv_is_refed = 1;
4170 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4171 1 << (block.
szx + 4), block.
num);
4177 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
4183 block.
num = (uint32_t)((chunk >> (block.
szx + 4)) - 1);
4184 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
4185#if COAP_Q_BLOCK_SUPPORT
4186 lg_xmit->send_blocks.range[0].begin = block.
num;
4187 lg_xmit->send_blocks.range[0].end = (uint32_t)((lg_xmit->
data_info->
length -1) / chunk);
4190 coap_log_debug(
"new Block size is %u, block number %u completed\n",
4191 1 << (block.
szx + 4), block.
num);
4196 track_echo(session, rcvd);
4197#if COAP_Q_BLOCK_SUPPORT
4200 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
4201 goto skip_app_handler;
4216 goto skip_app_handler;
4221 if ((block.
num + 1) * chunk < lg_xmit->data_info->length) {
4237 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
4240 assert(len <=
sizeof(buf));
4242 memcpy(buf, new_token->
s, len);
4261 offset = block.
num * chunk;
4263 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
4266 ((pdu->
max_size - token_options) /1024) * 1024;
4278#if COAP_CONSTRAINED_STACK
4280 static uint8_t l_data[1024];
4282 uint8_t l_data[1024];
4286 assert(chunk <= 1024);
4288 block.
num * chunk, l_data, &l_length,
4303#if COAP_Q_BLOCK_SUPPORT
4306 if (coap_send_q_block1(session, block, pdu,
4309 goto skip_app_handler;
4316 goto skip_app_handler;
4326 int observe_action = -1;
4329 goto lg_xmit_finished;
4339 goto lg_xmit_finished;
4345 goto call_app_handler;
4347 goto lg_xmit_finished;
4349 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
4350 goto skip_app_handler;
4351#if COAP_Q_BLOCK_SUPPORT
4356 goto skip_app_handler;
4360 const uint8_t *data;
4365 uint16_t fmt = fmt_opt ?
4375 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
4376 goto skip_app_handler;
4381 const uint8_t *bp = data;
4384 for (i = 0; (bp < data + length) &&
4386 if ((*bp & 0xc0) != 0x00)
4388 block.
num = derive_cbor_value(&bp, data + length - bp);
4390 if (block.
num > (1 << 20) -1)
4392 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4397 if (lg_xmit->send_blocks.used) {
4399 block.
num = lg_xmit->send_blocks.range[0].begin;
4400 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4401 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_MISSING);
4403 goto skip_app_handler;
4406 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4409 goto lg_xmit_finished;
4411 goto call_app_handler;
4423 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4434 lg_crcv->sent_pdu->lg_xmit = 0;
4446 LL_DELETE(session->
lg_xmit, lg_xmit);
4450 if (lg_crcv_is_refed)
4451 coap_lg_crcv_release_lkd(session, lg_crcv);
4455 if (lg_crcv_is_refed)
4456 coap_lg_crcv_release_lkd(session, lg_crcv);
4469 const uint8_t *data,
size_t offset,
size_t total) {
4482 const uint8_t *data,
size_t offset,
size_t total) {
4485 if (body_data ==
NULL && total) {
4488 if (body_data ==
NULL)
4492 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4498 if (offset + length <= total && body_data->length >= total) {
4499 memcpy(&body_data->
s[offset], data, length);
4515 memcpy(&body_data->
s[offset], data, length);
4524#if COAP_CLIENT_SUPPORT
4544#if COAP_Q_BLOCK_SUPPORT
4548 uint16_t block_opt = 0;
4550 int ack_rst_sent = 0;
4554 memset(&block, 0,
sizeof(block));
4555#if COAP_Q_BLOCK_SUPPORT
4556 memset(&qblock, 0,
sizeof(qblock));
4558 lg_crcv = coap_find_lg_crcv(session, rcvd);
4566 const uint8_t *data;
4569 size_t size2 = size_opt ?
4581#if COAP_Q_BLOCK_SUPPORT
4584 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4590 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4595 track_echo(session, rcvd);
4596 if (have_block && (block.
m || length)) {
4600 uint16_t fmt = fmt_opt ?
4607 size_t saved_offset;
4610#if COAP_Q_BLOCK_SUPPORT
4613 coap_log_info(
"Q-Block2: Size2 and ETag options required\n");
4630 goto expire_lg_crcv;
4633 chunk = (size_t)1 << (block.
szx + 4);
4634 offset = block.
num * chunk;
4635 if (size2 < (offset + length)) {
4637 size2 = offset + length + 1;
4639 size2 = offset + length;
4641 saved_offset = offset;
4643 if (lg_crcv->initial) {
4644#if COAP_Q_BLOCK_SUPPORT
4647 lg_crcv->initial = 0;
4648 if (lg_crcv->body_data) {
4650 lg_crcv->body_data =
NULL;
4653 lg_crcv->etag_length = (uint8_t)
min(
coap_opt_length(etag_opt),
sizeof(lg_crcv->etag));
4654 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4655 lg_crcv->etag_set = 1;
4657 lg_crcv->etag_set = 0;
4659 lg_crcv->total_len = size2;
4660 lg_crcv->content_format = fmt;
4661 lg_crcv->szx = block.
szx;
4662 lg_crcv->block_option = block_opt;
4663 lg_crcv->last_type = rcvd->
type;
4664 lg_crcv->rec_blocks.used = 0;
4665 lg_crcv->rec_blocks.total_blocks = 0;
4666#if COAP_Q_BLOCK_SUPPORT
4667 lg_crcv->rec_blocks.processing_payload_set = 0;
4670 if (lg_crcv->total_len < size2)
4671 lg_crcv->total_len = size2;
4675 uint8_t max_block_szx;
4678 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4679 max_block_szx = block.
szx;
4681 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4685 (size2 > max_body)) {
4688 if (max_body_size == 0 || max_body < max_body_size) {
4689 max_body_size = max_body;
4701 lg_crcv->etag, lg_crcv->etag_length)) {
4705 ++lg_crcv->retry_counter);
4709#if COAP_Q_BLOCK_SUPPORT
4714 coap_log_warn(
"Data body updated during receipt - new request started\n");
4718 lg_crcv->initial = 1;
4720 lg_crcv->body_data =
NULL;
4733 (0 << 4) | (0 << 3) | block.
aszx),
4739 goto skip_app_handler;
4741 }
else if (lg_crcv->etag_set) {
4747 if (fmt != lg_crcv->content_format) {
4751#if COAP_Q_BLOCK_SUPPORT
4754 size2, lg_crcv->total_len);
4758 if (block.
num == 0) {
4764 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4765 lg_crcv->observe_set = 1;
4767 lg_crcv->observe_set = 0;
4771 while (offset < saved_offset + length) {
4773#if COAP_Q_BLOCK_SUPPORT
4777 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4778 1 << (block.
szx + 4), block.
num);
4785#if COAP_Q_BLOCK_SUPPORT
4787 if (lg_crcv->rec_blocks.used > 1 &&
4788 (block.
num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end ||
4789 block.
num == lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end - 1) &&
4790 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].begin <
4791 lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end &&
4792 (lg_crcv->rec_blocks.range[lg_crcv->rec_blocks.used-1].end %
COAP_MAX_PAYLOADS(session)) + 1
4799 coap_request_missing_q_block2(session, lg_crcv);
4800 }
else if (lg_crcv->rec_blocks.used &&
4801 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4802 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4803 coap_request_missing_q_block2(session, lg_crcv);
4806 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4812 offset = block.
num << (block.
szx + 4);
4818 if (updated_block) {
4825 if (size2 < saved_offset + length) {
4826 size2 = saved_offset + length;
4833 &lg_crcv->body_data,
4835 saved_offset, size2));
4841 saved_offset, size2);
4842 if (lg_crcv->body_data ==
NULL) {
4855#if COAP_Q_BLOCK_SUPPORT
4861 if (check_all_blocks_in_for_payload_set(session,
4862 &lg_crcv->rec_blocks)) {
4863 block.
num = lg_crcv->rec_blocks.range[0].end;
4865 lg_crcv->rec_blocks.processing_payload_set =
4867 if (check_any_blocks_next_payload_set(session,
4868 &lg_crcv->rec_blocks)) {
4870 coap_request_missing_q_block2(session, lg_crcv);
4871 goto skip_app_handler;
4876 goto skip_app_handler;
4880 goto skip_app_handler;
4904 ((block.
num + 1) << 4) |
4905 (block.
m << 3) | block.
aszx),
4910 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4915 goto skip_app_handler;
4918 goto skip_app_handler;
4923#if COAP_Q_BLOCK_SUPPORT
4925 lg_crcv->total_len : size2;
4934 if (lg_crcv->app_token)
4936 lg_crcv->app_token->s);
4939 goto call_app_handler;
4941#if COAP_Q_BLOCK_SUPPORT
4947 if (lg_crcv->observe_set) {
4949 lg_crcv->observe_length, lg_crcv->observe);
4951 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4952#if COAP_Q_BLOCK_SUPPORT
4961 lg_crcv->total_len : saved_offset + length;
4969#if COAP_Q_BLOCK_SUPPORT
4971 lg_crcv->total_len : size2;
4984 if (lg_crcv->app_token)
4986 lg_crcv->app_token->s);
4989 body_free = lg_crcv->body_data;
4990 lg_crcv->body_data =
NULL;
4994 if (lg_crcv->observe_set == 0) {
4996 LL_DELETE(session->lg_crcv, lg_crcv);
4997 coap_block_delete_lg_crcv(session, lg_crcv);
4998 goto skip_app_handler;
5001 lg_crcv->initial = 1;
5004 goto skip_app_handler;
5019 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
5020 lg_crcv->observe_set = 1;
5022 lg_crcv->observe_set = 0;
5031 goto expire_lg_crcv;
5036#if COAP_OSCORE_SUPPORT
5037 if (check_freshness(session, rcvd,
5038 (session->oscore_encryption == 0) ? sent :
NULL,
5041 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
5043 goto skip_app_handler;
5044 goto expire_lg_crcv;
5053 const uint8_t *data;
5060 ++lg_crcv->retry_counter);
5068#if COAP_PROXY_SUPPORT
5069 coap_proxy_req_t *proxy_req = session->
context->proxy_list_count ?
5070 coap_proxy_map_outgoing_request(session, rcvd,
NULL) :
NULL;
5076 proxy_req->token_used =
new;
5077 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"upd");
5081 coap_log_debug(
"* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
5085 goto skip_app_handler;
5088 goto expire_lg_crcv;
5091 goto expire_lg_crcv;
5093 if (!block.
m && !lg_crcv->observe_set) {
5103 lg_crcv->last_used -= adjust;
5117#
if COAP_Q_BLOCK_SUPPORT
5124 goto skip_app_handler;
5127 const uint8_t *data;
5131#if COAP_Q_BLOCK_SUPPORT
5132 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
5138 if (block.
num != 0) {
5140 size_t chunk = (size_t)1 << (block.
szx + 4);
5145 goto call_app_handler;
5148#if COAP_Q_BLOCK_SUPPORT
5152 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
5158 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5161 LL_PREPEND(session->lg_crcv, lg_crcv);
5162 return coap_handle_response_get_block(context, session, sent, rcvd,
5175 track_echo(session, rcvd);
5177 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5180 LL_PREPEND(session->lg_crcv, lg_crcv);
5181 return coap_handle_response_get_block(context, session, sent, rcvd,
5193 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
5196 LL_PREPEND(session->lg_crcv, lg_crcv);
5197 return coap_handle_response_get_block(context, session, sent, rcvd,
5215 if (lg_crcv->app_token)
5217 lg_crcv->app_token->s);
5221 LL_DELETE(session->lg_crcv, lg_crcv);
5222 coap_block_delete_lg_crcv(session, lg_crcv);
5234#if COAP_SERVER_SUPPORT
5243 if (response->
code == 0)
5245 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
5253#if COAP_CLIENT_SUPPORT
5256 uint64_t token_match =
5262 if (session->lg_crcv) {
5263 LL_FOREACH(session->lg_crcv, lg_crcv) {
5268 lg_crcv->app_token->s);
5276 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_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
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
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.
union coap_lg_xmit_t::@175225305117254073376154331210377003213255310337 b
coap_pdu_t * sent_pdu
The sent pdu with all the data.
uint32_t ref
Reference count.
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