coap_server.h
Go to the documentation of this file.
1 /**
2  * @file coap_server.h
3  * @brief CoAP 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_SERVER_H
32 #define _COAP_SERVER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "coap/coap_common.h"
37 #include "coap/coap_message.h"
38 #include "coap/coap_option.h"
39 
40 //CoAP server support
41 #ifndef COAP_SERVER_SUPPORT
42  #define COAP_SERVER_SUPPORT ENABLED
43 #elif (COAP_SERVER_SUPPORT != ENABLED && COAP_SERVER_SUPPORT != DISABLED)
44  #error COAP_SERVER_SUPPORT parameter is not valid
45 #endif
46 
47 //DTLS-secured CoAP support
48 #ifndef COAP_SERVER_DTLS_SUPPORT
49  #define COAP_SERVER_DTLS_SUPPORT DISABLED
50 #elif (COAP_SERVER_DTLS_SUPPORT != ENABLED && COAP_SERVER_DTLS_SUPPORT != DISABLED)
51  #error COAP_SERVER_DTLS_SUPPORT parameter is not valid
52 #endif
53 
54 //Stack size required to run the CoAP server
55 #ifndef COAP_SERVER_STACK_SIZE
56  #define COAP_SERVER_STACK_SIZE 650
57 #elif (COAP_SERVER_STACK_SIZE < 1)
58  #error COAP_SERVER_STACK_SIZE parameter is not valid
59 #endif
60 
61 //Maximum number of simultaneous DTLS sessions
62 #ifndef COAP_SERVER_MAX_SESSIONS
63  #define COAP_SERVER_MAX_SESSIONS 4
64 #elif (COAP_SERVER_MAX_SESSIONS < 1)
65  #error COAP_SERVER_MAX_SESSIONS parameter is not valid
66 #endif
67 
68 //DTLS server tick interval
69 #ifndef COAP_SERVER_TICK_INTERVAL
70  #define COAP_SERVER_TICK_INTERVAL 500
71 #elif (COAP_SERVER_TICK_INTERVAL < 100)
72  #error COAP_SERVER_TICK_INTERVAL parameter is not valid
73 #endif
74 
75 //DTLS session timeout
76 #ifndef COAP_SERVER_SESSION_TIMEOUT
77  #define COAP_SERVER_SESSION_TIMEOUT 60000
78 #elif (COAP_SERVER_SESSION_TIMEOUT < 0)
79  #error COAP_SERVER_SESSION_TIMEOUT parameter is not valid
80 #endif
81 
82 //Size of buffer used for input/output operations
83 #ifndef COAP_SERVER_BUFFER_SIZE
84  #define COAP_SERVER_BUFFER_SIZE 2048
85 #elif (COAP_SERVER_BUFFER_SIZE < 1)
86  #error COAP_SERVER_BUFFER_SIZE parameter is not valid
87 #endif
88 
89 //Maximum size of the cookie secret
90 #ifndef COAP_SERVER_MAX_COOKIE_SECRET_SIZE
91  #define COAP_SERVER_MAX_COOKIE_SECRET_SIZE 32
92 #elif (COAP_SERVER_MAX_COOKIE_SECRET_SIZE < 1)
93  #error COAP_SERVER_MAX_COOKIE_SECRET_SIZE parameter is not valid
94 #endif
95 
96 //Maximum length of URI
97 #ifndef COAP_SERVER_MAX_URI_LEN
98  #define COAP_SERVER_MAX_URI_LEN 128
99 #elif (COAP_SERVER_MAX_URI_LEN < 1)
100  #error COAP_SERVER_MAX_URI_LEN parameter is not valid
101 #endif
102 
103 //Priority at which the CoAP server should run
104 #ifndef COAP_SERVER_PRIORITY
105  #define COAP_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
106 #endif
107 
108 //Application specific context
109 #ifndef COAP_SERVER_PRIVATE_CONTEXT
110  #define COAP_SERVER_PRIVATE_CONTEXT
111 #endif
112 
113 //DTLS supported?
114 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
115  #include "core/crypto.h"
116  #include "tls.h"
117 #endif
118 
119 //Forward declaration of CoapServerContext structure
120 struct _CoapServerContext;
121 #define CoapServerContext struct _CoapServerContext
122 
123 //Forward declaration of CoapDtlsSession structure
124 struct _CoapDtlsSession;
125 #define CoapDtlsSession struct _CoapDtlsSession
126 
127 //C++ guard
128 #ifdef __cplusplus
129 extern "C" {
130 #endif
131 
132 
133 /**
134  * @brief UDP initialization callback function
135  **/
136 
138  Socket *socket);
139 
140 
141 //DTLS supported?
142 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
143 
144 /**
145  * @brief DTLS initialization callback
146  **/
147 
150 
151 #endif
152 
153 
154 /**
155  * @brief CoAP request callback function
156  **/
157 
159  CoapCode method, const char_t *uri);
160 
161 
162 /**
163  * @brief CoAP server settings
164  **/
165 
166 typedef struct
167 {
168  OsTaskParameters task; ///<Task parameters
169  NetInterface *interface; ///<Underlying network interface
170  uint16_t port; ///<CoAP port number
171  CoapServerUdpInitCallback udpInitCallback; ///<UDP initialization callback
172 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
173  CoapServerDtlsInitCallback dtlsInitCallback; ///<DTLS initialization callback
174 #endif
175  CoapServerRequestCallback requestCallback; ///<CoAP request callback
177 
178 
179 /**
180  * @brief DTLS session
181  **/
182 
184 {
188  uint16_t clientPort;
189 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
191 #endif
193 };
194 
195 
196 /**
197  * @brief CoAP server context
198  **/
199 
201 {
202  CoapServerSettings settings; ///<User settings
203  bool_t running; ///<Operational state of the CoAP server
204  bool_t stop; ///<Stop request
205  OsEvent event; ///<Event object used to poll the underlying socket
206  OsTaskParameters taskParams; ///<Task parameters
207  OsTaskId taskId; ///<Task identifier
208  Socket *socket; ///<Underlying socket
209  IpAddr serverIpAddr; ///<Server's IP address
210  IpAddr clientIpAddr; ///<Client's IP address
211  uint16_t clientPort; ///<Client's port
212 #if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
214  size_t cookieSecretLen; ///<Length of the cookie secret, in bytes
216 #endif
217  uint8_t buffer[COAP_SERVER_BUFFER_SIZE]; ///<Memory buffer for input/output operations
218  size_t bufferLen; ///<Length of the buffer, in bytes
219  char_t uri[COAP_SERVER_MAX_URI_LEN + 1]; ///<Resource identifier
220  CoapMessage request; ///<CoAP request message
221  CoapMessage response; ///<CoAP response message
222  COAP_SERVER_PRIVATE_CONTEXT ///<Application specific context
223 };
224 
225 
226 //CoAP server related functions
228 
230  const CoapServerSettings *settings);
231 
233  const uint8_t *cookieSecret, size_t cookieSecretLen);
234 
237 
238 void coapServerTask(CoapServerContext *context);
239 
240 void coapServerDeinit(CoapServerContext *context);
241 
242 //C++ guard
243 #ifdef __cplusplus
244 }
245 #endif
246 
247 #endif
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
Definitions common to CoAP client and server.
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
CoAP message formatting and parsing.
Formatting and parsing of CoAP options.
#define COAP_SERVER_MAX_COOKIE_SECRET_SIZE
Definition: coap_server.h:91
#define COAP_SERVER_PRIVATE_CONTEXT
Definition: coap_server.h:110
void coapServerTask(CoapServerContext *context)
CoAP server task.
Definition: coap_server.c:319
error_t coapServerStart(CoapServerContext *context)
Start CoAP server.
Definition: coap_server.c:170
void coapServerGetDefaultSettings(CoapServerSettings *settings)
Initialize settings with default values.
Definition: coap_server.c:51
#define CoapServerContext
Definition: coap_server.h:121
error_t(* CoapServerRequestCallback)(CoapServerContext *context, CoapCode method, const char_t *uri)
CoAP request callback function.
Definition: coap_server.h:158
#define CoapDtlsSession
Definition: coap_server.h:125
error_t(* CoapServerDtlsInitCallback)(CoapServerContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition: coap_server.h:148
error_t coapServerStop(CoapServerContext *context)
Stop CoAP server.
Definition: coap_server.c:268
void coapServerDeinit(CoapServerContext *context)
Release CoAP server context.
Definition: coap_server.c:397
#define COAP_SERVER_MAX_URI_LEN
Definition: coap_server.h:98
error_t coapServerInit(CoapServerContext *context, const CoapServerSettings *settings)
CoAP server initialization.
Definition: coap_server.c:84
error_t coapServerSetCookieSecret(CoapServerContext *context, const uint8_t *cookieSecret, size_t cookieSecretLen)
Set cookie secret.
Definition: coap_server.c:140
#define COAP_SERVER_MAX_SESSIONS
Definition: coap_server.h:63
#define COAP_SERVER_BUFFER_SIZE
Definition: coap_server.h:84
error_t(* CoapServerUdpInitCallback)(CoapServerContext *context, Socket *socket)
UDP initialization callback function.
Definition: coap_server.h:137
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
General definitions for cryptographic algorithms.
error_t
Error codes.
Definition: error.h:43
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
uint32_t systime_t
System time.
thread_t * OsTaskId
Task identifier.
#define Socket
Definition: socket.h:36
DTLS session.
Definition: coap_server.h:184
uint16_t clientPort
Definition: coap_server.h:188
systime_t timestamp
Definition: coap_server.h:192
CoapServerContext * context
Definition: coap_server.h:185
TlsContext * dtlsContext
Definition: coap_server.h:190
CoAP server context.
Definition: coap_server.h:201
uint16_t clientPort
Client's port.
Definition: coap_server.h:211
CoapMessage response
CoAP response message.
Definition: coap_server.h:221
IpAddr clientIpAddr
Client's IP address.
Definition: coap_server.h:210
bool_t stop
Stop request.
Definition: coap_server.h:204
size_t cookieSecretLen
Length of the cookie secret, in bytes.
Definition: coap_server.h:214
OsTaskId taskId
Task identifier.
Definition: coap_server.h:207
uint8_t buffer[COAP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: coap_server.h:217
IpAddr serverIpAddr
Server's IP address.
Definition: coap_server.h:209
CoapServerSettings settings
User settings.
Definition: coap_server.h:202
bool_t running
Operational state of the CoAP server.
Definition: coap_server.h:203
size_t bufferLen
Length of the buffer, in bytes.
Definition: coap_server.h:218
OsTaskParameters taskParams
Task parameters.
Definition: coap_server.h:206
CoapMessage request
CoAP request message.
Definition: coap_server.h:220
CoapDtlsSession session[COAP_SERVER_MAX_SESSIONS]
DTLS sessions.
Definition: coap_server.h:215
Socket * socket
Underlying socket.
Definition: coap_server.h:208
OsEvent event
Event object used to poll the underlying socket.
Definition: coap_server.h:205
char_t uri[COAP_SERVER_MAX_URI_LEN+1]
Resource identifier.
Definition: coap_server.h:219
uint8_t cookieSecret[COAP_SERVER_MAX_COOKIE_SECRET_SIZE]
Cookie secret.
Definition: coap_server.h:213
CoAP message.
Definition: coap_message.h:56
CoAP server settings.
Definition: coap_server.h:167
OsTaskParameters task
Task parameters.
Definition: coap_server.h:168
CoapServerUdpInitCallback udpInitCallback
UDP initialization callback.
Definition: coap_server.h:171
uint16_t port
CoAP port number.
Definition: coap_server.h:170
CoapServerDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition: coap_server.h:173
NetInterface * interface
Underlying network interface.
Definition: coap_server.h:169
CoapServerRequestCallback requestCallback
CoAP request callback.
Definition: coap_server.h:175
IP network address.
Definition: ip.h:79
Event object.
Task parameters.
TLS (Transport Layer Security)
#define TlsContext
Definition: tls.h:36