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 if (lg_crcv->obs_token_cnt <= block_num)
2397 lg_crcv->obs_token_cnt = block_num + 1;
2400 if (lg_crcv->obs_token[block_num] ==
NULL)
2404 if (block_num < lg_crcv->obs_token_cnt) {
2405 return lg_crcv->obs_token[block_num];
2412#if COAP_Q_BLOCK_SUPPORT
2417 coap_send_pdu_t send_request) {
2420 uint64_t token_match =
2424 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2433 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2438#if COAP_SERVER_SUPPORT
2439#if COAP_Q_BLOCK_SUPPORT
2450 coap_send_pdu_t send_response) {
2455 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2460 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2463 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2473 if (data_info->
ref > 0) {
2485#if COAP_CLIENT_SUPPORT
2486#if COAP_Q_BLOCK_SUPPORT
2518 11, (
const uint8_t *)
".well-known");
2520 4, (
const uint8_t *)
"core");
2533 (0 << 4) | (0 << 3) | 0),
2553 if (lg_crcv ==
NULL)
2556 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2560 lg_crcv->initial = 1;
2578 const uint8_t *data;
2581 if (data_len < lg_xmit->data_info->length) {
2592 if (!lg_crcv->app_token) {
2593 coap_block_delete_lg_crcv(session, lg_crcv);
2599 lg_crcv->retry_counter = 1;
2600 lg_crcv->state_token = state_token;
2607 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2615 lg_crcv->o_blk_size = block.
aszx;
2626#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2629 if (lg_crcv ==
NULL)
2633 if (lg_crcv->obs_data) {
2635 lg_crcv->obs_data =
NULL;
2641 for (i = 0; i < lg_crcv->obs_token_cnt; i++) {
2650#if COAP_SERVER_SUPPORT
2654#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2657 if (lg_srcv ==
NULL)
2672 if (lg_xmit ==
NULL)
2687#if COAP_SERVER_SUPPORT
2694add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2695 uint32_t *count, uint32_t max_count) {
2698 for (i = 0; i < *count && *count < max_count; i++) {
2699 if (num == out_blocks[i].num)
2701 else if (num < out_blocks[i].num) {
2703 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2704 out_blocks[i].num = num;
2705 out_blocks[i].is_continue = is_continue;
2710 if (*count < max_count) {
2711 out_blocks[i].num = num;
2712 out_blocks[i].is_continue = is_continue;
2745 uint16_t block_opt = 0;
2746 send_track *out_blocks =
NULL;
2747 const char *error_phrase;
2752 uint32_t request_cnt, i;
2755#if COAP_Q_BLOCK_SUPPORT
2760 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2767 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2768 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2769 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2771 goto skip_app_handler;
2778 if (block.
num == 0) {
2782 lg_xmit = coap_find_lg_xmit_response(session, pdu, resource, query);
2783 if (lg_xmit ==
NULL)
2786#if COAP_Q_BLOCK_SUPPORT
2792 goto internal_issue;
2805 if (etag == lg_xmit->
b.
b2.
etag) {
2819 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2822 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2823 block.
num, block.
m);
2825 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2826 1 << (block.
szx + 4), block.
num, block.
m);
2829 if (block.
num == 0) {
2830 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2835 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2837 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2839 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2840 1 << (block.
szx + 4), block.
num);
2843 "next block is not aligned on requested block size "
2844 "boundary. (%" PRIuS " x %u mod %u = %" PRIuS " (which is not 0)\n",
2846 (1 << (block.
szx + 4)),
2847 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2850 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2851 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2863#if COAP_Q_BLOCK_SUPPORT
2876 sizeof(
"Changing blocksize during request invalid")-1,
2877 (
const uint8_t *)
"Changing blocksize during request invalid");
2879 goto skip_app_handler;
2881#if COAP_Q_BLOCK_SUPPORT
2886 goto call_app_handler;
2890 num + i < max_block; i++) {
2891 add_block_send(num + i, 1, out_blocks, &request_cnt,
2899 num + i < max_block; i++) {
2900 add_block_send(num + i, 0, out_blocks, &request_cnt,
2905 add_block_send(num, 0, out_blocks, &request_cnt,
2908 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2912 if (request_cnt == 0) {
2916 out_blocks[0].num = 0;
2917 out_blocks[0].is_continue = 0;
2921 for (i = 0; i < request_cnt; i++) {
2924 block.
num = out_blocks[i].num;
2927 if (i + 1 < request_cnt) {
2934 if (out_blocks[i].is_continue) {
2943 goto internal_issue;
2946 if (out_blocks[i].is_continue)
2961 goto internal_issue;
2971 ((out_pdu->
max_size - token_options) /1024) * 1024;
2982 goto internal_issue;
2984 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
2992 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
3010 goto internal_issue;
3018 goto internal_issue;
3020 if (i + 1 < request_cnt) {
3026 goto skip_app_handler;
3027#if COAP_Q_BLOCK_SUPPORT
3037 (
const uint8_t *)error_phrase);
3058 rec_blocks->
retry = 0;
3060 for (i = 0; i < rec_blocks->
used; i++) {
3061 if (block_num >= rec_blocks->
range[i].
begin &&
3062 block_num <= rec_blocks->range[i].end)
3065 if (block_num < rec_blocks->range[i].begin) {
3066 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3073 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3074 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3080 if (block_num == rec_blocks->
range[i].
end + 1) {
3081 rec_blocks->
range[i].
end = block_num;
3082 if (i + 1 < rec_blocks->
used) {
3083 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3086 if (i+2 < rec_blocks->
used) {
3087 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3088 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3096 if (i == rec_blocks->
used) {
3110#if COAP_SERVER_SUPPORT
3130 const uint8_t *data =
NULL;
3135 uint16_t block_option = 0;
3142 const uint8_t *rtag;
3143 uint32_t max_block_szx;
3145 unsigned int saved_num;
3146 size_t saved_offset;
3149 *pfree_lg_srcv =
NULL;
3156#if COAP_Q_BLOCK_SUPPORT
3159 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3160 (
const uint8_t *)
"Block1 + Q-Block1 together");
3162 goto skip_app_handler;
3166#if COAP_Q_BLOCK_SUPPORT
3172 if (!block_option ||
3186 goto skip_app_handler;
3188 goto call_app_handler;
3214 goto skip_app_handler;
3222 if (max_block_szx == 0 || max_block_szx > block.
szx) {
3223 max_block_szx = block.
szx;
3225 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
3229 (total > max_body)) {
3234 if (max_body_size == 0 || max_body < max_body_size) {
3235 max_body_size = max_body;
3242 snprintf(buf,
sizeof(buf),
"Max body size %" PRIu32, max_body_size);
3246 goto skip_app_handler;
3249 offset = block.
num << (block.
szx + 4);
3252#
if COAP_Q_BLOCK_SUPPORT
3271 if (total < (length + offset + (block.
m ? 1 : 0)))
3272 total = length + offset + (block.m ? 1 : 0);
3274 *added_block = block.
m;
3276 goto call_app_handler;
3282 LL_FOREACH(session->lg_srcv, lg_srcv) {
3283 if (rtag_opt || lg_srcv->rtag_set == 1) {
3284 if (!(rtag_opt && lg_srcv->rtag_set == 1))
3286 if (lg_srcv->rtag_length != rtag_length ||
3287 memcmp(lg_srcv->rtag, rtag, rtag_length) != 0)
3290 if (resource == lg_srcv->resource) {
3293 if ((lg_srcv->resource == context->unknown_resource ||
3294 resource == context->proxy_uri_resource) &&
3301 (
const uint8_t *)
"Missing block 0");
3303 goto skip_app_handler;
3309 if (lg_srcv ==
NULL) {
3311 (
const uint8_t *)
"Memory issue");
3313 goto skip_app_handler;
3318 lg_srcv->resource = resource;
3319 if (resource == context->unknown_resource ||
3320 resource == context->proxy_uri_resource)
3322 lg_srcv->content_format = fmt;
3323 lg_srcv->total_len = total;
3325 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3326 max_block_szx < block.
szx) {
3327 lg_srcv->szx = max_block_szx;
3329 lg_srcv->szx = block.
szx;
3331 lg_srcv->block_option = block_option;
3334 memcpy(lg_srcv->observe,
coap_opt_value(observe), lg_srcv->observe_length);
3335 lg_srcv->observe_set = 1;
3339 memcpy(lg_srcv->rtag,
coap_opt_value(rtag_opt), lg_srcv->rtag_length);
3340 lg_srcv->rtag_set = 1;
3342 lg_srcv->body_data =
NULL;
3343 LL_PREPEND(session->lg_srcv, lg_srcv);
3349 !check_if_next_block(&lg_srcv->rec_blocks, block.
num)) {
3351 (
const uint8_t *)
"Missing interim block");
3353 goto skip_app_handler;
3356 if (fmt != lg_srcv->content_format) {
3357 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3358 (
const uint8_t *)
"Content-Format mismatch");
3363#if COAP_Q_BLOCK_SUPPORT
3365 if (total != lg_srcv->total_len) {
3367 (
const uint8_t *)
"Size1 mismatch");
3377 lg_srcv->last_mid = pdu->
mid;
3378 lg_srcv->last_type = pdu->
type;
3381 saved_num = block.
num;
3382 saved_offset = offset;
3384 while (offset < saved_offset + length) {
3389 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3390 (
const uint8_t *)
"Too many missing blocks");
3397 offset = block.
num << (block.
szx + 4);
3403#if COAP_Q_BLOCK_SUPPORT
3404 lg_srcv->rec_blocks.processing_payload_set =
3407 if (lg_srcv->total_len < saved_offset + length) {
3408 lg_srcv->total_len = saved_offset + length;
3411#define USE_BLOCK_DATA_HANDLER (context && context->block_data_cb && \
3412 !resource->is_proxy_uri && \
3413 !resource->is_reverse_proxy && \
3414 ((session->block_mode & COAP_SINGLE_BLOCK_OR_Q) || block.bert) && \
3415 (resource->flags & COAP_RESOURCE_USE_BLOCK_DATA_HANDLER))
3417 if (USE_BLOCK_DATA_HANDLER) {
3422 &lg_srcv->body_data,
3423 length, data, saved_offset,
3424 lg_srcv->total_len));
3427 goto skip_app_handler;
3431 saved_offset, lg_srcv->total_len);
3432 if (!lg_srcv->body_data) {
3434 (
const uint8_t *)
"Memory issue");
3436 goto skip_app_handler;
3447#if COAP_Q_BLOCK_SUPPORT
3452 if (lg_srcv->rec_blocks.used == 1 &&
3455 if (block.
num != lg_srcv->rec_blocks.range[0].end) {
3457 block.
num = lg_srcv->rec_blocks.range[0].end;
3458 goto skip_app_handler;
3463 goto skip_app_handler;
3467 goto skip_app_handler;
3473 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3474 max_block_szx < block.
aszx) {
3475 block.
aszx = max_block_szx;
3506 LL_FOREACH(session->lg_srcv, sg) {
3507 if (lg_srcv == sg) {
3513 goto skip_app_handler;
3516 if (lg_srcv->last_token) {
3517 coap_update_token(response, lg_srcv->last_token->length, lg_srcv->last_token->s);
3525#if COAP_Q_BLOCK_SUPPORT
3530 lg_srcv->no_more_seen = 1;
3544#if COAP_Q_BLOCK_SUPPORT
3548 goto skip_app_handler;
3558 if (lg_srcv->observe_set) {
3560 lg_srcv->observe_length, lg_srcv->observe);
3563 if (lg_srcv->body_data) {
3572 if (USE_BLOCK_DATA_HANDLER) {
3581 lg_srcv->dont_timeout = 1;
3582 *pfree_lg_srcv = lg_srcv;
3588 LL_DELETE(session->lg_srcv, lg_srcv);
3589 coap_block_delete_lg_srcv(session, lg_srcv);
3596#if COAP_CLIENT_SUPPORT
3597#if COAP_Q_BLOCK_SUPPORT
3599derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3600 uint32_t value = **bp & 0x1f;
3604 }
else if (value == 24) {
3606 return (uint32_t)-1;
3610 }
else if (value == 25) {
3612 return (uint32_t)-1;
3620 return (uint32_t)-1;
3621 value = (uint32_t)(**bp) << 24;
3623 value |= **bp << 16;
3641 if (sent || lg_xmit || lg_crcv) {
3645 const uint8_t *data;
3655 }
else if (lg_xmit) {
3658 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3659 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3662 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3663 lg_xmit->data_info->length - offset;
3666 sent = lg_crcv->sent_pdu;
3675 ++lg_crcv->retry_counter);
3688 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3689#if COAP_OSCORE_SUPPORT
3690 if (session->oscore_encryption &&
3697 lg_crcv->state_token = token;
3751 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3754 lg_crcv = coap_find_lg_crcv(session, rcvd);
3765 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3766 1 << (block.
szx + 4), block.
num);
3772 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3773 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3778 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3780 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3782 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3783 1 << (block.
szx + 4), block.
num);
3788 "next block is not aligned on requested block size boundary. "
3789 "(%" PRIuS " x %u mod %u = %" PRIuS " != 0)\n",
3791 (1 << (block.
szx + 4)),
3792 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3795 track_echo(session, rcvd);
3813 lg_xmit->
offset = (block.
num + 1) * chunk;
3829 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3832 assert(len <=
sizeof(buf));
3834 memcpy(buf, new_token->
s, len);
3853 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3856 ((pdu->
max_size - token_options) /1024) * 1024;
3868#if COAP_CONSTRAINED_STACK
3870 static uint8_t l_data[1024];
3872 uint8_t l_data[1024];
3876 assert(chunk <= 1024);
3878 block.
num * chunk, l_data, &l_length,
3893#if COAP_Q_BLOCK_SUPPORT
3896 if (coap_send_q_block1(session, block, pdu,
3916 int observe_action = -1;
3919 goto lg_xmit_finished;
3929 goto lg_xmit_finished;
3937 goto lg_xmit_finished;
3939 if (check_freshness(session, rcvd, sent, lg_xmit,
NULL))
3941#if COAP_Q_BLOCK_SUPPORT
3950 const uint8_t *data;
3955 uint16_t fmt = fmt_opt ?
3965 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3971 const uint8_t *bp = data;
3977 size_t ltoken_length;
3979 for (i = 0; (bp < data + length) &&
3981 if ((*bp & 0xc0) != 0x00)
3983 block.
num = derive_cbor_value(&bp, data + length - bp);
3985 if (block.
num > (1 << 20) -1)
3987 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
4017 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
4020 goto lg_xmit_finished;
4034 lg_crcv->retry_counter = lg_xmit->
b.
b1.
count;
4045 lg_crcv->sent_pdu->lg_xmit = 0;
4057 LL_DELETE(session->
lg_xmit, lg_xmit);
4071 const uint8_t *data,
size_t offset,
size_t total) {
4084 const uint8_t *data,
size_t offset,
size_t total) {
4087 if (body_data ==
NULL && total) {
4090 if (body_data ==
NULL)
4094 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
4100 if (offset + length <= total && body_data->length >= total) {
4101 memcpy(&body_data->
s[offset], data, length);
4117 memcpy(&body_data->
s[offset], data, length);
4126#if COAP_CLIENT_SUPPORT
4146#if COAP_Q_BLOCK_SUPPORT
4150 uint16_t block_opt = 0;
4152 int ack_rst_sent = 0;
4155 memset(&block, 0,
sizeof(block));
4156#if COAP_Q_BLOCK_SUPPORT
4157 memset(&qblock, 0,
sizeof(qblock));
4159 lg_crcv = coap_find_lg_crcv(session, rcvd);
4167 const uint8_t *data;
4170 size_t size2 = size_opt ?
4182#if COAP_Q_BLOCK_SUPPORT
4185 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4191 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4196 track_echo(session, rcvd);
4197 if (have_block && (block.
m || length)) {
4201 uint16_t fmt = fmt_opt ?
4208 size_t saved_offset;
4221 goto expire_lg_crcv;
4224 chunk = (size_t)1 << (block.
szx + 4);
4225 offset = block.
num * chunk;
4226 if (size2 < (offset + length)) {
4228 size2 = offset + length + 1;
4230 size2 = offset + length;
4232 saved_offset = offset;
4234 if (lg_crcv->initial) {
4235#if COAP_Q_BLOCK_SUPPORT
4238 lg_crcv->initial = 0;
4239 if (lg_crcv->body_data) {
4241 lg_crcv->body_data =
NULL;
4245 memcpy(lg_crcv->etag,
coap_opt_value(etag_opt), lg_crcv->etag_length);
4246 lg_crcv->etag_set = 1;
4248 lg_crcv->etag_set = 0;
4250 lg_crcv->total_len = size2;
4251 lg_crcv->content_format = fmt;
4252 lg_crcv->szx = block.
szx;
4253 lg_crcv->block_option = block_opt;
4254 lg_crcv->last_type = rcvd->
type;
4255 lg_crcv->rec_blocks.used = 0;
4256 lg_crcv->rec_blocks.total_blocks = 0;
4257#if COAP_Q_BLOCK_SUPPORT
4258 lg_crcv->rec_blocks.processing_payload_set = 0;
4261 if (lg_crcv->total_len < size2)
4262 lg_crcv->total_len = size2;
4266 uint8_t max_block_szx;
4269 if (max_block_szx == 0 || max_block_szx > block.
szx) {
4270 max_block_szx = block.
szx;
4272 max_body = ((1UL << 20) * (1 << (max_block_szx + 4)));
4276 (size2 > max_body)) {
4279 if (max_body_size == 0 || max_body < max_body_size) {
4280 max_body_size = max_body;
4292 lg_crcv->etag, lg_crcv->etag_length)) {
4296 ++lg_crcv->retry_counter);
4300#if COAP_Q_BLOCK_SUPPORT
4305 coap_log_warn(
"Data body updated during receipt - new request started\n");
4309 lg_crcv->initial = 1;
4311 lg_crcv->body_data =
NULL;
4323 (0 << 4) | (0 << 3) | block.
aszx),
4329 goto skip_app_handler;
4331 }
else if (lg_crcv->etag_set) {
4337 if (fmt != lg_crcv->content_format) {
4341#if COAP_Q_BLOCK_SUPPORT
4347 if (block.
num == 0) {
4353 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4354 lg_crcv->observe_set = 1;
4356 lg_crcv->observe_set = 0;
4360 while (offset < saved_offset + length) {
4362#if COAP_Q_BLOCK_SUPPORT
4366 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4367 1 << (block.
szx + 4), block.
num);
4368#if COAP_Q_BLOCK_SUPPORT
4370 this_payload_set > lg_crcv->rec_blocks.processing_payload_set &&
4371 this_payload_set != lg_crcv->rec_blocks.latest_payload_set) {
4372 coap_request_missing_q_block2(session, lg_crcv);
4374 lg_crcv->rec_blocks.latest_payload_set = this_payload_set;
4384 offset = block.
num << (block.
szx + 4);
4390 if (updated_block) {
4397 if (size2 < saved_offset + length) {
4398 size2 = saved_offset + length;
4405 &lg_crcv->body_data,
4407 saved_offset, size2));
4413 saved_offset, size2);
4414 if (lg_crcv->body_data ==
NULL) {
4427#if COAP_Q_BLOCK_SUPPORT
4433 if (check_all_blocks_in_for_payload_set(session,
4434 &lg_crcv->rec_blocks)) {
4435 block.
num = lg_crcv->rec_blocks.range[0].end;
4437 lg_crcv->rec_blocks.processing_payload_set =
4439 if (check_any_blocks_next_payload_set(session,
4440 &lg_crcv->rec_blocks)) {
4442 coap_request_missing_q_block2(session, lg_crcv);
4443 goto skip_app_handler;
4448 goto skip_app_handler;
4452 goto skip_app_handler;
4475 ((block.
num + 1) << 4) |
4476 (block.
m << 3) | block.
aszx),
4481 coap_add_data_large_internal(session,
NULL, pdu,
NULL,
NULL, -1, 0, length, data,
NULL,
NULL,
NULL,
4486 goto skip_app_handler;
4489 goto skip_app_handler;
4494#if COAP_Q_BLOCK_SUPPORT
4496 lg_crcv->total_len : size2;
4505 if (lg_crcv->app_token)
4507 lg_crcv->app_token->s);
4510 goto call_app_handler;
4512#if COAP_Q_BLOCK_SUPPORT
4518 if (lg_crcv->observe_set) {
4520 lg_crcv->observe_length, lg_crcv->observe);
4522 rcvd->
body_data = lg_crcv->body_data ? lg_crcv->body_data->s :
NULL;
4523#if COAP_Q_BLOCK_SUPPORT
4532 lg_crcv->total_len : saved_offset + length;
4540#if COAP_Q_BLOCK_SUPPORT
4542 lg_crcv->total_len : size2;
4555 if (lg_crcv->app_token)
4557 lg_crcv->app_token->s);
4560 body_free = lg_crcv->body_data;
4561 lg_crcv->body_data =
NULL;
4565 if (lg_crcv->observe_set == 0) {
4567 LL_DELETE(session->lg_crcv, lg_crcv);
4568 coap_block_delete_lg_crcv(session, lg_crcv);
4569 goto skip_app_handler;
4572 lg_crcv->initial = 1;
4575 goto skip_app_handler;
4590 memcpy(lg_crcv->observe,
coap_opt_value(obs_opt), lg_crcv->observe_length);
4591 lg_crcv->observe_set = 1;
4593 lg_crcv->observe_set = 0;
4602 goto expire_lg_crcv;
4607#if COAP_OSCORE_SUPPORT
4608 if (check_freshness(session, rcvd,
4609 (session->oscore_encryption == 0) ? sent :
NULL,
4612 if (check_freshness(session, rcvd, sent,
NULL, lg_crcv))
4614 goto skip_app_handler;
4615 goto expire_lg_crcv;
4618 goto expire_lg_crcv;
4620 if (!block.
m && !lg_crcv->observe_set) {
4639#
if COAP_Q_BLOCK_SUPPORT
4646 goto skip_app_handler;
4649 const uint8_t *data;
4653#if COAP_Q_BLOCK_SUPPORT
4654 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4660 if (block.
num != 0) {
4662 size_t chunk = (size_t)1 << (block.
szx + 4);
4667 goto call_app_handler;
4670#if COAP_Q_BLOCK_SUPPORT
4674 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4680 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4683 LL_PREPEND(session->lg_crcv, lg_crcv);
4684 return coap_handle_response_get_block(context, session, sent, rcvd,
4697 track_echo(session, rcvd);
4699 lg_crcv = coap_block_new_lg_crcv(session, sent,
NULL);
4702 LL_PREPEND(session->lg_crcv, lg_crcv);
4703 return coap_handle_response_get_block(context, session, sent, rcvd,
4720 if (lg_crcv->app_token)
4722 lg_crcv->app_token->s);
4726 LL_DELETE(session->lg_crcv, lg_crcv);
4727 coap_block_delete_lg_crcv(session, lg_crcv);
4739#if COAP_SERVER_SUPPORT
4748 if (response->
code == 0)
4750 lg_xmit = coap_find_lg_xmit_response(session, request, resource, query);
4758#if COAP_CLIENT_SUPPORT
4761 uint64_t token_match =
4767 if (session->lg_crcv) {
4768 LL_FOREACH(session->lg_crcv, lg_crcv) {
4773 lg_crcv->app_token->s);
4781 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)
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_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.
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