nrf_cli.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /**
  2. * Copyright (c) 2017 - 2018, 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. } nrf_cli_flag_t;
  315. STATIC_ASSERT(sizeof(nrf_cli_flag_t) == sizeof(uint32_t));
  316. /**
  317. * @internal @brief Union for internal CLI usage.
  318. */
  319. typedef union
  320. {
  321. uint32_t value;
  322. nrf_cli_flag_t flag;
  323. } nrf_cli_internal_t;
  324. /**
  325. * @brief CLI instance context.
  326. */
  327. typedef struct
  328. {
  329. nrf_cli_state_t state; //!< Internal module state.
  330. nrf_cli_receive_t receive_state; //!< Escape sequence indicator.
  331. nrf_cli_static_entry_t const * p_current_stcmd; //!< Currently executed command.
  332. nrf_cli_vt100_ctx_t vt100_ctx; //!< VT100 color and cursor position, terminal width.
  333. nrf_cli_cmd_len_t cmd_buff_len; //!< Command length.
  334. nrf_cli_cmd_len_t cmd_buff_pos; //!< Command buffer cursor position.
  335. #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
  336. nrf_cli_cmd_len_t cmd_tmp_buff_len; //!< Command length in tmp buffer
  337. #endif
  338. char cmd_buff[NRF_CLI_CMD_BUFF_SIZE]; //!< Command input buffer.
  339. char temp_buff[NRF_CLI_CMD_BUFF_SIZE]; //!< Temporary buffer used by various functions.
  340. char printf_buff[NRF_CLI_PRINTF_BUFF_SIZE]; //!< Printf buffer size.
  341. #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
  342. nrf_cli_statistics_t statistics; //!< CLI statistics.
  343. #endif
  344. #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
  345. task_id_t task_id;
  346. #endif
  347. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  348. nrf_memobj_t * p_cmd_list_head; //!< Pointer to the head of history list.
  349. nrf_memobj_t * p_cmd_list_tail; //!< Pointer to the tail of history list.
  350. nrf_memobj_t * p_cmd_list_element; //!< Pointer to an element of history list.
  351. #endif
  352. volatile nrf_cli_internal_t internal; //!< Internal CLI data
  353. } nrf_cli_ctx_t;
  354. extern const nrf_log_backend_api_t nrf_log_backend_cli_api;
  355. typedef struct
  356. {
  357. nrf_queue_t const * p_queue;
  358. void * p_context;
  359. nrf_cli_t const * p_cli;
  360. } nrf_cli_log_backend_t;
  361. #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
  362. #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_size_) \
  363. NRF_QUEUE_DEF(nrf_log_entry_t, \
  364. CONCAT_2(_name_, _queue),_queue_size_, NRF_QUEUE_MODE_NO_OVERFLOW); \
  365. static nrf_cli_log_backend_t CONCAT_2(cli_log_backend,_name_) = { \
  366. .p_queue = &CONCAT_2(_name_, _queue), \
  367. }; \
  368. NRF_LOG_BACKEND_DEF(_name_, nrf_log_backend_cli_api, &CONCAT_2(cli_log_backend,_name_))
  369. #define NRF_CLI_BACKEND_PTR(_name_) &CONCAT_2(_name_, _log_backend)
  370. #else
  371. #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_sz_)
  372. #define NRF_CLI_BACKEND_PTR(_name_) NULL
  373. #endif
  374. #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
  375. /* Header consists memory for cmd length and pointer to: prev and next element. */
  376. #define NRF_CLI_HISTORY_HEADER_SIZE (sizeof(nrf_cli_memobj_header_t))
  377. #define NRF_CLI_HISTORY_MEM_OBJ(name) \
  378. NRF_MEMOBJ_POOL_DEF(CONCAT_2(name, _cmd_hist_memobj), \
  379. NRF_CLI_HISTORY_HEADER_SIZE + \
  380. NRF_CLI_HISTORY_ELEMENT_SIZE, \
  381. NRF_CLI_HISTORY_ELEMENT_COUNT)
  382. #define NRF_CLI_MEMOBJ_PTR(_name_) &CONCAT_2(_name_, _cmd_hist_memobj)
  383. #else
  384. #define NRF_CLI_MEMOBJ_PTR(_name_) NULL
  385. #define NRF_CLI_HISTORY_MEM_OBJ(name)
  386. #endif
  387. /**
  388. * @brief CLI instance internals.
  389. *
  390. * @ref nrf_cli_t
  391. */
  392. struct nrf_cli
  393. {
  394. char const * const p_name; //!< Terminal name.
  395. nrf_cli_transport_t const * p_iface; //!< Transport interface.
  396. nrf_cli_ctx_t * p_ctx; //!< Internal context.
  397. nrf_log_backend_t const * p_log_backend; //!< Logger backend.
  398. nrf_fprintf_ctx_t * p_fprintf_ctx; //!< fprintf context.
  399. nrf_memobj_pool_t const * p_cmd_hist_mempool; //!< Memory reserved for commands history.
  400. char const newline_char; //!< New line character, only allowed values: \\n and \\r.
  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 New line character - only allowed values are '\\n' or '\\r'.
  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. .newline_char = newline_ch \
  431. } /*lint -restore*/
  432. /**
  433. * @brief Function for initializing a transport layer and internal CLI state.
  434. *
  435. * @param[in] p_cli Pointer to CLI instance.
  436. * @param[in] p_transport_config Configuration forwarded to the transport during initialization.
  437. * @param[in] use_colors Enables colored prints.
  438. * @param[in] log_backend If true, the console will be used as logger backend.
  439. * @param[in] init_lvl Default severity level for the logger.
  440. *
  441. * @return Standard error code.
  442. */
  443. ret_code_t nrf_cli_init(nrf_cli_t const * p_cli,
  444. void const * p_transport_config,
  445. bool use_colors,
  446. bool log_backend,
  447. nrf_log_severity_t init_lvl);
  448. ret_code_t nrf_cli_task_create(nrf_cli_t const * p_cli);
  449. /**
  450. * @brief Function for uninitializing a transport layer and internal CLI state.
  451. * If function returns NRF_ERROR_BUSY, you must call @ref nrf_cli_process before calling
  452. * nrf_cli_uninit again.
  453. *
  454. * @param p_cli Pointer to CLI instance.
  455. *
  456. * @return Standard error code.
  457. */
  458. ret_code_t nrf_cli_uninit(nrf_cli_t const * p_cli);
  459. /**
  460. * @brief Function for starting CLI processing.
  461. *
  462. * @param p_cli Pointer to the CLI instance.
  463. *
  464. * @return Standard error code.
  465. */
  466. ret_code_t nrf_cli_start(nrf_cli_t const * p_cli);
  467. /**
  468. * @brief Function for stopping CLI processing.
  469. *
  470. * @param p_cli Pointer to CLI instance.
  471. *
  472. * @return Standard error code.
  473. */
  474. ret_code_t nrf_cli_stop(nrf_cli_t const * p_cli);
  475. /**
  476. * @brief CLI colors for @ref nrf_cli_fprintf function.
  477. */
  478. #define NRF_CLI_DEFAULT NRF_CLI_VT100_COLOR_DEFAULT /**< Turn off character attributes. */
  479. #define NRF_CLI_NORMAL NRF_CLI_VT100_COLOR_WHITE /**< Normal color printf. */
  480. #define NRF_CLI_INFO NRF_CLI_VT100_COLOR_GREEN /**< Info color printf. */
  481. #define NRF_CLI_OPTION NRF_CLI_VT100_COLOR_CYAN /**< Option color printf. */
  482. #define NRF_CLI_WARNING NRF_CLI_VT100_COLOR_YELLOW /**< Warning color printf. */
  483. #define NRF_CLI_ERROR NRF_CLI_VT100_COLOR_RED /**< Error color printf. */
  484. /**
  485. * @brief Printf-like function which sends formatted data stream to the CLI.
  486. * This function shall not be used outside of the CLI module or CLI command context.
  487. *
  488. * @param[in] p_cli Pointer to the CLI instance.
  489. * @param[in] color Printf color.
  490. * @param[in] p_fmt Format string.
  491. * @param[in] ... List of parameters to print.
  492. */
  493. void nrf_cli_fprintf(nrf_cli_t const * p_cli,
  494. nrf_cli_vt100_color_t color,
  495. char const * p_fmt,
  496. ...);
  497. /**
  498. * @brief Process function, which should be executed when data is ready in the transport interface.
  499. *
  500. * @param[in] p_cli Pointer to the CLI instance.
  501. */
  502. void nrf_cli_process(nrf_cli_t const * p_cli);
  503. /**
  504. * @brief Option descriptor.
  505. */
  506. typedef struct nrf_cli_getopt_option
  507. {
  508. char const * p_optname; //!< Option long name.
  509. char const * p_optname_short; //!< Option short name.
  510. char const * p_optname_help; //!< Option help string.
  511. } nrf_cli_getopt_option_t;
  512. /**
  513. * @brief Option structure initializer @ref nrf_cli_getopt_option.
  514. *
  515. * @param[in] _p_optname Option name long.
  516. * @param[in] _p_shortname Option name short.
  517. * @param[in] _p_help Option help string.
  518. */
  519. #define NRF_CLI_OPT(_p_optname, _p_shortname, _p_help) { \
  520. .p_optname = _p_optname, \
  521. .p_optname_short = _p_shortname, \
  522. .p_optname_help = _p_help, \
  523. }
  524. /**
  525. * @brief Informs that a command has been called with -h or --help option.
  526. *
  527. * @param[in] p_cli Pointer to the CLI instance.
  528. *
  529. * @return True if help has been requested.
  530. */
  531. __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli);
  532. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  533. __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli)
  534. {
  535. return p_cli->p_ctx->internal.flag.show_help;
  536. }
  537. #endif
  538. /**
  539. * @brief Prints the current command help.
  540. * @details Function will print a help string with: the currently entered command, its options,
  541. * and subcommands (if they exist).
  542. *
  543. * @param[in] p_cli Pointer to the CLI instance.
  544. * @param[in] p_opt Pointer to the optional option array.
  545. * @param[in] opt_len Option array size.
  546. */
  547. void nrf_cli_help_print(nrf_cli_t const * p_cli,
  548. nrf_cli_getopt_option_t const * p_opt,
  549. size_t opt_len);
  550. /**
  551. * @internal @brief This function shall not be used directly, it is required by the
  552. * nrf_fprintf module.
  553. *
  554. * @param[in] p_user_ctx Pointer to the context for the CLI instance.
  555. * @param[in] p_data Pointer to the data buffer.
  556. * @param[in] data_len Data buffer size.
  557. */
  558. void nrf_cli_print_stream(void const * p_user_ctx, char const * p_data, size_t data_len);
  559. /** @} */
  560. #ifdef __cplusplus
  561. }
  562. #endif
  563. #endif /* NRF_CLI_H__ */