12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145 |
- extern "C" {
- struct getopt_state;
- struct shell_static_entry;
- typedef void (*shell_dynamic_get)(size_t idx,
- struct shell_static_entry *entry);
- struct shell_cmd_entry {
- bool is_dynamic;
- union union_cmd_entry {
-
- shell_dynamic_get dynamic_get;
-
- const struct shell_static_entry *entry;
- } u;
- };
- struct shell;
- struct shell_static_args {
- uint8_t mandatory;
- uint8_t optional;
- };
- const struct device *shell_device_lookup(size_t idx,
- const char *prefix);
- typedef int (*shell_cmd_handler)(const struct shell *shell,
- size_t argc, char **argv);
- typedef int (*shell_dict_cmd_handler)(const struct shell *shell, size_t argc,
- char **argv, void *data);
- struct shell_static_entry {
- const char *syntax;
- const char *help;
- const struct shell_cmd_entry *subcmd;
- shell_cmd_handler handler;
- struct shell_static_args args;
- };
- mandatory, optional) \
- static const struct shell_static_entry UTIL_CAT(_shell_, syntax) = \
- SHELL_CMD_ARG(syntax, subcmd, help, handler, mandatory, optional); \
- static const struct shell_cmd_entry UTIL_CAT(shell_cmd_, syntax) \
- __attribute__ ((section("." \
- STRINGIFY(UTIL_CAT(shell_root_cmd_, syntax))))) \
- __attribute__((used)) = { \
- .is_dynamic = false, \
- .u = {.entry = &UTIL_CAT(_shell_, syntax)} \
- }
- mandatory, optional) \
- COND_CODE_1(\
- flag, \
- (\
- SHELL_CMD_ARG_REGISTER(syntax, subcmd, help, handler, \
- mandatory, optional) \
- ), \
- (\
- static shell_cmd_handler dummy_
- handler;\
- static const struct shell_cmd_entry *dummy_subcmd_
- __unused = subcmd\
- )\
- )
- SHELL_CMD_ARG_REGISTER(syntax, subcmd, help, handler, 0, 0)
- SHELL_COND_CMD_ARG_REGISTER(flag, syntax, subcmd, help, handler, 0, 0)
- static const struct shell_static_entry shell_
- __VA_ARGS__ \
- }; \
- static const struct shell_cmd_entry name = { \
- .is_dynamic = false, \
- .u = { .entry = shell_
- }
- static const struct shell_cmd_entry name = { \
- .is_dynamic = true, \
- .u = { .dynamic_get = get } \
- }
- SHELL_EXPR_CMD_ARG(1, syntax, subcmd, help, handler, mand, opt)
- SHELL_EXPR_CMD_ARG(IS_ENABLED(flag), syntax, subcmd, help, \
- handler, mand, opt)
- _mand, _opt) \
- { \
- .syntax = (_expr) ? (const char *)STRINGIFY(_syntax) : "", \
- .help = (_expr) ? (const char *)_help : NULL, \
- .subcmd = (const struct shell_cmd_entry *)((_expr) ? \
- _subcmd : NULL), \
- .handler = (shell_cmd_handler)((_expr) ? _handler : NULL), \
- .args = { .mandatory = _mand, .optional = _opt} \
- }
- SHELL_CMD_ARG(_syntax, _subcmd, _help, _handler, 0, 0)
- SHELL_COND_CMD_ARG(_flag, _syntax, _subcmd, _help, _handler, 0, 0)
- SHELL_EXPR_CMD_ARG(_expr, _syntax, _subcmd, _help, _handler, 0, 0)
- static int UTIL_CAT(cmd_dict_, GET_ARG_N(1, __DEBRACKET _data))( \
- const struct shell *shell, size_t argc, char **argv) \
- { \
- return _handler(shell, argc, argv, \
- (void *)GET_ARG_N(2, __DEBRACKET _data)); \
- }
- SHELL_CMD_ARG(GET_ARG_N(1, __DEBRACKET _data), NULL, NULL, \
- UTIL_CAT(cmd_dict_, GET_ARG_N(1, __DEBRACKET _data)), 1, 0)
- FOR_EACH_FIXED_ARG(Z_SHELL_CMD_DICT_HANDLER_CREATE, (), \
- _handler, __VA_ARGS__) \
- SHELL_STATIC_SUBCMD_SET_CREATE(_name, \
- FOR_EACH(SHELL_CMD_DICT_CREATE, (,), __VA_ARGS__), \
- SHELL_SUBCMD_SET_END \
- )
- enum shell_receive_state {
- SHELL_RECEIVE_DEFAULT,
- SHELL_RECEIVE_ESC,
- SHELL_RECEIVE_ESC_SEQ,
- SHELL_RECEIVE_TILDE_EXP
- };
- enum shell_state {
- SHELL_STATE_UNINITIALIZED,
- SHELL_STATE_INITIALIZED,
- SHELL_STATE_ACTIVE,
- SHELL_STATE_PANIC_MODE_ACTIVE,
- SHELL_STATE_PANIC_MODE_INACTIVE
- };
- enum shell_transport_evt {
- SHELL_TRANSPORT_EVT_RX_RDY,
- SHELL_TRANSPORT_EVT_TX_RDY
- };
- typedef void (*shell_transport_handler_t)(enum shell_transport_evt evt,
- void *context);
- typedef void (*shell_uninit_cb_t)(const struct shell *shell, int res);
- typedef void (*shell_bypass_cb_t)(const struct shell *shell,
- uint8_t *data,
- size_t len);
- struct shell_transport;
- struct shell_transport_api {
-
- int (*init)(const struct shell_transport *transport,
- const void *config,
- shell_transport_handler_t evt_handler,
- void *context);
-
- int (*uninit)(const struct shell_transport *transport);
-
- int (*enable)(const struct shell_transport *transport,
- bool blocking_tx);
-
- int (*write)(const struct shell_transport *transport,
- const void *data, size_t length, size_t *cnt);
-
- int (*read)(const struct shell_transport *transport,
- void *data, size_t length, size_t *cnt);
-
- void (*update)(const struct shell_transport *transport);
- };
- struct shell_transport {
- const struct shell_transport_api *api;
- void *ctx;
- };
- struct shell_stats {
- atomic_t log_lost_cnt;
- };
- struct shell_flags {
- uint32_t insert_mode :1;
- uint32_t use_colors :1;
- uint32_t echo :1;
- uint32_t obscure :1;
- uint32_t processing :1;
- uint32_t tx_rdy :1;
- uint32_t mode_delete :1;
- uint32_t history_exit:1;
- uint32_t last_nl :8;
- uint32_t cmd_ctx :1;
- uint32_t print_noinit:1;
- };
- BUILD_ASSERT((sizeof(struct shell_flags) == sizeof(uint32_t)),
- "Structure must fit in 4 bytes");
- union shell_internal {
- uint32_t value;
- struct shell_flags flags;
- };
- enum shell_signal {
- SHELL_SIGNAL_RXRDY,
- SHELL_SIGNAL_LOG_MSG,
- SHELL_SIGNAL_KILL,
- SHELL_SIGNAL_TXDONE,
- SHELL_SIGNALS
- };
- struct shell_ctx {
- const char *prompt;
- enum shell_state state;
- enum shell_receive_state receive_state;
-
- struct shell_static_entry active_cmd;
-
- const struct shell_static_entry *selected_cmd;
-
- struct shell_vt100_ctx vt100_ctx;
-
- shell_uninit_cb_t uninit_cb;
-
- shell_bypass_cb_t bypass;
-
- struct getopt_state getopt_state;
- uint16_t cmd_buff_len;
- uint16_t cmd_buff_pos;
- uint16_t cmd_tmp_buff_len;
-
- char cmd_buff[CONFIG_SHELL_CMD_BUFF_SIZE];
-
- char temp_buff[CONFIG_SHELL_CMD_BUFF_SIZE];
-
- char printf_buff[CONFIG_SHELL_PRINTF_BUFF_SIZE];
- volatile union shell_internal internal;
- struct k_poll_signal signals[SHELL_SIGNALS];
-
- struct k_poll_event events[SHELL_SIGNALS];
- struct k_mutex wr_mtx;
- k_tid_t tid;
- };
- extern const struct log_backend_api log_backend_shell_api;
- enum shell_flag {
- SHELL_FLAG_CRLF_DEFAULT = (1<<0),
- SHELL_FLAG_OLF_CRLF = (1<<1)
- };
- struct shell {
- const char *default_prompt;
- const struct shell_transport *iface;
- struct shell_ctx *ctx;
- struct shell_history *history;
- const enum shell_flag shell_flag;
- const struct shell_fprintf *fprintf_ctx;
- struct shell_stats *stats;
- const struct shell_log_backend *log_backend;
- LOG_INSTANCE_PTR_DECLARE(log);
- const char *thread_name;
- struct k_thread *thread;
- k_thread_stack_t *stack;
- };
- extern void z_shell_print_stream(const void *user_ctx, const char *data,
- size_t data_len);
- _log_queue_size, _log_timeout, _shell_flag) \
- static const struct shell _name; \
- static struct shell_ctx UTIL_CAT(_name, _ctx); \
- static uint8_t _name
- Z_SHELL_LOG_BACKEND_DEFINE(_name, _name
- CONFIG_SHELL_PRINTF_BUFF_SIZE, \
- _log_queue_size, _log_timeout); \
- Z_SHELL_HISTORY_DEFINE(_name
- Z_SHELL_FPRINTF_DEFINE(_name
- CONFIG_SHELL_PRINTF_BUFF_SIZE, \
- true, z_shell_print_stream); \
- LOG_INSTANCE_REGISTER(shell, _name, CONFIG_SHELL_LOG_LEVEL); \
- Z_SHELL_STATS_DEFINE(_name); \
- static K_KERNEL_STACK_DEFINE(_name
- static struct k_thread _name
- static const STRUCT_SECTION_ITERABLE(shell, _name) = { \
- .default_prompt = _prompt, \
- .iface = _transport_iface, \
- .ctx = &UTIL_CAT(_name, _ctx), \
- .history = IS_ENABLED(CONFIG_SHELL_HISTORY) ? \
- &_name
- .shell_flag = _shell_flag, \
- .fprintf_ctx = &_name
- .stats = Z_SHELL_STATS_PTR(_name), \
- .log_backend = Z_SHELL_LOG_BACKEND_PTR(_name), \
- LOG_INSTANCE_PTR_INIT(log, shell, _name) \
- .thread_name = STRINGIFY(_name), \
- .thread = &_name
- .stack = _name
- }
- int shell_init(const struct shell *shell, const void *transport_config,
- bool use_colors, bool log_backend, uint32_t init_log_level);
- void shell_uninit(const struct shell *shell, shell_uninit_cb_t cb);
- int shell_start(const struct shell *shell);
- int shell_stop(const struct shell *shell);
- void shell_fprintf(const struct shell *shell, enum shell_vt100_color color,
- const char *fmt, ...);
- void shell_vfprintf(const struct shell *shell, enum shell_vt100_color color,
- const char *fmt, va_list args);
- void shell_hexdump_line(const struct shell *shell, unsigned int offset,
- const uint8_t *data, size_t len);
- void shell_hexdump(const struct shell *shell, const uint8_t *data, size_t len);
- shell_fprintf(_sh, SHELL_INFO, _ft "\n",
- shell_fprintf(_sh, SHELL_NORMAL, _ft "\n",
- shell_fprintf(_sh, SHELL_WARNING, _ft "\n",
- shell_fprintf(_sh, SHELL_ERROR, _ft "\n",
- void shell_process(const struct shell *shell);
- int shell_prompt_change(const struct shell *shell, const char *prompt);
- void shell_help(const struct shell *shell);
- int shell_getopt(const struct shell *shell, int argc, char *const argv[],
- const char *ostr);
- struct getopt_state *shell_getopt_state_get(const struct shell *shell);
- int shell_execute_cmd(const struct shell *shell, const char *cmd);
- int shell_set_root_cmd(const char *cmd);
- void shell_set_bypass(const struct shell *shell, shell_bypass_cb_t bypass);
- int shell_insert_mode_set(const struct shell *shell, bool val);
- int shell_use_colors_set(const struct shell *shell, bool val);
- int shell_echo_set(const struct shell *shell, bool val);
- int shell_obscure_set(const struct shell *shell, bool obscure);
- int shell_mode_delete_set(const struct shell *shell, bool val);
- }
|