1 /*
2  * SPDX-FileCopyrightText: 2006 Christian Walter
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
7  */
8 /*
9  * FreeModbus Libary: ESP32 TCP Port
10  * Copyright (C) 2006 Christian Walter <wolti@sil.at>
11  * Parts of crt0.S Copyright (c) 1995, 1996, 1998 Cygnus Support
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *   notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *   notice, this list of conditions and the following disclaimer in the
20  *   documentation and/or other materials provided with the distribution.
21  * 3. The name of the author may not be used to endorse or promote products
22  *   derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  *
35  * File: $Id: port.h,v 1.2 2006/09/04 14:39:20 wolti Exp $
36  */
37 
38 #ifndef _PORT_TCP_SLAVE_H
39 #define _PORT_TCP_SLAVE_H
40 
41 /* ----------------------- Platform includes --------------------------------*/
42 #include "esp_log.h"
43 
44 #include "lwip/opt.h"
45 #include "lwip/sys.h"
46 #include "port.h"
47 #include "esp_modbus_common.h"      // for common types for network options
48 
49 /* ----------------------- Defines ------------------------------------------*/
50 
51 #ifndef TRUE
52 #define TRUE                    1
53 #endif
54 
55 #ifndef FALSE
56 #define FALSE                   0
57 #endif
58 
59 #ifdef __cplusplus
60 PR_BEGIN_EXTERN_C
61 #endif
62 
63 /* ----------------------- Type definitions ---------------------------------*/
64 typedef struct {
65     int xIndex;                     /*!< Modbus info index */
66     int xSockId;                    /*!< Socket id */
67     int xError;                     /*!< TCP/UDP sock error */
68     const char* pcIpAddr;           /*!< TCP/UDP IP address (string) */
69     UCHAR* pucTCPBuf;               /*!< buffer pointer */
70     USHORT usTCPBufPos;             /*!< buffer active position */
71     USHORT usTCPFrameBytesLeft;     /*!< buffer left bytes to receive transaction */
72     int64_t xSendTimeStamp;         /*!< send request timestamp */
73     int64_t xRecvTimeStamp;         /*!< receive response timestamp */
74     USHORT usTidCnt;                /*!< last TID counter from packet */
75 } MbClientInfo_t;
76 
77 typedef struct {
78     TaskHandle_t xMbTcpTaskHandle;      /*!< Server task handle */
79     xQueueHandle xRespQueueHandle;      /*!< Response queue handle */
80     MbClientInfo_t* pxCurClientInfo;    /*!< Current client info */
81     MbClientInfo_t** pxMbClientInfo;    /*!< Pointers to information about connected clients */
82     USHORT usPort;                      /*!< TCP/UDP port number */
83     CHAR* pcBindAddr;                   /*!< IP address to bind */
84     eMBPortProto eMbProto;              /*!< Protocol type used by port */
85     USHORT usClientCount;               /*!< Client connection count */
86     void* pvNetIface;                   /*!< Network netif interface pointer for port */
87     eMBPortIpVer xIpVer;                /*!< IP protocol version */
88 } MbSlavePortConfig_t;
89 
90 /* ----------------------- Function prototypes ------------------------------*/
91 
92 /**
93  * Function to setup communication options for TCP/UDP Modbus port
94  *
95  * @param pvNetIf netif interface pointer
96  * @param xIpVersion IP version
97  * @param xProto protocol type option
98  * @param pcBindAddr IP bind address
99  *
100  * @return error code
101  */
102 void vMBTCPPortSlaveSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto, CHAR* pcBindAddr);
103 
104 #ifdef __cplusplus
105 PR_END_EXTERN_C
106 #endif
107 #endif
108