1 /* 2 * FreeModbus Libary: A portable Modbus implementation for Modbus ASCII/RTU. 3 * Copyright (c) 2006 Christian Walter <[email protected]> 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_SLAVE_TCP_ENABLED > 0 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_TID 0 71 #define MB_TCP_PID 2 72 #define MB_TCP_LEN 4 73 #define MB_TCP_UID 6 74 #define MB_TCP_FUNC 7 75 76 #define MB_TCP_PROTOCOL_ID 0 /* 0 = Modbus Protocol */ 77 78 79 /* ----------------------- Start implementation -----------------------------*/ 80 eMBErrorCode 81 eMBTCPDoInit( USHORT ucTCPPort ) 82 { 83 eMBErrorCode eStatus = MB_ENOERR; 84 85 if( xMBTCPPortInit( ucTCPPort ) == FALSE ) 86 { 87 eStatus = MB_EPORTERR; 88 } 89 return eStatus; 90 } 91 92 void 93 eMBTCPStart( void ) 94 { 95 } 96 97 void 98 eMBTCPStop( void ) 99 { 100 /* Make sure that no more clients are connected. */ 101 vMBTCPPortDisable( ); 102 } 103 104 eMBErrorCode 105 eMBTCPReceive( UCHAR * pucRcvAddress, UCHAR ** ppucFrame, USHORT * pusLength ) 106 { 107 eMBErrorCode eStatus = MB_EIO; 108 UCHAR *pucMBTCPFrame; 109 USHORT usLength; 110 USHORT usPID; 111 112 if( xMBTCPPortGetRequest( &pucMBTCPFrame, &usLength ) != FALSE ) 113 { 114 usPID = pucMBTCPFrame[MB_TCP_PID] << 8U; 115 usPID |= pucMBTCPFrame[MB_TCP_PID + 1]; 116 117 if( usPID == MB_TCP_PROTOCOL_ID ) 118 { 119 *ppucFrame = &pucMBTCPFrame[MB_TCP_FUNC]; 120 *pusLength = usLength - MB_TCP_FUNC; 121 eStatus = MB_ENOERR; 122 123 /* Modbus TCP does not use any addresses. Fake the source address such 124 * that the processing part deals with this frame. 125 */ 126 *pucRcvAddress = MB_TCP_PSEUDO_ADDRESS; 127 } 128 } 129 else 130 { 131 eStatus = MB_EIO; 132 } 133 return eStatus; 134 } 135 136 eMBErrorCode 137 eMBTCPSend( UCHAR _unused, const UCHAR * pucFrame, USHORT usLength ) 138 { 139 eMBErrorCode eStatus = MB_ENOERR; 140 UCHAR *pucMBTCPFrame = ( UCHAR * ) pucFrame - MB_TCP_FUNC; 141 USHORT usTCPLength = usLength + MB_TCP_FUNC; 142 143 /* The MBAP header is already initialized because the caller calls this 144 * function with the buffer returned by the previous call. Therefore we 145 * only have to update the length in the header. Note that the length 146 * header includes the size of the Modbus PDU and the UID Byte. Therefore 147 * the length is usLength plus one. 148 */ 149 pucMBTCPFrame[MB_TCP_LEN] = ( usLength + 1 ) >> 8U; 150 pucMBTCPFrame[MB_TCP_LEN + 1] = ( usLength + 1 ) & 0xFF; 151 if( xMBTCPPortSendResponse( pucMBTCPFrame, usTCPLength ) == FALSE ) 152 { 153 eStatus = MB_EIO; 154 } 155 return eStatus; 156 } 157 158 #endif 159