libcoap 4.3.5-develop-f52fada
Loading...
Searching...
No Matches
coap_block.h
Go to the documentation of this file.
1/*
2 * coap_block.h -- block transfer
3 *
4 * Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
5 * Copyright (C) 2022-2026 Jon Shallow <supjps-libcoap@jpshallow.com>
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 *
9 * This file is part of the CoAP library libcoap. Please see README for terms
10 * of use.
11 */
12
17
18#ifndef COAP_BLOCK_H_
19#define COAP_BLOCK_H_
20
21#include "coap_encode.h"
22#include "coap_option.h"
23#include "coap_pdu.h"
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
35
36#ifndef COAP_MAX_BLOCK_SZX
40#define COAP_MAX_BLOCK_SZX 6
41#endif /* COAP_MAX_BLOCK_SZX */
42
46typedef struct {
47 unsigned int num;
48 unsigned int m:1;
49 unsigned int szx:3;
51
55typedef struct {
56 unsigned int num;
57 unsigned int m:1;
58 unsigned int szx:3;
59 unsigned int aszx:3;
60 unsigned int defined:1;
61 unsigned int bert:1;
62 uint32_t chunk_size;
64
65#define COAP_BLOCK_USE_LIBCOAP 0x01 /* Use libcoap to do block requests */
66#define COAP_BLOCK_SINGLE_BODY 0x02 /* Deliver the data as a single body */
67#define COAP_BLOCK_TRY_Q_BLOCK 0x04 /* Try Q-Block method */
68#define COAP_BLOCK_USE_M_Q_BLOCK 0x08 /* Use M bit when recovering Q-Block2 */
69#define COAP_BLOCK_NO_PREEMPTIVE_RTAG 0x10 /* (cl) Don't use pre-emptive Request-Tags */
70#define COAP_BLOCK_STLESS_FETCH 0x20 /* (cl) Assume server supports stateless FETCH */
71#define COAP_BLOCK_STLESS_BLOCK2 0x40 /* (svr)Server is stateless for handling Block2 */
72#define COAP_BLOCK_NOT_RANDOM_BLOCK1 0x80 /* (svr)Disable server handling random order
73 block1 */
74#define COAP_BLOCK_FORCE_Q_BLOCK 0x200 /* force Q-Block method without support check */
75/* WARNING: Added defined values must not encroach into 0xff000000 which are defined elsewhere */
76
81 */
82#define COAP_OPT_BLOCK_LAST(opt) \
83 (coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
84
85/** Returns the value of the last byte of @p opt. */
86#define COAP_OPT_BLOCK_END_BYTE(opt) \
87 ((coap_opt_length(opt) && coap_opt_value(opt)) ? \
88 *(coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
89
90/** Returns the value of the More-bit of a Block option @p opt. */
91#define COAP_OPT_BLOCK_MORE(opt) \
92 (coap_opt_length(opt) ? (COAP_OPT_BLOCK_END_BYTE(opt) & 0x08) : 0)
93
94/** Returns the value of the SZX-field of a Block option @p opt. */
95#define COAP_OPT_BLOCK_SZX(opt) \
96 (coap_opt_length(opt) ? (COAP_OPT_BLOCK_END_BYTE(opt) & 0x07) : 0)
97
101unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
102
108coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
109 return ((num+1) << (szx + 4)) < data_len;
110}
111
112#if 0
115coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
116 if (m)
117 *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
118 else
119 *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
120}
121#endif
122
136int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number,
137 coap_block_t *block);
138
139
155int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu,
156 coap_option_num_t number, coap_block_b_t *block);
157
179 coap_option_num_t number,
180 coap_pdu_t *pdu,
181 size_t data_length);
204 coap_block_b_t *block,
205 coap_option_num_t number,
206 coap_pdu_t *pdu,
207 size_t data_length);
208
209
223 size_t len,
224 const uint8_t *data,
225 unsigned int block_num,
226 unsigned char block_szx);
227
238int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
239 coap_block_b_t *block);
240
255 const uint8_t *data, size_t offset, size_t total);
256
277 coap_pdu_t *response,
278 uint16_t media_type,
279 int maxage,
280 size_t length,
281 const uint8_t *data);
282
292typedef void (*coap_release_large_data_t)(coap_session_t *session,
293 void *app_ptr);
294
338 coap_pdu_t *pdu,
339 size_t length,
340 const uint8_t *data,
341 coap_release_large_data_t release_func,
342 void *app_ptr);
343
361typedef int (*coap_get_large_data_t)(coap_session_t *session, size_t max,
362 size_t offset,
363 uint8_t *data, size_t *length,
364 void *app_ptr);
403 coap_pdu_t *pdu,
404 size_t length,
405 coap_release_large_data_t release_func,
406 coap_get_large_data_t get_func,
407 void *app_ptr);
408
460 coap_session_t *session,
461 const coap_pdu_t *request,
462 coap_pdu_t *response,
463 const coap_string_t *query,
464 uint16_t media_type,
465 int maxage,
466 uint64_t etag,
467 size_t length,
468 const uint8_t *data,
469 coap_release_large_data_t release_func,
470 void *app_ptr);
471
491 uint32_t block_mode);
492
507COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size);
508
510
511#ifdef __cplusplus
512}
513#endif
514
515#endif /* COAP_BLOCK_H_ */
Encoding and decoding of CoAP data types.
struct coap_resource_t coap_resource_t
#define COAP_API
Helpers for handling options in CoAP PDUs.
uint16_t coap_option_num_t
Definition coap_option.h:37
uint8_t coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
#define max(a, b)
Definition coap_pdu.c:41
Pre-defined constants that reflect defaults for CoAP.
COAP_API void coap_context_set_block_mode(coap_context_t *context, uint32_t block_mode)
Set the context level CoAP block handling bits for handling RFC7959.
Definition coap_block.c:421
COAP_API int coap_add_data_large_response(coap_resource_t *resource, coap_session_t *session, const coap_pdu_t *request, coap_pdu_t *response, const coap_string_t *query, uint16_t media_type, int maxage, uint64_t etag, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the response pdu that is passed as fourth parameter.
int(* coap_get_large_data_t)(coap_session_t *session, size_t max, size_t offset, uint8_t *data, size_t *length, void *app_ptr)
Callback handler for getting the data based on app_ptr provided to coap_add_data_large_request_app() ...
Definition coap_block.h:360
COAP_API int coap_add_data_large_request(coap_session_t *session, coap_pdu_t *pdu, size_t length, const uint8_t *data, coap_release_large_data_t release_func, void *app_ptr)
Associates given data with the pdu that is passed as second parameter.
COAP_API int coap_context_set_max_block_size(coap_context_t *context, size_t max_block_size)
Set the context level maximum block size that the server supports when sending or receiving packets w...
Definition coap_block.c:443
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.
Definition coap_block.c:260
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.
Definition coap_block.c:215
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.
Definition coap_block.c:246
COAP_API 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.
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
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.
Definition coap_block.c:285
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.
Definition coap_block.c:70
COAP_STATIC_INLINE int coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx)
Checks if more than num blocks are required to deliver data_len bytes of data for a block size of 1 <...
Definition coap_block.h:107
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.
Definition coap_block.c:51
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
Definition coap_block.c:123
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.
Definition coap_block.c:182
COAP_API int coap_add_data_large_request_app(coap_session_t *session, coap_pdu_t *pdu, size_t length, coap_release_large_data_t release_func, coap_get_large_data_t get_func, void *app_ptr)
Associates given data callback with the pdu that is passed as second parameter.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
#define COAP_STATIC_INLINE
Definition libcoap.h:57
CoAP binary data definition.
Definition coap_str.h:57
Structure of Block options with BERT support.
Definition coap_block.h:55
unsigned int num
block number
Definition coap_block.h:56
uint32_t chunk_size
Definition coap_block.h:62
unsigned int bert
Operating as BERT.
Definition coap_block.h:61
unsigned int aszx
block size (0-7 including BERT
Definition coap_block.h:59
unsigned int defined
Set if block found.
Definition coap_block.h:60
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:57
unsigned int szx
block size (0-6)
Definition coap_block.h:58
Structure of Block options.
Definition coap_block.h:46
unsigned int num
block number
Definition coap_block.h:47
unsigned int szx
block size
Definition coap_block.h:49
unsigned int m
1 if more blocks follow, 0 otherwise
Definition coap_block.h:48
The CoAP stack's global state is stored in a coap_context_t object.
structure for CoAP PDUs
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