1 /*
2 * SPDX-FileCopyrightText: 2006 Christian Walter
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
7 */
8 /*
9 * FreeRTOS Modbus Libary: A Modbus serial implementation for FreeRTOS
10 * Copyright (C) 2006 Christian Walter <wolti@sil.at>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * IF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 * File: $Id: mbfuncdisc.c,v 1.10 2007/09/12 10:15:56 wolti Exp $
35 */
36
37 /* ----------------------- System includes ----------------------------------*/
38 #include "stdlib.h"
39 #include "string.h"
40
41 /* ----------------------- Platform includes --------------------------------*/
42 #include "port.h"
43
44 /* ----------------------- Modbus includes ----------------------------------*/
45 #include "mb.h"
46 #include "mbframe.h"
47 #include "mbproto.h"
48 #include "mbconfig.h"
49
50 /* ----------------------- Defines ------------------------------------------*/
51 #define MB_PDU_FUNC_READ_ADDR_OFF ( MB_PDU_DATA_OFF )
52 #define MB_PDU_FUNC_READ_DISCCNT_OFF ( MB_PDU_DATA_OFF + 2 )
53 #define MB_PDU_FUNC_READ_SIZE ( 4 )
54 #define MB_PDU_FUNC_READ_DISCCNT_MAX ( 0x07D0 )
55
56 /* ----------------------- Static functions ---------------------------------*/
57 eMBException prveMBError2Exception( eMBErrorCode eErrorCode );
58
59 /* ----------------------- Start implementation -----------------------------*/
60 #if MB_SLAVE_RTU_ENABLED || MB_SLAVE_ASCII_ENABLED || MB_TCP_ENABLED
61
62 #if MB_FUNC_READ_COILS_ENABLED
63
64 eMBException
eMBFuncReadDiscreteInputs(UCHAR * pucFrame,USHORT * usLen)65 eMBFuncReadDiscreteInputs( UCHAR * pucFrame, USHORT * usLen )
66 {
67 USHORT usRegAddress;
68 USHORT usDiscreteCnt;
69 UCHAR ucNBytes;
70 UCHAR *pucFrameCur;
71
72 eMBException eStatus = MB_EX_NONE;
73 eMBErrorCode eRegStatus;
74
75 if( *usLen == ( MB_PDU_FUNC_READ_SIZE + MB_PDU_SIZE_MIN ) )
76 {
77 usRegAddress = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF] << 8 );
78 usRegAddress |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_ADDR_OFF + 1] );
79 usRegAddress++;
80
81 usDiscreteCnt = ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF] << 8 );
82 usDiscreteCnt |= ( USHORT )( pucFrame[MB_PDU_FUNC_READ_DISCCNT_OFF + 1] );
83
84 /* Check if the number of registers to read is valid. If not
85 * return Modbus illegal data value exception.
86 */
87 if( ( usDiscreteCnt >= 1 ) &&
88 ( usDiscreteCnt < MB_PDU_FUNC_READ_DISCCNT_MAX ) )
89 {
90 /* Set the current PDU data pointer to the beginning. */
91 pucFrameCur = &pucFrame[MB_PDU_FUNC_OFF];
92 *usLen = MB_PDU_FUNC_OFF;
93
94 /* First byte contains the function code. */
95 *pucFrameCur++ = MB_FUNC_READ_DISCRETE_INPUTS;
96 *usLen += 1;
97
98 /* Test if the quantity of coils is a multiple of 8. If not last
99 * byte is only partially field with unused coils set to zero. */
100 if( ( usDiscreteCnt & 0x0007 ) != 0 )
101 {
102 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 + 1 );
103 }
104 else
105 {
106 ucNBytes = ( UCHAR ) ( usDiscreteCnt / 8 );
107 }
108 *pucFrameCur++ = ucNBytes;
109 *usLen += 1;
110
111 eRegStatus =
112 eMBRegDiscreteCB( pucFrameCur, usRegAddress, usDiscreteCnt );
113
114 /* If an error occured convert it into a Modbus exception. */
115 if( eRegStatus != MB_ENOERR )
116 {
117 eStatus = prveMBError2Exception( eRegStatus );
118 }
119 else
120 {
121 /* The response contains the function code, the starting address
122 * and the quantity of registers. We reuse the old values in the
123 * buffer because they are still valid. */
124 *usLen += ucNBytes;;
125 }
126 }
127 else
128 {
129 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
130 }
131 }
132 else
133 {
134 /* Can't be a valid read coil register request because the length
135 * is incorrect. */
136 eStatus = MB_EX_ILLEGAL_DATA_VALUE;
137 }
138 return eStatus;
139 }
140
141 #endif
142
143 #endif
144