libcoap 4.3.5-develop-34cee6e
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
19#ifndef COAP_BLOCK_INTERNAL_H_
20#define COAP_BLOCK_INTERNAL_H_
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
33/* Use the top 20 bits of a 64 bit token for tracking current block in large transfer */
34#define STATE_MAX_BLK_CNT_BITS 20
35#define STATE_TOKEN_BASE(t) ((t) & (0xffffffffffffffffULL >> STATE_MAX_BLK_CNT_BITS))
36#define STATE_TOKEN_RETRY(t) ((uint64_t)(t) >> (64 - STATE_MAX_BLK_CNT_BITS))
37#define STATE_TOKEN_FULL(t,r) (STATE_TOKEN_BASE(t) + ((uint64_t)(r) << (64 - STATE_MAX_BLK_CNT_BITS)))
38
39#if COAP_Q_BLOCK_SUPPORT
40#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
41 COAP_BLOCK_SINGLE_BODY | \
42 COAP_BLOCK_TRY_Q_BLOCK | \
43 COAP_BLOCK_USE_M_Q_BLOCK | \
44 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
45 COAP_BLOCK_STLESS_FETCH | \
46 COAP_BLOCK_STLESS_BLOCK2 | \
47 COAP_BLOCK_NOT_RANDOM_BLOCK1 | \
48 COAP_BLOCK_CACHE_RESPONSE | \
49 COAP_BLOCK_FORCE_Q_BLOCK)
50#else /* ! COAP_Q_BLOCK_SUPPORT */
51#define COAP_BLOCK_SET_MASK (COAP_BLOCK_USE_LIBCOAP | \
52 COAP_BLOCK_SINGLE_BODY | \
53 COAP_BLOCK_NO_PREEMPTIVE_RTAG | \
54 COAP_BLOCK_STLESS_FETCH | \
55 COAP_BLOCK_STLESS_BLOCK2 | \
56 COAP_BLOCK_NOT_RANDOM_BLOCK1 | \
57 COAP_BLOCK_CACHE_RESPONSE)
58#endif /* ! COAP_Q_BLOCK_SUPPORT */
59
60#define COAP_BLOCK_MAX_SIZE_MASK 0x7000000 /* (svr)Mask to get the max supported block size */
61#define COAP_BLOCK_MAX_SIZE_SHIFT 24 /* (svr)Mask shift to get the max supported block size */
62#define COAP_BLOCK_MAX_SIZE_GET(a) (((a) & COAP_BLOCK_MAX_SIZE_MASK) >> COAP_BLOCK_MAX_SIZE_SHIFT)
63#define COAP_BLOCK_MAX_SIZE_SET(a) (((a) << COAP_BLOCK_MAX_SIZE_SHIFT) & COAP_BLOCK_MAX_SIZE_MASK)
64
65#if COAP_Q_BLOCK_SUPPORT
66/* Internal use only and are dropped when setting block_mode */
67#define COAP_BLOCK_HAS_Q_BLOCK 0x40000000 /* Set when Q_BLOCK supported */
68#define COAP_BLOCK_PROBE_Q_BLOCK 0x80000000 /* Set when Q_BLOCK probing */
69
70#define set_block_mode_probe_q(block_mode) \
71 do { \
72 block_mode |= COAP_BLOCK_PROBE_Q_BLOCK; \
73 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_HAS_Q_BLOCK); \
74 } while (0)
75
76#define set_block_mode_has_q(block_mode) \
77 do { \
78 block_mode |= COAP_BLOCK_HAS_Q_BLOCK; \
79 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK | COAP_BLOCK_PROBE_Q_BLOCK); \
80 } while (0)
81
82#define set_block_mode_drop_q(block_mode) \
83 do { \
84 block_mode &= ~(COAP_BLOCK_TRY_Q_BLOCK |\
85 COAP_BLOCK_PROBE_Q_BLOCK |\
86 COAP_BLOCK_HAS_Q_BLOCK | \
87 COAP_BLOCK_USE_M_Q_BLOCK | \
88 COAP_BLOCK_FORCE_Q_BLOCK); \
89 } while (0)
90
91#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY|COAP_BLOCK_HAS_Q_BLOCK)
92#else /* ! COAP_Q_BLOCK_SUPPORT */
93#define COAP_SINGLE_BLOCK_OR_Q (COAP_BLOCK_SINGLE_BODY)
94#endif /* ! COAP_Q_BLOCK_SUPPORT */
95
100
102 uint32_t begin;
103 uint32_t end;
104};
105
106#define COAP_RBLOCK_CNT 4
110typedef struct coap_rblock_t {
111 uint32_t used;
112 uint32_t retry;
113#if COAP_Q_BLOCK_SUPPORT
114 uint32_t processing_payload_set;
115 uint32_t latest_payload_set;
116#endif /* COAP_Q_BLOCK_SUPPORT */
119 uint32_t total_blocks;
121
126typedef struct coap_l_block1_t {
128 uint64_t state_token;
129 size_t bert_size;
130 uint32_t count;
131 coap_address_t upstream; /* Unicast IP of server if mcast client session */
133
148
157
163 uint8_t blk_size;
164 uint16_t option;
168 size_t offset;
169 union {
172 } b;
178#if COAP_Q_BLOCK_SUPPORT
179 coap_tick_t non_timeout_random_ticks;
180#endif /* COAP_Q_BLOCK_SUPPORT */
181 uint32_t ref;
182};
183
184#if COAP_CLIENT_SUPPORT
188struct coap_lg_crcv_t {
189 struct coap_lg_crcv_t *next;
190 uint8_t observe[3];
191 uint8_t observe_length;
192 uint8_t observe_set;
193 uint8_t szx;
194 uint8_t etag_set;
195 uint8_t etag_length;
196 uint8_t etag[8];
197 uint16_t content_format;
198 uint8_t last_type;
199 uint8_t initial;
200 uint16_t block_option;
201 uint16_t retry_counter;
202 size_t total_len;
203 coap_binary_t *body_data;
204 coap_binary_t *app_token;
205 coap_bin_const_t **obs_token;
207 size_t obs_token_cnt;
208 uint16_t o_block_option;
209 uint8_t o_blk_size;
210 uint32_t ref;
211 uint64_t state_token;
212 coap_pdu_t *sent_pdu;
213 coap_rblock_t rec_blocks;
214 coap_tick_t last_used;
215 coap_address_t upstream;
216 coap_lg_xmit_data_t *obs_data;
217};
218#endif /* COAP_CLIENT_SUPPORT */
219
220#if COAP_SERVER_SUPPORT
224struct coap_lg_srcv_t {
225 struct coap_lg_srcv_t *next;
226 uint8_t observe[3];
227 uint8_t observe_length;
228 uint8_t observe_set;
229 uint8_t no_more_seen;
230 uint8_t rtag_set;
231 uint8_t rtag_length;
232 uint8_t rtag[8];
233 uint16_t content_format;
234 uint8_t last_type;
235 uint8_t szx;
236 uint16_t block_option;
237 uint8_t dont_timeout;
238 coap_mid_t last_mid;
239 coap_tick_t last_used;
240 size_t total_len;
241 coap_binary_t *body_data;
242 coap_resource_t *resource;
243 coap_str_const_t *uri_path;
244 coap_rblock_t rec_blocks;
245 coap_bin_const_t *last_token;
246 uint32_t ref;
247};
248#endif /* COAP_SERVER_SUPPORT */
249
250#if COAP_Q_BLOCK_SUPPORT
251typedef enum {
252 COAP_SEND_SKIP_PDU,
253 COAP_SEND_INC_PDU
254} coap_send_pdu_t;
255#endif /* COAP_Q_BLOCK_SUPPORT */
256
257#if COAP_CLIENT_SUPPORT
258coap_lg_crcv_t *coap_block_new_lg_crcv(coap_session_t *session,
259 coap_pdu_t *pdu,
260 coap_lg_xmit_t *lg_xmit);
261
273void coap_block_delete_lg_crcv(coap_session_t *session,
274 coap_lg_crcv_t *lg_crcv);
275
287coap_lg_crcv_release_lkd(coap_session_t *session,coap_lg_crcv_t *lg_crcv) {
288 coap_block_delete_lg_crcv(session, lg_crcv);
289 return;
290}
291
300coap_lg_crcv_reference_lkd(coap_lg_crcv_t *lg_crcv) {
301 lg_crcv->ref++;
302 return;
303}
304
305int coap_block_check_lg_crcv_timeouts(coap_session_t *session,
306 coap_tick_t now,
307 coap_tick_t *tim_rem);
308
309#if COAP_Q_BLOCK_SUPPORT
310coap_mid_t coap_send_q_block1(coap_session_t *session,
311 coap_block_b_t block,
312 coap_pdu_t *request,
313 coap_send_pdu_t send_request);
314
324int coap_block_check_q_block1_xmit(coap_session_t *session,
325 coap_tick_t now, coap_tick_t *tim_rem);
326
327coap_mid_t coap_block_test_q_block(coap_session_t *session, coap_pdu_t *actual);
328#endif /* COAP_Q_BLOCK_SUPPORT */
329
330#endif /* COAP_CLIENT_SUPPORT */
331
332#if COAP_Q_BLOCK_SUPPORT
333coap_mid_t coap_send_q_blocks(coap_session_t *session,
334 coap_lg_xmit_t *lg_xmit,
335 coap_block_b_t block,
336 coap_pdu_t *pdu,
337 coap_send_pdu_t send_pdu);
338#endif /* COAP_Q_BLOCK_SUPPORT */
339
340#if COAP_SERVER_SUPPORT
352void coap_block_delete_lg_srcv(coap_session_t *session,
353 coap_lg_srcv_t *lg_srcv);
354
366coap_lg_srcv_release_lkd(coap_session_t *session,coap_lg_srcv_t *lg_srcv) {
367 coap_block_delete_lg_srcv(session, lg_srcv);
368 return;
369}
370
379coap_lg_srcv_reference_lkd(coap_lg_srcv_t *lg_srcv) {
380 lg_srcv->ref++;
381 return;
382}
383
384int coap_block_check_lg_srcv_timeouts(coap_session_t *session,
385 coap_tick_t now,
386 coap_tick_t *tim_rem);
387
388#if COAP_Q_BLOCK_SUPPORT
398int coap_block_check_q_block2_xmit(coap_session_t *session,
399 coap_tick_t now, coap_tick_t *tim_rem);
400
401coap_mid_t coap_send_q_block2(coap_session_t *session,
402 coap_resource_t *resource,
403 const coap_string_t *query,
404 coap_pdu_code_t request_method,
405 coap_block_b_t block,
406 coap_pdu_t *response,
407 coap_send_pdu_t send_response);
408#endif /* COAP_Q_BLOCK_SUPPORT */
409
410int coap_handle_request_send_block(coap_session_t *session,
411 coap_pdu_t *pdu,
412 coap_pdu_t *response,
413 coap_resource_t *resource,
414 coap_string_t *query);
415
416int coap_handle_request_put_block(coap_context_t *context,
417 coap_session_t *session,
418 coap_pdu_t *pdu,
419 coap_pdu_t *response,
420 coap_resource_t *resource,
421 coap_string_t *uri_path,
422 coap_opt_t *observe,
423 int *added_block,
424 coap_lg_srcv_t **free_lg_srcv);
425
426coap_lg_xmit_t *coap_find_lg_xmit_response(const coap_session_t *session,
427 const coap_pdu_t *request,
428 const coap_resource_t *resource,
429 const coap_string_t *query);
430
483int coap_add_data_large_response_lkd(coap_resource_t *resource,
484 coap_session_t *session,
485 const coap_pdu_t *request,
486 coap_pdu_t *response,
487 const coap_string_t *query,
488 uint16_t media_type,
489 int maxage,
490 uint64_t etag,
491 size_t length,
492 const uint8_t *data,
493 coap_release_large_data_t release_func,
494 void *app_ptr);
495
496#endif /* COAP_SERVER_SUPPORT */
497
498#if COAP_CLIENT_SUPPORT
499int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *sent,
500 coap_pdu_t *rcvd);
501
502int coap_handle_response_get_block(coap_context_t *context,
503 coap_session_t *session,
504 coap_pdu_t *sent,
505 coap_pdu_t *rcvd,
506 coap_recurse_t recursive);
507
508coap_mid_t coap_retransmit_oscore_pdu(coap_session_t *session,
509 coap_pdu_t *pdu,
510 coap_opt_t *echo);
511
521coap_lg_crcv_t *coap_find_lg_crcv(coap_session_t *session, coap_pdu_t *pdu);
522
523#endif /* COAP_CLIENT_SUPPORT */
524
535
548 coap_lg_xmit_t *lg_xmit);
549
562 coap_block_delete_lg_xmit(session, lg_xmit);
563 return;
564}
565
575 lg_xmit->ref++;
576 return;
577}
578
580 coap_tick_t now,
581 coap_tick_t *tim_rem);
582
583#if COAP_Q_BLOCK_SUPPORT
584int coap_block_drop_resp_q_block_xmit(coap_session_t *session,
585 coap_lg_xmit_t *lg_xmit);
586
587int coap_block_drop_resp_q_block2_crcv(coap_session_t *session,
588 coap_lg_crcv_t *lg_crcv,
589 coap_pdu_t *sent);
590#endif /* COAP_Q_BLOCK_SUPPORT */
591
603 const coap_pdu_t *request,
604 coap_pdu_t *response,
605 const coap_resource_t *resource,
606 const coap_string_t *query);
607
627 uint32_t block_mode);
628
643int coap_context_set_max_block_size_lkd(coap_context_t *context, size_t max_block_size);
644
660coap_binary_t *coap_block_build_body_lkd(coap_binary_t *body_data, size_t length,
661 const uint8_t *data, size_t offset, size_t total);
662
663#if COAP_CLIENT_SUPPORT
708int coap_add_data_large_request_lkd(coap_session_t *session,
709 coap_pdu_t *pdu,
710 size_t length,
711 const uint8_t *data,
712 coap_release_large_data_t release_func,
713 void *app_ptr);
714
754int coap_add_data_large_request_app_lkd(coap_session_t *session,
755 coap_pdu_t *pdu,
756 size_t length,
757 coap_release_large_data_t release_func,
758 coap_get_large_data_t get_func,
759 void *app_ptr);
760
769#else /* ! COAP_CLIENT_SUPPORT */
770#define coap_check_update_token(a,b)
771#endif /* ! COAP_CLIENT_SUPPORT */
772
775#ifdef __cplusplus
776}
777#endif
778
779#endif /* COAP_BLOCK_INTERNAL_H_ */
struct coap_lg_crcv_t coap_lg_crcv_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...
Definition coap_option.h:43
#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:448
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:423
#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:478
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:361
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:154
uint64_t coap_tick_t
This data type represents internal timer ticks with COAP_TICKS_PER_SECOND resolution.
Definition coap_time.h:149
int coap_mid_t
coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
Definition coap_pdu.h:267
coap_pdu_code_t
Set of codes available for a PDU.
Definition coap_pdu.h:331
#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
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
uint32_t ref
Reference count.
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 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:47
CoAP string data definition.
Definition coap_str.h:39