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-2024 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 2.4.0
29  **/
30 
31 #ifndef _COAP_COMMON_H
32 #define _COAP_COMMON_H
33 
34 //Dependencies
35 #include "core/net.h"
36 
37 //CoAP port number
38 #define COAP_PORT 5683
39 //DTLS-secured CoAP port number
40 #define COAPS_PORT 5684
41 
42 //CoAP message header size
43 #define COAP_HEADER_SIZE 4
44 //Maximum acceptable length for tokens
45 #define COAP_MAX_TOKEN_LEN 8
46 
47 //CoAP payload marker value
48 #define COAP_PAYLOAD_MARKER 0xFF
49 
50 //CoAP code definition
51 #define COAP_CODE(c, d) ((((c) & 0x07U) << 5U) | ((d) & 0x1FU))
52 
53 //Get code class
54 #define COAP_GET_CODE_CLASS(code) (((code) >> 5U) & 0x07U)
55 //Get code subclass
56 #define COAP_GET_CODE_SUBCLASS(code) ((code) & 0x1FU)
57 
58 //C++ guard
59 #ifdef __cplusplus
60 extern "C" {
61 #endif
62 
63 
64 /**
65  * @brief CoAP version numbers
66  **/
67 
68 typedef enum {
69  COAP_VERSION_1 = 1 ///<CoAP version 1
71 
72 
73 /**
74  * @brief CoAP transport protocols
75  **/
76 
77 typedef enum {
78  COAP_TRANSPORT_PROTOCOL_UDP = 1, ///<UDP protocol
79  COAP_TRANSPORT_PROTOCOL_DTLS = 2 ///<DTLS protocol
81 
82 
83 /**
84  * @brief CoAP message types
85  **/
86 
87 typedef enum
88 {
89  COAP_TYPE_CON = 0, ///<Confirmable message
90  COAP_TYPE_NON = 1, ///<Non-confirmable message
91  COAP_TYPE_ACK = 2, ///<Acknowledgment message
92  COAP_TYPE_RST = 3 ///<Reset message
94 
95 
96 /**
97  * @brief CoAP code classes
98  **/
99 
100 typedef enum
101 {
106 
107 
108 /**
109  * @brief CoAP method and response codes
110  **/
111 
112 typedef enum
113 {
152  COAP_CODE_ABORT = COAP_CODE(7, 5)
154 
155 
156 //CC-RX, CodeWarrior or Win32 compiler?
157 #if defined(__CCRX__)
158  #pragma pack
159 #elif defined(__CWCC__) || defined(_WIN32)
160  #pragma pack(push, 1)
161 #endif
162 
163 
164 /**
165  * @brief CoAP message format
166  **/
167 
169 {
170 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
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 __packed_struct
190 {
191 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
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 //CC-RX, CodeWarrior or Win32 compiler?
202 #if defined(__CCRX__)
203  #pragma unpack
204 #elif defined(__CWCC__) || defined(_WIN32)
205  #pragma pack(pop)
206 #endif
207 
208 //C++ guard
209 #ifdef __cplusplus
210 }
211 #endif
212 
213 #endif
uint8_t type
Definition: coap_common.h:176
CoapProtocolLevel
CoAP version numbers.
Definition: coap_common.h:68
@ COAP_VERSION_1
CoAP version 1.
Definition: coap_common.h:69
CoapOptionHeader
Definition: coap_common.h:198
#define COAP_CODE(c, d)
Definition: coap_common.h:51
uint8_t code
Definition: coap_common.h:179
uint16_t mid
Definition: coap_common.h:180
uint8_t token[]
Definition: coap_common.h:181
CoapMessageHeader
Definition: coap_common.h:182
uint8_t version
Definition: coap_common.h:177
uint8_t delta
Definition: coap_common.h:196
CoapMessageType
CoAP message types.
Definition: coap_common.h:88
@ COAP_TYPE_RST
Reset message.
Definition: coap_common.h:92
@ COAP_TYPE_NON
Non-confirmable message.
Definition: coap_common.h:90
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:89
@ COAP_TYPE_ACK
Acknowledgment message.
Definition: coap_common.h:91
typedef __packed_struct
CoAP message format.
Definition: coap_common.h:169
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:77
@ COAP_TRANSPORT_PROTOCOL_UDP
UDP protocol.
Definition: coap_common.h:78
@ COAP_TRANSPORT_PROTOCOL_DTLS
DTLS protocol.
Definition: coap_common.h:79
CoapCodeClass
CoAP code classes.
Definition: coap_common.h:101
@ COAP_CODE_CLASS_CLIENT_ERROR
Definition: coap_common.h:103
@ COAP_CODE_CLASS_SERVER_ERROR
Definition: coap_common.h:104
@ COAP_CODE_CLASS_SUCCESS
Definition: coap_common.h:102
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
@ COAP_CODE_METHOD_NOT_ALLOWED
Definition: coap_common.h:133
@ COAP_CODE_DELETE
Definition: coap_common.h:118
@ COAP_CODE_PATCH
Definition: coap_common.h:120
@ COAP_CODE_UNAUTHOZED
Definition: coap_common.h:129
@ COAP_CODE_FORBIDDEN
Definition: coap_common.h:131
@ COAP_CODE_CSM
Definition: coap_common.h:148
@ COAP_CODE_INTERNAL_SERVER
Definition: coap_common.h:141
@ COAP_CODE_BAD_OPTION
Definition: coap_common.h:130
@ COAP_CODE_CONTENT
Definition: coap_common.h:126
@ COAP_CODE_RELEASE
Definition: coap_common.h:151
@ COAP_CODE_DELETED
Definition: coap_common.h:123
@ COAP_CODE_ABORT
Definition: coap_common.h:152
@ COAP_CODE_CONFLICT
Definition: coap_common.h:136
@ COAP_CODE_GATEWAY_TIMEOUT
Definition: coap_common.h:145
@ COAP_CODE_BAD_REQUEST
Definition: coap_common.h:128
@ COAP_CODE_PUT
Definition: coap_common.h:117
@ COAP_CODE_PING
Definition: coap_common.h:149
@ COAP_CODE_BAD_GATEWAY
Definition: coap_common.h:143
@ COAP_CODE_NOT_FOUND
Definition: coap_common.h:132
@ COAP_CODE_CREATED
Definition: coap_common.h:122
@ COAP_CODE_UNPROCESSABLE_ENTITY
Definition: coap_common.h:140
@ COAP_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap_common.h:139
@ COAP_CODE_PRECONDITION_FAILED
Definition: coap_common.h:137
@ COAP_CODE_VALID
Definition: coap_common.h:124
@ COAP_CODE_PONG
Definition: coap_common.h:150
@ COAP_CODE_POST
Definition: coap_common.h:116
@ COAP_CODE_NOT_ACCEPTABLE
Definition: coap_common.h:134
@ COAP_CODE_PROXYING_NOT_SUPPORTED
Definition: coap_common.h:146
@ COAP_CODE_GET
Definition: coap_common.h:115
@ COAP_CODE_CHANGED
Definition: coap_common.h:125
@ COAP_CODE_FETCH
Definition: coap_common.h:119
@ COAP_CODE_SERVICE_UNAVAILABLE
Definition: coap_common.h:144
@ COAP_CODE_REQUEST_ENTITY_INCOMPLETE
Definition: coap_common.h:135
@ COAP_CODE_CONTINUE
Definition: coap_common.h:127
@ COAP_CODE_NOT_IMPLEMENTED
Definition: coap_common.h:142
@ COAP_CODE_EMPTY
Definition: coap_common.h:114
@ COAP_CODE_REQUEST_ENTITY_TO_LARGE
Definition: coap_common.h:138
@ COAP_CODE_IPATCH
Definition: coap_common.h:121
@ COAP_CODE_HOP_LIMIT_REACHED
Definition: coap_common.h:147
TCP/IP stack core.
uint8_t length
Definition: tcp.h:368