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-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 //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  //Select client operation mode
83  error = tlsSetConnectionEnd(context->tlsContext, TLS_CONNECTION_END_CLIENT);
84  //Any error to report?
85  if(error)
86  return error;
87 
88  //Bind TLS to the relevant socket
89  error = tlsSetSocket(context->tlsContext, context->socket);
90  //Any error to report?
91  if(error)
92  return error;
93 
94  //Set TX and RX buffer size
95  error = tlsSetBufferSize(context->tlsContext,
97  //Any error to report?
98  if(error)
99  return error;
100 
101  //Restore TLS session
102  error = tlsRestoreSessionState(context->tlsContext, &context->tlsSession);
103  //Any error to report?
104  if(error)
105  return error;
106 
107  //Perform TLS related initialization
108  error = context->tlsInitCallback(context, context->tlsContext);
109  //Any error to report?
110  if(error)
111  return error;
112  }
113 #endif
114 
115  //Successful processing
116  return NO_ERROR;
117 }
118 
119 
120 /**
121  * @brief Establish network connection
122  * @param[in] context Pointer to the Modbus/TCP client context
123  * @param[in] serverIpAddr IP address of the Modbus/TCP server to connect to
124  * @param[in] serverPort TCP port number that will be used to establish the
125  * connection
126  * @return Error code
127  **/
128 
130  const IpAddr *serverIpAddr, uint16_t serverPort)
131 {
132  error_t error;
133 
134  //Establish TCP connection
135  error = socketConnect(context->socket, serverIpAddr, serverPort);
136  //Any error to report?
137  if(error)
138  return error;
139 
140 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
141  //TLS-secured connection?
142  if(context->tlsContext != NULL)
143  {
144  //Establish TLS connection
145  error = tlsConnect(context->tlsContext);
146  //Any error to report?
147  if(error)
148  return error;
149 
150  //Save TLS session
151  error = tlsSaveSessionState(context->tlsContext, &context->tlsSession);
152  //Any error to report?
153  if(error)
154  return error;
155  }
156 #endif
157 
158  //Successful processing
159  return NO_ERROR;
160 }
161 
162 
163 /**
164  * @brief Shutdown network connection
165  * @param[in] context Pointer to the Modbus/TCP client context
166  * @return Error code
167  **/
168 
170 {
171  error_t error;
172 
173  //Initialize status code
174  error = NO_ERROR;
175 
176 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
177  //Valid TLS context?
178  if(context->tlsContext != NULL)
179  {
180  //Shutdown TLS session
181  error = tlsShutdown(context->tlsContext);
182  }
183 #endif
184 
185  //Check status code
186  if(!error)
187  {
188  //Valid TCP socket?
189  if(context->socket != NULL)
190  {
191  //Shutdown TCP connection
192  error = socketShutdown(context->socket, SOCKET_SD_BOTH);
193  }
194  }
195 
196  //Return status code
197  return error;
198 }
199 
200 
201 /**
202  * @brief Close network connection
203  * @param[in] context Pointer to the Modbus/TCP client context
204  **/
205 
207 {
208 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
209  //Release TLS context
210  if(context->tlsContext != NULL)
211  {
212  tlsFree(context->tlsContext);
213  context->tlsContext = NULL;
214  }
215 #endif
216 
217  //Close TCP connection
218  if(context->socket != NULL)
219  {
220  socketClose(context->socket);
221  context->socket = NULL;
222  }
223 }
224 
225 
226 /**
227  * @brief Send data using the relevant transport protocol
228  * @param[in] context Pointer to the Modbus/TCP client context
229  * @param[in] data Pointer to a buffer containing the data to be transmitted
230  * @param[in] length Number of bytes to be transmitted
231  * @param[out] written Actual number of bytes written (optional parameter)
232  * @param[in] flags Set of flags that influences the behavior of this function
233  * @return Error code
234  **/
235 
237  size_t length, size_t *written, uint_t flags)
238 {
239  error_t error;
240 
241 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
242  //TLS-secured connection?
243  if(context->tlsContext != NULL)
244  {
245  //Send TLS-encrypted data
246  error = tlsWrite(context->tlsContext, data, length, written, flags);
247  }
248  else
249 #endif
250  {
251  //Transmit data
252  error = socketSend(context->socket, data, length, written, flags);
253  }
254 
255  //Return status code
256  return error;
257 }
258 
259 
260 /**
261  * @brief Receive data using the relevant transport protocol
262  * @param[in] context Pointer to the Modbus/TCP client context
263  * @param[out] data Buffer into which received data will be placed
264  * @param[in] size Maximum number of bytes that can be received
265  * @param[out] received Number of bytes that have been received
266  * @param[in] flags Set of flags that influences the behavior of this function
267  * @return Error code
268  **/
269 
271  size_t size, size_t *received, uint_t flags)
272 {
273  error_t error;
274 
275 #if (MODBUS_CLIENT_TLS_SUPPORT == ENABLED)
276  //TLS-secured connection?
277  if(context->tlsContext != NULL)
278  {
279  //Receive TLS-encrypted data
280  error = tlsRead(context->tlsContext, data, size, received, flags);
281  }
282  else
283 #endif
284  {
285  //Receive data
286  error = socketReceive(context->socket, data, size, received, flags);
287  }
288 
289  //Return status code
290  return error;
291 }
292 
293 #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:1486
TlsContext * tlsInit(void)
TLS context initialization.
Definition: tls.c:65
Modbus/TCP client.
error_t tlsSetConnectionEnd(TlsContext *context, TlsConnectionEnd entity)
Set operation mode (client or server)
Definition: tls.c:349
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:2062
#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:2700
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t tlsShutdown(TlsContext *context)
Gracefully close TLS session.
Definition: tls.c:2307
#define tlsSetSocket(context, socket)
Definition: tls.h:927
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:1692
error_t socketConnect(Socket *socket, const IpAddr *remoteIpAddr, uint16_t remotePort)
Establish a connection to a specified socket.
Definition: socket.c:1349
error_t socketShutdown(Socket *socket, uint_t how)
Disable reception, transmission, or both.
Definition: socket.c:2020
error_t tlsSaveSessionState(const TlsContext *context, TlsSessionState *session)
Save TLS session.
Definition: tls.c:2631
uint8_t length
Definition: tcp.h:368
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:1989
#define socketBindToInterface
Definition: net_legacy.h:193
uint8_t flags
Definition: tcp.h:351
Transport protocol abstraction layer.
error_t tlsSetBufferSize(TlsContext *context, size_t txBufferSize, size_t rxBufferSize)
Set TLS buffer size.
Definition: tls.c:521
@ TLS_CONNECTION_END_CLIENT
Definition: tls.h:968
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:1854
void tlsFree(TlsContext *context)
Release TLS context.
Definition: tls.c:2469
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:50
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:1763
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.