fsl_shell.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _FSL_SHELL_H_
  35. #define _FSL_SHELL_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup SHELL
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @brief Macro to set on/off history feature. */
  45. #ifndef SHELL_USE_HISTORY
  46. #define SHELL_USE_HISTORY (0U)
  47. #endif
  48. /*! @brief Macro to set on/off history feature. */
  49. #ifndef SHELL_SEARCH_IN_HIST
  50. #define SHELL_SEARCH_IN_HIST (1U)
  51. #endif
  52. /*! @brief Macro to select method stream. */
  53. #ifndef SHELL_USE_FILE_STREAM
  54. #define SHELL_USE_FILE_STREAM (0U)
  55. #endif
  56. /*! @brief Macro to set on/off auto-complete feature. */
  57. #ifndef SHELL_AUTO_COMPLETE
  58. #define SHELL_AUTO_COMPLETE (1U)
  59. #endif
  60. /*! @brief Macro to set console buffer size. */
  61. #ifndef SHELL_BUFFER_SIZE
  62. #define SHELL_BUFFER_SIZE (64U)
  63. #endif
  64. /*! @brief Macro to set maximum arguments in command. */
  65. #ifndef SHELL_MAX_ARGS
  66. #define SHELL_MAX_ARGS (8U)
  67. #endif
  68. /*! @brief Macro to set maximum count of history commands. */
  69. #ifndef SHELL_HIST_MAX
  70. #define SHELL_HIST_MAX (3U)
  71. #endif
  72. /*! @brief Macro to set maximum count of commands. */
  73. #ifndef SHELL_MAX_CMD
  74. #define SHELL_MAX_CMD (20U)
  75. #endif
  76. /*! @brief Macro to bypass arguments check */
  77. #define SHELL_OPTIONAL_PARAMS (0xFF)
  78. /*! @brief Shell user send data callback prototype.*/
  79. typedef void (*send_data_cb_t)(uint8_t *buf, uint32_t len);
  80. /*! @brief Shell user receiver data callback prototype.*/
  81. typedef void (*recv_data_cb_t)(uint8_t *buf, uint32_t len);
  82. /*! @brief Shell user printf data prototype.*/
  83. typedef int (*printf_data_t)(const char *format, ...);
  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. /*! @brief Data structure for Shell environment. */
  92. typedef struct _shell_context_struct
  93. {
  94. char *prompt; /*!< Prompt string */
  95. enum _fun_key_status stat; /*!< Special key status */
  96. char line[SHELL_BUFFER_SIZE]; /*!< Consult buffer */
  97. uint8_t cmd_num; /*!< Number of user commands */
  98. uint8_t l_pos; /*!< Total line position */
  99. uint8_t c_pos; /*!< Current line position */
  100. #if SHELL_USE_FILE_STREAM
  101. FILE *STDOUT, *STDIN, *STDERR;
  102. #else
  103. send_data_cb_t send_data_func; /*!< Send data interface operation */
  104. recv_data_cb_t recv_data_func; /*!< Receive data interface operation */
  105. printf_data_t printf_data_func;
  106. #endif
  107. uint16_t hist_current; /*!< Current history command in hist buff*/
  108. uint16_t hist_count; /*!< Total history command in hist buff*/
  109. char hist_buf[SHELL_HIST_MAX][SHELL_BUFFER_SIZE]; /*!< History buffer*/
  110. bool exit; /*!< Exit Flag*/
  111. } shell_context_struct, *p_shell_context_t;
  112. /*! @brief User command function prototype. */
  113. typedef int32_t (*cmd_function_t)(p_shell_context_t context, int32_t argc, char **argv);
  114. /*! @brief User command data structure. */
  115. typedef struct _shell_command_context
  116. {
  117. const char *pcCommand; /*!< The command that is executed. For example "help". It must be all lower case. */
  118. char *pcHelpString; /*!< String that describes how to use the command. It should start with the command itself,
  119. and end with "\r\n". For example "help: Returns a list of all the commands\r\n". */
  120. const cmd_function_t
  121. pFuncCallBack; /*!< A pointer to the callback function that returns the output generated by the command. */
  122. uint8_t cExpectedNumberOfParameters; /*!< Commands expect a fixed number of parameters, which may be zero. */
  123. } shell_command_context_t;
  124. /*! @brief Structure list command. */
  125. typedef struct _shell_command_context_list
  126. {
  127. const shell_command_context_t *CommandList[SHELL_MAX_CMD]; /*!< The command table list */
  128. uint8_t numberOfCommandInList; /*!< The total command in list */
  129. } shell_command_context_list_t;
  130. /*******************************************************************************
  131. * API
  132. ******************************************************************************/
  133. #if defined(__cplusplus)
  134. extern "C" {
  135. #endif /* _cplusplus */
  136. /*!
  137. * @name Shell functional operation
  138. * @{
  139. */
  140. /*!
  141. * @brief Enables the clock gate and configures the Shell module according to the configuration structure.
  142. *
  143. * This function must be called before calling all other Shell functions.
  144. * Call operation the Shell commands with user-defined settings.
  145. * The example below shows how to set up the middleware Shell and
  146. * how to call the SHELL_Init function by passing in these parameters.
  147. * This is an example.
  148. * @code
  149. * shell_context_struct user_context;
  150. * SHELL_Init(&user_context, SendDataFunc, ReceiveDataFunc, "SHELL>> ");
  151. * @endcode
  152. * @param context The pointer to the Shell environment and runtime states.
  153. * @param send_cb The pointer to call back send data function.
  154. * @param recv_cb The pointer to call back receive data function.
  155. * @param prompt The string prompt of Shell
  156. */
  157. void SHELL_Init(p_shell_context_t context,
  158. send_data_cb_t send_cb,
  159. recv_data_cb_t recv_cb,
  160. printf_data_t shell_printf,
  161. char *prompt);
  162. /*!
  163. * @brief Shell register command.
  164. * @param command_context The pointer to the command data structure.
  165. * @return -1 if error or 0 if success
  166. */
  167. int32_t SHELL_RegisterCommand(const shell_command_context_t *command_context);
  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. #if defined(__cplusplus)
  178. }
  179. #endif
  180. /*! @}*/
  181. #endif /* _FSL_SHELL_H_ */