libcoap  4.1.2
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 
22 #ifndef COAP_MAX_BLOCK_SZX
23 
27 #define COAP_MAX_BLOCK_SZX 4
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 
64 static inline int
65 coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) {
66  return ((num+1) << (szx + 4)) < data_len;
67 }
68 
70 static inline void
71 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
72  if (m)
73  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) |= 0x08;
74  else
75  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) &= ~0x08;
76 }
77 
91 int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block);
92 
114  unsigned short type,
115  coap_pdu_t *pdu,
116  size_t data_length);
117 
130 int coap_add_block(coap_pdu_t *pdu,
131  unsigned int len,
132  const unsigned char *data,
133  unsigned int block_num,
134  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:45
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
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
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
Helpers for handling options in CoAP PDUs.
Header structure for CoAP PDUs.
Definition: pdu.h:227
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
Pre-defined constants that reflect defaults for CoAP.
Structure of Block options.
Definition: block.h:33
#define COAP_OPT_LENGTH(opt)
Definition: option.h:392
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
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
#define COAP_OPT_VALUE(opt)
Definition: option.h:406
unsigned int num
block number
Definition: block.h:34