1 /*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #ifndef SHELL_HISTORY_H__
8 #define SHELL_HISTORY_H__
9
10 #include <zephyr/kernel.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/sys/dlist.h>
13 #include <zephyr/sys/ring_buffer.h>
14 #include <stdbool.h>
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20
21 struct shell_history {
22 struct ring_buf *ring_buf;
23 sys_dlist_t list;
24 sys_dnode_t *current;
25 };
26
27 /**
28 * @brief Create shell history instance.
29 *
30 * @param _name History instance name.
31 * @param _size Memory dedicated for shell history.
32 */
33 #define Z_SHELL_HISTORY_DEFINE(_name, _size) \
34 static uint8_t __noinit __aligned(sizeof(void *)) \
35 _name##_ring_buf_data[_size]; \
36 static struct ring_buf _name##_ring_buf = \
37 { \
38 .size = _size, \
39 .buffer = _name##_ring_buf_data \
40 }; \
41 static struct shell_history _name = { \
42 .ring_buf = &_name##_ring_buf \
43 }
44
45
46 /**
47 * @brief Initialize shell history module.
48 *
49 * @param history Shell history instance.
50 */
51 void z_shell_history_init(struct shell_history *history);
52
53 /**
54 * @brief Purge shell history.
55 *
56 * Function clears whole shell command history.
57 *
58 * @param history Shell history instance.
59 *
60 */
61 void z_shell_history_purge(struct shell_history *history);
62
63 /**
64 * @brief Exit history browsing mode.
65 *
66 * @param history Shell history instance.
67 */
68 void z_shell_history_mode_exit(struct shell_history *history);
69
70 /**
71 * @brief Get next entry in shell command history.
72 *
73 * Function returns next (in given direction) stored line.
74 *
75 * @param[in] history Shell history instance.
76 * @param[in] up Direction.
77 * @param[out] dst Buffer where line is copied.
78 * @param[in,out] len Buffer size (input), amount of copied
79 * data (output).
80 * @return True if remains in history mode.
81 */
82 bool z_shell_history_get(struct shell_history *history, bool up,
83 uint8_t *dst, uint16_t *len);
84
85 /**
86 * @brief Put line into shell command history.
87 *
88 * If history is full, oldest entry (or entries) is removed.
89 *
90 * @param history Shell history instance.
91 * @param line Data.
92 * @param len Data length.
93 *
94 */
95 void z_shell_history_put(struct shell_history *history, uint8_t *line,
96 size_t len);
97
98 /**
99 * @brief Get state of shell history.
100 *
101 * @param history Shell history instance.
102 *
103 * @return True if in browsing mode.
104 */
z_shell_history_active(struct shell_history * history)105 static inline bool z_shell_history_active(struct shell_history *history)
106 {
107 return (history->current) ? true : false;
108 }
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif /* SHELL_HISTORY_H__ */
115