libcoap 4.3.5-develop-490e4e0
Loading...
Searching...
No Matches
coap_block_internal.h
Go to the documentation of this file.
1/*
2 * coap_block_internal.h -- Structures, Enums & Functions that are not
3 * exposed to application programming
4 *
5 * Copyright (C) 2010-2025 Olaf Bergmann <bergmann@tzi.org>
6 * Copyright (C) 2021-2025 Jon Shallow <supjps-libcoap@jpshallow.com>
7 *
8 * SPDX-License-Identifier: BSD-2-Clause
9 *
10 * This file is part of the CoAP library libcoap. Please see README for terms
11 * of use.
12 */
13
19#ifndef COAP_BLOCK_INTERNAL_H_
20#define COAP_BLOCK_INTERNAL_H_
21
22#include "coap_internal.h"
23#include "coap_pdu_internal.h"
24#include "coap_resource.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
37/* Use the top 20 bits of a 64 bit token for tracking current block in large transfer */
38#define STATE_MAX_BLK_CNT_BITS 20
39#define STATE_TOKEN_BASE(t) ((t) & (0xffffffffffffffffULL >> STATE_MAX_BLK_CNT_BITS))
40#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> (64 - STATE_MAX_BLK_CNT_BITS))
41#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << (64 - STATE_MAX_BLK_CNT_BITS)))
42
43#if COAP_Q_BLOCK_SUPPORT
44#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
45 COAP_BLOCK_SINGLE_BODY | \
46 COAP_BLOCK_TRY_Q_BLOCK | \
47 COAP_BLOCK_USE_M_Q_BLOCK | \
48 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
49 COAP_BLOCK_STLESS_FETCH | \
50 COAP_BLOCK_STLESS_BLOCK2 | \
51 COAP_BLOCK_NOT_RANDOM_BLOCK1 | \
52 COAP_BLOCK_CACHE_RESPONSE | \
53 COAP_BLOCK_FORCE_Q_BLOCK)
54#else /* ! COAP_Q_BLOCK_SUPPORT */
55#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
56 COAP_BLOCK_SINGLE_BODY | \
57 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
58 COAP_BLOCK_STLESS_FETCH | \
59 COAP_BLOCK_STLESS_BLOCK2 | \
60 COAP_BLOCK_NOT_RANDOM_BLOCK1 | \
61 COAP_BLOCK_CACHE_RESPONSE)
62#endif /* ! COAP_Q_BLOCK_SUPPORT */
63
64#define COAP_BLOCK_MAX_SIZE_MASK 0x7000000 /* (svr)Mask to get the max supported block size */
65#define COAP_BLOCK_MAX_SIZE_SHIFT 24 /* (svr)Mask shift to get the max supported block size */
66#define COAP_BLOCK_MAX_SIZE_GET(a) (((a) & COAP_BLOCK_MAX_SIZE_MASK) >> COAP_BLOCK_MAX_SIZE_SHIFT)
67#define COAP_BLOCK_MAX_SIZE_SET(a) (((a) << COAP_BLOCK_MAX_SIZE_SHIFT) & COAP_BLOCK_MAX_SIZE_MASK)
68
69#if COAP_Q_BLOCK_SUPPORT
70/* Internal use only and are dropped when setting block_mode */
71#define COAP_BLOCK_HAS_Q_BLOCK 0x40000000 /* Set when Q_BLOCK supported */
72#define COAP_BLOCK_PROBE_Q_BLOCK 0x80000000 /* Set when Q_BLOCK probing */
73
74#define set_block_mode_probe_q(block_mode) \
75 do { \
76 block_mode |= COAP_BLOCK_PROBE_Q_BLOCK; \
77 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_HAS_Q_BLOCK); \
78 } while (0)
79
80#define set_block_mode_has_q(block_mode) \
81 do { \
82 block_mode |= COAP_BLOCK_HAS_Q_BLOCK; \
83 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_PROBE_Q_BLOCK); \
84 } while (0)
85
86#define set_block_mode_drop_q(block_mode) \
87 do { \
88 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK |\
89 COAP_BLOCK_PROBE_Q_BLOCK |\
90 COAP_BLOCK_HAS_Q_BLOCK | \
91 COAP_BLOCK_USE_M_Q_BLOCK | \
92 COAP_BLOCK_FORCE_Q_BLOCK); \
93 } while (0)
94
95#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK)
96#else /* ! COAP_Q_BLOCK_SUPPORT */
97#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY)
98#endif /* ! COAP_Q_BLOCK_SUPPORT */
99
104
106 uint32_t begin;
107 uint32_t end;
108};
109
110#define COAP_RBLOCK_CNT 4
114typedef struct coap_rblock_t {
115 uint32_t used;
116 uint32_t retry;
117#if COAP_Q_BLOCK_SUPPORT
118 uint32_t processing_payload_set;
119 uint32_t latest_payload_set;
120#endif /* COAP_Q_BLOCK_SUPPORT */
123 uint32_t total_blocks;
125
130typedef struct coap_l_block1_t {
132 uint64_t state_token;
133 size_t bert_size;
134 uint32_t count;
135 coap_address_t upstream; /* Unicast IP of server if mcast client session */
137
152
160
166 uint8_t blk_size;
167 uint16_t option;
171 size_t offset;
172 union {
175 } b;
181#if COAP_Q_BLOCK_SUPPORT
182 coap_tick_t non_timeout_random_ticks;
183#endif /* COAP_Q_BLOCK_SUPPORT */
184};
185
186#if COAP_CLIENT_SUPPORT
219#endif /* COAP_CLIENT_SUPPORT */
220
221#if COAP_SERVER_SUPPORT
247#endif /* COAP_SERVER_SUPPORT */
248
249#if COAP_Q_BLOCK_SUPPORT
250typedef enum {
251 COAP_SEND_SKIP_PDU,
252 COAP_SEND_INC_PDU
253} coap_send_pdu_t;
254#endif /* COAP_Q_BLOCK_SUPPORT */
255
256#if COAP_CLIENT_SUPPORT
258 coap_pdu_t *pdu,
259 coap_lg_xmit_t *lg_xmit);
260
262 coap_lg_crcv_t *lg_crcv);
263
265 coap_tick_t now,
266 coap_tick_t *tim_rem);
267
268#if COAP_Q_BLOCK_SUPPORT
269coap_mid_t coap_send_q_block1(coap_session_t *session,
270 coap_block_b_t block,
271 coap_pdu_t *request,
272 coap_send_pdu_t send_request);
273
283int coap_block_check_q_block1_xmit(coap_session_t *session,
284 coap_tick_t now, coap_tick_t *tim_rem);
285
286coap_mid_t coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual);
287#endif /* COAP_Q_BLOCK_SUPPORT */
288
289#endif /* COAP_CLIENT_SUPPORT */
290
291#if COAP_Q_BLOCK_SUPPORT
292coap_mid_t coap_send_q_blocks(coap_session_t *session,
293 coap_lg_xmit_t *lg_xmit,
294 coap_block_b_t block,
295 coap_pdu_t *pdu,
296 coap_send_pdu_t send_pdu);
297#endif /* COAP_Q_BLOCK_SUPPORT */
298
299#if COAP_SERVER_SUPPORT
301 coap_lg_srcv_t *lg_srcv);
302
304 coap_tick_t now,
305 coap_tick_t *tim_rem);
306
307#if COAP_Q_BLOCK_SUPPORT
317int coap_block_check_q_block2_xmit(coap_session_t *session,
318 coap_tick_t now, coap_tick_t *tim_rem);
319
320coap_mid_t coap_send_q_block2(coap_session_t *session,
322 const coap_string_t *query,
323 coap_pdu_code_t request_method,
324 coap_block_b_t block,
325 coap_pdu_t *response,
326 coap_send_pdu_t send_response);
327#endif /* COAP_Q_BLOCK_SUPPORT */
328
330 coap_pdu_t *pdu,
331 coap_pdu_t *response,
333 coap_string_t *query);
334
336 coap_session_t *session,
337 coap_pdu_t *pdu,
338 coap_pdu_t *response,
342 int *added_block,
343 coap_lg_srcv_t **free_lg_srcv);
344
346 const coap_pdu_t *request,
348 const coap_string_t *query);
349
403 coap_session_t *session,
404 const coap_pdu_t *request,
405 coap_pdu_t *response,
406 const coap_string_t *query,
407 uint16_t media_type,
408 int maxage,
409 uint64_t etag,
410 size_t length,
411 const uint8_t *data,
412 coap_release_large_data_t release_func,
413 void *app_ptr);
414
415#endif /* COAP_SERVER_SUPPORT */
416
417#if COAP_CLIENT_SUPPORT
419 coap_pdu_t *rcvd);
420
422 coap_session_t *session,
423 coap_pdu_t *sent,
424 coap_pdu_t *rcvd,
425 coap_recurse_t recursive);
426
428 coap_pdu_t *pdu,
429 coap_opt_t *echo);
430
441
442#endif /* COAP_CLIENT_SUPPORT */
443
454
456 coap_lg_xmit_t *lg_xmit);
457
459 coap_tick_t now,
460 coap_tick_t *tim_rem);
461
462#if COAP_Q_BLOCK_SUPPORT
463int coap_block_drop_resp_q_block_xmit(coap_session_t *session,
464 coap_lg_xmit_t *lg_xmit);
465
466int coap_block_drop_resp_q_block2_crcv(coap_session_t *session,
467 coap_lg_crcv_t *lg_crcv,
468 coap_pdu_t *sent);
469#endif /* COAP_Q_BLOCK_SUPPORT */
470
482 const coap_pdu_t *request,
483 coap_pdu_t *response,
485 const coap_string_t *query);
486
506 uint32_t block_mode);
507
524int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size);
525
526#if COAP_CLIENT_SUPPORT
572 coap_pdu_t *pdu,
573 size_t length,
574 const uint8_t *data,
575 coap_release_large_data_t release_func,
576 void *app_ptr);
577
586#else /* ! COAP_CLIENT_SUPPORT */
587#define coap_check_update_token(a,b)
588#endif /* ! COAP_CLIENT_SUPPORT */
589
592#ifdef __cplusplus
593}
594#endif
595
596#endif /* COAP_BLOCK_INTERNAL_H_ */
Pulls together all the internal only header files.
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
Definition coap_option.h:30
CoAP PDU internal information.
Generic resource handling.
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...
Definition coap_block.c:442
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.
Definition coap_block.c:417
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_RBLOCK_CNT
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)
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)
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.
Definition coap_block.c:472
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)
@ COAP_RECURSE_OK
@ COAP_RECURSE_NO
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...
Definition coap_block.h:292
time_t coap_time_t
CoAP time in seconds since epoch.
Definition coap_time.h:156
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:151
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:268
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:331
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:67
CoAP binary data definition.
Definition coap_str.h:59
Structure of Block options with BERT support.
Definition coap_block.h:55
The CoAP stack's global state is stored in a coap_context_t object.
Structure to keep track of block1 specific information (Requests)
uint64_t state_token
state token
size_t bert_size
size of last BERT block
coap_address_t upstream
uint32_t count
the number of packets sent for payload
coap_binary_t * app_token
original PDU token
Structure to keep track of block2 specific information (Responses)
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.
uint64_t etag
ETag value.
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
struct coap_lg_crcv_t * next
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.
struct coap_lg_srcv_t * next
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
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.
coap_l_block1_t b1
coap_l_block2_t b2
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.
structure for CoAP PDUs
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.
Abstraction of virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
CoAP string data definition with const data.
Definition coap_str.h:49
CoAP string data definition.
Definition coap_str.h:41