19#define min(a,b) ((a) < (b) ? (a) : (b))
23#ifndef COAP_ETAG_MAX_BYTES
24#define COAP_ETAG_MAX_BYTES 4
26#if COAP_ETAG_MAX_BYTES < 1 || COAP_ETAG_MAX_BYTES > 8
27#error COAP_ETAG_MAX_BYTES byte size invalid
30#if COAP_Q_BLOCK_SUPPORT
76 if (block->
szx == 7) {
88 if (block->
m && (length % 1024) != 0) {
89 coap_log_debug(
"block: Oversized packet - reduced to %zu from %zu\n",
90 length - (length % 1024), length);
91 length -= length % 1024;
124 block->
m = block_b.
m;
134 unsigned int blk_size,
size_t total) {
136 size_t avail = pdu->
max_size - token_options;
137 unsigned int start = num << (blk_size + 4);
138 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
140 assert(start <= total);
141 memset(block, 0,
sizeof(*block));
143 block->
szx = block->
aszx = blk_size;
144 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
149 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
151 block->
chunk_size = (size_t)1 << (blk_size + 4);
152 if (avail < block->chunk_size && (total - start) >= avail) {
158 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
161 new_blk_size =
coap_flsll((
long long)avail) - 5;
162 coap_log_debug(
"decrease block size for %zu to %d\n", avail, new_blk_size);
164 block->
szx = new_blk_size;
165 block->
num <<= szx - block->
szx;
166 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
177 unsigned char buf[4];
182 start = block->
num << (block->
szx + 4);
183 if (block->
num != 0 && data_length <= start) {
193 block->
szx, data_length))
198 ((block_b.
num << 4) |
211 unsigned char buf[4];
215 start = block->
num << (block->
szx + 4);
216 if (block->
num != 0 && data_length <= start) {
224 block->
szx, data_length))
239 unsigned int block_num,
unsigned char block_szx) {
241 start = block_num << (block_szx + 4);
247 min(len - start, ((
size_t)1 << (block_szx + 4))),
254 unsigned int start = block->
num << (block->
szx + 4);
262 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
264 max_size = (size_t)1 << (block->
szx + 4);
269 min(len - start, max_size),
284 unsigned char buf[4];
286 int block2_requested = 0;
287#if COAP_SERVER_SUPPORT
293 memset(&block2, 0,
sizeof(block2));
300 block2_requested = 1;
301 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
304 length >> (block2.
szx + 4));
312#if COAP_SERVER_SUPPORT
318 if (request && request->
session &&
327 memcpy(&etag, digest.
key,
sizeof(etag));
328#if COAP_ETAG_MAX_BYTES != 8
351 if (block2_requested) {
404#if COAP_SERVER_SUPPORT
414 uint32_t block_mode) {
422 uint32_t block_mode) {
428#if ! COAP_Q_BLOCK_SUPPORT
436 size_t max_block_size) {
447 switch (max_block_size) {
458 coap_log_info(
"coap_context_set_max_block_size: Invalid max block size (%zu)\n",
463 max_block_size = (
coap_fls((uint32_t)max_block_size >> 4) - 1) & 0x07;
464 context->
block_mode &= ~COAP_BLOCK_MAX_SIZE_MASK;
471 const uint8_t *b,
size_t blen) {
472 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
479 uint64_t token_match =
483 LL_FOREACH(session->
lg_xmit, lg_xmit) {
489#if COAP_CLIENT_SUPPORT
509 lg_xmit->
next = NULL;
517 LL_PREPEND(session->
lg_xmit, lg_xmit);
522#if COAP_CLIENT_SUPPORT
540#if COAP_CLIENT_SUPPORT
564 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
569 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
577#if COAP_Q_BLOCK_SUPPORT
625#if COAP_Q_BLOCK_SUPPORT
627 if (using_q_block1) {
628 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
647 uint64_t token_match =
651 LL_FOREACH(session->
lg_crcv, lg_crcv) {
675 LL_PREPEND(session->
lg_crcv, lg_crcv);
681#if COAP_OSCORE_SUPPORT
727#if COAP_SERVER_SUPPORT
744 LL_FOREACH(session->
lg_xmit, lg_xmit) {
751 lg_xmit->b.b2.query ?
752 lg_xmit->b.b2.query : &empty)) {
761 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
787#if COAP_Q_BLOCK_SUPPORT
793 int have_block_defined = 0;
795 uint8_t max_blk_size;
797 size_t token_options;
800#if COAP_Q_BLOCK_SUPPORT
804#if !COAP_SERVER_SUPPORT
810 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
818 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
832#define MAX_BLK_LEN (((1UL << 20) - 1) * (1 << (6 + 4)))
834#if UINT_MAX > MAX_BLK_LEN
841#if COAP_SERVER_SUPPORT
862 memcpy(&etag, digest.
key,
sizeof(etag));
863#if COAP_ETAG_MAX_BYTES != 8
889 if (etag == etag_r) {
904#if COAP_Q_BLOCK_SUPPORT
920 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
924#if COAP_CLIENT_SUPPORT
927 LL_DELETE(session->
lg_xmit, lg_xmit);
938#if COAP_Q_BLOCK_SUPPORT
939 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
952#if COAP_SERVER_SUPPORT
960 LL_DELETE(session->
lg_xmit, lg_xmit);
967#if COAP_OSCORE_SUPPORT
968 if (session->oscore_encryption) {
976 avail = pdu->
max_size - token_options;
979#if COAP_OSCORE_SUPPORT
984 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
989 if (max_blk_size && blk_size > max_blk_size)
990 blk_size = max_blk_size;
994 if (block.
szx < blk_size)
995 blk_size = block.
szx;
996 have_block_defined = 1;
998#if COAP_Q_BLOCK_SUPPORT
1001 if (have_block_defined) {
1003 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
1007 if (block.
szx < blk_size)
1008 blk_size = block.
szx;
1009 have_block_defined = 1;
1010 option = alt_option;
1015 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
1017 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
1021 chunk = (size_t)1 << (blk_size + 4);
1022 if ((have_block_defined && block.
num != 0) || single_request ||
1027 if (length >= block.
num * chunk) {
1028#if COAP_SERVER_SUPPORT
1034 (
unsigned int)length),
1048 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1053 if (chunk > length - block.
num * chunk)
1054 rem = length - block.
num * chunk;
1061 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
1086 lg_xmit->blk_size = blk_size;
1087 lg_xmit->option = option;
1089 if (!lg_xmit->data_info)
1091 lg_xmit->data_info->ref = 0;
1092 lg_xmit->data_info->data = data;
1093 lg_xmit->data_info->length = length;
1094#if COAP_Q_BLOCK_SUPPORT
1095 lg_xmit->non_timeout_random_ticks =
1098 lg_xmit->data_info->get_func = get_func;
1099 lg_xmit->data_info->release_func = release_func;
1100 lg_xmit->data_info->app_ptr = app_ptr;
1107 if (!lg_xmit->b.b1.app_token)
1115 lg_xmit->b.b1.count = 1;
1117 lg_xmit->b.b1.count);
1126 (
unsigned int)length),
1139 lg_xmit->b.b2.resource = resource;
1142 if (lg_xmit->b.b2.query) {
1143 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
1146 lg_xmit->b.b2.query = NULL;
1151 sizeof(lg_xmit->b.b2.rtag));
1153 lg_xmit->b.b2.rtag_set = 1;
1155 lg_xmit->b.b2.rtag_set = 0;
1157 lg_xmit->b.b2.etag = etag;
1158 lg_xmit->b.b2.request_method = request_method;
1165 lg_xmit->b.b2.maxage_expire = 0;
1170 (
unsigned int)length),
1175 blk_size, lg_xmit->data_info->length))
1182 (block.
num << 4) | (block.
m << 3) | block.
aszx),
1190 avail = pdu->
max_size - token_options;
1195#if COAP_OSCORE_SUPPORT
1198 if (avail < (ssize_t)chunk) {
1201 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
1204 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
1205 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
1206 lg_xmit->blk_size = blk_size;
1207 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
1213 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
1218 if (rem > lg_xmit->data_info->length - block.
num * chunk)
1219 rem = lg_xmit->data_info->length - block.
num * chunk;
1221#if COAP_CONSTRAINED_STACK
1223 static uint8_t l_data[1024];
1225 uint8_t l_data[1024];
1229 assert(rem <= 1024);
1230 if (get_func(session, rem, block.
num * chunk, l_data, &l_length, lg_xmit->data_info->app_ptr)) {
1241 lg_xmit->b.b1.bert_size = rem;
1243 lg_xmit->last_block = -1;
1246 LL_PREPEND(session->
lg_xmit,lg_xmit);
1249 if (have_block_defined) {
1253 (0 << 4) | (0 << 3) | blk_size), buf);
1260 if (get_func(session, length, 0, l_data, &l_length, app_ptr)) {
1281 }
else if (release_func) {
1287#if COAP_CLIENT_SUPPORT
1292 const uint8_t *data,
1300 release_func, app_ptr);
1309 const uint8_t *data,
1323 length, data, release_func, NULL, app_ptr, 0, 0);
1337 release_func, get_func, app_ptr);
1357 length, NULL, release_func, get_func,
1362#if COAP_SERVER_SUPPORT
1369 uint16_t media_type,
1373 const uint8_t *data,
1380 response, query, media_type, maxage, etag,
1381 length, data, release_func, app_ptr);
1392 uint16_t media_type,
1396 const uint8_t *data,
1400 unsigned char buf[4];
1402 int block_requested = 0;
1403 int single_request = 0;
1404#if COAP_Q_BLOCK_SUPPORT
1405 uint32_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1411 memset(&block, 0,
sizeof(block));
1418 block_requested = 1;
1419 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1422 length >> (block.
szx + 4));
1427#if COAP_Q_BLOCK_SUPPORT
1429 block_requested = 1;
1430 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1433 length >> (block.
szx + 4));
1437 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1458 if (block_requested) {
1482 query, maxage, etag, length, data,
1483 release_func, NULL, app_ptr, single_request,
1486 goto error_released;
1496#if COAP_ERROR_PHRASE_LENGTH > 0
1514#if COAP_Q_BLOCK_SUPPORT
1524 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1528 LL_DELETE(session->
lg_xmit, lg_xmit);
1532 if (*tim_rem > lg_xmit->
last_all_sent + idle_timeout - now) {
1538 if (lg_xmit->
last_sent + partial_timeout <= now) {
1541#if COAP_CLIENT_SUPPORT
1545 LL_DELETE(session->
lg_xmit, lg_xmit);
1552 if (*tim_rem > lg_xmit->
last_sent + partial_timeout - now) {
1553 *tim_rem = lg_xmit->
last_sent + partial_timeout - now;
1562#if COAP_CLIENT_SUPPORT
1563#if COAP_Q_BLOCK_SUPPORT
1592 int block_payload_set = -1;
1608 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1609 sofar = block * block_size;
1610 if (sofar < lg_crcv->total_len) {
1613 pdu = coap_build_missing_pdu(session, lg_crcv);
1619 (block << 4) | (1 << 3) | lg_crcv->
szx),
1632 pdu = coap_build_missing_pdu(session, lg_crcv);
1637 if (block_payload_set == -1)
1643 (block << 4) | (0 << 3) | lg_crcv->
szx),
1651 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1652 sofar = (block + 1) * block_size;
1653 if (sofar < lg_crcv->total_len) {
1656 pdu = coap_build_missing_pdu(session, lg_crcv);
1660 sofar = (lg_crcv->
total_len + block_size - 1)/block_size;
1662 if (block_payload_set == -1)
1664 for (; block < (ssize_t)sofar &&
1668 (block << 4) | (0 << 3) | lg_crcv->
szx),
1676 if (block_payload_set != -1)
1677 lg_crcv->
rec_blocks.processing_payload_set = block_payload_set;
1692#if COAP_Q_BLOCK_SUPPORT
1698#if COAP_Q_BLOCK_SUPPORT
1700 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1706 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
1710#if COAP_Q_BLOCK_SUPPORT
1712 size_t scaled_timeout = receive_timeout *
1722 coap_request_missing_q_block2(session, lg_crcv);
1734 lg_crcv->
last_used + partial_timeout <= now) {
1735#if COAP_Q_BLOCK_SUPPORT
1739 LL_DELETE(session->
lg_crcv, lg_crcv);
1743 if (*tim_rem > lg_crcv->
last_used + partial_timeout - now) {
1744 *tim_rem = lg_crcv->
last_used + partial_timeout - now;
1753#if COAP_SERVER_SUPPORT
1754#if COAP_Q_BLOCK_SUPPORT
1782 assert(block >= 0 && block < (1 << 20));
1784 if (block < 0 || block >= (1 << 20)) {
1786 }
else if (block < 24) {
1789 }
else if (block < 0x100) {
1793 }
else if (block < 0x10000) {
1796 val[1] = block >> 8;
1797 val[2] = block & 0xff;
1801 val[1] = block >> 16;
1802 val[2] = (block >> 8) & 0xff;
1803 val[3] = block & 0xff;
1819 for (i = 0; i < rec_blocks->
used; i++) {
1820 if (block_num < rec_blocks->range[i].begin)
1822 if (block_num <= rec_blocks->range[i].end)
1828#if COAP_SERVER_SUPPORT
1830check_if_next_block(
coap_rblock_t *rec_blocks, uint32_t block_num) {
1831 if (rec_blocks->
used == 0) {
1832 return block_num == 0 ? 1 : 0;
1834 if (rec_blocks->
range[rec_blocks->
used-1].
end + 1 == block_num)
1851 for (i = 0; i < rec_blocks->
used; i++) {
1852 if (block < rec_blocks->range[i].begin)
1854 if (block < rec_blocks->range[i].end)
1860#if COAP_CLIENT_SUPPORT
1861#if COAP_Q_BLOCK_SUPPORT
1865 if (rec_blocks->
used &&
1867 rec_blocks->processing_payload_set)
1875 if (rec_blocks->
used > 1 &&
1877 rec_blocks->processing_payload_set)
1884#if COAP_SERVER_SUPPORT
1895#if COAP_Q_BLOCK_SUPPORT
1901#if COAP_Q_BLOCK_SUPPORT
1903 session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)
1909 LL_FOREACH_SAFE(session->
lg_srcv, lg_srcv, q) {
1917#if COAP_Q_BLOCK_SUPPORT
1919 size_t scaled_timeout = receive_timeout *
1929 size_t block_size = (size_t)1 << (lg_srcv->
szx + 4);
1930 size_t final_block = (lg_srcv->
total_len + block_size - 1)/block_size - 1;
1932 size_t last_payload_block;
1934 size_t no_blocks = 0;
1947 if (no_blocks == 0 && block == (
int)final_block)
1953 if (final_block > last_payload_block) {
1954 final_block = last_payload_block;
1956 no_blocks += final_block - block;
1957 if (no_blocks == 0) {
1959 final_block = (lg_srcv->
total_len + block_size - 1)/block_size - 1;
1961 if (final_block > last_payload_block) {
1962 final_block = last_payload_block;
1972 pdu = pdu_408_build(session, lg_srcv);
1978 if (!add_408_block(pdu, block)) {
1988 for (; block <= (int)final_block; block++) {
1990 pdu = pdu_408_build(session, lg_srcv);
1994 if (!add_408_block(pdu, block)) {
2014#if COAP_Q_BLOCK_SUPPORT
2034 (
const uint8_t *)
"Missing interim block");
2038 LL_DELETE(session->
lg_srcv, lg_srcv);
2042 if (*tim_rem > lg_srcv->
last_used + partial_timeout - now) {
2043 *tim_rem = lg_srcv->
last_used + partial_timeout - now;
2052#if COAP_Q_BLOCK_SUPPORT
2061 coap_send_pdu_t send_pdu) {
2066 const uint8_t *ptoken;
2068 size_t ltoken_length;
2069 uint32_t delayqueue_cnt = 0;
2072 if (send_pdu == COAP_SEND_INC_PDU)
2081 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
2082 LL_FOREACH(session->
delayqueue, delayqueue) {
2110 ptoken, &drop_options);
2116 if (send_pdu == COAP_SEND_INC_PDU &&
2126 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
2152 ltoken_length, ptoken, &drop_options);
2157 ((block.
num) << 4) |
2168#if COAP_CONSTRAINED_STACK
2170 static uint8_t l_data[1024];
2172 uint8_t l_data[1024];
2176 assert(chunk <= 1024);
2178 block.
num * chunk, l_data, &l_length,
2218#if COAP_CLIENT_SUPPORT
2231 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2232 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2234 if (now <= non_timeout) {
2236 *tim_rem = non_timeout - now;
2239 timed_out = now - non_timeout;
2245 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2247 memset(&block, 0,
sizeof(block));
2248 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2251 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2252 if (*tim_rem > non_timeout) {
2253 *tim_rem = non_timeout;
2267 LL_DELETE(session->
lg_xmit, lg_xmit);
2271 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2282#if COAP_SERVER_SUPPORT
2295 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
2296 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
2298 if (now <= non_timeout) {
2300 *tim_rem = non_timeout - now;
2303 timed_out = now - non_timeout;
2309 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2311 memset(&block, 0,
sizeof(block));
2312 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
2316 coap_send_q_blocks(session, lg_xmit, block, lg_xmit->
sent_pdu, COAP_SEND_SKIP_PDU);
2317 if (*tim_rem > non_timeout) {
2318 *tim_rem = non_timeout;
2332 LL_DELETE(session->
lg_xmit, lg_xmit);
2336 if (*tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now) {
2348#if COAP_CLIENT_SUPPORT
2362 if (opt && lg_crcv) {
2363 int observe_action = -1;
2374 (block_num + 1) *
sizeof(lg_crcv->
obs_token[0]));
2387 if (lg_crcv->
obs_token[block_num] == NULL)
2391 if (block_num < lg_crcv->obs_token_cnt) {
2399#if COAP_Q_BLOCK_SUPPORT
2404 coap_send_pdu_t send_request) {
2407 uint64_t token_match =
2411 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2420 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
2425#if COAP_SERVER_SUPPORT
2426#if COAP_Q_BLOCK_SUPPORT
2437 coap_send_pdu_t send_response) {
2442 LL_FOREACH(session->
lg_xmit, lg_xmit) {
2447 lg_xmit->b.b2.query ? lg_xmit->b.b2.query : &empty))
2450 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
2460 if (data_info->
ref > 0) {
2472#if COAP_CLIENT_SUPPORT
2473#if COAP_Q_BLOCK_SUPPORT
2505 11, (
const uint8_t *)
".well-known");
2507 4, (
const uint8_t *)
"core");
2520 (0 << 4) | (0 << 3) | 0),
2540 if (lg_crcv == NULL)
2543 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxxx%011llx\n",
2547 lg_crcv->initial = 1;
2565 const uint8_t *data;
2568 if (data_len < lg_xmit->data_info->length) {
2579 if (!lg_crcv->app_token) {
2586 lg_crcv->retry_counter = 1;
2587 lg_crcv->state_token = state_token;
2594 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2602 lg_crcv->o_blk_size = block.
aszx;
2613#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2616 if (lg_crcv == NULL)
2637#if COAP_SERVER_SUPPORT
2641#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2644 if (lg_srcv == NULL)
2659 if (lg_xmit == NULL)
2674#if COAP_SERVER_SUPPORT
2681add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2682 uint32_t *count, uint32_t max_count) {
2685 for (i = 0; i < *count && *count < max_count; i++) {
2686 if (num == out_blocks[i].num)
2688 else if (num < out_blocks[i].num) {
2690 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2691 out_blocks[i].num = num;
2692 out_blocks[i].is_continue = is_continue;
2697 if (*count < max_count) {
2698 out_blocks[i].num = num;
2699 out_blocks[i].is_continue = is_continue;
2732 uint16_t block_opt = 0;
2733 send_track *out_blocks = NULL;
2734 const char *error_phrase;
2739 uint32_t request_cnt, i;
2742#if COAP_Q_BLOCK_SUPPORT
2747 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2754 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2755 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2756 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2758 goto skip_app_handler;
2765 if (block.
num == 0) {
2770 if (lg_xmit == NULL)
2773#if COAP_Q_BLOCK_SUPPORT
2779 goto internal_issue;
2792 if (etag == lg_xmit->
b.
b2.
etag) {
2806 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2809 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2810 block.
num, block.
m);
2812 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2813 1 << (block.
szx + 4), block.
num, block.
m);
2816 if (block.
num == 0) {
2817 if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2822 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
2824 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
2826 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2827 1 << (block.
szx + 4), block.
num);
2830 "next block is not aligned on requested block size "
2831 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2833 (1 << (block.
szx + 4)),
2834 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2837 coap_log_debug(
"ignoring request to change Block size from %u to %u\n",
2838 (1 << (lg_xmit->
blk_size + 4)), (1 << (block.
szx + 4)));
2850#if COAP_Q_BLOCK_SUPPORT
2863 sizeof(
"Changing blocksize during request invalid")-1,
2864 (
const uint8_t *)
"Changing blocksize during request invalid");
2866 goto skip_app_handler;
2868#if COAP_Q_BLOCK_SUPPORT
2873 goto call_app_handler;
2877 num + i < max_block; i++) {
2878 add_block_send(num + i, 1, out_blocks, &request_cnt,
2886 num + i < max_block; i++) {
2887 add_block_send(num + i, 0, out_blocks, &request_cnt,
2892 add_block_send(num, 0, out_blocks, &request_cnt,
2895 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2899 if (request_cnt == 0) {
2903 out_blocks[0].num = 0;
2904 out_blocks[0].is_continue = 0;
2908 for (i = 0; i < request_cnt; i++) {
2911 block.
num = out_blocks[i].num;
2914 if (i + 1 < request_cnt) {
2921 if (out_blocks[i].is_continue) {
2930 goto internal_issue;
2933 if (out_blocks[i].is_continue)
2948 goto internal_issue;
2958 ((out_pdu->
max_size - token_options) /1024) * 1024;
2969 goto internal_issue;
2971 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
2979 if (!(lg_xmit->
offset + chunk < lg_xmit->data_info->length)) {
2997 goto internal_issue;
3005 goto internal_issue;
3007 if (i + 1 < request_cnt) {
3013 goto skip_app_handler;
3014#if COAP_Q_BLOCK_SUPPORT
3024 (
const uint8_t *)error_phrase);
3045 rec_blocks->
retry = 0;
3047 for (i = 0; i < rec_blocks->
used; i++) {
3048 if (block_num >= rec_blocks->
range[i].
begin &&
3049 block_num <= rec_blocks->range[i].end)
3052 if (block_num < rec_blocks->range[i].begin) {
3053 if (block_num + 1 == rec_blocks->
range[i].
begin) {
3060 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
3061 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
3067 if (block_num == rec_blocks->
range[i].
end + 1) {
3068 rec_blocks->
range[i].
end = block_num;
3069 if (i + 1 < rec_blocks->
used) {
3070 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
3073 if (i+2 < rec_blocks->
used) {
3074 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
3075 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
3083 if (i == rec_blocks->
used) {
3097#if COAP_SERVER_SUPPORT
3117 const uint8_t *data = NULL;
3122 uint16_t block_option = 0;
3129 const uint8_t *rtag;
3130 uint32_t max_block_szx;
3132 unsigned int saved_num;
3133 size_t saved_offset;
3136 *pfree_lg_srcv = NULL;
3143#if COAP_Q_BLOCK_SUPPORT
3146 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
3147 (
const uint8_t *)
"Block1 + Q-Block1 together");
3149 goto skip_app_handler;
3153#if COAP_Q_BLOCK_SUPPORT
3159 if (!block_option ||
3162 goto call_app_handler;
3188 goto skip_app_handler;
3192 offset = block.
num << (block.
szx + 4);
3195#
if COAP_Q_BLOCK_SUPPORT
3214 if (total < (length + offset + (block.
m ? 1 : 0)))
3215 total = length + offset + (block.m ? 1 : 0);
3217 *added_block = block.
m;
3219 goto call_app_handler;
3225 LL_FOREACH(session->
lg_srcv, lg_srcv) {
3226 if (rtag_opt || lg_srcv->
rtag_set == 1) {
3227 if (!(rtag_opt && lg_srcv->
rtag_set == 1))
3230 memcmp(lg_srcv->
rtag, rtag, rtag_length) != 0)
3233 if (resource == lg_srcv->
resource) {
3244 (
const uint8_t *)
"Missing block 0");
3246 goto skip_app_handler;
3252 if (lg_srcv == NULL) {
3254 (
const uint8_t *)
"Memory issue");
3256 goto skip_app_handler;
3268 if (!block.
bert && block.
num == 0 && max_block_szx != 0 &&
3269 max_block_szx < block.
szx) {
3270 lg_srcv->
szx = max_block_szx;
3272 lg_srcv->
szx = block.
szx;
3286 LL_PREPEND(session->
lg_srcv, lg_srcv);
3294 (
const uint8_t *)
"Missing interim block");
3296 goto skip_app_handler;
3300 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
3301 (
const uint8_t *)
"Content-Format mismatch");
3306#if COAP_Q_BLOCK_SUPPORT
3310 (
const uint8_t *)
"Size1 mismatch");
3324 saved_num = block.
num;
3325 saved_offset = offset;
3327 while (offset < saved_offset + length) {
3332 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
3333 (
const uint8_t *)
"Too many missing blocks");
3340 offset = block.
num << (block.
szx + 4);
3346#if COAP_Q_BLOCK_SUPPORT
3350 if (lg_srcv->
total_len < saved_offset + length) {
3351 lg_srcv->
total_len = saved_offset + length;
3359 length, data, saved_offset,
3363 goto skip_app_handler;
3370 (
const uint8_t *)
"Memory issue");
3372 goto skip_app_handler;
3383#if COAP_Q_BLOCK_SUPPORT
3396 goto skip_app_handler;
3400 goto skip_app_handler;
3406 if (!block.
bert && saved_num == 0 && max_block_szx != 0 &&
3407 max_block_szx < block.
aszx) {
3408 block.
aszx = max_block_szx;
3446#if COAP_Q_BLOCK_SUPPORT
3465#if COAP_Q_BLOCK_SUPPORT
3469 goto skip_app_handler;
3506 *pfree_lg_srcv = lg_srcv;
3512 LL_DELETE(session->
lg_srcv, lg_srcv);
3520#if COAP_CLIENT_SUPPORT
3521#if COAP_Q_BLOCK_SUPPORT
3523derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
3524 uint32_t value = **bp & 0x1f;
3528 }
else if (value == 24) {
3530 return (uint32_t)-1;
3534 }
else if (value == 25) {
3536 return (uint32_t)-1;
3544 return (uint32_t)-1;
3547 value |= **bp << 16;
3565 if (sent || lg_xmit || lg_crcv) {
3569 const uint8_t *data;
3579 }
else if (lg_xmit) {
3582 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
3583 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
3586 data_len = (lg_xmit->
data_info->
length - offset) > blk_size ? blk_size :
3587 lg_xmit->data_info->length - offset;
3612 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
3613#if COAP_OSCORE_SUPPORT
3614 if (session->oscore_encryption &&
3675 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3686 coap_log_debug(
"found Block option, block is BERT, block nr. %u (%zu)\n",
3689 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3690 1 << (block.
szx + 4), block.
num);
3696 1 << (block.
szx + 4), 1 << (lg_xmit->
blk_size + 4));
3697 }
else if ((lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3702 block.
num = (uint32_t)(((lg_xmit->
offset + chunk) >> (block.
szx + 4)) - 1);
3704 chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
3706 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3707 1 << (block.
szx + 4), block.
num);
3712 "next block is not aligned on requested block size boundary. "
3713 "(%zu x %u mod %u = %zu != 0)\n",
3715 (1 << (block.
szx + 4)),
3716 (lg_xmit->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3719 track_echo(session, rcvd);
3737 lg_xmit->
offset = (block.
num + 1) * chunk;
3753 new_token = track_fetch_observe(lg_xmit->
sent_pdu, lg_crcv, block.
num + 1,
3756 assert(len <=
sizeof(buf));
3758 memcpy(buf, new_token->
s, len);
3777 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3780 ((pdu->
max_size - token_options) /1024) * 1024;
3792#if COAP_CONSTRAINED_STACK
3794 static uint8_t l_data[1024];
3796 uint8_t l_data[1024];
3800 assert(chunk <= 1024);
3802 block.
num * chunk, l_data, &l_length,
3817#if COAP_Q_BLOCK_SUPPORT
3820 if (coap_send_q_block1(session, block, pdu,
3840 int observe_action = -1;
3843 goto lg_xmit_finished;
3853 goto lg_xmit_finished;
3861 goto lg_xmit_finished;
3863 if (check_freshness(session, rcvd, sent, lg_xmit, NULL))
3865#if COAP_Q_BLOCK_SUPPORT
3874 const uint8_t *data;
3879 uint16_t fmt = fmt_opt ?
3889 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3895 const uint8_t *bp = data;
3901 size_t ltoken_length;
3903 for (i = 0; (bp < data + length) &&
3905 if ((*bp & 0xc0) != 0x00)
3907 block.
num = derive_cbor_value(&bp, data + length - bp);
3909 if (block.
num > (1 << 20) -1)
3911 block.
m = (block.
num + 1) * chunk < lg_xmit->data_info->length;
3941 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
3944 goto lg_xmit_finished;
3981 LL_DELETE(session->
lg_xmit, lg_xmit);
3998 const uint8_t *data,
size_t offset,
size_t total) {
4001 if (body_data == NULL && total) {
4004 if (body_data == NULL)
4008 if (offset + length <= total && body_data->length >= total) {
4009 memcpy(&body_data->
s[offset], data, length);
4025 memcpy(&body_data->
s[offset], data, length);
4034#if COAP_CLIENT_SUPPORT
4054#if COAP_Q_BLOCK_SUPPORT
4058 uint16_t block_opt = 0;
4060 int ack_rst_sent = 0;
4063 memset(&block, 0,
sizeof(block));
4064#if COAP_Q_BLOCK_SUPPORT
4065 memset(&qblock, 0,
sizeof(qblock));
4075 const uint8_t *data;
4078 size_t size2 = size_opt ?
4090#if COAP_Q_BLOCK_SUPPORT
4093 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
4099 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4104 track_echo(session, rcvd);
4105 if (have_block && (block.
m || length)) {
4109 uint16_t fmt = fmt_opt ?
4116 size_t saved_offset;
4129 goto expire_lg_crcv;
4132 chunk = (size_t)1 << (block.
szx + 4);
4133 offset = block.
num * chunk;
4134 if (size2 < (offset + length)) {
4136 size2 = offset + length + 1;
4138 size2 = offset + length;
4140 saved_offset = offset;
4143#if COAP_Q_BLOCK_SUPPORT
4160 lg_crcv->
szx = block.
szx;
4165#if COAP_Q_BLOCK_SUPPORT
4166 lg_crcv->
rec_blocks.processing_payload_set = 0;
4183#if COAP_Q_BLOCK_SUPPORT
4188 coap_log_warn(
"Data body updated during receipt - new request started\n");
4206 (0 << 4) | (0 << 3) | block.
aszx),
4212 goto skip_app_handler;
4224#if COAP_Q_BLOCK_SUPPORT
4230 if (block.
num == 0) {
4243 while (offset < saved_offset + length) {
4245#if COAP_Q_BLOCK_SUPPORT
4249 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
4250 1 << (block.
szx + 4), block.
num);
4251#if COAP_Q_BLOCK_SUPPORT
4253 this_payload_set > lg_crcv->
rec_blocks.processing_payload_set &&
4254 this_payload_set != lg_crcv->
rec_blocks.latest_payload_set) {
4255 coap_request_missing_q_block2(session, lg_crcv);
4257 lg_crcv->
rec_blocks.latest_payload_set = this_payload_set;
4267 offset = block.
num << (block.
szx + 4);
4273 if (updated_block) {
4280 if (size2 < saved_offset + length) {
4281 size2 = saved_offset + length;
4287 data, saved_offset, size2);
4293 saved_offset, size2);
4307#if COAP_Q_BLOCK_SUPPORT
4313 if (check_all_blocks_in_for_payload_set(session,
4319 if (check_any_blocks_next_payload_set(session,
4322 coap_request_missing_q_block2(session, lg_crcv);
4323 goto skip_app_handler;
4328 goto skip_app_handler;
4332 goto skip_app_handler;
4355 ((block.
num + 1) << 4) |
4356 (block.
m << 3) | block.
aszx),
4361 coap_add_data_large_internal(session, NULL, pdu, NULL, NULL, -1, 0, length, data, NULL, NULL, NULL,
4366 goto skip_app_handler;
4369 goto skip_app_handler;
4374#if COAP_Q_BLOCK_SUPPORT
4390 goto call_app_handler;
4392#if COAP_Q_BLOCK_SUPPORT
4403#if COAP_Q_BLOCK_SUPPORT
4412 lg_crcv->
total_len : saved_offset + length;
4420#if COAP_Q_BLOCK_SUPPORT
4447 LL_DELETE(session->
lg_crcv, lg_crcv);
4449 goto skip_app_handler;
4455 goto skip_app_handler;
4474 goto expire_lg_crcv;
4479#if COAP_OSCORE_SUPPORT
4480 if (check_freshness(session, rcvd,
4481 (session->oscore_encryption == 0) ? sent : NULL,
4484 if (check_freshness(session, rcvd, sent, NULL, lg_crcv))
4486 goto skip_app_handler;
4487 goto expire_lg_crcv;
4490 goto expire_lg_crcv;
4511#
if COAP_Q_BLOCK_SUPPORT
4518 goto skip_app_handler;
4522#if COAP_Q_BLOCK_SUPPORT
4523 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
4529 if (block.
num != 0) {
4532 const uint8_t *data;
4533 size_t chunk = (size_t)1 << (block.
szx + 4);
4538 goto call_app_handler;
4541#if COAP_Q_BLOCK_SUPPORT
4545 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
4554 LL_PREPEND(session->
lg_crcv, lg_crcv);
4559 track_echo(session, rcvd);
4564 LL_PREPEND(session->
lg_crcv, lg_crcv);
4588 LL_DELETE(session->
lg_crcv, lg_crcv);
4601#if COAP_SERVER_SUPPORT
4610 if (response->
code == 0)
4620#if COAP_CLIENT_SUPPORT
4623 uint64_t token_match =
4630 LL_FOREACH(session->
lg_crcv, lg_crcv) {
4643 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)
@ 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...
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.
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)
int coap_add_data_large_request_app_lkd(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.
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)
coap_lg_crcv_t * coap_find_lg_crcv(coap_session_t *session, coap_pdu_t *pdu)
Find the current lg_crcv for the session that matches the pdu.
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)
#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)
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.
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.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
#define COAP_BLOCK_USE_M_Q_BLOCK
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
#define COAP_BLOCK_STLESS_BLOCK2
COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
#define COAP_BLOCK_TRY_Q_BLOCK
#define COAP_BLOCK_STLESS_FETCH
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data, coap_block_b_t *block)
Adds the appropriate payload data of the body to the pdu.
#define COAP_BLOCK_SINGLE_BODY
int coap_write_block_b_opt(coap_session_t *session, coap_block_b_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
int coap_add_block(coap_pdu_t *pdu, size_t len, const uint8_t *data, unsigned int block_num, unsigned char block_szx)
Adds the block_num block of size 1 << (block_szx + 4) from source data to pdu.
void(* coap_release_large_data_t)(coap_session_t *session, void *app_ptr)
Callback handler for de-allocating the data based on app_ptr provided to coap_add_data_large_*() func...
void coap_add_data_blocked_response(const coap_pdu_t *request, coap_pdu_t *response, uint16_t media_type, int maxage, size_t length, const uint8_t *data)
Adds the appropriate part of data to the response pdu.
int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu, coap_option_num_t number, coap_block_b_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
unsigned int coap_opt_block_num(const coap_opt_t *block_opt)
Returns the value of field num in the given block option block_opt.
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_BLOCK_NOT_RANDOM_BLOCK1
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
int coap_write_block_opt(coap_block_t *block, coap_option_num_t number, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type number to message pdu.
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
#define COAP_BLOCK_FORCE_Q_BLOCK
coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_USE_LIBCOAP
void coap_digest_free(coap_digest_ctx_t *digest_ctx)
Free off coap_digest_ctx_t.
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.
#define COAP_MAX_DELAY_TICKS
#define COAP_RESOURCE_USE_BLOCK_DATA_HANDLER
Call the block data handler for this resource if one is registered for the context.
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_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_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_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_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_handler
Called with each block data during block transfers.
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)
coap_lg_xmit_data_t * obs_data
lg_xmit data info if large observe request
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.
coap_rblock_t rec_blocks
list of received blocks
coap_address_t upstream
Unicast IP of server if mcast client session.
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.
uint8_t o_blk_size
Block size used when initiating Observe.
coap_tick_t last_used
Last time all data sent or 0.
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
coap_pdu_t * sent_pdu
The sent pdu with all the data.
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 dont_timeout
Set if app handler in use.
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
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 resource that can be attached to coap_context_t.
unsigned int is_proxy_uri
resource created for proxy URI handler
unsigned int is_reverse_proxy
resource created for reverse proxy URI handler
int flags
zero or more COAP_RESOURCE_FLAGS_* or'd together
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_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_socket_flags_t flags
1 or more of COAP_SOCKET* flag values
CoAP string data definition.
size_t length
length of string