libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
block.h
Go to the documentation of this file.
1 /* block.h -- block transfer
2  *
3  * Copyright (C) 2010--2012,2014 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #ifndef _COAP_BLOCK_H_
10 #define _COAP_BLOCK_H_
11 
12 #include "option.h"
13 #include "encode.h"
14 #include "pdu.h"
15 
21 #ifndef COAP_MAX_BLOCK_SZX
22 
27 #define COAP_MAX_BLOCK_SZX 4
28 #endif /* COAP_MAX_BLOCK_SZX */
29 
30 #if (COAP_MAX_PDU_SIZE - 6) < (1 << (COAP_MAX_BLOCK_SZX + 4))
31 #error "COAP_MAX_BLOCK_SZX too large"
32 #endif
33 
37 typedef struct {
38  unsigned int num:20;
39  unsigned int m:1;
40  unsigned int szx:3;
41 } coap_block_t;
42 
48 #define COAP_OPT_BLOCK_LAST(opt) \
49  (COAP_OPT_LENGTH(opt) ? (COAP_OPT_VALUE(opt) + (COAP_OPT_LENGTH(opt)-1)) : 0)
50 
52 #define COAP_OPT_BLOCK_MORE(opt) \
53  (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
54 
56 #define COAP_OPT_BLOCK_SZX(opt) \
57  (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
58 
63 unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
64 
69 static inline int
70 coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) {
71  return ((num+1) << (szx + 4)) < data_len;
72 }
73 
75 static inline void
76 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
77  if (m)
78  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) |= 0x08;
79  else
80  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) &= ~0x08;
81 }
82 
95 int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block);
96 
119 int coap_write_block_opt(coap_block_t *block, unsigned short type,
120  coap_pdu_t *pdu, size_t data_length);
121 
133 int coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
134  unsigned int block_num, unsigned char block_szx);
137 #endif /* _COAP_BLOCK_H_ */
int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block)
Initializes block from pdu.
Definition: block.c:41
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:22
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:60
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:70
helpers for handling options in CoAP PDUs
coap_block_t block
Definition: client.c:53
Header structure for CoAP PDUs.
Definition: pdu.h:206
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:121
Structure of Block options.
Definition: block.h:37
#define COAP_OPT_LENGTH(opt)
Definition: option.h:305
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:26
static void coap_opt_block_set_m(coap_opt_t *block_opt, int m)
Sets the More-bit in block_opt.
Definition: block.h:76
#define COAP_OPT_VALUE(opt)
Definition: option.h:318