block.h
Go to the documentation of this file.
/*
* block.h -- block transfer
*
* Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef _COAP_BLOCK_H_
#define _COAP_BLOCK_H_
#include "encode.h"
#include "option.h"
#include "pdu.h"
#ifndef COAP_MAX_BLOCK_SZX
#define COAP_MAX_BLOCK_SZX 4
#endif /* COAP_MAX_BLOCK_SZX */
typedef struct {
unsigned int num;
unsigned int m:1;
unsigned int szx:3;
} coap_block_t;
#define COAP_OPT_BLOCK_LAST(opt) \
(COAP_OPT_LENGTH(opt) ? (COAP_OPT_VALUE(opt) + (COAP_OPT_LENGTH(opt)-1)) : 0)
#define COAP_OPT_BLOCK_MORE(opt) \
(COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
#define COAP_OPT_BLOCK_SZX(opt) \
(COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
static inline int
coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) {
return ((num+1) << (szx + 4)) < data_len;
}
static inline void
coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
if (m)
*(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) |= 0x08;
else
*(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) &= ~0x08;
}
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block);
int coap_write_block_opt(coap_block_t *block,
unsigned short type,
coap_pdu_t *pdu,
size_t data_length);
int coap_add_block(coap_pdu_t *pdu,
unsigned int len,
const unsigned char *data,
unsigned int block_num,
unsigned char block_szx);
#endif /* _COAP_BLOCK_H_ */
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
encode.h
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
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
coap_more_blocks
static int coap_more_blocks(size_t data_len, unsigned int num, unsigned short 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
option.h
Helpers for handling options in CoAP PDUs.
coap_pdu_t
Header structure for CoAP PDUs.
Definition: pdu.h:227
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
pdu.h
Pre-defined constants that reflect defaults for CoAP.
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_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_opt_block_set_m
static void coap_opt_block_set_m(coap_opt_t *block_opt, int m)
Sets the More-bit in block_opt.
Definition: block.h:71
COAP_OPT_VALUE
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
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