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: mbfuncholding.c,v 1.12 2007/02/18 23:48:22 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 + 0)
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_WRITE_ADDR_OFF              ( MB_PDU_DATA_OFF + 0)
51 #define MB_PDU_FUNC_WRITE_VALUE_OFF             ( MB_PDU_DATA_OFF + 2 )
52 #define MB_PDU_FUNC_WRITE_SIZE                  ( 4 )
53 
54 #define MB_PDU_FUNC_WRITE_MUL_ADDR_OFF          ( MB_PDU_DATA_OFF + 0 )
55 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF        ( MB_PDU_DATA_OFF + 2 )
56 #define MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF       ( MB_PDU_DATA_OFF + 4 )
57 #define MB_PDU_FUNC_WRITE_MUL_VALUES_OFF        ( MB_PDU_DATA_OFF + 5 )
58 #define MB_PDU_FUNC_WRITE_MUL_SIZE_MIN          ( 5 )
59 #define MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX        ( 0x0078 )
60 
61 #define MB_PDU_FUNC_READWRITE_READ_ADDR_OFF     ( MB_PDU_DATA_OFF + 0 )
62 #define MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF   ( MB_PDU_DATA_OFF + 2 )
63 #define MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF    ( MB_PDU_DATA_OFF + 4 )
64 #define MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF  ( MB_PDU_DATA_OFF + 6 )
65 #define MB_PDU_FUNC_READWRITE_BYTECNT_OFF       ( MB_PDU_DATA_OFF + 8 )
66 #define MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF  ( MB_PDU_DATA_OFF + 9 )
67 #define MB_PDU_FUNC_READWRITE_SIZE_MIN          ( 9 )
68 
69 /* ----------------------- Static functions ---------------------------------*/
70 eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
71 
72 /* ----------------------- Start implementation -----------------------------*/
73 #if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
74 
75 #if MB_FUNC_WRITE_HOLDING_ENABLED
76 
77 eMBException
eMBFuncWriteHoldingRegister(UCHAR * pucFrame,USHORT * usLen)78 eMBFuncWriteHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
79 {
80     USHORT          usRegAddress;
81     eMBException    eStatus = MB_EX_NONE;
82     eMBErrorCode    eRegStatus;
83 
84     if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
85     {
86         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
87         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
88         usRegAddress++;
89 
90         /* Make callback to update the value. */
91         eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF],
92                                       usRegAddress, 1, MB_REG_WRITE );
93 
94         /* If an error occured convert it into a Modbus exception. */
95         if( eRegStatus != MB_ENOERR )
96         {
97             eStatus = prveMBError2Exception( eRegStatus );
98         }
99     }
100     else
101     {
102         /* Can't be a valid request because the length is incorrect. */
103         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
104     }
105     return eStatus;
106 }
107 #endif
108 
109 #if MB_FUNC_WRITE_MULTIPLE_HOLDING_ENABLED > 0
110 eMBException
eMBFuncWriteMultipleHoldingRegister(UCHAR * pucFrame,USHORT * usLen)111 eMBFuncWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
112 {
113     USHORT          usRegAddress;
114     USHORT          usRegCount;
115     UCHAR           ucRegByteCount;
116 
117     eMBException    eStatus = MB_EX_NONE;
118     eMBErrorCode    eRegStatus;
119 
120     if( *usLen >= ( MB_PDU_FUNC_WRITE_MUL_SIZE_MIN + MB_PDU_SIZE_MIN ) )
121     {
122         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
123         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
124         usRegAddress++;
125 
126         usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF] << 8 );
127         usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_REGCNT_OFF + 1] );
128 
129         ucRegByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
130 
131         if( ( usRegCount >= 1 ) &&
132             ( usRegCount <= MB_PDU_FUNC_WRITE_MUL_REGCNT_MAX ) &&
133             ( ucRegByteCount == ( UCHAR ) ( 2 * usRegCount ) ) )
134         {
135             /* Make callback to update the register values. */
136             eRegStatus =
137                 eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
138                                  usRegAddress, usRegCount, MB_REG_WRITE );
139 
140             /* If an error occured convert it into a Modbus exception. */
141             if( eRegStatus != MB_ENOERR )
142             {
143                 eStatus = prveMBError2Exception( eRegStatus );
144             }
145             else
146             {
147                 /* The response contains the function code, the starting
148                  * address and the quantity of registers. We reuse the
149                  * old values in the buffer because they are still valid.
150                  */
151                 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
152             }
153         }
154         else
155         {
156             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
157         }
158     }
159     else
160     {
161         /* Can't be a valid request because the length is incorrect. */
162         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
163     }
164     return eStatus;
165 }
166 #endif
167 
168 #if MB_FUNC_READ_HOLDING_ENABLED > 0
169 
170 eMBException
eMBFuncReadHoldingRegister(UCHAR * pucFrame,USHORT * usLen)171 eMBFuncReadHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
172 {
173     USHORT          usRegAddress;
174     USHORT          usRegCount;
175     UCHAR          *pucFrameCur;
176 
177     eMBException    eStatus = MB_EX_NONE;
178     eMBErrorCode    eRegStatus;
179 
180     if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
181     {
182         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
183         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
184         usRegAddress++;
185 
186         usRegCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF] << 8 );
187         usRegCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_REGCNT_OFF + 1] );
188 
189         /* Check if the number of registers to read is valid. If not
190          * return Modbus illegal data value exception.
191          */
192         if( ( usRegCount >= 1 ) && ( usRegCount <= MB_PDU_FUNC_READ_REGCNT_MAX ) )
193         {
194             /* Set the current PDU data pointer to the beginning. */
195             pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
196             *usLen = MB_PDU_FUNC_OFF;
197 
198             /* First byte contains the function code. */
199             *pucFrameCur++ = MB_FUNC_READ_HOLDING_REGISTER;
200             *usLen += 1;
201 
202             /* Second byte in the response contain the number of bytes. */
203             *pucFrameCur++ = ( UCHAR ) ( usRegCount * 2 );
204             *usLen += 1;
205 
206             /* Make callback to fill the buffer. */
207             eRegStatus = eMBRegHoldingCB( pucFrameCur, usRegAddress, usRegCount, MB_REG_READ );
208             /* If an error occured convert it into a Modbus exception. */
209             if( eRegStatus != MB_ENOERR )
210             {
211                 eStatus = prveMBError2Exception( eRegStatus );
212             }
213             else
214             {
215                 *usLen += usRegCount * 2;
216             }
217         }
218         else
219         {
220             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
221         }
222     }
223     else
224     {
225         /* Can't be a valid request because the length is incorrect. */
226         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
227     }
228     return eStatus;
229 }
230 
231 #endif
232 
233 #if MB_FUNC_READWRITE_HOLDING_ENABLED > 0
234 
235 eMBException
eMBFuncReadWriteMultipleHoldingRegister(UCHAR * pucFrame,USHORT * usLen)236 eMBFuncReadWriteMultipleHoldingRegister( UCHAR * pucFrame, USHORT * usLen )
237 {
238     USHORT          usRegReadAddress;
239     USHORT          usRegReadCount;
240     USHORT          usRegWriteAddress;
241     USHORT          usRegWriteCount;
242     UCHAR           ucRegWriteByteCount;
243     UCHAR          *pucFrameCur;
244 
245     eMBException    eStatus = MB_EX_NONE;
246     eMBErrorCode    eRegStatus;
247 
248     if( *usLen >= ( MB_PDU_FUNC_READWRITE_SIZE_MIN + MB_PDU_SIZE_MIN ) )
249     {
250         usRegReadAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF] << 8U );
251         usRegReadAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_ADDR_OFF + 1] );
252         usRegReadAddress++;
253 
254         usRegReadCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF] << 8U );
255         usRegReadCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_READ_REGCNT_OFF + 1] );
256 
257         usRegWriteAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF] << 8U );
258         usRegWriteAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_ADDR_OFF + 1] );
259         usRegWriteAddress++;
260 
261         usRegWriteCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF] << 8U );
262         usRegWriteCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READWRITE_WRITE_REGCNT_OFF + 1] );
263 
264         ucRegWriteByteCount = pucFrame[MB_PDU_FUNC_READWRITE_BYTECNT_OFF];
265 
266         if( ( usRegReadCount >= 1 ) && ( usRegReadCount <= 0x7D ) &&
267             ( usRegWriteCount >= 1 ) && ( usRegWriteCount <= 0x79 ) &&
268             ( ( 2 * usRegWriteCount ) == ucRegWriteByteCount ) )
269         {
270             /* Make callback to update the register values. */
271             eRegStatus = eMBRegHoldingCB( &pucFrame[MB_PDU_FUNC_READWRITE_WRITE_VALUES_OFF],
272                                           usRegWriteAddress, usRegWriteCount, MB_REG_WRITE );
273 
274             if( eRegStatus == MB_ENOERR )
275             {
276                 /* Set the current PDU data pointer to the beginning. */
277                 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
278                 *usLen = MB_PDU_FUNC_OFF;
279 
280                 /* First byte contains the function code. */
281                 *pucFrameCur++ = MB_FUNC_READWRITE_MULTIPLE_REGISTERS;
282                 *usLen += 1;
283 
284                 /* Second byte in the response contain the number of bytes. */
285                 *pucFrameCur++ = ( UCHAR ) ( usRegReadCount * 2 );
286                 *usLen += 1;
287 
288                 /* Make the read callback. */
289                 eRegStatus =
290                     eMBRegHoldingCB( pucFrameCur, usRegReadAddress, usRegReadCount, MB_REG_READ );
291                 if( eRegStatus == MB_ENOERR )
292                 {
293                     *usLen += 2 * usRegReadCount;
294                 }
295             }
296             if( eRegStatus != MB_ENOERR )
297             {
298                 eStatus = prveMBError2Exception( eRegStatus );
299             }
300         }
301         else
302         {
303             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
304         }
305     }
306     return eStatus;
307 }
308 
309 #endif
310 
311 #endif
312