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/sys.h"
45 #include "freertos/event_groups.h"
46 #include "port.h"
47 
48 /* ----------------------- Defines ------------------------------------------*/
49 
50 #ifndef TRUE
51 #define TRUE                    1
52 #endif
53 
54 #ifndef FALSE
55 #define FALSE                   0
56 #endif
57 
58 #ifdef __cplusplus
59 PR_BEGIN_EXTERN_C
60 #endif
61 
62 /* ----------------------- Type definitions ---------------------------------*/
63 
64 typedef struct {
65     int xIndex;                 /*!< Slave information index */
66     int xSockId;                /*!< Socket ID of slave */
67     int xError;                 /*!< Socket error */
68     int xRcvErr;                /*!< Socket receive error */
69     const char* pcIpAddr;       /*!< TCP/UDP IP address */
70     UCHAR ucSlaveAddr;          /*!< Slave short address */
71     UCHAR* pucRcvBuf;           /*!< Receive buffer pointer */
72     USHORT usRcvPos;            /*!< Receive buffer position */
73     int pcPort;                 /*!< TCP/UDP port number */
74     eMBPortProto xMbProto;      /*!< Protocol type */
75     int64_t xSendTimeStamp;     /*!< Send request time stamp */
76     int64_t xRecvTimeStamp;     /*!< Receive response time stamp */
77     uint16_t usTidCnt;          /*!< Transaction identifier (TID) for slave */
78 } MbSlaveInfo_t;
79 
80 typedef struct {
81     TaskHandle_t  xMbTcpTaskHandle;     /*!< Master TCP/UDP handling task handle */
82     QueueHandle_t xConnectQueue;        /*!< Master connection queue */
83     USHORT usPort;                      /*!< Master TCP/UDP port number */
84     USHORT usMbSlaveInfoCount;          /*!< Master count of connected slaves */
85     USHORT ucCurSlaveIndex;             /*!< Master current processing slave index */
86     eMBPortIpVer eMbIpVer;              /*!< Master IP version */
87     eMBPortProto eMbProto;              /*!< Master protocol type */
88     void* pvNetIface;                   /*!< Master netif interface pointer */
89     MbSlaveInfo_t** pxMbSlaveInfo;      /*!< Master information structure for each connected slave */
90     MbSlaveInfo_t* pxMbSlaveCurrInfo;   /*!< Master current slave information */
91 } MbPortConfig_t;
92 
93 typedef struct {
94     USHORT usIndex;                     /*!< index of the address info */
95     const char* pcIPAddr;               /*!< represents the IP address of the slave */
96     UCHAR ucSlaveAddr;                  /*!< slave unit ID (UID) field for MBAP frame  */
97 } MbSlaveAddrInfo_t;
98 
99 /* ----------------------- Function prototypes ------------------------------*/
100 
101 // The functions below are used by Modbus controller interface to configure Modbus port.
102 /**
103  * Registers slave IP address
104  *
105  * @param usIndex index of element in the configuration
106  * @param pcIpStr IP address to register
107  * @param ucSlaveAddress slave element index
108  *
109  * @return TRUE if address registered successfully, else FALSE
110  */
111 BOOL xMBTCPPortMasterAddSlaveIp(const USHORT usIndex, const CHAR* pcIpStr, UCHAR ucSlaveAddress);
112 
113 /**
114  * Keeps FSM event handle and mask then wait for Master stack to start
115  *
116  * @param xEventHandle Master event handle
117  * @param xEvent event mask to start Modbus stack FSM
118  * @param usTimeout - timeout in ticks to wait for stack to start
119  *
120  * @return TRUE if stack started, else FALSE
121  */
122 BOOL xMBTCPPortMasterWaitEvent(EventGroupHandle_t xEventHandle, EventBits_t xEvent, USHORT usTimeout);
123 
124 /**
125  * Set network options for Master port
126  *
127  * @param pvNetIf netif interface pointer
128  * @param xIpVersion IP version option for the Master port
129  * @param xProto Protocol version option for the Master port
130  *
131  * @return None
132  */
133 void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto);
134 
135 #ifdef __cplusplus
136 PR_END_EXTERN_C
137 #endif
138 #endif
139