mikroSDK Reference Manual
coap_server.h
Go to the documentation of this file.
1
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
120struct _CoapServerContext;
121#define CoapServerContext struct _CoapServerContext
122
123//Forward declaration of CoapDtlsSession structure
124struct _CoapDtlsSession;
125#define CoapDtlsSession struct _CoapDtlsSession
126
127//C++ guard
128#ifdef __cplusplus
129extern "C" {
130#endif
131
132
133//DTLS supported?
134#if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
135
140typedef error_t (*CoapServerDtlsInitCallback)(CoapServerContext *context,
141 TlsContext *dtlsContext);
142
143#endif
144
145
150typedef error_t (*CoapServerRequestCallback)(CoapServerContext *context,
151 CoapCode method, const char_t *uri);
152
153
158typedef struct
159{
160 NetInterface *interface;
161 uint16_t port;
162#if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
164#endif
167
168
174{
175 CoapServerContext *context;
176 IpAddr serverIpAddr;
177 IpAddr clientIpAddr;
178 uint16_t clientPort;
179#if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
180 TlsContext *dtlsContext;
181#endif
182 systime_t timestamp;
183};
184
185
191{
193 bool_t running;
194 bool_t stop;
197#if (OS_STATIC_TASK_SUPPORT == ENABLED)
198 OsTaskTcb taskTcb;
199 OsStackType taskStack[COAP_SERVER_STACK_SIZE];
200#endif
201 Socket *socket;
204 uint16_t clientPort;
205#if (COAP_SERVER_DTLS_SUPPORT == ENABLED)
206 uint8_t cookieSecret[COAP_SERVER_MAX_COOKIE_SECRET_SIZE];
208 CoapDtlsSession session[COAP_SERVER_MAX_SESSIONS];
209#endif
210 uint8_t buffer[COAP_SERVER_BUFFER_SIZE];
211 size_t bufferLen;
212 char_t uri[COAP_SERVER_MAX_URI_LEN + 1];
215 COAP_SERVER_PRIVATE_CONTEXT
216};
217
218
219//CoAP server related functions
220void coapServerGetDefaultSettings(CoapServerSettings *settings);
221
222error_t coapServerInit(CoapServerContext *context,
223 const CoapServerSettings *settings);
224
225error_t coapServerSetCookieSecret(CoapServerContext *context,
226 const uint8_t *cookieSecret, size_t cookieSecretLen);
227
228error_t coapServerStart(CoapServerContext *context);
229error_t coapServerStop(CoapServerContext *context);
230
231void coapServerTask(CoapServerContext *context);
232
233void coapServerDeinit(CoapServerContext *context);
234
235//C++ guard
236#ifdef __cplusplus
237}
238#endif
239
240#endif
Definitions common to CoAP client and server.
CoapCode
CoAP method and response codes.
Definition coap_common.h:113
CoAP message formatting and parsing.
CoAP option formatting and parsing.
error_t(* CoapServerRequestCallback)(CoapServerContext *context, CoapCode method, const char_t *uri)
CoAP request callback function.
Definition coap_server.h:150
error_t(* CoapServerDtlsInitCallback)(CoapServerContext *context, TlsContext *dtlsContext)
DTLS initialization callback.
Definition coap_server.h:140
error_t
Error codes.
Definition error.h:43
TCP/IP stack core.
uint_t OsEvent
Event object.
Definition os_port_none.h:104
uint_t OsTaskId
Task identifier.
Definition os_port_none.h:97
uint32_t systime_t
System time.
Definition os_port_none.h:90
CoAP message.
Definition coap_message.h:56
CoAP server settings.
Definition coap_server.h:159
CoapServerDtlsInitCallback dtlsInitCallback
DTLS initialization callback.
Definition coap_server.h:163
CoapServerRequestCallback requestCallback
CoAP request callback.
Definition coap_server.h:165
uint16_t port
CoAP port number.
Definition coap_server.h:161
NetInterface * interface
Underlying network interface.
Definition coap_server.h:160
IP network address.
Definition ip.h:72
DTLS session.
Definition coap_server.h:174
CoAP server context.
Definition coap_server.h:191
size_t bufferLen
Length of the buffer, in bytes.
Definition coap_server.h:211
CoapDtlsSession session[COAP_SERVER_MAX_SESSIONS]
DTLS sessions.
Definition coap_server.h:208
Socket * socket
Underlying socket.
Definition coap_server.h:201
CoapMessage response
CoAP response message.
Definition coap_server.h:214
OsStackType taskStack[COAP_SERVER_STACK_SIZE]
Task stack.
Definition coap_server.h:199
char_t uri[COAP_SERVER_MAX_URI_LEN+1]
Resource identifier.
Definition coap_server.h:212
size_t cookieSecretLen
Length of the cookie secret, in bytes.
Definition coap_server.h:207
bool_t running
Operational state of the CoAP server.
Definition coap_server.h:193
CoapMessage request
CoAP request message.
Definition coap_server.h:213
bool_t stop
Stop request.
Definition coap_server.h:194
OsTaskId taskId
Task identifier.
Definition coap_server.h:196
uint16_t clientPort
Client's port.
Definition coap_server.h:204
IpAddr clientIpAddr
Client's IP address.
Definition coap_server.h:203
IpAddr serverIpAddr
Server's IP address.
Definition coap_server.h:202
CoapServerSettings settings
User settings.
Definition coap_server.h:192
OsTaskTcb taskTcb
Task control block.
Definition coap_server.h:198
uint8_t cookieSecret[COAP_SERVER_MAX_COOKIE_SECRET_SIZE]
Cookie secret.
Definition coap_server.h:206
uint8_t buffer[COAP_SERVER_BUFFER_SIZE]
Memory buffer for input/output operations.
Definition coap_server.h:210
OsEvent event
Event object used to poll the underlying socket.
Definition coap_server.h:195