1  /* Copyright 2018 Espressif Systems (Shanghai) PTE LTD
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14 */
15  /*
16   * FreeModbus Libary: ESP32 TCP Port
17   * Copyright (C) 2006 Christian Walter <wolti@sil.at>
18   * Parts of crt0.S Copyright (c) 1995, 1996, 1998 Cygnus Support
19   *
20   * This library is free software; you can redistribute it and/or
21   * modify it under the terms of the GNU Lesser General Public
22   * License as published by the Free Software Foundation; either
23   * version 2.1 of the License, or (at your option) any later version.
24   *
25   * This library is distributed in the hope that it will be useful,
26   * but WITHOUT ANY WARRANTY; without even the implied warranty of
27   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
28   * Lesser General Public License for more details.
29   *
30   * You should have received a copy of the GNU Lesser General Public
31   * License along with this library; if not, write to the Free Software
32   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
33   *
34   * File: $Id: port.h,v 1.2 2006/09/04 14:39:20 wolti Exp $
35   */
36 
37 #ifndef _PORT_TCP_SLAVE_H
38 #define _PORT_TCP_SLAVE_H
39 
40 /* ----------------------- Platform includes --------------------------------*/
41 #include "esp_log.h"
42 
43 #include "lwip/sys.h"
44 #include "freertos/event_groups.h"
45 #include "port.h"
46 
47 /* ----------------------- Defines ------------------------------------------*/
48 
49 #ifndef TRUE
50 #define TRUE                    1
51 #endif
52 
53 #ifndef FALSE
54 #define FALSE                   0
55 #endif
56 
57 #ifdef __cplusplus
58 PR_BEGIN_EXTERN_C
59 #endif
60 
61 /* ----------------------- Type definitions ---------------------------------*/
62 
63 typedef struct {
64     int xIndex;                 /*!< Slave information index */
65     int xSockId;                /*!< Socket ID of slave */
66     int xError;                 /*!< Socket error */
67     int xRcvErr;                /*!< Socket receive error */
68     const char* pcIpAddr;       /*!< TCP/UDP IP address */
69     UCHAR* pucRcvBuf;           /*!< Receive buffer pointer */
70     USHORT usRcvPos;            /*!< Receive buffer position */
71     int pcPort;                 /*!< TCP/UDP port number */
72     eMBPortProto xMbProto;      /*!< Protocol type */
73     int64_t xSendTimeStamp;     /*!< Send request time stamp */
74     int64_t xRecvTimeStamp;     /*!< Receive response time stamp */
75     uint16_t usTidCnt;          /*!< Transaction identifier (TID) for slave */
76 } MbSlaveInfo_t;
77 
78 typedef struct {
79     TaskHandle_t  xMbTcpTaskHandle; /*!< Master TCP/UDP handling task handle */
80     QueueHandle_t xConnectQueue;    /*!< Master connection queue */
81     USHORT usPort;                  /*!< Master TCP/UDP port number */
82     USHORT usMbSlaveInfoCount;      /*!< Master count of connected slaves */
83     USHORT ucCurSlaveIndex;         /*!< Master current processing slave index */
84     eMBPortIpVer eMbIpVer;          /*!< Master IP version */
85     eMBPortProto eMbProto;          /*!< Master protocol type */
86     void* pvNetIface;               /*!< Master netif interface pointer */
87     MbSlaveInfo_t** pxMbSlaveInfo;  /*!< Master information structure for each connected slave */
88 } MbPortConfig_t;
89 
90 /* ----------------------- Function prototypes ------------------------------*/
91 
92 // The functions below are used by Modbus controller interface to configure Modbus port.
93 /**
94  * Registers slave IP address
95  *
96  * @param pcIpStr IP address to register
97  *
98  * @return TRUE if address registered successfully, else FALSE
99  */
100 BOOL xMBTCPPortMasterAddSlaveIp(const CHAR* pcIpStr);
101 
102 /**
103  * Keeps FSM event handle and mask then wait for Master stack to start
104  *
105  * @param xEventHandle Master event handle
106  * @param xEvent event mask to start Modbus stack FSM
107  *
108  * @return TRUE if stack started, else FALSE
109  */
110 BOOL xMBTCPPortMasterWaitEvent(EventGroupHandle_t xEventHandle, EventBits_t xEvent);
111 
112 /**
113  * Set network options for Master port
114  *
115  * @param pvNetIf netif interface pointer
116  * @param xIpVersion IP version option for the Master port
117  * @param xProto Protocol version option for the Master port
118  *
119  * @return None
120  */
121 void vMBTCPPortMasterSetNetOpt(void* pvNetIf, eMBPortIpVer xIpVersion, eMBPortProto xProto);
122 
123 /**
124  * Resume TCP/UDP Master processing task
125  *
126  * @return None
127  */
128 void vMBTCPPortMasterTaskStart(void);
129 
130 #ifdef __cplusplus
131 PR_END_EXTERN_C
132 #endif
133 #endif
134