mikroSDK Reference Manual
modbus_server.h
Go to the documentation of this file.
1
31#ifndef _MODBUS_SERVER_H
32#define _MODBUS_SERVER_H
33
34//Dependencies
35#include "core/net.h"
37
38//Modbus/TCP server support
39#ifndef MODBUS_SERVER_SUPPORT
40 #define MODBUS_SERVER_SUPPORT ENABLED
41#elif (MODBUS_SERVER_SUPPORT != ENABLED && MODBUS_SERVER_SUPPORT != DISABLED)
42 #error MODBUS_SERVER_SUPPORT parameter is not valid
43#endif
44
45//Modbus/TCP security
46#ifndef MODBUS_SERVER_TLS_SUPPORT
47 #define MODBUS_SERVER_TLS_SUPPORT DISABLED
48#elif (MODBUS_SERVER_TLS_SUPPORT != ENABLED && MODBUS_SERVER_TLS_SUPPORT != DISABLED)
49 #error MODBUS_SERVER_TLS_SUPPORT parameter is not valid
50#endif
51
52//Stack size required to run the Modbus/TCP server
53#ifndef MODBUS_SERVER_STACK_SIZE
54 #define MODBUS_SERVER_STACK_SIZE 650
55#elif (MODBUS_SERVER_STACK_SIZE < 1)
56 #error MODBUS_SERVER_STACK_SIZE parameter is not valid
57#endif
58
59//Priority at which the Modbus/TCP server should run
60#ifndef MODBUS_SERVER_PRIORITY
61 #define MODBUS_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
62#endif
63
64//Maximum number of simultaneous connections
65#ifndef MODBUS_SERVER_MAX_CONNECTIONS
66 #define MODBUS_SERVER_MAX_CONNECTIONS 2
67#elif (MODBUS_SERVER_MAX_CONNECTIONS < 1)
68 #error MODBUS_SERVER_MAX_CONNECTIONS parameter is not valid
69#endif
70
71//Idle connection timeout
72#ifndef MODBUS_SERVER_TIMEOUT
73 #define MODBUS_SERVER_TIMEOUT 60000
74#elif (MODBUS_SERVER_TIMEOUT < 1000)
75 #error MODBUS_SERVER_TIMEOUT parameter is not valid
76#endif
77
78//Modbus/TCP server tick interval
79#ifndef MODBUS_SERVER_TICK_INTERVAL
80 #define MODBUS_SERVER_TICK_INTERVAL 1000
81#elif (MODBUS_SERVER_TICK_INTERVAL < 100)
82 #error MODBUS_SERVER_TICK_INTERVAL parameter is not valid
83#endif
84
85//TX buffer size for TLS connections
86#ifndef MODBUS_SERVER_TLS_TX_BUFFER_SIZE
87 #define MODBUS_SERVER_TLS_TX_BUFFER_SIZE 2048
88#elif (MODBUS_SERVER_TLS_TX_BUFFER_SIZE < 512)
89 #error MODBUS_SERVER_TLS_TX_BUFFER_SIZE parameter is not valid
90#endif
91
92//RX buffer size for TLS connections
93#ifndef MODBUS_SERVER_TLS_RX_BUFFER_SIZE
94 #define MODBUS_SERVER_TLS_RX_BUFFER_SIZE 2048
95#elif (MODBUS_SERVER_TLS_RX_BUFFER_SIZE < 512)
96 #error MODBUS_SERVER_TLS_RX_BUFFER_SIZE parameter is not valid
97#endif
98
99//Maximum length of the client role OID
100#ifndef MODBUS_SERVER_MAX_ROLE_LEN
101 #define MODBUS_SERVER_MAX_ROLE_LEN 32
102#elif (MODBUS_SERVER_MAX_ROLE_LEN < 0)
103 #error MODBUS_SERVER_MAX_ROLE_LEN parameter is not valid
104#endif
105
106//Application specific context
107#ifndef MODBUS_SERVER_PRIVATE_CONTEXT
108 #define MODBUS_SERVER_PRIVATE_CONTEXT
109#endif
110
111//TLS supported?
112#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
113 #include "core/crypto.h"
114 #include "tls.h"
115 #include "tls_ticket.h"
116#endif
117
118//Forward declaration of ModbusServerContext structure
120#define ModbusServerContext struct _ModbusServerContext
121
122//Forward declaration of ModbusClientConnection structure
124#define ModbusClientConnection struct _ModbusClientConnection
125
126//C++ guard
127#ifdef __cplusplus
128extern "C" {
129#endif
130
131
136typedef enum
137{
138 MODBUS_CONNECTION_STATE_CLOSED = 0,
139 MODBUS_CONNECTION_STATE_CONNECT_TLS = 1,
140 MODBUS_CONNECTION_STATE_RECEIVE = 2,
141 MODBUS_CONNECTION_STATE_SEND = 3,
142 MODBUS_CONNECTION_STATE_SHUTDOWN_TLS = 4,
143 MODBUS_CONNECTION_STATE_SHUTDOWN_TX = 5,
144 MODBUS_CONNECTION_STATE_SHUTDOWN_RX = 6
146
147
152typedef error_t (*ModbusServerOpenCallback)(ModbusClientConnection *connection,
153 IpAddr clientIpAddr, uint16_t clientPort);
154
155
160typedef void (*ModbusServerCloseCallback)(ModbusClientConnection *connection);
161
162
163//TLS supported?
164#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
165
171 (ModbusClientConnection *connection, TlsContext *tlsContext);
172
173#endif
174
175
180typedef void (*ModbusServerLockCallback)(void);
181
182
187typedef void (*ModbusServerUnlockCallback)(void);
188
189
195 uint16_t address, bool_t *state);
196
197
203 uint16_t address, bool_t state, bool_t commit);
204
205
210typedef error_t (*ModbusServerReadRegCallback)(const char_t *role,
211 uint16_t address, uint16_t *value);
212
213
219 uint16_t address, uint16_t value, bool_t commit);
220
221
226typedef error_t (*ModbusServerProcessPduCallback)(const uint8_t *request,
227 size_t requestLen, uint8_t *response, size_t *responseLen);
228
229
234typedef void (*ModbusServerTickCallback)(ModbusServerContext *context);
235
236
264
265
271{
273 ModbusServerContext *context;
274 Socket *socket;
275#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
276 TlsContext *tlsContext;
277#endif
278 char_t role[MODBUS_SERVER_MAX_ROLE_LEN + 1];
280 uint8_t requestAdu[MODBUS_MAX_ADU_SIZE];
284 uint8_t responseAdu[MODBUS_MAX_ADU_SIZE];
287};
288
289
295{
297 bool_t running;
298 bool_t stop;
301#if (OS_STATIC_TASK_SUPPORT == ENABLED)
302 OsTaskTcb taskTcb;
303 OsStackType taskStack[MODBUS_SERVER_STACK_SIZE];
304#endif
305 Socket *socket;
306 ModbusClientConnection connection[MODBUS_SERVER_MAX_CONNECTIONS];
307#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
308 TlsTicketContext tlsTicketContext;
309#endif
310 MODBUS_SERVER_PRIVATE_CONTEXT
311};
312
313
314//Modbus/TCP server related functions
315void modbusServerGetDefaultSettings(ModbusServerSettings *settings);
316
317error_t modbusServerInit(ModbusServerContext *context,
318 const ModbusServerSettings *settings);
319
320error_t modbusServerStart(ModbusServerContext *context);
321error_t modbusServerStop(ModbusServerContext *context);
322
323void modbusServerTask(ModbusServerContext *context);
324
325void modbusServerDeinit(ModbusServerContext *context);
326
327//C++ guard
328#ifdef __cplusplus
329}
330#endif
331
332#endif
error_t
Error codes.
Definition error.h:43
Definitions common to Modbus/TCP client and server.
error_t(* ModbusServerReadCoilCallback)(const char_t *role, uint16_t address, bool_t *state)
Get coil state callback function.
Definition modbus_server.h:194
error_t(* ModbusServerWriteCoilCallback)(const char_t *role, uint16_t address, bool_t state, bool_t commit)
Set coil state callback function.
Definition modbus_server.h:202
void(* ModbusServerCloseCallback)(ModbusClientConnection *connection)
TCP connection close callback function.
Definition modbus_server.h:160
error_t(* ModbusServerProcessPduCallback)(const uint8_t *request, size_t requestLen, uint8_t *response, size_t *responseLen)
PDU processing callback function.
Definition modbus_server.h:226
error_t(* ModbusServerReadRegCallback)(const char_t *role, uint16_t address, uint16_t *value)
Get register value callback function.
Definition modbus_server.h:210
error_t(* ModbusServerWriteRegCallback)(const char_t *role, uint16_t address, uint16_t value, bool_t commit)
Set register value callback function.
Definition modbus_server.h:218
error_t(* ModbusServerOpenCallback)(ModbusClientConnection *connection, IpAddr clientIpAddr, uint16_t clientPort)
TCP connection open callback function.
Definition modbus_server.h:152
void(* ModbusServerTickCallback)(ModbusServerContext *context)
Tick callback function.
Definition modbus_server.h:234
void(* ModbusServerLockCallback)(void)
Lock Modbus table callback function.
Definition modbus_server.h:180
ModbusConnectionState
Modbus/TCP connection state.
Definition modbus_server.h:137
void(* ModbusServerUnlockCallback)(void)
Unlock Modbus table callback function.
Definition modbus_server.h:187
error_t(* ModbusServerTlsInitCallback)(ModbusClientConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
Definition modbus_server.h:171
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
IP network address.
Definition ip.h:72
Modbus/TCP server settings.
Definition modbus_server.h:242
uint16_t port
Modbus/TCP port number.
Definition modbus_server.h:244
ModbusServerReadRegCallback readInputRegCallback
Get input register value callback function.
Definition modbus_server.h:259
ModbusServerOpenCallback openCallback
TCP connection open callback function.
Definition modbus_server.h:247
ModbusServerCloseCallback closeCallback
TCP connection close callback function.
Definition modbus_server.h:248
ModbusServerWriteCoilCallback writeCoilCallback
Set coil state callback function.
Definition modbus_server.h:256
ModbusServerTlsInitCallback tlsInitCallback
TLS initialization callback function.
Definition modbus_server.h:250
ModbusServerReadCoilCallback readCoilCallback
Get coil state callback function.
Definition modbus_server.h:254
ModbusServerReadCoilCallback readDiscreteInputCallback
Get discrete input state callback function.
Definition modbus_server.h:255
systime_t timeout
Idle connection timeout.
Definition modbus_server.h:246
ModbusServerReadRegCallback readHoldingRegCallback
Get holding register value callback function.
Definition modbus_server.h:258
NetInterface * interface
Underlying network interface.
Definition modbus_server.h:243
ModbusServerReadRegCallback readRegCallback
Get register value callback function.
Definition modbus_server.h:257
ModbusServerWriteRegCallback writeRegCallback
Set register value callback function.
Definition modbus_server.h:260
ModbusServerLockCallback lockCallback
Lock Modbus table callback function.
Definition modbus_server.h:252
uint8_t unitId
Unit identifier.
Definition modbus_server.h:245
ModbusServerUnlockCallback unlockCallback
Unlock Modbus table callback function.
Definition modbus_server.h:253
ModbusServerTickCallback tickCallback
Tick callback function.
Definition modbus_server.h:262
ModbusServerProcessPduCallback processPduCallback
PDU processing callback function.
Definition modbus_server.h:261
Modbus/TCP client connection.
Definition modbus_server.h:271
uint8_t requestUnitId
Unit identifier.
Definition modbus_server.h:283
char_t role[MODBUS_SERVER_MAX_ROLE_LEN+1]
Client role OID.
Definition modbus_server.h:278
TlsContext * tlsContext
TLS context.
Definition modbus_server.h:276
systime_t timestamp
Time stamp.
Definition modbus_server.h:279
ModbusServerContext * context
Modbus/TCP server context.
Definition modbus_server.h:273
Socket * socket
Underlying socket.
Definition modbus_server.h:274
size_t responseAduPos
Current position in the response ADU.
Definition modbus_server.h:286
uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]
Request ADU.
Definition modbus_server.h:280
ModbusConnectionState state
Connection state.
Definition modbus_server.h:272
size_t requestAduLen
Length of the request ADU, in bytes.
Definition modbus_server.h:281
size_t requestAduPos
Current position in the request ADU.
Definition modbus_server.h:282
uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]
Response ADU.
Definition modbus_server.h:284
size_t responseAduLen
Length of the response ADU, in bytes.
Definition modbus_server.h:285
Modbus/TCP server context.
Definition modbus_server.h:295
OsStackType taskStack[MODBUS_SERVER_STACK_SIZE]
Task stack.
Definition modbus_server.h:303
OsEvent event
Event object used to poll the sockets.
Definition modbus_server.h:299
Socket * socket
Listening socket.
Definition modbus_server.h:305
bool_t running
Operational state of the Modbus/TCP server.
Definition modbus_server.h:297
OsTaskId taskId
Task identifier.
Definition modbus_server.h:300
bool_t stop
Stop request.
Definition modbus_server.h:298
OsTaskTcb taskTcb
Task control block.
Definition modbus_server.h:302
ModbusClientConnection connection[MODBUS_SERVER_MAX_CONNECTIONS]
Client connections.
Definition modbus_server.h:306
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
Definition modbus_server.h:308
ModbusServerSettings settings
User settings.
Definition modbus_server.h:296