21#define min(a,b) ((a) < (b) ? (a) : (b))
25#ifndef COAP_ETAG_MAX_BYTES
26#define COAP_ETAG_MAX_BYTES 4
28#if COAP_ETAG_MAX_BYTES < 1 || COAP_ETAG_MAX_BYTES > 8
29#error COAP_ETAG_MAX_BYTES byte size invalid
32#if COAP_Q_BLOCK_SUPPORT
78 if (block->
szx == 7) {
90 if (block->
m && (length % 1024) != 0) {
92 length - (length % 1024), length);
93 length -= length % 1024;
126 block->
m = block_b.
m;
136 unsigned int blk_size,
size_t total) {
138 size_t avail = pdu->
max_size - token_options;
139 unsigned int start = num << (blk_size + 4);
140 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
142 assert(start <= total);
143 memset(block, 0,
sizeof(*block));
145 block->
szx = block->
aszx = blk_size;
146 if (can_use_bert && blk_size == 6 && avail >= 1024 && session !=
NULL &&
151 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
153 block->
chunk_size = (size_t)1 << (blk_size + 4);
154 if (avail < block->chunk_size && (total - start) >= avail) {
160 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
163 new_blk_size =
coap_flsll((
long long)avail) - 5;
166 block->
szx = new_blk_size;
167 block->
num <<= szx - block->
szx;
168 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
179 unsigned char buf[4];
184 start = block->
num << (block->
szx + 4);
185 if (block->
num != 0 && data_length <= start) {
195 block->
szx, data_length))
200 ((block_b.
num << 4) |
213 unsigned char buf[4];
217 start = block->
num << (block->
szx + 4);
218 if (block->
num != 0 && data_length <= start) {
226 block->
szx, data_length))
241 unsigned int block_num,
unsigned char block_szx) {
243 start = block_num << (block_szx + 4);
249 min(len - start, ((
size_t)1 << (block_szx + 4))),
256 unsigned int start = block->
num << (block->
szx + 4);
264 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
266 max_size = (size_t)1 << (block->
szx + 4);
271 min(len - start, max_size),
286 unsigned char buf[4];
288 int block2_requested = 0;
289#if COAP_SERVER_SUPPORT
291 coap_digest_t digest;
292 coap_digest_ctx_t *dctx =
NULL;
295 memset(&block2, 0,
sizeof(block2));
302 block2_requested = 1;
303 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
306 length >> (block2.
szx + 4));
314#if COAP_SERVER_SUPPORT
317 dctx = coap_digest_setup();
320 if (request && request->
session &&
324 if (!coap_digest_update(dctx, data, length))
326 if (!coap_digest_final(dctx, &digest))
329 memcpy(&etag, digest.key,
sizeof(etag));
330#if COAP_ETAG_MAX_BYTES != 8
353 if (block2_requested) {
406#if COAP_SERVER_SUPPORT
407 coap_digest_free(dctx);
416 uint32_t block_mode) {
424 uint32_t block_mode) {
430#if ! COAP_Q_BLOCK_SUPPORT
438 size_t max_block_size) {
449 switch (max_block_size) {
460 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%" PRIuS ")\n",
465 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
466 context->
block_mode &= ~COAP_BLOCK_MAX_SIZE_MASK;
473 const uint8_t *b,
size_t blen) {
474 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
481 uint64_t token_match =
485 LL_FOREACH(session->
lg_xmit, lg_xmit) {
491#if COAP_CLIENT_SUPPORT
522 LL_PREPEND(session->
lg_xmit, lg_xmit);
527#if COAP_CLIENT_SUPPORT
531 lg_crcv = coap_find_lg_crcv(session, pdu);
533 lg_xmit->b.b1.state_token = lg_crcv->state_token;
545#if COAP_CLIENT_SUPPORT
553 ret = coap_cancel_observe_lkd(session, token, type);
569 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
574 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
575 if (lg_crcv->observe_set) {
576 if ((!token && !lg_crcv->app_token->length) || (token &&
582#if COAP_Q_BLOCK_SUPPORT
588 lg_crcv->obs_token[0] ?
589 lg_crcv->obs_token[0] :
599 lg_crcv->observe_set = 0;
609 if (lg_crcv->o_block_option) {
612 lg_crcv->o_blk_size),
615 if (lg_crcv->obs_data) {
616 coap_add_data_large_request_lkd(session, pdu,
617 lg_crcv->obs_data->length,
618 lg_crcv->obs_data->data,
NULL,
NULL);
620 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
631#if COAP_Q_BLOCK_SUPPORT
633 if (using_q_block1) {
634 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
653 uint64_t token_match =
657 LL_FOREACH(session->lg_crcv, lg_crcv) {
675 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
677 if (m_lg_crcv->obs_data) {
678 m_lg_crcv->obs_data->ref++;
679 lg_crcv->obs_data = m_lg_crcv->obs_data;
681 LL_PREPEND(session->lg_crcv, lg_crcv);
687#if COAP_OSCORE_SUPPORT
701 lg_crcv = coap_find_lg_crcv(session, pdu);
705 ++lg_crcv->retry_counter);
733#if COAP_SERVER_SUPPORT
750 LL_FOREACH(session->
lg_xmit, lg_xmit) {
757 lg_xmit->b.b2.query ?
758 lg_xmit->b.b2.query : &empty)) {
767 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
793#if COAP_Q_BLOCK_SUPPORT
799 int have_block_defined = 0;
801 uint8_t max_blk_size;
803 size_t token_options;
806#if COAP_Q_BLOCK_SUPPORT
810#if !COAP_SERVER_SUPPORT
816 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
824 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
838#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
839#if UINT_MAX < MAX_BLK_LEN
841#define MAX_BLK_LEN UINT_MAX
849#if COAP_SERVER_SUPPORT
861 coap_digest_t digest;
862 coap_digest_ctx_t *dctx = coap_digest_setup();
868 if (coap_digest_update(dctx, data, length)) {
869 if (coap_digest_final(dctx, &digest)) {
870 memcpy(&etag, digest.key,
sizeof(etag));
871#if COAP_ETAG_MAX_BYTES != 8
877 coap_digest_free(dctx);
897 if (etag == etag_r) {
912#if COAP_Q_BLOCK_SUPPORT
928 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
932#if COAP_CLIENT_SUPPORT
935 LL_DELETE(session->
lg_xmit, lg_xmit);
946#if COAP_Q_BLOCK_SUPPORT
947 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
960#if COAP_SERVER_SUPPORT
965 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
968 LL_DELETE(session->
lg_xmit, lg_xmit);
975#if COAP_OSCORE_SUPPORT
976 if (session->oscore_encryption) {
984 avail = pdu->
max_size - token_options;
987#if COAP_OSCORE_SUPPORT
996 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1002 if (max_blk_size && blk_size > max_blk_size)
1003 blk_size = max_blk_size;
1007 if (block.
szx < blk_size)
1008 blk_size = block.
szx;
1009 have_block_defined = 1;
1011#if COAP_Q_BLOCK_SUPPORT
1014 if (have_block_defined) {
1016 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1020 if (block.
szx < blk_size)
1021 blk_size = block.
szx;
1022 have_block_defined = 1;
1023 option = alt_option;
1028 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1030 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1034 chunk = (size_t)1 << (blk_size + 4);
1035 if ((have_block_defined && block.
num != 0) || single_request ||
1040 if (length >= block.
num * chunk) {
1041#if COAP_SERVER_SUPPORT
1047 (
unsigned int)length),
1061 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1066 if (chunk > length - block.
num * chunk)
1067 rem = length - block.
num * chunk;
1074 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1099 lg_xmit->blk_size = blk_size;
1100 lg_xmit->option = option;
1102 if (!lg_xmit->data_info)
1104 lg_xmit->data_info->ref = 0;
1105 lg_xmit->data_info->data = data;
1106 lg_xmit->data_info->length = length;
1107#if COAP_Q_BLOCK_SUPPORT
1108 lg_xmit->non_timeout_random_ticks =
1111 lg_xmit->data_info->get_func = get_func;
1112 lg_xmit->data_info->release_func = release_func;
1113 lg_xmit->data_info->app_ptr = app_ptr;
1120 if (!lg_xmit->b.b1.app_token)
1128 lg_xmit->b.b1.count = 1;
1130 lg_xmit->b.b1.count);
1139 (
unsigned int)length),
1152 lg_xmit->b.b2.resource = resource;
1155 if (lg_xmit->b.b2.query) {
1156 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1159 lg_xmit->b.b2.query =
NULL;
1164 sizeof(lg_xmit->b.b2.rtag));
1166 lg_xmit->b.b2.rtag_set = 1;
1168 lg_xmit->b.b2.rtag_set = 0;
1170 lg_xmit->b.b2.etag = etag;
1171 lg_xmit->b.b2.request_method = request_method;
1178 lg_xmit->b.b2.maxage_expire = 0;
1183 (
unsigned int)length),
1188 blk_size, lg_xmit->data_info->length))
1195 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1203 avail = pdu->
max_size - token_options;
1208#if COAP_OSCORE_SUPPORT
1211 if (avail < (ssize_t)chunk) {
1214 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1217 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1218 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1219 lg_xmit->blk_size = blk_size;
1220 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1226 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1231 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1232 rem = lg_xmit->data_info->length - block.
num * chunk;
1234#if COAP_CONSTRAINED_STACK
1236 static uint8_t l_data[1024];
1238 uint8_t l_data[1024];
1242 assert(rem <= 1024);
1243 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1254 lg_xmit->b.b1.bert_size = rem;
1256 lg_xmit->last_block = -1;
1259 LL_PREPEND(session->
lg_xmit,lg_xmit);
1262 if (have_block_defined) {
1266 (0 << 4) | (0 << 3) | blk_size), buf);
1273 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1294 }
else if (release_func) {
1300#if COAP_CLIENT_SUPPORT
1305 const uint8_t *data,
1312 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1313 release_func, app_ptr);
1322 const uint8_t *data,
1336 length, data, release_func,
NULL, app_ptr, 0, 0);
1349 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1350 release_func, get_func, app_ptr);
1370 length,
NULL, release_func, get_func,
1375#if COAP_SERVER_SUPPORT
1382 uint16_t media_type,
1386 const uint8_t *data,
1392 ret = coap_add_data_large_response_lkd(resource, session, request,
1393 response, query, media_type, maxage, etag,
1394 length, data, release_func, app_ptr);
1405 uint16_t media_type,
1409 const uint8_t *data,
1413 unsigned char buf[4];
1415 int block_requested = 0;
1416 int single_request = 0;
1417#if COAP_Q_BLOCK_SUPPORT
1418 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1424 memset(&block, 0,
sizeof(block));
1431 block_requested = 1;
1432 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1435 length >> (block.
szx + 4));
1440#if COAP_Q_BLOCK_SUPPORT
1442 block_requested = 1;
1443 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1446 length >> (block.
szx + 4));
1450 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1471 if (block_requested) {
1495 query, maxage, etag, length, data,
1496 release_func,
NULL, app_ptr, single_request,
1499 goto error_released;
1509#if COAP_ERROR_PHRASE_LENGTH > 0
1527#if COAP_Q_BLOCK_SUPPORT
1537 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1541 LL_DELETE(session->
lg_xmit, lg_xmit);
1545 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1551 if (lg_xmit->
last_sent + partial_timeout <= now) {
1554#if COAP_CLIENT_SUPPORT
1558 LL_DELETE(session->
lg_xmit, lg_xmit);
1565 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1566 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1575#if COAP_CLIENT_SUPPORT
1576#if COAP_Q_BLOCK_SUPPORT
1581 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1593 pdu->
type = lg_crcv->last_type;
1605 int block_payload_set = -1;
1616 if (lg_crcv->rec_blocks.used &&
1617 (lg_crcv->rec_blocks.used < 2 ||
1620 block = lg_crcv->rec_blocks.range[0].end + 1;
1621 block_size = (size_t)1 << (lg_crcv->szx + 4);
1622 sofar = block * block_size;
1623 if (sofar < lg_crcv->total_len) {
1626 pdu = coap_build_missing_pdu(session, lg_crcv);
1632 (block << 4) | (1 << 3) | lg_crcv->szx),
1640 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1641 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1642 lg_crcv->rec_blocks.range[i].begin != 0) {
1645 pdu = coap_build_missing_pdu(session, lg_crcv);
1650 if (block_payload_set == -1)
1652 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1656 (block << 4) | (0 << 3) | lg_crcv->szx),
1660 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1661 block = lg_crcv->rec_blocks.range[i].end;
1664 block_size = (size_t)1 << (lg_crcv->szx + 4);
1665 sofar = (block + 1) * block_size;
1666 if (sofar < lg_crcv->total_len) {
1669 pdu = coap_build_missing_pdu(session, lg_crcv);
1673 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1675 if (block_payload_set == -1)
1677 for (; block < (ssize_t)sofar &&
1681 (block << 4) | (0 << 3) | lg_crcv->szx),
1688 lg_crcv->rec_blocks.retry++;
1689 if (block_payload_set != -1)
1690 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1705#if COAP_Q_BLOCK_SUPPORT
1711#if COAP_Q_BLOCK_SUPPORT
1713 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1719 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1723#if COAP_Q_BLOCK_SUPPORT
1725 size_t scaled_timeout = receive_timeout *
1726 ((size_t)1 << lg_crcv->rec_blocks.retry);
1734 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1735 coap_request_missing_q_block2(session, lg_crcv);
1737 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1738 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1746 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1747 lg_crcv->last_used + partial_timeout <= now) {
1748#if COAP_Q_BLOCK_SUPPORT
1752 LL_DELETE(session->lg_crcv, lg_crcv);
1753 coap_block_delete_lg_crcv(session, lg_crcv);
1754 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1756 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1757 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1766#if COAP_SERVER_SUPPORT
1767#if COAP_Q_BLOCK_SUPPORT
1779 if (lg_srcv->last_token)
1780 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1795 assert(block >= 0 && block < (1 << 20));
1797 if (block < 0 || block >= (1 << 20)) {
1799 }
else if (block < 24) {
1802 }
else if (block < 0x100) {
1806 }
else if (block < 0x10000) {
1809 val[1] = block >> 8;
1810 val[2] = block & 0xff;
1814 val[1] = block >> 16;
1815 val[2] = (block >> 8) & 0xff;
1816 val[3] = block & 0xff;
1832 for (i = 0; i < rec_blocks->
used; i++) {
1833 if (block_num < rec_blocks->range[i].begin)
1835 if (block_num <= rec_blocks->range[i].end)
1841#if COAP_SERVER_SUPPORT
1843check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1844 if (rec_blocks->
used == 0) {
1845 return block_num == 0 ? 1 : 0;
1847 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1864 for (i = 0; i < rec_blocks->
used; i++) {
1865 if (block < rec_blocks->range[i].begin)
1867 if (block < rec_blocks->range[i].end)
1873#if COAP_CLIENT_SUPPORT
1874#if COAP_Q_BLOCK_SUPPORT
1878 if (rec_blocks->
used &&
1880 rec_blocks->processing_payload_set)
1888 if (rec_blocks->
used > 1 &&
1890 rec_blocks->processing_payload_set)
1897#if COAP_SERVER_SUPPORT
1908#if COAP_Q_BLOCK_SUPPORT
1914#if COAP_Q_BLOCK_SUPPORT
1916 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1922 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1923 if (lg_srcv->dont_timeout) {
1930#if COAP_Q_BLOCK_SUPPORT
1932 size_t scaled_timeout = receive_timeout *
1933 ((size_t)1 << lg_srcv->rec_blocks.retry);
1939 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1942 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1943 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1945 size_t last_payload_block;
1947 size_t no_blocks = 0;
1950 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1951 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1952 lg_srcv->rec_blocks.range[i].begin != 0) {
1954 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1956 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1957 block = lg_srcv->rec_blocks.range[i].end;
1960 if (no_blocks == 0 && block == (
int)final_block)
1966 if (final_block > last_payload_block) {
1967 final_block = last_payload_block;
1969 no_blocks += final_block - block;
1970 if (no_blocks == 0) {
1972 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1974 if (final_block > last_payload_block) {
1975 final_block = last_payload_block;
1980 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1981 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1982 lg_srcv->rec_blocks.range[i].begin != 0) {
1985 pdu = pdu_408_build(session, lg_srcv);
1990 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1991 if (!add_408_block(pdu, block)) {
1996 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1997 block = lg_srcv->rec_blocks.range[i].end;
2001 for (; block <= (int)final_block; block++) {
2003 pdu = pdu_408_build(session, lg_srcv);
2007 if (!add_408_block(pdu, block)) {
2013 lg_srcv->rec_blocks.retry++;
2016 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2017 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2024 if (lg_srcv->no_more_seen)
2026 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2027#if COAP_Q_BLOCK_SUPPORT
2044 if (lg_srcv->last_token)
2045 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2047 (
const uint8_t *)
"Missing interim block");
2051 LL_DELETE(session->lg_srcv, lg_srcv);
2052 coap_block_delete_lg_srcv(session, lg_srcv);
2053 }
else if (lg_srcv->last_used) {
2055 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2056 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2065#if COAP_Q_BLOCK_SUPPORT
2074 coap_send_pdu_t send_pdu) {
2079 const uint8_t *ptoken;
2081 size_t ltoken_length;
2082 uint32_t delayqueue_cnt = 0;
2085 if (send_pdu == COAP_SEND_INC_PDU)
2094 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2095 LL_FOREACH(session->
delayqueue, delayqueue) {
2129 if (send_pdu == COAP_SEND_INC_PDU &&
2140 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2166 ltoken_length, ptoken, &drop_options,
2172 ((block.
num) << 4) |
2183#if COAP_CONSTRAINED_STACK
2185 static uint8_t l_data[1024];
2187 uint8_t l_data[1024];
2191 assert(chunk <= 1024);
2193 block.
num * chunk, l_data, &l_length,
2235#if COAP_CLIENT_SUPPORT
2248 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2249 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2251 if (now <= non_timeout) {
2253 *tim_rem = non_timeout - now;
2256 timed_out = now - non_timeout;
2262 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2264 memset(&block, 0,
sizeof(block));
2265 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2268 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2269 if (*tim_rem > non_timeout) {
2270 *tim_rem = non_timeout;
2284 LL_DELETE(session->
lg_xmit, lg_xmit);
2288 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2299#if COAP_SERVER_SUPPORT
2312 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2313 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2315 if (now <= non_timeout) {
2317 *tim_rem = non_timeout - now;
2320 timed_out = now - non_timeout;
2326 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2328 memset(&block, 0,
sizeof(block));
2329 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2333 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2334 if (*tim_rem > non_timeout) {
2335 *tim_rem = non_timeout;
2349 LL_DELETE(session->
lg_xmit, lg_xmit);
2353 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2365#if COAP_CLIENT_SUPPORT
2379 if (opt && lg_crcv) {
2380 int observe_action = -1;
2387 if (lg_crcv->obs_token_cnt <= block_num) {
2391 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2394 lg_crcv->obs_token = tmp;
2395 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2396 lg_crcv->obs_token[i] =
NULL;
2401 if (lg_crcv->obs_token_cnt <= block_num)
2402 lg_crcv->obs_token_cnt = block_num + 1;
2405 if (lg_crcv->obs_token[block_num] ==
NULL)
2409 if (block_num < lg_crcv->obs_token_cnt) {
2410 return lg_crcv->obs_token[block_num];
2417#if COAP_Q_BLOCK_SUPPORT
2422 coap_send_pdu_t send_request) {
2425 uint64_t token_match =
2429 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2438 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2443#if COAP_SERVER_SUPPORT
2444#if COAP_Q_BLOCK_SUPPORT
2455 coap_send_pdu_t send_response) {
2460 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2465 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2468 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2478 if (data_info->
ref > 0) {
2490#if COAP_CLIENT_SUPPORT
2491#if COAP_Q_BLOCK_SUPPORT
2523 11, (
const uint8_t *)
".well-known");
2525 4, (
const uint8_t *)
"core");
2538 (0 << 4) | (0 << 3) | 0),
2558 if (lg_crcv ==
NULL)
2561 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2565 lg_crcv->initial = 1;
2583 const uint8_t *data;
2586 if (data_len < lg_xmit->data_info->length) {
2597 if (!lg_crcv->app_token) {
2598 coap_block_delete_lg_crcv(session, lg_crcv);
2604 lg_crcv->retry_counter = 1;
2605 lg_crcv->state_token = state_token;
2612 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2620 lg_crcv->o_blk_size = block.
aszx;
2631#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2634 if (lg_crcv ==
NULL)
2638 if (lg_crcv->obs_data) {
2640 lg_crcv->obs_data =
NULL;
2646 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2655#if COAP_SERVER_SUPPORT
2659#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2662 if (lg_srcv ==
NULL)
2677 if (lg_xmit ==
NULL)
2680 if (lg_xmit->
ref > 0) {
2697#if COAP_SERVER_SUPPORT
2704add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2705 uint32_t *count, uint32_t max_count) {
2708 for (i = 0; i < *count && *count < max_count; i++) {
2709 if (num == out_blocks[i].num)
2711 else if (num < out_blocks[i].num) {
2713 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2714 out_blocks[i].num = num;
2715 out_blocks[i].is_continue = is_continue;
2720 if (*count < max_count) {
2721 out_blocks[i].num = num;
2722 out_blocks[i].is_continue = is_continue;
2755 uint16_t block_opt = 0;
2756 send_track *out_blocks =
NULL;
2757 const char *error_phrase;
2762 uint32_t request_cnt, i;
2765#if COAP_Q_BLOCK_SUPPORT
2770 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2777 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2778 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2779 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2781 goto skip_app_handler;
2788 if (block.
num == 0) {
2792 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2793 if (lg_xmit ==
NULL)
2796#if COAP_Q_BLOCK_SUPPORT
2802 goto internal_issue;
2815 if (etag == lg_xmit->
b.
b2.
etag) {
2829 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2832 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2833 block.
num, block.
m);
2835 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2836 1 << (block.
szx + 4), block.
num, block.
m);
2839 if (block.
num == 0) {
2840 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2845 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2847 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2849 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2850 1 << (block.
szx + 4), block.
num);
2853 "next block is not aligned on requested block size "
2854 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2856 (1 << (block.
szx + 4)),
2857 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2860 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2861 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2873#if COAP_Q_BLOCK_SUPPORT
2886 sizeof(
"Changing blocksize during request invalid")-1,
2887 (
const uint8_t *)
"Changing blocksize during request invalid");
2889 goto skip_app_handler;
2891#if COAP_Q_BLOCK_SUPPORT
2896 goto call_app_handler;
2900 num + i < max_block; i++) {
2901 add_block_send(num + i, 1, out_blocks, &request_cnt,
2909 num + i < max_block; i++) {
2910 add_block_send(num + i, 0, out_blocks, &request_cnt,
2915 add_block_send(num, 0, out_blocks, &request_cnt,
2918 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2922 if (request_cnt == 0) {
2926 out_blocks[0].num = 0;
2927 out_blocks[0].is_continue = 0;
2931 for (i = 0; i < request_cnt; i++) {
2934 block.
num = out_blocks[i].num;
2937 if (i + 1 < request_cnt) {
2944 if (out_blocks[i].is_continue) {
2956 goto internal_issue;
2959 if (out_blocks[i].is_continue)
2974 goto internal_issue;
2984 ((out_pdu->
max_size - token_options) /1024) * 1024;
2995 goto internal_issue;
2997 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3005 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3023 goto internal_issue;
3031 goto internal_issue;
3033 if (i + 1 < request_cnt) {
3039 goto skip_app_handler;
3040#if COAP_Q_BLOCK_SUPPORT
3050 (
const uint8_t *)error_phrase);
3071 rec_blocks->
retry = 0;
3073 for (i = 0; i < rec_blocks->
used; i++) {
3074 if (block_num >= rec_blocks->
range[i].
begin &&
3075 block_num <= rec_blocks->range[i].end)
3078 if (block_num < rec_blocks->range[i].begin) {
3079 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3086 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3087 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3093 if (block_num == rec_blocks->
range[i].
end + 1) {
3094 rec_blocks->
range[i].
end = block_num;
3095 if (i + 1 < rec_blocks->
used) {
3096 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3099 if (i+2 < rec_blocks->
used) {
3100 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3101 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3109 if (i == rec_blocks->
used) {
3123#if COAP_SERVER_SUPPORT
3143 const uint8_t *data =
NULL;
3148 uint16_t block_option = 0;
3155 const uint8_t *rtag;
3156 uint32_t max_block_szx;
3158 unsigned int saved_num;
3159 size_t saved_offset;
3162 *pfree_lg_srcv =
NULL;
3169#if COAP_Q_BLOCK_SUPPORT
3172 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3173 (
const uint8_t *)
"Block1 + Q-Block1 together");
3175 goto skip_app_handler;
3179#if COAP_Q_BLOCK_SUPPORT
3185 if (!block_option ||
3199 goto skip_app_handler;
3201 goto call_app_handler;
3227 goto skip_app_handler;
3235 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3236 max_block_szx = block.
szx;
3238 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3242 (total > max_body)) {
3247 if (max_body_size == 0 || max_body < max_body_size) {
3248 max_body_size = max_body;
3255 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3259 goto skip_app_handler;
3262 offset = block.
num << (block.
szx + 4);
3265#
if COAP_Q_BLOCK_SUPPORT
3284 if (total < (length + offset + (block.
m ? 1 : 0)))
3285 total = length + offset + (block.m ? 1 : 0);
3287 *added_block = block.
m;
3289 goto call_app_handler;
3295 LL_FOREACH(session->lg_srcv, lg_srcv) {
3296 if (rtag_opt || lg_srcv->rtag_set == 1) {
3297 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3299 if (lg_srcv->rtag_length != rtag_length ||
3300 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3303 if (resource == lg_srcv->resource) {
3306 if ((lg_srcv->resource == context->unknown_resource ||
3307 resource == context->proxy_uri_resource) &&
3314 (
const uint8_t *)
"Missing block 0");
3316 goto skip_app_handler;
3322 if (lg_srcv ==
NULL) {
3324 (
const uint8_t *)
"Memory issue");
3326 goto skip_app_handler;
3331 lg_srcv->resource = resource;
3332 if (resource == context->unknown_resource ||
3333 resource == context->proxy_uri_resource)
3335 lg_srcv->content_format = fmt;
3336 lg_srcv->total_len = total;
3338 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3339 max_block_szx < block.
szx) {
3340 lg_srcv->szx = max_block_szx;
3342 lg_srcv->szx = block.
szx;
3344 lg_srcv->block_option = block_option;
3347 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3348 lg_srcv->observe_set = 1;
3352 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3353 lg_srcv->rtag_set = 1;
3355 lg_srcv->body_data =
NULL;
3356 LL_PREPEND(session->lg_srcv, lg_srcv);
3362 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3364 (
const uint8_t *)
"Missing interim block");
3366 goto skip_app_handler;
3369 if (fmt != lg_srcv->content_format) {
3370 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3371 (
const uint8_t *)
"Content-Format mismatch");
3376#if COAP_Q_BLOCK_SUPPORT
3378 if (total != lg_srcv->total_len) {
3380 (
const uint8_t *)
"Size1 mismatch");
3390 lg_srcv->last_mid = pdu->
mid;
3391 lg_srcv->last_type = pdu->
type;
3394 saved_num = block.
num;
3395 saved_offset = offset;
3397 while (offset < saved_offset + length) {
3402 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3403 (
const uint8_t *)
"Too many missing blocks");
3410 offset = block.
num << (block.
szx + 4);
3416#if COAP_Q_BLOCK_SUPPORT
3417 lg_srcv->rec_blocks.processing_payload_set =
3420 if (lg_srcv->total_len < saved_offset + length) {
3421 lg_srcv->total_len = saved_offset + length;
3424#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3425 !resource->is_proxy_uri && \
3426 !resource->is_reverse_proxy && \
3427 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3428 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3430 if (USE_BLOCK_DATA_HANDLER) {
3435 &lg_srcv->body_data,
3436 length, data, saved_offset,
3437 lg_srcv->total_len));
3440 goto skip_app_handler;
3444 saved_offset, lg_srcv->total_len);
3445 if (!lg_srcv->body_data) {
3447 (
const uint8_t *)
"Memory issue");
3449 goto skip_app_handler;
3460#if COAP_Q_BLOCK_SUPPORT
3465 if (lg_srcv->rec_blocks.used == 1 &&
3468 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3470 block.
num = lg_srcv->rec_blocks.range[0].end;
3471 goto skip_app_handler;
3476 goto skip_app_handler;
3480 goto skip_app_handler;
3486 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3487 max_block_szx < block.
aszx) {
3488 block.
aszx = max_block_szx;
3519 LL_FOREACH(session->lg_srcv, sg) {
3520 if (lg_srcv == sg) {
3526 goto skip_app_handler;
3529 if (lg_srcv->last_token) {
3530 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3538#if COAP_Q_BLOCK_SUPPORT
3543 lg_srcv->no_more_seen = 1;
3557#if COAP_Q_BLOCK_SUPPORT
3561 goto skip_app_handler;
3571 if (lg_srcv->observe_set) {
3573 lg_srcv->observe_length, lg_srcv->observe);
3576 if (lg_srcv->body_data) {
3585 if (USE_BLOCK_DATA_HANDLER) {
3594 lg_srcv->dont_timeout = 1;
3595 *pfree_lg_srcv = lg_srcv;
3601 LL_DELETE(session->lg_srcv, lg_srcv);
3602 coap_block_delete_lg_srcv(session, lg_srcv);
3609#if COAP_CLIENT_SUPPORT
3610#if COAP_Q_BLOCK_SUPPORT
3612derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3613 uint32_t value = **bp & 0x1f;
3617 }
else if (value == 24) {
3619 return (uint32_t)-1;
3623 }
else if (value == 25) {
3625 return (uint32_t)-1;
3633 return (uint32_t)-1;
3634 value = (uint32_t)(**bp) << 24;
3636 value |= **bp << 16;
3654 if (sent || lg_xmit || lg_crcv) {
3658 const uint8_t *data;
3668 }
else if (lg_xmit) {
3671 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3672 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3675 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3676 lg_xmit->data_info->length - offset;
3679 sent = lg_crcv->sent_pdu;
3688 ++lg_crcv->retry_counter);
3702 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3703#if COAP_OSCORE_SUPPORT
3704 if (session->oscore_encryption &&
3711 lg_crcv->state_token = token;
3765 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3768 lg_crcv = coap_find_lg_crcv(session, rcvd);
3779 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3780 1 << (block.
szx + 4), block.
num);
3786 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3787 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3792 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3794 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3796 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3797 1 << (block.
szx + 4), block.
num);
3802 "next block is not aligned on requested block size boundary. "
3803 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3805 (1 << (block.
szx + 4)),
3806 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3809 track_echo(session, rcvd);
3827 lg_xmit->
offset = (block.
num + 1) * chunk;
3843 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3846 assert(len <=
sizeof(buf));
3848 memcpy(buf, new_token->
s, len);
3868 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3871 ((pdu->
max_size - token_options) /1024) * 1024;
3883#if COAP_CONSTRAINED_STACK
3885 static uint8_t l_data[1024];
3887 uint8_t l_data[1024];
3891 assert(chunk <= 1024);
3893 block.
num * chunk, l_data, &l_length,
3908#if COAP_Q_BLOCK_SUPPORT
3911 if (coap_send_q_block1(session, block, pdu,
3931 int observe_action = -1;
3934 goto lg_xmit_finished;
3944 goto lg_xmit_finished;
3952 goto lg_xmit_finished;
3954 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
3956#if COAP_Q_BLOCK_SUPPORT
3965 const uint8_t *data;
3970 uint16_t fmt = fmt_opt ?
3980 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3986 const uint8_t *bp = data;
3992 size_t ltoken_length;
3994 for (i = 0; (bp < data + length) &&
3996 if ((*bp & 0xc0) != 0x00)
3998 block.
num = derive_cbor_value(&bp, data + length - bp);
4000 if (block.
num > (1 << 20) -1)
4002 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4032 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4035 goto lg_xmit_finished;
4049 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4060 lg_crcv->sent_pdu->lg_xmit = 0;
4072 LL_DELETE(session->
lg_xmit, lg_xmit);
4086 const uint8_t *data,
size_t offset,
size_t total) {
4099 const uint8_t *data,
size_t offset,
size_t total) {
4102 if (body_data ==
NULL && total) {
4105 if (body_data ==
NULL)
4109 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4115 if (offset + length <= total && body_data->length >= total) {
4116 memcpy(&body_data->
s[offset], data, length);
4132 memcpy(&body_data->
s[offset], data, length);
4141#if COAP_CLIENT_SUPPORT
4161#if COAP_Q_BLOCK_SUPPORT
4165 uint16_t block_opt = 0;
4167 int ack_rst_sent = 0;
4171 memset(&block, 0,
sizeof(block));
4172#if COAP_Q_BLOCK_SUPPORT
4173 memset(&qblock, 0,
sizeof(qblock));
4175 lg_crcv = coap_find_lg_crcv(session, rcvd);
4182 const uint8_t *data;
4185 size_t size2 = size_opt ?
4197#if COAP_Q_BLOCK_SUPPORT
4200 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4206 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4211 track_echo(session, rcvd);
4212 if (have_block && (block.
m || length)) {
4216 uint16_t fmt = fmt_opt ?
4223 size_t saved_offset;
4236 goto expire_lg_crcv;
4239 chunk = (size_t)1 << (block.
szx + 4);
4240 offset = block.
num * chunk;
4241 if (size2 < (offset + length)) {
4243 size2 = offset + length + 1;
4245 size2 = offset + length;
4247 saved_offset = offset;
4249 if (lg_crcv->initial) {
4250#if COAP_Q_BLOCK_SUPPORT
4253 lg_crcv->initial = 0;
4254 if (lg_crcv->body_data) {
4256 lg_crcv->body_data =
NULL;
4260 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4261 lg_crcv->etag_set = 1;
4263 lg_crcv->etag_set = 0;
4265 lg_crcv->total_len = size2;
4266 lg_crcv->content_format = fmt;
4267 lg_crcv->szx = block.
szx;
4268 lg_crcv->block_option = block_opt;
4269 lg_crcv->last_type = rcvd->
type;
4270 lg_crcv->rec_blocks.used = 0;
4271 lg_crcv->rec_blocks.total_blocks = 0;
4272#if COAP_Q_BLOCK_SUPPORT
4273 lg_crcv->rec_blocks.processing_payload_set = 0;
4276 if (lg_crcv->total_len < size2)
4277 lg_crcv->total_len = size2;
4281 uint8_t max_block_szx;
4284 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4285 max_block_szx = block.
szx;
4287 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4291 (size2 > max_body)) {
4294 if (max_body_size == 0 || max_body < max_body_size) {
4295 max_body_size = max_body;
4307 lg_crcv->etag, lg_crcv->etag_length)) {
4311 ++lg_crcv->retry_counter);
4315#if COAP_Q_BLOCK_SUPPORT
4320 coap_log_warn(
"Data body updated during receipt - new request started\n");
4324 lg_crcv->initial = 1;
4326 lg_crcv->body_data =
NULL;
4339 (0 << 4) | (0 << 3) | block.
aszx),
4345 goto skip_app_handler;
4347 }
else if (lg_crcv->etag_set) {
4353 if (fmt != lg_crcv->content_format) {
4357#if COAP_Q_BLOCK_SUPPORT
4363 if (block.
num == 0) {
4369 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4370 lg_crcv->observe_set = 1;
4372 lg_crcv->observe_set = 0;
4376 while (offset < saved_offset + length) {
4378#if COAP_Q_BLOCK_SUPPORT
4382 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4383 1 << (block.
szx + 4), block.
num);
4384#if COAP_Q_BLOCK_SUPPORT
4386 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4387 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4388 coap_request_missing_q_block2(session, lg_crcv);
4390 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4400 offset = block.
num << (block.
szx + 4);
4406 if (updated_block) {
4413 if (size2 < saved_offset + length) {
4414 size2 = saved_offset + length;
4421 &lg_crcv->body_data,
4423 saved_offset, size2));
4429 saved_offset, size2);
4430 if (lg_crcv->body_data ==
NULL) {
4443#if COAP_Q_BLOCK_SUPPORT
4449 if (check_all_blocks_in_for_payload_set(session,
4450 &lg_crcv->rec_blocks)) {
4451 block.
num = lg_crcv->rec_blocks.range[0].end;
4453 lg_crcv->rec_blocks.processing_payload_set =
4455 if (check_any_blocks_next_payload_set(session,
4456 &lg_crcv->rec_blocks)) {
4458 coap_request_missing_q_block2(session, lg_crcv);
4459 goto skip_app_handler;
4464 goto skip_app_handler;
4468 goto skip_app_handler;
4492 ((block.
num + 1) << 4) |
4493 (block.
m << 3) | block.
aszx),
4498 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4503 goto skip_app_handler;
4506 goto skip_app_handler;
4511#if COAP_Q_BLOCK_SUPPORT
4513 lg_crcv->total_len : size2;
4522 if (lg_crcv->app_token)
4524 lg_crcv->app_token->s);
4527 goto call_app_handler;
4529#if COAP_Q_BLOCK_SUPPORT
4535 if (lg_crcv->observe_set) {
4537 lg_crcv->observe_length, lg_crcv->observe);
4539 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4540#if COAP_Q_BLOCK_SUPPORT
4549 lg_crcv->total_len : saved_offset + length;
4557#if COAP_Q_BLOCK_SUPPORT
4559 lg_crcv->total_len : size2;
4572 if (lg_crcv->app_token)
4574 lg_crcv->app_token->s);
4577 body_free = lg_crcv->body_data;
4578 lg_crcv->body_data =
NULL;
4582 if (lg_crcv->observe_set == 0) {
4584 LL_DELETE(session->lg_crcv, lg_crcv);
4585 coap_block_delete_lg_crcv(session, lg_crcv);
4586 goto skip_app_handler;
4589 lg_crcv->initial = 1;
4592 goto skip_app_handler;
4607 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4608 lg_crcv->observe_set = 1;
4610 lg_crcv->observe_set = 0;
4619 goto expire_lg_crcv;
4624#if COAP_OSCORE_SUPPORT
4625 if (check_freshness(session, rcvd,
4626 (session->oscore_encryption == 0) ? sent :
NULL,
4629 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4631 goto skip_app_handler;
4632 goto expire_lg_crcv;
4641 const uint8_t *data;
4648 ++lg_crcv->retry_counter);
4656#if COAP_PROXY_SUPPORT
4657 coap_proxy_req_t *proxy_req = session->
context->proxy_list_count ?
4658 coap_proxy_map_outgoing_request(session, rcvd,
NULL) :
NULL;
4664 proxy_req->token_used =
new;
4665 coap_proxy_log_entry(proxy_req->incoming, proxy_req->pdu, proxy_req->token_used,
"upd");
4669 coap_log_debug(
"* Retransmitting PDU with Uri-Path-Abbrev replaced (1)\n");
4673 goto skip_app_handler;
4676 goto expire_lg_crcv;
4679 goto expire_lg_crcv;
4681 if (!block.
m && !lg_crcv->observe_set) {
4700#
if COAP_Q_BLOCK_SUPPORT
4707 goto skip_app_handler;
4710 const uint8_t *data;
4714#if COAP_Q_BLOCK_SUPPORT
4715 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4721 if (block.
num != 0) {
4723 size_t chunk = (size_t)1 << (block.
szx + 4);
4728 goto call_app_handler;
4731#if COAP_Q_BLOCK_SUPPORT
4735 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4741 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4744 LL_PREPEND(session->lg_crcv, lg_crcv);
4745 return coap_handle_response_get_block(context, session, sent, rcvd,
4758 track_echo(session, rcvd);
4760 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4763 LL_PREPEND(session->lg_crcv, lg_crcv);
4764 return coap_handle_response_get_block(context, session, sent, rcvd,
4776 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4779 LL_PREPEND(session->lg_crcv, lg_crcv);
4780 return coap_handle_response_get_block(context, session, sent, rcvd,
4798 if (lg_crcv->app_token)
4800 lg_crcv->app_token->s);
4804 LL_DELETE(session->lg_crcv, lg_crcv);
4805 coap_block_delete_lg_crcv(session, lg_crcv);
4817#if COAP_SERVER_SUPPORT
4826 if (response->
code == 0)
4828 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4836#if COAP_CLIENT_SUPPORT
4839 uint64_t token_match =
4845 if (session->lg_crcv) {
4846 LL_FOREACH(session->lg_crcv, lg_crcv) {
4851 lg_crcv->app_token->s);
4859 LL_FOREACH(session->
lg_xmit, lg_xmit) {
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void coap_block_release_lg_xmit_data(coap_session_t *session, coap_lg_xmit_data_t *data_info)
#define COAP_ETAG_MAX_BYTES
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
static int coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
static int setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, unsigned int num, unsigned int blk_size, size_t total)
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
int coap_flsll(long long j)
int coap_fls(unsigned int i)
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
@ COAP_NACK_TOO_MANY_RETRIES
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_unique_id[8]
uint16_t coap_option_num_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
void coap_call_response_handler(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, void *body_free)
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
#define coap_check_update_token(a, b)
int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
void coap_context_set_block_mode_lkd(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
#define COAP_BLOCK_MAX_SIZE_SET(a)
void coap_check_code_lg_xmit(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response_l...
COAP_STATIC_INLINE void coap_lg_xmit_reference_lkd(coap_lg_xmit_t *lg_xmit)
Increment reference counter on a lg_xmit.
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Remove a lg_xmit.
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
coap_binary_t * coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_SET_MASK
COAP_STATIC_INLINE void coap_lg_xmit_release_lkd(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Decrement reference counter on a lg_xmit.
coap_lg_xmit_t * coap_find_lg_xmit(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_xmit for the session that matches the pdu.
#define COAP_BLOCK_MAX_SIZE_GET(a)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
COAP_API int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
#define COAP_BLOCK_USE_M_Q_BLOCK
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
#define COAP_BLOCK_STLESS_BLOCK2
COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
#define COAP_BLOCK_TRY_Q_BLOCK
#define COAP_BLOCK_STLESS_FETCH
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, coap_block_b_t *block)
Adds the appropriate payload data of the body to the pdu.
#define COAP_BLOCK_SINGLE_BODY
int coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
int coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, unsigned int block_num, unsigned char block_szx)
Adds the block_num block of size 1 << (block_szx + 4) from source data to pdu.
COAP_API coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
void coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, uint16_t media_type, int maxage, size_t length, const uint8_t *data)
Adds the appropriate part of data to the response pdu.
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
#define COAP_BLOCK_USE_LIBCOAP
time_t coap_time_t
CoAP time in seconds since epoch.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
#define COAP_MAX_DELAY_TICKS
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
void coap_register_block_data_handler(coap_context_t *context, coap_block_data_handler_t block_data_handler)
Sets up a handler that is called for each received block during a block-wise transfer when COAP_BLOCK...
coap_response_t(* coap_block_data_handler_t)(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, coap_binary_t **body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Definition of the block data handler function.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
@ COAP_EVENT_BLOCK_ISSUE
Triggered when a block transfer could not be handled.
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
#define COAP_PDU_IS_RESPONSE(pdu)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options, coap_bool_t expand_opt_abb)
Duplicate an existing PDU.
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
#define COAP_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
#define COAP_PDU_IS_REQUEST(pdu)
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_BLOCK2
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_Q_BLOCK1
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_OPTION_URI_PATH
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_OPTION_SIZE1
coap_pdu_type_t
CoAP PDU message type definitions.
#define COAP_MEDIATYPE_TEXT_PLAIN
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_CONTENT_TYPE
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_Q_BLOCK2
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
#define COAP_OPTION_URI_PATH_ABB
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_OBSERVE
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
@ COAP_REQUEST_CODE_FETCH
#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
@ COAP_SESSION_TYPE_CLIENT
client-side
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
#define COAP_STATIC_INLINE
coap_address_t remote
remote address and port
coap_address_t local
local address and port
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options with BERT support.
unsigned int num
block number
uint32_t chunk_size
1024 if BERT
unsigned int bert
Operating as BERT.
unsigned int aszx
block size (0-7 including BERT
unsigned int defined
Set if block found.
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size (0-6)
Structure of Block options.
unsigned int num
block number
unsigned int szx
block size
unsigned int m
1 if more blocks follow, 0 otherwise
The CoAP stack's global state is stored in a coap_context_t object.
coap_block_data_handler_t block_data_cb
Called with each block data during block transfers.
uint32_t max_body_size
Max supported body size or 0 is unlimited.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint64_t state_token
state token
size_t bert_size
size of last BERT block
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
applicaton provided ptr for de-alloc function
uint32_t ref
Reference count.
const uint8_t * data
large data ptr
size_t length
large data length
coap_release_large_data_t release_func
large data de-alloc function
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
union coap_lg_xmit_t::@1 b
coap_lg_xmit_data_t * data_info
Pointer to large data information.
int last_block
last acknowledged block number Block1 last transmitted Q-Block2
coap_tick_t last_payload
Last time MAX_PAYLOAD was sent or 0.
size_t offset
large data next offset to transmit
coap_pdu_t * sent_pdu
The sent pdu with all the data.
uint32_t ref
Reference count.
uint16_t option
large block transmisson CoAP option
struct coap_lg_xmit_t * next
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint8_t no_path_abbrev
Set is remote does not support Uri-Path-Abbrev.
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
uint32_t tx_rtag
Next Request-Tag number to use.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
size_t length
length of string