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_m.h"
42 #include "mbframe.h"
43 #include "mbport.h"
44
45 #if MB_MASTER_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
eMBMasterTCPDoInit(USHORT ucTCPPort)75 eMBMasterTCPDoInit( USHORT ucTCPPort )
76 {
77 eMBErrorCode eStatus = MB_ENOERR;
78
79 if( xMBMasterTCPPortInit( ucTCPPort ) == FALSE )
80 {
81 eStatus = MB_EPORTERR;
82 }
83 return eStatus;
84 }
85
86 void
eMBMasterTCPStart(void)87 eMBMasterTCPStart( void )
88 {
89 ESP_LOGD(MB_PORT_TAG, "TCP Master port enable.");
90 vMBMasterTCPPortEnable( );
91 }
92
93 void
eMBMasterTCPStop(void)94 eMBMasterTCPStop( void )
95 {
96 ESP_LOGD(MB_PORT_TAG, "TCP Master port disable.");
97 vMBMasterTCPPortDisable( );
98 }
99
100 eMBErrorCode
eMBMasterTCPReceive(UCHAR * pucRcvAddress,UCHAR ** ppucFrame,USHORT * pusLength)101 eMBMasterTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength )
102 {
103 eMBErrorCode eStatus = MB_EIO;
104 UCHAR *pucMBTCPFrame;
105 USHORT usLength;
106 USHORT usPID;
107
108 if( xMBMasterTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE )
109 {
110 usPID = pucMBTCPFrame[MB_TCP_PID] << 8U;
111 usPID |= pucMBTCPFrame[MB_TCP_PID + 1];
112
113 if( usPID == MB_TCP_PROTOCOL_ID )
114 {
115 *ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC];
116 *pusLength = usLength - MB_TCP_FUNC;
117 eStatus = MB_ENOERR;
118
119 /* Modbus TCP does not use any addresses. Fake the source address such
120 * that the processing part deals with this frame.
121 */
122 *pucRcvAddress = MB_TCP_PSEUDO_ADDRESS;
123 }
124 }
125 else
126 {
127 eStatus = MB_EIO;
128 }
129 return eStatus;
130 }
131
132 eMBErrorCode
eMBMasterTCPSend(UCHAR _unused,const UCHAR * pucFrame,USHORT usLength)133 eMBMasterTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength )
134 {
135 eMBErrorCode eStatus = MB_ENOERR;
136 UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC;
137 USHORT usTCPLength = usLength + MB_TCP_FUNC;
138
139 /* The MBAP header is already initialized because the caller calls this
140 * function with the buffer returned by the previous call. Therefore we
141 * only have to update the length in the header. Note that the length
142 * header includes the size of the Modbus PDU and the UID Byte. Therefore
143 * the length is usLength plus one.
144 */
145 pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U;
146 pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF;
147 if( xMBMasterTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE )
148 {
149 eStatus = MB_EIO;
150 }
151 return eStatus;
152 }
153
154 #endif
155