Line data Source code
1 0 : /*
2 : * Copyright (c) 2018 Nordic Semiconductor ASA
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : #ifndef ZEPHYR_INCLUDE_SHELL_HISTORY_H_
8 : #define ZEPHYR_INCLUDE_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 0 : struct shell_history {
22 0 : struct k_heap *heap;
23 0 : sys_dlist_t list;
24 0 : sys_dnode_t *current;
25 : };
26 :
27 : /**
28 : * @brief Create shell history instance.
29 : *
30 : * @param _name History instance name.
31 : * @param _size Memory size dedicated for shell history.
32 : */
33 : #define Z_SHELL_HISTORY_DEFINE(_name, _size) \
34 : K_HEAP_DEFINE(_name##_heap, _size); \
35 : static struct shell_history _name = { \
36 : .heap = &_name##_heap, \
37 : .list = SYS_DLIST_STATIC_INIT(&_name.list), \
38 : }
39 :
40 : /**
41 : * @brief Purge shell history.
42 : *
43 : * Function clears whole shell command history.
44 : *
45 : * @param history Shell history instance.
46 : *
47 : */
48 : void z_shell_history_purge(struct shell_history *history);
49 :
50 : /**
51 : * @brief Exit history browsing mode.
52 : *
53 : * @param history Shell history instance.
54 : */
55 : void z_shell_history_mode_exit(struct shell_history *history);
56 :
57 : /**
58 : * @brief Get next entry in shell command history.
59 : *
60 : * Function returns next (in given direction) stored line.
61 : *
62 : * @param[in] history Shell history instance.
63 : * @param[in] up Direction.
64 : * @param[out] dst Buffer where line is copied.
65 : * @param[in,out] len Buffer size (input), amount of copied
66 : * data (output).
67 : * @return True if remains in history mode.
68 : */
69 : bool z_shell_history_get(struct shell_history *history, bool up,
70 : uint8_t *dst, uint16_t *len);
71 :
72 : /**
73 : * @brief Put line into shell command history.
74 : *
75 : * If history is full, oldest entry (or entries) is removed.
76 : *
77 : * @param history Shell history instance.
78 : * @param line Data.
79 : * @param len Data length.
80 : *
81 : */
82 : void z_shell_history_put(struct shell_history *history, uint8_t *line,
83 : size_t len);
84 :
85 : /**
86 : * @brief Get state of shell history.
87 : *
88 : * @param history Shell history instance.
89 : *
90 : * @return True if in browsing mode.
91 : */
92 : static inline bool z_shell_history_active(struct shell_history *history)
93 : {
94 : return (history->current) ? true : false;
95 : }
96 :
97 : #ifdef __cplusplus
98 : }
99 : #endif
100 :
101 : #endif /* ZEPHYR_INCLUDE_SHELL_HISTORY_H_ */
|