1 /*
2  * SPDX-FileCopyrightText: 2013 Armink
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * SPDX-FileContributor: 2016-2021 Espressif Systems (Shanghai) CO LTD
7  */
8 /*
9  * FreeModbus Libary: ESP32 Port
10  * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
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: port.c,v 1.60 2015/02/01 9:18:05 Armink $
35  */
36 
37 /* ----------------------- System includes --------------------------------*/
38 
39 /* ----------------------- Modbus includes ----------------------------------*/
40 #include "freertos/FreeRTOS.h"
41 #include "sys/lock.h"
42 #include "port.h"
43 
44 /* ----------------------- Variables ----------------------------------------*/
45 static _lock_t s_port_lock;
46 static UCHAR ucPortMode = 0;
47 
48 /* ----------------------- Start implementation -----------------------------*/
49 inline void
vMBPortEnterCritical(void)50 vMBPortEnterCritical(void)
51 {
52     _lock_acquire(&s_port_lock);
53 }
54 
55 inline void
vMBPortExitCritical(void)56 vMBPortExitCritical(void)
57 {
58     _lock_release(&s_port_lock);
59 }
60 
61 UCHAR
ucMBPortGetMode(void)62 ucMBPortGetMode( void )
63 {
64     return ucPortMode;
65 }
66 
67 void
vMBPortSetMode(UCHAR ucMode)68 vMBPortSetMode( UCHAR ucMode )
69 {
70     ENTER_CRITICAL_SECTION();
71     ucPortMode = ucMode;
72     EXIT_CRITICAL_SECTION();
73 }
74 
75 #if MB_TCP_DEBUG
76 
77 // This function is kept to realize legacy freemodbus frame logging functionality
78 void
prvvMBTCPLogFrame(const CHAR * pucMsg,UCHAR * pucFrame,USHORT usFrameLen)79 prvvMBTCPLogFrame( const CHAR * pucMsg, UCHAR * pucFrame, USHORT usFrameLen )
80 {
81     int             i;
82     int             res = 0;
83     int             iBufPos = 0;
84     size_t          iBufLeft = MB_TCP_FRAME_LOG_BUFSIZE;
85     static CHAR     arcBuffer[MB_TCP_FRAME_LOG_BUFSIZE];
86 
87     assert( pucFrame != NULL );
88 
89     for ( i = 0; i < usFrameLen; i++ ) {
90         // Print some additional frame information.
91         switch ( i )
92         {
93         case 0:
94             // TID = Transaction Identifier.
95             res = snprintf( &arcBuffer[iBufPos], iBufLeft, "| TID = " );
96             break;
97         case 2:
98             // PID = Protocol Identifier.
99             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | PID = " );
100             break;
101         case 4:
102             // Length
103             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | LEN = " );
104             break;
105         case 6:
106             // UID = Unit Identifier.
107             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | UID = " );
108             break;
109         case 7:
110             // MB Function Code.
111             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | FUNC = " );
112             break;
113         case 8:
114             // MB PDU rest.
115             res = snprintf( &arcBuffer[iBufPos], iBufLeft, " | DATA = " );
116             break;
117         default:
118             res = 0;
119             break;
120         }
121         if( res == -1 ) {
122             break;
123         }
124         else {
125             iBufPos += res;
126             iBufLeft -= res;
127         }
128 
129         // Print the data.
130         res = snprintf( &arcBuffer[iBufPos], iBufLeft, "%02X", pucFrame[i] );
131         if( res == -1 ) {
132             break;
133         } else {
134             iBufPos += res;
135             iBufLeft -= res;
136         }
137     }
138 
139     if( res != -1 ) {
140         // Append an end of frame string.
141         res = snprintf( &arcBuffer[iBufPos], iBufLeft, " |" );
142         if( res != -1 ) {
143             ESP_LOGD(pucMsg, "%s", arcBuffer);
144         }
145     }
146 }
147 #endif
148