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-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 "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->settings.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->settings.tickCallback != NULL)
90  {
91  //Invoke user callback function
92  context->settings.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->settings.unitId == 0 ||
261  context->settings.unitId == 255 ||
262  context->settings.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->settings.lockCallback != NULL)
511  {
512  //Invoke user callback function
513  context->settings.lockCallback();
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->settings.unlockCallback != NULL)
532  {
533  //Invoke user callback function
534  context->settings.unlockCallback();
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->settings.readCoilCallback != NULL)
558  {
559  //Invoke user callback function
560  error = context->settings.readCoilCallback(connection->role, address,
561  state);
562  }
563  else
564  {
565  //Report an error
566  error = ERROR_INVALID_ADDRESS;
567  }
568 
569  //Return status code
570  return error;
571 }
572 
573 
574 /**
575  * @brief Read a single discrete input
576  * @param[in] connection Pointer to the client connection
577  * @param[in] address Address of the discrete input
578  * @param[out] state Current state of the discrete input
579  * @return Error code
580  **/
581 
583  uint16_t address, bool_t *state)
584 {
585  error_t error;
586  ModbusServerContext *context;
587 
588  //Point to the Modbus/TCP server context
589  context = connection->context;
590 
591  //Any registered callback?
592  if(context->settings.readDiscreteInputCallback != NULL)
593  {
594  //Invoke user callback function
595  error = context->settings.readDiscreteInputCallback(connection->role,
596  address, state);
597  }
598  else if(context->settings.readCoilCallback != NULL)
599  {
600  //Invoke user callback function
601  error = context->settings.readCoilCallback(connection->role, address,
602  state);
603  }
604  else
605  {
606  //Report an error
607  error = ERROR_INVALID_ADDRESS;
608  }
609 
610  //Return status code
611  return error;
612 }
613 
614 
615 /**
616  * @brief Write a single coil
617  * @param[in] connection Pointer to the client connection
618  * @param[in] address Address of the coil
619  * @param[in] state Desired state of the coil
620  * @param[in] commit This flag indicates the current phase (validation phase
621  * or write phase if the validation was successful)
622  * @return Error code
623  **/
624 
626  uint16_t address, bool_t state, bool_t commit)
627 {
628  error_t error;
629  ModbusServerContext *context;
630 
631  //Point to the Modbus/TCP server context
632  context = connection->context;
633 
634  //Any registered callback?
635  if(context->settings.writeCoilCallback != NULL)
636  {
637  //Invoke user callback function
638  error = context->settings.writeCoilCallback(connection->role, address,
639  state, commit);
640  }
641  else
642  {
643  //Report an error
644  error = ERROR_INVALID_ADDRESS;
645  }
646 
647  //Return status code
648  return error;
649 }
650 
651 
652 /**
653  * @brief Read a single holding register
654  * @param[in] connection Pointer to the client connection
655  * @param[in] address Address of the holding register
656  * @param[out] value Current value of the holding register
657  * @return Error code
658  **/
659 
661  uint16_t address, uint16_t *value)
662 {
663  error_t error;
664  ModbusServerContext *context;
665 
666  //Point to the Modbus/TCP server context
667  context = connection->context;
668 
669  //Any registered callback?
670  if(context->settings.readHoldingRegCallback != NULL)
671  {
672  //Invoke user callback function
673  error = context->settings.readHoldingRegCallback(connection->role,
674  address, value);
675  }
676  else if(context->settings.readRegCallback != NULL)
677  {
678  //Invoke user callback function
679  error = context->settings.readRegCallback(connection->role, address,
680  value);
681  }
682  else
683  {
684  //Report an error
685  error = ERROR_INVALID_ADDRESS;
686  }
687 
688  //Return status code
689  return error;
690 }
691 
692 
693 /**
694  * @brief Read a single input register
695  * @param[in] connection Pointer to the client connection
696  * @param[in] address Address of the input register
697  * @param[out] value Current value of the input register
698  * @return Error code
699  **/
700 
702  uint16_t address, uint16_t *value)
703 {
704  error_t error;
705  ModbusServerContext *context;
706 
707  //Point to the Modbus/TCP server context
708  context = connection->context;
709 
710  //Any registered callback?
711  if(context->settings.readInputRegCallback != NULL)
712  {
713  //Invoke user callback function
714  error = context->settings.readInputRegCallback(connection->role,
715  address, value);
716  }
717  else if(context->settings.readRegCallback != NULL)
718  {
719  //Invoke user callback function
720  error = context->settings.readRegCallback(connection->role, address,
721  value);
722  }
723  else
724  {
725  //Report an error
726  error = ERROR_INVALID_ADDRESS;
727  }
728 
729  //Return status code
730  return error;
731 }
732 
733 
734 /**
735  * @brief Write a single register
736  * @param[in] connection Pointer to the client connection
737  * @param[in] address Address of the register
738  * @param[in] value Desired value of the register
739  * @param[in] commit This flag indicates the current phase (validation phase
740  * or write phase if the validation was successful)
741  * @return Error code
742  **/
743 
745  uint16_t address, uint16_t value, bool_t commit)
746 {
747  error_t error;
748  ModbusServerContext *context;
749 
750  //Point to the Modbus/TCP server context
751  context = connection->context;
752 
753  //Any registered callback?
754  if(context->settings.writeRegCallback != NULL)
755  {
756  //Invoke user callback function
757  error = context->settings.writeRegCallback(connection->role, address,
758  value, commit);
759  }
760  else
761  {
762  //Report an error
763  error = ERROR_INVALID_ADDRESS;
764  }
765 
766  //Return status code
767  return error;
768 }
769 
770 
771 /**
772  * @brief Translate exception code
773  * @param[in] status Status code
774  * @return Exception code
775  **/
776 
778 {
780 
781  //Check status code
782  switch(status)
783  {
785  //The function code received in the query is not an allowable action
786  //for the server
788  break;
789 
791  //The data address received in the query is not an allowable address
792  //for the server
794  break;
795 
796  case ERROR_INVALID_VALUE:
797  //A value contained in the query data field is not an allowable value
798  //for the server
800  break;
801 
802  case ERROR_DEVICE_BUSY:
803  //The client should retransmit the message later when the server is free
805  break;
806 
807  default:
808  //An unrecoverable error occurred while the server was attempting to
809  //perform the requested action
811  break;
812  }
813 
814  //Return exception code
815  return exceptionCode;
816 }
817 
818 #endif
#define htons(value)
Definition: cpu_endian.h:413
int bool_t
Definition: compiler_port.h:53
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:210
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TLS
#define timeCompare(t1, t2)
Definition: os_port.h:40
Structure describing socket events.
Definition: socket.h:426
@ ERROR_WRONG_STATE
Definition: error.h:209
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:2228
@ 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:95
uint8_t length
Definition: tcp.h:368
@ 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:429
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:2262
ModbusHeader
@ ERROR_DEVICE_BUSY
Definition: error.h:270
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ 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:325
error_t modbusServerReadHoldingReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single holding register.
uint8_t value[]
Definition: tcp.h:369
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:427
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:73
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:50
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:428
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ ERROR_INVALID_FUNCTION_CODE
Definition: error.h:269
systime_t osGetSystemTime(void)
Retrieve system time.