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] :
598 lg_crcv->observe_set = 0;
608 if (lg_crcv->o_block_option) {
611 lg_crcv->o_blk_size),
614 if (lg_crcv->obs_data) {
615 coap_add_data_large_request_lkd(session, pdu,
616 lg_crcv->obs_data->length,
617 lg_crcv->obs_data->data,
NULL,
NULL);
619 coap_add_data_large_request_lkd(session, pdu, size, data,
NULL,
NULL);
630#if COAP_Q_BLOCK_SUPPORT
632 if (using_q_block1) {
633 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
652 uint64_t token_match =
656 LL_FOREACH(session->lg_crcv, lg_crcv) {
674 lg_crcv = coap_block_new_lg_crcv(session, m_lg_crcv->sent_pdu,
NULL);
676 if (m_lg_crcv->obs_data) {
677 m_lg_crcv->obs_data->ref++;
678 lg_crcv->obs_data = m_lg_crcv->obs_data;
680 LL_PREPEND(session->lg_crcv, lg_crcv);
686#if COAP_OSCORE_SUPPORT
700 lg_crcv = coap_find_lg_crcv(session, pdu);
704 ++lg_crcv->retry_counter);
732#if COAP_SERVER_SUPPORT
749 LL_FOREACH(session->
lg_xmit, lg_xmit) {
756 lg_xmit->b.b2.query ?
757 lg_xmit->b.b2.query : &empty)) {
766 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
792#if COAP_Q_BLOCK_SUPPORT
798 int have_block_defined = 0;
800 uint8_t max_blk_size;
802 size_t token_options;
805#if COAP_Q_BLOCK_SUPPORT
809#if !COAP_SERVER_SUPPORT
815 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
823 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
837#define MAX_BLK_LEN ((1UL << 20) * (1 << (6 + 4)))
838#if UINT_MAX < MAX_BLK_LEN
840#define MAX_BLK_LEN UINT_MAX
848#if COAP_SERVER_SUPPORT
860 coap_digest_t digest;
861 coap_digest_ctx_t *dctx = coap_digest_setup();
867 if (coap_digest_update(dctx, data, length)) {
868 if (coap_digest_final(dctx, &digest)) {
869 memcpy(&etag, digest.key,
sizeof(etag));
870#if COAP_ETAG_MAX_BYTES != 8
876 coap_digest_free(dctx);
896 if (etag == etag_r) {
911#if COAP_Q_BLOCK_SUPPORT
927 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
931#if COAP_CLIENT_SUPPORT
934 LL_DELETE(session->
lg_xmit, lg_xmit);
945#if COAP_Q_BLOCK_SUPPORT
946 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
959#if COAP_SERVER_SUPPORT
964 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
967 LL_DELETE(session->
lg_xmit, lg_xmit);
974#if COAP_OSCORE_SUPPORT
975 if (session->oscore_encryption) {
983 avail = pdu->
max_size - token_options;
986#if COAP_OSCORE_SUPPORT
995 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1001 if (max_blk_size && blk_size > max_blk_size)
1002 blk_size = max_blk_size;
1006 if (block.
szx < blk_size)
1007 blk_size = block.
szx;
1008 have_block_defined = 1;
1010#if COAP_Q_BLOCK_SUPPORT
1013 if (have_block_defined) {
1015 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1019 if (block.
szx < blk_size)
1020 blk_size = block.
szx;
1021 have_block_defined = 1;
1022 option = alt_option;
1027 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1029 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1033 chunk = (size_t)1 << (blk_size + 4);
1034 if ((have_block_defined && block.
num != 0) || single_request ||
1039 if (length >= block.
num * chunk) {
1040#if COAP_SERVER_SUPPORT
1046 (
unsigned int)length),
1060 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1065 if (chunk > length - block.
num * chunk)
1066 rem = length - block.
num * chunk;
1073 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1098 lg_xmit->blk_size = blk_size;
1099 lg_xmit->option = option;
1101 if (!lg_xmit->data_info)
1103 lg_xmit->data_info->ref = 0;
1104 lg_xmit->data_info->data = data;
1105 lg_xmit->data_info->length = length;
1106#if COAP_Q_BLOCK_SUPPORT
1107 lg_xmit->non_timeout_random_ticks =
1110 lg_xmit->data_info->get_func = get_func;
1111 lg_xmit->data_info->release_func = release_func;
1112 lg_xmit->data_info->app_ptr = app_ptr;
1119 if (!lg_xmit->b.b1.app_token)
1127 lg_xmit->b.b1.count = 1;
1129 lg_xmit->b.b1.count);
1138 (
unsigned int)length),
1151 lg_xmit->b.b2.resource = resource;
1154 if (lg_xmit->b.b2.query) {
1155 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1158 lg_xmit->b.b2.query =
NULL;
1163 sizeof(lg_xmit->b.b2.rtag));
1165 lg_xmit->b.b2.rtag_set = 1;
1167 lg_xmit->b.b2.rtag_set = 0;
1169 lg_xmit->b.b2.etag = etag;
1170 lg_xmit->b.b2.request_method = request_method;
1177 lg_xmit->b.b2.maxage_expire = 0;
1182 (
unsigned int)length),
1187 blk_size, lg_xmit->data_info->length))
1194 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1202 avail = pdu->
max_size - token_options;
1207#if COAP_OSCORE_SUPPORT
1210 if (avail < (ssize_t)chunk) {
1213 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1216 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1217 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1218 lg_xmit->blk_size = blk_size;
1219 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1225 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1230 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1231 rem = lg_xmit->data_info->length - block.
num * chunk;
1233#if COAP_CONSTRAINED_STACK
1235 static uint8_t l_data[1024];
1237 uint8_t l_data[1024];
1241 assert(rem <= 1024);
1242 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1253 lg_xmit->b.b1.bert_size = rem;
1255 lg_xmit->last_block = -1;
1258 LL_PREPEND(session->
lg_xmit,lg_xmit);
1261 if (have_block_defined) {
1265 (0 << 4) | (0 << 3) | blk_size), buf);
1272 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1293 }
else if (release_func) {
1299#if COAP_CLIENT_SUPPORT
1304 const uint8_t *data,
1311 ret = coap_add_data_large_request_lkd(session, pdu, length, data,
1312 release_func, app_ptr);
1321 const uint8_t *data,
1335 length, data, release_func,
NULL, app_ptr, 0, 0);
1348 ret = coap_add_data_large_request_app_lkd(session, pdu, length,
1349 release_func, get_func, app_ptr);
1369 length,
NULL, release_func, get_func,
1374#if COAP_SERVER_SUPPORT
1381 uint16_t media_type,
1385 const uint8_t *data,
1391 ret = coap_add_data_large_response_lkd(resource, session, request,
1392 response, query, media_type, maxage, etag,
1393 length, data, release_func, app_ptr);
1404 uint16_t media_type,
1408 const uint8_t *data,
1412 unsigned char buf[4];
1414 int block_requested = 0;
1415 int single_request = 0;
1416#if COAP_Q_BLOCK_SUPPORT
1417 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1423 memset(&block, 0,
sizeof(block));
1430 block_requested = 1;
1431 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1434 length >> (block.
szx + 4));
1439#if COAP_Q_BLOCK_SUPPORT
1441 block_requested = 1;
1442 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1445 length >> (block.
szx + 4));
1449 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1470 if (block_requested) {
1494 query, maxage, etag, length, data,
1495 release_func,
NULL, app_ptr, single_request,
1498 goto error_released;
1508#if COAP_ERROR_PHRASE_LENGTH > 0
1526#if COAP_Q_BLOCK_SUPPORT
1536 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1540 LL_DELETE(session->
lg_xmit, lg_xmit);
1544 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1550 if (lg_xmit->
last_sent + partial_timeout <= now) {
1553#if COAP_CLIENT_SUPPORT
1557 LL_DELETE(session->
lg_xmit, lg_xmit);
1564 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1565 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1574#if COAP_CLIENT_SUPPORT
1575#if COAP_Q_BLOCK_SUPPORT
1580 uint64_t token =
STATE_TOKEN_FULL(lg_crcv->state_token, ++lg_crcv->retry_counter);
1592 pdu->
type = lg_crcv->last_type;
1604 int block_payload_set = -1;
1615 if (lg_crcv->rec_blocks.used &&
1616 (lg_crcv->rec_blocks.used < 2 ||
1619 block = lg_crcv->rec_blocks.range[0].end + 1;
1620 block_size = (size_t)1 << (lg_crcv->szx + 4);
1621 sofar = block * block_size;
1622 if (sofar < lg_crcv->total_len) {
1625 pdu = coap_build_missing_pdu(session, lg_crcv);
1631 (block << 4) | (1 << 3) | lg_crcv->szx),
1639 for (i = 0; i < lg_crcv->rec_blocks.used; i++) {
1640 if (block < (
int)lg_crcv->rec_blocks.range[i].begin &&
1641 lg_crcv->rec_blocks.range[i].begin != 0) {
1644 pdu = coap_build_missing_pdu(session, lg_crcv);
1649 if (block_payload_set == -1)
1651 for (; block < (int)lg_crcv->rec_blocks.range[i].begin &&
1655 (block << 4) | (0 << 3) | lg_crcv->szx),
1659 if (block < (
int)lg_crcv->rec_blocks.range[i].end) {
1660 block = lg_crcv->rec_blocks.range[i].end;
1663 block_size = (size_t)1 << (lg_crcv->szx + 4);
1664 sofar = (block + 1) * block_size;
1665 if (sofar < lg_crcv->total_len) {
1668 pdu = coap_build_missing_pdu(session, lg_crcv);
1672 sofar = (lg_crcv->total_len + block_size - 1)/block_size;
1674 if (block_payload_set == -1)
1676 for (; block < (ssize_t)sofar &&
1680 (block << 4) | (0 << 3) | lg_crcv->szx),
1687 lg_crcv->rec_blocks.retry++;
1688 if (block_payload_set != -1)
1689 lg_crcv->rec_blocks.processing_payload_set = block_payload_set;
1704#if COAP_Q_BLOCK_SUPPORT
1710#if COAP_Q_BLOCK_SUPPORT
1712 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1718 LL_FOREACH_SAFE(session->lg_crcv, lg_crcv, q) {
1722#if COAP_Q_BLOCK_SUPPORT
1724 size_t scaled_timeout = receive_timeout *
1725 ((size_t)1 << lg_crcv->rec_blocks.retry);
1733 if (lg_crcv->rec_blocks.last_seen + scaled_timeout <= now) {
1734 coap_request_missing_q_block2(session, lg_crcv);
1736 if (*tim_rem > lg_crcv->rec_blocks.last_seen + scaled_timeout - now) {
1737 *tim_rem = lg_crcv->rec_blocks.last_seen + scaled_timeout - now;
1745 if (!lg_crcv->observe_set && lg_crcv->last_used &&
1746 lg_crcv->last_used + partial_timeout <= now) {
1747#if COAP_Q_BLOCK_SUPPORT
1751 LL_DELETE(session->lg_crcv, lg_crcv);
1752 coap_block_delete_lg_crcv(session, lg_crcv);
1753 }
else if (!lg_crcv->observe_set && lg_crcv->last_used) {
1755 if (*tim_rem > lg_crcv->last_used + partial_timeout - now) {
1756 *tim_rem = lg_crcv->last_used + partial_timeout - now;
1765#if COAP_SERVER_SUPPORT
1766#if COAP_Q_BLOCK_SUPPORT
1778 if (lg_srcv->last_token)
1779 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
1794 assert(block >= 0 && block < (1 << 20));
1796 if (block < 0 || block >= (1 << 20)) {
1798 }
else if (block < 24) {
1801 }
else if (block < 0x100) {
1805 }
else if (block < 0x10000) {
1808 val[1] = block >> 8;
1809 val[2] = block & 0xff;
1813 val[1] = block >> 16;
1814 val[2] = (block >> 8) & 0xff;
1815 val[3] = block & 0xff;
1831 for (i = 0; i < rec_blocks->
used; i++) {
1832 if (block_num < rec_blocks->range[i].begin)
1834 if (block_num <= rec_blocks->range[i].end)
1840#if COAP_SERVER_SUPPORT
1842check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1843 if (rec_blocks->
used == 0) {
1844 return block_num == 0 ? 1 : 0;
1846 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1863 for (i = 0; i < rec_blocks->
used; i++) {
1864 if (block < rec_blocks->range[i].begin)
1866 if (block < rec_blocks->range[i].end)
1872#if COAP_CLIENT_SUPPORT
1873#if COAP_Q_BLOCK_SUPPORT
1877 if (rec_blocks->
used &&
1879 rec_blocks->processing_payload_set)
1887 if (rec_blocks->
used > 1 &&
1889 rec_blocks->processing_payload_set)
1896#if COAP_SERVER_SUPPORT
1907#if COAP_Q_BLOCK_SUPPORT
1913#if COAP_Q_BLOCK_SUPPORT
1915 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1921 LL_FOREACH_SAFE(session->lg_srcv, lg_srcv, q) {
1922 if (lg_srcv->dont_timeout) {
1929#if COAP_Q_BLOCK_SUPPORT
1931 size_t scaled_timeout = receive_timeout *
1932 ((size_t)1 << lg_srcv->rec_blocks.retry);
1938 if (lg_srcv->rec_blocks.last_seen + scaled_timeout <= now) {
1941 size_t block_size = (size_t)1 << (lg_srcv->szx + 4);
1942 size_t final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1944 size_t last_payload_block;
1946 size_t no_blocks = 0;
1949 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1950 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1951 lg_srcv->rec_blocks.range[i].begin != 0) {
1953 no_blocks += lg_srcv->rec_blocks.range[i].begin - block;
1955 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1956 block = lg_srcv->rec_blocks.range[i].end;
1959 if (no_blocks == 0 && block == (
int)final_block)
1965 if (final_block > last_payload_block) {
1966 final_block = last_payload_block;
1968 no_blocks += final_block - block;
1969 if (no_blocks == 0) {
1971 final_block = (lg_srcv->total_len + block_size - 1)/block_size - 1;
1973 if (final_block > last_payload_block) {
1974 final_block = last_payload_block;
1979 for (i = 0; i < lg_srcv->rec_blocks.used; i++) {
1980 if (block < (
int)lg_srcv->rec_blocks.range[i].begin &&
1981 lg_srcv->rec_blocks.range[i].begin != 0) {
1984 pdu = pdu_408_build(session, lg_srcv);
1989 for (; block < (int)lg_srcv->rec_blocks.range[i].begin; block++) {
1990 if (!add_408_block(pdu, block)) {
1995 if (block < (
int)lg_srcv->rec_blocks.range[i].end) {
1996 block = lg_srcv->rec_blocks.range[i].end;
2000 for (; block <= (int)final_block; block++) {
2002 pdu = pdu_408_build(session, lg_srcv);
2006 if (!add_408_block(pdu, block)) {
2012 lg_srcv->rec_blocks.retry++;
2015 if (*tim_rem > lg_srcv->rec_blocks.last_seen + scaled_timeout - now) {
2016 *tim_rem = lg_srcv->rec_blocks.last_seen + scaled_timeout - now;
2023 if (lg_srcv->no_more_seen)
2025 if (lg_srcv->last_used && lg_srcv->last_used + partial_timeout <= now) {
2026#if COAP_Q_BLOCK_SUPPORT
2043 if (lg_srcv->last_token)
2044 coap_add_token(pdu, lg_srcv->last_token->length, lg_srcv->last_token->s);
2046 (
const uint8_t *)
"Missing interim block");
2050 LL_DELETE(session->lg_srcv, lg_srcv);
2051 coap_block_delete_lg_srcv(session, lg_srcv);
2052 }
else if (lg_srcv->last_used) {
2054 if (*tim_rem > lg_srcv->last_used + partial_timeout - now) {
2055 *tim_rem = lg_srcv->last_used + partial_timeout - now;
2064#if COAP_Q_BLOCK_SUPPORT
2073 coap_send_pdu_t send_pdu) {
2078 const uint8_t *ptoken;
2080 size_t ltoken_length;
2081 uint32_t delayqueue_cnt = 0;
2084 if (send_pdu == COAP_SEND_INC_PDU)
2093 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2094 LL_FOREACH(session->
delayqueue, delayqueue) {
2122 ptoken, &drop_options);
2128 if (send_pdu == COAP_SEND_INC_PDU &&
2138 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2164 ltoken_length, ptoken, &drop_options);
2169 ((block.
num) << 4) |
2180#if COAP_CONSTRAINED_STACK
2182 static uint8_t l_data[1024];
2184 uint8_t l_data[1024];
2188 assert(chunk <= 1024);
2190 block.
num * chunk, l_data, &l_length,
2230#if COAP_CLIENT_SUPPORT
2243 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2244 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2246 if (now <= non_timeout) {
2248 *tim_rem = non_timeout - now;
2251 timed_out = now - non_timeout;
2257 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2259 memset(&block, 0,
sizeof(block));
2260 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2263 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2264 if (*tim_rem > non_timeout) {
2265 *tim_rem = non_timeout;
2279 LL_DELETE(session->
lg_xmit, lg_xmit);
2283 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2294#if COAP_SERVER_SUPPORT
2307 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2308 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2310 if (now <= non_timeout) {
2312 *tim_rem = non_timeout - now;
2315 timed_out = now - non_timeout;
2321 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2323 memset(&block, 0,
sizeof(block));
2324 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2328 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2329 if (*tim_rem > non_timeout) {
2330 *tim_rem = non_timeout;
2344 LL_DELETE(session->
lg_xmit, lg_xmit);
2348 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2360#if COAP_CLIENT_SUPPORT
2374 if (opt && lg_crcv) {
2375 int observe_action = -1;
2382 if (lg_crcv->obs_token_cnt <= block_num) {
2386 (block_num + 1) *
sizeof(lg_crcv->obs_token[0]));
2389 lg_crcv->obs_token = tmp;
2390 for (i = lg_crcv->obs_token_cnt; i < block_num + 1; i++) {
2391 lg_crcv->obs_token[i] =
NULL;
2396 lg_crcv->obs_token_cnt = block_num + 1;
2399 if (lg_crcv->obs_token[block_num] ==
NULL)
2403 if (block_num < lg_crcv->obs_token_cnt) {
2404 return lg_crcv->obs_token[block_num];
2411#if COAP_Q_BLOCK_SUPPORT
2416 coap_send_pdu_t send_request) {
2419 uint64_t token_match =
2423 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2432 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2437#if COAP_SERVER_SUPPORT
2438#if COAP_Q_BLOCK_SUPPORT
2449 coap_send_pdu_t send_response) {
2454 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2459 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2462 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2472 if (data_info->
ref > 0) {
2484#if COAP_CLIENT_SUPPORT
2485#if COAP_Q_BLOCK_SUPPORT
2517 11, (
const uint8_t *)
".well-known");
2519 4, (
const uint8_t *)
"core");
2532 (0 << 4) | (0 << 3) | 0),
2552 if (lg_crcv ==
NULL)
2555 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2559 lg_crcv->initial = 1;
2577 const uint8_t *data;
2580 if (data_len < lg_xmit->data_info->length) {
2591 if (!lg_crcv->app_token) {
2592 coap_block_delete_lg_crcv(session, lg_crcv);
2598 lg_crcv->retry_counter = 1;
2599 lg_crcv->state_token = state_token;
2606 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2614 lg_crcv->o_blk_size = block.
aszx;
2625#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2628 if (lg_crcv ==
NULL)
2632 if (lg_crcv->obs_data) {
2634 lg_crcv->obs_data =
NULL;
2640 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2649#if COAP_SERVER_SUPPORT
2653#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2656 if (lg_srcv ==
NULL)
2671 if (lg_xmit ==
NULL)
2686#if COAP_SERVER_SUPPORT
2693add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2694 uint32_t *count, uint32_t max_count) {
2697 for (i = 0; i < *count && *count < max_count; i++) {
2698 if (num == out_blocks[i].num)
2700 else if (num < out_blocks[i].num) {
2702 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2703 out_blocks[i].num = num;
2704 out_blocks[i].is_continue = is_continue;
2709 if (*count < max_count) {
2710 out_blocks[i].num = num;
2711 out_blocks[i].is_continue = is_continue;
2744 uint16_t block_opt = 0;
2745 send_track *out_blocks =
NULL;
2746 const char *error_phrase;
2751 uint32_t request_cnt, i;
2754#if COAP_Q_BLOCK_SUPPORT
2759 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2766 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2767 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2768 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2770 goto skip_app_handler;
2777 if (block.
num == 0) {
2781 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2782 if (lg_xmit ==
NULL)
2785#if COAP_Q_BLOCK_SUPPORT
2791 goto internal_issue;
2804 if (etag == lg_xmit->
b.
b2.
etag) {
2818 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2821 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2822 block.
num, block.
m);
2824 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2825 1 << (block.
szx + 4), block.
num, block.
m);
2828 if (block.
num == 0) {
2829 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2834 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2836 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2838 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2839 1 << (block.
szx + 4), block.
num);
2842 "next block is not aligned on requested block size "
2843 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2845 (1 << (block.
szx + 4)),
2846 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2849 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2850 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2862#if COAP_Q_BLOCK_SUPPORT
2875 sizeof(
"Changing blocksize during request invalid")-1,
2876 (
const uint8_t *)
"Changing blocksize during request invalid");
2878 goto skip_app_handler;
2880#if COAP_Q_BLOCK_SUPPORT
2885 goto call_app_handler;
2889 num + i < max_block; i++) {
2890 add_block_send(num + i, 1, out_blocks, &request_cnt,
2898 num + i < max_block; i++) {
2899 add_block_send(num + i, 0, out_blocks, &request_cnt,
2904 add_block_send(num, 0, out_blocks, &request_cnt,
2907 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2911 if (request_cnt == 0) {
2915 out_blocks[0].num = 0;
2916 out_blocks[0].is_continue = 0;
2920 for (i = 0; i < request_cnt; i++) {
2923 block.
num = out_blocks[i].num;
2926 if (i + 1 < request_cnt) {
2933 if (out_blocks[i].is_continue) {
2942 goto internal_issue;
2945 if (out_blocks[i].is_continue)
2960 goto internal_issue;
2970 ((out_pdu->
max_size - token_options) /1024) * 1024;
2981 goto internal_issue;
2983 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
2991 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3009 goto internal_issue;
3017 goto internal_issue;
3019 if (i + 1 < request_cnt) {
3025 goto skip_app_handler;
3026#if COAP_Q_BLOCK_SUPPORT
3036 (
const uint8_t *)error_phrase);
3057 rec_blocks->
retry = 0;
3059 for (i = 0; i < rec_blocks->
used; i++) {
3060 if (block_num >= rec_blocks->
range[i].
begin &&
3061 block_num <= rec_blocks->range[i].end)
3064 if (block_num < rec_blocks->range[i].begin) {
3065 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3072 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3073 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3079 if (block_num == rec_blocks->
range[i].
end + 1) {
3080 rec_blocks->
range[i].
end = block_num;
3081 if (i + 1 < rec_blocks->
used) {
3082 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3085 if (i+2 < rec_blocks->
used) {
3086 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3087 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3095 if (i == rec_blocks->
used) {
3109#if COAP_SERVER_SUPPORT
3129 const uint8_t *data =
NULL;
3134 uint16_t block_option = 0;
3141 const uint8_t *rtag;
3142 uint32_t max_block_szx;
3144 unsigned int saved_num;
3145 size_t saved_offset;
3148 *pfree_lg_srcv =
NULL;
3155#if COAP_Q_BLOCK_SUPPORT
3158 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3159 (
const uint8_t *)
"Block1 + Q-Block1 together");
3161 goto skip_app_handler;
3165#if COAP_Q_BLOCK_SUPPORT
3171 if (!block_option ||
3185 goto skip_app_handler;
3187 goto call_app_handler;
3213 goto skip_app_handler;
3221 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3222 max_block_szx = block.
szx;
3224 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3228 (total > max_body)) {
3233 if (max_body_size == 0 || max_body < max_body_size) {
3234 max_body_size = max_body;
3241 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3245 goto skip_app_handler;
3248 offset = block.
num << (block.
szx + 4);
3251#
if COAP_Q_BLOCK_SUPPORT
3270 if (total < (length + offset + (block.
m ? 1 : 0)))
3271 total = length + offset + (block.m ? 1 : 0);
3273 *added_block = block.
m;
3275 goto call_app_handler;
3281 LL_FOREACH(session->lg_srcv, lg_srcv) {
3282 if (rtag_opt || lg_srcv->rtag_set == 1) {
3283 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3285 if (lg_srcv->rtag_length != rtag_length ||
3286 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3289 if (resource == lg_srcv->resource) {
3292 if ((lg_srcv->resource == context->unknown_resource ||
3293 resource == context->proxy_uri_resource) &&
3300 (
const uint8_t *)
"Missing block 0");
3302 goto skip_app_handler;
3308 if (lg_srcv ==
NULL) {
3310 (
const uint8_t *)
"Memory issue");
3312 goto skip_app_handler;
3317 lg_srcv->resource = resource;
3318 if (resource == context->unknown_resource ||
3319 resource == context->proxy_uri_resource)
3321 lg_srcv->content_format = fmt;
3322 lg_srcv->total_len = total;
3324 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3325 max_block_szx < block.
szx) {
3326 lg_srcv->szx = max_block_szx;
3328 lg_srcv->szx = block.
szx;
3330 lg_srcv->block_option = block_option;
3333 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3334 lg_srcv->observe_set = 1;
3338 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3339 lg_srcv->rtag_set = 1;
3341 lg_srcv->body_data =
NULL;
3342 LL_PREPEND(session->lg_srcv, lg_srcv);
3348 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3350 (
const uint8_t *)
"Missing interim block");
3352 goto skip_app_handler;
3355 if (fmt != lg_srcv->content_format) {
3356 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3357 (
const uint8_t *)
"Content-Format mismatch");
3362#if COAP_Q_BLOCK_SUPPORT
3364 if (total != lg_srcv->total_len) {
3366 (
const uint8_t *)
"Size1 mismatch");
3376 lg_srcv->last_mid = pdu->
mid;
3377 lg_srcv->last_type = pdu->
type;
3380 saved_num = block.
num;
3381 saved_offset = offset;
3383 while (offset < saved_offset + length) {
3388 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3389 (
const uint8_t *)
"Too many missing blocks");
3396 offset = block.
num << (block.
szx + 4);
3402#if COAP_Q_BLOCK_SUPPORT
3403 lg_srcv->rec_blocks.processing_payload_set =
3406 if (lg_srcv->total_len < saved_offset + length) {
3407 lg_srcv->total_len = saved_offset + length;
3410#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3411 !resource->is_proxy_uri && \
3412 !resource->is_reverse_proxy && \
3413 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3414 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3416 if (USE_BLOCK_DATA_HANDLER) {
3421 &lg_srcv->body_data,
3422 length, data, saved_offset,
3423 lg_srcv->total_len));
3426 goto skip_app_handler;
3430 saved_offset, lg_srcv->total_len);
3431 if (!lg_srcv->body_data) {
3433 (
const uint8_t *)
"Memory issue");
3435 goto skip_app_handler;
3446#if COAP_Q_BLOCK_SUPPORT
3451 if (lg_srcv->rec_blocks.used == 1 &&
3455 block.
num = lg_srcv->rec_blocks.range[0].end;
3459 goto skip_app_handler;
3463 goto skip_app_handler;
3469 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3470 max_block_szx < block.
aszx) {
3471 block.
aszx = max_block_szx;
3502 LL_FOREACH(session->lg_srcv, sg) {
3503 if (lg_srcv == sg) {
3509 goto skip_app_handler;
3512 if (lg_srcv->last_token) {
3513 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3521#if COAP_Q_BLOCK_SUPPORT
3526 lg_srcv->no_more_seen = 1;
3540#if COAP_Q_BLOCK_SUPPORT
3544 goto skip_app_handler;
3554 if (lg_srcv->observe_set) {
3556 lg_srcv->observe_length, lg_srcv->observe);
3559 if (lg_srcv->body_data) {
3568 if (USE_BLOCK_DATA_HANDLER) {
3577 lg_srcv->dont_timeout = 1;
3578 *pfree_lg_srcv = lg_srcv;
3584 LL_DELETE(session->lg_srcv, lg_srcv);
3585 coap_block_delete_lg_srcv(session, lg_srcv);
3592#if COAP_CLIENT_SUPPORT
3593#if COAP_Q_BLOCK_SUPPORT
3595derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3596 uint32_t value = **bp & 0x1f;
3600 }
else if (value == 24) {
3602 return (uint32_t)-1;
3606 }
else if (value == 25) {
3608 return (uint32_t)-1;
3616 return (uint32_t)-1;
3617 value = (uint32_t)(**bp) << 24;
3619 value |= **bp << 16;
3637 if (sent || lg_xmit || lg_crcv) {
3641 const uint8_t *data;
3651 }
else if (lg_xmit) {
3654 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3655 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3658 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3659 lg_xmit->data_info->length - offset;
3662 sent = lg_crcv->sent_pdu;
3671 ++lg_crcv->retry_counter);
3684 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3685#if COAP_OSCORE_SUPPORT
3686 if (session->oscore_encryption &&
3693 lg_crcv->state_token = token;
3747 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3750 lg_crcv = coap_find_lg_crcv(session, rcvd);
3761 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3762 1 << (block.
szx + 4), block.
num);
3768 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3769 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3774 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3776 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3778 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3779 1 << (block.
szx + 4), block.
num);
3784 "next block is not aligned on requested block size boundary. "
3785 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3787 (1 << (block.
szx + 4)),
3788 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3791 track_echo(session, rcvd);
3809 lg_xmit->
offset = (block.
num + 1) * chunk;
3825 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3828 assert(len <=
sizeof(buf));
3830 memcpy(buf, new_token->
s, len);
3849 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3852 ((pdu->
max_size - token_options) /1024) * 1024;
3864#if COAP_CONSTRAINED_STACK
3866 static uint8_t l_data[1024];
3868 uint8_t l_data[1024];
3872 assert(chunk <= 1024);
3874 block.
num * chunk, l_data, &l_length,
3889#if COAP_Q_BLOCK_SUPPORT
3892 if (coap_send_q_block1(session, block, pdu,
3912 int observe_action = -1;
3915 goto lg_xmit_finished;
3925 goto lg_xmit_finished;
3933 goto lg_xmit_finished;
3935 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
3937#if COAP_Q_BLOCK_SUPPORT
3946 const uint8_t *data;
3951 uint16_t fmt = fmt_opt ?
3961 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3967 const uint8_t *bp = data;
3973 size_t ltoken_length;
3975 for (i = 0; (bp < data + length) &&
3977 if ((*bp & 0xc0) != 0x00)
3979 block.
num = derive_cbor_value(&bp, data + length - bp);
3981 if (block.
num > (1 << 20) -1)
3983 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4013 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4016 goto lg_xmit_finished;
4030 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4041 lg_crcv->sent_pdu->lg_xmit = 0;
4053 LL_DELETE(session->
lg_xmit, lg_xmit);
4070 const uint8_t *data,
size_t offset,
size_t total) {
4073 if (body_data ==
NULL && total) {
4076 if (body_data ==
NULL)
4080 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4085 if (offset + length <= total && body_data->length >= total) {
4086 memcpy(&body_data->
s[offset], data, length);
4102 memcpy(&body_data->
s[offset], data, length);
4111#if COAP_CLIENT_SUPPORT
4131#if COAP_Q_BLOCK_SUPPORT
4135 uint16_t block_opt = 0;
4137 int ack_rst_sent = 0;
4140 memset(&block, 0,
sizeof(block));
4141#if COAP_Q_BLOCK_SUPPORT
4142 memset(&qblock, 0,
sizeof(qblock));
4144 lg_crcv = coap_find_lg_crcv(session, rcvd);
4152 const uint8_t *data;
4155 size_t size2 = size_opt ?
4167#if COAP_Q_BLOCK_SUPPORT
4170 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4176 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4181 track_echo(session, rcvd);
4182 if (have_block && (block.
m || length)) {
4186 uint16_t fmt = fmt_opt ?
4193 size_t saved_offset;
4206 goto expire_lg_crcv;
4209 chunk = (size_t)1 << (block.
szx + 4);
4210 offset = block.
num * chunk;
4211 if (size2 < (offset + length)) {
4213 size2 = offset + length + 1;
4215 size2 = offset + length;
4217 saved_offset = offset;
4219 if (lg_crcv->initial) {
4220#if COAP_Q_BLOCK_SUPPORT
4223 lg_crcv->initial = 0;
4224 if (lg_crcv->body_data) {
4226 lg_crcv->body_data =
NULL;
4230 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4231 lg_crcv->etag_set = 1;
4233 lg_crcv->etag_set = 0;
4235 lg_crcv->total_len = size2;
4236 lg_crcv->content_format = fmt;
4237 lg_crcv->szx = block.
szx;
4238 lg_crcv->block_option = block_opt;
4239 lg_crcv->last_type = rcvd->
type;
4240 lg_crcv->rec_blocks.used = 0;
4241 lg_crcv->rec_blocks.total_blocks = 0;
4242#if COAP_Q_BLOCK_SUPPORT
4243 lg_crcv->rec_blocks.processing_payload_set = 0;
4246 if (lg_crcv->total_len < size2)
4247 lg_crcv->total_len = size2;
4251 uint8_t max_block_szx;
4254 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4255 max_block_szx = block.
szx;
4257 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4261 (size2 > max_body)) {
4264 if (max_body_size == 0 || max_body < max_body_size) {
4265 max_body_size = max_body;
4277 lg_crcv->etag, lg_crcv->etag_length)) {
4281 ++lg_crcv->retry_counter);
4285#if COAP_Q_BLOCK_SUPPORT
4290 coap_log_warn(
"Data body updated during receipt - new request started\n");
4294 lg_crcv->initial = 1;
4296 lg_crcv->body_data =
NULL;
4308 (0 << 4) | (0 << 3) | block.
aszx),
4314 goto skip_app_handler;
4316 }
else if (lg_crcv->etag_set) {
4322 if (fmt != lg_crcv->content_format) {
4326#if COAP_Q_BLOCK_SUPPORT
4332 if (block.
num == 0) {
4338 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4339 lg_crcv->observe_set = 1;
4341 lg_crcv->observe_set = 0;
4345 while (offset < saved_offset + length) {
4347#if COAP_Q_BLOCK_SUPPORT
4351 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4352 1 << (block.
szx + 4), block.
num);
4353#if COAP_Q_BLOCK_SUPPORT
4355 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4356 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4357 coap_request_missing_q_block2(session, lg_crcv);
4359 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4369 offset = block.
num << (block.
szx + 4);
4375 if (updated_block) {
4382 if (size2 < saved_offset + length) {
4383 size2 = saved_offset + length;
4390 &lg_crcv->body_data,
4392 saved_offset, size2));
4398 saved_offset, size2);
4399 if (lg_crcv->body_data ==
NULL) {
4412#if COAP_Q_BLOCK_SUPPORT
4418 if (check_all_blocks_in_for_payload_set(session,
4419 &lg_crcv->rec_blocks)) {
4420 block.
num = lg_crcv->rec_blocks.range[0].end;
4422 lg_crcv->rec_blocks.processing_payload_set =
4424 if (check_any_blocks_next_payload_set(session,
4425 &lg_crcv->rec_blocks)) {
4427 coap_request_missing_q_block2(session, lg_crcv);
4428 goto skip_app_handler;
4433 goto skip_app_handler;
4437 goto skip_app_handler;
4460 ((block.
num + 1) << 4) |
4461 (block.
m << 3) | block.
aszx),
4466 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4471 goto skip_app_handler;
4474 goto skip_app_handler;
4479#if COAP_Q_BLOCK_SUPPORT
4481 lg_crcv->total_len : size2;
4490 if (lg_crcv->app_token)
4492 lg_crcv->app_token->s);
4495 goto call_app_handler;
4497#if COAP_Q_BLOCK_SUPPORT
4503 if (lg_crcv->observe_set) {
4505 lg_crcv->observe_length, lg_crcv->observe);
4507 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4508#if COAP_Q_BLOCK_SUPPORT
4517 lg_crcv->total_len : saved_offset + length;
4525#if COAP_Q_BLOCK_SUPPORT
4527 lg_crcv->total_len : size2;
4540 if (lg_crcv->app_token)
4542 lg_crcv->app_token->s);
4545 body_free = lg_crcv->body_data;
4546 lg_crcv->body_data =
NULL;
4550 if (lg_crcv->observe_set == 0) {
4552 LL_DELETE(session->lg_crcv, lg_crcv);
4553 coap_block_delete_lg_crcv(session, lg_crcv);
4554 goto skip_app_handler;
4557 lg_crcv->initial = 1;
4560 goto skip_app_handler;
4575 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4576 lg_crcv->observe_set = 1;
4578 lg_crcv->observe_set = 0;
4587 goto expire_lg_crcv;
4592#if COAP_OSCORE_SUPPORT
4593 if (check_freshness(session, rcvd,
4594 (session->oscore_encryption == 0) ? sent :
NULL,
4597 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4599 goto skip_app_handler;
4600 goto expire_lg_crcv;
4603 goto expire_lg_crcv;
4605 if (!block.
m && !lg_crcv->observe_set) {
4624#
if COAP_Q_BLOCK_SUPPORT
4631 goto skip_app_handler;
4634 const uint8_t *data;
4638#if COAP_Q_BLOCK_SUPPORT
4639 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4645 if (block.
num != 0) {
4647 size_t chunk = (size_t)1 << (block.
szx + 4);
4652 goto call_app_handler;
4655#if COAP_Q_BLOCK_SUPPORT
4659 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4665 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4668 LL_PREPEND(session->lg_crcv, lg_crcv);
4669 return coap_handle_response_get_block(context, session, sent, rcvd,
4682 track_echo(session, rcvd);
4684 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4687 LL_PREPEND(session->lg_crcv, lg_crcv);
4688 return coap_handle_response_get_block(context, session, sent, rcvd,
4705 if (lg_crcv->app_token)
4707 lg_crcv->app_token->s);
4711 LL_DELETE(session->lg_crcv, lg_crcv);
4712 coap_block_delete_lg_crcv(session, lg_crcv);
4724#if COAP_SERVER_SUPPORT
4733 if (response->
code == 0)
4735 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4743#if COAP_CLIENT_SUPPORT
4746 uint64_t token_match =
4752 if (session->lg_crcv) {
4753 LL_FOREACH(session->lg_crcv, lg_crcv) {
4758 lg_crcv->app_token->s);
4766 LL_FOREACH(session->
lg_xmit, lg_xmit) {
int coap_is_mcast(const coap_address_t *a)
Checks if given address a denotes a multicast address.
void coap_address_copy(coap_address_t *dst, const coap_address_t *src)
int coap_address_equals(const coap_address_t *a, const coap_address_t *b)
Compares given address objects a and b.
static void coap_block_release_lg_xmit_data(coap_session_t *session, coap_lg_xmit_data_t *data_info)
#define COAP_ETAG_MAX_BYTES
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
static int coap_add_data_large_internal(coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *pdu, coap_resource_t *resource, const coap_string_t *query, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr, int single_request, coap_pdu_code_t request_method)
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num, uint32_t block_m)
static int check_all_blocks_in(coap_rblock_t *rec_blocks)
static int setup_block_b(coap_session_t *session, coap_pdu_t *pdu, coap_block_b_t *block, unsigned int num, unsigned int blk_size, size_t total)
static int check_if_received_block(coap_rblock_t *rec_blocks, uint32_t block_num)
int coap_flsll(long long j)
int coap_fls(unsigned int i)
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
@ COAP_NACK_TOO_MANY_RETRIES
#define COAP_SOCKET_MULTICAST
socket is used for multicast communication
Library specific build wrapper for coap_internal.h.
void * coap_realloc_type(coap_memory_tag_t type, void *p, size_t size)
Reallocates a chunk p of bytes created by coap_malloc_type() or coap_realloc_type() and returns a poi...
void * coap_malloc_type(coap_memory_tag_t type, size_t size)
Allocates a chunk of size bytes and returns a pointer to the newly allocated memory.
void coap_free_type(coap_memory_tag_t type, void *p)
Releases the memory that was allocated by coap_malloc_type().
uint8_t coap_unique_id[8]
uint16_t coap_option_num_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_mid_t coap_send_rst_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
void coap_call_response_handler(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, void *body_free)
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
#define coap_check_update_token(a, b)
int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
void coap_context_set_block_mode_lkd(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
#define COAP_BLOCK_MAX_SIZE_SET(a)
void coap_check_code_lg_xmit(const coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_resource_t *resource, const coap_string_t *query)
The function checks that the code in a newly formed lg_xmit created by coap_add_data_large_response_l...
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
#define STATE_TOKEN_FULL(t, r)
#define COAP_SINGLE_BLOCK_OR_Q
#define STATE_TOKEN_BASE(t)
#define COAP_BLOCK_SET_MASK
coap_lg_xmit_t * coap_find_lg_xmit(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_xmit for the session that matches the pdu.
#define COAP_BLOCK_MAX_SIZE_GET(a)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
COAP_API int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
#define COAP_BLOCK_USE_M_Q_BLOCK
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
#define COAP_BLOCK_STLESS_BLOCK2
COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
#define COAP_BLOCK_TRY_Q_BLOCK
#define COAP_BLOCK_STLESS_FETCH
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, coap_block_b_t *block)
Adds the appropriate payload data of the body to the pdu.
#define COAP_BLOCK_SINGLE_BODY
int coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
int coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, unsigned int block_num, unsigned char block_szx)
Adds the block_num block of size 1 << (block_szx + 4) from source data to pdu.
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
void coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, uint16_t media_type, int maxage, size_t length, const uint8_t *data)
Adds the appropriate part of data to the response pdu.
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_USE_LIBCOAP
time_t coap_time_t
CoAP time in seconds since epoch.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
#define COAP_MAX_DELAY_TICKS
int coap_handle_event_lkd(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
uint16_t coap_new_message_id_lkd(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_client_delay_first(coap_session_t *session)
Delay the sending of the first client request until some other negotiation has completed.
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *request_pdu)
Sends a CoAP message to given peer.
void coap_register_block_data_handler(coap_context_t *context, coap_block_data_handler_t block_data_handler)
Sets up a handler that is called for each received block during a block-wise transfer when COAP_BLOCK...
coap_response_t(* coap_block_data_handler_t)(coap_session_t *session, coap_pdu_t *pdu, coap_resource_t *resource, coap_binary_t **body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Definition of the block data handler function.
void coap_ticks(coap_tick_t *t)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_OK
Response is fine.
unsigned int coap_encode_var_safe(uint8_t *buf, size_t length, unsigned int val)
Encodes multiple-length byte sequences.
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t len)
Decodes multiple-length byte sequences.
unsigned int coap_encode_var_safe8(uint8_t *buf, size_t length, uint64_t val)
Encodes multiple-length byte sequences.
@ COAP_EVENT_BLOCK_ISSUE
Triggered when a block transfer could not be handled.
@ COAP_EVENT_PARTIAL_BLOCK
Triggered when not all of a large body has been received.
@ COAP_EVENT_XMIT_BLOCK_FAIL
Triggered when not all of a large body has been transmitted.
#define coap_lock_callback(func)
Dummy for no thread-safe code.
#define coap_lock_callback_ret(r, func)
Dummy for no thread-safe code.
#define coap_lock_unlock()
Dummy for no thread-safe code.
#define coap_lock_check_locked()
Dummy for no thread-safe code.
#define coap_lock_lock(failed)
Dummy for no thread-safe code.
#define coap_log_debug(...)
void coap_show_pdu(coap_log_t level, const coap_pdu_t *pdu)
Display the contents of the specified pdu.
const char * coap_session_str(const coap_session_t *session)
Get session description.
#define coap_log_info(...)
#define coap_log_warn(...)
#define COAP_OBSERVE_CANCEL
The value COAP_OBSERVE_CANCEL in a GET/FETCH request option COAP_OPTION_OBSERVE indicates that the ob...
#define COAP_OBSERVE_ESTABLISH
The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option COAP_OPTION_OBSERVE indicates a new ob...
COAP_API int coap_cancel_observe(coap_session_t *session, coap_binary_t *token, coap_pdu_type_t message_type)
Cancel an observe that is being tracked by the client large receive logic.
coap_opt_t * coap_option_next(coap_opt_iterator_t *oi)
Updates the iterator oi to point to the next option.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
coap_opt_iterator_t * coap_option_iterator_init(const coap_pdu_t *pdu, coap_opt_iterator_t *oi, const coap_opt_filter_t *filter)
Initializes the given option iterator oi to point to the beginning of the pdu's option list.
size_t coap_opt_encode_size(uint16_t delta, size_t length)
Compute storage bytes needed for an option with given delta and length.
#define COAP_OPT_ALL
Pre-defined filter that includes all options.
coap_opt_t * coap_check_option(const coap_pdu_t *pdu, coap_option_num_t number, coap_opt_iterator_t *oi)
Retrieves the first option of number number from pdu.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t option)
Sets the corresponding entry for number in filter.
int coap_rebuild_pdu_for_proxy(coap_pdu_t *pdu)
Convert PDU to use Proxy-Scheme option if Proxy-Uri option is present.
size_t coap_oscore_overhead(coap_session_t *session, coap_pdu_t *pdu)
Determine the additional data size requirements for adding in OSCORE.
#define COAP_PDU_IS_RESPONSE(pdu)
coap_pdu_t * coap_pdu_reference_lkd(coap_pdu_t *pdu)
Increment reference counter on a pdu to stop it prematurely getting freed off when coap_delete_pdu() ...
void coap_delete_pdu_lkd(coap_pdu_t *pdu)
Dispose of an CoAP PDU and free off associated storage.
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Inserts option of given number in the pdu with the appropriate data.
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number)
Removes (first) option of given number from the pdu.
int coap_update_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Updates token in pdu with length len and data.
size_t coap_update_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Updates existing first option of given number in the pdu with the new data.
coap_pdu_t * coap_pdu_duplicate_lkd(const coap_pdu_t *old_pdu, coap_session_t *session, size_t token_length, const uint8_t *token, coap_opt_filter_t *drop_options)
Duplicate an existing PDU.
#define COAP_PAYLOAD_START
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t size)
Dynamically grows the size of pdu to new_size if needed.
#define COAP_PDU_IS_REQUEST(pdu)
size_t coap_add_option_internal(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_BLOCK2
const char * coap_response_phrase(unsigned char code)
Returns a human-readable response phrase for the specified CoAP response code.
#define COAP_MEDIATYPE_APPLICATION_MB_CBOR_SEQ
#define COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_SIZE2
#define COAP_OPTION_BLOCK1
#define COAP_OPTION_Q_BLOCK1
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#define COAP_OPTION_URI_PATH
#define COAP_RESPONSE_CODE(N)
#define COAP_RESPONSE_CLASS(C)
coap_pdu_code_t
Set of codes available for a PDU.
#define COAP_OPTION_SIZE1
coap_pdu_type_t
CoAP PDU message type definitions.
#define COAP_MEDIATYPE_TEXT_PLAIN
int coap_add_token(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds token of length len to pdu.
#define COAP_OPTION_CONTENT_TYPE
size_t coap_add_option(coap_pdu_t *pdu, coap_option_num_t number, size_t len, const uint8_t *data)
Adds option of given number to pdu that is passed as first parameter.
#define COAP_OPTION_Q_BLOCK2
int coap_get_data(const coap_pdu_t *pdu, size_t *len, const uint8_t **data)
Retrieves the length and data pointer of specified PDU.
coap_pdu_t * coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code, coap_mid_t mid, size_t size)
Creates a new CoAP PDU with at least enough storage space for the given size maximum message size.
int coap_get_data_large(const coap_pdu_t *pdu, size_t *len, const uint8_t **data, size_t *offset, size_t *total)
Retrieves the data from a PDU, with support for large bodies of data that spans multiple PDUs.
#define COAP_INVALID_MID
Indicates an invalid message id.
#define COAP_OPTION_MAXAGE
#define COAP_OPTION_OBSERVE
int coap_add_data(coap_pdu_t *pdu, size_t len, const uint8_t *data)
Adds given data to the pdu that is passed as first parameter.
@ COAP_REQUEST_CODE_FETCH
#define COAP_NON_RECEIVE_TIMEOUT_TICKS(s)
The NON_RECEIVE_TIMEOUT definition for the session (s).
#define COAP_NON_TIMEOUT_TICKS(s)
void coap_handle_nack(coap_session_t *session, coap_pdu_t *sent, const coap_nack_reason_t reason, const coap_mid_t mid)
#define COAP_MAX_TRANSMIT_WAIT_TICKS(s)
#define COAP_NON_PARTIAL_TIMEOUT_TICKS(s)
The NON_PARTIAL_TIMEOUT definition for the session (s).
coap_tick_t coap_get_non_timeout_random_ticks(coap_session_t *session)
#define COAP_MAX_PAYLOADS(s)
size_t coap_session_max_pdu_size_lkd(const coap_session_t *session)
Get maximum acceptable PDU size.
#define COAP_NON_MAX_RETRANSMIT(s)
#define COAP_PROTO_NOT_RELIABLE(p)
#define COAP_PROTO_RELIABLE(p)
void coap_session_new_token(coap_session_t *session, size_t *len, uint8_t *data)
Creates a new token for use.
@ COAP_SESSION_TYPE_CLIENT
client-side
void coap_delete_bin_const(coap_bin_const_t *s)
Deletes the given const binary data and releases any memory allocated.
void coap_delete_str_const(coap_str_const_t *s)
Deletes the given const string and releases any memory allocated.
coap_binary_t * coap_new_binary(size_t size)
Returns a new binary object with at least size bytes storage allocated.
coap_bin_const_t * coap_new_bin_const(const uint8_t *data, size_t size)
Take the specified byte array (text) and create a coap_bin_const_t * Returns a new const binary objec...
coap_binary_t * coap_resize_binary(coap_binary_t *s, size_t size)
Resizes the given coap_binary_t object.
void coap_delete_binary(coap_binary_t *s)
Deletes the given coap_binary_t object and releases any memory allocated.
#define coap_binary_equal(binary1, binary2)
Compares the two binary data for equality.
#define coap_string_equal(string1, string2)
Compares the two strings for equality.
coap_string_t * coap_new_string(size_t size)
Returns a new string object with at least size+1 bytes storage allocated.
coap_str_const_t * coap_new_str_const(const uint8_t *data, size_t size)
Returns a new const string object with at least size+1 bytes storage allocated, and the provided data...
void coap_delete_string(coap_string_t *s)
Deletes the given string and releases any memory allocated.
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
#define COAP_STATIC_INLINE
coap_address_t remote
remote address and port
coap_address_t local
local address and port
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options with BERT support.
unsigned int num
block number
uint32_t chunk_size
1024 if BERT
unsigned int bert
Operating as BERT.
unsigned int aszx
block size (0-7 including BERT
unsigned int defined
Set if block found.
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size (0-6)
Structure of Block options.
unsigned int num
block number
unsigned int szx
block size
unsigned int m
1 if more blocks follow, 0 otherwise
The CoAP stack's global state is stored in a coap_context_t object.
coap_block_data_handler_t block_data_cb
Called with each block data during block transfers.
uint32_t max_body_size
Max supported body size or 0 is unlimited.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint64_t state_token
state token
size_t bert_size
size of last BERT block
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
coap_pdu_code_t request_method
Method used to request this data.
uint8_t rtag_length
RTag length.
coap_string_t * query
Associated query for the resource.
coap_resource_t * resource
associated resource
coap_time_t maxage_expire
When this entry expires.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint8_t rtag[8]
RTag for block checking.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
applicaton provided ptr for de-alloc function
uint32_t ref
Reference count.
const uint8_t * data
large data ptr
size_t length
large data length
coap_release_large_data_t release_func
large data de-alloc function
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
union coap_lg_xmit_t::@1 b
coap_lg_xmit_data_t * data_info
Pointer to large data information.
int last_block
last acknowledged block number Block1 last transmitted Q-Block2
coap_tick_t last_payload
Last time MAX_PAYLOAD was sent or 0.
size_t offset
large data next offset to transmit
coap_pdu_t * sent_pdu
The sent pdu with all the data.
uint16_t option
large block transmisson CoAP option
struct coap_lg_xmit_t * next
coap_tick_t last_obs
Last time used (Observe tracking) or 0.
Iterator to run through PDU options.
coap_option_num_t number
decoded option number
uint8_t * token
first byte of token (or extended length bytes prefix), if any, or options
coap_lg_xmit_t * lg_xmit
Holds ptr to lg_xmit if sending a set of blocks.
size_t body_length
Holds body data length.
size_t max_size
maximum size for token, options and payload, or zero for variable size pdu
const uint8_t * body_data
Holds ptr to re-assembled data or NULL.
size_t body_offset
Holds body data offset.
coap_pdu_code_t code
request method (value 1–31) or response code (value 64-255)
coap_bin_const_t actual_token
Actual token in pdu.
uint8_t * data
first byte of payload, if any
coap_mid_t mid
message id, if any, in regular host byte order
size_t used_size
used bytes of storage for token, options and payload
coap_session_t * session
Session responsible for PDU or NULL.
size_t body_total
Holds body data total size.
coap_pdu_type_t type
message type
Structure to keep track of received blocks.
uint32_t total_blocks
Set to block no + 1 when More bit unset.
uint32_t used
Number of range blocks in use.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
coap_lg_xmit_t * lg_xmit
list of large transmissions
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_socket_t sock
socket object for the session, if any
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
coap_mid_t remote_test_mid
mid used for checking remote support
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_addr_tuple_t addr_info
remote/local address info
coap_proto_t proto
protocol used
uint8_t con_active
Active CON request sent.
coap_queue_t * delayqueue
list of delayed messages waiting to be sent
uint32_t tx_rtag
Next Request-Tag number to use.
coap_session_type_t type
client or server side socket
coap_context_t * context
session's context
coap_bin_const_t * echo
last token used to make a request
coap_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
size_t length
length of string