libcoap  4.3.0beta
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  * This file is part of the CoAP library libcoap. Please see README for terms
7  * of use.
8  */
9 
10 #ifndef COAP_BLOCK_H_
11 #define COAP_BLOCK_H_
12 
13 #include "encode.h"
14 #include "option.h"
15 #include "pdu.h"
16 
23 #ifndef COAP_MAX_BLOCK_SZX
27 #define COAP_MAX_BLOCK_SZX 6
28 #endif /* COAP_MAX_BLOCK_SZX */
29 
33 typedef struct {
34  unsigned int num;
35  unsigned int m:1;
36  unsigned int szx:3;
37 } coap_block_t;
38 
44 #define COAP_OPT_BLOCK_LAST(opt) \
45  (coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
46 
48 #define COAP_OPT_BLOCK_MORE(opt) \
49  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
50 
52 #define COAP_OPT_BLOCK_SZX(opt) \
53  (coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
54 
58 unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
59 
65 coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
66  return ((num+1) << (szx + 4)) < data_len;
67 }
68 
69 #if 0
72 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
73  if (m)
74  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
75  else
76  *(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
77 }
78 #endif
79 
93 int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block);
94 
116  uint16_t type,
117  coap_pdu_t *pdu,
118  size_t data_length);
119 
132 int coap_add_block(coap_pdu_t *pdu,
133  size_t len,
134  const uint8_t *data,
135  unsigned int block_num,
136  unsigned char block_szx);
137 
157 void
159  struct coap_session_t *session,
160  coap_pdu_t *request,
161  coap_pdu_t *response,
162  const coap_binary_t *token,
163  uint16_t media_type,
164  int maxage,
165  size_t length,
166  const uint8_t* data);
167 
170 #endif /* COAP_BLOCK_H_ */
int coap_write_block_opt(coap_block_t *block, uint16_t type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
Definition: block.c:64
void coap_add_data_blocked_response(struct coap_resource_t *resource, struct coap_session_t *session, coap_pdu_t *request, coap_pdu_t *response, const coap_binary_t *token, 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:142
int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:36
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:125
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:65
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:17
uint32_t coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:209
const uint8_t * coap_opt_value(const coap_opt_t *opt)
Returns a pointer to the value of the given option.
Definition: option.c:246
#define COAP_STATIC_INLINE
Definition: libcoap.h:38
const uint8_t m
Definition: murmur3.c:55
Helpers for handling options in CoAP PDUs.
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:25
Pre-defined constants that reflect defaults for CoAP.
CoAP binary data definition.
Definition: str.h:48
Structure of Block options.
Definition: block.h:33
unsigned int num
block number
Definition: block.h:34
unsigned int szx
block size
Definition: block.h:36
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:35
structure for CoAP PDUs token, if any, follows the fixed size header, then options until payload mark...
Definition: pdu.h:287
unsigned char uint8_t
Definition: uthash.h:79