coap_common.h
Go to the documentation of this file.
1 /**
2  * @file coap_common.h
3  * @brief Definitions common to CoAP client and server
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _COAP_COMMON_H
30 #define _COAP_COMMON_H
31 
32 //Dependencies
33 #include "core/net.h"
34 
35 //Maximum size of CoAP messages
36 #ifndef COAP_MAX_MSG_SIZE
37  #define COAP_MAX_MSG_SIZE 1024
38 #elif (COAP_MAX_MSG_SIZE < 16)
39  #error COAP_MAX_MSG_SIZE parameter is not valid
40 #endif
41 
42 //CoAP port number
43 #define COAP_PORT 5683
44 //DTLS-secured CoAP port number
45 #define COAPS_PORT 5684
46 
47 //Maximum acceptable length for tokens
48 #define COAP_MAX_TOKEN_LEN 8
49 
50 //CoAP payload marker value
51 #define COAP_PAYLOAD_MARKER 0xFF
52 
53 //CoAP code definition
54 #define COAP_CODE(c, d) ((((c) & 0x07U) << 5U) | ((d) & 0x1FU))
55 
56 //Get code class
57 #define COAP_GET_CODE_CLASS(code) (((code) >> 5U) & 0x07U)
58 //Get code subclass
59 #define COAP_GET_CODE_SUBCLASS(code) ((code) & 0x1FU)
60 
61 //C++ guard
62 #ifdef __cplusplus
63  extern "C" {
64 #endif
65 
66 
67 /**
68  * @brief CoAP version numbers
69  **/
70 
71 typedef enum {
72  COAP_VERSION_1 = 1 ///<CoAP version 1
74 
75 
76 /**
77  * @brief CoAP transport protocols
78  **/
79 
80 typedef enum {
81  COAP_TRANSPORT_PROTOCOL_UDP = 1, ///<UDP protocol
82  COAP_TRANSPORT_PROTOCOL_DTLS = 2 ///<DTLS protocol
84 
85 
86 /**
87  * @brief CoAP message types
88  **/
89 
90 typedef enum
91 {
92  COAP_TYPE_CON = 0, ///<Confirmable message
93  COAP_TYPE_NON = 1, ///<Non-confirmable message
94  COAP_TYPE_ACK = 2, ///<Acknowledgment message
95  COAP_TYPE_RST = 3 ///<Reset message
97 
98 
99 /**
100  * @brief CoAP code classes
101  **/
102 
103 typedef enum
104 {
108 } CoapCodeClass;
109 
110 
111 /**
112  * @brief CoAP method and response codes
113  **/
114 
115 typedef enum
116 {
155 } CoapCode;
156 
157 
158 //CodeWarrior or Win32 compiler?
159 #if defined(__CWCC__) || defined(_WIN32)
160  #pragma pack(push, 1)
161 #endif
162 
163 
164 /**
165  * @brief CoAP message format
166  **/
167 
168 typedef __start_packed struct
169 {
170 #ifdef _CPU_BIG_ENDIAN
171  uint8_t version : 2; //0
172  uint8_t type : 2;
173  uint8_t tokenLen : 4;
174 #else
175  uint8_t tokenLen : 4; //0
176  uint8_t type : 2;
177  uint8_t version : 2;
178 #endif
179  uint8_t code; //1
180  uint16_t mid; //2-3
181  uint8_t token[]; //4
183 
184 
185 /**
186  * @brief CoAP option format
187  **/
188 
189 typedef __start_packed struct
190 {
191 #ifdef _CPU_BIG_ENDIAN
192  uint8_t delta : 4; //0
193  uint8_t length : 4;
194 #else
195  uint8_t length : 4; //0
196  uint8_t delta : 4;
197 #endif
199 
200 
201 //CodeWarrior or Win32 compiler?
202 #if defined(__CWCC__) || defined(_WIN32)
203  #pragma pack(pop)
204 #endif
205 
206 
207 //C++ guard
208 #ifdef __cplusplus
209  }
210 #endif
211 
212 #endif
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:80
CoapMessageType
CoAP message types.
Definition: coap_common.h:90
TCP/IP stack core.
Reset message.
Definition: coap_common.h:95
__start_packed struct @111 CoapOptionHeader
CoAP option format.
#define COAP_CODE(c, d)
Definition: coap_common.h:54
CoapCode
CoAP method and response codes.
Definition: coap_common.h:115
CoapCodeClass
CoAP code classes.
Definition: coap_common.h:103
uint8_t length
Definition: coap_common.h:195
CoAP version 1.
Definition: coap_common.h:72
Confirmable message.
Definition: coap_common.h:92
CoapProtocolLevel
CoAP version numbers.
Definition: coap_common.h:71
uint8_t type
Definition: coap_common.h:176
uint8_t tokenLen
Definition: coap_common.h:175
__start_packed struct _Ipv4Header __end_packed
uint8_t code
Definition: coap_common.h:179
uint8_t token[]
Definition: coap_common.h:181
uint16_t mid
Definition: coap_common.h:180
Acknowledgment message.
Definition: coap_common.h:94
uint8_t delta
Definition: coap_common.h:196
uint8_t version
Definition: coap_common.h:177
__start_packed struct @110 CoapMessageHeader
CoAP message format.
Non-confirmable message.
Definition: coap_common.h:93