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  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 #ifndef _COAP_COMMON_H
32 #define _COAP_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //Maximum size of CoAP messages
38 #ifndef COAP_MAX_MSG_SIZE
39  #define COAP_MAX_MSG_SIZE 1152
40 #elif (COAP_MAX_MSG_SIZE < 16)
41  #error COAP_MAX_MSG_SIZE parameter is not valid
42 #endif
43 
44 //CoAP port number
45 #define COAP_PORT 5683
46 //DTLS-secured CoAP port number
47 #define COAPS_PORT 5684
48 
49 //Maximum acceptable length for tokens
50 #define COAP_MAX_TOKEN_LEN 8
51 
52 //CoAP payload marker value
53 #define COAP_PAYLOAD_MARKER 0xFF
54 
55 //CoAP code definition
56 #define COAP_CODE(c, d) ((((c) & 0x07U) << 5U) | ((d) & 0x1FU))
57 
58 //Get code class
59 #define COAP_GET_CODE_CLASS(code) (((code) >> 5U) & 0x07U)
60 //Get code subclass
61 #define COAP_GET_CODE_SUBCLASS(code) ((code) & 0x1FU)
62 
63 //C++ guard
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 
69 /**
70  * @brief CoAP version numbers
71  **/
72 
73 typedef enum {
74  COAP_VERSION_1 = 1 ///<CoAP version 1
76 
77 
78 /**
79  * @brief CoAP transport protocols
80  **/
81 
82 typedef enum {
83  COAP_TRANSPORT_PROTOCOL_UDP = 1, ///<UDP protocol
84  COAP_TRANSPORT_PROTOCOL_DTLS = 2 ///<DTLS protocol
86 
87 
88 /**
89  * @brief CoAP message types
90  **/
91 
92 typedef enum
93 {
94  COAP_TYPE_CON = 0, ///<Confirmable message
95  COAP_TYPE_NON = 1, ///<Non-confirmable message
96  COAP_TYPE_ACK = 2, ///<Acknowledgment message
97  COAP_TYPE_RST = 3 ///<Reset message
99 
100 
101 /**
102  * @brief CoAP code classes
103  **/
104 
105 typedef enum
106 {
110 } CoapCodeClass;
111 
112 
113 /**
114  * @brief CoAP method and response codes
115  **/
116 
117 typedef enum
118 {
157 } CoapCode;
158 
159 
160 //CodeWarrior or Win32 compiler?
161 #if defined(__CWCC__) || defined(_WIN32)
162  #pragma pack(push, 1)
163 #endif
164 
165 
166 /**
167  * @brief CoAP message format
168  **/
169 
170 typedef __start_packed struct
171 {
172 #ifdef _CPU_BIG_ENDIAN
173  uint8_t version : 2; //0
174  uint8_t type : 2;
175  uint8_t tokenLen : 4;
176 #else
177  uint8_t tokenLen : 4; //0
178  uint8_t type : 2;
179  uint8_t version : 2;
180 #endif
181  uint8_t code; //1
182  uint16_t mid; //2-3
183  uint8_t token[]; //4
185 
186 
187 /**
188  * @brief CoAP option format
189  **/
190 
191 typedef __start_packed struct
192 {
193 #ifdef _CPU_BIG_ENDIAN
194  uint8_t delta : 4; //0
195  uint8_t length : 4;
196 #else
197  uint8_t length : 4; //0
198  uint8_t delta : 4;
199 #endif
201 
202 
203 //CodeWarrior or Win32 compiler?
204 #if defined(__CWCC__) || defined(_WIN32)
205  #pragma pack(pop)
206 #endif
207 
208 
209 //C++ guard
210 #ifdef __cplusplus
211 }
212 #endif
213 
214 #endif
uint8_t length
Definition: coap_common.h:197
@ COAP_CODE_FORBIDDEN
Definition: coap_common.h:136
@ COAP_CODE_PONG
Definition: coap_common.h:154
uint8_t code
Definition: coap_common.h:181
@ COAP_CODE_REQUEST_ENTITY_INCOMPLETE
Definition: coap_common.h:140
@ COAP_CODE_METHOD_NOT_ALLOWED
Definition: coap_common.h:138
@ COAP_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap_common.h:144
@ COAP_CODE_SERVICE_UNAVAILABLE
Definition: coap_common.h:149
uint8_t delta
Definition: coap_common.h:198
CoapCodeClass
CoAP code classes.
Definition: coap_common.h:105
@ COAP_CODE_PROXYING_NOT_SUPPORTED
Definition: coap_common.h:151
@ COAP_CODE_PING
Definition: coap_common.h:153
CoapProtocolLevel
CoAP version numbers.
Definition: coap_common.h:73
@ COAP_CODE_DELETED
Definition: coap_common.h:128
uint8_t type
Definition: coap_common.h:178
@ COAP_CODE_BAD_GATEWAY
Definition: coap_common.h:148
@ COAP_CODE_POST
Definition: coap_common.h:121
uint8_t version
Definition: coap_common.h:179
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:82
@ COAP_CODE_VALID
Definition: coap_common.h:129
@ COAP_VERSION_1
CoAP version 1.
Definition: coap_common.h:74
@ COAP_CODE_REQUEST_ENTITY_TO_LARGE
Definition: coap_common.h:143
@ COAP_CODE_CLASS_SERVER_ERROR
Definition: coap_common.h:109
uint16_t mid
Definition: coap_common.h:182
@ COAP_CODE_PRECONDITION_FAILED
Definition: coap_common.h:142
@ COAP_CODE_GATEWAY_TIMEOUT
Definition: coap_common.h:150
@ COAP_CODE_BAD_OPTION
Definition: coap_common.h:135
@ COAP_CODE_PUT
Definition: coap_common.h:122
@ COAP_CODE_UNPROCESSABLE_ENTITY
Definition: coap_common.h:145
@ COAP_CODE_NOT_ACCEPTABLE
Definition: coap_common.h:139
__start_packed struct @106 CoapMessageHeader
CoAP message format.
@ COAP_TRANSPORT_PROTOCOL_UDP
UDP protocol.
Definition: coap_common.h:83
@ COAP_CODE_CREATED
Definition: coap_common.h:127
@ COAP_CODE_NOT_IMPLEMENTED
Definition: coap_common.h:147
__start_packed struct _Ipv4Header __end_packed
@ COAP_TYPE_ACK
Acknowledgment message.
Definition: coap_common.h:96
@ COAP_CODE_NOT_FOUND
Definition: coap_common.h:137
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:94
#define COAP_CODE(c, d)
Definition: coap_common.h:56
@ COAP_TYPE_RST
Reset message.
Definition: coap_common.h:97
@ COAP_CODE_FETCH
Definition: coap_common.h:124
@ COAP_CODE_CLASS_CLIENT_ERROR
Definition: coap_common.h:108
@ COAP_CODE_RELEASE
Definition: coap_common.h:155
@ COAP_TRANSPORT_PROTOCOL_DTLS
DTLS protocol.
Definition: coap_common.h:84
@ COAP_CODE_GET
Definition: coap_common.h:120
CoapMessageType
CoAP message types.
Definition: coap_common.h:92
@ COAP_CODE_CHANGED
Definition: coap_common.h:130
__start_packed struct @107 CoapOptionHeader
CoAP option format.
@ COAP_CODE_INTERNAL_SERVER
Definition: coap_common.h:146
@ COAP_CODE_DELETE
Definition: coap_common.h:123
@ COAP_CODE_UNAUTHOZED
Definition: coap_common.h:134
@ COAP_CODE_EMPTY
Definition: coap_common.h:119
@ COAP_CODE_ABORT
Definition: coap_common.h:156
@ COAP_CODE_CONTINUE
Definition: coap_common.h:132
@ COAP_CODE_CSM
Definition: coap_common.h:152
@ COAP_CODE_IPATCH
Definition: coap_common.h:126
uint8_t tokenLen
Definition: coap_common.h:177
@ COAP_CODE_PATCH
Definition: coap_common.h:125
@ COAP_CODE_CLASS_SUCCESS
Definition: coap_common.h:107
TCP/IP stack core.
@ COAP_TYPE_NON
Non-confirmable message.
Definition: coap_common.h:95
CoapCode
CoAP method and response codes.
Definition: coap_common.h:117
@ COAP_CODE_CONTENT
Definition: coap_common.h:131
@ COAP_CODE_BAD_REQUEST
Definition: coap_common.h:133
@ COAP_CODE_CONFLICT
Definition: coap_common.h:141
uint8_t token[]
Definition: coap_common.h:183