9 #include "coap_config.h" 11 #if defined(HAVE_ASSERT_H) && !defined(assert) 18 #if (COAP_MAX_PDU_SIZE - 6) < (1 << (COAP_MAX_BLOCK_SZX + 4)) 19 #error "COAP_MAX_BLOCK_SZX too large" 22 #define min(a,b) ((a) < (b) ? (a) : (b)) 75 size_t start, want, avail;
80 start = block->
num << (block->
szx + 4);
81 if (data_length <= start) {
82 debug(
"illegal block requested\n");
87 want = 1 << (block->
szx + 4);
91 block->
m = want < data_length - start;
98 if (data_length - start <= avail) {
107 debug(
"not enough space, even the smallest block does not fit");
110 debug(
"decrease block size for %zu to %d\n", avail,
coap_fls(avail) - 5);
114 block->
num <<= szx - block->
szx;
129 unsigned int block_num,
unsigned char block_szx) {
131 start = block_num << (block_szx + 4);
137 min(len - start, (
unsigned int)(1 << (block_szx + 4))),
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
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.
int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *pdu, size_t data_length)
Writes a block option of type type to message pdu.
unsigned short length
PDU length (including header, options, data)
int coap_fls(unsigned int i)
Header structure for CoAP PDUs.
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
#define COAP_OPT_BLOCK_LAST(opt)
Returns the value of the least significant byte of a Block option opt.
size_t max_size
allocated storage for options and data
int coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *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.
Iterator to run through PDU options.
size_t coap_add_option(coap_pdu_t *pdu, unsigned short type, unsigned int len, const unsigned char *data)
de-duplicate code with coap_add_option_later
int coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data)
Adds given data to the pdu that is passed as first parameter.
Structure of Block options.
#define COAP_OPT_LENGTH(opt)
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
unsigned char coap_opt_t
Use byte-oriented access methods here because sliding a complex struct coap_opt_t over the data buffe...
coap_opt_t * coap_check_option(coap_pdu_t *pdu, unsigned short type, coap_opt_iterator_t *oi)
Retrieves the first option of type type from pdu.
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len)
Decodes multiple-length byte sequences.
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
#define COAP_OPT_VALUE(opt)
unsigned int m
1 if more blocks follow, 0 otherwise
unsigned int szx
block size
unsigned int num
block number