1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * Copyright 2016-2017 NXP
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _FSL_SHELL_H_
10 #define _FSL_SHELL_H_
11 
12 #include "fsl_common.h"
13 
14 /*!
15  * @addtogroup SHELL
16  * @{
17  */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 /*! @brief Macro to set on/off history feature. */
23 #ifndef SHELL_USE_HISTORY
24 #define SHELL_USE_HISTORY (0U)
25 #endif
26 
27 /*! @brief Macro to set on/off history feature. */
28 #ifndef SHELL_SEARCH_IN_HIST
29 #define SHELL_SEARCH_IN_HIST (1U)
30 #endif
31 
32 /*! @brief Macro to select method stream. */
33 #ifndef SHELL_USE_FILE_STREAM
34 #define SHELL_USE_FILE_STREAM (0U)
35 #endif
36 
37 /*! @brief Macro to set on/off auto-complete feature. */
38 #ifndef SHELL_AUTO_COMPLETE
39 #define SHELL_AUTO_COMPLETE (1U)
40 #endif
41 
42 /*! @brief Macro to set console buffer size. */
43 #ifndef SHELL_BUFFER_SIZE
44 #define SHELL_BUFFER_SIZE (64U)
45 #endif
46 
47 /*! @brief Macro to set maximum arguments in command. */
48 #ifndef SHELL_MAX_ARGS
49 #define SHELL_MAX_ARGS (8U)
50 #endif
51 
52 /*! @brief Macro to set maximum count of history commands. */
53 #ifndef SHELL_HIST_MAX
54 #define SHELL_HIST_MAX (3U)
55 #endif
56 
57 /*! @brief Macro to set maximum count of commands. */
58 #ifndef SHELL_MAX_CMD
59 #define SHELL_MAX_CMD (20U)
60 #endif
61 
62 /*! @brief Macro to bypass arguments check */
63 #define SHELL_OPTIONAL_PARAMS (0xFF)
64 
65 /*! @brief Shell user send data callback prototype.*/
66 typedef void (*send_data_cb_t)(uint8_t *buf, uint32_t len);
67 
68 /*! @brief Shell user receiver data callback prototype.*/
69 typedef void (*recv_data_cb_t)(uint8_t *buf, uint32_t len);
70 
71 /*! @brief Shell user printf data prototype.*/
72 typedef int (*printf_data_t)(const char *format, ...);
73 
74 /*! @brief A type for the handle special key. */
75 typedef enum _fun_key_status
76 {
77     kSHELL_Normal = 0U,   /*!< Normal key */
78     kSHELL_Special = 1U,  /*!< Special key */
79     kSHELL_Function = 2U, /*!< Function key */
80 } fun_key_status_t;
81 
82 /*! @brief Data structure for Shell environment. */
83 typedef struct _shell_context_struct
84 {
85     char *prompt;                 /*!< Prompt string */
86     enum _fun_key_status stat;    /*!< Special key status */
87     char line[SHELL_BUFFER_SIZE]; /*!< Consult buffer */
88     uint8_t cmd_num;              /*!< Number of user commands */
89     uint8_t l_pos;                /*!< Total line position */
90     uint8_t c_pos;                /*!< Current line position */
91 #if SHELL_USE_FILE_STREAM
92     FILE *STDOUT, *STDIN, *STDERR;
93 #else
94     send_data_cb_t send_data_func; /*!< Send data interface operation */
95     recv_data_cb_t recv_data_func; /*!< Receive data interface operation */
96     printf_data_t printf_data_func;
97 #endif
98     uint16_t hist_current;                            /*!< Current history command in hist buff*/
99     uint16_t hist_count;                              /*!< Total history command in hist buff*/
100     char hist_buf[SHELL_HIST_MAX][SHELL_BUFFER_SIZE]; /*!< History buffer*/
101     bool exit;                                        /*!< Exit Flag*/
102 } shell_context_struct, *p_shell_context_t;
103 
104 /*! @brief User command function prototype. */
105 typedef int32_t (*cmd_function_t)(p_shell_context_t context, int32_t argc, char **argv);
106 
107 /*! @brief User command data structure. */
108 typedef struct _shell_command_context
109 {
110     const char *pcCommand; /*!< The command that is executed.  For example "help".  It must be all lower case. */
111     char *pcHelpString;    /*!< String that describes how to use the command.  It should start with the command itself,
112                                     and end with "\r\n".  For example "help: Returns a list of all the commands\r\n". */
113     const cmd_function_t
114         pFuncCallBack; /*!< A pointer to the callback function that returns the output generated by the command. */
115     uint8_t cExpectedNumberOfParameters; /*!< Commands expect a fixed number of parameters, which may be zero. */
116 } shell_command_context_t;
117 
118 /*! @brief Structure list command. */
119 typedef struct _shell_command_context_list
120 {
121     const shell_command_context_t *CommandList[SHELL_MAX_CMD]; /*!< The command table list */
122     uint8_t numberOfCommandInList;                             /*!< The total command in list */
123 } shell_command_context_list_t;
124 
125 /*******************************************************************************
126  * API
127  ******************************************************************************/
128 
129 #if defined(__cplusplus)
130 extern "C" {
131 #endif /* _cplusplus */
132 
133 /*!
134  * @name Shell functional operation
135  * @{
136  */
137 
138 /*!
139 * @brief Enables the clock gate and configures the Shell module according to the configuration structure.
140 *
141 * This function must be called before calling all other Shell functions.
142 * Call operation the Shell commands with user-defined settings.
143 * The example below shows how to set up the middleware Shell and
144 * how to call the SHELL_Init function by passing in these parameters.
145 * This is an example.
146 * @code
147 *   shell_context_struct user_context;
148 *   SHELL_Init(&user_context, SendDataFunc, ReceiveDataFunc, "SHELL>> ");
149 * @endcode
150 * @param context The pointer to the Shell environment and  runtime states.
151 * @param send_cb The pointer to call back send data function.
152 * @param recv_cb The pointer to call back receive data function.
153 * @param prompt  The string prompt of Shell
154 */
155 void SHELL_Init(p_shell_context_t context,
156                 send_data_cb_t send_cb,
157                 recv_data_cb_t recv_cb,
158                 printf_data_t shell_printf,
159                 char *prompt);
160 
161 /*!
162  * @brief Shell register command.
163  * @param   command_context The pointer to the command data structure.
164  * @return  -1 if error or 0 if success
165  */
166 int32_t SHELL_RegisterCommand(const shell_command_context_t *command_context);
167 
168 /*!
169  * @brief Main loop for Shell.
170  * Main loop for Shell; After this function is called, Shell begins to initialize the basic variables and starts to
171  * work.
172  * @param    context The pointer to the Shell environment and  runtime states.
173  * @return   This function does not return until Shell command exit was called.
174  */
175 int32_t SHELL_Main(p_shell_context_t context);
176 
177 /* @} */
178 
179 #if defined(__cplusplus)
180 }
181 #endif
182 
183 /*! @}*/
184 
185 #endif /* _FSL_SHELL_H_ */
186