19#define min(a,b) ((a) < (b) ? (a) : (b))
22#if COAP_Q_BLOCK_SUPPORT
68 if (block->
szx == 7) {
80 if (block->
m && (length % 1024) != 0) {
81 coap_log_debug(
"block: Oversized packet - reduced to %zu from %zu\n",
82 length - (length % 1024), length);
83 length -= length % 1024;
116 block->
m = block_b.
m;
126 unsigned int blk_size,
size_t total) {
128 size_t avail = pdu->
max_size - token_options;
129 unsigned int start = num << (blk_size + 4);
130 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
132 assert(start <= total);
133 memset(block, 0,
sizeof(*block));
135 block->
szx = block->
aszx = blk_size;
136 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
141 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
143 block->
chunk_size = (size_t)1 << (blk_size + 4);
144 if (avail < block->chunk_size && (total - start) >= avail) {
150 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
153 new_blk_size =
coap_flsll((
long long)avail) - 5;
154 coap_log_debug(
"decrease block size for %zu to %d\n", avail, new_blk_size);
156 block->
szx = new_blk_size;
157 block->
num <<= szx - block->
szx;
158 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
169 unsigned char buf[4];
174 start = block->
num << (block->
szx + 4);
175 if (block->
num != 0 && data_length <= start) {
185 block->
szx, data_length))
190 ((block_b.
num << 4) |
203 unsigned char buf[4];
207 start = block->
num << (block->
szx + 4);
208 if (block->
num != 0 && data_length <= start) {
216 block->
szx, data_length))
231 unsigned int block_num,
unsigned char block_szx) {
233 start = block_num << (block_szx + 4);
239 min(len - start, ((
size_t)1 << (block_szx + 4))),
246 unsigned int start = block->
num << (block->
szx + 4);
254 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
256 max_size = (size_t)1 << (block->
szx + 4);
261 min(len - start, max_size),
277 unsigned char buf[4];
279 int block2_requested = 0;
281 memset(&block2, 0,
sizeof(block2));
288 block2_requested = 1;
289 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
292 length >> (block2.
szx + 4));
301 memset(etag, 0,
sizeof(etag));
316 if (block2_requested) {
376 uint32_t block_mode) {
384 uint32_t block_mode) {
390#if ! COAP_Q_BLOCK_SUPPORT
398 size_t max_block_size) {
409 switch (max_block_size) {
420 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%zu)\n",
425 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
433 const uint8_t *b,
size_t blen) {
434 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
437#if COAP_CLIENT_SUPPORT
461 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
466 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
474#if COAP_Q_BLOCK_SUPPORT
516#if COAP_Q_BLOCK_SUPPORT
518 if (using_q_block1) {
519 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
535#if COAP_OSCORE_SUPPORT
541 uint64_t token_match =
552 LL_FOREACH(session->
lg_crcv, lg_crcv) {
591#if COAP_SERVER_SUPPORT
608 LL_FOREACH(session->
lg_xmit, lg_xmit) {
625 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
650#if COAP_Q_BLOCK_SUPPORT
656 int have_block_defined = 0;
658 uint8_t max_blk_size;
660 size_t token_options;
663#if COAP_Q_BLOCK_SUPPORT
669 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
677 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
691#define MAX_BLK_LEN (((1UL << 20) - 1) * (1 << (6 + 4)))
693#if UINT_MAX > MAX_BLK_LEN
704#if COAP_Q_BLOCK_SUPPORT
720 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
723 LL_DELETE(session->
lg_xmit, lg_xmit);
733#if COAP_Q_BLOCK_SUPPORT
734 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
747#if COAP_SERVER_SUPPORT
755 LL_DELETE(session->
lg_xmit, lg_xmit);
762#if COAP_OSCORE_SUPPORT
763 if (session->oscore_encryption) {
771 avail = pdu->
max_size - token_options;
774#if COAP_OSCORE_SUPPORT
779 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
784 if (max_blk_size && blk_size > max_blk_size)
785 blk_size = max_blk_size;
789 if (block.
szx < blk_size)
790 blk_size = block.
szx;
791 have_block_defined = 1;
793#if COAP_Q_BLOCK_SUPPORT
796 if (have_block_defined) {
798 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
802 if (block.
szx < blk_size)
803 blk_size = block.
szx;
804 have_block_defined = 1;
810 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
812 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
816 chunk = (size_t)1 << (blk_size + 4);
817 if ((have_block_defined && block.
num != 0) || single_request ||
822 if (length >= block.
num * chunk) {
824#if COAP_SERVER_SUPPORT
829 (
unsigned int)length),
841 memcpy(&etag, digest.
key,
sizeof(etag));
859 (block.
num << 4) | (block.
m << 3) | block.
aszx),
864 if (chunk > length - block.
num * chunk)
865 rem = length - block.
num * chunk;
872 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
892 lg_xmit->blk_size = blk_size;
893 lg_xmit->option = option;
894 lg_xmit->data = data;
895 lg_xmit->length = length;
896#if COAP_Q_BLOCK_SUPPORT
897 lg_xmit->non_timeout_random_ticks =
900 lg_xmit->release_func = release_func;
901 lg_xmit->app_ptr = app_ptr;
908 if (!lg_xmit->b.b1.app_token)
916 lg_xmit->b.b1.count = 1;
918 lg_xmit->b.b1.count);
926 (
unsigned int)length),
939 lg_xmit->b.b2.resource = resource;
942 if (lg_xmit->b.b2.query) {
943 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
946 lg_xmit->b.b2.query = NULL;
951 sizeof(lg_xmit->b.b2.rtag));
953 lg_xmit->b.b2.rtag_set = 1;
955 lg_xmit->b.b2.rtag_set = 0;
957 lg_xmit->b.b2.etag = etag;
958 lg_xmit->b.b2.request_method = request_method;
965 lg_xmit->b.b2.maxage_expire = 0;
970 (
unsigned int)length),
984 blk_size, lg_xmit->length))
991 (block.
num << 4) | (block.
m << 3) | block.
aszx),
995 memcpy(&lg_xmit->pdu, pdu,
sizeof(lg_xmit->pdu));
997 lg_xmit->pdu.used_size + lg_xmit->pdu.max_hdr_size);
998 if (!lg_xmit->pdu.token)
1001 lg_xmit->pdu.alloc_size = lg_xmit->pdu.used_size;
1002 lg_xmit->pdu.token += lg_xmit->pdu.max_hdr_size;
1003 memcpy(lg_xmit->pdu.token, pdu->
token, lg_xmit->pdu.used_size);
1005 lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->
data - pdu->
token);
1006 lg_xmit->pdu.actual_token.s = lg_xmit->pdu.token + pdu->
e_token_length -
1012 avail = pdu->
max_size - token_options;
1017#if COAP_OSCORE_SUPPORT
1020 if (avail < (ssize_t)chunk) {
1023 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1026 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1027 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1028 lg_xmit->blk_size = blk_size;
1029 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1035 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1040 if (rem > lg_xmit->length - block.
num * chunk)
1041 rem = lg_xmit->length - block.
num * chunk;
1046 lg_xmit->b.b1.bert_size = rem;
1048 lg_xmit->last_block = -1;
1051 LL_PREPEND(session->
lg_xmit,lg_xmit);
1060 if (have_block_defined) {
1064 (0 << 4) | (0 << 3) | blk_size), buf);
1079 }
else if (release_func) {
1085#if COAP_CLIENT_SUPPORT
1090 const uint8_t *data,
1098 release_func, app_ptr);
1107 const uint8_t *data,
1121 length, data, release_func, app_ptr, 0, 0);
1125#if COAP_SERVER_SUPPORT
1132 uint16_t media_type,
1136 const uint8_t *data,
1144 response, query, media_type, maxage, etag,
1145 length, data, release_func, app_ptr);
1156 uint16_t media_type,
1160 const uint8_t *data,
1164 unsigned char buf[4];
1166 int block_requested = 0;
1167 int single_request = 0;
1168#if COAP_Q_BLOCK_SUPPORT
1169 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1175 memset(&block, 0,
sizeof(block));
1182 block_requested = 1;
1183 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1186 length >> (block.
szx + 4));
1191#if COAP_Q_BLOCK_SUPPORT
1193 block_requested = 1;
1194 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1197 length >> (block.
szx + 4));
1201 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1222 if (block_requested) {
1246 query, maxage, etag, length, data,
1247 release_func, app_ptr, single_request,
1250 goto error_released;
1260#if COAP_ERROR_PHRASE_LENGTH > 0
1278#if COAP_Q_BLOCK_SUPPORT
1288 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1292 LL_DELETE(session->
lg_xmit, lg_xmit);
1296 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1302 if (lg_xmit->
last_sent + partial_timeout <= now) {
1304 LL_DELETE(session->
lg_xmit, lg_xmit);
1309 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1310 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1319#if COAP_CLIENT_SUPPORT
1320#if COAP_Q_BLOCK_SUPPORT
1348 int block_payload_set = -1;
1364 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1365 sofar = block * block_size;
1366 if (sofar < lg_crcv->total_len) {
1369 pdu = coap_build_missing_pdu(session, lg_crcv);
1375 (block << 4) | (1 << 3) | lg_crcv->
szx),
1388 pdu = coap_build_missing_pdu(session, lg_crcv);
1393 if (block_payload_set == -1)
1399 (block << 4) | (0 << 3) | lg_crcv->
szx),
1407 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1408 sofar = (block + 1) * block_size;
1409 if (sofar < lg_crcv->total_len) {
1412 pdu = coap_build_missing_pdu(session, lg_crcv);
1416 sofar = (lg_crcv->
total_len + block_size - 1)/block_size;
1418 if (block_payload_set == -1)
1420 for (; block < (ssize_t)sofar &&
1424 (block << 4) | (0 << 3) | lg_crcv->
szx),
1432 if (block_payload_set != -1)
1433 lg_crcv->
rec_blocks.processing_payload_set = block_payload_set;
1448#if COAP_Q_BLOCK_SUPPORT
1454#if COAP_Q_BLOCK_SUPPORT
1456 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1462 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
1466#if COAP_Q_BLOCK_SUPPORT
1468 size_t scaled_timeout = receive_timeout *
1481 coap_request_missing_q_block2(session, lg_crcv);
1493 lg_crcv->
last_used + partial_timeout <= now) {
1494#if COAP_Q_BLOCK_SUPPORT
1498 LL_DELETE(session->
lg_crcv, lg_crcv);
1502 if (*tim_rem > lg_crcv->
last_used + partial_timeout - now) {
1503 *tim_rem = lg_crcv->
last_used + partial_timeout - now;
1512#if COAP_SERVER_SUPPORT
1513#if COAP_Q_BLOCK_SUPPORT
1541 assert(block >= 0 && block < (1 << 20));
1543 if (block < 0 || block >= (1 << 20)) {
1545 }
else if (block < 24) {
1548 }
else if (block < 0x100) {
1552 }
else if (block < 0x10000) {
1555 val[1] = block >> 8;
1556 val[2] = block & 0xff;
1560 val[1] = block >> 16;
1561 val[2] = (block >> 8) & 0xff;
1562 val[3] = block & 0xff;
1578 for (i = 0; i < rec_blocks->
used; i++) {
1579 if (block_num < rec_blocks->range[i].begin)
1581 if (block_num <= rec_blocks->range[i].end)
1587#if COAP_SERVER_SUPPORT
1589check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1590 if (rec_blocks->
used == 0) {
1591 return block_num == 0 ? 1 : 0;
1593 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1610 for (i = 0; i < rec_blocks->
used; i++) {
1611 if (block < rec_blocks->range[i].begin)
1613 if (block < rec_blocks->range[i].end)
1619#if COAP_CLIENT_SUPPORT
1620#if COAP_Q_BLOCK_SUPPORT
1624 if (rec_blocks->
used &&
1626 rec_blocks->processing_payload_set)
1634 if (rec_blocks->
used > 1 &&
1636 rec_blocks->processing_payload_set)
1643#if COAP_SERVER_SUPPORT
1654#if COAP_Q_BLOCK_SUPPORT
1660#if COAP_Q_BLOCK_SUPPORT
1662 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1668 LL_FOREACH_SAFE(session->
lg_srcv, lg_srcv, q) {
1672#if COAP_Q_BLOCK_SUPPORT
1674 size_t scaled_timeout = receive_timeout *
1684 size_t block_size = (size_t)1 << (lg_srcv->
szx + 4);
1685 size_t final_block = (lg_srcv->
total_len + block_size - 1)/block_size - 1;
1687 size_t last_payload_block;
1689 size_t no_blocks = 0;
1702 if (no_blocks == 0 && block == (
int)final_block)
1708 if (final_block > last_payload_block) {
1709 final_block = last_payload_block;
1711 no_blocks += final_block - block;
1712 if (no_blocks == 0) {
1714 final_block = (lg_srcv->
total_len + block_size - 1)/block_size - 1;
1716 if (final_block > last_payload_block) {
1717 final_block = last_payload_block;
1727 pdu = pdu_408_build(session, lg_srcv);
1733 if (!add_408_block(pdu, block)) {
1743 for (; block <= (int)final_block; block++) {
1745 pdu = pdu_408_build(session, lg_srcv);
1749 if (!add_408_block(pdu, block)) {
1769#if COAP_Q_BLOCK_SUPPORT
1789 (
const uint8_t *)
"Missing interim block");
1793 LL_DELETE(session->
lg_srcv, lg_srcv);
1797 if (*tim_rem > lg_srcv->
last_used + partial_timeout - now) {
1798 *tim_rem = lg_srcv->
last_used + partial_timeout - now;
1807#if COAP_Q_BLOCK_SUPPORT
1816 coap_send_pdu_t send_pdu) {
1821 const uint8_t *ptoken;
1823 size_t ltoken_length;
1824 uint32_t delayqueue_cnt = 0;
1827 if (send_pdu == COAP_SEND_INC_PDU)
1836 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
1837 LL_FOREACH(session->
delayqueue, delayqueue) {
1865 ptoken, &drop_options);
1871 if (send_pdu == COAP_SEND_INC_PDU &&
1881 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
1907 ltoken_length, ptoken, &drop_options);
1912 ((block.
num) << 4) |
1951#if COAP_CLIENT_SUPPORT
1959 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1960 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1962 if (now < non_timeout)
1963 return non_timeout - now;
1964 timed_out = now - non_timeout;
1970 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
1972 memset(&block, 0,
sizeof(block));
1973 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
1976 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->
pdu, COAP_SEND_SKIP_PDU);
1977 if (tim_rem > non_timeout)
1978 tim_rem = non_timeout;
1988 LL_DELETE(session->
lg_xmit, lg_xmit);
1992 if (tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now)
2001#if COAP_SERVER_SUPPORT
2009 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2010 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2012 if (now < non_timeout)
2013 return non_timeout - now;
2014 timed_out = now - non_timeout;
2020 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2022 memset(&block, 0,
sizeof(block));
2023 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2027 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->
pdu, COAP_SEND_SKIP_PDU);
2028 if (tim_rem > non_timeout)
2029 tim_rem = non_timeout;
2039 LL_DELETE(session->
lg_xmit, lg_xmit);
2043 if (tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now)
2053#if COAP_CLIENT_SUPPORT
2067 if (opt && lg_crcv) {
2068 int observe_action = -1;
2079 (block_num + 1) *
sizeof(lg_crcv->
obs_token[0]));
2093 if (lg_crcv->
obs_token[block_num] == NULL)
2097 if (block_num < lg_crcv->obs_token_cnt) {
2105#if COAP_Q_BLOCK_SUPPORT
2110 coap_send_pdu_t send_request) {
2113 uint64_t token_match =
2117 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2126 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2131#if COAP_SERVER_SUPPORT
2132#if COAP_Q_BLOCK_SUPPORT
2143 coap_send_pdu_t send_response) {
2148 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2156 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2161#if COAP_CLIENT_SUPPORT
2162#if COAP_Q_BLOCK_SUPPORT
2194 11, (
const u_char *)
".well-known");
2196 4, (
const u_char *)
"core");
2209 (0 << 4) | (0 << 3) | 0),
2226 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
2228 size_t data_len = lg_xmit ? lg_xmit->
length :
2234 if (lg_crcv == NULL)
2237 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
2241 lg_crcv->initial = 1;
2244 memcpy(&lg_crcv->pdu, pdu,
sizeof(lg_crcv->pdu));
2246 lg_crcv->pdu.max_size = token_options + data_len + 9;
2247 lg_crcv->pdu.used_size = token_options + data_len;
2249 token_options + data_len + lg_crcv->pdu.max_hdr_size);
2250 if (!lg_crcv->pdu.token) {
2254 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
2255 memcpy(lg_crcv->pdu.token, pdu->
token, token_options);
2256 if (lg_crcv->pdu.data) {
2257 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
2259 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->
data : pdu->
data, data_len);
2264 if (!lg_crcv->app_token) {
2271 lg_crcv->retry_counter = 1;
2272 lg_crcv->state_token = state_token;
2278 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2286 lg_crcv->o_blk_size = block.
aszx;
2302#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2305 if (lg_crcv == NULL)
2308 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2309 if (lg_xmit->
lg_crcv == lg_crcv) {
2329#if COAP_SERVER_SUPPORT
2333#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2336 if (lg_srcv == NULL)
2351 if (lg_xmit == NULL)
2370#if COAP_SERVER_SUPPORT
2377add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2378 uint32_t *count, uint32_t max_count) {
2381 for (i = 0; i < *count && *count < max_count; i++) {
2382 if (num == out_blocks[i].num)
2384 else if (num < out_blocks[i].num) {
2386 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2387 out_blocks[i].num = num;
2388 out_blocks[i].is_continue = is_continue;
2393 if (*count < max_count) {
2394 out_blocks[i].num = num;
2395 out_blocks[i].is_continue = is_continue;
2428 uint16_t block_opt = 0;
2429 send_track *out_blocks = NULL;
2430 const char *error_phrase;
2435 uint32_t request_cnt, i;
2438#if COAP_Q_BLOCK_SUPPORT
2443 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2450 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2451 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2452 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2454 goto skip_app_handler;
2461 if (block.
num == 0) {
2466 if (lg_xmit == NULL)
2469#if COAP_Q_BLOCK_SUPPORT
2475 goto internal_issue;
2484 if (etag != lg_xmit->
b.
b2.
etag) {
2490 goto skip_app_handler;
2497 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2500 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2501 block.
num, block.
m);
2503 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2504 1 << (block.
szx + 4), block.
num, block.
m);
2507 if (block.
num == 0) {
2508 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2513 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2515 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2517 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2518 1 << (block.
szx + 4), block.
num);
2521 "next block is not aligned on requested block size "
2522 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2524 (1 << (block.
szx + 4)),
2525 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2528 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2529 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2541#if COAP_Q_BLOCK_SUPPORT
2542 max_block = (lg_xmit->
length + chunk - 1)/chunk;
2554 sizeof(
"Changing blocksize during request invalid")-1,
2555 (
const uint8_t *)
"Changing blocksize during request invalid");
2557 goto skip_app_handler;
2559#if COAP_Q_BLOCK_SUPPORT
2564 goto call_app_handler;
2568 num + i < max_block; i++) {
2569 add_block_send(num + i, 1, out_blocks, &request_cnt,
2577 num + i < max_block; i++) {
2578 add_block_send(num + i, 0, out_blocks, &request_cnt,
2583 add_block_send(num, 0, out_blocks, &request_cnt,
2586 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2590 if (request_cnt == 0) {
2594 out_blocks[0].num = 0;
2595 out_blocks[0].is_continue = 0;
2599 for (i = 0; i < request_cnt; i++) {
2602 block.
num = out_blocks[i].num;
2605 if (i + 1 < request_cnt) {
2612 if (out_blocks[i].is_continue) {
2620 goto internal_issue;
2623 if (out_blocks[i].is_continue)
2638 goto internal_issue;
2648 ((out_pdu->
max_size - token_options) /1024) * 1024;
2659 goto internal_issue;
2661 if (!(lg_xmit->
offset + chunk < lg_xmit->length)) {
2669 if (!(lg_xmit->
offset + chunk < lg_xmit->length)) {
2687 goto internal_issue;
2695 goto internal_issue;
2697 if (i + 1 < request_cnt) {
2703 goto skip_app_handler;
2704#if COAP_Q_BLOCK_SUPPORT
2714 (
const uint8_t *)error_phrase);
2735 rec_blocks->
retry = 0;
2737 for (i = 0; i < rec_blocks->
used; i++) {
2738 if (block_num >= rec_blocks->
range[i].
begin &&
2739 block_num <= rec_blocks->range[i].end)
2742 if (block_num < rec_blocks->range[i].begin) {
2743 if (block_num + 1 == rec_blocks->
range[i].
begin) {
2750 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
2751 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
2757 if (block_num == rec_blocks->
range[i].
end + 1) {
2758 rec_blocks->
range[i].
end = block_num;
2759 if (i + 1 < rec_blocks->
used) {
2760 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
2763 if (i+2 < rec_blocks->
used) {
2764 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
2765 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
2773 if (i == rec_blocks->
used) {
2787#if COAP_SERVER_SUPPORT
2807 const uint8_t *data = NULL;
2812 uint16_t block_option = 0;
2819 const uint8_t *rtag;
2820 uint32_t max_block_szx;
2822 unsigned int saved_num;
2823 size_t saved_offset;
2826 *pfree_lg_srcv = NULL;
2833#if COAP_Q_BLOCK_SUPPORT
2836 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
2837 (
const uint8_t *)
"Block1 + Q-Block1 together");
2839 goto skip_app_handler;
2843#if COAP_Q_BLOCK_SUPPORT
2849 if (!block_option ||
2852 goto call_app_handler;
2878 goto skip_app_handler;
2882 offset = block.
num << (block.
szx + 4);
2904 if (total < (length + offset + (block.
m ? 1 : 0)))
2905 total = length + offset + (block.
m ? 1 : 0);
2907 *added_block = block.
m;
2909 goto call_app_handler;
2915 LL_FOREACH(session->
lg_srcv, lg_srcv) {
2916 if (rtag_opt || lg_srcv->
rtag_set == 1) {
2917 if (!(rtag_opt && lg_srcv->
rtag_set == 1))
2920 memcmp(lg_srcv->
rtag, rtag, rtag_length) != 0)
2923 if (resource == lg_srcv->
resource) {
2934 (
const uint8_t *)
"Missing block 0");
2936 goto skip_app_handler;
2942 if (lg_srcv == NULL) {
2944 (
const uint8_t *)
"Memory issue");
2946 goto skip_app_handler;
2958 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
2959 max_block_szx < block.
szx) {
2960 lg_srcv->
szx = max_block_szx;
2962 lg_srcv->
szx = block.
szx;
2976 LL_PREPEND(session->
lg_srcv, lg_srcv);
2984 (
const uint8_t *)
"Missing interim block");
2986 goto skip_app_handler;
2990 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
2991 (
const uint8_t *)
"Content-Format mismatch");
2996#if COAP_Q_BLOCK_SUPPORT
3000 (
const uint8_t *)
"Size1 mismatch");
3014 saved_num = block.
num;
3015 saved_offset = offset;
3017 while (offset < saved_offset + length) {
3022 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3023 (
const uint8_t *)
"Too many missing blocks");
3030 offset = block.
num << (block.
szx + 4);
3036#if COAP_Q_BLOCK_SUPPORT
3040 if (lg_srcv->
total_len < saved_offset + length) {
3041 lg_srcv->
total_len = saved_offset + length;
3047 (
const uint8_t *)
"Memory issue");
3049 goto skip_app_handler;
3059#if COAP_Q_BLOCK_SUPPORT
3070 goto skip_app_handler;
3075 goto skip_app_handler;
3079 goto skip_app_handler;
3085 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3086 max_block_szx < block.
aszx) {
3087 block.
aszx = max_block_szx;
3125#if COAP_Q_BLOCK_SUPPORT
3144#if COAP_Q_BLOCK_SUPPORT
3148 goto skip_app_handler;
3174 *pfree_lg_srcv = lg_srcv;
3180 LL_DELETE(session->
lg_srcv, lg_srcv);
3188#if COAP_CLIENT_SUPPORT
3189#if COAP_Q_BLOCK_SUPPORT
3191derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3192 uint32_t value = **bp & 0x1f;
3196 }
else if (value == 24) {
3198 return (uint32_t)-1;
3202 }
else if (value == 25) {
3204 return (uint32_t)-1;
3212 return (uint32_t)-1;
3215 value |= **bp << 16;
3233 if (sent || lg_xmit || lg_crcv) {
3237 const uint8_t *data;
3247 }
else if (lg_xmit) {
3248 sent = &lg_xmit->
pdu;
3250 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3251 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3253 data = &lg_xmit->
data[offset];
3254 data_len = (lg_xmit->
length - offset) > blk_size ? blk_size :
3255 lg_xmit->length - offset;
3258 sent = &lg_crcv->
pdu;
3280 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3281#if COAP_OSCORE_SUPPORT
3282 if (session->oscore_encryption &&
3339 uint64_t token_match =
3344 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
3352 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3363 coap_log_debug(
"found Block option, block is BERT, block nr. %u (%zu)\n",
3366 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3367 1 << (block.
szx + 4), block.
num);
3373 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3374 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3379 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3381 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3383 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3384 1 << (block.
szx + 4), block.
num);
3389 "next block is not aligned on requested block size boundary. "
3390 "(%zu x %u mod %u = %zu != 0)\n",
3392 (1 << (block.
szx + 4)),
3393 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3396 track_echo(session, rcvd);
3414 lg_xmit->
offset = (block.
num + 1) * chunk;
3430 new_token = track_fetch_observe(&lg_xmit->
pdu, lg_crcv, block.
num + 1,
3433 assert(len <=
sizeof(buf));
3435 memcpy(buf, new_token->
s, len);
3446 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3449 ((pdu->
max_size - token_options) /1024) * 1024;
3467#if COAP_Q_BLOCK_SUPPORT
3470 if (coap_send_q_block1(session, block, pdu,
3490 int observe_action = -1;
3493 goto lg_xmit_finished;
3503 goto lg_xmit_finished;
3511 goto lg_xmit_finished;
3513 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3515#if COAP_Q_BLOCK_SUPPORT
3524 const uint8_t *data;
3529 uint16_t fmt = fmt_opt ?
3539 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3545 const uint8_t *bp = data;
3551 size_t ltoken_length;
3553 for (i = 0; (bp < data + length) &&
3555 if ((*bp & 0xc0) != 0x00)
3557 block.
num = derive_cbor_value(&bp, data + length - bp);
3559 if (block.
num > (1 << 20) -1)
3561 block.
m = (block.
num + 1) * chunk < lg_xmit->length;
3591 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
3594 goto lg_xmit_finished;
3631 LL_DELETE(session->
lg_xmit, lg_xmit);
3639 const uint8_t *data,
size_t offset,
size_t total) {
3653 const uint8_t *data,
size_t offset,
size_t total) {
3656 if (body_data == NULL && total) {
3659 if (body_data == NULL)
3663 if (SIZE_MAX - length < 8 || offset > SIZE_MAX - length - 8) {
3669 if (offset + length <= total && body_data->length >= total) {
3670 memcpy(&body_data->
s[offset], data, length);
3686 memcpy(&body_data->
s[offset], data, length);
3695#if COAP_CLIENT_SUPPORT
3715#if COAP_Q_BLOCK_SUPPORT
3719 uint16_t block_opt = 0;
3721 int ack_rst_sent = 0;
3722 uint64_t token_match =
3727 memset(&block, 0,
sizeof(block));
3728#if COAP_Q_BLOCK_SUPPORT
3729 memset(&qblock, 0,
sizeof(qblock));
3731 LL_FOREACH(session->
lg_crcv, lg_crcv) {
3746 const uint8_t *data;
3749 size_t size2 = size_opt ?
3761#if COAP_Q_BLOCK_SUPPORT
3764 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
3770 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3775 track_echo(session, rcvd);
3776 if (have_block && (block.
m || length)) {
3780 uint16_t fmt = fmt_opt ?
3787 size_t saved_offset;
3800 goto expire_lg_crcv;
3803 chunk = (size_t)1 << (block.
szx + 4);
3804 offset = block.
num * chunk;
3805 if (size2 < (offset + length)) {
3807 size2 = offset + length + 1;
3809 size2 = offset + length;
3811 saved_offset = offset;
3814#if COAP_Q_BLOCK_SUPPORT
3831 lg_crcv->
szx = block.
szx;
3836#if COAP_Q_BLOCK_SUPPORT
3837 lg_crcv->
rec_blocks.processing_payload_set = 0;
3854#if COAP_Q_BLOCK_SUPPORT
3859 coap_log_warn(
"Data body updated during receipt - new request started\n");
3876 (0 << 4) | (0 << 3) | block.
aszx),
3882 goto skip_app_handler;
3894#if COAP_Q_BLOCK_SUPPORT
3900 if (block.
num == 0) {
3913 while (offset < saved_offset + length) {
3915#if COAP_Q_BLOCK_SUPPORT
3919 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3920 1 << (block.
szx + 4), block.
num);
3921#if COAP_Q_BLOCK_SUPPORT
3923 this_payload_set > lg_crcv->
rec_blocks.processing_payload_set &&
3924 this_payload_set != lg_crcv->
rec_blocks.latest_payload_set) {
3925 coap_request_missing_q_block2(session, lg_crcv);
3927 lg_crcv->
rec_blocks.latest_payload_set = this_payload_set;
3937 offset = block.
num << (block.
szx + 4);
3943 if (updated_block) {
3945 if (size2 < saved_offset + length) {
3946 size2 = saved_offset + length;
3949 saved_offset, size2);
3961#if COAP_Q_BLOCK_SUPPORT
3967 if (check_all_blocks_in_for_payload_set(session,
3973 if (check_any_blocks_next_payload_set(session,
3976 coap_request_missing_q_block2(session, lg_crcv);
3977 goto skip_app_handler;
3982 goto skip_app_handler;
3986 goto skip_app_handler;
4007 ((block.
num + 1) << 4) |
4008 (block.
m << 3) | block.
aszx),
4013 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, 0, 0);
4019 goto skip_app_handler;
4024#if COAP_Q_BLOCK_SUPPORT
4040 goto call_app_handler;
4042#if COAP_Q_BLOCK_SUPPORT
4053#if COAP_Q_BLOCK_SUPPORT
4055 lg_crcv->
total_len : saved_offset + length;
4063#if COAP_Q_BLOCK_SUPPORT
4102 LL_DELETE(session->
lg_crcv, lg_crcv);
4104 goto skip_app_handler;
4114 goto skip_app_handler;
4132 goto expire_lg_crcv;
4137#if COAP_OSCORE_SUPPORT
4138 if (check_freshness(session, rcvd,
4139 (session->oscore_encryption == 0) ? sent : NULL,
4142 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4144 goto skip_app_handler;
4145 goto expire_lg_crcv;
4148 goto expire_lg_crcv;
4177 goto skip_app_handler;
4181#if COAP_Q_BLOCK_SUPPORT
4182 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4188 if (block.
num != 0) {
4191 const uint8_t *data;
4192 size_t chunk = (size_t)1 << (block.
szx + 4);
4197 goto call_app_handler;
4200#if COAP_Q_BLOCK_SUPPORT
4204 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4213 LL_PREPEND(session->
lg_crcv, lg_crcv);
4218 track_echo(session, rcvd);
4223 LL_PREPEND(session->
lg_crcv, lg_crcv);
4247 LL_DELETE(session->
lg_crcv, lg_crcv);
4260#if COAP_SERVER_SUPPORT
4269 if (response->
code == 0)
4272 if (lg_xmit && lg_xmit->
pdu.
code == 0) {
4279#if COAP_CLIENT_SUPPORT
4282 uint64_t token_match =
4289 LL_FOREACH(session->
lg_crcv, lg_crcv) {
4302 LL_FOREACH(session->
lg_xmit, lg_xmit) {
COAP_STATIC_INLINE int full_match(const uint8_t *a, size_t alen, const uint8_t *b, size_t blen)
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 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, void *app_ptr, int single_request, coap_pdu_code_t request_method)
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)
#define COAP_Q_BLOCK_SUPPORT
int coap_flsll(long long j)
int coap_fls(unsigned int i)
struct coap_resource_t coap_resource_t
unsigned char coap_key_t[4]
#define coap_hash(String, Length, Result)
@ COAP_NACK_TOO_MANY_RETRIES
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().
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.
coap_mid_t coap_send_ack_lkd(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
#define COAP_BLOCK_MAX_SIZE_MASK
int coap_add_data_large_response_lkd(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_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_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
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.
int coap_block_check_lg_crcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
int coap_block_check_lg_srcv_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
#define COAP_BLOCK_MAX_SIZE_SET(a)
void coap_block_delete_lg_crcv(coap_session_t *session, coap_lg_crcv_t *lg_crcv)
int coap_handle_response_get_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd, coap_recurse_t recursive)
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...
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent, coap_pdu_t *rcvd)
coap_mid_t coap_retransmit_oscore_pdu(coap_session_t *session, coap_pdu_t *pdu, coap_opt_t *echo)
int coap_add_data_large_request_lkd(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.
void coap_check_update_token(coap_session_t *session, coap_pdu_t *pdu)
The function checks if the token needs to be updated before PDU is presented to the application (only...
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
int coap_handle_request_put_block(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *uri_path, coap_opt_t *observe, int *added_block, coap_lg_srcv_t **free_lg_srcv)
#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_response(const coap_session_t *session, const coap_pdu_t *request, const coap_resource_t *resource, const coap_string_t *query)
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, coap_lg_xmit_t *lg_xmit)
int coap_handle_request_send_block(coap_session_t *session, coap_pdu_t *pdu, coap_pdu_t *response, coap_resource_t *resource, coap_string_t *query)
#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.
#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.
#define COAP_BLOCK_USE_LIBCOAP
int coap_digest_final(coap_digest_ctx_t *digest_ctx, coap_digest_t *digest_buffer)
Finalize the coap_digest information into the provided digest_buffer.
int coap_digest_update(coap_digest_ctx_t *digest_ctx, const uint8_t *data, size_t data_len)
Update the coap_digest information with the next chunk of data.
coap_digest_ctx_t * coap_digest_setup(void)
Initialize a coap_digest.
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.
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)
Sends a CoAP message to given peer.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
@ COAP_RESPONSE_FAIL
Response not liked - send CoAP RST packet.
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_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_ret_release(r, c, func, failed)
Dummy for no thread-safe code.
#define coap_lock_unlock(c)
Dummy for no thread-safe code.
#define coap_lock_lock(c, failed)
Dummy for no thread-safe code.
#define coap_lock_callback(c, func)
Dummy for no thread-safe code.
#define coap_lock_check_locked(c)
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)
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)
#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_cancel_observe_lkd(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.
int coap_q_block_is_supported(void)
Check whether Q-BlockX is available.
#define COAP_STATIC_INLINE
CoAP binary data definition with const data.
size_t length
length of binary data
const uint8_t * s
read-only binary data
CoAP binary data definition.
size_t length
length of binary data
Structure of Block options with BERT support.
unsigned int num
block number
unsigned int bert
Operating as BERT.
unsigned int aszx
block size (0-7 including BERT
unsigned int defined
Set if block found.
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size (0-6)
Structure of Block options.
unsigned int num
block number
unsigned int szx
block size
unsigned int m
1 if more blocks follow, 0 otherwise
The CoAP stack's global state is stored in a coap_context_t object.
uint64_t etag
Next ETag to use.
coap_nack_handler_t nack_handler
Called when a response issue has occurred.
coap_response_handler_t response_handler
Called when a response is received.
uint32_t block_mode
Zero or more COAP_BLOCK_ or'd options.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
coap_resource_t * unknown_resource
can be used for handling unknown resources
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.
Structure to hold large body (many blocks) client receive information.
uint16_t block_option
Block option in use.
uint8_t etag[8]
ETag for block checking.
uint8_t etag_length
ETag length.
uint8_t last_type
Last request type (CON/NON).
uint8_t observe_length
Length of observe data.
uint8_t observe[3]
Observe data (if observe_set) (only 24 bits).
uint8_t etag_set
Set if ETag is in receive PDU.
uint8_t initial
If set, has not been used yet.
uint8_t szx
size of individual blocks
uint16_t o_block_option
Block CoAP option used when initiating Observe.
uint16_t content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
uint8_t o_blk_size
Block size used when initiating Observe.
coap_tick_t last_used
< list of received blocks
coap_bin_const_t ** obs_token
Tokens used in setting up Observe (to handle large FETCH).
uint64_t state_token
state token
coap_binary_t * app_token
app requesting PDU token
uint16_t retry_counter
Retry counter (part of state token).
coap_binary_t * body_data
Used for re-assembling entire body.
size_t obs_token_cnt
number of tokens used to set up Observe
uint8_t observe_set
Set if this is an observe receive PDU.
size_t total_len
Length as indicated by SIZE2 option.
Structure to hold large body (many blocks) server receive information.
uint8_t rtag[8]
RTag for block checking.
coap_mid_t last_mid
Last received mid for this set of packets.
uint8_t rtag_set
Set if RTag is in receive PDU.
uint16_t block_option
Block option in use.
size_t total_len
Length as indicated by SIZE1 option.
uint8_t observe_length
Length of observe data.
coap_rblock_t rec_blocks
set to uri_path if unknown resource
uint8_t no_more_seen
Set if block with more not set seen.
coap_binary_t * body_data
Used for re-assembling entire body.
coap_resource_t * resource
associated resource
uint8_t observe_set
Set if this is an observe receive PDU.
uint8_t rtag_length
RTag length.
uint8_t last_type
Last request type (CON/NON).
uint8_t szx
size of individual blocks
coap_tick_t last_used
Last time data sent or 0.
uint8_t observe[3]
Observe data (if set) (only 24 bits).
uint16_t content_format
Content format for the set of blocks.
coap_bin_const_t * last_token
< list of received blocks
coap_str_const_t * uri_path
Structure to hold large body (many blocks) transmission information.
coap_tick_t last_all_sent
Last time all data sent or 0.
coap_release_large_data_t release_func
large data de-alloc function
uint8_t blk_size
large block transmission size
coap_tick_t last_sent
Last time any data sent.
coap_lg_crcv_t * lg_crcv
The lg_crcv associated with this blocked xmit.
const uint8_t * data
large data ptr
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 pdu
skeletal PDU
size_t length
large data length
union coap_lg_xmit_t::@152016206261220260141173172226260025274141240323 b
uint16_t option
large block transmisson CoAP option
void * app_ptr
applicaton provided ptr for de-alloc function
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 max_hdr_size
space reserved for protocol-specific header
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
uint32_t e_token_length
length of Token space (includes leading extended bytes
size_t used_size
used bytes of storage for token, options and payload
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.
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_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_lg_srcv_t * lg_srcv
Server list of expected large receives.
coap_lg_crcv_t * lg_crcv
Client list of expected large receives.
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 string data definition.
size_t length
length of string