shell: internal api update: history module

Add prefix z_ to internal functions handling history feature.

Signed-off-by: Jakub Rzeszutko <jakub.rzeszutko@nordicsemi.no>
This commit is contained in:
Jakub Rzeszutko 2020-12-07 10:25:28 +01:00 committed by Carles Cufí
commit 91b01ce703
6 changed files with 68 additions and 66 deletions

View file

@ -734,7 +734,7 @@ extern void z_shell_print_stream(const void *user_ctx, const char *data,
SHELL_LOG_BACKEND_DEFINE(_name, _name##_out_buffer, \ SHELL_LOG_BACKEND_DEFINE(_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE, \ CONFIG_SHELL_PRINTF_BUFF_SIZE, \
_log_queue_size, _log_timeout); \ _log_queue_size, _log_timeout); \
SHELL_HISTORY_DEFINE(_name##_history, CONFIG_SHELL_HISTORY_BUFFER); \ Z_SHELL_HISTORY_DEFINE(_name##_history, CONFIG_SHELL_HISTORY_BUFFER); \
Z_SHELL_FPRINTF_DEFINE(_name##_fprintf, &_name, _name##_out_buffer, \ Z_SHELL_FPRINTF_DEFINE(_name##_fprintf, &_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE, \ CONFIG_SHELL_PRINTF_BUFF_SIZE, \
true, z_shell_print_stream); \ true, z_shell_print_stream); \

View file

