1 /*
2 * Copyright (c) 2015, Freescale Semiconductor, Inc.
3 * Copyright 2016-2022 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 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
65 #include "fsl_component_common_task.h"
66 #else
67 #include "fsl_os_abstraction.h"
68 #endif
69
70 #endif
71
72 /*******************************************************************************
73 * Definitions
74 ******************************************************************************/
75 #if defined(OSA_USED)
76 #define SHELL_WRITEX SHELL_WriteSynchronization
77 #else
78 #define SHELL_WRITEX SHELL_Write
79 #endif
80
81 #if defined(OSA_USED)
82 #if (defined(USE_RTOS) && (USE_RTOS > 0U))
83 static OSA_MUTEX_HANDLE_DEFINE(s_shellMutex);
84 #define SHELL_MUTEX_CREATE() (void)OSA_MutexCreate(s_shellMutex)
85 #define SHELL_ENTER_CRITICAL() (void)OSA_MutexLock(s_shellMutex, osaWaitForever_c)
86 #define SHELL_EXIT_CRITICAL() (void)OSA_MutexUnlock(s_shellMutex)
87 #else
88 #define SHELL_MUTEX_CREATE()
89 #define SHELL_ENTER_CRITICAL()
90 #define SHELL_EXIT_CRITICAL()
91 #endif
92 #else
93 #ifdef SDK_OS_FREE_RTOS
94 #include "FreeRTOS.h"
95 #include "queue.h"
96 #include "semphr.h"
97 static QueueHandle_t s_shellMutex;
98
99 #define SHELL_MUTEX_CREATE() s_shellMutex = xSemaphoreCreateMutex()
100 #define SHELL_ENTER_CRITICAL() (void)xSemaphoreTakeRecursive(s_shellMutex, portMAX_DELAY)
101 #define SHELL_EXIT_CRITICAL() (void)xSemaphoreGiveRecursive(s_shellMutex)
102 #else /* BM case*/
103 #define SHELL_MUTEX_CREATE()
104 #define SHELL_ENTER_CRITICAL()
105 #define SHELL_EXIT_CRITICAL()
106 #endif
107 #endif
108
109 #define KEY_ESC (0x1BU)
110 #define KET_DEL (0x7FU)
111
112 #define SHELL_EVENT_DATA_ARRIVED (1U << 0)
113 #define SHELL_EVENT_DATA_SENT (1U << 1)
114
115 #define SHELL_SPRINTF_BUFFER_SIZE (64U)
116
117 /*! @brief A type for the handle special key. */
118 typedef enum _fun_key_status
119 {
120 kSHELL_Normal = 0U, /*!< Normal key */
121 kSHELL_Special = 1U, /*!< Special key */
122 kSHELL_Function = 2U, /*!< Function key */
123 } fun_key_status_t;
124
125 /*! @brief Data structure for Shell environment. */
126 typedef struct _shell_context_handle
127 {
128 list_label_t commandContextListHead; /*!< Command shellContextHandle list queue head */
129 serial_handle_t serialHandle; /*!< Serial manager handle */
130 uint8_t
131 serialWriteHandleBuffer[SERIAL_MANAGER_WRITE_HANDLE_SIZE]; /*!< The buffer for serial manager write handle */
132 serial_write_handle_t serialWriteHandle; /*!< The serial manager write handle */
133 uint8_t serialReadHandleBuffer[SERIAL_MANAGER_READ_HANDLE_SIZE]; /*!< The buffer for serial manager read handle */
134 serial_read_handle_t serialReadHandle; /*!< The serial manager read handle */
135 char *prompt; /*!< Prompt string */
136 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
137
138 #if defined(OSA_USED)
139
140 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
141 common_task_message_t commontaskMsg; /*!< Message for common task */
142 #else
143 uint8_t event[OSA_EVENT_HANDLE_SIZE]; /*!< Event instance */
144 uint8_t taskId[OSA_TASK_HANDLE_SIZE]; /*!< Task handle */
145 #endif
146
147 #endif
148
149 #endif
150 char line[SHELL_BUFFER_SIZE]; /*!< Consult buffer */
151 char hist_buf[SHELL_HISTORY_COUNT][SHELL_BUFFER_SIZE]; /*!< History buffer*/
152 char printBuffer[SHELL_SPRINTF_BUFFER_SIZE]; /*!< Buffer for print */
153 uint32_t printLength; /*!< All length has been printed */
154 uint16_t hist_current; /*!< Current history command in hist buff*/
155 uint16_t hist_count; /*!< Total history command in hist buff*/
156 enum _fun_key_status stat; /*!< Special key status */
157 uint8_t cmd_num; /*!< Number of user commands */
158 uint8_t l_pos; /*!< Total line position */
159 uint8_t c_pos; /*!< Current line position */
160 volatile uint8_t notificationPost; /*!< The serial manager notification is post */
161 uint8_t exit; /*!< Exit Flag*/
162 uint8_t printBusy : 1; /*!< Print is busy */
163 uint8_t taskBusy : 1; /*!< Task is busy */
164 } shell_context_handle_t;
165
166 #if 0
167 #define SHELL_STRUCT_OFFSET(type, field) ((size_t) & (((type *)0)->field))
168 #define SHEEL_COMMAND_POINTER(node) \
169 ((shell_command_t *)(((uint32_t)(node)) - SHELL_STRUCT_OFFSET(shell_command_t, link)))
170 #else
171 #define SHEEL_COMMAND_POINTER(node) \
172 ((shell_command_t *)(((uint32_t)(node)) - (sizeof(shell_command_t) - sizeof(list_element_t))))
173 #endif
174 /*******************************************************************************
175 * Prototypes
176 ******************************************************************************/
177 static shell_status_t SHELL_HelpCommand(shell_handle_t shellHandle, int32_t argc, char **argv); /*!< help command */
178
179 static shell_status_t SHELL_ExitCommand(shell_handle_t shellHandle, int32_t argc, char **argv); /*!< exit command */
180
181 static int32_t SHELL_ParseLine(const char *cmd, uint32_t len, char *argv[]); /*!< parse line command */
182
183 static int32_t SHELL_StringCompare(const char *str1, const char *str2, int32_t count); /*!< compare string command */
184
185 static void SHELL_ProcessCommand(shell_context_handle_t *shellContextHandle, const char *cmd); /*!< process a command */
186
187 static void SHELL_GetHistoryCommand(shell_context_handle_t *shellContextHandle,
188 uint8_t hist_pos); /*!< get commands history */
189
190 static void SHELL_AutoComplete(shell_context_handle_t *shellContextHandle); /*!< auto complete command */
191
192 static shell_status_t SHELL_GetChar(shell_context_handle_t *shellContextHandle,
193 uint8_t *ch); /*!< get a char from communication interface */
194
195 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
196 static void SHELL_Task(void *param); /*!< Shell task*/
197 #endif
198
199 /*******************************************************************************
200 * Variables
201 ******************************************************************************/
202
203 static SHELL_COMMAND_DEFINE(help, "\r\n\"help\": List all the registered commands\r\n", SHELL_HelpCommand, 0);
204 static SHELL_COMMAND_DEFINE(exit, "\r\n\"exit\": Exit program\r\n", SHELL_ExitCommand, 0);
205
206 static char s_paramBuffer[SHELL_BUFFER_SIZE];
207
208 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
209 #if defined(OSA_USED)
210 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
211 #else
212 /*
213 * \brief Defines the serial manager task's stack
214 */
215 static OSA_TASK_DEFINE(SHELL_Task, SHELL_TASK_PRIORITY, 1, SHELL_TASK_STACK_SIZE, false);
216 #endif
217 #endif /* OSA_USED */
218 #endif /* SHELL_NON_BLOCKING_MODE */
219 /*******************************************************************************
220 * Code
221 ******************************************************************************/
222
223 #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)224 static void SHELL_SerialManagerRxCallback(void *callbackParam,
225 serial_manager_callback_message_t *message,
226 serial_manager_status_t status)
227 {
228 shell_context_handle_t *shellHandle;
229
230 assert(callbackParam);
231 assert(message);
232
233 shellHandle = (shell_context_handle_t *)callbackParam;
234
235 if (0U == shellHandle->notificationPost)
236 {
237 shellHandle->notificationPost = 1U;
238 #if defined(OSA_USED)
239
240 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
241 shellHandle->commontaskMsg.callback = SHELL_Task;
242 shellHandle->commontaskMsg.callbackParam = shellHandle;
243 (void)COMMON_TASK_post_message(&shellHandle->commontaskMsg);
244 #else
245 (void)OSA_EventSet((osa_event_handle_t)shellHandle->event, SHELL_EVENT_DATA_ARRIVED);
246 #endif
247
248 #else
249 SHELL_Task(shellHandle);
250 #endif
251 }
252 }
253 #endif
254
SHELL_WriteBuffer(char * buffer,int32_t * indicator,char val,int len)255 static void SHELL_WriteBuffer(char *buffer, int32_t *indicator, char val, int len)
256 {
257 shell_context_handle_t *shellContextHandle;
258 int i = 0;
259 shellContextHandle = (shell_context_handle_t *)(void *)buffer;
260
261 for (i = 0; i < len; i++)
262 {
263 if ((*indicator + 1) >= (int32_t)SHELL_SPRINTF_BUFFER_SIZE)
264 {
265 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
266 if (NULL == shellContextHandle->serialHandle)
267 {
268 for (uint32_t index = 0; index < ((uint32_t)*indicator); index++)
269 {
270 (void)putchar(shellContextHandle->printBuffer[index]);
271 }
272 }
273 else
274 #endif
275 {
276 (void)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle,
277 (uint8_t *)shellContextHandle->printBuffer, (uint32_t)*indicator);
278 }
279
280 shellContextHandle->printLength += (uint32_t)*indicator;
281 *indicator = 0;
282 }
283
284 shellContextHandle->printBuffer[*indicator] = val;
285 (*indicator)++;
286 }
287 }
288
SHELL_Sprintf(void * buffer,const char * formatString,va_list ap)289 static int SHELL_Sprintf(void *buffer, const char *formatString, va_list ap)
290 {
291 shell_context_handle_t *shellContextHandle;
292 uint32_t length;
293 shellContextHandle = (shell_context_handle_t *)buffer;
294
295 length = (uint32_t)StrFormatPrintf(formatString, ap, (char *)buffer, SHELL_WriteBuffer);
296 shellContextHandle->printLength += length;
297 return (int32_t)length;
298 }
299
SHELL_hisOperation(uint8_t ch,shell_context_handle_t * shellContextHandle)300 static void SHELL_hisOperation(uint8_t ch, shell_context_handle_t *shellContextHandle)
301 {
302 switch ((char)ch)
303 {
304 /* History operation here */
305 case 'A': /* Up key */
306 SHELL_GetHistoryCommand(shellContextHandle, (uint8_t)shellContextHandle->hist_current);
307 if (shellContextHandle->hist_current < (shellContextHandle->hist_count - 1U))
308 {
309 shellContextHandle->hist_current++;
310 }
311 break;
312 case 'B': /* Down key */
313 SHELL_GetHistoryCommand(shellContextHandle, (uint8_t)shellContextHandle->hist_current);
314 if (shellContextHandle->hist_current > 0U)
315 {
316 shellContextHandle->hist_current--;
317 }
318 break;
319 case 'D': /* Left key */
320 if ((bool)shellContextHandle->c_pos)
321 {
322 (void)SHELL_WRITEX(shellContextHandle, "\b", 1);
323 shellContextHandle->c_pos--;
324 }
325 break;
326 case 'C': /* Right key */
327 if (shellContextHandle->c_pos < shellContextHandle->l_pos)
328 {
329 (void)SHELL_WRITEX(shellContextHandle, &shellContextHandle->line[shellContextHandle->c_pos], 1);
330 shellContextHandle->c_pos++;
331 }
332 break;
333 default:
334 /* MISRA C-2012 Rule 16.4 */
335 break;
336 }
337 }
338 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
SHELL_Task(void * param)339 static void SHELL_Task(void *param)
340 #else
341 void SHELL_Task(shell_handle_t shellHandle)
342 #endif
343 {
344 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
345 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)param;
346 #else
347 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
348 #endif
349 uint8_t ch;
350
351 if (NULL != shellContextHandle)
352 {
353 uint32_t osaCurrentSr = 0U;
354
355 osaCurrentSr = DisableGlobalIRQ();
356 shellContextHandle->notificationPost = 0U;
357 if (shellContextHandle->taskBusy > 0U)
358 {
359 EnableGlobalIRQ(osaCurrentSr);
360 return;
361 }
362 shellContextHandle->taskBusy = 1U;
363 EnableGlobalIRQ(osaCurrentSr);
364 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
365
366 #if defined(OSA_USED)
367
368 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
369 #else
370 osa_event_flags_t ev = 0;
371
372 do
373 {
374 if (KOSA_StatusSuccess == OSA_EventWait((osa_event_handle_t)shellContextHandle->event, osaEventFlagsAll_c,
375 0U, osaWaitForever_c, &ev))
376 {
377 if (0U != (ev & SHELL_EVENT_DATA_ARRIVED))
378 #endif
379
380 #endif
381
382 #endif
383 {
384 shellContextHandle->notificationPost = 0U;
385 do
386 {
387 if ((bool)shellContextHandle->exit)
388 {
389 if (shellContextHandle->serialReadHandle != NULL)
390 {
391 (void)SerialManager_CloseReadHandle(shellContextHandle->serialReadHandle);
392 shellContextHandle->serialReadHandle = NULL;
393 }
394 if (shellContextHandle->serialWriteHandle != NULL)
395 {
396 (void)SerialManager_CloseWriteHandle(shellContextHandle->serialWriteHandle);
397 shellContextHandle->serialWriteHandle = NULL;
398 }
399 break;
400 }
401 if (kStatus_SHELL_Success != (shell_status_t)SHELL_GetChar(shellContextHandle, &ch))
402 {
403 /* If error occurred when getting a char, exit the task and waiting the new data arriving. */
404 break;
405 }
406
407 /* Special key */
408 if (ch == KEY_ESC)
409 {
410 shellContextHandle->stat = kSHELL_Special;
411 continue;
412 }
413 else if (shellContextHandle->stat == kSHELL_Special)
414 {
415 /* Function key */
416 if ((char)ch == '[')
417 {
418 shellContextHandle->stat = kSHELL_Function;
419 continue;
420 }
421 shellContextHandle->stat = kSHELL_Normal;
422 }
423 else if (shellContextHandle->stat == kSHELL_Function)
424 {
425 shellContextHandle->stat = kSHELL_Normal;
426 SHELL_hisOperation(ch, shellContextHandle);
427 continue;
428 }
429 /* Handle tab key */
430 else if ((char)ch == '\t')
431 {
432 #if SHELL_AUTO_COMPLETE
433 /* Move the cursor to the beginning of line */
434 uint32_t i;
435 for (i = 0; i < (uint32_t)shellContextHandle->c_pos; i++)
436 {
437 (void)SHELL_WRITEX(shellContextHandle, "\b", 1);
438 }
439 /* Do auto complete */
440 SHELL_AutoComplete(shellContextHandle);
441 /* Move position to end */
442 shellContextHandle->l_pos = (uint8_t)strlen(shellContextHandle->line);
443 shellContextHandle->c_pos = shellContextHandle->l_pos;
444 #endif
445 continue;
446 }
447 /* Handle backspace key */
448 else if ((ch == KET_DEL) || ((char)ch == '\b'))
449 {
450 /* There must be at last one char */
451 if (shellContextHandle->c_pos == 0U)
452 {
453 continue;
454 }
455
456 shellContextHandle->l_pos--;
457 shellContextHandle->c_pos--;
458
459 if (shellContextHandle->l_pos > shellContextHandle->c_pos)
460 {
461 (void)memmove(&shellContextHandle->line[shellContextHandle->c_pos],
462 &shellContextHandle->line[shellContextHandle->c_pos + 1U],
463 (uint32_t)shellContextHandle->l_pos - (uint32_t)shellContextHandle->c_pos);
464 shellContextHandle->line[shellContextHandle->l_pos] = '\0';
465 (void)SHELL_WRITEX(shellContextHandle, "\b", 1);
466 (void)SHELL_WRITEX(shellContextHandle, &shellContextHandle->line[shellContextHandle->c_pos],
467 strlen(&shellContextHandle->line[shellContextHandle->c_pos]));
468 (void)SHELL_WRITEX(shellContextHandle, " \b", 3);
469
470 /* Reset position */
471 uint32_t i;
472 for (i = (uint32_t)shellContextHandle->c_pos; i <= (uint32_t)shellContextHandle->l_pos; i++)
473 {
474 (void)SHELL_WRITEX(shellContextHandle, "\b", 1);
475 }
476 }
477 else /* Normal backspace operation */
478 {
479 (void)SHELL_WRITEX(shellContextHandle, "\b \b", 3);
480 shellContextHandle->line[shellContextHandle->l_pos] = '\0';
481 }
482 continue;
483 }
484 else
485 {
486 /* MISRA C-2012 Rule 15.7 */
487 }
488
489 /* Input too long */
490 if (shellContextHandle->l_pos >= (SHELL_BUFFER_SIZE - 1U))
491 {
492 shellContextHandle->l_pos = 0U;
493 }
494
495 /* Handle end of line, break */
496 if (((char)ch == '\r') || ((char)ch == '\n'))
497 {
498 static char endoflinechar = '\0';
499
500 if (((uint8_t)endoflinechar != 0U) && ((uint8_t)endoflinechar != ch))
501 {
502 continue;
503 }
504 else
505 {
506 endoflinechar = (char)ch;
507 /* Print new line. */
508 (void)SHELL_WRITEX(shellContextHandle, "\r\n", 2U);
509 /* If command line is not NULL, will start process it. */
510 if (0U != strlen(shellContextHandle->line))
511 {
512 SHELL_ProcessCommand(shellContextHandle, shellContextHandle->line);
513 }
514 /* Print prompt. */
515 (void)SHELL_WRITEX(shellContextHandle, shellContextHandle->prompt,
516 strlen(shellContextHandle->prompt));
517 /* Reset all params */
518 shellContextHandle->c_pos = shellContextHandle->l_pos = 0;
519 shellContextHandle->hist_current = 0;
520 (void)memset(shellContextHandle->line, 0, sizeof(shellContextHandle->line));
521 continue;
522 }
523 }
524
525 /* Normal character */
526 if (shellContextHandle->c_pos < shellContextHandle->l_pos)
527 {
528 (void)memmove(&shellContextHandle->line[shellContextHandle->c_pos + 1U],
529 &shellContextHandle->line[shellContextHandle->c_pos],
530 (uint32_t)shellContextHandle->l_pos - (uint32_t)shellContextHandle->c_pos);
531 shellContextHandle->line[shellContextHandle->c_pos] = (char)ch;
532 (void)SHELL_WRITEX(shellContextHandle, &shellContextHandle->line[shellContextHandle->c_pos],
533 strlen(&shellContextHandle->line[shellContextHandle->c_pos]));
534 /* Move the cursor to new position */
535 uint32_t i;
536 for (i = (uint32_t)shellContextHandle->c_pos; i < (uint32_t)shellContextHandle->l_pos; i++)
537 {
538 (void)SHELL_WRITEX(shellContextHandle, "\b", 1);
539 }
540 }
541 else
542 {
543 shellContextHandle->line[shellContextHandle->l_pos] = (char)ch;
544 (void)SHELL_WRITEX(shellContextHandle, &shellContextHandle->line[shellContextHandle->l_pos], 1);
545 }
546
547 ch = 0;
548 shellContextHandle->l_pos++;
549 shellContextHandle->c_pos++;
550 } while (0U == shellContextHandle->exit);
551 }
552 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
553
554 #if defined(OSA_USED)
555
556 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
557 #else
558 }
559 } while (1U == gUseRtos_c); /* USE_RTOS = 0 for BareMetal and 1 for OS */
560 #endif
561
562 #endif
563
564 #endif
565 osaCurrentSr = DisableGlobalIRQ();
566 shellContextHandle->taskBusy = 0U;
567 EnableGlobalIRQ(osaCurrentSr);
568 }
569 }
570
SHELL_HelpCommand(shell_handle_t shellHandle,int32_t argc,char ** argv)571 static shell_status_t SHELL_HelpCommand(shell_handle_t shellHandle, int32_t argc, char **argv)
572 {
573 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
574 shell_command_t *shellCommandContextHandle;
575 list_element_handle_t p = LIST_GetHead(&shellContextHandle->commandContextListHead);
576
577 while (p != NULL)
578 {
579 shellCommandContextHandle = SHEEL_COMMAND_POINTER(p);
580 if ((shellCommandContextHandle->pcHelpString != NULL) && (bool)strlen(shellCommandContextHandle->pcHelpString))
581 {
582 (void)SHELL_WRITEX(shellContextHandle, shellCommandContextHandle->pcHelpString,
583 strlen(shellCommandContextHandle->pcHelpString));
584 }
585
586 p = LIST_GetNext(p);
587 }
588 return kStatus_SHELL_Success;
589 }
590
SHELL_ExitCommand(shell_handle_t shellHandle,int32_t argc,char ** argv)591 static shell_status_t SHELL_ExitCommand(shell_handle_t shellHandle, int32_t argc, char **argv)
592 {
593 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
594 /* Skip warning */
595 (void)SHELL_WRITEX(shellContextHandle, "\r\nSHELL exited\r\n", strlen("\r\nSHELL exited\r\n"));
596 shellContextHandle->exit = (uint8_t) true;
597 return kStatus_SHELL_Success;
598 }
599
SHELL_ProcessCommand(shell_context_handle_t * shellContextHandle,const char * cmd)600 static void SHELL_ProcessCommand(shell_context_handle_t *shellContextHandle, const char *cmd)
601 {
602 shell_command_t *tmpCommand = NULL;
603 const char *tmpCommandString;
604 int32_t argc;
605 char *argv[SHELL_BUFFER_SIZE] = {0};
606 list_element_handle_t p;
607 uint8_t flag = 1;
608 uint8_t tmpCommandLen;
609 uint8_t tmpLen;
610 uint8_t i = 0;
611
612 tmpLen = (uint8_t)strlen(cmd);
613 argc = SHELL_ParseLine(cmd, tmpLen, argv);
614
615 if ((argc > 0))
616 {
617 p = LIST_GetHead(&shellContextHandle->commandContextListHead);
618 while (p != NULL)
619 {
620 tmpCommand = SHEEL_COMMAND_POINTER(p);
621 tmpCommandString = tmpCommand->pcCommand;
622 tmpCommandLen = (uint8_t)strlen(tmpCommandString);
623 /* Compare with space or end of string */
624 if ((cmd[tmpCommandLen] == ' ') || (cmd[tmpCommandLen] == (char)0x00))
625 {
626 if (SHELL_StringCompare(tmpCommandString, argv[0], (int32_t)tmpCommandLen) == 0)
627 {
628 /* support commands with optional number of parameters */
629 if (tmpCommand->cExpectedNumberOfParameters == (uint8_t)SHELL_IGNORE_PARAMETER_COUNT)
630 {
631 flag = 0;
632 }
633 else if ((tmpCommand->cExpectedNumberOfParameters == 0U) && (argc == 1))
634 {
635 flag = 0;
636 }
637 else if (tmpCommand->cExpectedNumberOfParameters > 0U)
638 {
639 if ((argc - 1) == (int32_t)tmpCommand->cExpectedNumberOfParameters)
640 {
641 flag = 0;
642 }
643 }
644 else
645 {
646 flag = 1;
647 }
648 break;
649 }
650 }
651 p = LIST_GetNext(p);
652 }
653 if (NULL == p)
654 {
655 tmpCommand = NULL;
656 }
657 }
658
659 if ((tmpCommand != NULL) && (flag == 1U))
660 {
661 (void)SHELL_Write(
662 shellContextHandle,
663 "\r\nIncorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n",
664 strlen(
665 "\r\nIncorrect command parameter(s). Enter \"help\" to view a list of available commands.\r\n\r\n"));
666 }
667 else if (tmpCommand != NULL)
668 {
669 tmpLen = (uint8_t)strlen(cmd);
670 /* Compare with last command. Push back to history buffer if different */
671 if (tmpLen != (uint8_t)SHELL_StringCompare(cmd, shellContextHandle->hist_buf[0], (int32_t)strlen(cmd)))
672 {
673 for (i = SHELL_HISTORY_COUNT - 1U; i > 0U; i--)
674 {
675 (void)memset(shellContextHandle->hist_buf[i], (int)'\0', SHELL_BUFFER_SIZE);
676 tmpLen = (uint8_t)strlen(shellContextHandle->hist_buf[i - 1U]);
677 (void)memcpy(shellContextHandle->hist_buf[i], shellContextHandle->hist_buf[i - 1U], tmpLen);
678 }
679 (void)memset(shellContextHandle->hist_buf[0], (int)'\0', SHELL_BUFFER_SIZE);
680 tmpLen = (uint8_t)strlen(cmd);
681 (void)memcpy(shellContextHandle->hist_buf[0], cmd, tmpLen);
682 if (shellContextHandle->hist_count < SHELL_HISTORY_COUNT)
683 {
684 shellContextHandle->hist_count++;
685 }
686 }
687 if (kStatus_SHELL_RetUsage == tmpCommand->pFuncCallBack(shellContextHandle, argc, argv))
688 {
689 if (NULL != tmpCommand->pcHelpString)
690 {
691 (void)SHELL_WRITEX(shellContextHandle, tmpCommand->pcHelpString, strlen(tmpCommand->pcHelpString));
692 }
693 else
694 {
695 (void)SHELL_HelpCommand(shellContextHandle, 0, NULL);
696 }
697 }
698 }
699 else
700 {
701 (void)SHELL_Write(
702 shellContextHandle,
703 "\r\nCommand not recognized. Enter 'help' to view a list of available commands.\r\n\r\n",
704 strlen("\r\nCommand not recognized. Enter 'help' to view a list of available commands.\r\n\r\n"));
705 }
706 }
707
SHELL_GetHistoryCommand(shell_context_handle_t * shellContextHandle,uint8_t hist_pos)708 static void SHELL_GetHistoryCommand(shell_context_handle_t *shellContextHandle, uint8_t hist_pos)
709 {
710 uint32_t i;
711 uint32_t tmp;
712
713 if (shellContextHandle->hist_buf[0][0] == '\0')
714 {
715 shellContextHandle->hist_current = 0;
716 return;
717 }
718
719 #if 0 /*hist_pos is passed from hist_current. And hist_current is only changed in case 'A'/'B',as hist_count is 3 \
720 most, it can't be more than 3 */
721 if (hist_pos >= SHELL_HISTORY_COUNT)
722 {
723 hist_pos = SHELL_HISTORY_COUNT - 1U;
724 }
725 #endif
726
727 tmp = strlen(shellContextHandle->line);
728 /* Clear current if have */
729 if (tmp > 0U)
730 {
731 (void)memset(shellContextHandle->line, (int)'\0', tmp);
732 for (i = 0U; i < tmp; i++)
733 {
734 (void)SHELL_WRITEX(shellContextHandle, "\b \b", 3);
735 }
736 }
737
738 shellContextHandle->l_pos = (uint8_t)strlen(shellContextHandle->hist_buf[hist_pos]);
739 shellContextHandle->c_pos = shellContextHandle->l_pos;
740 (void)memcpy(shellContextHandle->line, shellContextHandle->hist_buf[hist_pos], shellContextHandle->l_pos);
741 (void)SHELL_WRITEX(shellContextHandle, shellContextHandle->hist_buf[hist_pos],
742 strlen(shellContextHandle->hist_buf[hist_pos]));
743 }
744
SHELL_AutoComplete(shell_context_handle_t * shellContextHandle)745 static void SHELL_AutoComplete(shell_context_handle_t *shellContextHandle)
746 {
747 int32_t minLen;
748 list_element_handle_t p;
749 shell_command_t *tmpCommand = NULL;
750 const char *namePtr;
751 const char *cmdName;
752
753 minLen = (int32_t)SHELL_BUFFER_SIZE;
754 namePtr = NULL;
755
756 /* Empty tab, list all commands */
757 if (shellContextHandle->line[0] == '\0')
758 {
759 (void)SHELL_HelpCommand(shellContextHandle, 0, NULL);
760 return;
761 }
762
763 (void)SHELL_WRITEX(shellContextHandle, "\r\n", 2);
764
765 /* Do auto complete */
766 p = LIST_GetHead(&shellContextHandle->commandContextListHead);
767 while (p != NULL)
768 {
769 tmpCommand = SHEEL_COMMAND_POINTER(p);
770 cmdName = tmpCommand->pcCommand;
771 if (SHELL_StringCompare(shellContextHandle->line, cmdName, (int32_t)strlen(shellContextHandle->line)) == 0)
772 {
773 /* Show possible matches */
774 (void)SHELL_Printf(shellContextHandle, "%s ", cmdName);
775 if (minLen > ((int32_t)strlen(cmdName)))
776 {
777 namePtr = cmdName;
778 minLen = (int32_t)strlen(namePtr);
779 }
780 }
781 p = LIST_GetNext(p);
782 }
783 /* Auto complete string */
784 if (namePtr != NULL)
785 {
786 (void)memcpy(shellContextHandle->line, namePtr, (uint32_t)minLen);
787 }
788 SHELL_PrintPrompt(shellContextHandle);
789 (void)SHELL_WRITEX(shellContextHandle, shellContextHandle->line, strlen(shellContextHandle->line));
790 return;
791 }
792
SHELL_StringCompare(const char * str1,const char * str2,int32_t count)793 static int32_t SHELL_StringCompare(const char *str1, const char *str2, int32_t count)
794 {
795 while ((bool)(count--))
796 {
797 if (*str1++ != *str2++)
798 {
799 return (int32_t)(*(str1 - 1) - *(str2 - 1));
800 }
801 }
802 return 0;
803 }
804
SHELL_ParseLine(const char * cmd,uint32_t len,char * argv[])805 static int32_t SHELL_ParseLine(const char *cmd, uint32_t len, char *argv[])
806 {
807 uint32_t argc;
808 char *p;
809 uint32_t position;
810
811 /* Init params */
812 (void)memset(s_paramBuffer, (int)'\0', len + 1U);
813 (void)memcpy(s_paramBuffer, cmd, len);
814
815 p = s_paramBuffer;
816 position = 0;
817 argc = 0;
818
819 while (position < len)
820 {
821 /* Skip all blanks */
822 while ((position < len) && ((char)(*p) == ' '))
823 {
824 *p = '\0';
825 p++;
826 position++;
827 }
828
829 if (position >= len)
830 {
831 break;
832 }
833
834 /* Process begin of a string */
835 if (*p == '"')
836 {
837 p++;
838 position++;
839 argv[argc] = p;
840 argc++;
841 /* Skip this string */
842 while ((*p != '"') && (position < len))
843 {
844 p++;
845 position++;
846 }
847 /* Skip '"' */
848 *p = '\0';
849 p++;
850 position++;
851 }
852 else /* Normal char */
853 {
854 argv[argc] = p;
855 argc++;
856 while (((char)*p != ' ') && (position < len))
857 {
858 p++;
859 position++;
860 }
861 }
862 }
863 return (int32_t)argc;
864 }
865
SHELL_GetChar(shell_context_handle_t * shellContextHandle,uint8_t * ch)866 static shell_status_t SHELL_GetChar(shell_context_handle_t *shellContextHandle, uint8_t *ch)
867 {
868 shell_status_t status;
869
870 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
871 if (NULL == shellContextHandle->serialHandle)
872 {
873 int ret;
874 ret = getchar();
875 if (ret > 0)
876 {
877 *ch = (uint8_t)ret;
878 status = kStatus_SHELL_Success;
879 }
880 else
881 {
882 status = kStatus_SHELL_Error;
883 }
884 }
885 else
886 #endif
887 {
888 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
889 uint32_t length = 0;
890
891 (void)SerialManager_TryRead(shellContextHandle->serialReadHandle, ch, 1, &length);
892
893 if (length > 0U)
894 {
895 status = kStatus_SHELL_Success;
896 }
897 else
898 {
899 status = kStatus_SHELL_Error;
900 }
901 #else
902 status = (shell_status_t)SerialManager_ReadBlocking(shellContextHandle->serialReadHandle, ch, 1);
903 #endif
904 }
905
906 return status;
907 }
908
SHELL_Init(shell_handle_t shellHandle,serial_handle_t serialHandle,char * prompt)909 shell_status_t SHELL_Init(shell_handle_t shellHandle, serial_handle_t serialHandle, char *prompt)
910 {
911 shell_context_handle_t *shellContextHandle;
912 serial_manager_status_t status = kStatus_SerialManager_Error;
913 (void)status;
914
915 assert(shellHandle);
916 #if !(!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
917 assert(serialHandle);
918 #endif
919 assert(prompt);
920 assert(SHELL_HANDLE_SIZE >= sizeof(shell_context_handle_t));
921
922 shellContextHandle = (shell_context_handle_t *)shellHandle;
923
924 /* memory set for shellHandle */
925 (void)memset(shellHandle, 0, SHELL_HANDLE_SIZE);
926
927 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
928 if (NULL == serialHandle)
929 {
930 }
931 else
932 #endif
933 {
934 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
935
936 #if defined(OSA_USED)
937
938 #if (defined(SHELL_USE_COMMON_TASK) && (SHELL_USE_COMMON_TASK > 0U))
939 (void)COMMON_TASK_init();
940 #else
941 if (KOSA_StatusSuccess != OSA_EventCreate((osa_event_handle_t)shellContextHandle->event, 1U))
942 {
943 return kStatus_SHELL_Error;
944 }
945
946 if (KOSA_StatusSuccess !=
947 OSA_TaskCreate((osa_task_handle_t)shellContextHandle->taskId, OSA_TASK(SHELL_Task), shellContextHandle))
948 {
949 return kStatus_SHELL_Error;
950 }
951 #endif
952
953 #endif
954
955 #endif
956 }
957
958 shellContextHandle->prompt = prompt;
959 shellContextHandle->serialHandle = serialHandle;
960
961 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
962 if (NULL == serialHandle)
963 {
964 }
965 else
966 #endif
967 {
968 shellContextHandle->serialWriteHandle = (serial_write_handle_t)&shellContextHandle->serialWriteHandleBuffer[0];
969 status = SerialManager_OpenWriteHandle(shellContextHandle->serialHandle, shellContextHandle->serialWriteHandle);
970 assert(kStatus_SerialManager_Success == status);
971
972 shellContextHandle->serialReadHandle = (serial_read_handle_t)&shellContextHandle->serialReadHandleBuffer[0];
973 status = SerialManager_OpenReadHandle(shellContextHandle->serialHandle, shellContextHandle->serialReadHandle);
974 assert(kStatus_SerialManager_Success == status);
975
976 #if (defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
977 status = SerialManager_InstallRxCallback(shellContextHandle->serialReadHandle, SHELL_SerialManagerRxCallback,
978 shellContextHandle);
979 assert(kStatus_SerialManager_Success == status);
980 #endif
981 (void)status;
982 }
983
984 (void)SHELL_RegisterCommand(shellContextHandle, SHELL_COMMAND(help));
985 (void)SHELL_RegisterCommand(shellContextHandle, SHELL_COMMAND(exit));
986 SHELL_MUTEX_CREATE();
987 (void)SHELL_Write(shellContextHandle, "\r\nCopyright 2022 NXP\r\n", strlen("\r\nCopyright 2022 NXP\r\n"));
988 SHELL_PrintPrompt(shellContextHandle);
989
990 return kStatus_SHELL_Success;
991 }
992
SHELL_RegisterCommand(shell_handle_t shellHandle,shell_command_t * shellCommand)993 shell_status_t SHELL_RegisterCommand(shell_handle_t shellHandle, shell_command_t *shellCommand)
994 {
995 shell_context_handle_t *shellContextHandle = (shell_context_handle_t *)shellHandle;
996 assert(shellHandle);
997 assert(shellCommand);
998
999 /* memory set for shellHandle */
1000 (void)memset(&shellCommand->link, 0, sizeof(shellCommand->link));
1001
1002 (void)LIST_AddTail(&shellContextHandle->commandContextListHead, &shellCommand->link);
1003
1004 return kStatus_SHELL_Success;
1005 }
1006
SHELL_UnregisterCommand(shell_command_t * shellCommand)1007 shell_status_t SHELL_UnregisterCommand(shell_command_t *shellCommand)
1008 {
1009 assert(shellCommand);
1010
1011 (void)LIST_RemoveElement(&shellCommand->link);
1012
1013 /* memory set for shellHandle */
1014 (void)memset(&shellCommand->link, 0, sizeof(shellCommand->link));
1015
1016 return kStatus_SHELL_Success;
1017 }
1018
SHELL_Write(shell_handle_t shellHandle,const char * buffer,uint32_t length)1019 shell_status_t SHELL_Write(shell_handle_t shellHandle, const char *buffer, uint32_t length)
1020 {
1021 shell_context_handle_t *shellContextHandle;
1022 uint32_t primask;
1023 shell_status_t status;
1024
1025 assert(shellHandle);
1026 assert(buffer);
1027
1028 if (!(bool)length)
1029 {
1030 return kStatus_SHELL_Success;
1031 }
1032
1033 shellContextHandle = (shell_context_handle_t *)shellHandle;
1034
1035 primask = DisableGlobalIRQ();
1036 if ((bool)shellContextHandle->printBusy)
1037 {
1038 EnableGlobalIRQ(primask);
1039 return kStatus_SHELL_Error;
1040 }
1041 shellContextHandle->printBusy = 1U;
1042 EnableGlobalIRQ(primask);
1043 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
1044 if (NULL == shellContextHandle->serialHandle)
1045 {
1046 status = kStatus_SHELL_Success;
1047 for (uint32_t index = 0; index < length; index++)
1048 {
1049 (void)putchar(buffer[index]);
1050 }
1051 }
1052 else
1053 #endif
1054 {
1055 status = (shell_status_t)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle, (uint8_t *)buffer,
1056 length);
1057 }
1058
1059 shellContextHandle->printBusy = 0U;
1060
1061 return status;
1062 }
1063
SHELL_Printf(shell_handle_t shellHandle,const char * formatString,...)1064 int SHELL_Printf(shell_handle_t shellHandle, const char *formatString, ...)
1065 {
1066 shell_context_handle_t *shellContextHandle;
1067 uint32_t length;
1068 uint32_t primask;
1069 va_list ap;
1070
1071 assert(shellHandle);
1072 assert(formatString);
1073
1074 shellContextHandle = (shell_context_handle_t *)shellHandle;
1075
1076 primask = DisableGlobalIRQ();
1077 if ((bool)shellContextHandle->printBusy)
1078 {
1079 EnableGlobalIRQ(primask);
1080 return -1;
1081 }
1082 shellContextHandle->printBusy = 1U;
1083 EnableGlobalIRQ(primask);
1084
1085 va_start(ap, formatString);
1086
1087 shellContextHandle->printLength = 0U;
1088 length = (uint32_t)SHELL_Sprintf(shellHandle, formatString, ap);
1089 #if (!defined(SDK_DEBUGCONSOLE_UART) && (defined(SDK_DEBUGCONSOLE) && (SDK_DEBUGCONSOLE != 1)))
1090 if (NULL == shellContextHandle->serialHandle)
1091 {
1092 for (uint32_t index = 0; index < length; index++)
1093 {
1094 (void)putchar(shellContextHandle->printBuffer[index]);
1095 }
1096 }
1097 else
1098 #endif
1099 {
1100 (void)SerialManager_WriteBlocking(shellContextHandle->serialWriteHandle,
1101 (uint8_t *)shellContextHandle->printBuffer, length);
1102 }
1103 va_end(ap);
1104
1105 shellContextHandle->printBusy = 0U;
1106 return (int32_t)shellContextHandle->printLength;
1107 }
1108
SHELL_WriteSynchronization(shell_handle_t shellHandle,const char * buffer,uint32_t length)1109 shell_status_t SHELL_WriteSynchronization(shell_handle_t shellHandle, const char *buffer, uint32_t length)
1110 {
1111 shell_status_t status;
1112
1113 assert(SHELL_checkRunningInIsr() == false);
1114
1115 SHELL_ENTER_CRITICAL();
1116 status = SHELL_Write(shellHandle, buffer, length);
1117
1118 SHELL_EXIT_CRITICAL();
1119
1120 return status;
1121 }
1122
SHELL_PrintfSynchronization(shell_handle_t shellHandle,const char * formatString,...)1123 int SHELL_PrintfSynchronization(shell_handle_t shellHandle, const char *formatString, ...)
1124 {
1125 shell_status_t status;
1126 shell_context_handle_t *shellContextHandle;
1127 va_list ap;
1128 uint32_t length;
1129
1130 assert(SHELL_checkRunningInIsr() == false);
1131
1132 shellContextHandle = (shell_context_handle_t *)shellHandle;
1133
1134 SHELL_ENTER_CRITICAL();
1135 va_start(ap, formatString);
1136 length = (uint32_t)SHELL_Sprintf(shellHandle, formatString, ap);
1137
1138 status = SHELL_Write(shellHandle, (const char *)shellContextHandle->printBuffer, length);
1139 va_end(ap);
1140 SHELL_EXIT_CRITICAL();
1141
1142 return (status == kStatus_SHELL_Success) ? (int)length : 0;
1143 }
SHELL_ChangePrompt(shell_handle_t shellHandle,char * prompt)1144 void SHELL_ChangePrompt(shell_handle_t shellHandle, char *prompt)
1145 {
1146 shell_context_handle_t *shellContextHandle;
1147 assert(shellHandle);
1148 assert(prompt);
1149
1150 shellContextHandle = (shell_context_handle_t *)shellHandle;
1151
1152 shellContextHandle->prompt = prompt;
1153 SHELL_PrintPrompt(shellContextHandle);
1154 }
1155
SHELL_PrintPrompt(shell_handle_t shellHandle)1156 void SHELL_PrintPrompt(shell_handle_t shellHandle)
1157 {
1158 shell_context_handle_t *shellContextHandle;
1159 assert(shellHandle);
1160
1161 shellContextHandle = (shell_context_handle_t *)shellHandle;
1162
1163 (void)SHELL_Write(shellContextHandle, "\r\n", 2U);
1164 (void)SHELL_Write(shellContextHandle, shellContextHandle->prompt, strlen(shellContextHandle->prompt));
1165 }
1166