libcoap  4.3.0
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  *
6  * SPDX-License-Identifier: BSD-2-Clause
7  *
8  * This file is part of the CoAP library libcoap. Please see README for terms
9  * of use.
10  */
11 
12 #ifndef COAP_BLOCK_H_
13 #define COAP_BLOCK_H_
14 
15 #include "encode.h"
16 #include "option.h"
17 #include "pdu.h"
18 
25 #ifndef COAP_MAX_BLOCK_SZX
29 #define COAP_MAX_BLOCK_SZX 6
30 #endif /* COAP_MAX_BLOCK_SZX */
31 
35 typedef struct {
36  unsigned int num;
37  unsigned int m:1;
38  unsigned int szx:3;
39 } coap_block_t;
40 
41 #define COAP_BLOCK_USE_LIBCOAP 0x01 /* Use libcoap to do block requests */
42 #define COAP_BLOCK_SINGLE_BODY 0x02 /* Deliver the data as a single body */
43 
49 #define COAP_OPT_BLOCK_LAST(opt) \
50  (coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
51 
53 #define COAP_OPT_BLOCK_MORE(opt) \
54  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
55 
57 #define COAP_OPT_BLOCK_SZX(opt) \
58  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
59 
63 unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
64 
70 coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
71  return ((num+1) << (szx + 4)) < data_len;
72 }
73 
74 #if 0
77 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
78  if (m)
79  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
80  else
81  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
82 }
83 #endif
84 
98 int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number,
99  coap_block_t *block);
100 
122  coap_option_num_t number,
123  coap_pdu_t *pdu,
124  size_t data_length);
125 
138 int coap_add_block(coap_pdu_t *pdu,
139  size_t len,
140  const uint8_t *data,
141  unsigned int block_num,
142  unsigned char block_szx);
143 
158 coap_block_build_body(coap_binary_t *body_data, size_t length,
159  const uint8_t *data, size_t offset, size_t total);
160 
180 void
182  coap_pdu_t *response,
183  uint16_t media_type,
184  int maxage,
185  size_t length,
186  const uint8_t* data);
187 
197 typedef void (*coap_release_large_data_t)(coap_session_t *session,
198  void *app_ptr);
199 
241  coap_pdu_t *pdu,
242  size_t length,
243  const uint8_t *data,
244  coap_release_large_data_t release_func,
245  void *app_ptr);
246 
295 int
297  coap_session_t *session,
298  const coap_pdu_t *request,
299  coap_pdu_t *response,
300  const coap_string_t *query,
301  uint16_t media_type,
302  int maxage,
303  uint64_t etag,
304  size_t length,
305  const uint8_t *data,
306  coap_release_large_data_t release_func,
307  void *app_ptr);
308 
328  uint8_t block_mode);
329 
344  coap_pdu_type_t message_type);
345 
348 #endif /* COAP_BLOCK_H_ */
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:249
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.
Definition: block.c:627
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:127
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:1559
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:197
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:144
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:70
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:18
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:37
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:66
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.
Definition: block.c:279
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.
Definition: block.c:638
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:211
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:248
coap_pdu_type_t
CoAP PDU message type definitions.
Definition: pdu.h:56
#define COAP_STATIC_INLINE
Definition: libcoap.h:40
const uint8_t m
Definition: murmur3.c:55
Helpers for handling options in CoAP PDUs.
uint16_t coap_option_num_t
Definition: 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: option.h:26
Pre-defined constants that reflect defaults for CoAP.
CoAP binary data definition.
Definition: str.h:50
Structure of Block options.
Definition: block.h:35
unsigned int num
block number
Definition: block.h:36
unsigned int szx
block size
Definition: block.h:38
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:37
The CoAP stack's global state is stored in a coap_context_t object.
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
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:32