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: mbfuncinput.c,v 1.10 2007/09/12 10:15:56 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 "mbframe.h"
41 #include "mbproto.h"
42 #include "mbconfig.h"
43
44 /* ----------------------- Defines ------------------------------------------*/
45 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
46 #define MB_PDU_FUNC_READ_REGCNT_OFF ( MB_PDU_DATA_OFF + 2 )
47 #define MB_PDU_FUNC_READ_SIZE ( 4 )
48 #define MB_PDU_FUNC_READ_REGCNT_MAX ( 0x007D )
49
50 #define MB_PDU_FUNC_READ_RSP_BYTECNT_OFF ( MB_PDU_DATA_OFF )
51
52 /* ----------------------- Static functions ---------------------------------*/
53 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
54
55 /* ----------------------- Start implementation -----------------------------*/
56 #if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
57
58 #if MB_FUNC_READ_INPUT_ENABLED
59
60 eMBException
eMBFuncReadInputRegister(UCHAR * pucFrame,USHORT * usLen)61 eMBFuncReadInputRegister( UCHAR * pucFrame, USHORT * usLen )
62 {
63 USHORT usRegAddress;
64 USHORT usRegCount;
65 UCHAR *pucFrameCur;
66
67 eMBException eStatus = MB_EX_NONE;
68 eMBErrorCode eRegStatus;
69
70 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
71 {
72 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
73 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
74 usRegAddress++;
75
76 usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
77 usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
78
79 /* Check if the number of registers to read is valid. If not
80 * return Modbus illegal data value exception.
81 */
82 if( ( usRegCount >= 1 )
83 && ( usRegCount < MB_PDU_FUNC_READ_REGCNT_MAX ) )
84 {
85 /* Set the current PDU data pointer to the beginning. */
86 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
87 *usLen = MB_PDU_FUNC_OFF;
88
89 /* First byte contains the function code. */
90 *pucFrameCur++ = MB_FUNC_READ_INPUT_REGISTER;
91 *usLen += 1;
92
93 /* Second byte in the response contain the number of bytes. */
94 *pucFrameCur++ = ( UCHAR )( usRegCount * 2 );
95 *usLen += 1;
96
97 eRegStatus =
98 eMBRegInputCB( pucFrameCur, usRegAddress, usRegCount );
99
100 /* If an error occured convert it into a Modbus exception. */
101 if( eRegStatus != MB_ENOERR )
102 {
103 eStatus = prveMBError2Exception( eRegStatus );
104 }
105 else
106 {
107 *usLen += usRegCount * 2;
108 }
109 }
110 else
111 {
112 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
113 }
114 }
115 else
116 {
117 /* Can't be a valid read input register request because the length
118 * is incorrect. */
119 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
120 }
121 return eStatus;
122 }
123
124 #endif
125
126 #endif
127