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: mbfunccoils.c,v 1.8 2007/02/18 23:47:16 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_COILCNT_OFF        ( MB_PDU_DATA_OFF + 2 )
47 #define MB_PDU_FUNC_READ_SIZE               ( 4 )
48 #define MB_PDU_FUNC_READ_COILCNT_MAX        ( 0x07D0 )
49 
50 #define MB_PDU_FUNC_WRITE_ADDR_OFF          ( MB_PDU_DATA_OFF )
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 )
55 #define MB_PDU_FUNC_WRITE_MUL_COILCNT_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_COILCNT_MAX   ( 0x07B0 )
60 
61 /* ----------------------- Static functions ---------------------------------*/
62 eMBException    prveMBError2Exception( eMBErrorCode eErrorCode );
63 
64 /* ----------------------- Start implementation -----------------------------*/
65 #if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
66 
67 #if MB_FUNC_READ_COILS_ENABLED
68 
69 eMBException
eMBFuncReadCoils(UCHAR * pucFrame,USHORT * usLen)70 eMBFuncReadCoils( UCHAR * pucFrame, USHORT * usLen )
71 {
72     USHORT          usRegAddress;
73     USHORT          usCoilCount;
74     UCHAR           ucNBytes;
75     UCHAR          *pucFrameCur;
76 
77     eMBException    eStatus = MB_EX_NONE;
78     eMBErrorCode    eRegStatus;
79 
80     if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
81     {
82         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
83         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
84         usRegAddress++;
85 
86         usCoilCount = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF] << 8 );
87         usCoilCount |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_COILCNT_OFF + 1] );
88 
89         /* Check if the number of registers to read is valid. If not
90          * return Modbus illegal data value exception.
91          */
92         if( ( usCoilCount >= 1 ) &&
93             ( usCoilCount < MB_PDU_FUNC_READ_COILCNT_MAX ) )
94         {
95             /* Set the current PDU data pointer to the beginning. */
96             pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
97             *usLen = MB_PDU_FUNC_OFF;
98 
99             /* First byte contains the function code. */
100             *pucFrameCur++ = MB_FUNC_READ_COILS;
101             *usLen += 1;
102 
103             /* Test if the quantity of coils is a multiple of 8. If not last
104              * byte is only partially field with unused coils set to zero. */
105             if( ( usCoilCount & 0x0007 ) != 0 )
106             {
107                 ucNBytes = ( UCHAR )( usCoilCount / 8 + 1 );
108             }
109             else
110             {
111                 ucNBytes = ( UCHAR )( usCoilCount / 8 );
112             }
113             *pucFrameCur++ = ucNBytes;
114             *usLen += 1;
115 
116             eRegStatus =
117                 eMBRegCoilsCB( pucFrameCur, usRegAddress, usCoilCount,
118                                MB_REG_READ );
119 
120             /* If an error occured convert it into a Modbus exception. */
121             if( eRegStatus != MB_ENOERR )
122             {
123                 eStatus = prveMBError2Exception( eRegStatus );
124             }
125             else
126             {
127                 /* The response contains the function code, the starting address
128                  * and the quantity of registers. We reuse the old values in the
129                  * buffer because they are still valid. */
130                 *usLen += ucNBytes;;
131             }
132         }
133         else
134         {
135             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
136         }
137     }
138     else
139     {
140         /* Can't be a valid read coil register request because the length
141          * is incorrect. */
142         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
143     }
144     return eStatus;
145 }
146 
147 #if MB_FUNC_WRITE_COIL_ENABLED > 0
148 eMBException
eMBFuncWriteCoil(UCHAR * pucFrame,USHORT * usLen)149 eMBFuncWriteCoil( UCHAR * pucFrame, USHORT * usLen )
150 {
151     USHORT          usRegAddress;
152     UCHAR           ucBuf[2];
153 
154     eMBException    eStatus = MB_EX_NONE;
155     eMBErrorCode    eRegStatus;
156 
157     if( *usLen == ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
158     {
159         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF] << 8 );
160         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_ADDR_OFF + 1] );
161         usRegAddress++;
162 
163         if( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF + 1] == 0x00 ) &&
164             ( ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF ) ||
165               ( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0x00 ) ) )
166         {
167             ucBuf[1] = 0;
168             if( pucFrame[MB_PDU_FUNC_WRITE_VALUE_OFF] == 0xFF )
169             {
170                 ucBuf[0] = 1;
171             }
172             else
173             {
174                 ucBuf[0] = 0;
175             }
176             eRegStatus =
177                 eMBRegCoilsCB( &ucBuf[0], usRegAddress, 1, MB_REG_WRITE );
178 
179             /* If an error occured convert it into a Modbus exception. */
180             if( eRegStatus != MB_ENOERR )
181             {
182                 eStatus = prveMBError2Exception( eRegStatus );
183             }
184         }
185         else
186         {
187             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
188         }
189     }
190     else
191     {
192         /* Can't be a valid write coil register request because the length
193          * is incorrect. */
194         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
195     }
196     return eStatus;
197 }
198 
199 #endif
200 
201 #if MB_FUNC_WRITE_MULTIPLE_COILS_ENABLED > 0
202 eMBException
eMBFuncWriteMultipleCoils(UCHAR * pucFrame,USHORT * usLen)203 eMBFuncWriteMultipleCoils( UCHAR * pucFrame, USHORT * usLen )
204 {
205     USHORT          usRegAddress;
206     USHORT          usCoilCnt;
207     UCHAR           ucByteCount;
208     UCHAR           ucByteCountVerify;
209 
210     eMBException    eStatus = MB_EX_NONE;
211     eMBErrorCode    eRegStatus;
212 
213     if( *usLen > ( MB_PDU_FUNC_WRITE_SIZE + MB_PDU_SIZE_MIN ) )
214     {
215         usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF] << 8 );
216         usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_ADDR_OFF + 1] );
217         usRegAddress++;
218 
219         usCoilCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF] << 8 );
220         usCoilCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_WRITE_MUL_COILCNT_OFF + 1] );
221 
222         ucByteCount = pucFrame[MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF];
223 
224         /* Compute the number of expected bytes in the request. */
225         if( ( usCoilCnt & 0x0007 ) != 0 )
226         {
227             ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 + 1 );
228         }
229         else
230         {
231             ucByteCountVerify = ( UCHAR )( usCoilCnt / 8 );
232         }
233 
234         if( ( usCoilCnt >= 1 ) &&
235             ( usCoilCnt <= MB_PDU_FUNC_WRITE_MUL_COILCNT_MAX ) &&
236             ( ucByteCountVerify == ucByteCount ) )
237         {
238             eRegStatus =
239                 eMBRegCoilsCB( &pucFrame[MB_PDU_FUNC_WRITE_MUL_VALUES_OFF],
240                                usRegAddress, usCoilCnt, MB_REG_WRITE );
241 
242             /* If an error occured convert it into a Modbus exception. */
243             if( eRegStatus != MB_ENOERR )
244             {
245                 eStatus = prveMBError2Exception( eRegStatus );
246             }
247             else
248             {
249                 /* The response contains the function code, the starting address
250                  * and the quantity of registers. We reuse the old values in the
251                  * buffer because they are still valid. */
252                 *usLen = MB_PDU_FUNC_WRITE_MUL_BYTECNT_OFF;
253             }
254         }
255         else
256         {
257             eStatus = MB_EX_ILLEGAL_DATA_VALUE;
258         }
259     }
260     else
261     {
262         /* Can't be a valid write coil register request because the length
263          * is incorrect. */
264         eStatus = MB_EX_ILLEGAL_DATA_VALUE;
265     }
266     return eStatus;
267 }
268 
269 #endif
270 
271 #endif
272 
273 #endif
274