nrf_cli.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA 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. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_CLI_H__
  41. #define NRF_CLI_H__
  42. #include "sdk_common.h"
  43. #include "nrf_cli_types.h"
  44. #include "nrf_section.h"
  45. #include "nrf_log_backend_interface.h"
  46. #include "nrf_queue.h"
  47. #include "nrf_log_ctrl.h"
  48. #include "app_util_platform.h"
  49. #include "nrf_memobj.h"
  50. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  51. #include "task_manager.h"
  52. #endif
  53. #include "nrf_fprintf.h"
  54. #include "nrf_fprintf_format.h"
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #define NRF_CLI_RX_BUFF_SIZE 16
  59. /* CLI reserves top task manager flags, bits 0...18 are available for application. */
  60. #define NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT (1UL << 19)
  61. #define NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT (1UL << 20)
  62. #define NRF_CLI_LOG_PENDING_TASK_EVT (1UL << 21)
  63. #define NRF_CLI_CMD_EXECUTE_EVT (1UL << 22)
  64. #define NRF_CLI_KILL_TASK_EVT (1UL << 23)
  65. #define NRF_CLI_TASK_EVTS (NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT | \
  66. NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT | \
  67. NRF_CLI_LOG_PENDING_TASK_EVT | \
  68. NRF_CLI_CMD_EXECUTE_EVT | \
  69. NRF_CLI_KILL_TASK_EVT)
  70. /**
  71. * @defgroup nrf_cli Command Line Interface
  72. * @ingroup app_common
  73. *
  74. * @brief Module for unified command line handling.
  75. *
  76. * @{
  77. */
  78. /**
  79. * @brief Aliases to: @ref nrf_cli, @ref nrf_cli_cmd_entry, and @ref nrf_cli_static_entry.
  80. * Must be created here to satisfy module declaration order dependencies.
  81. */
  82. typedef struct nrf_cli nrf_cli_t;
  83. typedef struct nrf_cli_cmd_entry nrf_cli_cmd_entry_t;
  84. typedef struct nrf_cli_static_entry nrf_cli_static_entry_t;
  85. /**
  86. * @brief CLI dynamic command descriptor.
  87. *
  88. * @details Function shall fill the received @ref nrf_cli_static_entry structure with requested (idx)
  89. * dynamic subcommand data. If there is more than one dynamic subcommand available,
  90. * the function shall ensure that the returned commands: p_static->p_syntax are sorted in
  91. * alphabetical order. If idx exceeds the available dynamic subcommands, the function must write
  92. * to p_static->p_syntax NULL value. This will indicate to the CLI module that
  93. * there are no more dynamic commands to read.
  94. */
  95. typedef void (*nrf_cli_dynamic_get)(size_t idx, nrf_cli_static_entry_t * p_static);
  96. /**
  97. * @brief CLI command descriptor.
  98. */
  99. struct nrf_cli_cmd_entry
  100. {
  101. bool is_dynamic;
  102. union
  103. {
  104. nrf_cli_dynamic_get p_dynamic_get; //!< Pointer to function returning dynamic commands.
  105. nrf_cli_static_entry_t const * p_static; //!< Pointer to array of static commands.
  106. } u;
  107. };
  108. /**
  109. * @brief CLI command handler prototype.
  110. */
  111. typedef void (*nrf_cli_cmd_handler)(nrf_cli_t const * p_cli, size_t argc, char **argv);
  112. /**
  113. * @brief CLI static command descriptor.
  114. */
  115. struct nrf_cli_static_entry
  116. {
  117. char const * p_syntax; //!< Command syntax strings.
  118. char const * p_help; //!< Command help string.
  119. nrf_cli_cmd_entry_t const * p_subcmd; //!< Pointer to subcommand.
  120. nrf_cli_cmd_handler handler; //!< Command handler.
  121. };
  122. /**
  123. * @brief Macro for defining and adding a root command (level 0).
  124. *
  125. * @note Each root command shall have unique syntax.
  126. *
  127. * @param[in] p_syntax Command syntax (for example: history).
  128. * @param[in] p_subcmd Pointer to a subcommands array.
  129. * @param[in] p_help Pointer to a command help string.
  130. * @param[in] p_handler Pointer to a function handler.
  131. */
  132. #define NRF_CLI_CMD_REGISTER(p_syntax, p_subcmd, p_help, p_handler) \
  133. nrf_cli_static_entry_t const CONCAT_3(nrf_cli_, p_syntax, _raw) = \
  134. NRF_CLI_CMD(p_syntax, p_subcmd, p_help, p_handler); \
  135. NRF_SECTION_ITEM_REGISTER(cli_command, \
  136. nrf_cli_cmd_entry_t const CONCAT_3(nrf_cli_, p_syntax, _const)) = { \
  137. .is_dynamic = false, \
  138. .u.p_static = &CONCAT_3(nrf_cli_, p_syntax, _raw) \
  139. }; \
  140. NRF_SECTION_ITEM_REGISTER(cli_sorted_cmd_ptrs, char const * CONCAT_2(p_syntax, _str_ptr))
  141. /**
  142. * @brief Macro for creating a subcommand set. It must be used outside of any function body.
  143. *
  144. * @param[in] name Name of the subcommand set.
  145. */
  146. #define NRF_CLI_CREATE_STATIC_SUBCMD_SET(name) \
  147. /*lint -save -e85 -e31*/ \
  148. static nrf_cli_static_entry_t const CONCAT_2(name, _raw)[]; \
  149. static nrf_cli_cmd_entry_t const name = { \
  150. .is_dynamic = false, \
  151. .u.p_static = CONCAT_2(name, _raw) \
  152. }; \
  153. static nrf_cli_static_entry_t const CONCAT_2(name, _raw)[] = /*lint -restore*/
  154. /**
  155. * @brief Define ending subcommands set.
  156. *
  157. */
  158. #define NRF_CLI_SUBCMD_SET_END {NULL}
  159. /**
  160. * @brief Macro for creating a dynamic entry.
  161. *
  162. * @param[in] name Name of the dynamic entry.
  163. * @param[in] p_get Pointer to the function returning dynamic commands array @ref nrf_cli_dynamic_get.
  164. */
  165. #define NRF_CLI_CREATE_DYNAMIC_CMD(name, p_get) \
  166. /*lint -save -e19*/ \
  167. static nrf_cli_cmd_entry_t const name = { \
  168. .is_dynamic = true, \
  169. .u.p_dynamic_get = p_get \
  170. }; /*lint -restore*/
  171. /**
  172. * @brief Initializes a CLI command (@ref nrf_cli_static_entry).
  173. *
  174. * @param[in] _p_syntax Command syntax (for example: history).
  175. * @param[in] _p_subcmd Pointer to a subcommands array.
  176. * @param[in] _p_help Pointer to a command help string.
  177. * @param[in] _p_handler Pointer to a function handler.
  178. */
  179. #define NRF_CLI_CMD(_p_syntax, _p_subcmd, _p_help, _p_handler) { \
  180. .p_syntax = (const char *) STRINGIFY(_p_syntax), \
  181. .p_subcmd = _p_subcmd, \
  182. .p_help = (const char *) _p_help, \
  183. .handler = _p_handler \
  184. }
  185. /**
  186. * @internal @brief Internal CLI state in response to data received from the terminal.
  187. */
  188. typedef enum
  189. {
  190. NRF_CLI_RECEIVE_DEFAULT,
  191. NRF_CLI_RECEIVE_ESC,
  192. NRF_CLI_RECEIVE_ESC_SEQ,
  193. NRF_CLI_RECEIVE_TILDE_EXP
  194. } nrf_cli_receive_t;
  195. /**
  196. * @internal @brief Internal CLI state.
  197. */
  198. typedef enum
  199. {
  200. NRF_CLI_STATE_UNINITIALIZED, //!< State uninitialized.
  201. NRF_CLI_STATE_INITIALIZED, //!< State initialized but not active.
  202. NRF_CLI_STATE_ACTIVE, //!< State active.
  203. NRF_CLI_STATE_PANIC_MODE_ACTIVE, //!< State panic mode activated.
  204. NRF_CLI_STATE_PANIC_MODE_INACTIVE //!< State panic mode requested but not supported.
  205. } nrf_cli_state_t;
  206. /**
  207. * @brief Event type from CLI transport.
  208. */
  209. typedef enum
  210. {
  211. NRF_CLI_TRANSPORT_EVT_RX_RDY,
  212. NRF_CLI_TRANSPORT_EVT_TX_RDY
  213. } nrf_cli_transport_evt_t;
  214. typedef void (*nrf_cli_transport_handler_t)(nrf_cli_transport_evt_t evt_type, void * p_context);
  215. typedef struct nrf_cli_transport_s nrf_cli_transport_t;
  216. /**
  217. * @brief Unified CLI transport interface.
  218. */
  219. typedef struct
  220. {
  221. /**
  222. * @brief Function for initializing the CLI transport interface.
  223. *
  224. * @param[in] p_transport Pointer to the transfer instance.
  225. * @param[in] p_config Pointer to instance configuration.
  226. * @param[in] evt_handler Event handler.
  227. * @param[in] p_context Pointer to the context passed to event handler.
  228. *
  229. * @return Standard error code.
  230. */
  231. ret_code_t (*init)(nrf_cli_transport_t const * p_transport,
  232. void const * p_config,
  233. nrf_cli_transport_handler_t evt_handler,
  234. void * p_context);
  235. /**
  236. * @brief Function for uninitializing the CLI transport interface.
  237. *
  238. * @param[in] p_transport Pointer to the transfer instance.
  239. *
  240. * @return Standard error code.
  241. */
  242. ret_code_t (*uninit)(nrf_cli_transport_t const * p_transport);
  243. /**
  244. * @brief Function for reconfiguring the transport to work in blocking mode.
  245. *
  246. * @param p_transport Pointer to the transfer instance.
  247. * @param blocking If true, the transport is enabled in blocking mode.
  248. *
  249. * @return NRF_SUCCESS on successful enabling, error otherwise (also if not supported).
  250. */
  251. ret_code_t (*enable)(nrf_cli_transport_t const * p_transport,
  252. bool blocking);
  253. /**
  254. * @brief Function for writing data to the transport interface.
  255. *
  256. * @param[in] p_transport Pointer to the transfer instance.
  257. * @param[in] p_data Pointer to the source buffer.
  258. * @param[in] length Source buffer length.
  259. * @param[in] p_cnt Pointer to the sent bytes counter.
  260. *
  261. * @return Standard error code.
  262. */
  263. ret_code_t (*write)(nrf_cli_transport_t const * p_transport,
  264. const void * p_data,
  265. size_t length,
  266. size_t * p_cnt);
  267. /**
  268. * @brief Function for reading data from the transport interface.
  269. *
  270. * @param[in] p_transport Pointer to the transfer instance.
  271. * @param[in] p_data Pointer to the destination buffer.
  272. * @param[in] length Destination buffer length.
  273. * @param[in] p_cnt Pointer to the received bytes counter.
  274. *
  275. * @return Standard error code.
  276. */
  277. ret_code_t (*read)(nrf_cli_transport_t const * p_transport,
  278. void * p_data,
  279. size_t length,
  280. size_t * p_cnt);
  281. } nrf_cli_transport_api_t;
  282. struct nrf_cli_transport_s
  283. {
  284. nrf_cli_transport_api_t const * p_api;
  285. };
  286. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  287. /**
  288. * @brief CLI history object header.
  289. */
  290. typedef PACKED_STRUCT
  291. {
  292. nrf_memobj_t * p_prev; //!< Pointer to the next object.
  293. nrf_memobj_t * p_next; //!< Pointer to the previous object.
  294. nrf_cli_cmd_len_t cmd_len; //!< Command length.
  295. } nrf_cli_memobj_header_t;
  296. #endif
  297. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  298. typedef struct
  299. {
  300. uint32_t log_lost_cnt; //!< Lost log counter.
  301. } nrf_cli_statistics_t;
  302. #endif
  303. /**
  304. * @internal @brief Flags for internal CLI usage.
  305. */
  306. typedef struct
  307. {
  308. uint32_t insert_mode : 1; //!< Enables or disables console insert mode for text introduction.
  309. uint32_t show_help : 1; //!< Shows help if the command was called with -h or --help parameter.
  310. uint32_t use_colors : 1; //!< Enables or disables colored syntax.
  311. uint32_t echo : 1; //!< Enables or disables CLI echo.
  312. uint32_t processing : 1; //!< CLI is executing process function.
  313. uint32_t tx_rdy : 1;
  314. uint32_t last_nl : 8; //!< The last received newline character.
  315. } nrf_cli_flag_t;
  316. STATIC_ASSERT(sizeof(nrf_cli_flag_t) == sizeof(uint32_t));
  317. /**
  318. * @internal @brief Union for internal CLI usage.
  319. */
  320. typedef union
  321. {
  322. uint32_t value;
  323. nrf_cli_flag_t flag;
  324. } nrf_cli_internal_t;
  325. /**
  326. * @brief CLI instance context.
  327. */
  328. typedef struct
  329. {
  330. nrf_cli_state_t state; //!< Internal module state.
  331. nrf_cli_receive_t receive_state; //!< Escape sequence indicator.
  332. nrf_cli_static_entry_t active_cmd; //!< Currently executed command
  333. nrf_cli_vt100_ctx_t vt100_ctx; //!< VT100 color and cursor position, terminal width.
  334. nrf_cli_cmd_len_t cmd_buff_len; //!< Command length.
  335. nrf_cli_cmd_len_t cmd_buff_pos; //!< Command buffer cursor position.
  336. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  337. nrf_cli_cmd_len_t cmd_tmp_buff_len; //!< Command length in tmp buffer
  338. #endif
  339. char cmd_buff[NRF_CLI_CMD_BUFF_SIZE]; //!< Command input buffer.
  340. char temp_buff[NRF_CLI_CMD_BUFF_SIZE]; //!< Temporary buffer used by various functions.
  341. char printf_buff[NRF_CLI_PRINTF_BUFF_SIZE]; //!< Printf buffer size.
  342. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  343. nrf_cli_statistics_t statistics; //!< CLI statistics.
  344. #endif
  345. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  346. task_id_t task_id;
  347. #endif
  348. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  349. nrf_memobj_t * p_cmd_list_head; //!< Pointer to the head of history list.
  350. nrf_memobj_t * p_cmd_list_tail; //!< Pointer to the tail of history list.
  351. nrf_memobj_t * p_cmd_list_element; //!< Pointer to an element of history list.
  352. #endif
  353. volatile nrf_cli_internal_t internal; //!< Internal CLI data
  354. } nrf_cli_ctx_t;
  355. extern const nrf_log_backend_api_t nrf_log_backend_cli_api;
  356. typedef struct
  357. {
  358. nrf_queue_t const * p_queue;
  359. void * p_context;
  360. nrf_cli_t const * p_cli;
  361. } nrf_cli_log_backend_t;
  362. #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
  363. #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_size_) \
  364. NRF_QUEUE_DEF(nrf_log_entry_t, \
  365. CONCAT_2(_name_, _queue),_queue_size_, NRF_QUEUE_MODE_NO_OVERFLOW); \
  366. static nrf_cli_log_backend_t CONCAT_2(cli_log_backend,_name_) = { \
  367. .p_queue = &CONCAT_2(_name_, _queue), \
  368. }; \
  369. NRF_LOG_BACKEND_DEF(_name_, nrf_log_backend_cli_api, &CONCAT_2(cli_log_backend,_name_))
  370. #define NRF_CLI_BACKEND_PTR(_name_) &CONCAT_2(_name_, _log_backend)
  371. #else
  372. #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_sz_)
  373. #define NRF_CLI_BACKEND_PTR(_name_) NULL
  374. #endif
  375. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  376. /* Header consists memory for cmd length and pointer to: prev and next element. */
  377. #define NRF_CLI_HISTORY_HEADER_SIZE (sizeof(nrf_cli_memobj_header_t))
  378. #define NRF_CLI_HISTORY_MEM_OBJ(name) \
  379. NRF_MEMOBJ_POOL_DEF(CONCAT_2(name, _cmd_hist_memobj), \
  380. NRF_CLI_HISTORY_HEADER_SIZE + \
  381. NRF_CLI_HISTORY_ELEMENT_SIZE, \
  382. NRF_CLI_HISTORY_ELEMENT_COUNT)
  383. #define NRF_CLI_MEMOBJ_PTR(_name_) &CONCAT_2(_name_, _cmd_hist_memobj)
  384. #else
  385. #define NRF_CLI_MEMOBJ_PTR(_name_) NULL
  386. #define NRF_CLI_HISTORY_MEM_OBJ(name)
  387. #endif
  388. /**
  389. * @brief CLI instance internals.
  390. *
  391. * @ref nrf_cli_t
  392. */
  393. struct nrf_cli
  394. {
  395. char const * const p_name; //!< Terminal name.
  396. nrf_cli_transport_t const * p_iface; //!< Transport interface.
  397. nrf_cli_ctx_t * p_ctx; //!< Internal context.
  398. nrf_log_backend_t const * p_log_backend; //!< Logger backend.
  399. nrf_fprintf_ctx_t * p_fprintf_ctx; //!< fprintf context.
  400. nrf_memobj_pool_t const * p_cmd_hist_mempool; //!< Memory reserved for commands history.
  401. };
  402. /**
  403. * @brief Macro for defining a command line interface instance.
  404. *
  405. * @param[in] name Instance name.
  406. * @param[in] cli_prefix CLI prefix string.
  407. * @param[in] p_transport_iface Pointer to the transport interface.
  408. * @param[in] newline_ch Deprecated parameter, not used any more. Any uint8_t value can be used.
  409. * @param[in] log_queue_size Logger processing queue size.
  410. */
  411. #define NRF_CLI_DEF(name, cli_prefix, p_transport_iface, newline_ch, log_queue_size) \
  412. static nrf_cli_t const name; \
  413. static nrf_cli_ctx_t CONCAT_2(name, _ctx); \
  414. NRF_FPRINTF_DEF(CONCAT_2(name, _fprintf_ctx), \
  415. &name, \
  416. CONCAT_2(name, _ctx).printf_buff, \
  417. NRF_CLI_PRINTF_BUFF_SIZE, \
  418. false, \
  419. nrf_cli_print_stream); \
  420. NRF_LOG_BACKEND_CLI_DEF(CONCAT_2(name, _log_backend), log_queue_size); \
  421. NRF_CLI_HISTORY_MEM_OBJ(name); \
  422. /*lint -save -e31*/ \
  423. static nrf_cli_t const name = { \
  424. .p_name = cli_prefix, \
  425. .p_iface = p_transport_iface, \
  426. .p_ctx = &CONCAT_2(name, _ctx), \
  427. .p_log_backend = NRF_CLI_BACKEND_PTR(name), \
  428. .p_fprintf_ctx = &CONCAT_2(name, _fprintf_ctx), \
  429. .p_cmd_hist_mempool = NRF_CLI_MEMOBJ_PTR(name), \
  430. } /*lint -restore*/
  431. /**
  432. * @brief Function for initializing a transport layer and internal CLI state.
  433. *
  434. * @param[in] p_cli Pointer to CLI instance.
  435. * @param[in] p_transport_config Configuration forwarded to the transport during initialization.
  436. * @param[in] use_colors Enables colored prints.
  437. * @param[in] log_backend If true, the console will be used as logger backend.
  438. * @param[in] init_lvl Default severity level for the logger.
  439. *
  440. * @return Standard error code.
  441. */
  442. ret_code_t nrf_cli_init(nrf_cli_t const * p_cli,
  443. void const * p_transport_config,
  444. bool use_colors,
  445. bool log_backend,
  446. nrf_log_severity_t init_lvl);
  447. ret_code_t nrf_cli_task_create(nrf_cli_t const * p_cli);
  448. /**
  449. * @brief Function for uninitializing a transport layer and internal CLI state.
  450. * If function returns NRF_ERROR_BUSY, you must call @ref nrf_cli_process before calling
  451. * nrf_cli_uninit again.
  452. *
  453. * @param p_cli Pointer to CLI instance.
  454. *
  455. * @return Standard error code.
  456. */
  457. ret_code_t nrf_cli_uninit(nrf_cli_t const * p_cli);
  458. /**
  459. * @brief Function for starting CLI processing.
  460. *
  461. * @param p_cli Pointer to the CLI instance.
  462. *
  463. * @return Standard error code.
  464. */
  465. ret_code_t nrf_cli_start(nrf_cli_t const * p_cli);
  466. /**
  467. * @brief Function for stopping CLI processing.
  468. *
  469. * @param p_cli Pointer to CLI instance.
  470. *
  471. * @return Standard error code.
  472. */
  473. ret_code_t nrf_cli_stop(nrf_cli_t const * p_cli);
  474. /**
  475. * @brief CLI colors for @ref nrf_cli_fprintf function.
  476. */
  477. #define NRF_CLI_DEFAULT NRF_CLI_VT100_COLOR_DEFAULT /**< Turn off character attributes. */
  478. #define NRF_CLI_NORMAL NRF_CLI_VT100_COLOR_WHITE /**< Normal color printf. */
  479. #define NRF_CLI_INFO NRF_CLI_VT100_COLOR_GREEN /**< Info color printf. */
  480. #define NRF_CLI_OPTION NRF_CLI_VT100_COLOR_CYAN /**< Option color printf. */
  481. #define NRF_CLI_WARNING NRF_CLI_VT100_COLOR_YELLOW /**< Warning color printf. */
  482. #define NRF_CLI_ERROR NRF_CLI_VT100_COLOR_RED /**< Error color printf. */
  483. /**
  484. * @brief Printf-like function which sends formatted data stream to the CLI.
  485. * This function shall not be used outside of the CLI module or CLI command context.
  486. *
  487. * @param[in] p_cli Pointer to the CLI instance.
  488. * @param[in] color Printf color.
  489. * @param[in] p_fmt Format string.
  490. * @param[in] ... List of parameters to print.
  491. */
  492. void nrf_cli_fprintf(nrf_cli_t const * p_cli,
  493. nrf_cli_vt100_color_t color,
  494. char const * p_fmt,
  495. ...);
  496. /**
  497. * @brief Print an info message to the CLI.
  498. *
  499. * See @ref nrf_cli_fprintf.
  500. *
  501. * @param[in] _p_cli Pointer to the CLI instance.
  502. * @param[in] _ft Format string.
  503. * @param[in] ... List of parameters to print.
  504. */
  505. #define nrf_cli_info(_p_cli, _ft, ...) \
  506. nrf_cli_fprintf(_p_cli, NRF_CLI_INFO, _ft "\n", ##__VA_ARGS__)
  507. /**
  508. * @brief Print a normal message to the CLI.
  509. *
  510. * See @ref nrf_cli_fprintf.
  511. *
  512. * @param[in] _p_cli Pointer to the CLI instance.
  513. * @param[in] _ft Format string.
  514. * @param[in] ... List of parameters to print.
  515. */
  516. #define nrf_cli_print(_p_cli, _ft, ...) \
  517. nrf_cli_fprintf(_p_cli, NRF_CLI_DEFAULT, _ft "\n", ##__VA_ARGS__)
  518. /**
  519. * @brief Print a warning message to the CLI.
  520. *
  521. * See @ref nrf_cli_fprintf.
  522. *
  523. * @param[in] _p_cli Pointer to the CLI instance.
  524. * @param[in] _ft Format string.
  525. * @param[in] ... List of parameters to print.
  526. */
  527. #define nrf_cli_warn(_p_cli, _ft, ...) \
  528. nrf_cli_fprintf(_p_cli, NRF_CLI_WARNING, _ft "\n", ##__VA_ARGS__)
  529. /**
  530. * @brief Print an error message to the CLI.
  531. *
  532. * See @ref nrf_cli_fprintf.
  533. *
  534. * @param[in] _p_cli Pointer to the CLI instance.
  535. * @param[in] _ft Format string.
  536. * @param[in] ... List of parameters to print.
  537. */
  538. #define nrf_cli_error(_p_cli, _ft, ...) \
  539. nrf_cli_fprintf(_p_cli, NRF_CLI_ERROR, _ft "\n", ##__VA_ARGS__)
  540. /**
  541. * @brief Process function, which should be executed when data is ready in the transport interface.
  542. *
  543. * @param[in] p_cli Pointer to the CLI instance.
  544. */
  545. void nrf_cli_process(nrf_cli_t const * p_cli);
  546. /**
  547. * @brief Option descriptor.
  548. */
  549. typedef struct nrf_cli_getopt_option
  550. {
  551. char const * p_optname; //!< Option long name.
  552. char const * p_optname_short; //!< Option short name.
  553. char const * p_optname_help; //!< Option help string.
  554. } nrf_cli_getopt_option_t;
  555. /**
  556. * @brief Option structure initializer @ref nrf_cli_getopt_option.
  557. *
  558. * @param[in] _p_optname Option name long.
  559. * @param[in] _p_shortname Option name short.
  560. * @param[in] _p_help Option help string.
  561. */
  562. #define NRF_CLI_OPT(_p_optname, _p_shortname, _p_help) { \
  563. .p_optname = _p_optname, \
  564. .p_optname_short = _p_shortname, \
  565. .p_optname_help = _p_help, \
  566. }
  567. /**
  568. * @brief Informs that a command has been called with -h or --help option.
  569. *
  570. * @param[in] p_cli Pointer to the CLI instance.
  571. *
  572. * @return True if help has been requested.
  573. */
  574. __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli);
  575. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  576. __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli)
  577. {
  578. return p_cli->p_ctx->internal.flag.show_help;
  579. }
  580. #endif
  581. /**
  582. * @brief Prints the current command help.
  583. * @details Function will print a help string with: the currently entered command, its options,
  584. * and subcommands (if they exist).
  585. *
  586. * @param[in] p_cli Pointer to the CLI instance.
  587. * @param[in] p_opt Pointer to the optional option array.
  588. * @param[in] opt_len Option array size.
  589. */
  590. void nrf_cli_help_print(nrf_cli_t const * p_cli,
  591. nrf_cli_getopt_option_t const * p_opt,
  592. size_t opt_len);
  593. /**
  594. * @internal @brief This function shall not be used directly, it is required by the
  595. * nrf_fprintf module.
  596. *
  597. * @param[in] p_user_ctx Pointer to the context for the CLI instance.
  598. * @param[in] p_data Pointer to the data buffer.
  599. * @param[in] data_len Data buffer size.
  600. */
  601. void nrf_cli_print_stream(void const * p_user_ctx, char const * p_data, size_t data_len);
  602. /** @} */
  603. #ifdef __cplusplus
  604. }
  605. #endif
  606. #endif /* NRF_CLI_H__ */