1 /*
2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU.
3 * Copyright (c) 2006 Christian Walter <wolti@sil.at>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * File: $Id: mbtcp.c,v 1.3 2006/12/07 22:10:34 wolti Exp $
29 */
30
31 /* ----------------------- System includes ----------------------------------*/
32 #include "stdlib.h"
33 #include "string.h"
34
35 /* ----------------------- Platform includes --------------------------------*/
36 #include "port.h"
37
38 /* ----------------------- Modbus includes ----------------------------------*/
39 #include "mb.h"
40 #include "mbconfig.h"
41 #include "mbtcp.h"
42 #include "mbframe.h"
43 #include "mbport.h"
44
45 #if MB_TCP_ENABLED
46
47 /* ----------------------- Defines ------------------------------------------*/
48
49 /* ----------------------- MBAP Header --------------------------------------*/
50 /*
51 *
52 * <------------------------ MODBUS TCP/IP ADU(1) ------------------------->
53 * <----------- MODBUS PDU (1') ---------------->
54 * +-----------+---------------+------------------------------------------+
55 * | TID | PID | Length | UID |Code | Data |
56 * +-----------+---------------+------------------------------------------+
57 * | | | | |
58 * (2) (3) (4) (5) (6)
59 *
60 * (2) ... MB_TCP_TID = 0 (Transaction Identifier - 2 Byte)
61 * (3) ... MB_TCP_PID = 2 (Protocol Identifier - 2 Byte)
62 * (4) ... MB_TCP_LEN = 4 (Number of bytes - 2 Byte)
63 * (5) ... MB_TCP_UID = 6 (Unit Identifier - 1 Byte)
64 * (6) ... MB_TCP_FUNC = 7 (Modbus Function Code)
65 *
66 * (1) ... Modbus TCP/IP Application Data Unit
67 * (1') ... Modbus Protocol Data Unit
68 */
69
70 #define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */
71
72
73 /* ----------------------- Start implementation -----------------------------*/
74 eMBErrorCode
eMBTCPDoInit(USHORT ucTCPPort)75 eMBTCPDoInit( USHORT ucTCPPort )
76 {
77 eMBErrorCode eStatus = MB_ENOERR;
78
79 if( xMBTCPPortInit( ucTCPPort ) == FALSE )
80 {
81 eStatus = MB_EPORTERR;
82 }
83 return eStatus;
84 }
85
86 void
eMBTCPStart(void)87 eMBTCPStart( void )
88 {
89 ESP_LOGD(MB_PORT_TAG, "TCP Slave port enable.");
90 vMBTCPPortEnable( );
91 }
92
93 void
eMBTCPStop(void)94 eMBTCPStop( void )
95 {
96 /* Make sure that no more clients are connected. */
97 ESP_LOGD(MB_PORT_TAG, "TCP Slave port disable.");
98 vMBTCPPortDisable( );
99 }
100
101 eMBErrorCode
eMBTCPReceive(UCHAR * pucRcvAddress,UCHAR ** ppucFrame,USHORT * pusLength)102 eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
103 {
104 eMBErrorCode eStatus = MB_EIO;
105 UCHAR *pucMBTCPFrame;
106 USHORT usLength;
107 USHORT usPID;
108
109 if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
110 {
111 usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
112 usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
113
114 if( usPID == MB_TCP_PROTOCOL_ID )
115 {
116 *ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
117 *pusLength = usLength - MB_TCP_FUNC;
118 eStatus = MB_ENOERR;
119
120 /* Modbus TCP does not use any addresses. Fake the source address such
121 * that the processing part deals with this frame.
122 */
123 *pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
124 }
125 }
126 else
127 {
128 eStatus = MB_EIO;
129 }
130 return eStatus;
131 }
132
133 eMBErrorCode
eMBTCPSend(UCHAR _unused,const UCHAR * pucFrame,USHORT usLength)134 eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
135 {
136 eMBErrorCode eStatus = MB_ENOERR;
137 UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
138 USHORT usTCPLength = usLength + MB_TCP_FUNC;
139
140 /* The MBAP header is already initialized because the caller calls this
141 * function with the buffer returned by the previous call. Therefore we
142 * only have to update the length in the header. Note that the length
143 * header includes the size of the Modbus PDU and the UID Byte. Therefore
144 * the length is usLength plus one.
145 */
146 pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
147 pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
148 if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
149 {
150 eStatus = MB_EIO;
151 }
152 return eStatus;
153 }
154
155 #endif
156