19#define min(a,b) ((a) < (b) ? (a) : (b))
22#define STATE_TOKEN_BASE(t) ((t) & 0xffffffffffffULL)
23#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> 48)
24#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << 48))
26#if COAP_Q_BLOCK_SUPPORT
72 if (block->
szx == 7) {
84 if (block->
m && (length % 1024) != 0) {
85 coap_log_debug(
"block: Oversized packet - reduced to %zu from %zu\n",
86 length - (length % 1024), length);
87 length -= length % 1024;
120 block->
m = block_b.
m;
130 unsigned int blk_size,
size_t total) {
132 size_t avail = pdu->
max_size - token_options;
133 unsigned int start = num << (blk_size + 4);
134 unsigned int can_use_bert = block->
defined == 0 || block->
bert;
136 assert(start <= total);
137 memset(block, 0,
sizeof(*block));
139 block->
szx = block->
aszx = blk_size;
140 if (can_use_bert && blk_size == 6 && avail >= 1024 && session != NULL &&
145 block->
chunk_size = (uint32_t)((avail / 1024) * 1024);
147 block->
chunk_size = (size_t)1 << (blk_size + 4);
148 if (avail < block->chunk_size && (total - start) >= avail) {
154 coap_log_debug(
"not enough space, even the smallest block does not fit (1)\n");
157 new_blk_size =
coap_flsll((
long long)avail) - 5;
158 coap_log_debug(
"decrease block size for %zu to %d\n", avail, new_blk_size);
160 block->
szx = new_blk_size;
161 block->
num <<= szx - block->
szx;
162 block->
chunk_size = (size_t)1 << (new_blk_size + 4);
173 unsigned char buf[4];
178 start = block->
num << (block->
szx + 4);
179 if (block->
num != 0 && data_length <= start) {
189 block->
szx, data_length))
194 ((block_b.
num << 4) |
207 unsigned char buf[4];
211 start = block->
num << (block->
szx + 4);
212 if (block->
num != 0 && data_length <= start) {
220 block->
szx, data_length))
235 unsigned int block_num,
unsigned char block_szx) {
237 start = block_num << (block_szx + 4);
243 min(len - start, ((
size_t)1 << (block_szx + 4))),
250 unsigned int start = block->
num << (block->
szx + 4);
258 max_size = ((pdu->
max_size - token_options) / 1024) * 1024;
260 max_size = (size_t)1 << (block->
szx + 4);
265 min(len - start, max_size),
281 unsigned char buf[4];
283 int block2_requested = 0;
285 memset(&block2, 0,
sizeof(block2));
292 block2_requested = 1;
293 if (block2.
num != 0 && length <= (block2.
num << (block2.
szx + 4))) {
296 length >> (block2.
szx + 4));
305 memset(etag, 0,
sizeof(etag));
320 if (block2_requested) {
380 uint8_t block_mode) {
383#if COAP_Q_BLOCK_SUPPORT
390#
if ! COAP_Q_BLOCK_SUPPORT
398 const uint8_t *b,
size_t blen) {
399 return alen == blen && (alen == 0 || memcmp(a, b, alen) == 0);
402#if COAP_CLIENT_SUPPORT
414 coap_log_debug(
"** %s: coap_cancel_observe: COAP_BLOCK_USE_LIBCOAP not enabled\n",
419 LL_FOREACH_SAFE(session->
lg_crcv, lg_crcv, q) {
427#if COAP_Q_BLOCK_SUPPORT
446#if COAP_Q_BLOCK_SUPPORT
473#if COAP_Q_BLOCK_SUPPORT
475 if (using_q_block1) {
476 mid = coap_send_q_block1(session, block, pdu, COAP_SEND_INC_PDU);
492#if COAP_OSCORE_SUPPORT
498 uint64_t token_match =
509 LL_FOREACH(session->
lg_crcv, lg_crcv) {
548#if COAP_SERVER_SUPPORT
565 LL_FOREACH(session->
lg_xmit, lg_xmit) {
582 memcmp(lg_xmit->
b.
b2.
rtag, rtag, rtag_length) != 0)
607#if COAP_Q_BLOCK_SUPPORT
613 int have_block_defined = 0;
616 size_t token_options;
619#if COAP_Q_BLOCK_SUPPORT
625 coap_log_warn(
"coap_add_data_large: PDU already contains data\n");
627 release_func(session, app_ptr);
632 coap_log_debug(
"** %s: coap_add_data_large: COAP_BLOCK_USE_LIBCOAP not enabled\n",
646#define MAX_BLK_LEN (((1 << 20) - 1) * (1 << (6 + 4)))
657#if COAP_Q_BLOCK_SUPPORT
673 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
676 LL_DELETE(session->
lg_xmit, lg_xmit);
686#if COAP_Q_BLOCK_SUPPORT
687 if (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) {
700#if COAP_SERVER_SUPPORT
708 LL_DELETE(session->
lg_xmit, lg_xmit);
715#if COAP_OSCORE_SUPPORT
716 if (session->oscore_encryption) {
724 avail = pdu->
max_size - token_options;
727#if COAP_OSCORE_SUPPORT
732 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
738 if (block.
szx < blk_size)
739 blk_size = block.
szx;
740 have_block_defined = 1;
742#if COAP_Q_BLOCK_SUPPORT
745 if (have_block_defined) {
747 coap_log_warn(
"Both BlockX and Q-BlockX cannot be set at the same time\n");
751 if (block.
szx < blk_size)
752 blk_size = block.
szx;
753 have_block_defined = 1;
759 if (avail < 16 && ((ssize_t)length > avail || have_block_defined)) {
761 coap_log_debug(
"not enough space, even the smallest block does not fit (2)\n");
765 chunk = (size_t)1 << (blk_size + 4);
766 if (have_block_defined &&
767 (block.
num != 0 || single_request)) {
771 if (length >= block.
num * chunk) {
773 if (chunk > length - block.
num * chunk)
774 rem = length - block.
num * chunk;
779 release_func(session, app_ptr);
780 }
else if ((have_block_defined && length > chunk) || (ssize_t)length > avail) {
800 lg_xmit->blk_size = blk_size;
801 lg_xmit->option = option;
802 lg_xmit->data = data;
803 lg_xmit->length = length;
804#if COAP_Q_BLOCK_SUPPORT
805 lg_xmit->non_timeout_random_ticks =
808 lg_xmit->release_func = release_func;
809 lg_xmit->app_ptr = app_ptr;
816 if (!lg_xmit->b.b1.app_token)
824 lg_xmit->b.b1.count = 1;
826 lg_xmit->b.b1.count);
834 (
unsigned int)length),
847 lg_xmit->b.b2.resource = resource;
850 if (lg_xmit->b.b2.query) {
851 memcpy(lg_xmit->b.b2.query->s, query->
s, query->
length);
854 lg_xmit->b.b2.query = NULL;
859 sizeof(lg_xmit->b.b2.rtag));
861 lg_xmit->b.b2.rtag_set = 1;
863 lg_xmit->b.b2.rtag_set = 0;
865 lg_xmit->b.b2.etag = etag;
866 lg_xmit->b.b2.request_method = request_method;
873 lg_xmit->b.b2.maxage_expire = 0;
878 (
unsigned int)length),
892 blk_size, lg_xmit->length))
899 (block.
num << 4) | (block.
m << 3) | block.
aszx),
903 memcpy(&lg_xmit->pdu, pdu,
sizeof(lg_xmit->pdu));
905 lg_xmit->pdu.used_size + lg_xmit->pdu.max_hdr_size);
906 if (!lg_xmit->pdu.token)
909 lg_xmit->pdu.alloc_size = lg_xmit->pdu.used_size;
910 lg_xmit->pdu.token += lg_xmit->pdu.max_hdr_size;
911 memcpy(lg_xmit->pdu.token, pdu->
token, lg_xmit->pdu.used_size);
913 lg_xmit->pdu.data = lg_xmit->pdu.token + (pdu->
data - pdu->
token);
914 lg_xmit->pdu.actual_token.s = lg_xmit->pdu.token + pdu->
e_token_length -
920 avail = pdu->
max_size - token_options;
925#
if COAP_OSCORE_SUPPORT
928 if (avail < (ssize_t)chunk) {
931 coap_log_warn(
"not enough space, even the smallest block does not fit (3)\n");
934 blk_size =
coap_flsll((
long long)avail) - 4 - 1;
935 block.
num = block.
num << (lg_xmit->blk_size - blk_size);
936 lg_xmit->blk_size = blk_size;
937 chunk = (size_t)1 << (lg_xmit->blk_size + 4);
943 (block.
num << 4) | (block.
m << 3) | lg_xmit->blk_size),
948 if (rem > lg_xmit->length - block.
num * chunk)
949 rem = lg_xmit->length - block.
num * chunk;
954 lg_xmit->b.b1.bert_size = rem;
956 lg_xmit->last_block = -1;
959 LL_PREPEND(session->
lg_xmit,lg_xmit);
968 if (have_block_defined) {
972 (0 << 4) | (0 << 3) | blk_size), buf);
979 release_func(session, app_ptr);
986 }
else if (release_func) {
987 release_func(session, app_ptr);
992#if COAP_CLIENT_SUPPORT
1006 release_func(session, app_ptr);
1010 length, data, release_func, app_ptr, 0, 0);
1014#if COAP_SERVER_SUPPORT
1021 uint16_t media_type,
1025 const uint8_t *data,
1029 unsigned char buf[4];
1031 int block_requested = 0;
1032 int single_request = 0;
1033#if COAP_Q_BLOCK_SUPPORT
1034 uint16_t block_opt = (session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK) ?
1040 memset(&block, 0,
sizeof(block));
1047 block_requested = 1;
1048 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1051 length >> (block.
szx + 4));
1056#if COAP_Q_BLOCK_SUPPORT
1058 block_requested = 1;
1059 if (block.
num != 0 && length <= (block.
num << (block.
szx + 4))) {
1062 length >> (block.
szx + 4));
1066 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
1087 if (block_requested) {
1111 query, maxage, etag, length, data,
1112 release_func, app_ptr, single_request,
1115 goto error_released;
1122 release_func(session, app_ptr);
1124#if COAP_ERROR_PHRASE_LENGTH > 0
1142#if COAP_Q_BLOCK_SUPPORT
1152 LL_FOREACH_SAFE(session->
lg_xmit, p, q) {
1156 LL_DELETE(session->
lg_xmit, p);
1166 if (p->
last_sent + partial_timeout <= now) {
1168 LL_DELETE(session->
lg_xmit, p);
1173 if (*tim_rem > p->
last_sent + partial_timeout - now) {
1174 *tim_rem = p->
last_sent + partial_timeout - now;
1183#if COAP_CLIENT_SUPPORT
1184#if COAP_Q_BLOCK_SUPPORT
1212 int block_payload_set = -1;
1228 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1229 sofar = block * block_size;
1230 if (sofar < lg_crcv->total_len) {
1233 pdu = coap_build_missing_pdu(session, lg_crcv);
1239 (block << 4) | (1 << 3) | lg_crcv->
szx),
1252 pdu = coap_build_missing_pdu(session, lg_crcv);
1257 if (block_payload_set == -1)
1263 (block << 4) | (0 << 3) | lg_crcv->
szx),
1271 block_size = (size_t)1 << (lg_crcv->
szx + 4);
1272 sofar = (block + 1) * block_size;
1273 if (sofar < lg_crcv->total_len) {
1276 pdu = coap_build_missing_pdu(session, lg_crcv);
1280 sofar = (lg_crcv->
total_len + block_size - 1)/block_size;
1282 if (block_payload_set == -1)
1284 for (; block < (ssize_t)sofar &&
1288 (block << 4) | (0 << 3) | lg_crcv->
szx),
1296 if (block_payload_set != -1)
1297 lg_crcv->
rec_blocks.processing_payload_set = block_payload_set;
1312#if COAP_Q_BLOCK_SUPPORT
1318#if COAP_Q_BLOCK_SUPPORT
1325 LL_FOREACH_SAFE(session->
lg_crcv, p, q) {
1329#if COAP_Q_BLOCK_SUPPORT
1331 size_t scaled_timeout = receive_timeout *
1343 coap_request_missing_q_block2(session, p);
1355 p->
last_used + partial_timeout <= now) {
1356#if COAP_Q_BLOCK_SUPPORT
1360 LL_DELETE(session->
lg_crcv, p);
1364 if (*tim_rem > p->
last_used + partial_timeout - now) {
1365 *tim_rem = p->
last_used + partial_timeout - now;
1374#if COAP_SERVER_SUPPORT
1375#if COAP_Q_BLOCK_SUPPORT
1403 assert(block >= 0 && block < (1 << 20));
1405 if (block < 0 || block >= (1 << 20)) {
1407 }
else if (block < 24) {
1410 }
else if (block < 0x100) {
1414 }
else if (block < 0x10000) {
1417 val[1] = block >> 8;
1418 val[2] = block & 0xff;
1422 val[1] = block >> 16;
1423 val[2] = (block >> 8) & 0xff;
1424 val[3] = block & 0xff;
1440 for (i = 0; i < rec_blocks->
used; i++) {
1441 if (block_num < rec_blocks->range[i].begin)
1443 if (block_num <= rec_blocks->range[i].end)
1454 for (i = 0; i < rec_blocks->
used; i++) {
1455 if (block < rec_blocks->range[i].begin)
1457 if (block < rec_blocks->range[i].end)
1461 if (block + 1 < total_blocks)
1467#if COAP_CLIENT_SUPPORT
1468#if COAP_Q_BLOCK_SUPPORT
1472 if (rec_blocks->
used &&
1474 rec_blocks->processing_payload_set)
1482 if (rec_blocks->
used > 1 &&
1484 rec_blocks->processing_payload_set)
1491#if COAP_SERVER_SUPPORT
1502#if COAP_Q_BLOCK_SUPPORT
1508#if COAP_Q_BLOCK_SUPPORT
1515 LL_FOREACH_SAFE(session->
lg_srcv, p, q) {
1519#if COAP_Q_BLOCK_SUPPORT
1521 size_t scaled_timeout = receive_timeout *
1531 size_t block_size = (size_t)1 << (p->
szx + 4);
1532 size_t final_block = (p->
total_len + block_size - 1)/block_size - 1;
1534 size_t last_payload_block;
1536 size_t no_blocks = 0;
1549 if (no_blocks == 0 && block == (
int)final_block)
1555 if (final_block > last_payload_block) {
1556 final_block = last_payload_block;
1558 no_blocks += final_block - block;
1559 if (no_blocks == 0) {
1561 final_block = (p->
total_len + block_size - 1)/block_size - 1;
1563 if (final_block > last_payload_block) {
1564 final_block = last_payload_block;
1566 no_blocks += final_block - block;
1575 pdu = pdu_408_build(session, p);
1581 if (!add_408_block(pdu, block)) {
1591 for (; block <= (int)final_block; block++) {
1593 pdu = pdu_408_build(session, p);
1597 if (!add_408_block(pdu, block)) {
1615#if COAP_Q_BLOCK_SUPPORT
1619 LL_DELETE(session->
lg_srcv, p);
1623 if (*tim_rem > p->
last_used + partial_timeout - now) {
1624 *tim_rem = p->
last_used + partial_timeout - now;
1633#if COAP_Q_BLOCK_SUPPORT
1642 coap_send_pdu_t send_pdu) {
1648 const uint8_t *ptoken;
1650 size_t ltoken_length;
1651 uint32_t delayqueue_cnt = 0;
1654 if (send_pdu == COAP_SEND_INC_PDU)
1663 (send_pdu == COAP_SEND_INC_PDU ? 1 : 0);
1664 LL_FOREACH(session->
delayqueue, delayqueue) {
1692 ptoken, &drop_options);
1698 if (send_pdu == COAP_SEND_INC_PDU &&
1708 size_t chunk = ((size_t)1 << (lg_xmit->
blk_size + 4));
1734 ltoken_length, ptoken, &drop_options);
1739 ((block.
num) << 4) |
1778#if COAP_CLIENT_SUPPORT
1786 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1787 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1789 if (now < non_timeout)
1790 return non_timeout - now;
1791 timed_out = now - non_timeout;
1797 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
1799 memset(&block, 0,
sizeof(block));
1800 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
1803 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->
pdu, COAP_SEND_SKIP_PDU);
1804 if (tim_rem > non_timeout)
1805 tim_rem = non_timeout;
1815 LL_DELETE(session->
lg_xmit, lg_xmit);
1819 if (tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now)
1828#if COAP_SERVER_SUPPORT
1836 LL_FOREACH_SAFE(session->
lg_xmit, lg_xmit, q) {
1837 coap_tick_t non_timeout = lg_xmit->non_timeout_random_ticks;
1839 if (now < non_timeout)
1840 return non_timeout - now;
1841 timed_out = now - non_timeout;
1847 size_t chunk = (size_t)1 << (lg_xmit->
blk_size + 4);
1849 memset(&block, 0,
sizeof(block));
1850 block.
num = (uint32_t)(lg_xmit->
offset / chunk);
1854 coap_send_q_blocks(session, lg_xmit, block, &lg_xmit->
pdu, COAP_SEND_SKIP_PDU);
1855 if (tim_rem > non_timeout)
1856 tim_rem = non_timeout;
1866 LL_DELETE(session->
lg_xmit, lg_xmit);
1870 if (tim_rem > lg_xmit->
last_all_sent + 4 * non_timeout - now)
1880#if COAP_CLIENT_SUPPORT
1894 if (opt && lg_crcv) {
1895 int observe_action = -1;
1903 (block_num + 1) *
sizeof(lg_crcv->
obs_token[0]));
1913 if (lg_crcv->
obs_token[block_num] == NULL)
1917 if (block_num < lg_crcv->obs_token_cnt) {
1927#if COAP_Q_BLOCK_SUPPORT
1932 coap_send_pdu_t send_request) {
1935 uint64_t token_match =
1939 LL_FOREACH(session->
lg_xmit, lg_xmit) {
1948 return coap_send_q_blocks(session, lg_xmit, block, request, send_request);
1953#if COAP_SERVER_SUPPORT
1954#if COAP_Q_BLOCK_SUPPORT
1965 coap_send_pdu_t send_response) {
1970 LL_FOREACH(session->
lg_xmit, lg_xmit) {
1978 return coap_send_q_blocks(session, lg_xmit, block, response, send_response);
1983#if COAP_CLIENT_SUPPORT
1984#if COAP_Q_BLOCK_SUPPORT
2017 (0 << 4) | (0 << 3) | 0),
2033 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
2035 size_t data_len = lg_xmit ? lg_xmit->
length :
2041 if (lg_crcv == NULL)
2044 coap_log_debug(
"** %s: lg_crcv %p initialized - stateless token xxxx%012llx\n",
2048 lg_crcv->initial = 1;
2051 memcpy(&lg_crcv->pdu, pdu,
sizeof(lg_crcv->pdu));
2053 lg_crcv->pdu.max_size = token_options + data_len + 9;
2054 lg_crcv->pdu.used_size = token_options + data_len;
2056 token_options + data_len + lg_crcv->pdu.max_hdr_size);
2057 if (!lg_crcv->pdu.token) {
2061 lg_crcv->pdu.token += lg_crcv->pdu.max_hdr_size;
2062 memcpy(lg_crcv->pdu.token, pdu->
token, token_options);
2063 if (lg_crcv->pdu.data) {
2064 lg_crcv->pdu.data = lg_crcv->pdu.token + token_options;
2065 memcpy(lg_crcv->pdu.data, lg_xmit ? lg_xmit->
data : pdu->
data, data_len);
2070 if (!lg_crcv->app_token) {
2077 lg_crcv->retry_counter = 1;
2078 lg_crcv->state_token = state_token;
2084 new_token = track_fetch_observe(pdu, lg_crcv, 0, &pdu->
actual_token);
2100#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2103 if (lg_crcv == NULL)
2120#if COAP_SERVER_SUPPORT
2124#if (COAP_MAX_LOGGING_LEVEL < _COAP_LOG_DEBUG)
2127 if (lg_srcv == NULL)
2131#if COAP_Q_BLOCK_SUPPORT
2144 if (lg_xmit == NULL)
2163#if COAP_SERVER_SUPPORT
2170add_block_send(uint32_t num,
int is_continue, send_track *out_blocks,
2171 uint32_t *count, uint32_t max_count) {
2174 for (i = 0; i < *count && *count < max_count; i++) {
2175 if (num == out_blocks[i].num)
2177 else if (num < out_blocks[i].num) {
2179 memmove(&out_blocks[i], &out_blocks[i+1], *count - i -1);
2180 out_blocks[i].num = num;
2181 out_blocks[i].is_continue = is_continue;
2186 if (*count < max_count) {
2187 out_blocks[i].num = num;
2188 out_blocks[i].is_continue = is_continue;
2221 uint16_t block_opt = 0;
2222 send_track *out_blocks = NULL;
2223 const char *error_phrase;
2228 uint32_t request_cnt, i;
2231#if COAP_Q_BLOCK_SUPPORT
2236 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK))
2243 coap_log_warn(
"Block2 and Q-Block2 cannot be in the same request\n");
2244 coap_add_data(response,
sizeof(
"Both Block2 and Q-Block2 invalid")-1,
2245 (
const uint8_t *)
"Both Block2 and Q-Block2 invalid");
2247 goto skip_app_handler;
2254 if (block.
num == 0) {
2262#if COAP_Q_BLOCK_SUPPORT
2268 goto internal_issue;
2283 goto skip_app_handler;
2290 chunk = (size_t)1 << (p->
blk_size + 4);
2293 coap_log_debug(
"found Block option, block is BERT, block nr. %u, M %d\n",
2294 block.
num, block.
m);
2296 coap_log_debug(
"found Block option, block size is %u, block nr. %u, M %d\n",
2297 1 << (block.
szx + 4), block.
num, block.
m);
2300 if ((p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
2305 block.
num = (uint32_t)(((p->
offset + chunk) >> (block.
szx + 4)) - 1);
2307 chunk = (size_t)1 << (p->
blk_size + 4);
2309 coap_log_debug(
"new Block size is %u, block number %u completed\n",
2310 1 << (block.
szx + 4), block.
num);
2313 "next block is not aligned on requested block size "
2314 "boundary. (%zu x %u mod %u = %zu (which is not 0)\n",
2316 (1 << (block.
szx + 4)),
2317 (p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
2328#if COAP_Q_BLOCK_SUPPORT
2329 max_block = (p->
length + chunk - 1)/chunk;
2341 sizeof(
"Changing blocksize during request invalid")-1,
2342 (
const uint8_t *)
"Changing blocksize during request invalid");
2344 goto skip_app_handler;
2346#if COAP_Q_BLOCK_SUPPORT
2351 goto call_app_handler;
2355 num + i < max_block; i++) {
2356 add_block_send(num + i, 1, out_blocks, &request_cnt,
2364 num + i < max_block; i++) {
2365 add_block_send(num + i, 0, out_blocks, &request_cnt,
2370 add_block_send(num, 0, out_blocks, &request_cnt,
2373 add_block_send(num, 0, out_blocks, &request_cnt, 1);
2377 if (request_cnt == 0) {
2381 out_blocks[0].num = 0;
2382 out_blocks[0].is_continue = 0;
2386 for (i = 0; i < request_cnt; i++) {
2389 block.
num = out_blocks[i].num;
2392 if (i + 1 < request_cnt) {
2399 if (out_blocks[i].is_continue) {
2407 goto internal_issue;
2410 if (out_blocks[i].is_continue)
2425 goto internal_issue;
2435 ((out_pdu->
max_size - token_options) /1024) * 1024;
2446 goto internal_issue;
2448 if (!(p->
offset + chunk < p->length)) {
2456 if (!(p->
offset + chunk < p->length)) {
2474 goto internal_issue;
2482 goto internal_issue;
2484 if (i + 1 < request_cnt) {
2490 goto skip_app_handler;
2491#if COAP_Q_BLOCK_SUPPORT
2501 (
const uint8_t *)error_phrase);
2517 rec_blocks->
retry = 0;
2519 for (i = 0; i < rec_blocks->
used; i++) {
2520 if (block_num >= rec_blocks->
range[i].
begin &&
2521 block_num <= rec_blocks->range[i].end)
2524 if (block_num < rec_blocks->range[i].begin) {
2525 if (block_num + 1 == rec_blocks->
range[i].
begin) {
2532 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i],
2533 (rec_blocks->
used - i) *
sizeof(rec_blocks->
range[0]));
2539 if (block_num == rec_blocks->
range[i].
end + 1) {
2540 rec_blocks->
range[i].
end = block_num;
2541 if (i + 1 < rec_blocks->
used) {
2542 if (rec_blocks->
range[i+1].
begin == block_num + 1) {
2545 if (i+2 < rec_blocks->
used) {
2546 memmove(&rec_blocks->
range[i+1], &rec_blocks->
range[i+2],
2547 (rec_blocks->
used - (i+2)) *
sizeof(rec_blocks->
range[0]));
2555 if (i == rec_blocks->
used) {
2566#if COAP_SERVER_SUPPORT
2586 const uint8_t *data = NULL;
2591 uint16_t block_option = 0;
2594 *pfree_lg_srcv = NULL;
2601#if COAP_Q_BLOCK_SUPPORT
2604 coap_add_data(response,
sizeof(
"Block1 + Q-Block1 together")-1,
2605 (
const uint8_t *)
"Block1 + Q-Block1 together");
2607 goto skip_app_handler;
2611#if COAP_Q_BLOCK_SUPPORT
2632 const uint8_t *rtag = rtag_opt ?
coap_opt_value(rtag_opt) : NULL;
2641 offset = block.
num << (block.
szx + 4);
2643 LL_FOREACH(session->
lg_srcv, p) {
2644 if (rtag_opt || p->
rtag_set == 1) {
2645 if (!(rtag_opt && p->
rtag_set == 1))
2648 memcmp(p->
rtag, rtag, rtag_length) != 0)
2659 if (!p && block.
num != 0) {
2664 pdu->
body_total = length + offset + (block.
m ? 1 : 0);
2667 else if (!p && !(offset == 0 && block.
m == 0)) {
2671 (
const uint8_t *)
"Memory issue");
2673 goto skip_app_handler;
2699 LL_PREPEND(session->
lg_srcv, p);
2703 coap_add_data(response,
sizeof(
"Content-Format mismatch")-1,
2704 (
const uint8_t *)
"Content-Format mismatch");
2708#if COAP_Q_BLOCK_SUPPORT
2712 (
const uint8_t *)
"Size1 mismatch");
2720#if COAP_Q_BLOCK_SUPPORT
2726#
if COAP_Q_BLOCK_SUPPORT
2732 size_t chunk = (size_t)1 << (block.
szx + 4);
2733 int update_data = 0;
2734 unsigned int saved_num = block.
num;
2735 size_t saved_offset = offset;
2737 while (offset < saved_offset + length) {
2742 coap_add_data(response,
sizeof(
"Too many missing blocks")-1,
2743 (
const uint8_t *)
"Too many missing blocks");
2750 offset = block.
num << (block.
szx + 4);
2754#if COAP_Q_BLOCK_SUPPORT
2759 if (p->
total_len < saved_offset + length) {
2765 goto call_app_handler;
2770 (uint32_t)(p->
total_len + chunk -1)/chunk)) {
2775#if COAP_Q_BLOCK_SUPPORT
2778 (uint32_t)(p->
total_len + chunk -1)/chunk)) {
2789 goto skip_app_handler;
2793 goto skip_app_handler;
2804 goto skip_app_handler;
2806 goto skip_app_handler;
2814#if COAP_Q_BLOCK_SUPPORT
2829 goto call_app_handler;
2837 if (total > offset + length + block.
m)
2849 goto call_app_handler;
2859 goto call_app_handler;
2862 LL_DELETE(session->
lg_srcv, p);
2864 goto skip_app_handler;
2875#if COAP_CLIENT_SUPPORT
2876#if COAP_Q_BLOCK_SUPPORT
2878derive_cbor_value(
const uint8_t **bp,
size_t rem_len) {
2879 uint32_t value = **bp & 0x1f;
2883 }
else if (value == 24) {
2885 return (uint32_t)-1;
2889 }
else if (value == 25) {
2891 return (uint32_t)-1;
2899 return (uint32_t)-1;
2920 if (sent || lg_xmit || lg_crcv) {
2924 const uint8_t *data;
2934 }
else if (lg_xmit) {
2935 sent = &lg_xmit->
pdu;
2937 size_t blk_size = (size_t)1 << (lg_xmit->
blk_size + 4);
2938 size_t offset = (lg_xmit->
last_block + 1) * blk_size;
2940 data = &lg_xmit->
data[offset];
2941 data_len = (lg_xmit->
length - offset) > blk_size ? blk_size :
2942 lg_xmit->
length - offset;
2945 sent = &lg_crcv->
pdu;
2967 track_fetch_observe(echo_pdu, lg_crcv, 0, &echo_pdu->
actual_token);
2968#if COAP_OSCORE_SUPPORT
2969 if (session->oscore_encryption &&
3026 uint64_t token_match =
3031 LL_FOREACH_SAFE(session->
lg_xmit, p, q) {
3041 size_t chunk = (size_t)1 << (p->
blk_size + 4);
3048 coap_log_debug(
"found Block option, block is BERT, block nr. %u (%zu)\n",
3051 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3052 1 << (block.
szx + 4), block.
num);
3055 if ((p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)) == 0) {
3060 block.
num = (uint32_t)(((p->
offset + chunk) >> (block.
szx + 4)) - 1);
3062 chunk = (size_t)1 << (p->
blk_size + 4);
3064 coap_log_debug(
"new Block size is %u, block number %u completed\n",
3065 1 << (block.
szx + 4), block.
num);
3070 "next block is not aligned on requested block size boundary. "
3071 "(%zu x %u mod %u = %zu != 0)\n",
3073 (1 << (block.
szx + 4)),
3074 (p->
offset + chunk) % ((
size_t)1 << (block.
szx + 4)));
3077 track_echo(session, rcvd);
3105 LL_FOREACH(session->
lg_crcv, lg_crcv) {
3111 new_token = track_fetch_observe(&p->
pdu, lg_crcv, block.
num + 1,
3114 assert(len <=
sizeof(buf));
3116 memcpy(buf, new_token->
s, len);
3128 size_t token_options = pdu->
data ? (size_t)(pdu->
data - pdu->
token) :
3131 ((pdu->
max_size - token_options) /1024) * 1024;
3149#if COAP_Q_BLOCK_SUPPORT
3152 if (coap_send_q_block1(session, block, pdu,
3165 if (check_freshness(session, rcvd, sent, p, NULL))
3167#if COAP_Q_BLOCK_SUPPORT
3176 const uint8_t *data;
3181 uint16_t fmt = fmt_opt ?
3191 coap_log_debug(
"Unexpected 4.08 - protocol violation - ignore\n");
3197 const uint8_t *bp = data;
3204 size_t ltoken_length;
3206 for (i = 0; (bp < data + length) &&
3208 if ((*bp & 0xc0) != 0x00)
3210 block.
num = derive_cbor_value(&bp, data + length - bp);
3212 if (block.
num > (1 << 20) -1)
3214 block.
m = (block.
num + 1) * chunk < p->length;
3244 coap_log_info(
"Invalid application/missing-blocks+cbor-seq\n");
3247 goto lg_xmit_finished;
3257 LL_FOREACH(session->
lg_crcv, lg_crcv) {
3275 LL_DELETE(session->
lg_xmit, p);
3286 const uint8_t *data,
size_t offset,
size_t total) {
3289 if (body_data == NULL && total) {
3292 if (body_data == NULL)
3296 if (offset + length <= total && body_data->length >= total) {
3297 memcpy(&body_data->
s[offset], data, length);
3313 memcpy(&body_data->
s[offset], data, length);
3322#if COAP_CLIENT_SUPPORT
3342#if COAP_Q_BLOCK_SUPPORT
3346 uint16_t block_opt = 0;
3348 int ack_rst_sent = 0;
3349 uint64_t token_match =
3353 memset(&block, 0,
sizeof(block));
3354#if COAP_Q_BLOCK_SUPPORT
3355 memset(&qblock, 0,
sizeof(qblock));
3357 LL_FOREACH(session->
lg_crcv, p) {
3372 const uint8_t *data;
3375 size_t size2 = size_opt ?
3387#if COAP_Q_BLOCK_SUPPORT
3390 coap_log_warn(
"Both Block1 and Q-Block1 not supported in a response\n");
3396 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3401 track_echo(session, rcvd);
3402 if (have_block && (block.
m || length)) {
3406 uint16_t fmt = fmt_opt ?
3413 size_t saved_offset;
3422 chunk = (size_t)1 << (block.
szx + 4);
3423 offset = block.
num * chunk;
3424 if (size2 < (offset + length)) {
3426 size2 = offset + length + 1;
3428 size2 = offset + length;
3430 saved_offset = offset;
3433#if COAP_Q_BLOCK_SUPPORT
3454#if COAP_Q_BLOCK_SUPPORT
3472#if COAP_Q_BLOCK_SUPPORT
3477 coap_log_warn(
"Data body updated during receipt - new request started\n");
3494 (0 << 4) | (0 << 3) | block.
aszx),
3500 goto skip_app_handler;
3512#if COAP_Q_BLOCK_SUPPORT
3518 if (block.
num == 0) {
3531 while (offset < saved_offset + length) {
3533#if COAP_Q_BLOCK_SUPPORT
3537 coap_log_debug(
"found Block option, block size is %u, block nr. %u\n",
3538 1 << (block.
szx + 4), block.
num);
3539#if COAP_Q_BLOCK_SUPPORT
3541 this_payload_set > p->
rec_blocks.processing_payload_set &&
3542 this_payload_set != p->
rec_blocks.latest_payload_set) {
3543 coap_request_missing_q_block2(session, p);
3545 p->
rec_blocks.latest_payload_set = this_payload_set;
3555 offset = block.
num << (block.
szx + 4);
3561 if (updated_block) {
3563 if (size2 < saved_offset + length) {
3564 size2 = saved_offset + length;
3567 saved_offset, size2);
3573 (size2 + chunk -1) / chunk)) {
3580#if COAP_Q_BLOCK_SUPPORT
3584 (size2 + chunk -1) / chunk)) {
3587 if (check_all_blocks_in_for_payload_set(session,
3593 if (check_any_blocks_next_payload_set(session,
3596 coap_request_missing_q_block2(session, p);
3597 goto skip_app_handler;
3602 goto skip_app_handler;
3606 goto skip_app_handler;
3627 ((block.
num + 1) << 4) |
3628 (block.
m << 3) | block.
aszx),
3635 goto skip_app_handler;
3640#if COAP_Q_BLOCK_SUPPORT
3648 goto call_app_handler;
3650#if COAP_Q_BLOCK_SUPPORT
3661#if COAP_Q_BLOCK_SUPPORT
3671#if COAP_Q_BLOCK_SUPPORT
3696 LL_DELETE(session->
lg_crcv, p);
3698 goto skip_app_handler;
3708 goto skip_app_handler;
3726 goto expire_lg_crcv;
3731#if COAP_OSCORE_SUPPORT
3732 if (check_freshness(session, rcvd,
3733 (session->oscore_encryption == 0) ? sent : NULL,
3736 if (check_freshness(session, rcvd, sent, NULL, p))
3738 goto skip_app_handler;
3739 goto expire_lg_crcv;
3742 goto expire_lg_crcv;
3764#
if COAP_Q_BLOCK_SUPPORT
3771 goto skip_app_handler;
3775#if COAP_Q_BLOCK_SUPPORT
3776 if (session->
block_mode & COAP_BLOCK_PROBE_Q_BLOCK) {
3783 if (block.
num != 0) {
3786 const uint8_t *data;
3787 size_t chunk = (size_t)1 << (block.
szx + 4);
3792 goto call_app_handler;
3795#if COAP_Q_BLOCK_SUPPORT
3800 if (!(session->
block_mode & COAP_BLOCK_HAS_Q_BLOCK)) {
3809 LL_PREPEND(session->
lg_crcv, lg_crcv);
3814 track_echo(session, rcvd);
3819 LL_PREPEND(session->
lg_crcv, lg_crcv);
3835 LL_DELETE(session->
lg_crcv, p);
3848#if COAP_SERVER_SUPPORT
3857 if (response->
code == 0)
3860 if (lg_xmit && lg_xmit->
pdu.
code == 0) {
3867#if COAP_CLIENT_SUPPORT
3870 uint64_t token_match =
3877 LL_FOREACH(session->
lg_crcv, lg_crcv) {
3890 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 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)
#define STATE_TOKEN_FULL(t, r)
static int check_all_blocks_in(coap_rblock_t *rec_blocks, size_t total_blocks)
#define STATE_TOKEN_BASE(t)
static int update_received_blocks(coap_rblock_t *rec_blocks, uint32_t block_num)
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)
unsigned char coap_key_t[4]
#define coap_hash(String, Length, Result)
Pulls together all the internal only header files.
@ COAP_NACK_TOO_MANY_RETRIES
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...
void coap_block_delete_lg_srcv(coap_session_t *session, coap_lg_srcv_t *lg_srcv)
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)
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()...
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)
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 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)
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)
int coap_block_check_lg_xmit_timeouts(coap_session_t *session, coap_tick_t now, coap_tick_t *tim_rem)
void coap_context_set_block_mode(coap_context_t *context, uint8_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
#define COAP_BLOCK_USE_M_Q_BLOCK
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_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
#define COAP_BLOCK_TRY_Q_BLOCK
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.
#define COAP_BLOCK_NO_PREEMPTIVE_RTAG
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
#define COAP_OPT_BLOCK_END_BYTE(opt)
Returns the value of the last byte of opt.
int coap_q_block_is_supported(void)
Returns 1 if libcoap was built with option Q-BlockX support, 0 otherwise.
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.
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.
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.
coap_binary_t * coap_block_build_body(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
#define COAP_BLOCK_USE_LIBCOAP
time_t coap_time_t
CoAP time in seconds since epoch.
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
coap_time_t coap_ticks_to_rt(coap_tick_t t)
Helper function that converts coap ticks to wallclock time.
#define COAP_TICKS_PER_SECOND
Use ms resolution on POSIX systems.
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.
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request)
Sends an ACK message with code 0 for the specified request to dst.
uint16_t coap_new_message_id(coap_session_t *session)
Returns a new message id and updates session->tx_mid accordingly.
int coap_handle_event(coap_context_t *context, coap_event_t event, coap_session_t *session)
Invokes the event handler of context for the given event and data.
void coap_ticks(coap_tick_t *)
Returns the current value of an internal tick counter.
COAP_STATIC_INLINE coap_mid_t coap_send_rst(coap_session_t *session, const coap_pdu_t *request)
Sends an RST message with code 0 for the specified request to dst.
@ 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_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_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)
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.
#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
void coap_delete_pdu(coap_pdu_t *pdu)
Dispose of an CoAP PDU and frees associated storage.
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
#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
#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_duplicate(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.
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)
#define COAP_NON_MAX_RETRANSMIT(s)
size_t coap_session_max_pdu_size(const coap_session_t *session)
Get maximum acceptable PDU size.
#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.
#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
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.
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.
coap_resource_t * proxy_uri_resource
can be used for handling proxy URI resources
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
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 content_format
Content format for the set of blocks.
coap_pdu_t pdu
skeletal PDU
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
< list of received blocks
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
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
size_t amount_so_far
Amount of data seen so far.
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_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.
union coap_lg_xmit_t::@1 b
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
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.
struct coap_lg_range range[COAP_RBLOCK_CNT]
Abstraction of resource that can be attached to coap_context_t.
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
uint8_t csm_bert_rem_support
CSM TCP BERT blocks supported (remote)
uint64_t tx_token
Next token number to use.
uint8_t block_mode
Zero or more COAP_BLOCK_ or'd options.
uint8_t csm_bert_loc_support
CSM TCP BERT blocks supported (local)
coap_proto_t proto
protocol used
uint16_t remote_test_mid
mid used for checking remote support
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