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-2023 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.3.2
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  {
459  *length = connection->requestAduLen - sizeof(ModbusHeader);
460  }
461  else
462  {
463  *length = 0;
464  }
465 
466  //Return a pointer to the request PDU
467  return requestPdu;
468 }
469 
470 
471 /**
472  * @brief Retrieve response PDU
473  * @param[in] connection Pointer to the client connection
474  * @return Pointer to the response PDU
475  **/
476 
478 {
479  //Point to the response PDU
480  return connection->responseAdu + sizeof(ModbusHeader);
481 }
482 
483 
484 /**
485  * @brief Lock Modbus table
486  * @param[in] connection Pointer to the client connection
487  **/
488 
490 {
491  ModbusServerContext *context;
492 
493  //Point to the Modbus/TCP server context
494  context = connection->context;
495 
496  //Any registered callback?
497  if(context->settings.lockCallback != NULL)
498  {
499  //Invoke user callback function
500  context->settings.lockCallback();
501  }
502 }
503 
504 
505 /**
506  * @brief Unlock Modbus table
507  * @param[in] connection Pointer to the client connection
508  **/
509 
511 {
512  ModbusServerContext *context;
513 
514  //Point to the Modbus/TCP server context
515  context = connection->context;
516 
517  //Any registered callback?
518  if(context->settings.unlockCallback != NULL)
519  {
520  //Invoke user callback function
521  context->settings.unlockCallback();
522  }
523 }
524 
525 
526 /**
527  * @brief Read a single coil
528  * @param[in] connection Pointer to the client connection
529  * @param[in] address Address of the coil
530  * @param[out] state Current state of the coil
531  * @return Error code
532  **/
533 
535  uint16_t address, bool_t *state)
536 {
537  error_t error;
538  ModbusServerContext *context;
539 
540  //Point to the Modbus/TCP server context
541  context = connection->context;
542 
543  //Any registered callback?
544  if(context->settings.readCoilCallback != NULL)
545  {
546  //Invoke user callback function
547  error = context->settings.readCoilCallback(connection->role, address,
548  state);
549  }
550  else
551  {
552  //Report an error
553  error = ERROR_INVALID_ADDRESS;
554  }
555 
556  //Return status code
557  return error;
558 }
559 
560 
561 /**
562  * @brief Read a single discrete input
563  * @param[in] connection Pointer to the client connection
564  * @param[in] address Address of the discrete input
565  * @param[out] state Current state of the discrete input
566  * @return Error code
567  **/
568 
570  uint16_t address, bool_t *state)
571 {
572  error_t error;
573  ModbusServerContext *context;
574 
575  //Point to the Modbus/TCP server context
576  context = connection->context;
577 
578  //Any registered callback?
579  if(context->settings.readDiscreteInputCallback != NULL)
580  {
581  //Invoke user callback function
582  error = context->settings.readDiscreteInputCallback(connection->role,
583  address, state);
584  }
585  else if(context->settings.readCoilCallback != NULL)
586  {
587  //Invoke user callback function
588  error = context->settings.readCoilCallback(connection->role, address,
589  state);
590  }
591  else
592  {
593  //Report an error
594  error = ERROR_INVALID_ADDRESS;
595  }
596 
597  //Return status code
598  return error;
599 }
600 
601 
602 /**
603  * @brief Write a single coil
604  * @param[in] connection Pointer to the client connection
605  * @param[in] address Address of the coil
606  * @param[in] state Desired state of the coil
607  * @param[in] commit This flag indicates the current phase (validation phase
608  * or write phase if the validation was successful)
609  * @return Error code
610  **/
611 
613  uint16_t address, bool_t state, bool_t commit)
614 {
615  error_t error;
616  ModbusServerContext *context;
617 
618  //Point to the Modbus/TCP server context
619  context = connection->context;
620 
621  //Any registered callback?
622  if(context->settings.writeCoilCallback != NULL)
623  {
624  //Invoke user callback function
625  error = context->settings.writeCoilCallback(connection->role, address,
626  state, commit);
627  }
628  else
629  {
630  //Report an error
631  error = ERROR_INVALID_ADDRESS;
632  }
633 
634  //Return status code
635  return error;
636 }
637 
638 
639 /**
640  * @brief Read a single holding register
641  * @param[in] connection Pointer to the client connection
642  * @param[in] address Address of the holding register
643  * @param[out] value Current value of the holding register
644  * @return Error code
645  **/
646 
648  uint16_t address, uint16_t *value)
649 {
650  error_t error;
651  ModbusServerContext *context;
652 
653  //Point to the Modbus/TCP server context
654  context = connection->context;
655 
656  //Any registered callback?
657  if(context->settings.readHoldingRegCallback != NULL)
658  {
659  //Invoke user callback function
660  error = context->settings.readHoldingRegCallback(connection->role,
661  address, value);
662  }
663  else if(context->settings.readRegCallback != NULL)
664  {
665  //Invoke user callback function
666  error = context->settings.readRegCallback(connection->role, address,
667  value);
668  }
669  else
670  {
671  //Report an error
672  error = ERROR_INVALID_ADDRESS;
673  }
674 
675  //Return status code
676  return error;
677 }
678 
679 
680 /**
681  * @brief Read a single input register
682  * @param[in] connection Pointer to the client connection
683  * @param[in] address Address of the input register
684  * @param[out] value Current value of the input register
685  * @return Error code
686  **/
687 
689  uint16_t address, uint16_t *value)
690 {
691  error_t error;
692  ModbusServerContext *context;
693 
694  //Point to the Modbus/TCP server context
695  context = connection->context;
696 
697  //Any registered callback?
698  if(context->settings.readInputRegCallback != NULL)
699  {
700  //Invoke user callback function
701  error = context->settings.readInputRegCallback(connection->role,
702  address, value);
703  }
704  else if(context->settings.readRegCallback != NULL)
705  {
706  //Invoke user callback function
707  error = context->settings.readRegCallback(connection->role, address,
708  value);
709  }
710  else
711  {
712  //Report an error
713  error = ERROR_INVALID_ADDRESS;
714  }
715 
716  //Return status code
717  return error;
718 }
719 
720 
721 /**
722  * @brief Write a single register
723  * @param[in] connection Pointer to the client connection
724  * @param[in] address Address of the register
725  * @param[in] value Desired value of the register
726  * @param[in] commit This flag indicates the current phase (validation phase
727  * or write phase if the validation was successful)
728  * @return Error code
729  **/
730 
732  uint16_t address, uint16_t value, bool_t commit)
733 {
734  error_t error;
735  ModbusServerContext *context;
736 
737  //Point to the Modbus/TCP server context
738  context = connection->context;
739 
740  //Any registered callback?
741  if(context->settings.writeRegCallback != NULL)
742  {
743  //Invoke user callback function
744  error = context->settings.writeRegCallback(connection->role, address,
745  value, commit);
746  }
747  else
748  {
749  //Report an error
750  error = ERROR_INVALID_ADDRESS;
751  }
752 
753  //Return status code
754  return error;
755 }
756 
757 
758 /**
759  * @brief Translate exception code
760  * @param[in] status Status code
761  * @return Exception code
762  **/
763 
765 {
767 
768  //Check status code
769  switch(status)
770  {
772  //The function code received in the query is not an allowable action
773  //for the server
775  break;
776 
778  //The data address received in the query is not an allowable address
779  //for the server
781  break;
782 
783  case ERROR_INVALID_VALUE:
784  //A value contained in the query data field is not an allowable value
785  //for the server
787  break;
788 
789  case ERROR_DEVICE_BUSY:
790  //The client should retransmit the message later when the server is free
792  break;
793 
794  default:
795  //An unrecoverable error occurred while the server was attempting to
796  //perform the requested action
798  break;
799  }
800 
801  //Return exception code
802  return exceptionCode;
803 }
804 
805 #endif
unsigned int uint_t
Definition: compiler_port.h:50
#define PRIuSIZE
int bool_t
Definition: compiler_port.h:53
#define htons(value)
Definition: cpu_endian.h:413
#define ntohs(value)
Definition: cpu_endian.h:421
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
uint8_t n
uint32_t time
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_FUNCTION_CODE
Definition: error.h:268
@ ERROR_INVALID_ADDRESS
Definition: error.h:103
@ ERROR_WRONG_IDENTIFIER
Definition: error.h:89
@ ERROR_TIMEOUT
Definition: error.h:95
@ ERROR_END_OF_STREAM
Definition: error.h:210
@ ERROR_INVALID_VALUE
Definition: error.h:116
@ NO_ERROR
Success.
Definition: error.h:44
@ ERROR_WRONG_STATE
Definition: error.h:209
@ ERROR_DEVICE_BUSY
Definition: error.h:269
@ ERROR_INVALID_LENGTH
Definition: error.h:111
Ipv6Addr address[]
Definition: ipv6.h:314
ModbusExceptionCode
Modbus exception codes.
@ MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE
@ MODBUS_EXCEPTION_SLAVE_DEVICE_BUSY
@ MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS
@ MODBUS_EXCEPTION_SLAVE_DEVICE_FAILURE
@ MODBUS_EXCEPTION_ILLEGAL_FUNCTION
uint8_t exceptionCode
#define MODBUS_PROTOCOL_ID
Definition: modbus_common.h:43
ModbusHeader
#define MODBUS_MAX_PDU_SIZE
Definition: modbus_common.h:48
Modbus/TCP server.
#define ModbusServerContext
@ MODBUS_CONNECTION_STATE_SEND
@ MODBUS_CONNECTION_STATE_RECEIVE
@ MODBUS_CONNECTION_STATE_CONNECT_TLS
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TLS
@ MODBUS_CONNECTION_STATE_SHUTDOWN_RX
@ MODBUS_CONNECTION_STATE_SHUTDOWN_TX
@ MODBUS_CONNECTION_STATE_CLOSED
#define ModbusClientConnection
#define MODBUS_SERVER_MAX_CONNECTIONS
Definition: modbus_server.h:66
void modbusServerRegisterConnectionEvents(ModbusClientConnection *connection, SocketEventDesc *eventDesc)
Register connection events.
error_t modbusServerWriteReg(ModbusClientConnection *connection, uint16_t address, uint16_t value, bool_t commit)
Write a single register.
error_t modbusServerFormatMbapHeader(ModbusClientConnection *connection, size_t length)
Format response MBAP header.
ModbusExceptionCode modbusServerTranslateExceptionCode(error_t status)
Translate exception code.
error_t modbusServerWriteCoil(ModbusClientConnection *connection, uint16_t address, bool_t state, bool_t commit)
Write a single coil.
error_t modbusServerParseMbapHeader(ModbusClientConnection *connection)
Parse request MBAP header.
void * modbusServerGetResponsePdu(ModbusClientConnection *connection)
Retrieve response PDU.
error_t modbusServerReadDiscreteInput(ModbusClientConnection *connection, uint16_t address, bool_t *state)
Read a single discrete input.
void modbusServerTick(ModbusServerContext *context)
Handle periodic operations.
error_t modbusServerReadInputReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single input register.
error_t modbusServerReadHoldingReg(ModbusClientConnection *connection, uint16_t address, uint16_t *value)
Read a single holding register.
void modbusServerUnlock(ModbusClientConnection *connection)
Unlock Modbus table.
error_t modbusServerReadCoil(ModbusClientConnection *connection, uint16_t address, bool_t *state)
Read a single coil.
void * modbusServerGetRequestPdu(ModbusClientConnection *connection, size_t *length)
Retrieve request PDU.
void modbusServerProcessConnectionEvents(ModbusClientConnection *connection)
Connection event handler.
void modbusServerLock(ModbusClientConnection *connection)
Lock Modbus table.
Helper functions for Modbus/TCP server.
error_t modbusServerProcessRequest(ModbusClientConnection *connection)
Process Modbus request.
Modbus PDU processing.
error_t modbusServerEstablishSecureConnection(ModbusClientConnection *connection)
Establish secure connection.
Modbus/TCP security layer.
error_t modbusServerShutdownConnection(ModbusClientConnection *connection)
Shutdown network connection.
void modbusServerCloseConnection(ModbusClientConnection *connection)
Close network connection.
error_t modbusServerReceiveData(ModbusClientConnection *connection, void *data, size_t size, size_t *received, uint_t flags)
Receive data using the relevant transport protocol.
error_t modbusServerSendData(ModbusClientConnection *connection, const void *data, size_t length, size_t *written, uint_t flags)
Send data using the relevant transport protocol.
Transport protocol abstraction layer.
#define timeCompare(t1, t2)
Definition: os_port.h:42
systime_t osGetSystemTime(void)
Retrieve system time.
uint32_t systime_t
System time.
@ SOCKET_FLAG_NO_DELAY
Definition: socket.h:133
@ SOCKET_EVENT_RX_SHUTDOWN
Definition: socket.h:170
@ SOCKET_EVENT_TX_READY
Definition: socket.h:165
@ SOCKET_EVENT_RX_READY
Definition: socket.h:169
@ SOCKET_EVENT_TX_SHUTDOWN
Definition: socket.h:168
Structure describing socket events.
Definition: socket.h:395
uint_t eventMask
Requested events.
Definition: socket.h:397
Socket * socket
Handle to a socket to monitor.
Definition: socket.h:396
uint_t eventFlags
Returned events.
Definition: socket.h:398
uint8_t length
Definition: tcp.h:366
uint8_t value[]
Definition: tcp.h:367
bool_t tlsIsTxReady(TlsContext *context)
Check whether some data is ready for transmission.
Definition: tls.c:2186
bool_t tlsIsRxReady(TlsContext *context)
Check whether some data is available in the receive buffer.
Definition: tls.c:2220