fsl_shell.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef __FSL_SHELL_H__
  9. #define __FSL_SHELL_H__
  10. /*!
  11. * @addtogroup SHELL
  12. * @{
  13. */
  14. #include "fsl_common.h"
  15. #include "serial_manager.h"
  16. #include "generic_list.h"
  17. /*******************************************************************************
  18. * Definitions
  19. ******************************************************************************/
  20. /*! @brief Whether use non-blocking mode. */
  21. #define SHELL_NON_BLOCKING_MODE SERIAL_MANAGER_NON_BLOCKING_MODE
  22. /*! @brief Macro to set on/off auto-complete feature. */
  23. #define SHELL_AUTO_COMPLETE (1U)
  24. /*! @brief Macro to set console buffer size. */
  25. #define SHELL_BUFFER_SIZE (64U)
  26. /*! @brief Macro to set maximum arguments in command. */
  27. #define SHELL_MAX_ARGS (8U)
  28. /*! @brief Macro to set maximum count of history commands. */
  29. #define SHELL_HISTORY_COUNT (3U)
  30. /*! @brief Macro to bypass arguments check */
  31. #define SHELL_IGNORE_PARAMETER_COUNT (0xFF)
  32. /*! @brief The handle size of the shell module. It is the sum of the SHELL_HISTORY_COUNT * SHELL_BUFFER_SIZE +
  33. * SHELL_BUFFER_SIZE + SERIAL_MANAGER_READ_HANDLE_SIZE + SERIAL_MANAGER_WRITE_HANDLE_SIZE*/
  34. #define SHELL_HANDLE_SIZE (520U)
  35. #define SHELL_USE_COMMON_TASK (1U)
  36. #define SHELL_TASK_PRIORITY (2U)
  37. #define SHELL_TASK_STACK_SIZE (1000U)
  38. typedef enum _shell_status
  39. {
  40. kStatus_SHELL_Success = kStatus_Success, /*!< Success */
  41. kStatus_SHELL_Error = MAKE_STATUS(kStatusGroup_SHELL, 1), /*!< Failed */
  42. kStatus_SHELL_OpenWriteHandleFailed = MAKE_STATUS(kStatusGroup_SHELL, 2), /*!< Open write handle failed */
  43. kStatus_SHELL_OpenReadHandleFailed = MAKE_STATUS(kStatusGroup_SHELL, 3), /*!< Open read handle failed */
  44. } shell_status_t;
  45. /*! @brief The handle of the shell module */
  46. typedef void *shell_handle_t;
  47. /*! @brief User command function prototype. */
  48. typedef shell_status_t (*cmd_function_t)(shell_handle_t shellHandle, int32_t argc, char **argv);
  49. /*! @brief User command data configuration structure. */
  50. typedef struct _shell_command
  51. {
  52. const char *pcCommand; /*!< The command that is executed. For example "help". It must be all lower case. */
  53. char *pcHelpString; /*!< String that describes how to use the command. It should start with the command itself,
  54. and end with "\r\n". For example "help: Returns a list of all the commands\r\n". */
  55. const cmd_function_t
  56. pFuncCallBack; /*!< A pointer to the callback function that returns the output generated by the command. */
  57. uint8_t cExpectedNumberOfParameters; /*!< Commands expect a fixed number of parameters, which may be zero. */
  58. list_element_t link; /*!< link of the element */
  59. } shell_command_t;
  60. #if defined(__ICCARM__)
  61. /* disable misra 19.13 */
  62. _Pragma("diag_suppress=Pm120")
  63. #endif
  64. /*!
  65. * @brief Defines the shell command structure
  66. *
  67. * This macro is used to define the shell command structure #shell_command_t.
  68. * And then uses the macro SHELL_COMMAND to get the command structure pointer.
  69. * The macro should not be used in any function.
  70. *
  71. * This is a example,
  72. * @code
  73. * SHELL_COMMAND_DEFINE(exit, "\r\n\"exit\": Exit program\r\n", SHELL_ExitCommand, 0);
  74. * SHELL_RegisterCommand(s_shellHandle, SHELL_COMMAND(exit));
  75. * @endcode
  76. *
  77. * @param command The command string of the command. The double quotes do not need. Such as exit for "exit",
  78. * help for "Help", read for "read".
  79. * @param descriptor The description of the command is used for showing the command usage when "help" is typing.
  80. * @param callback The callback of the command is used to handle the command line when the input command is matched.
  81. * @param paramCount The max parameter count of the current command.
  82. */
  83. #define SHELL_COMMAND_DEFINE(command, descriptor, callback, paramCount) \
  84. \
  85. shell_command_t g_shellCommand##command = { \
  86. (#command), (descriptor), (callback), (paramCount), {0}, \
  87. }
  88. /*!
  89. * @brief Gets the shell command pointer
  90. *
  91. * This macro is used to get the shell command pointer. The macro should not be used before the macro
  92. * SHELL_COMMAND_DEFINE is used.
  93. *
  94. * @param command The command string of the command. The double quotes do not need. Such as exit for "exit",
  95. * help for "Help", read for "read".
  96. */
  97. #define SHELL_COMMAND(command) &g_shellCommand##command
  98. #if defined(__ICCARM__)
  99. _Pragma("diag_default=Pm120")
  100. #endif
  101. /*******************************************************************************
  102. * API
  103. ******************************************************************************/
  104. #if defined(__cplusplus)
  105. extern "C"
  106. {
  107. #endif /* _cplusplus */
  108. /*!
  109. * @name Shell functional operation
  110. * @{
  111. */
  112. /*!
  113. * @brief Initializes the shell module
  114. *
  115. * This function must be called before calling all other Shell functions.
  116. * Call operation the Shell commands with user-defined settings.
  117. * The example below shows how to set up the Shell and
  118. * how to call the SHELL_Init function by passing in these parameters.
  119. * This is an example.
  120. * @code
  121. * static uint8_t s_shellHandleBuffer[SHELL_HANDLE_SIZE];
  122. * static shell_handle_t s_shellHandle = &s_shellHandleBuffer[0];
  123. * SHELL_Init(s_shellHandle, s_serialHandle, "Test@SHELL>");
  124. * @endcode
  125. * @param shellHandle Pointer to point to a memory space of size #SHELL_HANDLE_SIZE allocated by the caller.
  126. * @param serialHandle The serial manager module handle pointer.
  127. * @param prompt The string prompt pointer of Shell. Only the global variable can be passed.
  128. * @retval kStatus_SHELL_Success The shell initialization succeed.
  129. * @retval kStatus_SHELL_Error An error occurred when the shell is initialized.
  130. * @retval kStatus_SHELL_OpenWriteHandleFailed Open the write handle failed.
  131. * @retval kStatus_SHELL_OpenReadHandleFailed Open the read handle failed.
  132. */
  133. shell_status_t SHELL_Init(shell_handle_t shellHandle, serial_handle_t serialHandle, char *prompt);
  134. /*!
  135. * @brief Registers the shell command
  136. *
  137. * This function is used to register the shell command by using the command configuration #shell_command_config_t.
  138. * This is a example,
  139. * @code
  140. * SHELL_COMMAND_DEFINE(exit, "\r\n\"exit\": Exit program\r\n", SHELL_ExitCommand, 0);
  141. * SHELL_RegisterCommand(s_shellHandle, SHELL_COMMAND(exit));
  142. * @endcode
  143. * @param shellHandle The shell module handle pointer.
  144. * @param command The command element.
  145. * @retval kStatus_SHELL_Success Successfully register the command.
  146. * @retval kStatus_SHELL_Error An error occurred.
  147. */
  148. shell_status_t SHELL_RegisterCommand(shell_handle_t shellHandle, shell_command_t * command);
  149. /*!
  150. * @brief Unregisters the shell command
  151. *
  152. * This function is used to unregister the shell command.
  153. *
  154. * @param command The command element.
  155. * @retval kStatus_SHELL_Success Successfully unregister the command.
  156. */
  157. shell_status_t SHELL_UnregisterCommand(shell_command_t * command);
  158. /*!
  159. * @brief Sends data to the shell output stream.
  160. *
  161. * This function is used to send data to the shell output stream.
  162. *
  163. * @param shellHandle The shell module handle pointer.
  164. * @param buffer Start address of the data to write.
  165. * @param length Length of the data to write.
  166. * @retval kStatus_SHELL_Success Successfully send data.
  167. * @retval kStatus_SHELL_Error An error occurred.
  168. */
  169. shell_status_t SHELL_Write(shell_handle_t shellHandle, char *buffer, uint32_t length);
  170. /*!
  171. * @brief Writes formatted output to the shell output stream.
  172. *
  173. * Call this function to write a formatted output to the shell output stream.
  174. *
  175. * @param shellHandle The shell module handle pointer.
  176. *
  177. * @param formatString Format string.
  178. * @return Returns the number of characters printed or a negative value if an error occurs.
  179. */
  180. int SHELL_Printf(shell_handle_t shellHandle, const char *formatString, ...);
  181. /*!
  182. * @brief The task function for Shell.
  183. * The task function for Shell; The function should be polled by upper layer.
  184. * This function does not return until Shell command exit was called.
  185. *
  186. * @param shellHandle The shell module handle pointer.
  187. */
  188. #if !(defined(SHELL_NON_BLOCKING_MODE) && (SHELL_NON_BLOCKING_MODE > 0U))
  189. void SHELL_Task(shell_handle_t shellHandle);
  190. #endif
  191. /* @} */
  192. #if defined(__cplusplus)
  193. }
  194. #endif
  195. /*! @}*/
  196. #endif /* __FSL_SHELL_H__ */