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.4
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
CoapDtlsSession session[COAP_SERVER_MAX_SESSIONS]
DTLS sessions.
Definition: coap_server.h:215
OsTaskParameters taskParams
Task parameters.
Definition: coap_server.h:206
NetInterface * interface
Underlying network interface.
Definition: coap_server.h:169
error_t(* CoapServerUdpInitCallback)(CoapServerContext *context, Socket *socket)
UDP initialization callback function.
Definition: coap_server.h:137
int bool_t
Definition: compiler_port.h:53
IP network address.
Definition: ip.h:90
Event object.
CoapServerUdpInitCallback udpInitCallback
UDP initialization callback.
Definition: coap_server.h:171
CoAP server context.
Definition: coap_server.h:201
#define COAP_SERVER_MAX_SESSIONS
Definition: coap_server.h:63
CoAP message formatting and parsing.
uint16_t clientPort
Definition: coap_server.h:188
OsTaskId taskId
Task identifier.
Definition: coap_server.h:207
uint16_t port
CoAP port number.
Definition: coap_server.h:170
OsTaskParameters task
Task parameters.
Definition: coap_server.h:168
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
OsEvent event
Event object used to poll the underlying socket.
Definition: coap_server.h:205
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
char_t uri[COAP_SERVER_MAX_URI_LEN+1]
Resource identifier.
Definition: coap_server.h:219
CoAP message.
Definition: coap_message.h:56
#define NetInterface
Definition: net.h:36
bool_t stop
Stop request.
Definition: coap_server.h:204
void coapServerDeinit(CoapServerContext *context)
Release CoAP server context.
Definition: coap_server.c:399
CoapMessage response
CoAP response message.
Definition: coap_server.h:221
error_t coapServerStop(CoapServerContext *context)
Stop CoAP server.
Definition: coap_server.c:268
General definitions for cryptographic algorithms.
Task parameters.
size_t bufferLen
Length of the buffer, in bytes.
Definition: coap_server.h:218
IpAddr serverIpAddr
Server's IP address.
Definition: coap_server.h:209
#define CoapDtlsSession
Definition: coap_server.h:125
uint32_t systime_t
System time.
uint8_t buffer[COAP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition: coap_server.h:217
char char_t
Definition: compiler_port.h:48
CoapServerContext * context
Definition: coap_server.h:185
uint16_t clientPort
Client's port.
Definition: coap_server.h:211
TlsContext * dtlsContext
Definition: coap_server.h:190
#define COAP_SERVER_MAX_URI_LEN
Definition: coap_server.h:98
#define CoapServerContext
Definition: coap_server.h:121
CoapServerRequestCallback requestCallback
CoAP request callback.
Definition: coap_server.h:175
#define Socket
Definition: socket.h:36
uint8_t cookieSecret[COAP_SERVER_MAX_COOKIE_SECRET_SIZE]
Cookie secret.
Definition: coap_server.h:213
error_t(* CoapServerRequestCallback)(CoapServerContext *context, CoapCode method, const char_t *uri)
CoAP request callback function.
Definition: coap_server.h:158
CoapMessage request
CoAP request message.
Definition: coap_server.h:220
void coapServerGetDefaultSettings(CoapServerSettings *settings)
Initialize settings with default values.
Definition: coap_server.c:51
IpAddr clientIpAddr
Client's IP address.
Definition: coap_server.h:210
TLS (Transport Layer Security)
error_t coapServerStart(CoapServerContext *context)
Start CoAP server.
Definition: coap_server.c:170
CoapServerSettings settings
User settings.
Definition: coap_server.h:202
#define COAP_SERVER_PRIVATE_CONTEXT
Definition: coap_server.h:110
CoapServerDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition: coap_server.h:173
bool_t running
Operational state of the CoAP server.
Definition: coap_server.h:203
thread_t * OsTaskId
Task identifier.
#define COAP_SERVER_MAX_COOKIE_SECRET_SIZE
Definition: coap_server.h:91
systime_t timestamp
Definition: coap_server.h:192
TCP/IP stack core.
error_t(* CoapServerDtlsInitCallback)(CoapServerContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition: coap_server.h:148
CoAP server settings.
Definition: coap_server.h:167
void coapServerTask(CoapServerContext *context)
CoAP server task.
Definition: coap_server.c:321
size_t cookieSecretLen
Length of the cookie secret, in bytes.
Definition: coap_server.h:214
Socket * socket
Underlying socket.
Definition: coap_server.h:208
#define COAP_SERVER_BUFFER_SIZE
Definition: coap_server.h:84
CoapCode
CoAP method and response codes.
Definition: coap_common.h:113
DTLS session.
Definition: coap_server.h:184
Definitions common to CoAP client and server.
Formatting and parsing of CoAP options.
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