@ -30,16 +30,16 @@ struct shell_history {
* @param _name History instance name. * @param _name History instance name.
* @param _size Memory dedicated for shell history. * @param _size Memory dedicated for shell history.
*/ */
#define SHELL_HISTORY_DEFINE(_name, _size) \ #define Z_SHELL_HISTORY_DEFINE(_name, _size) \
static uint8_t __noinit __aligned(sizeof(void *)) \ static uint8_t __noinit __aligned(sizeof(void *)) \
_name##_ring_buf_data[_size]; \ _name##_ring_buf_data[_size]; \
static struct ring_buf _name##_ring_buf = \ static struct ring_buf _name##_ring_buf = \
{ \ { \
.size = _size, \ .size = _size, \
.buf = { .buf8 = _name##_ring_buf_data } \ .buf = { .buf8 = _name##_ring_buf_data } \
}; \ }; \
static struct shell_history _name = { \ static struct shell_history _name = { \
.ring_buf = &_name##_ring_buf \ .ring_buf = &_name##_ring_buf \
} }
@ -48,7 +48,7 @@ struct shell_history {
* *
* @param history Shell history instance. * @param history Shell history instance.
*/ */
void shell_history_init(struct shell_history *history); void z_shell_history_init(struct shell_history *history);
/** /**
* @brief Purge shell history. * @brief Purge shell history.
@ -58,14 +58,14 @@ void shell_history_init(struct shell_history *history);
* @param history Shell history instance. * @param history Shell history instance.
* *
*/ */
void shell_history_purge(struct shell_history *history); void z_shell_history_purge(struct shell_history *history);
/** /**
* @brief Exit history browsing mode. * @brief Exit history browsing mode.
* *
* @param history Shell history instance. * @param history Shell history instance.
*/ */
void shell_history_mode_exit(struct shell_history *history); void z_shell_history_mode_exit(struct shell_history *history);
/** /**
* @brief Get next entry in shell command history. * @brief Get next entry in shell command history.
@ -79,8 +79,8 @@ void shell_history_mode_exit(struct shell_history *history);
* data (output). * data (output).
* @return True if remains in history mode. * @return True if remains in history mode.
*/ */
bool shell_history_get(struct shell_history *history, bool up, bool z_shell_history_get(struct shell_history *history, bool up,
uint8_t *dst, uint16_t *len); uint8_t *dst, uint16_t *len);
/** /**
* @brief Put line into shell command history. * @brief Put line into shell command history.
@ -92,7 +92,8 @@ bool shell_history_get(struct shell_history *history, bool up,
* @param len Data length. * @param len Data length.
* *
*/ */
void shell_history_put(struct shell_history *history, uint8_t *line, size_t len); void z_shell_history_put(struct shell_history *history, uint8_t *line,
size_t len);
/** /**
* @brief Get state of shell history. * @brief Get state of shell history.
@ -101,7 +102,7 @@ void shell_history_put(struct shell_history *history, uint8_t *line, size_t len)
* *
* @return True if in browsing mode. * @return True if in browsing mode.
*/ */
static inline bool shell_history_active(struct shell_history *history) static inline bool z_shell_history_active(struct shell_history *history)
{ {
return (history->current) ? true : false; return (history->current) ? true : false;
} }

View file

@ -146,7 +146,7 @@ static void history_init(const struct shell *shell)
return; return;
} }
shell_history_init(shell->history); z_shell_history_init(shell->history);
} }
static void history_purge(const struct shell *shell) static void history_purge(const struct shell *shell)
@ -155,7 +155,7 @@ static void history_purge(const struct shell *shell)
return; return;
} }
shell_history_purge(shell->history); z_shell_history_purge(shell->history);
} }
static void history_mode_exit(const struct shell *shell) static void history_mode_exit(const struct shell *shell)
@ -165,7 +165,7 @@ static void history_mode_exit(const struct shell *shell)
} }
flag_history_exit_set(shell, false); flag_history_exit_set(shell, false);
shell_history_mode_exit(shell->history); z_shell_history_mode_exit(shell->history);
} }
static void history_put(const struct shell *shell, uint8_t *line, size_t length) static void history_put(const struct shell *shell, uint8_t *line, size_t length)
@ -174,7 +174,7 @@ static void history_put(const struct shell *shell, uint8_t *line, size_t length)
return; return;
} }
shell_history_put(shell->history, line, length); z_shell_history_put(shell->history, line, length);
} }
static void history_handle(const struct shell *shell, bool up) static void history_handle(const struct shell *shell, bool up)
@ -190,11 +190,11 @@ static void history_handle(const struct shell *shell, bool up)
/* Checking if history process has been stopped */ /* Checking if history process has been stopped */
if (flag_history_exit_get(shell)) { if (flag_history_exit_get(shell)) {
flag_history_exit_set(shell, false); flag_history_exit_set(shell, false);
shell_history_mode_exit(shell->history); z_shell_history_mode_exit(shell->history);
} }
/* Backup command if history is entered */ /* Backup command if history is entered */
if (!shell_history_active(shell->history)) { if (!z_shell_history_active(shell->history)) {
if (up) { if (up) {
uint16_t cmd_len = shell_strlen(shell->ctx->cmd_buff); uint16_t cmd_len = shell_strlen(shell->ctx->cmd_buff);
@ -211,8 +211,8 @@ static void history_handle(const struct shell *shell, bool up)
} }
/* Start by checking if history is not empty. */ /* Start by checking if history is not empty. */
history_mode = shell_history_get(shell->history, up, history_mode = z_shell_history_get(shell->history, up,
shell->ctx->cmd_buff, &len); shell->ctx->cmd_buff, &len);
/* On exiting history mode print backed up command. */ /* On exiting history mode print backed up command. */
if (!history_mode) { if (!history_mode) {

View file

@ -287,8 +287,8 @@ static int cmd_history(const struct shell *shell, size_t argc, char **argv)
uint16_t len; uint16_t len;
while (1) { while (1) {
shell_history_get(shell->history, true, z_shell_history_get(shell->history, true,
shell->ctx->temp_buff, &len); shell->ctx->temp_buff, &len);
if (len) { if (len) {
shell_print(shell, "[%3d] %s", shell_print(shell, "[%3d] %s",

View file

@ -41,13 +41,13 @@ struct shell_history_item {
char data[0]; char data[0];
}; };
void shell_history_mode_exit(struct shell_history *history) void z_shell_history_mode_exit(struct shell_history *history)
{ {
history->current = NULL; history->current = NULL;
} }
bool shell_history_get(struct shell_history *history, bool up, bool z_shell_history_get(struct shell_history *history, bool up,
uint8_t *dst, uint16_t *len) uint8_t *dst, uint16_t *len)
{ {
struct shell_history_item *h_item; /* history item */ struct shell_history_item *h_item; /* history item */
sys_dnode_t *l_item; /* list item */ sys_dnode_t *l_item; /* list item */
@ -121,13 +121,14 @@ static bool remove_from_tail(struct shell_history *history)
return true; return true;
} }
void shell_history_purge(struct shell_history *history) void z_shell_history_purge(struct shell_history *history)
{ {
while (remove_from_tail(history)) { while (remove_from_tail(history)) {
} }
} }
void shell_history_put(struct shell_history *history, uint8_t *line, size_t len) void z_shell_history_put(struct shell_history *history, uint8_t *line,
size_t len)
{ {
sys_dnode_t *l_item; /* list item */ sys_dnode_t *l_item; /* list item */
struct shell_history_item *h_item; struct shell_history_item *h_item;
@ -143,7 +144,7 @@ void shell_history_put(struct shell_history *history, uint8_t *line, size_t len)
return; return;
} }
shell_history_mode_exit(history); z_shell_history_mode_exit(history);
if (len == 0) { if (len == 0) {
return; return;
@ -198,7 +199,7 @@ void shell_history_put(struct shell_history *history, uint8_t *line, size_t len)
} while (1); } while (1);
} }
void shell_history_init(struct shell_history *history) void z_shell_history_init(struct shell_history *history)
{ {
sys_dlist_init(&history->list); sys_dlist_init(&history->list);
history->current = NULL; history->current = NULL;

View file

@ -15,7 +15,7 @@
#include <shell/shell_history.h> #include <shell/shell_history.h>
#define HIST_BUF_SIZE 160 #define HIST_BUF_SIZE 160
SHELL_HISTORY_DEFINE(history, HIST_BUF_SIZE); Z_SHELL_HISTORY_DEFINE(history, HIST_BUF_SIZE);
static void init_test_buf(uint8_t *buf, size_t len, uint8_t offset) static void init_test_buf(uint8_t *buf, size_t len, uint8_t offset)
{ {
@ -36,7 +36,7 @@ static void test_get(bool ok, bool up, uint8_t *exp_buf, uint16_t exp_len)
out_len = sizeof(out_buf); out_len = sizeof(out_buf);
res = shell_history_get(&history, up, out_buf, &out_len); res = z_shell_history_get(&history, up, out_buf, &out_len);
if (ok) { if (ok) {
zassert_true(res, "history should contain one entry.\n"); zassert_true(res, "history should contain one entry.\n");
@ -64,15 +64,15 @@ static void test_history_add_get(void)
init_test_buf(exp_buf, sizeof(exp_buf), 0); init_test_buf(exp_buf, sizeof(exp_buf), 0);
shell_history_init(&history); z_shell_history_init(&history);
test_get(false, true, NULL, 0); test_get(false, true, NULL, 0);
shell_history_put(&history, exp_buf, 20); z_shell_history_put(&history, exp_buf, 20);
test_get(true, true, exp_buf, 20); test_get(true, true, exp_buf, 20);
shell_history_purge(&history); z_shell_history_purge(&history);
} }
/* Test verifies that after purging there is no line in the history. */ /* Test verifies that after purging there is no line in the history. */
@ -82,12 +82,12 @@ static void test_history_purge(void)
init_test_buf(exp_buf, sizeof(exp_buf), 0); init_test_buf(exp_buf, sizeof(exp_buf), 0);
shell_history_init(&history); z_shell_history_init(&history);
shell_history_put(&history, exp_buf, 20); z_shell_history_put(&history, exp_buf, 20);
shell_history_put(&history, exp_buf, 20); z_shell_history_put(&history, exp_buf, 20);
shell_history_purge(&history); z_shell_history_purge(&history);
test_get(false, true, NULL, 0); test_get(false, true, NULL, 0);
} }
@ -117,11 +117,11 @@ static void test_history_get_up_and_down(void)
init_test_buf(exp2_buf, sizeof(exp2_buf), 10); init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
init_test_buf(exp3_buf, sizeof(exp3_buf), 20); init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
shell_history_init(&history); z_shell_history_init(&history);
shell_history_put(&history, exp1_buf, 20); z_shell_history_put(&history, exp1_buf, 20);
shell_history_put(&history, exp2_buf, 15); z_shell_history_put(&history, exp2_buf, 15);
shell_history_put(&history, exp3_buf, 20); z_shell_history_put(&history, exp3_buf, 20);
test_get(true, true, exp3_buf, 20); /* up - 3*/ test_get(true, true, exp3_buf, 20); /* up - 3*/
test_get(true, true, exp2_buf, 15); /* up - 2*/ test_get(true, true, exp2_buf, 15); /* up - 2*/
@ -132,7 +132,7 @@ static void test_history_get_up_and_down(void)
test_get(true, false, exp3_buf, 20); /* down - 3 */ test_get(true, false, exp3_buf, 20); /* down - 3 */
test_get(false, false, NULL, 0); /* down - nothing */ test_get(false, false, NULL, 0); /* down - nothing */
shell_history_purge(&history); z_shell_history_purge(&history);
} }
/* Function for getting maximal buffer size that can be stored in the history */ /* Function for getting maximal buffer size that can be stored in the history */
@ -143,13 +143,13 @@ static int get_max_buffer_len(void)
int len = sizeof(buf); int len = sizeof(buf);
uint16_t out_len; uint16_t out_len;
shell_history_init(&history); z_shell_history_init(&history);
do { do {
shell_history_put(&history, buf, len); z_shell_history_put(&history, buf, len);
out_len = sizeof(out_buf); out_len = sizeof(out_buf);
if (shell_history_get(&history, true, out_buf, &out_len)) { if (z_shell_history_get(&history, true, out_buf, &out_len)) {
shell_history_purge(&history); z_shell_history_purge(&history);
break; break;
} }
} while (len--); } while (len--);
@ -172,21 +172,21 @@ static void test_too_long_line_not_stored(void)
int max_len = get_max_buffer_len(); int max_len = get_max_buffer_len();
init_test_buf(exp1_buf, sizeof(exp1_buf), 0); init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
shell_history_init(&history); z_shell_history_init(&history);
shell_history_put(&history, exp1_buf, max_len + 1); z_shell_history_put(&history, exp1_buf, max_len + 1);
/*validate that nothing is stored */ /*validate that nothing is stored */
test_get(false, true, NULL, 0); /* empty */ test_get(false, true, NULL, 0); /* empty */
shell_history_put(&history, exp1_buf, 20); z_shell_history_put(&history, exp1_buf, 20);
shell_history_put(&history, exp1_buf, max_len - 10); z_shell_history_put(&history, exp1_buf, max_len - 10);
/* Test that long entry evicts older entry. */ /* Test that long entry evicts older entry. */
test_get(true, true, exp1_buf, max_len - 10); test_get(true, true, exp1_buf, max_len - 10);
test_get(false, true, NULL, 0); /* only one entry */ test_get(false, true, NULL, 0); /* only one entry */
shell_history_purge(&history); z_shell_history_purge(&history);
} }
/* Test verifies that same line as the previous one is not stored in the /* Test verifies that same line as the previous one is not stored in the
@ -202,16 +202,16 @@ static void test_no_duplicates_in_a_row(void)
uint8_t exp1_buf[HIST_BUF_SIZE]; uint8_t exp1_buf[HIST_BUF_SIZE];
init_test_buf(exp1_buf, sizeof(exp1_buf), 0); init_test_buf(exp1_buf, sizeof(exp1_buf), 0);
shell_history_init(&history); z_shell_history_init(&history);
shell_history_put(&history, exp1_buf, 20); z_shell_history_put(&history, exp1_buf, 20);
shell_history_put(&history, exp1_buf, 20); z_shell_history_put(&history, exp1_buf, 20);
test_get(true, true, exp1_buf, 20); test_get(true, true, exp1_buf, 20);
/* only one line stored. */ /* only one line stored. */
test_get(false, true, NULL, 0); test_get(false, true, NULL, 0);
shell_history_purge(&history); z_shell_history_purge(&history);
} }
/* Test storing long lines in the history. /* Test storing long lines in the history.
@ -236,21 +236,21 @@ static void test_storing_long_buffers(void)
init_test_buf(exp2_buf, sizeof(exp2_buf), 10); init_test_buf(exp2_buf, sizeof(exp2_buf), 10);
init_test_buf(exp3_buf, sizeof(exp3_buf), 20); init_test_buf(exp3_buf, sizeof(exp3_buf), 20);
shell_history_init(&history); z_shell_history_init(&history);
shell_history_put(&history, exp1_buf, max_len); z_shell_history_put(&history, exp1_buf, max_len);
test_get(true, true, exp1_buf, max_len); test_get(true, true, exp1_buf, max_len);
test_get(false, true, NULL, 0); /* only one entry */ test_get(false, true, NULL, 0); /* only one entry */
shell_history_put(&history, exp2_buf, max_len); z_shell_history_put(&history, exp2_buf, max_len);
test_get(true, true, exp2_buf, max_len); test_get(true, true, exp2_buf, max_len);
test_get(false, true, NULL, 0); /* only one entry */ test_get(false, true, NULL, 0); /* only one entry */
shell_history_put(&history, exp3_buf, max_len); z_shell_history_put(&history, exp3_buf, max_len);
test_get(true, true, exp3_buf, max_len); test_get(true, true, exp3_buf, max_len);
test_get(false, true, NULL, 0); /* only one entry */ test_get(false, true, NULL, 0); /* only one entry */
shell_history_purge(&history); z_shell_history_purge(&history);
} }
void test_main(void) void test_main(void)