283ba69bb9215e4781da916ca3778c7d2cff31a4
[platform/upstream/iotivity.git] / csdk / libcoap-4.1.1 / block.h
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
16 /**
17  * @defgroup block Block Transfer
18  * @{
19  */
20
21 #ifndef COAP_MAX_BLOCK_SZX
22 /**
23  * The largest value for the SZX component in a Block option. Note
24  * that 1 << (COAP_MAX_BLOCK_SZX + 4) should not exceed
25  * COAP_MAX_PDU_SIZE.
26  */
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
34 /**
35  * Structure of Block options. 
36  */
37 typedef struct {
38   unsigned int num:20;          /**< block number */
39   unsigned int m:1;             /**< 1 if more blocks follow, 0 otherwise */
40   unsigned int szx:3;           /**< block size */
41 } coap_block_t;
42
43 /**
44  * Returns the value of the least significant byte of a Block option @p opt. 
45  * For zero-length options (i.e. num == m == szx == 0), COAP_OPT_BLOCK_LAST
46  * returns @c NULL.
47  */
48 #define COAP_OPT_BLOCK_LAST(opt) \
49   (COAP_OPT_LENGTH(opt) ? (COAP_OPT_VALUE(opt) + (COAP_OPT_LENGTH(opt)-1)) : 0)
50
51 /** Returns the value of the More-bit of a Block option @p opt. */
52 #define COAP_OPT_BLOCK_MORE(opt) \
53   (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
54
55 /** Returns the value of the SZX-field of a Block option @p opt. */
56 #define COAP_OPT_BLOCK_SZX(opt)  \
57   (COAP_OPT_LENGTH(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
58
59 /**
60  * Returns the value of field @c num in the given block option @p
61  * block_opt.
62  */
63 unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
64
65 /**
66  * Checks if more than @p num blocks are required to deliver @p data_len
67  * bytes of data for a block size of 1 << (@p szx + 4).
68  */
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
74 /** Sets the More-bit in @p block_opt */
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
83 /**
84  * Initializes @p block from @p pdu. @p type must be either COAP_OPTION_BLOCK1
85  * or COAP_OPTION_BLOCK2. When option @p type was found in @p pdu, @p block
86  * is initialized with values from this option and the function returns the
87  * value @c 1. Otherwise, @c 0 is returned.
88  *
89  * @param pdu   The pdu to search for option @p type.
90  * @param type  The option to search for (must be COAP_OPTION_BLOCK1 or 
91  *              COAP_OPTION_BLOCK2)
92  * @param block The block structure to initilize.
93  * @return @c 1 on success, @c 0 otherwise.
94  */
95 int coap_get_block(coap_pdu_t *pdu, unsigned short type, coap_block_t *block);
96
97 /**
98  * Writes a block option of type @p type to message @p pdu. If the
99  * requested block size is too large to fit in @p pdu, it is reduced
100  * accordingly. An exception is made for the final block when less
101  * space is required. The actual length of the resource is specified
102  * in @p data_length.
103  *
104  * This function may change *block to reflect the values written to 
105  * @p pdu. As the function takes into consideration the remaining space
106  * @p pdu, no more options should be added after coap_write_block_opt() 
107  * has returned.
108  *
109  * @param block      The block structure to use. On return, this object
110  *                   is updated according to the values that have been
111  *                   written to @p pdu.
112  * @param type       COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2
113  * @param pdu        The message where the block option should be
114  *                   written.
115  * @param data_length The length of the actual data that will be added
116  *                   the @p pdu by calling coap_add_block().
117  * @return @c 1 on success, or a negative value on error.
118  */
119 int coap_write_block_opt(coap_block_t *block, unsigned short type,
120                          coap_pdu_t *pdu, size_t data_length);
121
122 /** 
123  * Adds the @p block_num block of size 1 << (@p block_szx + 4) from
124  * source @p data to @p pdu.
125  *
126  * @param pdu    The message to add the block
127  * @param len    The length of @p data.
128  * @param data   The source data to fill the block with
129  * @param block_num The actual block number
130  * @param block_szx Encoded size of block @p block_number
131  * @return @c 1 on success, @c 0 otherwise.
132  */
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);
135 /**@}*/
136
137 #endif /* _COAP_BLOCK_H_ */