modbus_client_transport.c
Go to the documentation of this file.
1 /**
2  * @file modbus_client_transport.c
3  * @brief Transport protocol abstraction layer
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.0
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL MODBUS_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "modbus/modbus_client.h"
38 #include "debug.h"
39 
40 //Check TCP/IP stack configuration
41 #if (MODBUS_CLIENT_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Open network connection
46  * @param[in] context Pointer to the Modbus/TCP client context
47  * @return Error code
48  **/
49 
51 {
52  error_t error;
53 
54  //Open a TCP socket
55  context->socket = socketOpenEx(context->netContext, SOCKET_TYPE_STREAM,
57  //Failed to open socket?
58  if(context->socket == NULL)
59  return ERROR_OPEN_FAILED;
60 
61  //Associate the socket with the relevant interface
62  error = socketBindToInterface(context->socket, context->interface);
63  //Any error to report?
64  if(error)
65  return error;
66 
67  //Set timeout
68  error = socketSetTimeout(context->socket, context->timeout);
69  //Any error to report?
70  if(error)
71  return error;
72 
73 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
74  //TLS-secured connection?
75  if(context->tlsInitCallback != NULL)
76  {
77  //Allocate TLS context
78  context->tlsContext = tlsInit();
79  //Failed to allocate TLS context?
80  if(context->tlsContext == NULL)
81  return ERROR_OPEN_FAILED;
82 
83  //Devices must provide TLS v1.2 or better
84  error = tlsSetVersion(context->tlsContext, TLS_VERSION_1_2,
86  //Any error to report?
87  if(error)
88  return error;
89 
90  //Select client operation mode
91  error = tlsSetConnectionEnd(context->tlsContext,
93  //Any error to report?
94  if(error)
95  return error;
96 
97  //Bind TLS to the relevant socket
98  error = tlsSetSocket(context->tlsContext, context->socket);
99  //Any error to report?
100  if(error)
101  return error;
102 
103  //Set TX and RX buffer size
104  error = tlsSetBufferSize(context->tlsContext,
106  //Any error to report?
107  if(error)
108  return error;
109 
110  //Restore TLS session
111  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
112  //Any error to report?
113  if(error)
114  return error;
115 
116  //Perform TLS related initialization
117  error = context->tlsInitCallback(context, context->tlsContext);
118  //Any error to report?
119  if(error)
120  return error;
121  }
122 #endif
123 
124  //Successful processing
125  return NO_ERROR;
126 }
127 
128 
129 /**
130  * @brief Establish network connection
131  * @param[in] context Pointer to the Modbus/TCP client context
132  * @param[in] serverIpAddr IP address of the Modbus/TCP server to connect to
133  * @param[in] serverPort TCP port number that will be used to establish the
134  * connection
135  * @return Error code
136  **/
137 
139  const IpAddr *serverIpAddr, uint16_t serverPort)
140 {
141  error_t error;
142 
143  //Establish TCP connection
144  error = socketConnect(context->socket, serverIpAddr, serverPort);
145  //Any error to report?
146  if(error)
147  return error;
148 
149 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
150  //TLS-secured connection?
151  if(context->tlsContext != NULL)
152  {
153  //Establish TLS connection
154  error = tlsConnect(context->tlsContext);
155  //Any error to report?
156  if(error)
157  return error;
158 
159  //Save TLS session
160  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
161  //Any error to report?
162  if(error)
163  return error;
164  }
165 #endif
166 
167  //Successful processing
168  return NO_ERROR;
169 }
170 
171 
172 /**
173  * @brief Shutdown network connection
174  * @param[in] context Pointer to the Modbus/TCP client context
175  * @return Error code
176  **/
177 
179 {
180  error_t error;
181 
182  //Initialize status code
183  error = NO_ERROR;
184 
185 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
186  //Valid TLS context?
187  if(context->tlsContext != NULL)
188  {
189  //Shutdown TLS session
190  error = tlsShutdown(context->tlsContext);
191  }
192 #endif
193 
194  //Check status code
195  if(!error)
196  {
197  //Valid TCP socket?
198  if(context->socket != NULL)
199  {
200  //Shutdown TCP connection
201  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
202  }
203  }
204 
205  //Return status code
206  return error;
207 }
208 
209 
210 /**
211  * @brief Close network connection
212  * @param[in] context Pointer to the Modbus/TCP client context
213  **/
214 
216 {
217 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
218  //Release TLS context
219  if(context->tlsContext != NULL)
220  {
221  tlsFree(context->tlsContext);
222  context->tlsContext = NULL;
223  }
224 #endif
225 
226  //Close TCP connection
227  if(context->socket != NULL)
228  {
229  socketClose(context->socket);
230  context->socket = NULL;
231  }
232 }
233 
234 
235 /**
236  * @brief Send data using the relevant transport protocol
237  * @param[in] context Pointer to the Modbus/TCP client context
238  * @param[in] data Pointer to a buffer containing the data to be transmitted
239  * @param[in] length Number of bytes to be transmitted
240  * @param[out] written Actual number of bytes written (optional parameter)
241  * @param[in] flags Set of flags that influences the behavior of this function
242  * @return Error code
243  **/
244 
246  size_t length, size_t *written, uint_t flags)
247 {
248  error_t error;
249 
250 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
251  //TLS-secured connection?
252  if(context->tlsContext != NULL)
253  {
254  //Send TLS-encrypted data
255  error = tlsWrite(context->tlsContext, data, length, written, flags);
256  }
257  else
258 #endif
259  {
260  //Transmit data
261  error = socketSend(context->socket, data, length, written, flags);
262  }
263 
264  //Return status code
265  return error;
266 }
267 
268 
269 /**
270  * @brief Receive data using the relevant transport protocol
271  * @param[in] context Pointer to the Modbus/TCP client context
272  * @param[out] data Buffer into which received data will be placed
273  * @param[in] size Maximum number of bytes that can be received
274  * @param[out] received Number of bytes that have been received
275  * @param[in] flags Set of flags that influences the behavior of this function
276  * @return Error code
277  **/
278 
280  size_t size, size_t *received, uint_t flags)
281 {
282  error_t error;
283 
284 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
285  //TLS-secured connection?
286  if(context->tlsContext != NULL)
287  {
288  //Receive TLS-encrypted data
289  error = tlsRead(context->tlsContext, data, size, received, flags);
290  }
291  else
292 #endif
293  {
294  //Receive data
295  error = socketReceive(context->socket, data, size, received, flags);
296  }
297 
298  //Return status code
299  return error;
300 }
301 
302 #endif
error_t socketSend(Socket *socket, const void *data, size_t length, size_t *written, uint_t flags)
Send data to a connected socket.
Definition: socket.c:1514
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:67
Modbus/TCP client.
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:385
error_t modbusClientSendData(ModbusClientContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
IP network address.
Definition: ip.h:90
#define MODBUS_CLIENT_TLS_TX_BUFFER_SIZE
Definition: modbus_client.h:61
error_t modbusClientOpenConnection(ModbusClientContext *context)
Open network connection.
uint8_t data[]
Definition: ethernet.h:224
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
#define ModbusClientContext
Definition: modbus_client.h:86
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
error_t modbusClientEstablishConnection(ModbusClientContext *context, const IpAddr *serverIpAddr, uint16_t serverPort)
Establish network connection.
error_t tlsRestoreSessionState(TlsContext *context, const TlsSessionState *session)
Restore TLS session.
Definition: tls.c:3012
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsSetVersion(TlsContext *context, uint16_t versionMin, uint16_t versionMax)
Set minimum and maximum versions permitted.
Definition: tls.c:295
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2603
#define tlsSetSocket(context, socket)
Definition: tls.h:970
void modbusClientCloseConnection(ModbusClientContext *context)
Close network connection.
error_t
Error codes.
Definition: error.h:43
error_t socketReceive(Socket *socket, void *data, size_t size, size_t *received, uint_t flags)
Receive data from a connected socket.
Definition: socket.c:1724
#define TLS_VERSION_1_2
Definition: tls.h:96
#define TLS_VERSION_1_3
Definition: tls.h:97
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1377
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2052
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2943
uint8_t length
Definition: tcp.h:375
#define MODBUS_CLIENT_TLS_RX_BUFFER_SIZE
Definition: modbus_client.h:68
error_t tlsRead(TlsContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive application data from a the remote host using TLS.
Definition: tls.c:2285
#define socketBindToInterface
Definition: net_legacy.h:193
Transport protocol abstraction layer.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:557
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:1012
error_t tlsWrite(TlsContext *context, const void *data, size_t length, size_t *written, uint_t flags)
Send application data to the remote host using TLS.
Definition: tls.c:2148
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2765
error_t modbusClientReceiveData(ModbusClientContext *context, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t modbusClientShutdownConnection(ModbusClientContext *context)
Shutdown network connection.
uint8_t flags
Definition: tcp.h:358
unsigned int uint_t
Definition: compiler_port.h:57
TCP/IP stack core.
@ SOCKET_SD_BOTH
Definition: socket.h:161
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:169
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1819
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.