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-2019 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 1.9.6
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 //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 //Maximum time the server will wait before closing the connection
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 //TLS supported?
107 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
108  #include "core/crypto.h"
109  #include "tls.h"
110  #include "tls_ticket.h"
111 #endif
112 
113 //Forward declaration of ModbusServerContext structure
114 struct _ModbusServerContext;
115 #define ModbusServerContext struct _ModbusServerContext
116 
117 //Forward declaration of ModbusClientConnection structure
119 #define ModbusClientConnection struct _ModbusClientConnection
120 
121 //C++ guard
122 #ifdef __cplusplus
123 extern "C" {
124 #endif
125 
126 
127 /**
128  * @brief Modbus/TCP connection state
129  **/
130 
131 typedef enum
132 {
141 
142 
143 //TLS supported?
144 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
145 
146 /**
147  * @brief TLS initialization callback function
148  **/
149 
152 
153 #endif
154 
155 
156 /**
157  * @brief Lock Modbus table callback function
158  **/
159 
160 typedef void (*ModbusServerLockCallback)(void);
161 
162 
163 /**
164  * @brief Unlock Modbus table callback function
165  **/
166 
167 typedef void (*ModbusServerUnlockCallback)(void);
168 
169 
170 /**
171  * @brief Get coil state callback function
172  **/
173 
175  uint16_t address, bool_t *state);
176 
177 
178 /**
179  * @brief Set coil state callback function
180  **/
181 
183  uint16_t address, bool_t state, bool_t commit);
184 
185 
186 /**
187  * @brief Get register value callback function
188  **/
189 
191  uint16_t address, uint16_t *value);
192 
193 
194 /**
195  * @brief Set register value callback function
196  **/
197 
199  uint16_t address, uint16_t value, bool_t commit);
200 
201 
202 /**
203  * @brief PDU processing callback function
204  **/
205 
206 typedef error_t (*ModbusServerProcessPduCallback)(const uint8_t *request,
207  size_t requestLen, uint8_t *response, size_t *responseLen);
208 
209 
210 /**
211  * @brief Modbus/TCP server settings
212  **/
213 
214 typedef struct
215 {
216  NetInterface *interface; ///<Underlying network interface
217  uint16_t port; ///<Modbus/TCP port number
218  uint8_t unitId; ///<Unit identifier
219 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
220  ModbusServerTlsInitCallback tlsInitCallback; ///<TLS initialization callback function
221 #endif
222  ModbusServerLockCallback lockCallback; ///<Lock Modbus table callback function
223  ModbusServerUnlockCallback unlockCallback; ///<Unlock Modbus table callback function
224  ModbusServerReadCoilCallback readCoilCallback; ///<Get coil state callback function
225  ModbusServerReadCoilCallback readDiscreteInputCallback; ///<Get discrete input state callback function
226  ModbusServerWriteCoilCallback writeCoilCallback; ///<Set coil state callback function
227  ModbusServerReadRegCallback readRegCallback; ///<Get register value callback function
228  ModbusServerReadRegCallback readHoldingRegCallback; ///<Get holding register value callback function
229  ModbusServerReadRegCallback readInputRegCallback; ///<Get input register value callback function
230  ModbusServerWriteRegCallback writeRegCallback; ///<Set register value callback function
233 
234 
235 /**
236  * @brief Modbus/TCP client connection
237  **/
238 
240 {
241  ModbusConnectionState state; ///<Connection state
242  ModbusServerContext *context; ///<Modbus/TCP server context
243  Socket *socket; ///<Underlying socket
244 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
245  TlsContext *tlsContext; ///<TLS context
246 #endif
247  char_t role[MODBUS_SERVER_MAX_ROLE_LEN + 1]; ///<Client role OID
248  systime_t timestamp; ///<Time stamp
249  uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]; ///<Request ADU
250  size_t requestAduLen; ///<Length of the request ADU, in bytes
251  size_t requestAduPos; ///<Current position in the request ADU
252  uint8_t requestUnitId; ///<Unit identifier
253  uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]; ///<Response ADU
254  size_t responseAduLen; ///<Length of the response ADU, in bytes
255  size_t responseAduPos; ///<Current position in the response ADU
256 };
257 
258 
259 /**
260  * @brief Modbus/TCP server context
261  **/
262 
264 {
265  ModbusServerSettings settings; ///<User settings
266  OsEvent event; ///<Event object used to poll the sockets
267  Socket *socket; ///<Listening socket
269 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED && TLS_TICKET_SUPPORT == ENABLED)
270  TlsTicketContext tlsTicketContext; ///<TLS ticket encryption context
271 #endif
272 };
273 
274 
275 //Modbus/TCP server related functions
277 
279  const ModbusServerSettings *settings);
280 
282 
284 
286 
287 //C++ guard
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #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:49
ModbusServerTlsInitCallback tlsInitCallback
TLS initialization callback function.
@ MODBUS_CONNECTION_STATE_SEND
error_t modbusServerStart(ModbusServerContext *context)
Start Modbus/TCP server.
uint8_t requestUnitId
Unit identifier.
ModbusConnectionState state
Connection state.
@ 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.
@ 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.
Session ticket encryption context.
Definition: tls_ticket.h:90
#define TlsContext
Definition: tls.h:36
error_t
Error codes.
Definition: error.h:42
ModbusServerReadCoilCallback readCoilCallback
Get coil state callback function.
ModbusServerContext * context
Modbus/TCP server context.
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.
ModbusServerReadRegCallback readRegCallback
Get register value callback function.
size_t requestAduPos
Current position in the request ADU.
General definitions for cryptographic algorithms.
TlsTicketContext tlsTicketContext
TLS ticket encryption context.
ModbusClientConnection connection[MODBUS_SERVER_MAX_CONNECTIONS]
Client connections.
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.
TLS session tickets.
uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]
Response ADU.
char char_t
Definition: compiler_port.h:43
void(* ModbusServerLockCallback)(void)
Lock Modbus table callback function.
Definitions common to Modbus/TCP client and server.
#define Socket
Definition: socket.h:36
error_t modbusServerInit(ModbusServerContext *context, const ModbusServerSettings *settings)
Initialize Modbus/TCP server context.
Definition: modbus_server.c:94
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.
Ipv6Addr address
ModbusServerWriteRegCallback writeRegCallback
Set register value callback function.
size_t responseAduPos
Current position in the response ADU.
uint8_t value[]
Definition: dtls_misc.h:150
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:66
TCP/IP stack core.
#define ModbusServerContext
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TX
uint32_t systime_t
Definition: compiler_port.h:46
error_t(* ModbusServerWriteCoilCallback)(const char_t *role, uint16_t address, bool_t state, bool_t commit)
Set coil state callback function.
ModbusServerSettings settings
User settings.
#define MODBUS_MAX_ADU_SIZE
Definition: modbus_common.h:50
ModbusConnectionState
Modbus/TCP connection state.