libcoap  4.0.3
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
block.h
Go to the documentation of this file.
1 /* block.h -- block transfer
2  *
3  * Copyright (C) 2010--2012 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 
24 typedef struct {
25  unsigned int num:20;
26  unsigned int m:1;
27  unsigned int szx:3;
28 } coap_block_t;
29 
31 #define COAP_OPT_BLOCK_LAST(opt) ( COAP_OPT_VALUE(opt) + (COAP_OPT_LENGTH(opt) - 1) )
32 
34 #define COAP_OPT_BLOCK_MORE(opt) ( *COAP_OPT_BLOCK_LAST(opt) & 0x08 )
35 
37 #define COAP_OPT_BLOCK_SZX(opt) ( *COAP_OPT_BLOCK_LAST(opt) & 0x07 )
38 
40 static inline unsigned int
41 _coap_block_num_impl(const coap_opt_t *block_opt) {
42  unsigned int num = 0;
43 
44  if (COAP_OPT_LENGTH(block_opt) > 1)
45  num = coap_decode_var_bytes(COAP_OPT_VALUE(block_opt),
46  COAP_OPT_LENGTH(block_opt) - 1);
47 
48  return (num << 4) | ((*COAP_OPT_BLOCK_LAST(block_opt) & 0xF0) >> 4);
49 }
50 
52 #define COAP_OPT_BLOCK_NUM(Block) _coap_block_num_impl(Block)
53 
58 static inline int
59 coap_more_blocks(size_t data_len, unsigned int num, unsigned short szx) {
60  return ((num+1) << (szx + 4)) < data_len;
61 }
62 
64 static inline void
65 coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
66  if (m)
67  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) |= 0x08;
68  else
69  *(COAP_OPT_VALUE(block_opt) + (COAP_OPT_LENGTH(block_opt) - 1)) &= ~0x08;
70 }
71 
84 int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block);
85 
108 int coap_write_block_opt(coap_block_t *block, unsigned short type,
109  coap_pdu_t *pdu, size_t data_length);
110 
122 int coap_add_block(coap_pdu_t *pdu, unsigned int len, const unsigned char *data,
123  unsigned int block_num, unsigned char block_szx);
126 #endif /* _COAP_BLOCK_H_ */