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-2025 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.5.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
56  //Failed to open socket?
57  if(context->socket == NULL)
58  return ERROR_OPEN_FAILED;
59 
60  //Associate the socket with the relevant interface
61  error = socketBindToInterface(context->socket, context->interface);
62  //Any error to report?
63  if(error)
64  return error;
65 
66  //Set timeout
67  error = socketSetTimeout(context->socket, context->timeout);
68  //Any error to report?
69  if(error)
70  return error;
71 
72 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
73  //TLS-secured connection?
74  if(context->tlsInitCallback != NULL)
75  {
76  //Allocate TLS context
77  context->tlsContext = tlsInit();
78  //Failed to allocate TLS context?
79  if(context->tlsContext == NULL)
80  return ERROR_OPEN_FAILED;
81 
82  //Devices must provide TLS v1.2 or better
83  error = tlsSetVersion(context->tlsContext, TLS_VERSION_1_2,
85  //Any error to report?
86  if(error)
87  return error;
88 
89  //Select client operation mode
90  error = tlsSetConnectionEnd(context->tlsContext,
92  //Any error to report?
93  if(error)
94  return error;
95 
96  //Bind TLS to the relevant socket
97  error = tlsSetSocket(context->tlsContext, context->socket);
98  //Any error to report?
99  if(error)
100  return error;
101 
102  //Set TX and RX buffer size
103  error = tlsSetBufferSize(context->tlsContext,
105  //Any error to report?
106  if(error)
107  return error;
108 
109  //Restore TLS session
110  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
111  //Any error to report?
112  if(error)
113  return error;
114 
115  //Perform TLS related initialization
116  error = context->tlsInitCallback(context, context->tlsContext);
117  //Any error to report?
118  if(error)
119  return error;
120  }
121 #endif
122 
123  //Successful processing
124  return NO_ERROR;
125 }
126 
127 
128 /**
129  * @brief Establish network connection
130  * @param[in] context Pointer to the Modbus/TCP client context
131  * @param[in] serverIpAddr IP address of the Modbus/TCP server to connect to
132  * @param[in] serverPort TCP port number that will be used to establish the
133  * connection
134  * @return Error code
135  **/
136 
138  const IpAddr *serverIpAddr, uint16_t serverPort)
139 {
140  error_t error;
141 
142  //Establish TCP connection
143  error = socketConnect(context->socket, serverIpAddr, serverPort);
144  //Any error to report?
145  if(error)
146  return error;
147 
148 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
149  //TLS-secured connection?
150  if(context->tlsContext != NULL)
151  {
152  //Establish TLS connection
153  error = tlsConnect(context->tlsContext);
154  //Any error to report?
155  if(error)
156  return error;
157 
158  //Save TLS session
159  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
160  //Any error to report?
161  if(error)
162  return error;
163  }
164 #endif
165 
166  //Successful processing
167  return NO_ERROR;
168 }
169 
170 
171 /**
172  * @brief Shutdown network connection
173  * @param[in] context Pointer to the Modbus/TCP client context
174  * @return Error code
175  **/
176 
178 {
179  error_t error;
180 
181  //Initialize status code
182  error = NO_ERROR;
183 
184 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
185  //Valid TLS context?
186  if(context->tlsContext != NULL)
187  {
188  //Shutdown TLS session
189  error = tlsShutdown(context->tlsContext);
190  }
191 #endif
192 
193  //Check status code
194  if(!error)
195  {
196  //Valid TCP socket?
197  if(context->socket != NULL)
198  {
199  //Shutdown TCP connection
200  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
201  }
202  }
203 
204  //Return status code
205  return error;
206 }
207 
208 
209 /**
210  * @brief Close network connection
211  * @param[in] context Pointer to the Modbus/TCP client context
212  **/
213 
215 {
216 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
217  //Release TLS context
218  if(context->tlsContext != NULL)
219  {
220  tlsFree(context->tlsContext);
221  context->tlsContext = NULL;
222  }
223 #endif
224 
225  //Close TCP connection
226  if(context->socket != NULL)
227  {
228  socketClose(context->socket);
229  context->socket = NULL;
230  }
231 }
232 
233 
234 /**
235  * @brief Send data using the relevant transport protocol
236  * @param[in] context Pointer to the Modbus/TCP client context
237  * @param[in] data Pointer to a buffer containing the data to be transmitted
238  * @param[in] length Number of bytes to be transmitted
239  * @param[out] written Actual number of bytes written (optional parameter)
240  * @param[in] flags Set of flags that influences the behavior of this function
241  * @return Error code
242  **/
243 
245  size_t length, size_t *written, uint_t flags)
246 {
247  error_t error;
248 
249 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
250  //TLS-secured connection?
251  if(context->tlsContext != NULL)
252  {
253  //Send TLS-encrypted data
254  error = tlsWrite(context->tlsContext, data, length, written, flags);
255  }
256  else
257 #endif
258  {
259  //Transmit data
260  error = socketSend(context->socket, data, length, written, flags);
261  }
262 
263  //Return status code
264  return error;
265 }
266 
267 
268 /**
269  * @brief Receive data using the relevant transport protocol
270  * @param[in] context Pointer to the Modbus/TCP client context
271  * @param[out] data Buffer into which received data will be placed
272  * @param[in] size Maximum number of bytes that can be received
273  * @param[out] received Number of bytes that have been received
274  * @param[in] flags Set of flags that influences the behavior of this function
275  * @return Error code
276  **/
277 
279  size_t size, size_t *received, uint_t flags)
280 {
281  error_t error;
282 
283 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
284  //TLS-secured connection?
285  if(context->tlsContext != NULL)
286  {
287  //Receive TLS-encrypted data
288  error = tlsRead(context->tlsContext, data, size, received, flags);
289  }
290  else
291 #endif
292  {
293  //Receive data
294  error = socketReceive(context->socket, data, size, received, flags);
295  }
296 
297  //Return status code
298  return error;
299 }
300 
301 #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:1491
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:357
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:222
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2067
#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:2818
@ 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:289
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2423
#define tlsSetSocket(context, socket)
Definition: tls.h:949
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:1697
#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:1354
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2025
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2749
uint8_t length
Definition: tcp.h:375
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
#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:2105
#define socketBindToInterface
Definition: net_legacy.h:193
uint8_t flags
Definition: tcp.h:358
Transport protocol abstraction layer.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:529
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:990
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:1970
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2585
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.
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:148
error_t tlsConnect(TlsContext *context)
Initiate the TLS handshake.
Definition: tls.c:1730
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.