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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _MODBUS_SERVER_H
30 #define _MODBUS_SERVER_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "modbus/modbus_common.h"
35 
36 //Modbus/TCP server support
37 #ifndef MODBUS_SERVER_SUPPORT
38  #define MODBUS_SERVER_SUPPORT ENABLED
39 #elif (MODBUS_SERVER_SUPPORT != ENABLED && MODBUS_SERVER_SUPPORT != DISABLED)
40  #error MODBUS_SERVER_SUPPORT parameter is not valid
41 #endif
42 
43 //Stack size required to run the Modbus/TCP server
44 #ifndef MODBUS_SERVER_STACK_SIZE
45  #define MODBUS_SERVER_STACK_SIZE 650
46 #elif (MODBUS_SERVER_STACK_SIZE < 1)
47  #error MODBUS_SERVER_STACK_SIZE parameter is not valid
48 #endif
49 
50 //Priority at which the Modbus/TCP server should run
51 #ifndef MODBUS_SERVER_PRIORITY
52  #define MODBUS_SERVER_PRIORITY OS_TASK_PRIORITY_NORMAL
53 #endif
54 
55 //Maximum number of simultaneous connections
56 #ifndef MODBUS_SERVER_MAX_CONNECTIONS
57  #define MODBUS_SERVER_MAX_CONNECTIONS 2
58 #elif (MODBUS_SERVER_MAX_CONNECTIONS < 1)
59  #error MODBUS_SERVER_MAX_CONNECTIONS parameter is not valid
60 #endif
61 
62 //Maximum time the server will wait before closing the connection
63 #ifndef MODBUS_SERVER_TIMEOUT
64  #define MODBUS_SERVER_TIMEOUT 60000
65 #elif (MODBUS_SERVER_TIMEOUT < 1000)
66  #error MODBUS_SERVER_TIMEOUT parameter is not valid
67 #endif
68 
69 //Modbus/TCP server tick interval
70 #ifndef MODBUS_SERVER_TICK_INTERVAL
71  #define MODBUS_SERVER_TICK_INTERVAL 1000
72 #elif (MODBUS_SERVER_TICK_INTERVAL < 100)
73  #error MODBUS_SERVER_TICK_INTERVAL parameter is not valid
74 #endif
75 
76 //Forward declaration of ModbusServerContext structure
78 #define ModbusServerContext struct _ModbusServerContext
79 
80 //C++ guard
81 #ifdef __cplusplus
82  extern "C" {
83 #endif
84 
85 
86 /**
87  * @brief Modbus/TCP connection state
88  **/
89 
90 typedef enum
91 {
96 
97 
98 /**
99  * @brief Lock Modbus table callback function
100  **/
101 
102 typedef void (*ModbusServerLockCallback)(void);
103 
104 
105 /**
106  * @brief Unlock Modbus table callback function
107  **/
108 
109 typedef void (*ModbusServerUnlockCallback)(void);
110 
111 
112 /**
113  * @brief Get coil state callback function
114  **/
115 
117  bool_t *state);
118 
119 
120 /**
121  * @brief Set coil state callback function
122  **/
123 
125  bool_t state, bool_t commit);
126 
127 
128 /**
129  * @brief Get register value callback function
130  **/
131 
133  uint16_t *value);
134 
135 
136 /**
137  * @brief Set register value callback function
138  **/
139 
141  uint16_t value, bool_t commit);
142 
143 
144 /**
145  * @brief PDU processing callback function
146  **/
147 
148 typedef error_t (*ModbusServerProcessPduCallback)(const uint8_t *request,
149  size_t requestLen, uint8_t *response, size_t *responseLen);
150 
151 
152 /**
153  * @brief Modbus/TCP server settings
154  **/
155 
156 typedef struct
157 {
158  NetInterface *interface; ///<Underlying network interface
159  uint16_t port; ///<Modbus/TCP port number
160  uint8_t unitId; ///<Unit identifier
161  ModbusServerLockCallback lockCallback; ///<Lock Modbus table callback function
162  ModbusServerUnlockCallback unlockCallback; ///<Unlock Modbus table callback function
163  ModbusServerReadCoilCallback readCoilCallback; ///<Get coil state callback function
164  ModbusServerWriteCoilCallback writeCoilCallback; ///<Set coil state callback function
165  ModbusServerReadRegCallback readRegCallback; ///<Get register value callback function
166  ModbusServerWriteRegCallback writeRegValueCallback; ///<Set register value callback function
169 
170 
171 /**
172  * @brief Modbus/TCP client connection
173  **/
174 
175 typedef struct
176 {
177  ModbusConnectionState state; ///<Connection state
178  ModbusServerContext *context; ///<Modbus/TCP server context
179  Socket *socket; ///<Underlying socket
180  systime_t timestamp; ///<Time stamp
181  uint8_t requestAdu[MODBUS_MAX_ADU_SIZE]; ///<Request ADU
182  size_t requestAduLen; ///<Length of the request ADU, in bytes
183  size_t requestAduPos; ///<Current position in the request ADU
184  uint8_t requestUnitId; ///<Unit identifier
185  uint8_t responseAdu[MODBUS_MAX_ADU_SIZE]; ///<Response ADU
186  size_t responseAduLen; ///<Length of the response ADU, in bytes
187  size_t responseAduPos; ///<Current position in the response ADU
189 
190 
191 /**
192  * @brief Modbus/TCP server context
193  **/
194 
196 {
197  ModbusServerSettings settings; ///<User settings
198  OsEvent event; ///<Event object used to poll the sockets
199  Socket *socket; ///<Listening socket
201  SocketEventDesc eventDesc[MODBUS_SERVER_MAX_CONNECTIONS + 1]; ///<The events the application is interested in
202 };
203 
204 
205 //Modbus/TCP server related functions
207 
209  const ModbusServerSettings *settings);
210 
212 
214 
215 //C++ guard
216 #ifdef __cplusplus
217  }
218 #endif
219 
220 #endif
error_t modbusServerInit(ModbusServerContext *context, const ModbusServerSettings *settings)
Initialize Modbus/TCP server context.
Definition: modbus_server.c:80
ModbusServerSettings settings
User settings.
uint32_t systime_t
Definition: compiler_port.h:44
Definitions common to Modbus/TCP client and server.
error_t modbusServerStart(ModbusServerContext *context)
Start Modbus/TCP server.
uint16_t port
Modbus/TCP port number.
ModbusServerLockCallback lockCallback
Lock Modbus table callback function.
Modbus/TCP server settings.
ModbusServerWriteRegCallback writeRegValueCallback
Set register value callback function.
ModbusServerProcessPduCallback processPduCallback
PDU processing callback.
TCP/IP stack core.
systime_t timestamp
Time stamp.
ModbusClientConnection connection[MODBUS_SERVER_MAX_CONNECTIONS]
Client connections.
ModbusServerWriteCoilCallback writeCoilCallback
Set coil state callback function.
ModbusServerReadRegCallback readRegCallback
Get register value callback function.
size_t requestAduPos
Current position in the request ADU.
error_t(* ModbusServerReadRegCallback)(uint16_t address, uint16_t *value)
Get register value callback function.
void modbusServerTask(ModbusServerContext *context)
Modbus/TCP server task.
size_t responseAduLen
Length of the response ADU, in bytes.
ModbusConnectionState
Modbus/TCP connection state.
Definition: modbus_server.h:90
ModbusServerContext * context
Modbus/TCP server context.
error_t(* ModbusServerProcessPduCallback)(const uint8_t *request, size_t requestLen, uint8_t *response, size_t *responseLen)
PDU processing callback function.
NetInterface * interface
Underlying network interface.
Event object.
void modbusServerGetDefaultSettings(ModbusServerSettings *settings)
Initialize settings with default values.
Definition: modbus_server.c:46
ModbusConnectionState state
Connection state.
#define Socket
Definition: socket.h:34
Socket * socket
Underlying socket.
size_t responseAduPos
Current position in the response ADU.
Modbus/TCP server context.
error_t(* ModbusServerWriteRegCallback)(uint16_t address, uint16_t value, bool_t commit)
Set register value callback function.
error_t(* ModbusServerWriteCoilCallback)(uint16_t address, bool_t state, bool_t commit)
Set coil state callback function.
Modbus/TCP client connection.
uint8_t requestUnitId
Unit identifier.
uint8_t unitId
Unit identifier.
Structure describing socket events.
Definition: socket.h:303
error_t(* ModbusServerReadCoilCallback)(uint16_t address, bool_t *state)
Get coil state callback function.
void(* ModbusServerLockCallback)(void)
Lock Modbus table callback function.
Ipv6Addr address
#define ModbusServerContext
Definition: modbus_server.h:78
error_t
Error codes.
Definition: error.h:40
#define NetInterface
Definition: net.h:34
Socket * socket
Listening socket.
SocketEventDesc eventDesc[MODBUS_SERVER_MAX_CONNECTIONS+1]
The events the application is interested in.
uint8_t value[]
Definition: dtls_misc.h:141
OsEvent event
Event object used to poll the sockets.
ModbusServerReadCoilCallback readCoilCallback
Get coil state callback function.
size_t requestAduLen
Length of the request ADU, in bytes.
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:57
void(* ModbusServerUnlockCallback)(void)
Unlock Modbus table callback function.
#define MODBUS_MAX_ADU_SIZE
Definition: modbus_common.h:45
ModbusServerUnlockCallback unlockCallback
Unlock Modbus table callback function.
int bool_t
Definition: compiler_port.h:47