libcoap  4.3.1
block.h
Go to the documentation of this file.
1 /*
2  * block.h -- block transfer
3  *
4  * Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
5  * Copyright (C) 2022 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 
18 #ifndef COAP_BLOCK_H_
19 #define COAP_BLOCK_H_
20 
21 #include "encode.h"
22 #include "coap_option.h"
23 #include "pdu.h"
24 
32 #ifndef COAP_MAX_BLOCK_SZX
36 #define COAP_MAX_BLOCK_SZX 6
37 #endif /* COAP_MAX_BLOCK_SZX */
38 
42 typedef struct {
43  unsigned int num;
44  unsigned int m:1;
45  unsigned int szx:3;
46 } coap_block_t;
47 
51 typedef struct {
52  unsigned int num;
53  unsigned int m:1;
54  unsigned int szx:3;
55  unsigned int aszx:3;
56  unsigned int defined:1;
57  unsigned int bert:1;
58  uint32_t chunk_size;
60 
61 #define COAP_BLOCK_USE_LIBCOAP 0x01 /* Use libcoap to do block requests */
62 #define COAP_BLOCK_SINGLE_BODY 0x02 /* Deliver the data as a single body */
63 
69 #define COAP_OPT_BLOCK_LAST(opt) \
70  (coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
71 
73 #define COAP_OPT_BLOCK_MORE(opt) \
74  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
75 
77 #define COAP_OPT_BLOCK_SZX(opt) \
78  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
79 
83 unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
84 
90 coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
91  return ((num+1) << (szx + 4)) < data_len;
92 }
93 
94 #if 0
97 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
98  if (m)
99  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
100  else
101  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
102 }
103 #endif
104 
118 int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number,
119  coap_block_t *block);
120 
121 
137 int coap_get_block_b(const coap_session_t *session, const coap_pdu_t *pdu,
138  coap_option_num_t number, coap_block_b_t *block);
139 
161  coap_option_num_t number,
162  coap_pdu_t *pdu,
163  size_t data_length);
186  coap_block_b_t *block,
187  coap_option_num_t number,
188  coap_pdu_t *pdu,
189  size_t data_length);
190 
191 
204 int coap_add_block(coap_pdu_t *pdu,
205  size_t len,
206  const uint8_t *data,
207  unsigned int block_num,
208  unsigned char block_szx);
209 
220 int coap_add_block_b_data(coap_pdu_t *pdu, size_t len, const uint8_t *data,
221  coap_block_b_t *block);
222 
237 coap_block_build_body(coap_binary_t *body_data, size_t length,
238  const uint8_t *data, size_t offset, size_t total);
239 
259 void
261  coap_pdu_t *response,
262  uint16_t media_type,
263  int maxage,
264  size_t length,
265  const uint8_t* data);
266 
276 typedef void (*coap_release_large_data_t)(coap_session_t *session,
277  void *app_ptr);
278 
322  coap_pdu_t *pdu,
323  size_t length,
324  const uint8_t *data,
325  coap_release_large_data_t release_func,
326  void *app_ptr);
327 
378 int
380  coap_session_t *session,
381  const coap_pdu_t *request,
382  coap_pdu_t *response,
383  const coap_string_t *query,
384  uint16_t media_type,
385  int maxage,
386  uint64_t etag,
387  size_t length,
388  const uint8_t *data,
389  coap_release_large_data_t release_func,
390  void *app_ptr);
391 
411  uint8_t block_mode);
412 
427  coap_pdu_type_t message_type);
428 
431 #endif /* COAP_BLOCK_H_ */
Helpers for handling options in CoAP PDUs.
uint16_t coap_option_num_t
Definition: coap_option.h:20
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:26
Encoding and decoding of CoAP data types.
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.
Definition: block.c:364
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.
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: block.c:233
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: block.c:188
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: block.c:219
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.
Definition: block.c:1973
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: block.h:276
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: block.c:258
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: block.c:46
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: block.h:90
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: block.c:27
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:94
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: block.c:155
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.
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: coap_option.c:215
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: coap_option.c:252
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:60
#define COAP_STATIC_INLINE
Definition: libcoap.h:45
Pre-defined constants that reflect defaults for CoAP.
CoAP binary data definition.
Definition: str.h:56
Structure of Block options with BERT support.
Definition: block.h:51
unsigned int num
block number
Definition: block.h:52
uint32_t chunk_size
1024 if BERT
Definition: block.h:58
unsigned int bert
Operating as BERT.
Definition: block.h:57
unsigned int aszx
block size (0-7 including BERT
Definition: block.h:55
unsigned int defined
Set if block found.
Definition: block.h:56
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:53
unsigned int szx
block size (0-6)
Definition: block.h:54
Structure of Block options.
Definition: block.h:42
unsigned int num
block number
Definition: block.h:43
unsigned int szx
block size
Definition: block.h:45
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:44
The CoAP stack's global state is stored in a coap_context_t object.
structure for CoAP PDUs
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.
Definition: str.h:38