mikroSDK Reference Manual
coap_option.h
Go to the documentation of this file.
1
31#ifndef _COAP_OPTION_H
32#define _COAP_OPTION_H
33
34//Dependencies
35#include "core/net.h"
36#include "coap/coap_common.h"
37#include "coap/coap_message.h"
38
39//Option delta encoding
40#define COAP_OPT_DELTA_8_BITS 13
41#define COAP_OPT_DELTA_16_BITS 14
42#define COAP_OPT_DELTA_RESERVED 15
43#define COAP_OPT_DELTA_MINUS_8_BITS 13
44#define COAP_OPT_DELTA_MINUS_16_BITS 269
45
46//Option length encoding
47#define COAP_OPT_LEN_8_BITS 13
48#define COAP_OPT_LEN_16_BITS 14
49#define COAP_OPT_LEN_RESERVED 15
50#define COAP_OPT_LEN_MINUS_8_BITS 13
51#define COAP_OPT_LEN_MINUS_16_BITS 269
52
53//Default Max-Age option value
54#define COAP_DEFAULT_MAX_AGE 60
55
56//Test whether an option is critical
57#define COAP_IS_OPTION_CRITICAL(num) (((num) & 0x01U) ? TRUE : FALSE)
58//Test whether an option is unsafe to forward
59#define COAP_IS_OPTION_UNSAFE(num) (((num) & 0x02U) ? TRUE : FALSE)
60
61//Set block number
62#define COAP_SET_BLOCK_NUM(value, n) value = ((value) & 0x0FU) | ((n) << 4U)
63//Set More flag
64#define COAP_SET_BLOCK_M(value, m) value = ((value) & ~0x08U) | (((m) << 3U) & 0x08U)
65//Set block size
66#define COAP_SET_BLOCK_SZX(value, s) value = ((value) & ~0x07U) | ((s) & 0x07U)
67
68//Get block number
69#define COAP_GET_BLOCK_NUM(value) ((value) >> 4U)
70//Get More flag
71#define COAP_GET_BLOCK_M(value) (((value) >> 3U) & 0x01U)
72//Get block size
73#define COAP_GET_BLOCK_SZX(value) ((value) & 0x07U)
74
75//Get block size (in bytes)
76#define COAP_GET_BLOCK_SIZE(value) (16U << ((value) & 0x07U))
77//Get block position from the beginning of the resource (in bytes)
78#define COAP_GET_BLOCK_POS(value) (((value) & ~0x0FU) << ((value) & 0x07U))
79
80//C++ guard
81#ifdef __cplusplus
82extern "C" {
83#endif
84
85
90typedef enum
91{
92 COAP_OPT_IF_MATCH = 1, //RFC 7252
93 COAP_OPT_URI_HOST = 3, //RFC 7252
94 COAP_OPT_ETAG = 4, //RFC 7252
95 COAP_OPT_IF_NONE_MATCH = 5, //RFC 7252
96 COAP_OPT_OBSERVE = 6, //RFC 7641
97 COAP_OPT_URI_PORT = 7, //RFC 7252
98 COAP_OPT_LOCATION_PATH = 8, //RFC 7252
99 COAP_OPT_URI_PATH = 11, //RFC 7252
100 COAP_OPT_CONTENT_FORMAT = 12, //RFC 7252
101 COAP_OPT_MAX_AGE = 14, //RFC 7252
102 COAP_OPT_URI_QUERY = 15, //RFC 7252
103 COAP_OPT_ACCEPT = 17, //RFC 7252
104 COAP_OPT_LOCATION_QUERY = 20, //RFC 7252
105 COAP_OPT_BLOCK2 = 23, //RFC 7959
106 COAP_OPT_BLOCK1 = 27, //RFC 7959
107 COAP_OPT_SIZE2 = 28, //RFC 7959
108 COAP_OPT_PROXY_URI = 35, //RFC 7252
109 COAP_OPT_PROXY_SCHEME = 39, //RFC 7252
110 COAP_OPT_SIZE1 = 60, //RFC 7252
111 COAP_OPT_ECHO = 252, //RFC 9175
112 COAP_OPT_NO_RESPONSE = 258, //RFC 7967
113 COAP_OPT_REQUEST_TAG = 292 //RFC 9175
115
116
128
129
134typedef enum
135{
136 COAP_OBSERVE_REGISTER = 0,
137 COAP_OBSERVE_DEREGISTER = 1
139
140
145typedef enum
146{
147 COAP_CONTENT_FORMAT_TEXT_PLAIN = 0,
148 COAP_CONTENT_FORMAT_APP_COSE_ENCRYPT0 = 16,
149 COAP_CONTENT_FORMAT_APP_COSE_MAC0 = 17,
150 COAP_CONTENT_FORMAT_APP_COSE_SIGN1 = 18,
151 COAP_CONTENT_FORMAT_APP_LINK_FORMAT = 40,
152 COAP_CONTENT_FORMAT_APP_XML = 41,
153 COAP_CONTENT_FORMAT_APP_OCTET_STREAM = 42,
154 COAP_CONTENT_FORMAT_APP_EXI = 47,
155 COAP_CONTENT_FORMAT_APP_JSON = 50,
156 COAP_CONTENT_FORMAT_APP_JSON_PATCH_JSON = 51,
157 COAP_CONTENT_FORMAT_APP_MERGE_PATCH_JSON = 52,
158 COAP_CONTENT_FORMAT_APP_CBOR = 60,
159 COAP_CONTENT_FORMAT_APP_CWT = 61,
160 COAP_CONTENT_FORMAT_APP_MULTIPART_CORE = 62,
161 COAP_CONTENT_FORMAT_APP_CBOR_SEQ = 63,
162 COAP_CONTENT_FORMAT_APP_COSE_ENCRYPT = 96,
163 COAP_CONTENT_FORMAT_APP_COSE_MAC = 97,
164 COAP_CONTENT_FORMAT_APP_COSE_SIGN = 98,
165 COAP_CONTENT_FORMAT_APP_COSE_KEY = 101,
166 COAP_CONTENT_FORMAT_APP_COSE_KEY_SET = 102,
167 COAP_CONTENT_FORMAT_APP_SENML_JSON = 110,
168 COAP_CONTENT_FORMAT_APP_SENSML_JSON = 111,
169 COAP_CONTENT_FORMAT_APP_SENML_CBOR = 112,
170 COAP_CONTENT_FORMAT_APP_SENSML_CBOR = 113,
171 COAP_CONTENT_FORMAT_APP_SENML_EXI = 114,
172 COAP_CONTENT_FORMAT_APP_SENSML_EXI = 115,
173 COAP_CONTENT_FORMAT_APP_COAP_GROUP_JSON = 256,
174 COAP_CONTENT_FORMAT_APP_PKCS7_MIME_SERVER_KEY = 280,
175 COAP_CONTENT_FORMAT_APP_PKCS7_MIME_CERTS_ONLY = 281,
176 COAP_CONTENT_FORMAT_APP_PKCS7_MIME_CMC_REQ = 282,
177 COAP_CONTENT_FORMAT_APP_PKCS7_MIME_CMC_RESP = 283,
178 COAP_CONTENT_FORMAT_APP_PKCS8 = 284,
179 COAP_CONTENT_FORMAT_APP_CSRATTRS = 285,
180 COAP_CONTENT_FORMAT_APP_PKCS10 = 286,
181 COAP_CONTENT_FORMAT_APP_PKIX_CERT = 287,
182 COAP_CONTENT_FORMAT_APP_SENML_XML = 310,
183 COAP_CONTENT_FORMAT_APP_SENSML_XML = 311,
184 COAP_CONTENT_FORMAT_SENML_ETCH_JSON = 320,
185 COAP_CONTENT_FORMAT_SENML_ETCH_CBOR = 322,
186 COAP_CONTENT_FORMAT_APP_VND_OCF_CBOR = 10000,
187 COAP_CONTENT_FORMAT_APP_OSCORE = 10001,
188 COAP_CONTENT_FORMAT_APP_JSON_DEFLATE = 11050,
189 COAP_CONTENT_FORMAT_APP_CBOR_DEFLATE = 11060,
190 COAP_CONTENT_FORMAT_APP_VND_OMA_LWM2M_TLV = 11542,
191 COAP_CONTENT_FORMAT_APP_VND_OMA_LWM2M_JSON = 11543
193
194
199typedef enum
200{
201 COAP_BLOCK_SIZE_16 = 0,
202 COAP_BLOCK_SIZE_32 = 1,
203 COAP_BLOCK_SIZE_64 = 2,
204 COAP_BLOCK_SIZE_128 = 3,
205 COAP_BLOCK_SIZE_256 = 4,
206 COAP_BLOCK_SIZE_512 = 5,
207 COAP_BLOCK_SIZE_1024 = 6,
208 COAP_BLOCK_SIZE_RESERVED = 7,
210
211
216typedef struct
217{
218 uint16_t delta;
219 uint16_t number;
220 size_t length;
221 const uint8_t *value;
222} CoapOption;
223
224
229typedef struct
230{
231 uint16_t number;
232 bool_t critical;
233 bool_t unsafe;
234 bool_t noCacheKey;
235 bool_t repeatable;
236 const char_t *name;
238 uint16_t minLength;
239 uint16_t maxLength;
241
242
243//CoAP option related functions
244error_t coapParseOptions(const uint8_t *p, size_t length, size_t *consumed);
245
246error_t coapParseOption(const uint8_t *p, size_t length,
247 uint16_t prevOptionNum, CoapOption *option, size_t *consumed);
248
249error_t coapFormatOption(uint8_t *p, uint16_t prevOptionNum,
250 CoapOption *option, size_t *written);
251
252error_t coapSetOption(CoapMessage *message, uint16_t optionNum,
253 uint_t optionIndex, const uint8_t *optionValue, size_t optionLen);
254
255error_t coapSetUintOption(CoapMessage *message, uint16_t optionNum,
256 uint_t optionIndex, uint32_t optionValue);
257
258error_t coapGetOption(const CoapMessage *message, uint16_t optionNum,
259 uint_t optionIndex, const uint8_t **optionValue, size_t *optionLen);
260
261error_t coapGetUintOption(const CoapMessage *message, uint16_t optionNum,
262 uint_t optionIndex, uint32_t *optionValue);
263
264error_t coapDeleteOption(CoapMessage *message, uint16_t optionNum,
265 uint_t optionIndex);
266
267error_t coapSplitRepeatableOption(CoapMessage *message, uint16_t optionNum,
268 const char_t *optionValue, char_t separator);
269
270error_t coapJoinRepeatableOption(const CoapMessage *message,
271 uint16_t optionNum, char_t *optionValue, size_t maxLen, char_t separator);
272
273const CoapOptionParameters *coapGetOptionParameters(uint16_t optionNum);
274
275//C++ guard
276#ifdef __cplusplus
277}
278#endif
279
280#endif
Definitions common to CoAP client and server.
CoAP message formatting and parsing.
CoapOptionFormat
CoAP option formats.
Definition coap_option.h:122
@ COAP_OPT_FORMAT_EMPTY
Zero-length sequence of bytes.
Definition coap_option.h:123
@ COAP_OPT_FORMAT_OPAQUE
Opaque sequence of bytes.
Definition coap_option.h:124
@ COAP_OPT_FORMAT_UINT
Non-negative integer.
Definition coap_option.h:125
@ COAP_OPT_FORMAT_STRING
UTF-8 string.
Definition coap_option.h:126
CoapObserveOption
Observe option.
Definition coap_option.h:135
CoapOptionNumber
CoAP options.
Definition coap_option.h:91
CoapBlockSize
Block size parameter.
Definition coap_option.h:200
CoapContentFormat
Content-Format option.
Definition coap_option.h:146
error_t
Error codes.
Definition error.h:43
TCP/IP stack core.
CoAP message.
Definition coap_message.h:56
CoAP option parameters.
Definition coap_option.h:230
uint16_t number
Option number.
Definition coap_option.h:231
const char_t * name
Option name.
Definition coap_option.h:236
bool_t noCacheKey
NoCacheKey property.
Definition coap_option.h:234
uint16_t minLength
Minimum acceptable length.
Definition coap_option.h:238
uint16_t maxLength
Maximum acceptable length.
Definition coap_option.h:239
bool_t critical
Critical property.
Definition coap_option.h:232
bool_t repeatable
Repeatable option.
Definition coap_option.h:235
bool_t unsafe
Unsafe property.
Definition coap_option.h:233
CoapOptionFormat format
Option format.
Definition coap_option.h:237
CoAP option.
Definition coap_option.h:217