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-2020 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.0.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 //Maximum acceptable length for tokens
43 #define COAP_MAX_TOKEN_LEN 8
44 
45 //CoAP payload marker value
46 #define COAP_PAYLOAD_MARKER 0xFF
47 
48 //CoAP code definition
49 #define COAP_CODE(c, d) ((((c) & 0x07U) << 5U) | ((d) & 0x1FU))
50 
51 //Get code class
52 #define COAP_GET_CODE_CLASS(code) (((code) >> 5U) & 0x07U)
53 //Get code subclass
54 #define COAP_GET_CODE_SUBCLASS(code) ((code) & 0x1FU)
55 
56 //C++ guard
57 #ifdef __cplusplus
58 extern "C" {
59 #endif
60 
61 
62 /**
63  * @brief CoAP version numbers
64  **/
65 
66 typedef enum {
67  COAP_VERSION_1 = 1 ///<CoAP version 1
69 
70 
71 /**
72  * @brief CoAP transport protocols
73  **/
74 
75 typedef enum {
76  COAP_TRANSPORT_PROTOCOL_UDP = 1, ///<UDP protocol
77  COAP_TRANSPORT_PROTOCOL_DTLS = 2 ///<DTLS protocol
79 
80 
81 /**
82  * @brief CoAP message types
83  **/
84 
85 typedef enum
86 {
87  COAP_TYPE_CON = 0, ///<Confirmable message
88  COAP_TYPE_NON = 1, ///<Non-confirmable message
89  COAP_TYPE_ACK = 2, ///<Acknowledgment message
90  COAP_TYPE_RST = 3 ///<Reset message
92 
93 
94 /**
95  * @brief CoAP code classes
96  **/
97 
98 typedef enum
99 {
104 
105 
106 /**
107  * @brief CoAP method and response codes
108  **/
109 
110 typedef enum
111 {
149  COAP_CODE_ABORT = COAP_CODE(7, 5)
151 
152 
153 //CodeWarrior or Win32 compiler?
154 #if defined(__CWCC__) || defined(_WIN32)
155  #pragma pack(push, 1)
156 #endif
157 
158 
159 /**
160  * @brief CoAP message format
161  **/
162 
163 typedef __start_packed struct
164 {
165 #if defined(_CPU_BIG_ENDIAN) && !defined(__IAR_SYSTEMS_ICC__)
166  uint8_t version : 2; //0
167  uint8_t type : 2;
168  uint8_t tokenLen : 4;
169 #else
170  uint8_t tokenLen : 4; //0
171  uint8_t type : 2;
172  uint8_t version : 2;
173 #endif
174  uint8_t code; //1
175  uint16_t mid; //2-3
176  uint8_t token[]; //4
178 
179 
180 /**
181  * @brief CoAP option format
182  **/
183 
184 typedef __start_packed struct
185 {
186 #if defined(_CPU_BIG_ENDIAN) && !defined(__IAR_SYSTEMS_ICC__)
187  uint8_t delta : 4; //0
188  uint8_t length : 4;
189 #else
190  uint8_t length : 4; //0
191  uint8_t delta : 4;
192 #endif
194 
195 
196 //CodeWarrior or Win32 compiler?
197 #if defined(__CWCC__) || defined(_WIN32)
198  #pragma pack(pop)
199 #endif
200 
201 
202 //C++ guard
203 #ifdef __cplusplus
204 }
205 #endif
206 
207 #endif
uint8_t length
Definition: coap_common.h:190
@ COAP_CODE_FORBIDDEN
Definition: coap_common.h:129
@ COAP_CODE_PONG
Definition: coap_common.h:147
uint8_t code
Definition: coap_common.h:174
__start_packed struct @0 CoapMessageHeader
CoAP message format.
@ COAP_CODE_REQUEST_ENTITY_INCOMPLETE
Definition: coap_common.h:133
@ COAP_CODE_METHOD_NOT_ALLOWED
Definition: coap_common.h:131
@ COAP_CODE_UNSUPPORTED_CONTENT_FORMAT
Definition: coap_common.h:137
@ COAP_CODE_SERVICE_UNAVAILABLE
Definition: coap_common.h:142
uint8_t delta
Definition: coap_common.h:191
CoapCodeClass
CoAP code classes.
Definition: coap_common.h:99
@ COAP_CODE_PROXYING_NOT_SUPPORTED
Definition: coap_common.h:144
@ COAP_CODE_PING
Definition: coap_common.h:146
CoapProtocolLevel
CoAP version numbers.
Definition: coap_common.h:66
@ COAP_CODE_DELETED
Definition: coap_common.h:121
uint8_t type
Definition: coap_common.h:171
@ COAP_CODE_BAD_GATEWAY
Definition: coap_common.h:141
@ COAP_CODE_POST
Definition: coap_common.h:114
uint8_t version
Definition: coap_common.h:172
CoapTransportProtocol
CoAP transport protocols.
Definition: coap_common.h:75
@ COAP_CODE_VALID
Definition: coap_common.h:122
@ COAP_VERSION_1
CoAP version 1.
Definition: coap_common.h:67
@ COAP_CODE_REQUEST_ENTITY_TO_LARGE
Definition: coap_common.h:136
@ COAP_CODE_CLASS_SERVER_ERROR
Definition: coap_common.h:102
uint16_t mid
Definition: coap_common.h:175
@ COAP_CODE_PRECONDITION_FAILED
Definition: coap_common.h:135
@ COAP_CODE_GATEWAY_TIMEOUT
Definition: coap_common.h:143
@ COAP_CODE_BAD_OPTION
Definition: coap_common.h:128
@ COAP_CODE_PUT
Definition: coap_common.h:115
@ COAP_CODE_UNPROCESSABLE_ENTITY
Definition: coap_common.h:138
@ COAP_CODE_NOT_ACCEPTABLE
Definition: coap_common.h:132
@ COAP_TRANSPORT_PROTOCOL_UDP
UDP protocol.
Definition: coap_common.h:76
@ COAP_CODE_CREATED
Definition: coap_common.h:120
@ COAP_CODE_NOT_IMPLEMENTED
Definition: coap_common.h:140
__start_packed struct _Ipv4Header __end_packed
@ COAP_TYPE_ACK
Acknowledgment message.
Definition: coap_common.h:89
@ COAP_CODE_NOT_FOUND
Definition: coap_common.h:130
@ COAP_TYPE_CON
Confirmable message.
Definition: coap_common.h:87
#define COAP_CODE(c, d)
Definition: coap_common.h:49
@ COAP_TYPE_RST
Reset message.
Definition: coap_common.h:90
@ COAP_CODE_FETCH
Definition: coap_common.h:117
@ COAP_CODE_CLASS_CLIENT_ERROR
Definition: coap_common.h:101
@ COAP_CODE_RELEASE
Definition: coap_common.h:148
@ COAP_TRANSPORT_PROTOCOL_DTLS
DTLS protocol.
Definition: coap_common.h:77
@ COAP_CODE_GET
Definition: coap_common.h:113
CoapMessageType
CoAP message types.
Definition: coap_common.h:86
@ COAP_CODE_CHANGED
Definition: coap_common.h:123
@ COAP_CODE_INTERNAL_SERVER
Definition: coap_common.h:139
@ COAP_CODE_DELETE
Definition: coap_common.h:116
@ COAP_CODE_UNAUTHOZED
Definition: coap_common.h:127
@ COAP_CODE_EMPTY
Definition: coap_common.h:112
@ COAP_CODE_ABORT
Definition: coap_common.h:149
@ COAP_CODE_CONTINUE
Definition: coap_common.h:125
@ COAP_CODE_CSM
Definition: coap_common.h:145
__start_packed struct @1 CoapOptionHeader
CoAP option format.
@ COAP_CODE_IPATCH
Definition: coap_common.h:119
uint8_t tokenLen
Definition: coap_common.h:170
@ COAP_CODE_PATCH
Definition: coap_common.h:118
@ COAP_CODE_CLASS_SUCCESS
Definition: coap_common.h:100
TCP/IP stack core.
@ COAP_TYPE_NON
Non-confirmable message.
Definition: coap_common.h:88
CoapCode
CoAP method and response codes.
Definition: coap_common.h:111
@ COAP_CODE_CONTENT
Definition: coap_common.h:124
@ COAP_CODE_BAD_REQUEST
Definition: coap_common.h:126
@ COAP_CODE_CONFLICT
Definition: coap_common.h:134
uint8_t token[]
Definition: coap_common.h:176