O-CU-UP
[scp/ocu/5gnr.git] / Include / vos_cli.h
1 /******************************************************************************
2 ###############################################################################
3 #   Copyright (c) [2017-2020] [ICT/CAS]                                        #
4 #   Licensed under the ORAN Software License v1.0 (License)             #
5 ###############################################################################
6 ******************************************************************************/
7
8
9 #ifndef __VOS_CLI_H__
10 #define __VOS_CLI_H__
11 #include "vos_types.h"
12
13 #ifdef    __cplusplus
14 extern "C"
15 {
16 #endif /* __cplusplus */
17
18 /** 历史命令记录条数 */
19 #define VTY_MAXHIST 20
20
21 /** 命令行提示符长度 */
22 #define VTY_CMD_PROMPT_MAX  64
23
24 /** 节点嵌套层数 */
25 #define VTY_NODE_HIS_MAX 8
26
27 /* 换行字符串 */
28 #define VTY_NEWLINE  ((vty->type == VTY_TERM) ? "\r\n" : "\r\n")
29
30 /** show 命令说明字符串 */
31 #define SHOW_STR "Show running system information\n"
32
33 /** config 命令说明字符串 */
34 #define CONFIG_STR "Config system's setting\n"
35
36 /** no 命令说明字符串 */
37 #define NO_STR "Negate a command or set its defaults\n"
38
39 /** debug 命令说明字符串 */
40 #define DEBUG_STR "Debugging functions\n"
41
42 /** 命令回调函数返回值 命令执行 OK */
43 #define CMD_RET_OK       (0)
44
45 /** 命令回调函数返回值 命令执行 ERROR */
46 #define CMD_RET_ERROR    (1)
47
48
49
50 /** VOS 预定义的节点 */
51 enum node_type
52 {
53     VOS_CLI_VIEW_NODE,              ///<  View node. Default mode of vty interface. 
54     VOS_CLI_CONFIG_NODE,            ///<  Config node. Default mode of config file. 
55     VOS_CLI_DEBUG_HIDDEN_NODE,      ///<  Debug hidden node. 
56     VOS_CLI_CONFIRM_ACTION_NODE,    ///<  Confirm action.
57     VOS_CLI_RESV_MAX_NODE           ///<  Vos max reserved node ID
58 };
59
60
61 struct exit_use_node
62 {
63     int   node_in_use;                          ///<  使用标记
64     enum node_type node;                        ///<  节点ID
65     void *context_data;                         ///<  记录命令行上下文数据,如进入端口节点可以记录端口号
66     INT  context_data_need_free;                ///<  标记context_data 是否需要free,在exit 命令中会执行VOS_free
67     CHAR  vty_cmd_prompt[ VTY_CMD_PROMPT_MAX ]; ///<  自定义提示符
68 };
69
70
71 /** VTY 结构体,只需关心fd之前的成员. */
72 struct vty
73 {
74     char vty_cmd_prompt[VTY_CMD_PROMPT_MAX];   ///<  自定义提示符
75
76     VOID *context_data;                        ///<  记录命令行上下文数据,如进入端口节点可以记录端口号
77
78     INT context_data_need_free;                ///<  标记context_data 是否需要free,在exit 命令中会执行VOS_free
79
80     int (*action_func) (struct vty *vty);      ///<  confirm action 的回调函数
81
82     int (*no_action_func) (struct vty *vty);   ///<  refuse action  的回调函数
83
84     /* 文件描述符 */
85     int fd;
86
87     /* 当前节点ID */
88     enum node_type node;
89
90     /* 上一节点ID */
91     enum node_type prev_node;
92
93     /* VTY 类型 */
94     enum { VTY_TERM, }type;
95
96     /* client IP */
97     char *address;
98
99     /* vty 输出缓冲区 */
100     struct buffer *obuf;
101
102     /* obuf 的锁 */
103     LONG  Sem_obuf ;
104
105     /* vty 命令输入缓冲区 */
106     char *buf;
107
108     /* 当前光标的位置 */
109     int cp;
110
111     /* 当前输入的长度 */
112     int length;
113
114     /* buf 的大小. */
115     int max;
116
117     /* 历史命令 */
118     char *hist[VTY_MAXHIST];
119
120     /* 查看历史命令时记录当前索引 */
121     int hp;
122
123     /* 历史命令条数 */
124     int hindex;
125
126     struct exit_use_node node_his_save[VTY_NODE_HIS_MAX];
127
128     /* 转义字符状态. */
129     unsigned char escape;
130
131     /* vty 状态. */
132     enum { VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_START, VTY_CONTINUE, VTY_IOREDIRECT }status;
133
134     /* telnet IAC handling */
135     unsigned char iac;
136
137     /* telnet IAC SB handling */
138     unsigned char iac_sb_in_progress;
139     /* telnet IAC SB buffer */
140     struct buffer *sb_buffer;
141
142     /* 窗口宽度,暂未使用 */
143     int width;
144     /* 窗口高度,lines为0时有效 */
145     int height;
146
147     /* 一次输出的做大行数 */
148     int lines;
149
150     /* 已进入config节点,用于限制多用户进入config */
151     int config;
152
153     /* 输入监控任务 */
154     struct cl_lib_thread *t_read;
155     /* 输出监控任务 */
156     struct cl_lib_thread *t_write;
157
158     /*VTY 超时时间,秒 */
159     unsigned long v_timeout;
160
161     /*VTY 超时处理任务 */
162     struct cl_lib_thread *t_timeout;
163
164     /*VTY 退出标志位 */
165     unsigned short cl_vty_exit;
166
167     /* vty 接入类型console,telnet, telnet IPv6 */
168     int conn_type;
169     
170     /* vty 接入用户名 */
171     char *user_name;
172
173     /* vty 会话任务 */
174     struct cl_lib_thread_master *sub_thread_master;
175     
176     /* vty 会话任务handle */
177     VOS_HANDLE sub_task_id;
178
179     /* vty 调试标志位 */
180     unsigned short debug_monitor;  
181
182     /************************************
183     frozen vty in and(or) out 
184     used for if a command can't return immediately
185     then call frozen to freeze vty's in and(or) out
186     call unfrozen when finished.
187     1: freezed
188     0: unfreezed
189     ***********************************/
190     unsigned short frozen;
191
192     //VOS_SYSLOG_MONITOR_CONF  monitor_conf;
193     int veryBigOutLineCount;
194     int veryBigOutNoFirst;
195
196     /* vty 引用计数,当引用计数为0的时候调用vty_close释放内存 */
197     int ncount;
198 };
199
200
201 /** cl_vector 动态数组指针 */
202 struct _cl_vector 
203 {
204     unsigned int max;       ///< 当前最大索引加1 
205     unsigned int alloced;   ///< 数组大小 */
206     void **index;           ///< 指针数组
207 };
208 typedef struct _cl_vector *cl_vector;
209
210
211 /** 命令结构体 */
212 struct cmd_element
213 {
214     char *string;                ///< 命令字符串,多个用空格分隔的单词组成的句子
215     int (*func) (struct cmd_element *, struct vty *, int, char **); ///< 命令回调函数
216     char *doc;                   ///< 命令说明,对命令字符串中的每个单词进行说明
217     int msg_flag;        ///< 命令类型标识,当前只有 DEFUN_FLAG 类型
218     cl_vector strvec;    ///< 将 string和doc 拆分后放入该数组中 ,存放struct desc 
219     int cmdsize;                 ///< string 中单词个数
220     cl_vector subconfig; ///< 将 string中可以重复的单词的重复位置和重复次数等 
221 };
222
223
224 /** 命令节点结构体 */
225 struct cmd_node
226 {
227     enum node_type node_id;           ///< 节点ID
228     char prompt[VTY_CMD_PROMPT_MAX];  ///< 节点提示符
229     cl_vector cmd_vector;             ///< 节点命令数组
230 };
231
232
233 #define DEFUN_FLAG     0
234
235 /** 
236  * 命令行定义宏
237  * @param[in]   funcname   回调函数的函数名
238  * @param[in]   cmdname    命令变量的变量名
239  * @param[in]   cmdstr     命令字符串
240  * @param[in]   helpstr    命令字符串中各个单词的注解
241  * @return      返回输出的字节数
242  */
243 #define DEFUN(funcname, cmdname, cmdstr, helpstr)   \
244         int funcname (struct cmd_element *, struct vty *, int, char **);\
245         struct cmd_element cmdname =   {cmdstr,     funcname,     helpstr , DEFUN_FLAG};\
246         int funcname  (struct cmd_element *self, struct vty *vty, int argc, char **argv)
247
248
249 /** 
250  * 命令行回调函数中的标准输出函数,
251  * 注意 LONG 输出时,要用%ld,或%lx
252  * @param[in]   vty     vty
253  * @param[in]   format  格式字符串
254  * @param[in]   ...     要输出的变量
255  * @return      返回输出的字节数
256  */
257 INT vty_out ( struct vty * vty, const CHAR * format, ... );
258
259 /** 
260  * 创建并安装命令节点
261  * @param[in]   prompt         节点提示符,用于命令行提示符的显示
262  * @return      成功返回 一个新节点的指针,失败返回NULL
263  */
264 struct cmd_node *VOS_cli_create_node(char prompt[VTY_CMD_PROMPT_MAX]);
265
266
267 /** 
268  * 把命令安装到指定节点
269  * @param[in]   node_id     节点ID
270  * @param[in]   cmd         命令
271  * @return      成功返回 VOS_OK,失败返回其他
272  */
273 VOID VOS_cli_install_element( enum node_type node_id, struct cmd_element * cmd );
274
275
276 /** 
277  * 在进入节点函数中调用,用于进入节点
278  * @param[in]   vty       vty
279  * @param[in]   newnode   要进入的节点ID
280  * @return      成功返回 VOS_OK,失败返回其他
281  */
282 LONG  vty_enter_node(struct vty *vty,enum node_type newnode);
283
284
285 /** 
286  * 在进入节点函数中调用,用于自定义要进入节点的命令行提示符,
287  * 调用vty_enter_node后调用
288  * @param[in]   vty       vty
289  * @param[in]   cpPrompt  提示符
290  */
291 VOID vty_set_prompt( struct vty * vty, CHAR cpPrompt[VTY_CMD_PROMPT_MAX] );
292
293
294
295
296 /**
297  * Completion match types. 匹配度,数值越大匹配度越高,命令行模块内部使用无需关心
298  */
299 enum match_type
300 {
301     no_match,
302     incomplete_match,
303     ambiguous_match,
304     extend_match,
305     vararg_match,
306     one_element_two_partly_match,
307     partly_match,
308     almost_exact_match,   /* exactmatch except last word */
309     ipv4_prefix_match,
310     ipv4_match,
311     ipv6_prefix_match,
312     ipv6_match,
313
314     aann_match,
315     mac_match,
316     slot_port_match,
317     slot_subport_match,
318     
319     time_match,
320     range_match,
321
322     /*其余的匹配类型在此之前*/
323     register_match,
324     
325     exact_match
326 };
327
328
329 /** 自定义命令行参数解析函数  */
330 typedef struct _cmd_notify_register_tag
331 {
332     CHAR *match_str ;                            ///<  匹配的格式,如<H.H.H>
333     enum match_type (*pnotify_call) (CHAR *str); ///<  格式解析的回调函数,解析成功返回exact_match,解析失败返回no_match
334     CHAR IsCapital ;                             ///<  是否包含大写字母 0 - 全小写; 1 - 含有大写字母
335 }CMD_NOTIFY_REFISTER_S;
336
337 /** 
338  * 添加自定义命令行参数解析函数
339  * @param[in]   pNotifyRegister   自定义解析函数信息
340  * @return      成功返回 register_match,失败返回no_match
341  */
342 enum match_type cmd_rugular_register(CMD_NOTIFY_REFISTER_S * pNotifyRegister);
343
344 /** 
345  * 配置/获取 vty超时时间
346  * @param[in ]   vty         vty
347  * @param[in ]   newTime     新的超时时间,大于等于0时有效,0表示不会超时
348  * @param[out]   oldTime     如果不为NULL 返回配置前的超时时间
349  * @return      成功返回 VOS_OK,失败则返回其他
350  */ 
351 LONG vty_timeout_config ( struct vty * vty, LONG newTime, LONG *oldTime);
352
353
354 /** 
355  * 要使用vty_very_big_out 函数必须先调用此初始化函数 
356  * @param[in ]   vty         vty
357  */
358 void vty_very_big_out_init ( struct vty *vty );
359
360 /** 
361  * 用于长字符串打印 
362  * @param[in]   vty     vty
363  * @param[in]   format  格式字符串
364  * @param[in]   ...     要输出的变量
365  */
366 int vty_very_big_out (struct vty *vty, const char *format, ...);
367
368
369 #ifdef __cplusplus
370 }
371 #endif /* __cplusplus */
372 #endif /* __VOS_CLI_H__ */