1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2021 NXP
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: BSD-3-Clause
7 *
8 * POSIX getopt for Windows
9 * Code given out at the 1985 UNIFORUM conference in Dallas.
10 *
11 * From std-unix@ut-sally.UUCP (Moderator, John Quarterman) Sun Nov 3 14:34:15 1985
12 * Relay-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site gatech.CSNET
13 * Posting-Version: version B 2.10.2 9/18/84; site ut-sally.UUCP
14 * Path: gatech!akgua!mhuxv!mhuxt!mhuxr!ulysses!allegra!mit-eddie!genrad!panda!talcott!harvard!seismo!ut-sally!std-unix
15 * From: std-unix@ut-sally.UUCP (Moderator, John Quarterman)
16 * Newsgroups: mod.std.unix
17 * Subject: public domain AT&T getopt source
18 * Message-ID: <3352@ut-sally.UUCP>
19 * Date: 3 Nov 85 19:34:15 GMT
20 * Date-Received: 4 Nov 85 12:25:09 GMT
21 * Organization: IEEE/P1003 Portable Operating System Environment Committee
22 * Lines: 91
23 * Approved: jsq@ut-sally.UUC
24 * Here's something you've all been waiting for: the AT&T public domain
25 * source for getopt(3). It is the code which was given out at the 1985
26 * UNIFORUM conference in Dallas. I obtained it by electronic mail
27 * directly from AT&T. The people there assure me that it is indeed
28 * in the public domain
29 * There is no manual page. That is because the one they gave out at
30 * UNIFORUM was slightly different from the current System V Release 2
31 * manual page. The difference apparently involved a note about the
32 * famous rules 5 and 6, recommending using white space between an option
33 * and its first argument, and not grouping options that have arguments.
34 * Getopt itself is currently lenient about both of these things White
35 * space is allowed, but not mandatory, and the last option in a group can
36 * have an argument. That particular version of the man page evidently
37 * has no official existence, and my source at AT&T did not send a copy.
38 * The current SVR2 man page reflects the actual behavor of this getopt.
39 * However, I am not about to post a copy of anything licensed by AT&T.
40 */
41
42 #include <assert.h>
43 #include <stdarg.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46
47 #include "fsl_common.h"
48 #include "fsl_str.h"
49
50 #include "fsl_component_generic_list.h"
51 #include "fsl_component_serial_manager.h"
52
53 #include "fsl_shell.h"
54
55 /*
56 * The OSA_USED macro can only be defined when the OSA component is used.
57 * If the source code of the OSA component does not exist, the OSA_USED cannot be defined.
58 * OR, If OSA component is not added into project event the OSA source code exists, the OSA_USED
59 * also cannot be defined.
60 * The source code path of the OSA component is <MCUXpresso_SDK>/components/osa.
61 *
62 */
63 #if defined(OSA_USED)
64
65 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
66 #include "fsl_component_common_task.h"
67 #else
68 #include "fsl_os_abstraction.h"
69 #endif
70
71 #endif
72
73 /*******************************************************************************
74 * Definitions
75 ******************************************************************************/
76 #define KEY_ESC (0x1BU)
77 #define KET_DEL (0x7FU)
78
79 #define SHELL_EVENT_DATA_ARRIVED (1U << 0)
80 #define SHELL_EVENT_DATA_SENT (1U << 1)
81
82 #define SHELL_SPRINTF_BUFFER_SIZE (64U)
83
84 /*! @brief A type for the handle special key. */
85 typedef enum _fun_key_status
86 {
87 kSHELL_Normal = 0U, /*!< Normal key */
88 kSHELL_Special = 1U, /*!< Special key */
89 kSHELL_Function = 2U, /*!< Function key */
90 } fun_key_status_t;
91
92 /*! @brief Data structure for Shell environment. */
93 typedef struct _shell_context_handle
94 {
95 list_label_t commandContextListHead; /*!< Command shellContextHandle list queue head */
96 serial_handle_t serialHandle; /*!< Serial manager handle */
97 uint8_t
98 serialWriteHandleBuffer[SERIAL_MANAGER_WRITE_HANDLE_SIZE]; /*!< The buffer for serial manager write handle */
99 serial_write_handle_t serialWriteHandle; /*!< The serial manager write handle */
100 uint8_t serialReadHandleBuffer[SERIAL_MANAGER_READ_HANDLE_SIZE]; /*!< The buffer for serial manager read handle */
101 serial_read_handle_t serialReadHandle; /*!< The serial manager read handle */
102 char *prompt; /*!< Prompt string */
103 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
104
105 #if defined(OSA_USED)
106
107 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
108 common_task_message_t commontaskMsg; /*!< Message for common task */
109 #else
110 uint8_t event[OSA_EVENT_HANDLE_SIZE]; /*!< Event instance */
111 uint8_t taskId[OSA_TASK_HANDLE_SIZE]; /*!< Task handle */
112 #endif
113
114 #endif
115
116 #endif
117 char line[SHELL_BUFFER_SIZE]; /*!< Consult buffer */
118 char hist_buf[SHELL_HISTORY_COUNT][SHELL_BUFFER_SIZE]; /*!< History buffer*/
119 char printBuffer[SHELL_SPRINTF_BUFFER_SIZE]; /*!< Buffer for print */
120 uint32_t printLength; /*!< All length has been printed */
121 uint16_t hist_current; /*!< Current history command in hist buff*/
122 uint16_t hist_count; /*!< Total history command in hist buff*/
123 enum _fun_key_status stat; /*!< Special key status */
124 uint8_t cmd_num; /*!< Number of user commands */
125 uint8_t l_pos; /*!< Total line position */
126 uint8_t c_pos; /*!< Current line position */
127 volatile uint8_t notificationPost; /*!< The serial manager notification is post */
128 uint8_t exit; /*!< Exit Flag*/
129 uint8_t printBusy; /*!< Print is busy */
130 } shell_context_handle_t;
131
132 #if 0
133 #define SHELL_STRUCT_OFFSET(type, field) ((size_t) & (((type *)0)->field))
134 #define SHEEL_COMMAND_POINTER(node) \
135 ((shell_command_t *)(((uint32_t)(node)) - SHELL_STRUCT_OFFSET(shell_command_t, link)))
136 #else
137 #define SHEEL_COMMAND_POINTER(node) \
138 ((shell_command_t *)(((uint32_t)(node)) - (sizeof(shell_command_t) - sizeof(list_element_t))))
139 #endif
140 /*******************************************************************************
141 * Prototypes
142 ******************************************************************************/
143 static shell_status_t SHELL_HelpCommand(shell_handle_t shellHandle, int32_t argc, char **argv); /*!< help command */
144
145 static shell_status_t SHELL_ExitCommand(shell_handle_t shellHandle, int32_t argc, char **argv); /*!< exit command */
146
147 static int32_t SHELL_ParseLine(const char *cmd, uint32_t len, char *argv[]); /*!< parse line command */
148
149 static int32_t SHELL_StringCompare(const char *str1, const char *str2, int32_t count); /*!< compare string command */
150
151 static void SHELL_ProcessCommand(shell_context_handle_t *shellContextHandle, const char *cmd); /*!< process a command */
152
153 static void SHELL_GetHistoryCommand(shell_context_handle_t *shellContextHandle,
154 uint8_t hist_pos); /*!< get commands history */
155
156 static void SHELL_AutoComplete(shell_context_handle_t *shellContextHandle); /*!< auto complete command */
157
158 static shell_status_t SHELL_GetChar(shell_context_handle_t *shellContextHandle,
159 uint8_t *ch); /*!< get a char from communication interface */
160
161 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
162 static void SHELL_Task(void *param); /*!< Shell task*/
163 #endif
164
165 /*******************************************************************************
166 * Variables
167 ******************************************************************************/
168
169 static SHELL_COMMAND_DEFINE(help, "\r\n\"help\": List all the registered commands\r\n", SHELL_HelpCommand, 0);
170 static SHELL_COMMAND_DEFINE(exit, "\r\n\"exit\": Exit program\r\n", SHELL_ExitCommand, 0);
171
172 static char s_paramBuffer[SHELL_BUFFER_SIZE];
173
174 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
175 #if defined(OSA_USED)
176 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
177 #else
178 /*
179 * \brief Defines the serial manager task's stack
180 */
181 static OSA_TASK_DEFINE(SHELL_Task, SHELL_TASK_PRIORITY, 1, SHELL_TASK_STACK_SIZE, false);
182 #endif
183 #endif /* OSA_USED */
184 #endif /* SHELL_NON_BLOCKING_MODE */
185 /*******************************************************************************
186 * Code
187 ******************************************************************************/
188
189 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
SHELL_SerialManagerRxCallback(void * callbackParam,serial_manager_callback_message_t * message,serial_manager_status_t status)190 static void SHELL_SerialManagerRxCallback(void *callbackParam,
191 serial_manager_callback_message_t *message,
192 serial_manager_status_t status)
193 {
194 shell_context_handle_t *shellHandle;
195
196 assert(callbackParam);
197 assert(message);
198
199 shellHandle = (shell_context_handle_t *)callbackParam;
200
201 if (0U == shellHandle->notificationPost)
202 {
203 shellHandle->notificationPost = 1U;
204 #if defined(OSA_USED)
205
206 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
207 shellHandle->commontaskMsg.callback = SHELL_Task;
208 shellHandle->commontaskMsg.callbackParam = shellHandle;
209 (void)COMMON_TASK_post_message(&shellHandle->commontaskMsg);
210 #else
211 (void)OSA_EventSet((osa_event_handle_t)shellHandle->event, SHELL_EVENT_DATA_ARRIVED);
212 #endif
213
214 #else
215 SHELL_Task(shellHandle);
216 #endif
217 }
218 }
219 #endif
220
SHELL_WriteBuffer(char * buffer,int32_t * indicator,char val,int len)221 static void SHELL_WriteBuffer(char *buffer, int32_t *indicator, char val, int len)
222 {
223 shell_context_handle_t *shellContextHandle;
224 int i = 0;
225 shellContextHandle = (shell_context_handle_t *)(void *)buffer;
226
227 for (i = 0; i < len; i++)
228 {
229 if ((*indicator + 1) >= (int32_t)SHELL_SPRINTF_BUFFER_SIZE)
230 {
231 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
232 if (NULL == shellContextHandle->serialHandle)
233 {
234 for (uint32_t index = 0; index < ((uint32_t)*indicator); index++)
235 {
236 (void)putchar(shellContextHandle->printBuffer[index]);
237 }
238 }
239 else
240 #endif
241 {
242 (void)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle,
243 (uint8_t *)shellContextHandle->printBuffer, (uint32_t)*indicator);
244 }
245
246 shellContextHandle->printLength += (uint32_t)*indicator;
247 *indicator = 0;
248 }
249
250 shellContextHandle->printBuffer[*indicator] = val;
251 (*indicator)++;
252 }
253 }
254
SHELL_Sprintf(void * buffer,const char * formatString,va_list ap)255 static int SHELL_Sprintf(void *buffer, const char *formatString, va_list ap)
256 {
257 shell_context_handle_t *shellContextHandle;
258 uint32_t length;
259 shellContextHandle = (shell_context_handle_t *)buffer;
260
261 length = (uint32_t)StrFormatPrintf(formatString, ap, (char *)buffer, SHELL_WriteBuffer);
262 shellContextHandle->printLength += length;
263 return (int32_t)length;
264 }
265
266 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
SHELL_Task(void * param)267 static void SHELL_Task(void *param)
268 #else
269 void SHELL_Task(shell_handle_t shellHandle)
270 #endif
271 {
272 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
273 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)param;
274 #else
275 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
276 #endif
277 uint8_t ch;
278
279 if (NULL != shellContextHandle)
280 {
281 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
282
283 #if defined(OSA_USED)
284
285 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
286 #else
287 osa_event_flags_t ev = 0;
288
289 do
290 {
291 if (KOSA_StatusSuccess == OSA_EventWait((osa_event_handle_t)shellContextHandle->event, osaEventFlagsAll_c,
292 0U, osaWaitForever_c, &ev))
293 {
294 if (0U != (ev & SHELL_EVENT_DATA_ARRIVED))
295 #endif
296
297 #endif
298
299 #endif
300 {
301 shellContextHandle->notificationPost = 0;
302 do
303 {
304 if ((bool)shellContextHandle->exit)
305 {
306 if (shellContextHandle->serialReadHandle != NULL)
307 {
308 (void)SerialManager_CloseReadHandle(shellContextHandle->serialReadHandle);
309 shellContextHandle->serialReadHandle = NULL;
310 }
311 if (shellContextHandle->serialWriteHandle != NULL)
312 {
313 (void)SerialManager_CloseWriteHandle(shellContextHandle->serialWriteHandle);
314 shellContextHandle->serialWriteHandle = NULL;
315 }
316 break;
317 }
318 if (kStatus_SHELL_Success != (shell_status_t)SHELL_GetChar(shellContextHandle, &ch))
319 {
320 /* If error occurred when getting a char, exit the task and waiting the new data arriving. */
321 break;
322 }
323
324 /* Special key */
325 if (ch == KEY_ESC)
326 {
327 shellContextHandle->stat = kSHELL_Special;
328 continue;
329 }
330 else if (shellContextHandle->stat == kSHELL_Special)
331 {
332 /* Function key */
333 if ((char)ch == '[')
334 {
335 shellContextHandle->stat = kSHELL_Function;
336 continue;
337 }
338 shellContextHandle->stat = kSHELL_Normal;
339 }
340 else if (shellContextHandle->stat == kSHELL_Function)
341 {
342 shellContextHandle->stat = kSHELL_Normal;
343
344 switch ((char)ch)
345 {
346 /* History operation here */
347 case 'A': /* Up key */
348 SHELL_GetHistoryCommand(shellContextHandle, (uint8_t)shellContextHandle->hist_current);
349 if (shellContextHandle->hist_current < (shellContextHandle->hist_count - 1U))
350 {
351 shellContextHandle->hist_current++;
352 }
353 break;
354 case 'B': /* Down key */
355 SHELL_GetHistoryCommand(shellContextHandle, (uint8_t)shellContextHandle->hist_current);
356 if (shellContextHandle->hist_current > 0U)
357 {
358 shellContextHandle->hist_current--;
359 }
360 break;
361 case 'D': /* Left key */
362 if ((bool)shellContextHandle->c_pos)
363 {
364 (void)SHELL_Write(shellContextHandle, "\b", 1);
365 shellContextHandle->c_pos--;
366 }
367 break;
368 case 'C': /* Right key */
369 if (shellContextHandle->c_pos < shellContextHandle->l_pos)
370 {
371 (void)SHELL_Write(shellContextHandle,
372 &shellContextHandle->line[shellContextHandle->c_pos], 1);
373 shellContextHandle->c_pos++;
374 }
375 break;
376 default:
377 /* MISRA C-2012 Rule 16.4 */
378 break;
379 }
380 continue;
381 }
382 /* Handle tab key */
383 else if ((char)ch == '\t')
384 {
385 #if SHELL_AUTO_COMPLETE
386 /* Move the cursor to the beginning of line */
387 uint32_t i;
388 for (i = 0; i < (uint32_t)shellContextHandle->c_pos; i++)
389 {
390 (void)SHELL_Write(shellContextHandle, "\b", 1);
391 }
392 /* Do auto complete */
393 SHELL_AutoComplete(shellContextHandle);
394 /* Move position to end */
395 shellContextHandle->l_pos = (uint8_t)strlen(shellContextHandle->line);
396 shellContextHandle->c_pos = shellContextHandle->l_pos;
397 #endif
398 continue;
399 }
400 /* Handle backspace key */
401 else if ((ch == KET_DEL) || ((char)ch == '\b'))
402 {
403 /* There must be at last one char */
404 if (shellContextHandle->c_pos == 0U)
405 {
406 continue;
407 }
408
409 shellContextHandle->l_pos--;
410 shellContextHandle->c_pos--;
411
412 if (shellContextHandle->l_pos > shellContextHandle->c_pos)
413 {
414 (void)memmove(&shellContextHandle->line[shellContextHandle->c_pos],
415 &shellContextHandle->line[shellContextHandle->c_pos + 1U],
416 (uint32_t)shellContextHandle->l_pos - (uint32_t)shellContextHandle->c_pos);
417 shellContextHandle->line[shellContextHandle->l_pos] = '\0';
418 (void)SHELL_Write(shellContextHandle, "\b", 1);
419 (void)SHELL_Write(shellContextHandle, &shellContextHandle->line[shellContextHandle->c_pos],
420 strlen(&shellContextHandle->line[shellContextHandle->c_pos]));
421 (void)SHELL_Write(shellContextHandle, " \b", 3);
422
423 /* Reset position */
424 uint32_t i;
425 for (i = (uint32_t)shellContextHandle->c_pos; i <= (uint32_t)shellContextHandle->l_pos; i++)
426 {
427 (void)SHELL_Write(shellContextHandle, "\b", 1);
428 }
429 }
430 else /* Normal backspace operation */
431 {
432 (void)SHELL_Write(shellContextHandle, "\b \b", 3);
433 shellContextHandle->line[shellContextHandle->l_pos] = '\0';
434 }
435 continue;
436 }
437 else
438 {
439 /* MISRA C-2012 Rule 15.7 */
440 }
441
442 /* Input too long */
443 if (shellContextHandle->l_pos >= (SHELL_BUFFER_SIZE - 1U))
444 {
445 shellContextHandle->l_pos = 0U;
446 }
447
448 /* Handle end of line, break */
449 if (((char)ch == '\r') || ((char)ch == '\n'))
450 {
451 static char endoflinechar = '\0';
452
453 if (((uint8_t)endoflinechar != 0U) && ((uint8_t)endoflinechar != ch))
454 {
455 continue;
456 }
457 else
458 {
459 endoflinechar = (char)ch;
460 /* Print new line. */
461 (void)SHELL_Write(shellContextHandle, "\r\n", 2U);
462 /* If command line is not NULL, will start process it. */
463 if (0U != strlen(shellContextHandle->line))
464 {
465 SHELL_ProcessCommand(shellContextHandle, shellContextHandle->line);
466 }
467 /* Print prompt. */
468 (void)SHELL_Write(shellContextHandle, shellContextHandle->prompt,
469 strlen(shellContextHandle->prompt));
470 /* Reset all params */
471 shellContextHandle->c_pos = shellContextHandle->l_pos = 0;
472 shellContextHandle->hist_current = 0;
473 (void)memset(shellContextHandle->line, 0, sizeof(shellContextHandle->line));
474 continue;
475 }
476 }
477
478 /* Normal character */
479 if (shellContextHandle->c_pos < shellContextHandle->l_pos)
480 {
481 (void)memmove(&shellContextHandle->line[shellContextHandle->c_pos + 1U],
482 &shellContextHandle->line[shellContextHandle->c_pos],
483 (uint32_t)shellContextHandle->l_pos - (uint32_t)shellContextHandle->c_pos);
484 shellContextHandle->line[shellContextHandle->c_pos] = (char)ch;
485 (void)SHELL_Write(shellContextHandle, &shellContextHandle->line[shellContextHandle->c_pos],
486 strlen(&shellContextHandle->line[shellContextHandle->c_pos]));
487 /* Move the cursor to new position */
488 uint32_t i;
489 for (i = (uint32_t)shellContextHandle->c_pos; i < (uint32_t)shellContextHandle->l_pos; i++)
490 {
491 (void)SHELL_Write(shellContextHandle, "\b", 1);
492 }
493 }
494 else
495 {
496 shellContextHandle->line[shellContextHandle->l_pos] = (char)ch;
497 (void)SHELL_Write(shellContextHandle, &shellContextHandle->line[shellContextHandle->l_pos], 1);
498 }
499
500 ch = 0;
501 shellContextHandle->l_pos++;
502 shellContextHandle->c_pos++;
503 } while (0U == shellContextHandle->exit);
504 }
505 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
506
507 #if defined(OSA_USED)
508
509 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
510 #else
511 }
512 } while (1U == gUseRtos_c); /* USE_RTOS = 0 for BareMetal and 1 for OS */
513 #endif
514
515 #endif
516
517 #endif
518 }
519 }
520
521 static shell_status_t SHELL_HelpCommand(shell_handle_t shellHandle, int32_t argc, char **argv)
522 {
523 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
524 shell_command_t *shellCommandContextHandle;
525 list_element_handle_t p = LIST_GetHead(&shellContextHandle->commandContextListHead);
526
527 while (p != NULL)
528 {
529 shellCommandContextHandle = SHEEL_COMMAND_POINTER(p);
530 if ((shellCommandContextHandle->pcHelpString != NULL) && (bool)strlen(shellCommandContextHandle->pcHelpString))
531 {
532 (void)SHELL_Write(shellContextHandle, shellCommandContextHandle->pcHelpString,
533 strlen(shellCommandContextHandle->pcHelpString));
534 }
535
536 p = LIST_GetNext(p);
537 }
538 return kStatus_SHELL_Success;
539 }
540
541 static shell_status_t SHELL_ExitCommand(shell_handle_t shellHandle, int32_t argc, char **argv)
542 {
543 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
544 /* Skip warning */
545 (void)SHELL_Write(shellContextHandle, "\r\nSHELL exited\r\n", strlen("\r\nSHELL exited\r\n"));
546 shellContextHandle->exit = (uint8_t) true;
547 return kStatus_SHELL_Success;
548 }
549
550 static void SHELL_ProcessCommand(shell_context_handle_t *shellContextHandle, const char *cmd)
551 {
552 shell_command_t *tmpCommand = NULL;
553 const char *tmpCommandString;
554 int32_t argc;
555 char *argv[SHELL_BUFFER_SIZE] = {0};
556 list_element_handle_t p;
557 uint8_t flag = 1;
558 uint8_t tmpCommandLen;
559 uint8_t tmpLen;
560 uint8_t i = 0;
561
562 tmpLen = (uint8_t)strlen(cmd);
563 argc = SHELL_ParseLine(cmd, tmpLen, argv);
564
565 if ((argc > 0))
566 {
567 p = LIST_GetHead(&shellContextHandle->commandContextListHead);
568 while (p != NULL)
569 {
570 tmpCommand = SHEEL_COMMAND_POINTER(p);
571 tmpCommandString = tmpCommand->pcCommand;
572 tmpCommandLen = (uint8_t)strlen(tmpCommandString);
573 /* Compare with space or end of string */
574 if ((cmd[tmpCommandLen] == ' ') || (cmd[tmpCommandLen] == (char)0x00))
575 {
576 if (SHELL_StringCompare(tmpCommandString, argv[0], (int32_t)tmpCommandLen) == 0)
577 {
578 /* support commands with optional number of parameters */
579 if (tmpCommand->cExpectedNumberOfParameters == (uint8_t)SHELL_IGNORE_PARAMETER_COUNT)
580 {
581 flag = 0;
582 }
583 else if ((tmpCommand->cExpectedNumberOfParameters == 0U) && (argc == 1))
584 {
585 flag = 0;
586 }
587 else if (tmpCommand->cExpectedNumberOfParameters > 0U)
588 {
589 if ((argc - 1) == (int32_t)tmpCommand->cExpectedNumberOfParameters)
590 {
591 flag = 0;
592 }
593 }
594 else
595 {
596 flag = 1;
597 }
598 break;
599 }
600 }
601 p = LIST_GetNext(p);
602 }
603 if (NULL == p)
604 {
605 tmpCommand = NULL;
606 }
607 }
608
609 if ((tmpCommand != NULL) && (flag == 1U))
610 {
611 (void)SHELL_Write(
612 shellContextHandle,
613 "\r\nIncorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n",
614 strlen(
615 "\r\nIncorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n"));
616 }
617 else if (tmpCommand != NULL)
618 {
619 tmpLen = (uint8_t)strlen(cmd);
620 /* Compare with last command. Push back to history buffer if different */
621 if (tmpLen != (uint8_t)SHELL_StringCompare(cmd, shellContextHandle->hist_buf[0], (int32_t)strlen(cmd)))
622 {
623 for (i = SHELL_HISTORY_COUNT - 1U; i > 0U; i--)
624 {
625 (void)memset(shellContextHandle->hist_buf[i], (int)'\0', SHELL_BUFFER_SIZE);
626 tmpLen = (uint8_t)strlen(shellContextHandle->hist_buf[i - 1U]);
627 (void)memcpy(shellContextHandle->hist_buf[i], shellContextHandle->hist_buf[i - 1U], tmpLen);
628 }
629 (void)memset(shellContextHandle->hist_buf[0], (int)'\0', SHELL_BUFFER_SIZE);
630 tmpLen = (uint8_t)strlen(cmd);
631 (void)memcpy(shellContextHandle->hist_buf[0], cmd, tmpLen);
632 if (shellContextHandle->hist_count < SHELL_HISTORY_COUNT)
633 {
634 shellContextHandle->hist_count++;
635 }
636 }
637 (void)tmpCommand->pFuncCallBack(shellContextHandle, argc, argv);
638 }
639 else
640 {
641 (void)SHELL_Write(
642 shellContextHandle,
643 "\r\nCommand not recognized. Enter 'help' to view a list of available commands.\r\n\r\n",
644 strlen("\r\nCommand not recognized. Enter 'help' to view a list of available commands.\r\n\r\n"));
645 }
646 }
647
648 static void SHELL_GetHistoryCommand(shell_context_handle_t *shellContextHandle, uint8_t hist_pos)
649 {
650 uint32_t i;
651 uint32_t tmp;
652
653 if (shellContextHandle->hist_buf[0][0] == '\0')
654 {
655 shellContextHandle->hist_current = 0;
656 return;
657 }
658
659 #if 0 /*hist_pos is passed from hist_current. And hist_current is only changed in case 'A'/'B',as hist_count is 3 \
660 most, it can't be more than 3 */
661 if (hist_pos >= SHELL_HISTORY_COUNT)
662 {
663 hist_pos = SHELL_HISTORY_COUNT - 1U;
664 }
665 #endif
666
667 tmp = strlen(shellContextHandle->line);
668 /* Clear current if have */
669 if (tmp > 0U)
670 {
671 (void)memset(shellContextHandle->line, (int)'\0', tmp);
672 for (i = 0U; i < tmp; i++)
673 {
674 (void)SHELL_Write(shellContextHandle, "\b \b", 3);
675 }
676 }
677
678 shellContextHandle->l_pos = (uint8_t)strlen(shellContextHandle->hist_buf[hist_pos]);
679 shellContextHandle->c_pos = shellContextHandle->l_pos;
680 (void)memcpy(shellContextHandle->line, shellContextHandle->hist_buf[hist_pos], shellContextHandle->l_pos);
681 (void)SHELL_Write(shellContextHandle, shellContextHandle->hist_buf[hist_pos],
682 strlen(shellContextHandle->hist_buf[hist_pos]));
683 }
684
685 static void SHELL_AutoComplete(shell_context_handle_t *shellContextHandle)
686 {
687 int32_t minLen;
688 list_element_handle_t p;
689 shell_command_t *tmpCommand = NULL;
690 const char *namePtr;
691 const char *cmdName;
692
693 minLen = (int32_t)SHELL_BUFFER_SIZE;
694 namePtr = NULL;
695
696 /* Empty tab, list all commands */
697 if (shellContextHandle->line[0] == '\0')
698 {
699 (void)SHELL_HelpCommand(shellContextHandle, 0, NULL);
700 return;
701 }
702
703 (void)SHELL_Write(shellContextHandle, "\r\n", 2);
704
705 /* Do auto complete */
706 p = LIST_GetHead(&shellContextHandle->commandContextListHead);
707 while (p != NULL)
708 {
709 tmpCommand = SHEEL_COMMAND_POINTER(p);
710 cmdName = tmpCommand->pcCommand;
711 if (SHELL_StringCompare(shellContextHandle->line, cmdName, (int32_t)strlen(shellContextHandle->line)) == 0)
712 {
713 /* Show possible matches */
714 (void)SHELL_Printf(shellContextHandle, "%s ", cmdName);
715 if (minLen > ((int32_t)strlen(cmdName)))
716 {
717 namePtr = cmdName;
718 minLen = (int32_t)strlen(namePtr);
719 }
720 }
721 p = LIST_GetNext(p);
722 }
723 /* Auto complete string */
724 if (namePtr != NULL)
725 {
726 (void)memcpy(shellContextHandle->line, namePtr, (uint32_t)minLen);
727 }
728 SHELL_PrintPrompt(shellContextHandle);
729 (void)SHELL_Write(shellContextHandle, shellContextHandle->line, strlen(shellContextHandle->line));
730 return;
731 }
732
733 static int32_t SHELL_StringCompare(const char *str1, const char *str2, int32_t count)
734 {
735 while ((bool)(count--))
736 {
737 if (*str1++ != *str2++)
738 {
739 return (int32_t)(*(str1 - 1) - *(str2 - 1));
740 }
741 }
742 return 0;
743 }
744
745 static int32_t SHELL_ParseLine(const char *cmd, uint32_t len, char *argv[])
746 {
747 uint32_t argc;
748 char *p;
749 uint32_t position;
750
751 /* Init params */
752 (void)memset(s_paramBuffer, (int)'\0', len + 1U);
753 (void)memcpy(s_paramBuffer, cmd, len);
754
755 p = s_paramBuffer;
756 position = 0;
757 argc = 0;
758
759 while (position < len)
760 {
761 /* Skip all blanks */
762 while ((position < len) && ((char)(*p) == ' '))
763 {
764 *p = '\0';
765 p++;
766 position++;
767 }
768
769 if (position >= len)
770 {
771 break;
772 }
773
774 /* Process begin of a string */
775 if (*p == '"')
776 {
777 p++;
778 position++;
779 argv[argc] = p;
780 argc++;
781 /* Skip this string */
782 while ((*p != '"') && (position < len))
783 {
784 p++;
785 position++;
786 }
787 /* Skip '"' */
788 *p = '\0';
789 p++;
790 position++;
791 }
792 else /* Normal char */
793 {
794 argv[argc] = p;
795 argc++;
796 while (((char)*p != ' ') && (position < len))
797 {
798 p++;
799 position++;
800 }
801 }
802 }
803 return (int32_t)argc;
804 }
805
806 static shell_status_t SHELL_GetChar(shell_context_handle_t *shellContextHandle, uint8_t *ch)
807 {
808 shell_status_t status;
809
810 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
811 if (NULL == shellContextHandle->serialHandle)
812 {
813 int ret;
814 ret = getchar();
815 if (ret > 0)
816 {
817 *ch = (uint8_t)ret;
818 status = kStatus_SHELL_Success;
819 }
820 else
821 {
822 status = kStatus_SHELL_Error;
823 }
824 }
825 else
826 #endif
827 {
828 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
829 uint32_t length = 0;
830
831 (void)SerialManager_TryRead(shellContextHandle->serialReadHandle, ch, 1, &length);
832
833 if (length > 0U)
834 {
835 status = kStatus_SHELL_Success;
836 }
837 else
838 {
839 status = kStatus_SHELL_Error;
840 }
841 #else
842 status = (shell_status_t)SerialManager_ReadBlocking(shellContextHandle->serialReadHandle, ch, 1);
843 #endif
844 }
845
846 return status;
847 }
848
849 shell_status_t SHELL_Init(shell_handle_t shellHandle, serial_handle_t serialHandle, char *prompt)
850 {
851 shell_context_handle_t *shellContextHandle;
852 serial_manager_status_t status = kStatus_SerialManager_Error;
853 (void)status;
854
855 assert(shellHandle);
856 #if !(!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
857 assert(serialHandle);
858 #endif
859 assert(prompt);
860 assert(SHELL_HANDLE_SIZE >= sizeof(shell_context_handle_t));
861
862 shellContextHandle = (shell_context_handle_t *)shellHandle;
863
864 /* memory set for shellHandle */
865 (void)memset(shellHandle, 0, SHELL_HANDLE_SIZE);
866
867 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
868 if (NULL == serialHandle)
869 {
870 }
871 else
872 #endif
873 {
874 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
875
876 #if defined(OSA_USED)
877
878 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
879 (void)COMMON_TASK_init();
880 #else
881 if (KOSA_StatusSuccess != OSA_EventCreate((osa_event_handle_t)shellContextHandle->event, 1U))
882 {
883 return kStatus_SHELL_Error;
884 }
885
886 if (KOSA_StatusSuccess !=
887 OSA_TaskCreate((osa_task_handle_t)shellContextHandle->taskId, OSA_TASK(SHELL_Task), shellContextHandle))
888 {
889 return kStatus_SHELL_Error;
890 }
891 #endif
892
893 #endif
894
895 #endif
896 }
897
898 shellContextHandle->prompt = prompt;
899 shellContextHandle->serialHandle = serialHandle;
900
901 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
902 if (NULL == serialHandle)
903 {
904 }
905 else
906 #endif
907 {
908 shellContextHandle->serialWriteHandle = (serial_write_handle_t)&shellContextHandle->serialWriteHandleBuffer[0];
909 status = SerialManager_OpenWriteHandle(shellContextHandle->serialHandle, shellContextHandle->serialWriteHandle);
910 assert(kStatus_SerialManager_Success == status);
911
912 shellContextHandle->serialReadHandle = (serial_read_handle_t)&shellContextHandle->serialReadHandleBuffer[0];
913 status = SerialManager_OpenReadHandle(shellContextHandle->serialHandle, shellContextHandle->serialReadHandle);
914 assert(kStatus_SerialManager_Success == status);
915
916 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
917 status = SerialManager_InstallRxCallback(shellContextHandle->serialReadHandle, SHELL_SerialManagerRxCallback,
918 shellContextHandle);
919 assert(kStatus_SerialManager_Success == status);
920 #endif
921 (void)status;
922 }
923
924 (void)SHELL_RegisterCommand(shellContextHandle, SHELL_COMMAND(help));
925 (void)SHELL_RegisterCommand(shellContextHandle, SHELL_COMMAND(exit));
926
927 (void)SHELL_Write(shellContextHandle, "\r\nCopyright 2020 NXP\r\n", strlen("\r\nCopyright 2020 NXP\r\n"));
928 SHELL_PrintPrompt(shellContextHandle);
929
930 return kStatus_SHELL_Success;
931 }
932
933 shell_status_t SHELL_RegisterCommand(shell_handle_t shellHandle, shell_command_t *shellCommand)
934 {
935 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
936 assert(shellHandle);
937 assert(shellCommand);
938
939 /* memory set for shellHandle */
940 (void)memset(&shellCommand->link, 0, sizeof(shellCommand->link));
941
942 (void)LIST_AddTail(&shellContextHandle->commandContextListHead, &shellCommand->link);
943
944 return kStatus_SHELL_Success;
945 }
946
947 shell_status_t SHELL_UnregisterCommand(shell_command_t *shellCommand)
948 {
949 assert(shellCommand);
950
951 (void)LIST_RemoveElement(&shellCommand->link);
952
953 /* memory set for shellHandle */
954 (void)memset(&shellCommand->link, 0, sizeof(shellCommand->link));
955
956 return kStatus_SHELL_Success;
957 }
958
959 shell_status_t SHELL_Write(shell_handle_t shellHandle, const char *buffer, uint32_t length)
960 {
961 shell_context_handle_t *shellContextHandle;
962 uint32_t primask;
963 shell_status_t status;
964
965 assert(shellHandle);
966 assert(buffer);
967
968 if (!(bool)length)
969 {
970 return kStatus_SHELL_Success;
971 }
972
973 shellContextHandle = (shell_context_handle_t *)shellHandle;
974
975 primask = DisableGlobalIRQ();
976 if ((bool)shellContextHandle->printBusy)
977 {
978 EnableGlobalIRQ(primask);
979 return kStatus_SHELL_Error;
980 }
981 shellContextHandle->printBusy = 1U;
982 EnableGlobalIRQ(primask);
983 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
984 if (NULL == shellContextHandle->serialHandle)
985 {
986 status = kStatus_SHELL_Success;
987 for (uint32_t index = 0; index < length; index++)
988 {
989 (void)putchar(buffer[index]);
990 }
991 }
992 else
993 #endif
994 {
995 status = (shell_status_t)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle, (uint8_t *)buffer,
996 length);
997 }
998
999 shellContextHandle->printBusy = 0U;
1000
1001 return status;
1002 }
1003
1004 int SHELL_Printf(shell_handle_t shellHandle, const char *formatString, ...)
1005 {
1006 shell_context_handle_t *shellContextHandle;
1007 uint32_t length;
1008 uint32_t primask;
1009 va_list ap;
1010
1011 assert(shellHandle);
1012 assert(formatString);
1013
1014 shellContextHandle = (shell_context_handle_t *)shellHandle;
1015
1016 primask = DisableGlobalIRQ();
1017 if ((bool)shellContextHandle->printBusy)
1018 {
1019 EnableGlobalIRQ(primask);
1020 return -1;
1021 }
1022 shellContextHandle->printBusy = 1U;
1023 EnableGlobalIRQ(primask);
1024
1025 va_start(ap, formatString);
1026
1027 shellContextHandle->printLength = 0U;
1028 length = (uint32_t)SHELL_Sprintf(shellHandle, formatString, ap);
1029 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
1030 if (NULL == shellContextHandle->serialHandle)
1031 {
1032 for (uint32_t index = 0; index < length; index++)
1033 {
1034 (void)putchar(shellContextHandle->printBuffer[index]);
1035 }
1036 }
1037 else
1038 #endif
1039 {
1040 (void)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle,
1041 (uint8_t *)shellContextHandle->printBuffer, length);
1042 }
1043 va_end(ap);
1044
1045 shellContextHandle->printBusy = 0U;
1046 return (int32_t)shellContextHandle->printLength;
1047 }
1048
1049 void SHELL_ChangePrompt(shell_handle_t shellHandle, char *prompt)
1050 {
1051 shell_context_handle_t *shellContextHandle;
1052 assert(shellHandle);
1053 assert(prompt);
1054
1055 shellContextHandle = (shell_context_handle_t *)shellHandle;
1056
1057 shellContextHandle->prompt = prompt;
1058 SHELL_PrintPrompt(shellContextHandle);
1059 }
1060
1061 void SHELL_PrintPrompt(shell_handle_t shellHandle)
1062 {
1063 shell_context_handle_t *shellContextHandle;
1064 assert(shellHandle);
1065
1066 shellContextHandle = (shell_context_handle_t *)shellHandle;
1067
1068 (void)SHELL_Write(shellContextHandle, "\r\n", 2U);
1069 (void)SHELL_Write(shellContextHandle, shellContextHandle->prompt, strlen(shellContextHandle->prompt));
1070 }
1071