modbus_server_misc.c
Go to the documentation of this file.
1 /**
2  * @file modbus_server_misc.c
3  * @brief Helper functions for Modbus/TCP server
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 "modbus/modbus_server.h"
40 #include "debug.h"
41 
42 //Check TCP/IP stack configuration
43 #if (MODBUS_SERVER_SUPPORT == ENABLED)
44 
45 
46 /**
47  * @brief Handle periodic operations
48  * @param[in] context Pointer to the Modbus/TCP server context
49  **/
50 
52 {
53  uint_t i;
55  systime_t timeout;
56  ModbusClientConnection *connection;
57 
58  //Get current time
60 
61  //Loop through the connection table
62  for(i = 0; i < MODBUS_SERVER_MAX_CONNECTIONS; i++)
63  {
64  //Point to the current entry
65  connection = &context->connection[i];
66 
67  //Check the state of the current connection
68  if(connection->state != MODBUS_CONNECTION_STATE_CLOSED)
69  {
70  //Retrieve idle connection timeout
71  timeout = context->timeout;
72 
73  //A value of zero means no timeout
74  if(timeout != 0)
75  {
76  //Disconnect inactive client after idle timeout
77  if(timeCompare(time, connection->timestamp + timeout) >= 0)
78  {
79  //Debug message
80  TRACE_INFO("Modbus server: Closing inactive connection...\r\n");
81  //Close the Modbus/TCP connection
82  modbusServerCloseConnection(connection);
83  }
84  }
85  }
86  }
87 
88  //Any registered callback?
89  if(context->tickCallback != NULL)
90  {
91  //Invoke user callback function
92  context->tickCallback(context);
93  }
94 }
95 
96 
97 /**
98  * @brief Register connection events
99  * @param[in] connection Pointer to the client connection
100  * @param[in] eventDesc Socket events to be registered
101  **/
102 
104  SocketEventDesc *eventDesc)
105 {
106  //Check the state of the connection
107  if(connection->state == MODBUS_CONNECTION_STATE_CONNECT_TLS)
108  {
109 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
110  //Any data pending in the send buffer?
111  if(tlsIsTxReady(connection->tlsContext))
112  {
113  //Wait until there is more room in the send buffer
114  eventDesc->socket = connection->socket;
115  eventDesc->eventMask = SOCKET_EVENT_TX_READY;
116  }
117  else
118  {
119  //Wait for data to be available for reading
120  eventDesc->socket = connection->socket;
121  eventDesc->eventMask = SOCKET_EVENT_RX_READY;
122  }
123 #endif
124  }
125  else if(connection->state == MODBUS_CONNECTION_STATE_RECEIVE)
126  {
127 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
128  //Any data available in the receive buffer?
129  if(connection->tlsContext != NULL &&
130  tlsIsRxReady(connection->tlsContext))
131  {
132  //No need to poll the underlying socket for incoming traffic
133  eventDesc->eventFlags |= SOCKET_EVENT_RX_READY;
134  }
135  else
136 #endif
137  {
138  //Wait for data to be available for reading
139  eventDesc->socket = connection->socket;
140  eventDesc->eventMask = SOCKET_EVENT_RX_READY;
141  }
142  }
143  else if(connection->state == MODBUS_CONNECTION_STATE_SEND ||
144  connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TLS)
145  {
146  //Wait until there is more room in the send buffer
147  eventDesc->socket = connection->socket;
148  eventDesc->eventMask = SOCKET_EVENT_TX_READY;
149  }
150  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TX)
151  {
152  //Wait for the FIN to be acknowledged
153  eventDesc->socket = connection->socket;
154  eventDesc->eventMask = SOCKET_EVENT_TX_SHUTDOWN;
155  }
156  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_RX)
157  {
158  //Wait for a FIN to be received
159  eventDesc->socket = connection->socket;
160  eventDesc->eventMask = SOCKET_EVENT_RX_SHUTDOWN;
161  }
162  else
163  {
164  //Just for sanity
165  }
166 }
167 
168 
169 /**
170  * @brief Connection event handler
171  * @param[in] connection Pointer to the client connection
172  **/
173 
175 {
176  error_t error;
177  size_t n;
178  ModbusServerContext *context;
179 
180  //Initialize status code
181  error = NO_ERROR;
182 
183  //Point to the Modbus/TCP server context
184  context = connection->context;
185  //Update time stamp
186  connection->timestamp = osGetSystemTime();
187 
188  //Check the state of the connection
189  if(connection->state == MODBUS_CONNECTION_STATE_CONNECT_TLS)
190  {
191 #if (MODBUS_SERVER_TLS_SUPPORT == ENABLED)
192  //Perform TLS handshake
193  error = modbusServerEstablishSecureConnection(connection);
194 
195  //Check status code
196  if(!error)
197  {
198  //Wait for incoming Modbus requests
199  connection->state = MODBUS_CONNECTION_STATE_RECEIVE;
200  }
201 #else
202  //Modbus/TCP security is not implemented
203  error = ERROR_WRONG_STATE;
204 #endif
205  }
206  else if(connection->state == MODBUS_CONNECTION_STATE_RECEIVE)
207  {
208  //Receive Modbus request
209  if(connection->requestAduPos < sizeof(ModbusHeader))
210  {
211  //Receive more data
212  error = modbusServerReceiveData(connection,
213  connection->requestAdu + connection->requestAduPos,
214  sizeof(ModbusHeader) - connection->requestAduPos, &n, 0);
215 
216  //Check status code
217  if(error == NO_ERROR)
218  {
219  //Advance data pointer
220  connection->requestAduPos += n;
221 
222  //MBAP header successfully received?
223  if(connection->requestAduPos >= sizeof(ModbusHeader))
224  {
225  //Parse MBAP header
226  error = modbusServerParseMbapHeader(connection);
227  }
228  }
229  else if(error == ERROR_END_OF_STREAM)
230  {
231  //Initiate a graceful connection shutdown
232  error = modbusServerShutdownConnection(connection);
233  }
234  else
235  {
236  //Just for sanity
237  }
238  }
239  else if(connection->requestAduPos < connection->requestAduLen)
240  {
241  //Receive more data
242  error = modbusServerReceiveData(connection,
243  connection->requestAdu + connection->requestAduPos,
244  connection->requestAduLen - connection->requestAduPos, &n, 0);
245 
246  //Check status code
247  if(error == NO_ERROR)
248  {
249  //Advance data pointer
250  connection->requestAduPos += n;
251 
252  //Modbus request successfully received?
253  if(connection->requestAduPos >= connection->requestAduLen)
254  {
255 #if (MODBUS_SERVER_DIAG_SUPPORT == ENABLED)
256  //Total number of messages received
257  context->rxMessageCount++;
258 #endif
259  //Check unit identifier
260  if(context->unitId == 0 ||
261  context->unitId == 255 ||
262  context->unitId == connection->requestUnitId)
263  {
264  //Process Modbus request
265  error = modbusServerProcessRequest(connection);
266  }
267  }
268  }
269  }
270  else
271  {
272  //Just for sanity
273  error = ERROR_WRONG_STATE;
274  }
275  }
276  else if(connection->state == MODBUS_CONNECTION_STATE_SEND)
277  {
278  //Send Modbus response
279  if(connection->responseAduPos < connection->responseAduLen)
280  {
281  //Send more data
282  error = modbusServerSendData(connection,
283  connection->responseAdu + connection->responseAduPos,
284  connection->responseAduLen - connection->responseAduPos,
286 
287  //Check status code
288  if(error == NO_ERROR || error == ERROR_TIMEOUT)
289  {
290  //Advance data pointer
291  connection->responseAduPos += n;
292 
293  //Modbus response successfully sent?
294  if(connection->responseAduPos >= connection->responseAduLen)
295  {
296 #if (MODBUS_SERVER_DIAG_SUPPORT == ENABLED)
297  //Total number of messages sent
298  context->txMessageCount++;
299 #endif
300  //Flush receive buffer
301  connection->requestAduLen = 0;
302  connection->requestAduPos = 0;
303 
304  //Wait for the next Modbus request
305  connection->state = MODBUS_CONNECTION_STATE_RECEIVE;
306  }
307  }
308  }
309  else
310  {
311  //Just for sanity
312  error = ERROR_WRONG_STATE;
313  }
314  }
315  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TLS ||
316  connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TX)
317  {
318  //Graceful connection shutdown
319  error = modbusServerShutdownConnection(connection);
320  }
321  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_RX)
322  {
323  //Close the Modbus/TCP connection
324  modbusServerCloseConnection(connection);
325  }
326  else
327  {
328  //Invalid state
329  error = ERROR_WRONG_STATE;
330  }
331 
332  //Any communication error?
333  if(error != NO_ERROR && error != ERROR_TIMEOUT)
334  {
335 #if (MODBUS_SERVER_DIAG_SUPPORT == ENABLED)
336  //Total number of communication errors
337  context->commErrorCount++;
338 #endif
339 
340  //Close the Modbus/TCP connection
341  modbusServerCloseConnection(connection);
342  }
343 }
344 
345 
346 /**
347  * @brief Parse request MBAP header
348  * @param[in] connection Pointer to the client connection
349  * @return Error code
350  **/
351 
353 {
354  size_t n;
355  ModbusHeader *requestHeader;
356 
357  //Sanity check
358  if(connection->requestAduPos < sizeof(ModbusHeader))
359  return ERROR_INVALID_LENGTH;
360 
361  //Point to the beginning of the request ADU
362  requestHeader = (ModbusHeader *) connection->requestAdu;
363 
364  //The length field is a byte count of the following fields, including
365  //the unit identifier and data fields
366  n = ntohs(requestHeader->length);
367 
368  //Malformed Modbus request?
369  if(n < sizeof(uint8_t))
370  return ERROR_INVALID_LENGTH;
371 
372  //Retrieve the length of the PDU
373  n -= sizeof(uint8_t);
374 
375  //Debug message
376  TRACE_DEBUG("\r\nModbus Server: ADU received (%" PRIuSIZE " bytes)...\r\n",
377  sizeof(ModbusHeader) + n);
378 
379  //Dump MBAP header
380  TRACE_DEBUG(" Transaction ID = %" PRIu16 "\r\n", ntohs(requestHeader->transactionId));
381  TRACE_DEBUG(" Protocol ID = %" PRIu16 "\r\n", ntohs(requestHeader->protocolId));
382  TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(requestHeader->length));
383  TRACE_DEBUG(" Unit ID = %" PRIu16 "\r\n", requestHeader->unitId);
384 
385  //Check protocol identifier
386  if(ntohs(requestHeader->protocolId) != MODBUS_PROTOCOL_ID)
387  return ERROR_WRONG_IDENTIFIER;
388 
389  //The length of the Modbus PDU is limited to 253 bytes
390  if(n > MODBUS_MAX_PDU_SIZE)
391  return ERROR_INVALID_LENGTH;
392 
393  //Save unit identifier
394  connection->requestUnitId = requestHeader->unitId;
395  //Compute the length of the request ADU
396  connection->requestAduLen = sizeof(ModbusHeader) + n;
397 
398  //Successful processing
399  return NO_ERROR;
400 }
401 
402 
403 /**
404  * @brief Format response MBAP header
405  * @param[in] connection Pointer to the client connection
406  * @param[in] length Length of the PDU, in bytes
407  * @return Error code
408  **/
409 
411  size_t length)
412 {
413  ModbusHeader *requestHeader;
414  ModbusHeader *responseHeader;
415 
416  //Sanity check
417  if(connection->requestAduPos < sizeof(ModbusHeader))
418  return ERROR_INVALID_LENGTH;
419 
420  //Point to the beginning of the request ADU
421  requestHeader = (ModbusHeader *) connection->requestAdu;
422  //Point to the beginning of the response ADU
423  responseHeader = (ModbusHeader *) connection->responseAdu;
424 
425  //Format MBAP header
426  responseHeader->transactionId = requestHeader->transactionId;
427  responseHeader->protocolId = requestHeader->protocolId;
428  responseHeader->length = htons(length + sizeof(uint8_t));
429  responseHeader->unitId = requestHeader->unitId;
430 
431  //Compute the length of the response ADU
432  connection->responseAduLen = sizeof(ModbusHeader) + length;
433 
434  //Debug message
435  TRACE_DEBUG("Modbus Server: Sending ADU (%" PRIuSIZE " bytes)...\r\n",
436  connection->responseAduLen);
437 
438  //Dump MBAP header
439  TRACE_DEBUG(" Transaction ID = %" PRIu16 "\r\n", ntohs(responseHeader->transactionId));
440  TRACE_DEBUG(" Protocol ID = %" PRIu16 "\r\n", ntohs(responseHeader->protocolId));
441  TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(responseHeader->length));
442  TRACE_DEBUG(" Unit ID = %" PRIu16 "\r\n", responseHeader->unitId);
443 
444  //Rewind to the beginning of the transmit buffer
445  connection->responseAduPos = 0;
446  //Send the response ADU to the client
447  connection->state = MODBUS_CONNECTION_STATE_SEND;
448 
449  //Successful processing
450  return NO_ERROR;
451 }
452 
453 
454 /**
455  * @brief Retrieve request PDU
456  * @param[in] connection Pointer to the client connection
457  * @param[out] length Length request the request PDU, in bytes
458  * @return Pointer to the request PDU
459  **/
460 
462  size_t *length)
463 {
464  uint8_t *requestPdu;
465 
466  //Point to the request PDU
467  requestPdu = connection->requestAdu + sizeof(ModbusHeader);
468 
469  //Retrieve the length of the PDU
470  if(connection->requestAduLen >= sizeof(ModbusHeader))
471  {
472  *length = connection->requestAduLen - sizeof(ModbusHeader);
473  }
474  else
475  {
476  *length = 0;
477  }
478 
479  //Return a pointer to the request PDU
480  return requestPdu;
481 }
482 
483 
484 /**
485  * @brief Retrieve response PDU
486  * @param[in] connection Pointer to the client connection
487  * @return Pointer to the response PDU
488  **/
489 
491 {
492  //Point to the response PDU
493  return connection->responseAdu + sizeof(ModbusHeader);
494 }
495 
496 
497 /**
498  * @brief Lock Modbus table
499  * @param[in] connection Pointer to the client connection
500  **/
501 
503 {
504  ModbusServerContext *context;
505 
506  //Point to the Modbus/TCP server context
507  context = connection->context;
508 
509  //Any registered callback?
510  if(context->lockCallback != NULL)
511  {
512  //Invoke user callback function
513  context->lockCallback(connection);
514  }
515 }
516 
517 
518 /**
519  * @brief Unlock Modbus table
520  * @param[in] connection Pointer to the client connection
521  **/
522 
524 {
525  ModbusServerContext *context;
526 
527  //Point to the Modbus/TCP server context
528  context = connection->context;
529 
530  //Any registered callback?
531  if(context->unlockCallback != NULL)
532  {
533  //Invoke user callback function
534  context->unlockCallback(connection);
535  }
536 }
537 
538 
539 /**
540  * @brief Read a single coil
541  * @param[in] connection Pointer to the client connection
542  * @param[in] address Address of the coil
543  * @param[out] state Current state of the coil
544  * @return Error code
545  **/
546 
548  uint16_t address, bool_t *state)
549 {
550  error_t error;
551  ModbusServerContext *context;
552 
553  //Point to the Modbus/TCP server context
554  context = connection->context;
555 
556  //Any registered callback?
557  if(context->readCoilCallback != NULL)
558  {
559  //Invoke user callback function
560  error = context->readCoilCallback(connection, address, state);
561  }
562  else
563  {
564  //Report an error
565  error = ERROR_INVALID_ADDRESS;
566  }
567 
568  //Return status code
569  return error;
570 }
571 
572 
573 /**
574  * @brief Read a single discrete input
575  * @param[in] connection Pointer to the client connection
576  * @param[in] address Address of the discrete input
577  * @param[out] state Current state of the discrete input
578  * @return Error code
579  **/
580 
582  uint16_t address, bool_t *state)
583 {
584  error_t error;
585  ModbusServerContext *context;
586 
587  //Point to the Modbus/TCP server context
588  context = connection->context;
589 
590  //Any registered callback?
591  if(context->readDiscreteInputCallback != NULL)
592  {
593  //Invoke user callback function
594  error = context->readDiscreteInputCallback(connection, address, state);
595  }
596  else if(context->readCoilCallback != NULL)
597  {
598  //Invoke user callback function
599  error = context->readCoilCallback(connection, address, state);
600  }
601  else
602  {
603  //Report an error
604  error = ERROR_INVALID_ADDRESS;
605  }
606 
607  //Return status code
608  return error;
609 }
610 
611 
612 /**
613  * @brief Write a single coil
614  * @param[in] connection Pointer to the client connection
615  * @param[in] address Address of the coil
616  * @param[in] state Desired state of the coil
617  * @param[in] commit This flag indicates the current phase (validation phase
618  * or write phase if the validation was successful)
619  * @return Error code
620  **/
621 
623  uint16_t address, bool_t state, bool_t commit)
624 {
625  error_t error;
626  ModbusServerContext *context;
627 
628  //Point to the Modbus/TCP server context
629  context = connection->context;
630 
631  //Any registered callback?
632  if(context->writeCoilCallback != NULL)
633  {
634  //Invoke user callback function
635  error = context->writeCoilCallback(connection, address, state, commit);
636  }
637  else
638  {
639  //Report an error
640  error = ERROR_INVALID_ADDRESS;
641  }
642 
643  //Return status code
644  return error;
645 }
646 
647 
648 /**
649  * @brief Read a single holding register
650  * @param[in] connection Pointer to the client connection
651  * @param[in] address Address of the holding register
652  * @param[out] value Current value of the holding register
653  * @return Error code
654  **/
655 
657  uint16_t address, uint16_t *value)
658 {
659  error_t error;
660  ModbusServerContext *context;
661 
662  //Point to the Modbus/TCP server context
663  context = connection->context;
664 
665  //Any registered callback?
666  if(context->readHoldingRegCallback != NULL)
667  {
668  //Invoke user callback function
669  error = context->readHoldingRegCallback(connection, address, value);
670  }
671  else if(context->readRegCallback != NULL)
672  {
673  //Invoke user callback function
674  error = context->readRegCallback(connection, address, value);
675  }
676  else
677  {
678  //Report an error
679  error = ERROR_INVALID_ADDRESS;
680  }
681 
682  //Return status code
683  return error;
684 }
685 
686 
687 /**
688  * @brief Read a single input register
689  * @param[in] connection Pointer to the client connection
690  * @param[in] address Address of the input register
691  * @param[out] value Current value of the input register
692  * @return Error code
693  **/
694 
696  uint16_t address, uint16_t *value)
697 {
698  error_t error;
699  ModbusServerContext *context;
700 
701  //Point to the Modbus/TCP server context
702  context = connection->context;
703 
704  //Any registered callback?
705  if(context->readInputRegCallback != NULL)
706  {
707  //Invoke user callback function
708  error = context->readInputRegCallback(connection, address, value);
709  }
710  else if(context->readRegCallback != NULL)
711  {
712  //Invoke user callback function
713  error = context->readRegCallback(connection, address, value);
714  }
715  else
716  {
717  //Report an error
718  error = ERROR_INVALID_ADDRESS;
719  }
720 
721  //Return status code
722  return error;
723 }
724 
725 
726 /**
727  * @brief Write a single register
728  * @param[in] connection Pointer to the client connection
729  * @param[in] address Address of the register
730  * @param[in] value Desired value of the register
731  * @param[in] commit This flag indicates the current phase (validation phase
732  * or write phase if the validation was successful)
733  * @return Error code
734  **/
735 
737  uint16_t address, uint16_t value, bool_t commit)
738 {
739  error_t error;
740  ModbusServerContext *context;
741 
742  //Point to the Modbus/TCP server context
743  context = connection->context;
744 
745  //Any registered callback?
746  if(context->writeRegCallback != NULL)
747  {
748  //Invoke user callback function
749  error = context->writeRegCallback(connection, address, value, commit);
750  }
751  else
752  {
753  //Report an error
754  error = ERROR_INVALID_ADDRESS;
755  }
756 
757  //Return status code
758  return error;
759 }
760 
761 
762 /**
763  * @brief Translate exception code
764  * @param[in] status Status code
765  * @return Exception code
766  **/
767 
769 {
771 
772  //Check status code
773  switch(status)
774  {
776  //The function code received in the query is not an allowable action
777  //for the server
779  break;
780 
782  //The data address received in the query is not an allowable address
783  //for the server
785  break;
786 
787  case ERROR_INVALID_VALUE:
788  //A value contained in the query data field is not an allowable value
789  //for the server
791  break;
792 
793  case ERROR_DEVICE_BUSY:
794  //The client should retransmit the message later when the server is free
796  break;
797 
798  default:
799  //An unrecoverable error occurred while the server was attempting to
800  //perform the requested action
802  break;
803  }
804 
805  //Return exception code
806  return exceptionCode;
807 }
808 
809 #endif
#define htons(value)
Definition: cpu_endian.h:413
int bool_t
Definition: compiler_port.h:63
void modbusServerCloseConnection(ModbusClientConnection *connection)
Close network connection.
@ MODBUS_CONNECTION_STATE_SEND
@ MODBUS_CONNECTION_STATE_CONNECT_TLS
void modbusServerLock(ModbusClientConnection *connection)
Lock Modbus table.
#define MODBUS_MAX_PDU_SIZE
Definition: modbus_common.h:48
Modbus/TCP security layer.
error_t modbusServerWriteCoil(ModbusClientConnection *connection, uint16_t address, bool_t state, bool_t commit)
Write a single coil.
error_t modbusServerFormatMbapHeader(ModbusClientConnection *connection, size_t length)
Format response MBAP header.
void modbusServerUnlock(ModbusClientConnection *connection)
Unlock Modbus table.
error_t modbusServerReadDiscreteInput(ModbusClientConnection *connection, uint16_t address, bool_t *state)
Read a single discrete input.
@ MODBUS_CONNECTION_STATE_RECEIVE
error_t modbusServerReadInputReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single input register.
@ ERROR_END_OF_STREAM
Definition: error.h:211
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TLS
#define timeCompare(t1, t2)
Definition: os_port.h:40
Structure describing socket events.
Definition: socket.h:433
@ ERROR_WRONG_STATE
Definition: error.h:210
error_t modbusServerParseMbapHeader(ModbusClientConnection *connection)
Parse request MBAP header.
#define ModbusClientConnection
void modbusServerProcessConnectionEvents(ModbusClientConnection *connection)
Connection event handler.
error_t
Error codes.
Definition: error.h:43
@ MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE
ModbusExceptionCode modbusServerTranslateExceptionCode(error_t status)
Translate exception code.
Modbus/TCP server.
@ SOCKET_EVENT_TX_READY
Definition: socket.h:175
@ ERROR_INVALID_ADDRESS
Definition: error.h:103
@ MODBUS_CONNECTION_STATE_SHUTDOWN_RX
Helper functions for Modbus/TCP server.
bool_t tlsIsTxReady(TlsContext *context)
Check whether some data is ready for transmission.
Definition: tls.c:2524
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ SOCKET_EVENT_RX_SHUTDOWN
Definition: socket.h:180
Transport protocol abstraction layer.
@ MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
@ MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY
error_t modbusServerShutdownConnection(ModbusClientConnection *connection)
Shutdown network connection.
#define TRACE_INFO(...)
Definition: debug.h:105
uint8_t length
Definition: tcp.h:375
@ MODBUS_CONNECTION_STATE_CLOSED
void modbusServerRegisterConnectionEvents(ModbusClientConnection *connection, SocketEventDesc *eventDesc)
Register connection events.
error_t modbusServerReceiveData(ModbusClientConnection *connection, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
uint_t eventFlags
Returned events.
Definition: socket.h:436
error_t modbusServerProcessRequest(ModbusClientConnection *connection)
Process Modbus request.
uint32_t systime_t
System time.
#define ntohs(value)
Definition: cpu_endian.h:421
bool_t tlsIsRxReady(TlsContext *context)
Check whether some data is available in the receive buffer.
Definition: tls.c:2558
ModbusHeader
@ ERROR_DEVICE_BUSY
Definition: error.h:271
#define TRACE_DEBUG(...)
Definition: debug.h:119
@ ERROR_TIMEOUT
Definition: error.h:95
error_t modbusServerSendData(ModbusClientConnection *connection, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
uint32_t time
void * modbusServerGetRequestPdu(ModbusClientConnection *connection, size_t *length)
Retrieve request PDU.
@ ERROR_INVALID_VALUE
Definition: error.h:116
@ SOCKET_FLAG_NO_DELAY
Definition: socket.h:143
@ SOCKET_EVENT_RX_READY
Definition: socket.h:179
uint8_t n
@ SOCKET_EVENT_TX_SHUTDOWN
Definition: socket.h:178
error_t modbusServerWriteReg(ModbusClientConnection *connection, uint16_t address, uint16_t value, bool_t commit)
Write a single register.
Ipv6Addr address[]
Definition: ipv6.h:345
error_t modbusServerReadHoldingReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single holding register.
uint8_t value[]
Definition: tcp.h:376
void modbusServerTick(ModbusServerContext *context)
Handle periodic operations.
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:89
@ MODBUS_EXCEPTION_ILLEGAL_FUNCTION
void * modbusServerGetResponsePdu(ModbusClientConnection *connection)
Retrieve response PDU.
error_t modbusServerReadCoil(ModbusClientConnection *connection, uint16_t address, bool_t *state)
Read a single coil.
Modbus PDU processing.
error_t modbusServerEstablishSecureConnection(ModbusClientConnection *connection)
Establish secure connection.
@ MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:434
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:73
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:57
ModbusExceptionCode
Modbus exception codes.
#define ModbusServerContext
uint8_t exceptionCode
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TX
#define MODBUS_PROTOCOL_ID
Definition: modbus_common.h:43
uint_t eventMask
Requested events.
Definition: socket.h:435
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ ERROR_INVALID_FUNCTION_CODE
Definition: error.h:270
systime_t osGetSystemTime(void)
Retrieve system time.