modbus_server.h
Go to the documentation of this file.
1 /**
2  * @file modbus_server.h
3  * @brief Modbus/TCP 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 _MODBUS_SERVER_H
32 #define _MODBUS_SERVER_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "modbus/modbus_common.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 //Modbus diagnostics
53 #ifndef MODBUS_SERVER_DIAG_SUPPORT
54  #define MODBUS_SERVER_DIAG_SUPPORT DISABLED
55 #elif (MODBUS_SERVER_DIAG_SUPPORT != ENABLED && MODBUS_SERVER_DIAG_SUPPORT != DISABLED)
56  #error MODBUS_SERVER_DIAG_SUPPORT parameter is not valid
57 #endif
58 
59 //Stack size required to run the Modbus/TCP server
60 #ifndef MODBUS_SERVER_STACK_SIZE
61  #define MODBUS_SERVER_STACK_SIZE 650
62 #elif (MODBUS_SERVER_STACK_SIZE < 1)
63  #error MODBUS_SERVER_STACK_SIZE parameter is not valid
64 #endif
65 
66 //Priority at which the Modbus/TCP server should run
67 #ifndef MODBUS_SERVER_PRIORITY
68  #define MODBUS_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
69 #endif
70 
71 //Maximum number of simultaneous connections
72 #ifndef MODBUS_SERVER_MAX_CONNECTIONS
73  #define MODBUS_SERVER_MAX_CONNECTIONS 2
74 #elif (MODBUS_SERVER_MAX_CONNECTIONS < 1)
75  #error MODBUS_SERVER_MAX_CONNECTIONS parameter is not valid
76 #endif
77 
78 //Idle connection timeout
79 #ifndef MODBUS_SERVER_TIMEOUT
80  #define MODBUS_SERVER_TIMEOUT 60000
81 #elif (MODBUS_SERVER_TIMEOUT < 1000)
82  #error MODBUS_SERVER_TIMEOUT parameter is not valid
83 #endif
84 
85 //Modbus/TCP server tick interval
86 #ifndef MODBUS_SERVER_TICK_INTERVAL
87  #define MODBUS_SERVER_TICK_INTERVAL 1000
88 #elif (MODBUS_SERVER_TICK_INTERVAL < 100)
89  #error MODBUS_SERVER_TICK_INTERVAL parameter is not valid
90 #endif
91 
92 //TX buffer size for TLS connections
93 #ifndef MODBUS_SERVER_TLS_TX_BUFFER_SIZE
94  #define MODBUS_SERVER_TLS_TX_BUFFER_SIZE 2048
95 #elif (MODBUS_SERVER_TLS_TX_BUFFER_SIZE < 512)
96  #error MODBUS_SERVER_TLS_TX_BUFFER_SIZE parameter is not valid
97 #endif
98 
99 //RX buffer size for TLS connections
100 #ifndef MODBUS_SERVER_TLS_RX_BUFFER_SIZE
101  #define MODBUS_SERVER_TLS_RX_BUFFER_SIZE 2048
102 #elif (MODBUS_SERVER_TLS_RX_BUFFER_SIZE < 512)
103  #error MODBUS_SERVER_TLS_RX_BUFFER_SIZE parameter is not valid
104 #endif
105 
106 //Maximum length of the client role OID
107 #ifndef MODBUS_SERVER_MAX_ROLE_LEN
108  #define MODBUS_SERVER_MAX_ROLE_LEN 32
109 #elif (MODBUS_SERVER_MAX_ROLE_LEN < 0)
110  #error MODBUS_SERVER_MAX_ROLE_LEN parameter is not valid
111 #endif
112 
113 //Application specific context
114 #ifndef MODBUS_SERVER_PRIVATE_CONTEXT
115  #define MODBUS_SERVER_PRIVATE_CONTEXT
116 #endif
117 
118 //TLS supported?
119 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
120  #include "core/crypto.h"
121  #include "tls.h"
122  #include "tls_ticket.h"
123 #endif
124 
125 //Forward declaration of ModbusServerContext structure
126 struct _ModbusServerContext;
127 #define ModbusServerContext struct _ModbusServerContext
128 
129 //Forward declaration of ModbusClientConnection structure
131 #define ModbusClientConnection struct _ModbusClientConnection
132 
133 //C++ guard
134 #ifdef __cplusplus
135 extern "C" {
136 #endif
137 
138 
139 /**
140  * @brief Modbus/TCP connection state
141  **/
142 
143 typedef enum
144 {
153 
154 
155 /**
156  * @brief TCP connection open callback function
157  **/
158 
160  IpAddr clientIpAddr, uint16_t clientPort);
161 
162 
163 /**
164  * @brief TCP connection close callback function
165  **/
166 
168 
169 
170 //TLS supported?
171 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
172 
173 /**
174  * @brief TLS initialization callback function
175  **/
176 
179 
180 #endif
181 
182 
183 /**
184  * @brief Lock Modbus table callback function
185  **/
186 
187 typedef void (*ModbusServerLockCallback)(void);
188 
189 
190 /**
191  * @brief Unlock Modbus table callback function
192  **/
193 
194 typedef void (*ModbusServerUnlockCallback)(void);
195 
196 
197 /**
198  * @brief Get coil state callback function
199  **/
200 
202  uint16_t address, bool_t *state);
203 
204 
205 /**
206  * @brief Set coil state callback function
207  **/
208 
210  uint16_t address, bool_t state, bool_t commit);
211 
212 
213 /**
214  * @brief Get register value callback function
215  **/
216 
218  uint16_t address, uint16_t *value);
219 
220 
221 /**
222  * @brief Set register value callback function
223  **/
224 
226  uint16_t address, uint16_t value, bool_t commit);
227 
228 
229 /**
230  * @brief PDU processing callback function
231  **/
232 
233 typedef error_t (*ModbusServerProcessPduCallback)(const uint8_t *request,
234  size_t requestLen, uint8_t *response, size_t *responseLen);
235 
236 
237 /**
238  * @brief Tick callback function
239  **/
240 
242 
243 
244 /**
245  * @brief Modbus/TCP server settings
246  **/
247 
248 typedef struct
249 {
250  OsTaskParameters task; ///<Task parameters
251  NetInterface *interface; ///<Underlying network interface
252  uint16_t port; ///<Modbus/TCP port number
253  uint8_t unitId; ///<Unit identifier
254  systime_t timeout; ///<Idle connection timeout
255  ModbusServerOpenCallback openCallback; ///<TCP connection open callback function
256  ModbusServerCloseCallback closeCallback; ///<TCP connection close callback function
257 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
258  ModbusServerTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
259 #endif
260  ModbusServerLockCallback lockCallback; ///<Lock Modbus table callback function
261  ModbusServerUnlockCallback unlockCallback; ///<Unlock Modbus table callback function
262  ModbusServerReadCoilCallback readCoilCallback; ///<Get coil state callback function
263  ModbusServerReadCoilCallback readDiscreteInputCallback; ///<Get discrete input state callback function
264  ModbusServerWriteCoilCallback writeCoilCallback; ///<Set coil state callback function
265  ModbusServerReadRegCallback readRegCallback; ///<Get register value callback function
266  ModbusServerReadRegCallback readHoldingRegCallback; ///<Get holding register value callback function
267  ModbusServerReadRegCallback readInputRegCallback; ///<Get input register value callback function
268  ModbusServerWriteRegCallback writeRegCallback; ///<Set register value callback function
269  ModbusServerProcessPduCallback processPduCallback; ///<PDU processing callback function
270  ModbusServerTickCallback tickCallback; ///<Tick callback function
272 
273 
274 /**
275  * @brief Modbus/TCP client connection
276  **/
277 
279 {
280  ModbusConnectionState state; ///<Connection state
281  ModbusServerContext *context; ///<Modbus/TCP server context
282  Socket *socket; ///<Underlying socket
283 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
284  TlsContext *tlsContext; ///<TLS context
285 #endif
286  char_t role[MODBUS_SERVER_MAX_ROLE_LEN + 1]; ///<Client role OID
287  systime_t timestamp; ///<Time stamp
288  uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]; ///<Request ADU
289  size_t requestAduLen; ///<Length of the request ADU, in bytes
290  size_t requestAduPos; ///<Current position in the request ADU
291  uint8_t requestUnitId; ///<Unit identifier
292  uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]; ///<Response ADU
293  size_t responseAduLen; ///<Length of the response ADU, in bytes
294  size_t responseAduPos; ///<Current position in the response ADU
295 };
296 
297 
298 /**
299  * @brief Modbus/TCP server context
300  **/
301 
303 {
304  ModbusServerSettings settings; ///<User settings
305  bool_t running; ///<Operational state of the Modbus/TCP server
306  bool_t stop; ///<Stop request
307  OsEvent event; ///<Event object used to poll the sockets
308  OsTaskParameters taskParams; ///<Task parameters
309  OsTaskId taskId; ///<Task identifier
310  Socket *socket; ///<Listening socket
312 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
313  TlsTicketContext tlsTicketContext; ///<TLS ticket encryption context
314 #endif
315 #if (MODBUS_SERVER_DIAG_SUPPORT == ENABLED)
316  uint32_t rxMessageCount; ///<Total number of messages received
317  uint32_t txMessageCount; ///<Total number of messages sent
318  uint32_t commErrorCount; ///<Total number of communication errors
319  uint32_t exceptionErrorCount; ///<Total number of exception errors
320 #endif
321  MODBUS_SERVER_PRIVATE_CONTEXT ///<Application specific context
322 };
323 
324 
325 //Modbus/TCP server related functions
327 
329  const ModbusServerSettings *settings);
330 
333 
335 
337 
338 //C++ guard
339 #ifdef __cplusplus
340 }
341 #endif
342 
343 #endif
uint8_t unitId
Unit identifier.
OsEvent event
Event object used to poll the sockets.
systime_t timestamp
Time stamp.
error_t(* ModbusServerReadRegCallback)(const char_t *role, uint16_t address, uint16_t *value)
Get register value callback function.
Modbus/TCP server settings.
int bool_t
Definition: compiler_port.h:53
ModbusServerTlsInitCallback tlsInitCallback
TLS initialization callback function.
uint32_t txMessageCount
Total number of messages sent.
@ MODBUS_CONNECTION_STATE_SEND
error_t modbusServerStart(ModbusServerContext *context)
Start Modbus/TCP server.
bool_t stop
Stop request.
ModbusServerOpenCallback openCallback
TCP connection open callback function.
void(* ModbusServerCloseCallback)(ModbusClientConnection *connection)
TCP connection close callback function.
uint8_t requestUnitId
Unit identifier.
ModbusConnectionState state
Connection state.
IP network address.
Definition: ip.h:90
@ MODBUS_CONNECTION_STATE_CONNECT_TLS
ModbusServerReadCoilCallback readDiscreteInputCallback
Get discrete input state callback function.
uint16_t port
Modbus/TCP port number.
Event object.
ModbusServerUnlockCallback unlockCallback
Unlock Modbus table callback function.
size_t responseAduLen
Length of the response ADU, in bytes.
Socket * socket
Listening socket.
uint32_t exceptionErrorCount
Total number of exception errors.
@ MODBUS_CONNECTION_STATE_RECEIVE
ModbusServerReadRegCallback readHoldingRegCallback
Get holding register value callback function.
void modbusServerDeinit(ModbusServerContext *context)
Release Modbus/TCP server context.
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TLS
size_t requestAduLen
Length of the request ADU, in bytes.
uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]
Request ADU.
error_t(* ModbusServerWriteRegCallback)(const char_t *role, uint16_t address, uint16_t value, bool_t commit)
Set register value callback function.
ModbusServerWriteCoilCallback writeCoilCallback
Set coil state callback function.
#define ModbusClientConnection
error_t(* ModbusServerTlsInitCallback)(ModbusClientConnection *connection, TlsContext *tlsContext)
TLS initialization callback function.
ModbusServerLockCallback lockCallback
Lock Modbus table callback function.
NetInterface * interface
Underlying network interface.
error_t(* ModbusServerProcessPduCallback)(const uint8_t *request, size_t requestLen, uint8_t *response, size_t *responseLen)
PDU processing callback function.
ModbusServerCloseCallback closeCallback
TCP connection close callback function.
Session ticket encryption context.
Definition: tls_ticket.h:91
bool_t running
Operational state of the Modbus/TCP server.
error_t modbusServerStop(ModbusServerContext *context)
Stop Modbus/TCP server.
OsTaskParameters task
Task parameters.
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:43
ModbusServerReadCoilCallback readCoilCallback
Get coil state callback function.
ModbusServerContext * context
Modbus/TCP server context.
uint32_t rxMessageCount
Total number of messages received.
Modbus/TCP server context.
@ MODBUS_CONNECTION_STATE_SHUTDOWN_RX
void modbusServerTask(ModbusServerContext *context)
Modbus/TCP server task.
#define MODBUS_SERVER_MAX_ROLE_LEN
#define NetInterface
Definition: net.h:36
ModbusServerProcessPduCallback processPduCallback
PDU processing callback function.
ModbusServerReadRegCallback readRegCallback
Get register value callback function.
size_t requestAduPos
Current position in the request ADU.
ModbusServerTickCallback tickCallback
Tick callback function.
General definitions for cryptographic algorithms.
#define MODBUS_SERVER_PRIVATE_CONTEXT
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
ModbusClientConnection connection[MODBUS_SERVER_MAX_CONNECTIONS]
Client connections.
Task parameters.
error_t(* ModbusServerReadCoilCallback)(const char_t *role, uint16_t address, bool_t *state)
Get coil state callback function.
void modbusServerGetDefaultSettings(ModbusServerSettings *settings)
Initialize settings with default values.
Definition: modbus_server.c:49
@ MODBUS_CONNECTION_STATE_CLOSED
ModbusServerReadRegCallback readInputRegCallback
Get input register value callback function.
void(* ModbusServerTickCallback)(ModbusServerContext *context)
Tick callback function.
TLS session tickets.
uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]
Response ADU.
uint32_t systime_t
System time.
uint32_t commErrorCount
Total number of communication errors.
char char_t
Definition: compiler_port.h:48
void(* ModbusServerLockCallback)(void)
Lock Modbus table callback function.
error_t(* ModbusServerOpenCallback)(ModbusClientConnection *connection, IpAddr clientIpAddr, uint16_t clientPort)
TCP connection open callback function.
Definitions common to Modbus/TCP client and server.
Ipv6Addr address[]
Definition: ipv6.h:325
#define Socket
Definition: socket.h:36
error_t modbusServerInit(ModbusServerContext *context, const ModbusServerSettings *settings)
Initialize Modbus/TCP server context.
systime_t timeout
Idle connection timeout.
uint8_t value[]
Definition: tcp.h:369
Modbus/TCP client connection.
char_t role[MODBUS_SERVER_MAX_ROLE_LEN+1]
Client role OID.
Socket * socket
Underlying socket.
TLS (Transport Layer Security)
void(* ModbusServerUnlockCallback)(void)
Unlock Modbus table callback function.
TlsContext * tlsContext
TLS context.
ModbusServerWriteRegCallback writeRegCallback
Set register value callback function.
size_t responseAduPos
Current position in the response ADU.
thread_t * OsTaskId
Task identifier.
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:73
TCP/IP stack core.
#define ModbusServerContext
OsTaskId taskId
Task identifier.
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TX
error_t(* ModbusServerWriteCoilCallback)(const char_t *role, uint16_t address, bool_t state, bool_t commit)
Set coil state callback function.
OsTaskParameters taskParams
Task parameters.
ModbusServerSettings settings
User settings.
#define MODBUS_MAX_ADU_SIZE
Definition: modbus_common.h:50
ModbusConnectionState
Modbus/TCP connection state.