blob: 30d873dd5fcd0e3b0b8c4bce27350bd50477f9b7 [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001/*
2<:copyright-BRCM:2016:DUAL/GPL:standard
3
4 Broadcom Proprietary and Confidential.(c) 2016 Broadcom
5 All Rights Reserved
6
7Unless you and Broadcom execute a separate written software license
8agreement governing use of this software, this software is licensed
9to you under the terms of the GNU General Public License version 2
10(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
11with the following added to such license:
12
13 As a special exception, the copyright holders of this software give
14 you permission to link this software with independent modules, and
15 to copy and distribute the resulting executable under terms of your
16 choice, provided that you also meet, for each linked independent
17 module, the terms and conditions of the license of that module.
18 An independent module is a module which is not derived from this
19 software. The special exception does not apply to any modifications
20 of the software.
21
22Not withstanding the above, under no circumstances may you combine
23this software in any way with any other Broadcom software provided
24under a license other than the GPL, without Broadcom's express prior
25written consent.
26
27:>
28 */
29
30
31/*******************************************************************
32 * bcmcli_session.h
33 *
34 * BCM CLI engine - session management
35 *
36 *******************************************************************/
37
38#ifndef BCMCLI_SESSION_H
39
40#define BCMCLI_SESSION_H
41
42#include <bcmos_system.h>
43#include <stdarg.h>
44#include "bcmolt_string.h"
45
46/** \defgroup bcmcli_session Management Session Control
47 *
48 * APIs in this header file allow to create/destroy management sessions.
49 * Management session is characterized by its access level and also
50 * input/output functions.
51 * Management sessions allow managed entities in the system to communicate
52 * with local or remote managers (e.g., local or remote shell or NMS)
53 * @{
54 */
55
56/** Access rights */
57typedef enum
58{
59 BCMCLI_ACCESS_GUEST, /**< Guest. Doesn't have access to commands and directories registered with ADMIN rights */
60 BCMCLI_ACCESS_ADMIN, /**< Administrator: full access */
61 BCMCLI_ACCESS_DEBUG, /**< Administrator: full access + extended debug features */
62} bcmcli_access_right;
63
64/** Line edit mode */
65typedef enum
66{
67 BCMCLI_LINE_EDIT_DEFAULT,/**< Enable line editing and history if CONFIG_EDITLINE is defined, disable otherwise */
68 BCMCLI_LINE_EDIT_ENABLE, /**< Enable line editing. Requires CONFIG_EDITLINE define and libedit-dev library */
69 BCMCLI_LINE_EDIT_DISABLE,/**< Disable line editing and history */
70} bcmcli_line_edit_mode;
71
72
73/** Management session handle
74 */
75typedef struct bcmcli_session bcmcli_session;
76
77
78/** Session parameters structure.
79 * See \ref bcmcli_session_open
80 */
81typedef struct bcmcli_session_parm
82{
83 const char *name; /**< Session name */
84 void *user_priv; /**< Private user's data */
85
86 /** Session's output function. NULL=use write(stdout)
87 * returns the number of bytes written or <0 if error
88 */
89 int (*write)(bcmcli_session *session, const char *buf, uint32_t size);
90
91 /** Session line input function. NULL=use default(stdin[+line edit) */
92 char *(*gets)(bcmcli_session *session, char *buf, uint32_t size);
93
94 /** Session char input function. NULL=use bcmos_getchar() */
95 int (*get_char)(bcmcli_session *session);
96
97 /** Fill the specified buffer with the prompt for this session (NULL-terminated). NULL = no prompt. */
98 void (*get_prompt)(bcmcli_session *session, char *buf, uint32_t max_len);
99
100 /** Access rights */
101 bcmcli_access_right access_right;
102
103 /** Line editing mode */
104 bcmcli_line_edit_mode line_edit_mode;
105
106 /** Extra data size to be allocated along with session control block.
107 * The extra data is accessible using bcmcli_session_data().
108 * Please note that if session is created using bcmcli_session_open(),
109 * extra_size is reserved.
110 * It can only be used for user context allocation if session is created
111 * using bcmcli_session_open_user()
112 */
113 uint32_t extra_size;
114} bcmcli_session_parm;
115
116
117/** Open monitor session
118 *
119 * Monitor supports multiple simultaneous sessions with different
120 * access rights.
121 * Note that there already is a default session with full administrative rights,
122 * that takes input from stdin and outputs to stdout.
123 *
124 * Please don't use parm.extra_size. This field is reserved.
125 *
126 * \param[in] parm Session parameters. Must not be allocated on the stack.
127 * \param[out] p_session Session handle
128 * \return
129 * 0 =OK\n
130 * <0 =error code
131 */
132bcmos_errno bcmcli_session_open(const bcmcli_session_parm *parm, bcmcli_session **p_session);
133
134
135/** Close monitor session.
136 * \param[in] session Session handle
137 */
138void bcmcli_session_close(bcmcli_session *session );
139
140
141/** Write function.
142 * Write buffer to the current session.
143 * \param[in] session Session handle. NULL=use stdout
144 * \param[in] buf output buffer
145 * \param[in] size number of bytes to be written
146 * \return
147 * >=0 - number of bytes written\n
148 * <0 - output error
149 */
150int bcmcli_session_write(bcmcli_session *session, const char *buf, uint32_t size);
151
152
153/** Read line
154 * \param[in] session Session handle. NULL=use default
155 * \param[in,out] buf input buffer
156 * \param[in] size buf size
157 * \return
158 * buf if successful
159 * NULL if EOF or error
160 */
161char *bcmcli_session_gets(bcmcli_session *session, char *buf, uint32_t size);
162
163
164/** Print function.
165 * Prints in the context of current session.
166 * \param[in] session Session handle. NULL=use stdout
167 * \param[in] format print format - as in printf
168 */
169void bcmcli_session_print(bcmcli_session *session, const char *format, ...)
170#ifndef BCMCLI_SESSION_DISABLE_FORMAT_CHECK
171__attribute__((format(printf, 2, 3)))
172#endif
173;
174
175
176/** Print function.
177 * Prints in the context of current session.
178 * \param[in] session Session handle. NULL=use stdout
179 * \param[in] format print format - as in printf
180 * \param[in] ap parameters list. Undefined after the call
181 */
182void bcmcli_session_vprint(bcmcli_session *session, const char *format, va_list ap);
183
184/** Print buffer in hexadecimal format
185 * \param[in] session Session handle. NULL=use stdout
186 * \param[in] buffer Buffer address
187 * \param[in] offset Start offset in the buffer
188 * \param[in] count Number of bytes to dump
189 * \param[in] indent Optional indentation string
190 */
191void bcmcli_session_hexdump(bcmcli_session *session, const void *buffer, uint32_t offset, uint32_t count, const char *indent);
192
193/** Get extra data associated with the session
194 * \param[in] session Session handle. NULL=default session
195 * \return extra_data pointer or NULL if there is no extra data
196 */
197void *bcmcli_session_data(bcmcli_session *session);
198
199
200/** Get user_priv provided in session parameters when it was registered
201 * \param[in] session Session handle. NULL=default session
202 * \return usr_priv value
203 */
204void *bcmcli_session_user_priv(bcmcli_session *session);
205
206
207/** Get session name
208 * \param[in] session Session handle. NULL=use stdin
209 * \return session name
210 */
211const char *bcmcli_session_name(bcmcli_session *session);
212
213
214/** Get session access rights
215 * \param[in] session Session handle. NULL=default debug session
216 * \return session access right
217 */
218bcmcli_access_right bcmcli_session_access_right(bcmcli_session *session);
219
220/** @} end of bcmcli_session group */
221
222/** Low-level interface for when session is used outside CLI
223 *
224 * \param[in] parm Session parameters. Must not be allocated on the stack.
225 * \param[out] p_session Session handle
226 * \return
227 * 0 =OK\n
228 * <0 =error code
229 */
230int bcmcli_session_open_user(const bcmcli_session_parm *parm, bcmcli_session **p_session);
231
232/** Open a session that prints to the specified string
233 */
234bcmos_errno bcmcli_session_open_string(bcmcli_session **session, bcmolt_string *str);
235
236/** Configure RAW input mode
237 *
238 * \param[in] session Session handle
239 * \param[in] is_raw TRUE=enable raw mode, FALSE=disable raw mode
240 * \return
241 * =0 - OK \n
242 * BCM_ERR_NOT_SUPPORTED - raw mode is not supported\n
243 */
244bcmos_errno bcmcli_session_raw_mode_set(bcmcli_session *session, bcmos_bool is_raw);
245
246/** Context extension
247 *
248 * - if no command - display list of command or extend command
249 * - if prev char is " "
250 * - if positional and next parm is enum - show/extends list of matching values
251 * - else - show/extend list of unused parameters
252 * else
253 * - if entering value and enum - show/extends list of matching values
254 * - else - show/extend list of matching unused parameters
255 *
256 * \param[in] session Session handle
257 * \param[in] input_string String to be parsed
258 * \param[out] insert_str String to insert at cursor position
259 * \param[in] insert_size Insert buffer size
260 * \return
261 * =0 - OK \n
262 * BCM_ERR_PARM - parsing error\n
263 */
264bcmos_errno bcmcli_extend(bcmcli_session *session, char *input_string,
265 char *insert_str, uint32_t insert_size);
266
267
268#ifdef BCMCLI_INTERNAL
269
270#define BCMCLI_SESSION_OUTBUF_LEN 2048
271#define BCMCLI_MAX_PROMPT_LEN 8
272
273/* editline functionality */
274/* If libedit is included - it takes precedence */
275#ifdef CONFIG_LIBEDIT
276#include <histedit.h>
277#undef CONFIG_LINENOISE
278#endif /* #ifdef CONFIG_LIBEDIT */
279
280#ifdef CONFIG_LINENOISE
281#include <linenoise.h>
282#endif
283
284/* Management session structure */
285struct bcmcli_session
286{
287 bcmcli_session *next;
288 bcmcli_session_parm parms;
289 uint32_t magic;
290#define BCMCLI_SESSION_MAGIC (('s'<<24)|('e'<<16)|('s'<<8)|'s')
291#define BCMCLI_SESSION_MAGIC_DEL (('s'<<24)|('e'<<16)|('s'<<8)|'~')
292
293 /* Line editing and history support */
294#ifdef CONFIG_LIBEDIT
295 EditLine *el;
296 History *history;
297 HistEvent histevent;
298#endif
299#ifdef CONFIG_LINENOISE
300 linenoiseSession *ln_session;
301#endif
302 char outbuf[BCMCLI_SESSION_OUTBUF_LEN];
303 char prompt_buf[BCMCLI_MAX_PROMPT_LEN];
304
305 /* Followed by session data */
306};
307#endif
308
309#endif /* #ifndef BCMCLI_SESSION_H */