31#ifndef _MODBUS_SERVER_H
32#define _MODBUS_SERVER_H
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
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
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
60#ifndef MODBUS_SERVER_PRIORITY
61 #define MODBUS_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
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
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
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
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
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
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
107#ifndef MODBUS_SERVER_PRIVATE_CONTEXT
108 #define MODBUS_SERVER_PRIVATE_CONTEXT
112#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
113 #include "core/crypto.h"
115 #include "tls_ticket.h"
120#define ModbusServerContext struct _ModbusServerContext
124#define ModbusClientConnection struct _ModbusClientConnection
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
153 IpAddr clientIpAddr, uint16_t clientPort);
164#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
171 (ModbusClientConnection *connection, TlsContext *
tlsContext);
195 uint16_t address, bool_t *
state);
203 uint16_t address, bool_t
state, bool_t commit);
211 uint16_t address, uint16_t *value);
219 uint16_t address, uint16_t value, bool_t commit);
227 size_t requestLen, uint8_t *response,
size_t *responseLen);
249#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
275#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
278 char_t
role[MODBUS_SERVER_MAX_ROLE_LEN + 1];
301#if (OS_STATIC_TASK_SUPPORT == ENABLED)
306 ModbusClientConnection
connection[MODBUS_SERVER_MAX_CONNECTIONS];
307#if (MODBUS_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
310 MODBUS_SERVER_PRIVATE_CONTEXT
317error_t modbusServerInit(ModbusServerContext *context,
320error_t modbusServerStart(ModbusServerContext *context);
321error_t modbusServerStop(ModbusServerContext *context);
323void modbusServerTask(ModbusServerContext *context);
325void modbusServerDeinit(ModbusServerContext *context);
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
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