libcoap 4.3.5-develop-c7c8934
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-2026 Olaf Bergmann <bergmann@tzi.org>
6 * Copyright (C) 2021-2026 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
18
19#ifndef COAP_BLOCK_INTERNAL_H_
20#define COAP_BLOCK_INTERNAL_H_
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
32
33/*
34 * Use the top 20 bits of a 64 bit token for tracking current block in large transfer.
35 * However, if smaller tokens are required. this would be set up to 48, leaving 16 bits
36 * for unique tokens.
37 */
38#ifndef STATE_MAX_BLK_CNT_BITS
39#define STATE_MAX_BLK_CNT_BITS 20
40#endif /* STATE_MAX_BLK_CNT_BITS */
41#define STATE_TOKEN_BASE(t) ((t) & (0xffffffffffffffffULL >> STATE_MAX_BLK_CNT_BITS))
42#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> (64 - STATE_MAX_BLK_CNT_BITS))
43#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << (64 - STATE_MAX_BLK_CNT_BITS)))
44
45#if COAP_Q_BLOCK_SUPPORT
46#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
47 COAP_BLOCK_SINGLE_BODY | \
48 COAP_BLOCK_TRY_Q_BLOCK | \
49 COAP_BLOCK_USE_M_Q_BLOCK | \
50 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
51 COAP_BLOCK_STLESS_FETCH | \
52 COAP_BLOCK_STLESS_BLOCK2 | \
53 COAP_BLOCK_NOT_RANDOM_BLOCK1 | \
54 COAP_BLOCK_FORCE_Q_BLOCK)
55#else /* ! COAP_Q_BLOCK_SUPPORT */
56#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
57 COAP_BLOCK_SINGLE_BODY | \
58 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
59 COAP_BLOCK_STLESS_FETCH | \
60 COAP_BLOCK_STLESS_BLOCK2 | \
61 COAP_BLOCK_NOT_RANDOM_BLOCK1)
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/* Gives actual max block_size or -1 if not defined */
67#define COAP_BLOCK_MAX_SIZE_GET(a) ((((a) & COAP_BLOCK_MAX_SIZE_MASK) >> COAP_BLOCK_MAX_SIZE_SHIFT) -1)
68/* In COAP_BLOCK_MAX_SIZE_MASK, 0 = not configured, 1 actually is max 0, 2 is max 1 ... 7 is max 6. */
69#define COAP_BLOCK_MAX_SIZE_SET(a) (((a) << COAP_BLOCK_MAX_SIZE_SHIFT) & COAP_BLOCK_MAX_SIZE_MASK)
70
71#if COAP_Q_BLOCK_SUPPORT
72/* Internal use only and are dropped when setting block_mode */
73#define COAP_BLOCK_HAS_Q_BLOCK 0x40000000 /* Set when Q_BLOCK supported */
74#define COAP_BLOCK_PROBE_Q_BLOCK 0x80000000 /* Set when Q_BLOCK probing */
75
76#define set_block_mode_probe_q(block_mode) \
77 do { \
78 block_mode |= COAP_BLOCK_PROBE_Q_BLOCK; \
79 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_HAS_Q_BLOCK); \
80 } while (0)
81
82#define set_block_mode_has_q(block_mode) \
83 do { \
84 block_mode |= COAP_BLOCK_HAS_Q_BLOCK; \
85 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_PROBE_Q_BLOCK); \
86 } while (0)
87
88#define set_block_mode_drop_q(block_mode) \
89 do { \
90 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK |\
91 COAP_BLOCK_PROBE_Q_BLOCK |\
92 COAP_BLOCK_HAS_Q_BLOCK | \
93 COAP_BLOCK_USE_M_Q_BLOCK | \
94 COAP_BLOCK_FORCE_Q_BLOCK); \
95 } while (0)
96
97#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK)
98#else /* ! COAP_Q_BLOCK_SUPPORT */
99#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY)
100#endif /* ! COAP_Q_BLOCK_SUPPORT */
101
106
108 uint32_t begin;
109 uint32_t end;
110};
111
112#ifndef COAP_RBLOCK_CNT
113#define COAP_RBLOCK_CNT 6
114#endif
115#if COAP_RBLOCK_CNT > COAP_DEFAULT_MAX_PAYLOADS -1
116#error COAP_RBLOCK_CNT too large
117#endif
118#if COAP_RBLOCK_CNT < 2
119#error COAP_RBLOCK_CNT too small
120#endif
121
125typedef struct coap_rblock_t {
126 uint32_t used;
127 uint32_t retry;
128#if COAP_Q_BLOCK_SUPPORT
129 uint32_t processing_payload_set;
130 uint32_t latest_payload_set;
131#endif /* COAP_Q_BLOCK_SUPPORT */
134 uint32_t total_blocks;
136
141typedef struct coap_l_block1_t {
143 uint64_t state_token;
144 size_t bert_size;
145 uint32_t count;
146 coap_address_t upstream; /* Unicast IP of server if mcast client session */
148
163
172
178 uint8_t blk_size;
179 uint16_t option;
183 union {
186 } b;
192#if COAP_Q_BLOCK_SUPPORT
193 coap_tick_t non_timeout_random_ticks;
194 coap_rblock_t send_blocks;
195#endif /* COAP_Q_BLOCK_SUPPORT */
196 uint32_t ref;
197};
198
199#if COAP_CLIENT_SUPPORT
203struct coap_lg_crcv_t {
204 struct coap_lg_crcv_t *next;
205 uint8_t observe[3];
206 uint8_t observe_length;
207 uint8_t observe_set;
208 uint8_t szx;
209 uint8_t etag_set;
210 uint8_t etag_length;
211 uint8_t etag[8];
212 uint16_t content_format;
213 uint8_t last_type;
214 uint8_t initial;
215 uint16_t block_option;
216 uint16_t retry_counter;
217 size_t total_len;
218 coap_binary_t *body_data;
219 coap_binary_t *app_token;
220 coap_bin_const_t **obs_token;
222 size_t obs_token_cnt;
223 uint16_t o_block_option;
224 uint8_t o_blk_size;
225 uint8_t total_len_set;
226 uint32_t ref;
227 uint64_t state_token;
228 coap_pdu_t *sent_pdu;
229 coap_rblock_t rec_blocks;
230 coap_tick_t last_used;
231 coap_address_t upstream;
232 coap_lg_xmit_data_t *obs_data;
233};
234#endif /* COAP_CLIENT_SUPPORT */
235
236#if COAP_SERVER_SUPPORT
240struct coap_lg_srcv_t {
241 struct coap_lg_srcv_t *next;
242 uint8_t observe[3];
243 uint8_t observe_length;
244 uint8_t observe_set;
245 uint8_t no_more_seen;
246 uint8_t rtag_set;
247 uint8_t rtag_length;
248 uint8_t rtag[8];
249 uint16_t content_format;
250 uint8_t last_type;
251 uint8_t szx;
252 uint16_t block_option;
253 uint8_t dont_timeout;
254 uint8_t total_len_set;
255 coap_tick_t last_used;
256 size_t total_len;
257 coap_binary_t *body_data;
258 coap_resource_t *resource;
259 coap_rblock_t rec_blocks;
260 coap_bin_const_t *last_token;
261 uint32_t ref;
262#if COAP_Q_BLOCK_SUPPORT
263 int32_t r_m_payload_set;
264#endif /* COAP_Q_BLOCK_SUPPORT */
265#if COAP_OSCORE_SUPPORT
266 oscore_recipient_ctx_t *recipient_ctx;
268#endif /* COAP_OSCORE_SUPPORT */
269 coap_cache_key_t *cache_key;
270};
271#endif /* COAP_SERVER_SUPPORT */
272
273#if COAP_Q_BLOCK_SUPPORT
274typedef enum {
275 COAP_SEND_SKIP_PDU,
276 COAP_SEND_INC_PDU,
277 COAP_SEND_MISSING,
278} coap_send_pdu_t;
279#endif /* COAP_Q_BLOCK_SUPPORT */
280
281#if COAP_CLIENT_SUPPORT
282coap_lg_crcv_t *coap_block_new_lg_crcv(coap_session_t *session,
283 coap_pdu_t *pdu,
284 coap_lg_xmit_t *lg_xmit);
285
297void coap_block_delete_lg_crcv(coap_session_t *session,
298 coap_lg_crcv_t *lg_crcv);
299
311coap_lg_crcv_release_lkd(coap_session_t *session,coap_lg_crcv_t *lg_crcv) {
312 coap_block_delete_lg_crcv(session, lg_crcv);
313 return;
314}
315
324coap_lg_crcv_reference_lkd(coap_lg_crcv_t *lg_crcv) {
325 lg_crcv->ref++;
326 return;
327}
328
329int coap_block_check_lg_crcv_timeouts(coap_session_t *session,
330 coap_tick_t now,
331 coap_tick_t *tim_rem);
332
333#if COAP_Q_BLOCK_SUPPORT
334coap_mid_t coap_send_q_block1(coap_session_t *session,
335 coap_block_b_t block,
336 coap_pdu_t *request,
337 coap_send_pdu_t send_request);
338
348int coap_block_check_q_block1_xmit(coap_session_t *session,
349 coap_tick_t now, coap_tick_t *tim_rem);
350
351coap_mid_t coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual);
352#endif /* COAP_Q_BLOCK_SUPPORT */
353
354#endif /* COAP_CLIENT_SUPPORT */
355
356#if COAP_Q_BLOCK_SUPPORT
372coap_mid_t coap_send_q_blocks(coap_session_t *session,
373 coap_lg_xmit_t *lg_xmit,
374 coap_block_b_t block,
375 coap_pdu_t *pdu,
376 coap_send_pdu_t send_pdu);
377#endif /* COAP_Q_BLOCK_SUPPORT */
378
379#if COAP_SERVER_SUPPORT
391void coap_block_delete_lg_srcv(coap_session_t *session,
392 coap_lg_srcv_t *lg_srcv);
393
405coap_lg_srcv_release_lkd(coap_session_t *session,coap_lg_srcv_t *lg_srcv) {
406 coap_block_delete_lg_srcv(session, lg_srcv);
407 return;
408}
409
418coap_lg_srcv_reference_lkd(coap_lg_srcv_t *lg_srcv) {
419 lg_srcv->ref++;
420 return;
421}
422
423int coap_block_check_lg_srcv_timeouts(coap_session_t *session,
424 coap_tick_t now,
425 coap_tick_t *tim_rem);
426
427#if COAP_Q_BLOCK_SUPPORT
437int coap_block_check_q_block2_xmit(coap_session_t *session,
438 coap_tick_t now, coap_tick_t *tim_rem);
439
440coap_mid_t coap_send_q_block2(coap_session_t *session,
441 coap_resource_t *resource,
442 const coap_string_t *query,
443 coap_pdu_code_t request_method,
444 coap_block_b_t block,
445 coap_pdu_t *response,
446 coap_send_pdu_t send_response);
447#endif /* COAP_Q_BLOCK_SUPPORT */
448
449int coap_handle_request_send_block(coap_session_t *session,
450 coap_pdu_t *pdu,
451 coap_pdu_t *response,
452 coap_resource_t *resource,
453 coap_string_t *query);
454
469int coap_handle_request_put_block(coap_context_t *context,
470 coap_session_t *session,
471 coap_pdu_t *pdu,
472 coap_pdu_t *response,
473 coap_resource_t *resource,
474 coap_opt_t *observe,
475 int *added_block,
476 coap_lg_srcv_t **free_lg_srcv);
477
478coap_lg_xmit_t *coap_find_lg_xmit_response(const coap_session_t *session,
479 const coap_pdu_t *request,
480 const coap_resource_t *resource,
481 const coap_string_t *query);
482
535int coap_add_data_large_response_lkd(coap_resource_t *resource,
536 coap_session_t *session,
537 const coap_pdu_t *request,
538 coap_pdu_t *response,
539 const coap_string_t *query,
540 uint16_t media_type,
541 int maxage,
542 uint64_t etag,
543 size_t length,
544 const uint8_t *data,
545 coap_release_large_data_t release_func,
546 void *app_ptr);
547
548#endif /* COAP_SERVER_SUPPORT */
549
550#if COAP_CLIENT_SUPPORT
551int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
552 coap_pdu_t *rcvd);
553
554int coap_handle_response_get_block(coap_context_t *context,
555 coap_session_t *session,
556 coap_pdu_t *sent,
557 coap_pdu_t *rcvd,
558 coap_recurse_t recursive);
559
560coap_mid_t coap_retransmit_oscore_pdu(coap_session_t *session,
561 coap_pdu_t *pdu,
562 coap_opt_t *echo);
563
573coap_lg_crcv_t *coap_find_lg_crcv(coap_session_t *session, coap_pdu_t *pdu);
574
575#endif /* COAP_CLIENT_SUPPORT */
576
587
600 coap_lg_xmit_t *lg_xmit);
601
614 coap_block_delete_lg_xmit(session, lg_xmit);
615 return;
616}
617
627 lg_xmit->ref++;
628 return;
629}
630
632 coap_tick_t now,
633 coap_tick_t *tim_rem);
634
635#if COAP_Q_BLOCK_SUPPORT
636int coap_block_drop_resp_q_block_xmit(coap_session_t *session,
637 coap_lg_xmit_t *lg_xmit);
638
639int coap_block_drop_resp_q_block2_crcv(coap_session_t *session,
640 coap_lg_crcv_t *lg_crcv,
641 coap_pdu_t *sent);
642#endif /* COAP_Q_BLOCK_SUPPORT */
643
655 const coap_pdu_t *request,
656 coap_pdu_t *response,
657 const coap_resource_t *resource,
658 const coap_string_t *query);
659
679 uint32_t block_mode);
680
695int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size);
696
712coap_binary_t *coap_block_build_body_lkd(coap_binary_t *body_data, size_t length,
713 const uint8_t *data, size_t offset, size_t total);
714
715#if COAP_CLIENT_SUPPORT
760int coap_add_data_large_request_lkd(coap_session_t *session,
761 coap_pdu_t *pdu,
762 size_t length,
763 const uint8_t *data,
764 coap_release_large_data_t release_func,
765 void *app_ptr);
766
806int coap_add_data_large_request_app_lkd(coap_session_t *session,
807 coap_pdu_t *pdu,
808 size_t length,
809 coap_release_large_data_t release_func,
810 coap_get_large_data_t get_func,
811 void *app_ptr);
812
821#else /* ! COAP_CLIENT_SUPPORT */
822#define coap_check_update_token(a,b)
823#endif /* ! COAP_CLIENT_SUPPORT */
824
826
827#ifdef __cplusplus
828}
829#endif
830
831#endif /* COAP_BLOCK_INTERNAL_H_ */
struct coap_lg_crcv_t coap_lg_crcv_t
struct coap_cache_key_t coap_cache_key_t
struct coap_resource_t coap_resource_t
struct coap_lg_srcv_t coap_lg_srcv_t
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
struct coap_rblock_t coap_rblock_t
Structure to keep track of received blocks.
#define coap_check_update_token(a, b)
int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
Definition coap_block.c:455
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:430
#define COAP_RBLOCK_CNT
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...
COAP_STATIC_INLINE void coap_lg_xmit_reference_lkd(coap_lg_xmit_t *lg_xmit)
Increment reference counter on a lg_xmit.
void coap_block_delete_lg_xmit(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Remove a lg_xmit.
coap_binary_t * coap_block_build_body_lkd(coap_binary_t *body_data, size_t length, const uint8_t *data, size_t offset, size_t total)
Re-assemble payloads into a body.
COAP_STATIC_INLINE void coap_lg_xmit_release_lkd(coap_session_t *session, coap_lg_xmit_t *lg_xmit)
Decrement reference counter on a 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:490
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
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() ...
Definition coap_block.h:360
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:291
time_t coap_time_t
CoAP time in seconds since epoch.
Definition coap_time.h:154
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
struct oscore_recipient_ctx_t oscore_recipient_ctx_t
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:192
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:256
struct coap_binary_t coap_binary_t
CoAP binary data definition.
struct coap_bin_const_t coap_bin_const_t
CoAP binary data definition with const data.
#define COAP_STATIC_INLINE
Definition libcoap.h:57
Multi-purpose address abstraction.
CoAP binary data definition with const data.
Definition coap_str.h:65
CoAP binary data definition.
Definition coap_str.h:57
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.
coap_get_large_data_t get_func
Where to get data id needed.
void * app_ptr
application 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.
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.
coap_pdu_t * sent_pdu
The sent pdu with all the data.
coap_l_block1_t b1
coap_l_block2_t b2
uint32_t ref
Reference count.
uint16_t option
large block transmission CoAP option
struct coap_lg_xmit_t * next
union coap_lg_xmit_t::@216322246066344230131205055031364353126322330220 b
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 virtual session that can be attached to coap_context_t (client) or coap_endpoint_t (se...
CoAP string data definition.
Definition coap_str.h:39