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-2022 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.2.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 //CodeWarrior or Win32 compiler?
157 #if defined(__CWCC__) || defined(_WIN32)
158  #pragma pack(push, 1)
159 #endif
160 
161 
162 /**
163  * @brief CoAP message format
164  **/
165 
166 typedef __start_packed struct
167 {
168 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
169  uint8_t version : 2; //0
170  uint8_t type : 2;
171  uint8_t tokenLen : 4;
172 #else
173  uint8_t tokenLen : 4; //0
174  uint8_t type : 2;
175  uint8_t version : 2;
176 #endif
177  uint8_t code; //1
178  uint16_t mid; //2-3
179  uint8_t token[]; //4
181 
182 
183 /**
184  * @brief CoAP option format
185  **/
186 
187 typedef __start_packed struct
188 {
189 #if defined(_CPU_BIG_ENDIAN) && !defined(__ICCRX__)
190  uint8_t delta : 4; //0
191  uint8_t length : 4;
192 #else
193  uint8_t length : 4; //0
194  uint8_t delta : 4;
195 #endif
197 
198 
199 //CodeWarrior or Win32 compiler?
200 #if defined(__CWCC__) || defined(_WIN32)
201  #pragma pack(pop)
202 #endif
203 
204 //C++ guard
205 #ifdef __cplusplus
206 }
207 #endif
208 
209 #endif
uint8_t length
Definition: coap_common.h:193
@ COAP_CODE_FORBIDDEN
Definition: coap_common.h:131
@ COAP_CODE_PONG
Definition: coap_common.h:150
uint8_t code
Definition: coap_common.h:177
__start_packed struct @0 CoapMessageHeader
CoAP message format.
@ COAP_CODE_REQUEST_ENTITY_INCOMPLETE
Definition: coap_common.h:135
@ COAP_CODE_METHOD_NOT_ALLOWED
Definition: coap_common.h:133
@ COAP_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap_common.h:139
@ COAP_CODE_SERVICE_UNAVAILABLE
Definition: coap_common.h:144
uint8_t delta
Definition: coap_common.h:194
CoapCodeClass
CoAP code classes.
Definition: coap_common.h:101
@ COAP_CODE_PROXYING_NOT_SUPPORTED
Definition: coap_common.h:146
@ COAP_CODE_PING
Definition: coap_common.h:149
CoapProtocolLevel
CoAP version numbers.
Definition: coap_common.h:68
@ COAP_CODE_DELETED
Definition: coap_common.h:123
uint8_t type
Definition: coap_common.h:174
@ COAP_CODE_BAD_GATEWAY
Definition: coap_common.h:143
@ COAP_CODE_POST
Definition: coap_common.h:116
uint8_t version
Definition: coap_common.h:175
@ COAP_CODE_HOP_LIMIT_REACHED
Definition: coap_common.h:147
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:77
@ COAP_CODE_VALID
Definition: coap_common.h:124
@ COAP_VERSION_1
CoAP version 1.
Definition: coap_common.h:69
@ COAP_CODE_REQUEST_ENTITY_TO_LARGE
Definition: coap_common.h:138
@ COAP_CODE_CLASS_SERVER_ERROR
Definition: coap_common.h:104
uint16_t mid
Definition: coap_common.h:178
@ COAP_CODE_PRECONDITION_FAILED
Definition: coap_common.h:137
@ COAP_CODE_GATEWAY_TIMEOUT
Definition: coap_common.h:145
@ COAP_CODE_BAD_OPTION
Definition: coap_common.h:130
@ COAP_CODE_PUT
Definition: coap_common.h:117
@ COAP_CODE_UNPROCESSABLE_ENTITY
Definition: coap_common.h:140
@ COAP_CODE_NOT_ACCEPTABLE
Definition: coap_common.h:134
@ COAP_TRANSPORT_PROTOCOL_UDP
UDP protocol.
Definition: coap_common.h:78
@ COAP_CODE_CREATED
Definition: coap_common.h:122
@ COAP_CODE_NOT_IMPLEMENTED
Definition: coap_common.h:142
__start_packed struct _Ipv4Header __end_packed
@ COAP_TYPE_ACK
Acknowledgment message.
Definition: coap_common.h:91
@ COAP_CODE_NOT_FOUND
Definition: coap_common.h:132
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:89
#define COAP_CODE(c, d)
Definition: coap_common.h:51
@ COAP_TYPE_RST
Reset message.
Definition: coap_common.h:92
@ COAP_CODE_FETCH
Definition: coap_common.h:119
@ COAP_CODE_CLASS_CLIENT_ERROR
Definition: coap_common.h:103
@ COAP_CODE_RELEASE
Definition: coap_common.h:151
@ COAP_TRANSPORT_PROTOCOL_DTLS
DTLS protocol.
Definition: coap_common.h:79
@ COAP_CODE_GET
Definition: coap_common.h:115
CoapMessageType
CoAP message types.
Definition: coap_common.h:88
@ COAP_CODE_CHANGED
Definition: coap_common.h:125
@ COAP_CODE_INTERNAL_SERVER
Definition: coap_common.h:141
@ COAP_CODE_DELETE
Definition: coap_common.h:118
@ COAP_CODE_UNAUTHOZED
Definition: coap_common.h:129
@ COAP_CODE_EMPTY
Definition: coap_common.h:114
@ COAP_CODE_ABORT
Definition: coap_common.h:152
@ COAP_CODE_CONTINUE
Definition: coap_common.h:127
@ COAP_CODE_CSM
Definition: coap_common.h:148
__start_packed struct @1 CoapOptionHeader
CoAP option format.
@ COAP_CODE_IPATCH
Definition: coap_common.h:121
uint8_t tokenLen
Definition: coap_common.h:173
@ COAP_CODE_PATCH
Definition: coap_common.h:120
@ COAP_CODE_CLASS_SUCCESS
Definition: coap_common.h:102
TCP/IP stack core.
@ COAP_TYPE_NON
Non-confirmable message.
Definition: coap_common.h:90
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
@ COAP_CODE_CONTENT
Definition: coap_common.h:126
@ COAP_CODE_BAD_REQUEST
Definition: coap_common.h:128
@ COAP_CODE_CONFLICT
Definition: coap_common.h:136
uint8_t token[]
Definition: coap_common.h:179