1 /*
2  *  Copyright (c) 2016, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "openthread-core-config.h"
29 
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 
34 #include <openthread-system.h>
35 #include <openthread/cli.h>
36 #include <openthread/logging.h>
37 
38 #include "cli/cli_config.h"
39 #include "common/code_utils.hpp"
40 #include "common/debug.hpp"
41 #include "utils/uart.h"
42 
43 #if OPENTHREAD_POSIX
44 #include <signal.h>
45 #include <sys/types.h>
46 #endif
47 
48 /**
49  * @def OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE
50  *
51  * The size of CLI UART RX buffer in bytes.
52  *
53  */
54 #ifndef OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE
55 #if OPENTHREAD_CONFIG_REFERENCE_DEVICE_ENABLE
56 #define OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE 640
57 #else
58 #define OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE 512
59 #endif
60 #endif
61 
62 /**
63  * @def OPENTHREAD_CONFIG_CLI_TX_BUFFER_SIZE
64  *
65  * The size of CLI message buffer in bytes.
66  *
67  */
68 #ifndef OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE
69 #define OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE 1024
70 #endif
71 
72 #if OPENTHREAD_CONFIG_DIAG_ENABLE
73 #if OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE > OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE
74 #error "diag output buffer should be smaller than CLI UART tx buffer"
75 #endif
76 #if OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE > OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE
77 #error "diag command line should be smaller than CLI UART rx buffer"
78 #endif
79 #endif
80 
81 #if OPENTHREAD_CONFIG_CLI_MAX_LINE_LENGTH > OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE
82 #error "command line should be should be smaller than CLI rx buffer"
83 #endif
84 
85 enum
86 {
87     kRxBufferSize = OPENTHREAD_CONFIG_CLI_UART_RX_BUFFER_SIZE,
88     kTxBufferSize = OPENTHREAD_CONFIG_CLI_UART_TX_BUFFER_SIZE,
89 };
90 
91 char     sRxBuffer[kRxBufferSize];
92 uint16_t sRxLength;
93 
94 char     sTxBuffer[kTxBufferSize];
95 uint16_t sTxHead;
96 uint16_t sTxLength;
97 
98 uint16_t sSendLength;
99 
100 #ifdef OT_CLI_UART_LOCK_HDR_FILE
101 
102 #include OT_CLI_UART_LOCK_HDR_FILE
103 
104 #else
105 
106 /**
107  * Macro to acquire an exclusive lock of uart cli output
108  * Default implementation does nothing
109  *
110  */
111 #ifndef OT_CLI_UART_OUTPUT_LOCK
112 #define OT_CLI_UART_OUTPUT_LOCK() \
113     do                            \
114     {                             \
115     } while (0)
116 #endif
117 
118 /**
119  * Macro to release the exclusive lock of uart cli output
120  * Default implementation does nothing
121  *
122  */
123 #ifndef OT_CLI_UART_OUTPUT_UNLOCK
124 #define OT_CLI_UART_OUTPUT_UNLOCK() \
125     do                              \
126     {                               \
127     } while (0)
128 #endif
129 
130 #endif // OT_CLI_UART_LOCK_HDR_FILE
131 
132 static int     Output(const char *aBuf, uint16_t aBufLength);
133 static otError ProcessCommand(void);
134 
ReceiveTask(const uint8_t * aBuf,uint16_t aBufLength)135 static void ReceiveTask(const uint8_t *aBuf, uint16_t aBufLength)
136 {
137     static const char sEraseString[] = {'\b', ' ', '\b'};
138     static const char CRNL[]         = {'\r', '\n'};
139     static uint8_t    sLastChar      = '\0';
140     const uint8_t    *end;
141 
142     end = aBuf + aBufLength;
143 
144     for (; aBuf < end; aBuf++)
145     {
146         switch (*aBuf)
147         {
148         case '\n':
149             if (sLastChar == '\r')
150             {
151                 break;
152             }
153 
154             OT_FALL_THROUGH;
155 
156         case '\r':
157             Output(CRNL, sizeof(CRNL));
158             sRxBuffer[sRxLength] = '\0';
159             IgnoreError(ProcessCommand());
160             break;
161 
162 #if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
163         case 0x03: // ASCII for Ctrl-C
164             kill(0, SIGINT);
165             break;
166 
167         case 0x04: // ASCII for Ctrl-D
168             exit(EXIT_SUCCESS);
169             break;
170 #endif
171 
172         case '\b':
173         case 127:
174             if (sRxLength > 0)
175             {
176                 Output(sEraseString, sizeof(sEraseString));
177                 sRxBuffer[--sRxLength] = '\0';
178             }
179 
180             break;
181 
182         default:
183             if (sRxLength < kRxBufferSize - 1)
184             {
185                 Output(reinterpret_cast<const char *>(aBuf), 1);
186                 sRxBuffer[sRxLength++] = static_cast<char>(*aBuf);
187             }
188 
189             break;
190         }
191 
192         sLastChar = *aBuf;
193     }
194 }
195 
ProcessCommand(void)196 static otError ProcessCommand(void)
197 {
198     otError error = OT_ERROR_NONE;
199 
200     while (sRxLength > 0 && (sRxBuffer[sRxLength - 1] == '\n' || sRxBuffer[sRxLength - 1] == '\r'))
201     {
202         sRxBuffer[--sRxLength] = '\0';
203     }
204 
205     otCliInputLine(sRxBuffer);
206     sRxLength = 0;
207 
208     return error;
209 }
210 
Send(void)211 static void Send(void)
212 {
213     VerifyOrExit(sSendLength == 0);
214 
215     if (sTxLength > kTxBufferSize - sTxHead)
216     {
217         sSendLength = kTxBufferSize - sTxHead;
218     }
219     else
220     {
221         sSendLength = sTxLength;
222     }
223 
224     if (sSendLength > 0)
225     {
226 #if OPENTHREAD_CONFIG_ENABLE_DEBUG_UART
227         /* duplicate the output to the debug uart */
228         otSysDebugUart_write_bytes(reinterpret_cast<uint8_t *>(sTxBuffer + sTxHead), sSendLength);
229 #endif
230         IgnoreError(otPlatUartSend(reinterpret_cast<uint8_t *>(sTxBuffer + sTxHead), sSendLength));
231     }
232 
233 exit:
234     return;
235 }
236 
SendDoneTask(void)237 static void SendDoneTask(void)
238 {
239     sTxHead = (sTxHead + sSendLength) % kTxBufferSize;
240     sTxLength -= sSendLength;
241     sSendLength = 0;
242 
243     Send();
244 }
245 
Output(const char * aBuf,uint16_t aBufLength)246 static int Output(const char *aBuf, uint16_t aBufLength)
247 {
248     OT_CLI_UART_OUTPUT_LOCK();
249     uint16_t sent = 0;
250 
251     while (aBufLength > 0)
252     {
253         uint16_t remaining = kTxBufferSize - sTxLength;
254         uint16_t tail;
255         uint16_t sendLength = aBufLength;
256 
257         if (sendLength > remaining)
258         {
259             sendLength = remaining;
260         }
261 
262         for (uint16_t i = 0; i < sendLength; i++)
263         {
264             tail            = (sTxHead + sTxLength) % kTxBufferSize;
265             sTxBuffer[tail] = *aBuf++;
266             aBufLength--;
267             sTxLength++;
268         }
269 
270         Send();
271 
272         sent += sendLength;
273 
274         if (aBufLength > 0)
275         {
276             // More to send, so flush what's waiting now
277             otError err = otPlatUartFlush();
278 
279             if (err == OT_ERROR_NONE)
280             {
281                 // Flush successful, reset the pointers
282                 SendDoneTask();
283             }
284             else
285             {
286                 // Flush did not succeed, so abort here.
287                 break;
288             }
289         }
290     }
291 
292     OT_CLI_UART_OUTPUT_UNLOCK();
293 
294     return sent;
295 }
296 
CliUartOutput(void * aContext,const char * aFormat,va_list aArguments)297 static int CliUartOutput(void *aContext, const char *aFormat, va_list aArguments)
298 {
299     OT_UNUSED_VARIABLE(aContext);
300 
301     int rval;
302 
303     if (sTxLength == 0)
304     {
305         rval = vsnprintf(sTxBuffer, kTxBufferSize, aFormat, aArguments);
306         VerifyOrExit(rval >= 0 && rval < kTxBufferSize, otLogWarnPlat("Failed to format CLI output `%s`", aFormat));
307         sTxHead     = 0;
308         sTxLength   = static_cast<uint16_t>(rval);
309         sSendLength = 0;
310     }
311     else
312     {
313         va_list  retryArguments;
314         uint16_t tail      = (sTxHead + sTxLength) % kTxBufferSize;
315         uint16_t remaining = (sTxHead > tail ? (sTxHead - tail) : (kTxBufferSize - tail));
316 
317         va_copy(retryArguments, aArguments);
318 
319         rval = vsnprintf(&sTxBuffer[tail], remaining, aFormat, aArguments);
320 
321         if (rval < 0)
322         {
323             otLogWarnPlat("Failed to format CLI output `%s`", aFormat);
324         }
325         else if (rval < remaining)
326         {
327             sTxLength += rval;
328         }
329         else if (rval < kTxBufferSize)
330         {
331             while (sTxLength != 0)
332             {
333                 otError error;
334 
335                 Send();
336 
337                 error = otPlatUartFlush();
338 
339                 if (error == OT_ERROR_NONE)
340                 {
341                     // Flush successful, reset the pointers
342                     SendDoneTask();
343                 }
344                 else
345                 {
346                     // Flush did not succeed, so abandon buffered output.
347                     otLogWarnPlat("Failed to output CLI: %s", otThreadErrorToString(error));
348                     break;
349                 }
350             }
351             rval = vsnprintf(sTxBuffer, kTxBufferSize, aFormat, retryArguments);
352             OT_ASSERT(rval > 0);
353             sTxLength   = static_cast<uint16_t>(rval);
354             sTxHead     = 0;
355             sSendLength = 0;
356         }
357         else
358         {
359             otLogWarnPlat("CLI output `%s` truncated", aFormat);
360         }
361 
362         va_end(retryArguments);
363     }
364 
365     Send();
366 
367 exit:
368     return rval;
369 }
370 
otPlatUartReceived(const uint8_t * aBuf,uint16_t aBufLength)371 void otPlatUartReceived(const uint8_t *aBuf, uint16_t aBufLength) { ReceiveTask(aBuf, aBufLength); }
372 
otPlatUartSendDone(void)373 void otPlatUartSendDone(void) { SendDoneTask(); }
374 
otAppCliInit(otInstance * aInstance)375 extern "C" void otAppCliInit(otInstance *aInstance)
376 {
377     sRxLength   = 0;
378     sTxHead     = 0;
379     sTxLength   = 0;
380     sSendLength = 0;
381 
382     IgnoreError(otPlatUartEnable());
383 
384     otCliInit(aInstance, CliUartOutput, aInstance);
385 }
386