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-2022 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.2.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->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  //Check unit identifier
256  if(context->settings.unitId == 0 ||
257  context->settings.unitId == 255 ||
258  context->settings.unitId == connection->requestUnitId)
259  {
260  //Process Modbus request
261  error = modbusServerProcessRequest(connection);
262  }
263  }
264  }
265  }
266  else
267  {
268  //Just for sanity
269  error = ERROR_WRONG_STATE;
270  }
271  }
272  else if(connection->state == MODBUS_CONNECTION_STATE_SEND)
273  {
274  //Send Modbus response
275  if(connection->responseAduPos < connection->responseAduLen)
276  {
277  //Send more data
278  error = modbusServerSendData(connection,
279  connection->responseAdu + connection->responseAduPos,
280  connection->responseAduLen - connection->responseAduPos,
282 
283  //Check status code
284  if(error == NO_ERROR || error == ERROR_TIMEOUT)
285  {
286  //Advance data pointer
287  connection->responseAduPos += n;
288 
289  //Modbus response successfully sent?
290  if(connection->responseAduPos >= connection->responseAduLen)
291  {
292  //Flush receive buffer
293  connection->requestAduLen = 0;
294  connection->requestAduPos = 0;
295 
296  //Wait for the next Modbus request
297  connection->state = MODBUS_CONNECTION_STATE_RECEIVE;
298  }
299  }
300  }
301  else
302  {
303  //Just for sanity
304  error = ERROR_WRONG_STATE;
305  }
306  }
307  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TLS ||
308  connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_TX)
309  {
310  //Graceful connection shutdown
311  error = modbusServerShutdownConnection(connection);
312  }
313  else if(connection->state == MODBUS_CONNECTION_STATE_SHUTDOWN_RX)
314  {
315  //Close the Modbus/TCP connection
316  modbusServerCloseConnection(connection);
317  }
318  else
319  {
320  //Invalid state
321  error = ERROR_WRONG_STATE;
322  }
323 
324  //Any communication error?
325  if(error != NO_ERROR && error != ERROR_TIMEOUT)
326  {
327  //Close the Modbus/TCP connection
328  modbusServerCloseConnection(connection);
329  }
330 }
331 
332 
333 /**
334  * @brief Parse request MBAP header
335  * @param[in] connection Pointer to the client connection
336  * @return Error code
337  **/
338 
340 {
341  size_t n;
342  ModbusHeader *requestHeader;
343 
344  //Sanity check
345  if(connection->requestAduPos < sizeof(ModbusHeader))
346  return ERROR_INVALID_LENGTH;
347 
348  //Point to the beginning of the request ADU
349  requestHeader = (ModbusHeader *) connection->requestAdu;
350 
351  //The length field is a byte count of the following fields, including
352  //the unit identifier and data fields
353  n = ntohs(requestHeader->length);
354 
355  //Malformed Modbus request?
356  if(n < sizeof(uint8_t))
357  return ERROR_INVALID_LENGTH;
358 
359  //Retrieve the length of the PDU
360  n -= sizeof(uint8_t);
361 
362  //Debug message
363  TRACE_DEBUG("\r\nModbus Server: ADU received (%" PRIuSIZE " bytes)...\r\n",
364  sizeof(ModbusHeader) + n);
365 
366  //Dump MBAP header
367  TRACE_DEBUG(" Transaction ID = %" PRIu16 "\r\n", ntohs(requestHeader->transactionId));
368  TRACE_DEBUG(" Protocol ID = %" PRIu16 "\r\n", ntohs(requestHeader->protocolId));
369  TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(requestHeader->length));
370  TRACE_DEBUG(" Unit ID = %" PRIu16 "\r\n", requestHeader->unitId);
371 
372  //Check protocol identifier
373  if(ntohs(requestHeader->protocolId) != MODBUS_PROTOCOL_ID)
374  return ERROR_WRONG_IDENTIFIER;
375 
376  //The length of the Modbus PDU is limited to 253 bytes
377  if(n > MODBUS_MAX_PDU_SIZE)
378  return ERROR_INVALID_LENGTH;
379 
380  //Save unit identifier
381  connection->requestUnitId = requestHeader->unitId;
382  //Compute the length of the request ADU
383  connection->requestAduLen = sizeof(ModbusHeader) + n;
384 
385  //Successful processing
386  return NO_ERROR;
387 }
388 
389 
390 /**
391  * @brief Format response MBAP header
392  * @param[in] connection Pointer to the client connection
393  * @param[in] length Length of the PDU, in bytes
394  * @return Error code
395  **/
396 
398  size_t length)
399 {
400  ModbusHeader *requestHeader;
401  ModbusHeader *responseHeader;
402 
403  //Sanity check
404  if(connection->requestAduPos < sizeof(ModbusHeader))
405  return ERROR_INVALID_LENGTH;
406 
407  //Point to the beginning of the request ADU
408  requestHeader = (ModbusHeader *) connection->requestAdu;
409  //Point to the beginning of the response ADU
410  responseHeader = (ModbusHeader *) connection->responseAdu;
411 
412  //Format MBAP header
413  responseHeader->transactionId = requestHeader->transactionId;
414  responseHeader->protocolId = requestHeader->protocolId;
415  responseHeader->length = htons(length + sizeof(uint8_t));
416  responseHeader->unitId = requestHeader->unitId;
417 
418  //Compute the length of the response ADU
419  connection->responseAduLen = sizeof(ModbusHeader) + length;
420 
421  //Debug message
422  TRACE_DEBUG("Modbus Server: Sending ADU (%" PRIuSIZE " bytes)...\r\n",
423  connection->responseAduLen);
424 
425  //Dump MBAP header
426  TRACE_DEBUG(" Transaction ID = %" PRIu16 "\r\n", ntohs(responseHeader->transactionId));
427  TRACE_DEBUG(" Protocol ID = %" PRIu16 "\r\n", ntohs(responseHeader->protocolId));
428  TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(responseHeader->length));
429  TRACE_DEBUG(" Unit ID = %" PRIu16 "\r\n", responseHeader->unitId);
430 
431  //Rewind to the beginning of the transmit buffer
432  connection->responseAduPos = 0;
433  //Send the response ADU to the client
434  connection->state = MODBUS_CONNECTION_STATE_SEND;
435 
436  //Successful processing
437  return NO_ERROR;
438 }
439 
440 
441 /**
442  * @brief Retrieve request PDU
443  * @param[in] connection Pointer to the client connection
444  * @param[out] length Length request the request PDU, in bytes
445  * @return Pointer to the request PDU
446  **/
447 
449  size_t *length)
450 {
451  uint8_t *requestPdu;
452 
453  //Point to the request PDU
454  requestPdu = connection->requestAdu + sizeof(ModbusHeader);
455 
456  //Retrieve the length of the PDU
457  if(connection->requestAduLen >= sizeof(ModbusHeader))
458  *length = connection->requestAduLen - sizeof(ModbusHeader);
459  else
460  *length = 0;
461 
462  //Return a pointer to the request PDU
463  return requestPdu;
464 }
465 
466 
467 /**
468  * @brief Retrieve response PDU
469  * @param[in] connection Pointer to the client connection
470  * @return Pointer to the response PDU
471  **/
472 
474 {
475  //Point to the response PDU
476  return connection->responseAdu + sizeof(ModbusHeader);
477 }
478 
479 
480 /**
481  * @brief Lock Modbus table
482  * @param[in] connection Pointer to the client connection
483  **/
484 
486 {
487  ModbusServerContext *context;
488 
489  //Point to the Modbus/TCP server context
490  context = connection->context;
491 
492  //Any registered callback?
493  if(context->settings.lockCallback != NULL)
494  {
495  //Invoke user callback function
496  context->settings.lockCallback();
497  }
498 }
499 
500 
501 /**
502  * @brief Unlock Modbus table
503  * @param[in] connection Pointer to the client connection
504  **/
505 
507 {
508  ModbusServerContext *context;
509 
510  //Point to the Modbus/TCP server context
511  context = connection->context;
512 
513  //Any registered callback?
514  if(context->settings.unlockCallback != NULL)
515  {
516  //Invoke user callback function
517  context->settings.unlockCallback();
518  }
519 }
520 
521 
522 /**
523  * @brief Read a single coil
524  * @param[in] connection Pointer to the client connection
525  * @param[in] address Address of the coil
526  * @param[out] state Current state of the coil
527  * @return Error code
528  **/
529 
531  uint16_t address, bool_t *state)
532 {
533  error_t error;
534  ModbusServerContext *context;
535 
536  //Point to the Modbus/TCP server context
537  context = connection->context;
538 
539  //Any registered callback?
540  if(context->settings.readCoilCallback != NULL)
541  {
542  //Invoke user callback function
543  error = context->settings.readCoilCallback(connection->role, address,
544  state);
545  }
546  else
547  {
548  //Report an error
549  error = ERROR_INVALID_ADDRESS;
550  }
551 
552  //Return status code
553  return error;
554 }
555 
556 
557 /**
558  * @brief Read a single discrete input
559  * @param[in] connection Pointer to the client connection
560  * @param[in] address Address of the discrete input
561  * @param[out] state Current state of the discrete input
562  * @return Error code
563  **/
564 
566  uint16_t address, bool_t *state)
567 {
568  error_t error;
569  ModbusServerContext *context;
570 
571  //Point to the Modbus/TCP server context
572  context = connection->context;
573 
574  //Any registered callback?
575  if(context->settings.readDiscreteInputCallback != NULL)
576  {
577  //Invoke user callback function
578  error = context->settings.readDiscreteInputCallback(connection->role,
579  address, state);
580  }
581  else if(context->settings.readCoilCallback != NULL)
582  {
583  //Invoke user callback function
584  error = context->settings.readCoilCallback(connection->role, address,
585  state);
586  }
587  else
588  {
589  //Report an error
590  error = ERROR_INVALID_ADDRESS;
591  }
592 
593  //Return status code
594  return error;
595 }
596 
597 
598 /**
599  * @brief Write a single coil
600  * @param[in] connection Pointer to the client connection
601  * @param[in] address Address of the coil
602  * @param[in] state Desired state of the coil
603  * @param[in] commit This flag indicates the current phase (validation phase
604  * or write phase if the validation was successful)
605  * @return Error code
606  **/
607 
609  uint16_t address, bool_t state, bool_t commit)
610 {
611  error_t error;
612  ModbusServerContext *context;
613 
614  //Point to the Modbus/TCP server context
615  context = connection->context;
616 
617  //Any registered callback?
618  if(context->settings.writeCoilCallback != NULL)
619  {
620  //Invoke user callback function
621  error = context->settings.writeCoilCallback(connection->role, address,
622  state, commit);
623  }
624  else
625  {
626  //Report an error
627  error = ERROR_INVALID_ADDRESS;
628  }
629 
630  //Return status code
631  return error;
632 }
633 
634 
635 /**
636  * @brief Read a single holding register
637  * @param[in] connection Pointer to the client connection
638  * @param[in] address Address of the holding register
639  * @param[out] value Current value of the holding register
640  * @return Error code
641  **/
642 
644  uint16_t address, uint16_t *value)
645 {
646  error_t error;
647  ModbusServerContext *context;
648 
649  //Point to the Modbus/TCP server context
650  context = connection->context;
651 
652  //Any registered callback?
653  if(context->settings.readHoldingRegCallback != NULL)
654  {
655  //Invoke user callback function
656  error = context->settings.readHoldingRegCallback(connection->role,
657  address, value);
658  }
659  else if(context->settings.readRegCallback != NULL)
660  {
661  //Invoke user callback function
662  error = context->settings.readRegCallback(connection->role, address,
663  value);
664  }
665  else
666  {
667  //Report an error
668  error = ERROR_INVALID_ADDRESS;
669  }
670 
671  //Return status code
672  return error;
673 }
674 
675 
676 /**
677  * @brief Read a single input register
678  * @param[in] connection Pointer to the client connection
679  * @param[in] address Address of the input register
680  * @param[out] value Current value of the input register
681  * @return Error code
682  **/
683 
685  uint16_t address, uint16_t *value)
686 {
687  error_t error;
688  ModbusServerContext *context;
689 
690  //Point to the Modbus/TCP server context
691  context = connection->context;
692 
693  //Any registered callback?
694  if(context->settings.readInputRegCallback != NULL)
695  {
696  //Invoke user callback function
697  error = context->settings.readInputRegCallback(connection->role,
698  address, value);
699  }
700  else if(context->settings.readRegCallback != NULL)
701  {
702  //Invoke user callback function
703  error = context->settings.readRegCallback(connection->role, address,
704  value);
705  }
706  else
707  {
708  //Report an error
709  error = ERROR_INVALID_ADDRESS;
710  }
711 
712  //Return status code
713  return error;
714 }
715 
716 
717 /**
718  * @brief Write a single register
719  * @param[in] connection Pointer to the client connection
720  * @param[in] address Address of the register
721  * @param[in] value Desired value of the register
722  * @param[in] commit This flag indicates the current phase (validation phase
723  * or write phase if the validation was successful)
724  * @return Error code
725  **/
726 
728  uint16_t address, uint16_t value, bool_t commit)
729 {
730  error_t error;
731  ModbusServerContext *context;
732 
733  //Point to the Modbus/TCP server context
734  context = connection->context;
735 
736  //Any registered callback?
737  if(context->settings.writeRegCallback != NULL)
738  {
739  //Invoke user callback function
740  error = context->settings.writeRegCallback(connection->role, address,
741  value, commit);
742  }
743  else
744  {
745  //Report an error
746  error = ERROR_INVALID_ADDRESS;
747  }
748 
749  //Return status code
750  return error;
751 }
752 
753 
754 /**
755  * @brief Translate exception code
756  * @param[in] status Status code
757  * @return Exception code
758  **/
759 
761 {
763 
764  //Check status code
765  switch(status)
766  {
768  //The function code received in the query is not an allowable action
769  //for the server
771  break;
772 
774  //The data address received in the query is not an allowable address
775  //for the server
777  break;
778 
779  case ERROR_INVALID_VALUE:
780  //A value contained in the query data field is not an allowable value
781  //for the server
783  break;
784 
785  case ERROR_DEVICE_BUSY:
786  //The client should retransmit the message later when the server is free
788  break;
789 
790  default:
791  //An unrecoverable error occurred while the server was attempting to
792  //perform the requested action
794  break;
795  }
796 
797  //Return exception code
798  return exceptionCode;
799 }
800 
801 #endif
__start_packed struct @0 ModbusHeader
MBAP header (Modbus Application Protocol)
uint8_t length
Definition: coap_common.h:193
#define htons(value)
Definition: cpu_endian.h:413
int bool_t
Definition: compiler_port.h:48
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:209
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TLS
#define timeCompare(t1, t2)
Definition: os_port.h:42
Structure describing socket events.
Definition: socket.h:365
@ ERROR_WRONG_STATE
Definition: error.h:208
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:158
uint8_t value[]
Definition: tcp.h:367
@ ERROR_INVALID_ADDRESS
Definition: error.h:102
@ 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:2092
@ ERROR_INVALID_LENGTH
Definition: error.h:110
@ SOCKET_EVENT_RX_SHUTDOWN
Definition: socket.h:163
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
@ 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:368
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:2125
@ ERROR_DEVICE_BUSY
Definition: error.h:268
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ ERROR_TIMEOUT
Definition: error.h:94
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:115
@ SOCKET_FLAG_NO_DELAY
Definition: socket.h:126
@ SOCKET_EVENT_RX_READY
Definition: socket.h:162
uint8_t n
@ SOCKET_EVENT_TX_SHUTDOWN
Definition: socket.h:161
error_t modbusServerWriteReg(ModbusClientConnection *connection, uint16_t address, uint16_t value, bool_t commit)
Write a single register.
error_t modbusServerReadHoldingReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single holding register.
void modbusServerTick(ModbusServerContext *context)
Handle periodic operations.
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:88
@ 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.
Ipv6Addr address
error_t modbusServerEstablishSecureConnection(ModbusClientConnection *connection)
Establish secure connection.
@ MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:366
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:66
#define PRIuSIZE
unsigned int uint_t
Definition: compiler_port.h:45
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:367
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ ERROR_INVALID_FUNCTION_CODE
Definition: error.h:267
systime_t osGetSystemTime(void)
Retrieve system time.