mikroSDK Reference Manual
echo_server.h
Go to the documentation of this file.
1
31#ifndef _ECHO_SERVER_H
32#define _ECHO_SERVER_H
33
34//Dependencies
35#include "core/net.h"
36#include "core/socket.h"
37
38//Echo server support
39#ifndef ECHO_SERVER_SUPPORT
40 #define ECHO_SERVER_SUPPORT DISABLED
41#elif (ECHO_SERVER_SUPPORT != ENABLED && ECHO_SERVER_SUPPORT != DISABLED)
42 #error ECHO_SERVER_SUPPORT parameter is not valid
43#endif
44
45//Stack size required to run the Echo server
46#ifndef ECHO_SERVER_STACK_SIZE
47 #define ECHO_SERVER_STACK_SIZE 500
48#elif (ECHO_SERVER_STACK_SIZE < 1)
49 #error ECHO_SERVER_STACK_SIZE parameter is not valid
50#endif
51
52//Priority at which the Echo server should run
53#ifndef ECHO_SERVER_PRIORITY
54 #define ECHO_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
55#endif
56
57//TCP Echo service support
58#ifndef ECHO_SERVER_TCP_SUPPORT
59 #define ECHO_SERVER_TCP_SUPPORT ENABLED
60#elif (ECHO_SERVER_TCP_SUPPORT != ENABLED && ECHO_SERVER_TCP_SUPPORT != DISABLED)
61 #error ECHO_SERVER_TCP_SUPPORT parameter is not valid
62#endif
63
64//Maximum number of simultaneous TCP connections
65#ifndef ECHO_SERVER_MAX_TCP_CONNECTIONS
66 #define ECHO_SERVER_MAX_TCP_CONNECTIONS 2
67#elif (ECHO_SERVER_MAX_TCP_CONNECTIONS < 1)
68 #error ECHO_SERVER_MAX_TCP_CONNECTIONS parameter is not valid
69#endif
70
71//Size of the buffer for input/output operations (TCP)
72#ifndef ECHO_SERVER_TCP_BUFFER_SIZE
73 #define ECHO_SERVER_TCP_BUFFER_SIZE 512
74#elif (ECHO_SERVER_TCP_BUFFER_SIZE < 1)
75 #error ECHO_SERVER_TCP_BUFFER_SIZE parameter is not valid
76#endif
77
78//UDP Echo service support
79#ifndef ECHO_SERVER_UDP_SUPPORT
80 #define ECHO_SERVER_UDP_SUPPORT ENABLED
81#elif (ECHO_SERVER_UDP_SUPPORT != ENABLED && ECHO_SERVER_UDP_SUPPORT != DISABLED)
82 #error ECHO_SERVER_UDP_SUPPORT parameter is not valid
83#endif
84
85//Size of the buffer for input/output operations (UDP)
86#ifndef ECHO_SERVER_UDP_BUFFER_SIZE
87 #define ECHO_SERVER_UDP_BUFFER_SIZE 1472
88#elif (ECHO_SERVER_UDP_BUFFER_SIZE < 1)
89 #error ECHO_SERVER_UDP_BUFFER_SIZE parameter is not valid
90#endif
91
92//Idle connection timeout
93#ifndef ECHO_SERVER_TIMEOUT
94 #define ECHO_SERVER_TIMEOUT 30000
95#elif (ECHO_SERVER_TIMEOUT < 1)
96 #error ECHO_SERVER_TIMEOUT parameter is not valid
97#endif
98
99//Echo server tick interval
100#ifndef ECHO_SERVER_TICK_INTERVAL
101 #define ECHO_SERVER_TICK_INTERVAL 1000
102#elif (ECHO_SERVER_TICK_INTERVAL < 100)
103 #error ECHO_SERVER_TICK_INTERVAL parameter is not valid
104#endif
105
106//Application specific context
107#ifndef ECHO_SERVER_PRIVATE_CONTEXT
108 #define ECHO_SERVER_PRIVATE_CONTEXT
109#endif
110
111//Echo service port number
112#define ECHO_PORT 7
113
114//C++ guard
115#ifdef __cplusplus
116extern "C" {
117#endif
118
119
124typedef enum
125{
126 ECHO_TCP_CONNECTION_STATE_CLOSED = 0,
127 ECHO_TCP_CONNECTION_STATE_OPEN = 1
129
130
135typedef struct
136{
137 NetInterface *interface;
138 uint16_t port;
140
141
146typedef struct
147{
149 Socket *socket;
151 char_t buffer[ECHO_SERVER_TCP_BUFFER_SIZE];
152 size_t bufferLen;
153 size_t bufferPos;
155
156
161typedef struct
162{
164 bool_t running;
165 bool_t stop;
168#if (OS_STATIC_TASK_SUPPORT == ENABLED)
169 OsTaskTcb taskTcb;
170 OsStackType taskStack[ECHO_SERVER_STACK_SIZE];
171#endif
172#if (ECHO_SERVER_TCP_SUPPORT == ENABLED)
173 Socket *tcpSocket;
174 EchoTcpConnection tcpConnection[ECHO_SERVER_MAX_TCP_CONNECTIONS];
175#endif
176#if (ECHO_SERVER_UDP_SUPPORT == ENABLED)
177 Socket *udpSocket;
178 char_t udpBuffer[ECHO_SERVER_UDP_BUFFER_SIZE];
179#endif
180 ECHO_SERVER_PRIVATE_CONTEXT
182
183
184//Echo server related functions
185void echoServerGetDefaultSettings(EchoServerSettings *settings);
186
187error_t echoServerInit(EchoServerContext *context,
188 const EchoServerSettings *settings);
189
190error_t echoServerStart(EchoServerContext *context);
191error_t echoServerStop(EchoServerContext *context);
192
193void echoServerTask(EchoServerContext *context);
194
195void echoServerDeinit(EchoServerContext *context);
196
197//C++ guard
198#ifdef __cplusplus
199}
200#endif
201
202#endif
EchoTcpConnectionState
TCP connection state.
Definition echo_server.h:125
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
Socket API.
Echo server context.
Definition echo_server.h:162
bool_t running
Operational state of the Echo server.
Definition echo_server.h:164
bool_t stop
Stop request.
Definition echo_server.h:165
EchoServerSettings settings
User settings.
Definition echo_server.h:163
Socket * tcpSocket
Listening TCP socket.
Definition echo_server.h:173
OsTaskId taskId
Task identifier.
Definition echo_server.h:167
OsEvent event
Event object used to poll the sockets.
Definition echo_server.h:166
Socket * udpSocket
UDP socket.
Definition echo_server.h:177
OsTaskTcb taskTcb
Task control block.
Definition echo_server.h:169
Echo server settings.
Definition echo_server.h:136
uint16_t port
Echo server port number.
Definition echo_server.h:138
NetInterface * interface
Underlying network interface.
Definition echo_server.h:137
Echo TCP connection.
Definition echo_server.h:147
EchoTcpConnectionState state
Connection state.
Definition echo_server.h:148
size_t bufferPos
Current position in the buffer.
Definition echo_server.h:153
size_t bufferLen
Length of the buffer, in bytes.
Definition echo_server.h:152
systime_t timestamp
Time stamp.
Definition echo_server.h:150
Socket * socket
Underlying TCP socket.
Definition echo_server.h:149