123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704 |
- #ifndef NRF_CLI_H__
- #define NRF_CLI_H__
- #include "sdk_common.h"
- #include "nrf_cli_types.h"
- #include "nrf_section.h"
- #include "nrf_log_backend_interface.h"
- #include "nrf_queue.h"
- #include "nrf_log_ctrl.h"
- #include "app_util_platform.h"
- #include "nrf_memobj.h"
- #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
- #include "task_manager.h"
- #endif
- #include "nrf_fprintf.h"
- #include "nrf_fprintf_format.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #define NRF_CLI_RX_BUFF_SIZE 16
- #define NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT (1UL << 19)
- #define NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT (1UL << 20)
- #define NRF_CLI_LOG_PENDING_TASK_EVT (1UL << 21)
- #define NRF_CLI_CMD_EXECUTE_EVT (1UL << 22)
- #define NRF_CLI_KILL_TASK_EVT (1UL << 23)
- #define NRF_CLI_TASK_EVTS (NRF_CLI_TRANSPORT_TX_RDY_TASK_EVT | \
- NRF_CLI_TRANSPORT_RX_RDY_TASK_EVT | \
- NRF_CLI_LOG_PENDING_TASK_EVT | \
- NRF_CLI_CMD_EXECUTE_EVT | \
- NRF_CLI_KILL_TASK_EVT)
- typedef struct nrf_cli nrf_cli_t;
- typedef struct nrf_cli_cmd_entry nrf_cli_cmd_entry_t;
- typedef struct nrf_cli_static_entry nrf_cli_static_entry_t;
- typedef void (*nrf_cli_dynamic_get)(size_t idx, nrf_cli_static_entry_t * p_static);
- struct nrf_cli_cmd_entry
- {
- bool is_dynamic;
- union
- {
- nrf_cli_dynamic_get p_dynamic_get;
- nrf_cli_static_entry_t const * p_static;
- } u;
- };
- typedef void (*nrf_cli_cmd_handler)(nrf_cli_t const * p_cli, size_t argc, char **argv);
- struct nrf_cli_static_entry
- {
- char const * p_syntax;
- char const * p_help;
- nrf_cli_cmd_entry_t const * p_subcmd;
- nrf_cli_cmd_handler handler;
- };
- #define NRF_CLI_CMD_REGISTER(p_syntax, p_subcmd, p_help, p_handler) \
- nrf_cli_static_entry_t const CONCAT_3(nrf_cli_, p_syntax, _raw) = \
- NRF_CLI_CMD(p_syntax, p_subcmd, p_help, p_handler); \
- NRF_SECTION_ITEM_REGISTER(cli_command, \
- nrf_cli_cmd_entry_t const CONCAT_3(nrf_cli_, p_syntax, _const)) = { \
- .is_dynamic = false, \
- .u = {.p_static = &CONCAT_3(nrf_cli_, p_syntax, _raw)} \
- }; \
- NRF_SECTION_ITEM_REGISTER(cli_sorted_cmd_ptrs, char const * CONCAT_2(p_syntax, _str_ptr))
- #define NRF_CLI_CREATE_STATIC_SUBCMD_SET(name) \
- \
- static nrf_cli_static_entry_t const CONCAT_2(name, _raw)[]; \
- static nrf_cli_cmd_entry_t const name = { \
- .is_dynamic = false, \
- .u = {.p_static = CONCAT_2(name, _raw) } \
- }; \
- static nrf_cli_static_entry_t const CONCAT_2(name, _raw)[] =
- #define NRF_CLI_SUBCMD_SET_END {NULL}
- #define NRF_CLI_CREATE_DYNAMIC_CMD(name, p_get) \
- \
- static nrf_cli_cmd_entry_t const name = { \
- .is_dynamic = true, \
- .u = { .p_dynamic_get = p_get } \
- };
- #define NRF_CLI_CPP_CREATE_STATIC_SUBCMD_SET(name, ...) \
- static nrf_cli_static_entry_t const CONCAT_2(name, _raw)[] = { \
- __VA_ARGS__ \
- }; \
- static nrf_cli_cmd_entry_t const name = { \
- .is_dynamic = false, \
- .u = { .p_static = CONCAT_2(name, _raw) } \
- }
- #define NRF_CLI_CMD(_p_syntax, _p_subcmd, _p_help, _p_handler) { \
- .p_syntax = (const char *) STRINGIFY(_p_syntax), \
- .p_help = (const char *) _p_help, \
- .p_subcmd = _p_subcmd, \
- .handler = _p_handler \
- }
- typedef enum
- {
- NRF_CLI_RECEIVE_DEFAULT,
- NRF_CLI_RECEIVE_ESC,
- NRF_CLI_RECEIVE_ESC_SEQ,
- NRF_CLI_RECEIVE_TILDE_EXP
- } nrf_cli_receive_t;
- typedef enum
- {
- NRF_CLI_STATE_UNINITIALIZED,
- NRF_CLI_STATE_INITIALIZED,
- NRF_CLI_STATE_ACTIVE,
- NRF_CLI_STATE_PANIC_MODE_ACTIVE,
- NRF_CLI_STATE_PANIC_MODE_INACTIVE
- } nrf_cli_state_t;
- typedef enum
- {
- NRF_CLI_TRANSPORT_EVT_RX_RDY,
- NRF_CLI_TRANSPORT_EVT_TX_RDY
- } nrf_cli_transport_evt_t;
- typedef void (*nrf_cli_transport_handler_t)(nrf_cli_transport_evt_t evt_type, void * p_context);
- typedef struct nrf_cli_transport_s nrf_cli_transport_t;
- typedef struct
- {
-
- ret_code_t (*init)(nrf_cli_transport_t const * p_transport,
- void const * p_config,
- nrf_cli_transport_handler_t evt_handler,
- void * p_context);
-
- ret_code_t (*uninit)(nrf_cli_transport_t const * p_transport);
-
- ret_code_t (*enable)(nrf_cli_transport_t const * p_transport,
- bool blocking);
-
- ret_code_t (*write)(nrf_cli_transport_t const * p_transport,
- const void * p_data,
- size_t length,
- size_t * p_cnt);
-
- ret_code_t (*read)(nrf_cli_transport_t const * p_transport,
- void * p_data,
- size_t length,
- size_t * p_cnt);
- } nrf_cli_transport_api_t;
- struct nrf_cli_transport_s
- {
- nrf_cli_transport_api_t const * p_api;
- };
- #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
- typedef PACKED_STRUCT
- {
- nrf_memobj_t * p_prev;
- nrf_memobj_t * p_next;
- nrf_cli_cmd_len_t cmd_len;
- } nrf_cli_memobj_header_t;
- #endif
- #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
- typedef struct
- {
- uint32_t log_lost_cnt;
- } nrf_cli_statistics_t;
- #endif
- typedef struct
- {
- uint32_t insert_mode : 1;
- uint32_t show_help : 1;
- uint32_t use_colors : 1;
- uint32_t echo : 1;
- uint32_t processing : 1;
- uint32_t tx_rdy : 1;
- uint32_t last_nl : 8;
- } nrf_cli_flag_t;
- STATIC_ASSERT(sizeof(nrf_cli_flag_t) == sizeof(uint32_t));
- typedef union
- {
- uint32_t value;
- nrf_cli_flag_t flag;
- } nrf_cli_internal_t;
- typedef struct
- {
- nrf_cli_state_t state;
- nrf_cli_receive_t receive_state;
- nrf_cli_static_entry_t active_cmd;
- nrf_cli_vt100_ctx_t vt100_ctx;
- nrf_cli_cmd_len_t cmd_buff_len;
- nrf_cli_cmd_len_t cmd_buff_pos;
- #if NRF_MODULE_ENABLED(NRF_CLI_WILDCARD)
- nrf_cli_cmd_len_t cmd_tmp_buff_len;
- #endif
- char cmd_buff[NRF_CLI_CMD_BUFF_SIZE];
- char temp_buff[NRF_CLI_CMD_BUFF_SIZE];
- char printf_buff[NRF_CLI_PRINTF_BUFF_SIZE];
- #if NRF_MODULE_ENABLED(NRF_CLI_STATISTICS)
- nrf_cli_statistics_t statistics;
- #endif
- #if NRF_MODULE_ENABLED(NRF_CLI_USES_TASK_MANAGER)
- task_id_t task_id;
- #endif
- #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
- nrf_memobj_t * p_cmd_list_head;
- nrf_memobj_t * p_cmd_list_tail;
- nrf_memobj_t * p_cmd_list_element;
- #endif
- volatile nrf_cli_internal_t internal;
- } nrf_cli_ctx_t;
- extern const nrf_log_backend_api_t nrf_log_backend_cli_api;
- typedef struct
- {
- nrf_queue_t const * p_queue;
- void * p_context;
- nrf_cli_t const * p_cli;
- } nrf_cli_log_backend_t;
- #if NRF_CLI_LOG_BACKEND && NRF_MODULE_ENABLED(NRF_LOG)
- #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_size_) \
- NRF_QUEUE_DEF(nrf_log_entry_t, \
- CONCAT_2(_name_, _queue),_queue_size_, NRF_QUEUE_MODE_NO_OVERFLOW); \
- static nrf_cli_log_backend_t CONCAT_2(cli_log_backend,_name_) = { \
- .p_queue = &CONCAT_2(_name_, _queue), \
- }; \
- NRF_LOG_BACKEND_DEF(_name_, nrf_log_backend_cli_api, &CONCAT_2(cli_log_backend,_name_))
- #define NRF_CLI_BACKEND_PTR(_name_) &CONCAT_2(_name_, _log_backend)
- #else
- #define NRF_LOG_BACKEND_CLI_DEF(_name_, _queue_sz_)
- #define NRF_CLI_BACKEND_PTR(_name_) NULL
- #endif
- #if NRF_MODULE_ENABLED(NRF_CLI_HISTORY)
- #define NRF_CLI_HISTORY_HEADER_SIZE (sizeof(nrf_cli_memobj_header_t))
- #define NRF_CLI_HISTORY_MEM_OBJ(name) \
- NRF_MEMOBJ_POOL_DEF(CONCAT_2(name, _cmd_hist_memobj), \
- NRF_CLI_HISTORY_HEADER_SIZE + \
- NRF_CLI_HISTORY_ELEMENT_SIZE, \
- NRF_CLI_HISTORY_ELEMENT_COUNT)
- #define NRF_CLI_MEMOBJ_PTR(_name_) &CONCAT_2(_name_, _cmd_hist_memobj)
- #else
- #define NRF_CLI_MEMOBJ_PTR(_name_) NULL
- #define NRF_CLI_HISTORY_MEM_OBJ(name)
- #endif
- struct nrf_cli
- {
- char const * const p_name;
- nrf_cli_transport_t const * p_iface;
- nrf_cli_ctx_t * p_ctx;
- nrf_log_backend_t const * p_log_backend;
- nrf_fprintf_ctx_t * p_fprintf_ctx;
- nrf_memobj_pool_t const * p_cmd_hist_mempool;
- };
- #define NRF_CLI_DEF(name, cli_prefix, p_transport_iface, newline_ch, log_queue_size) \
- static nrf_cli_t const name; \
- static nrf_cli_ctx_t CONCAT_2(name, _ctx); \
- NRF_FPRINTF_DEF(CONCAT_2(name, _fprintf_ctx), \
- &name, \
- CONCAT_2(name, _ctx).printf_buff, \
- NRF_CLI_PRINTF_BUFF_SIZE, \
- false, \
- nrf_cli_print_stream); \
- NRF_LOG_BACKEND_CLI_DEF(CONCAT_2(name, _log_backend), log_queue_size); \
- NRF_CLI_HISTORY_MEM_OBJ(name); \
- \
- static nrf_cli_t const name = { \
- .p_name = cli_prefix, \
- .p_iface = p_transport_iface, \
- .p_ctx = &CONCAT_2(name, _ctx), \
- .p_log_backend = NRF_CLI_BACKEND_PTR(name), \
- .p_fprintf_ctx = &CONCAT_2(name, _fprintf_ctx), \
- .p_cmd_hist_mempool = NRF_CLI_MEMOBJ_PTR(name), \
- }
- ret_code_t nrf_cli_init(nrf_cli_t const * p_cli,
- void const * p_transport_config,
- bool use_colors,
- bool log_backend,
- nrf_log_severity_t init_lvl);
- ret_code_t nrf_cli_task_create(nrf_cli_t const * p_cli);
- ret_code_t nrf_cli_uninit(nrf_cli_t const * p_cli);
- ret_code_t nrf_cli_start(nrf_cli_t const * p_cli);
- ret_code_t nrf_cli_stop(nrf_cli_t const * p_cli);
- #define NRF_CLI_DEFAULT NRF_CLI_VT100_COLOR_DEFAULT
- #define NRF_CLI_NORMAL NRF_CLI_VT100_COLOR_WHITE
- #define NRF_CLI_INFO NRF_CLI_VT100_COLOR_GREEN
- #define NRF_CLI_OPTION NRF_CLI_VT100_COLOR_CYAN
- #define NRF_CLI_WARNING NRF_CLI_VT100_COLOR_YELLOW
- #define NRF_CLI_ERROR NRF_CLI_VT100_COLOR_RED
- void nrf_cli_fprintf(nrf_cli_t const * p_cli,
- nrf_cli_vt100_color_t color,
- char const * p_fmt,
- ...);
- #define nrf_cli_info(_p_cli, _ft, ...) \
- nrf_cli_fprintf(_p_cli, NRF_CLI_INFO, _ft "\n", ##__VA_ARGS__)
- #define nrf_cli_print(_p_cli, _ft, ...) \
- nrf_cli_fprintf(_p_cli, NRF_CLI_DEFAULT, _ft "\n", ##__VA_ARGS__)
- #define nrf_cli_warn(_p_cli, _ft, ...) \
- nrf_cli_fprintf(_p_cli, NRF_CLI_WARNING, _ft "\n", ##__VA_ARGS__)
- #define nrf_cli_error(_p_cli, _ft, ...) \
- nrf_cli_fprintf(_p_cli, NRF_CLI_ERROR, _ft "\n", ##__VA_ARGS__)
- void nrf_cli_process(nrf_cli_t const * p_cli);
- typedef struct nrf_cli_getopt_option
- {
- char const * p_optname;
- char const * p_optname_short;
- char const * p_optname_help;
- } nrf_cli_getopt_option_t;
- #define NRF_CLI_OPT(_p_optname, _p_shortname, _p_help) { \
- .p_optname = _p_optname, \
- .p_optname_short = _p_shortname, \
- .p_optname_help = _p_help, \
- }
- __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli);
- #ifndef SUPPRESS_INLINE_IMPLEMENTATION
- __STATIC_INLINE bool nrf_cli_help_requested(nrf_cli_t const * p_cli)
- {
- return p_cli->p_ctx->internal.flag.show_help;
- }
- #endif
- void nrf_cli_help_print(nrf_cli_t const * p_cli,
- nrf_cli_getopt_option_t const * p_opt,
- size_t opt_len);
- void nrf_cli_print_stream(void const * p_user_ctx, char const * p_data, size_t data_len);
- #ifdef __cplusplus
- }
- #endif
- #endif
|