block.c
Go to the documentation of this file.
/* block.c -- block transfer
*
* Copyright (C) 2010--2012,2015-2016 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#include "coap_config.h"
#if defined(HAVE_ASSERT_H) && !defined(assert)
# include <assert.h>
#endif
#include "debug.h"
#include "block.h"
#if (COAP_MAX_PDU_SIZE - 6) < (1 << (COAP_MAX_BLOCK_SZX + 4))
#error "COAP_MAX_BLOCK_SZX too large"
#endif
#define min(a,b) ((a) < (b) ? (a) : (b))
#ifndef WITHOUT_BLOCK
unsigned int
coap_opt_block_num(const coap_opt_t *block_opt) {
unsigned int num = 0;
unsigned short len;
len = coap_opt_length(block_opt);
if (len == 0) {
return 0;
}
if (len > 1) {
num = coap_decode_var_bytes(COAP_OPT_VALUE(block_opt),
COAP_OPT_LENGTH(block_opt) - 1);
}
return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4);
}
int
coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block) {
coap_opt_iterator_t opt_iter;
coap_opt_t *option;
assert(block);
memset(block, 0, sizeof(coap_block_t));
if (pdu && (option = coap_check_option(pdu, type, &opt_iter))) {
unsigned int num;
block->szx = COAP_OPT_BLOCK_SZX(option);
if (COAP_OPT_BLOCK_MORE(option))
block->m = 1;
/* The block number is at most 20 bits, so values above 2^20 - 1
* are illegal. */
num = coap_opt_block_num(option);
if (num > 0xFFFFF) {
return 0;
}
block->num = num;
return 1;
}
return 0;
}
int
coap_write_block_opt(coap_block_t *block, unsigned short type,
coap_pdu_t *pdu, size_t data_length) {
size_t start, want, avail;
unsigned char buf[4];
assert(pdu);
start = block->num << (block->szx + 4);
if (data_length <= start) {
debug("illegal block requested\n");
return -2;
}
avail = pdu->max_size - pdu->length - 4;
want = 1 << (block->szx + 4);
/* check if entire block fits in message */
if (want <= avail) {
block->m = want < data_length - start;
} else {
/* Sender has requested a block that is larger than the remaining
* space in pdu. This is ok if the remaining data fits into the pdu
* anyway. The block size needs to be adjusted only if there is more
* data left that cannot be delivered in this message. */
if (data_length - start <= avail) {
/* it's the final block and everything fits in the message */
block->m = 0;
} else {
unsigned char szx;
/* we need to decrease the block size */
if (avail < 16) { /* bad luck, this is the smallest block size */
debug("not enough space, even the smallest block does not fit");
return -3;
}
debug("decrease block size for %zu to %d\n", avail, coap_fls(avail) - 5);
szx = block->szx;
block->szx = coap_fls(avail) - 5;
block->m = 1;
block->num <<= szx - block->szx;
}
}
/* to re-encode the block option */
coap_add_option(pdu, type, coap_encode_var_bytes(buf, ((block->num << 4) |
(block->m << 3) |
block->szx)),
buf);
return 1;
}
int
coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
unsigned int block_num, unsigned char block_szx) {
size_t start;
start = block_num << (block_szx + 4);
if (len <= start)
return 0;
return coap_add_data(pdu,
min(len - start, (unsigned int)(1 << (block_szx + 4))),
data + start);
}
#endif /* WITHOUT_BLOCK */
coap_get_block
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:45
coap_opt_block_num
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:26
min
#define min(a, b)
Definition: block.c:22
coap_write_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.
Definition: block.c:73
debug.h
coap_pdu_t::length
unsigned short length
PDU length (including header, options, data)
Definition: pdu.h:234
coap_fls
int coap_fls(unsigned int i)
Definition: encode.c:17
debug
#define debug(...)
Definition: debug.h:66
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
assert
#define assert(...)
Definition: mem.c:17
coap_encode_var_bytes
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:34
COAP_OPT_BLOCK_LAST
#define COAP_OPT_BLOCK_LAST(opt)
Returns the value of the least significant byte of a Block option opt.
Definition: block.h:44
coap_pdu_t::max_size
size_t max_size
allocated storage for options and data
Definition: pdu.h:228
coap_add_block
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.
Definition: block.c:128
coap_opt_iterator_t
Iterator to run through PDU options.
Definition: option.h:253
coap_add_option
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
Definition: pdu.c:171
coap_add_data
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.
Definition: pdu.c:234
coap_block_t
Structure of Block options.
Definition: block.h:33
COAP_OPT_LENGTH
#define COAP_OPT_LENGTH(opt)
Definition: option.h:392
COAP_OPT_BLOCK_SZX
#define COAP_OPT_BLOCK_SZX(opt)
Returns the value of the SZX-field of a Block option opt.
Definition: block.h:52
coap_opt_t
unsigned char 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
coap_check_option
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.
Definition: option.c:209
COAP_OPT_BLOCK_MORE
#define COAP_OPT_BLOCK_MORE(opt)
Returns the value of the More-bit of a Block option opt.
Definition: block.h:48
coap_decode_var_bytes
unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len)
Decodes multiple-length byte sequences.
Definition: encode.c:25
coap_opt_length
unsigned short coap_opt_length(const coap_opt_t *opt)
Returns the length of the given option.
Definition: option.c:251
COAP_OPT_VALUE
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
block.h
coap_block_t::m
unsigned int m
1 if more blocks follow, 0 otherwise
Definition: block.h:35
coap_block_t::szx
unsigned int szx
block size
Definition: block.h:36
coap_block_t::num
unsigned int num
block number
Definition: block.h:34