• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef _BSHELL_H_
16 #define _BSHELL_H_
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #ifdef __cplusplus
22 #if __cplusplus
23 extern "C" {
24 #endif
25 #endif
26 
27 #define PARAM_REVERESD_NAME_CURR_PARAMETER "_current_param_"
28 #define PARAM_SHELL_DEFAULT_PROMPT "param#"
29 
30 typedef struct BShellEnv_ *BShellHandle;
31 typedef int32_t (*BShellCmdExecuter_)(BShellHandle handle, int32_t argc, char *argv[]);
32 typedef int32_t (*BShellInput_)(char *, int32_t);
33 typedef int32_t (*BShellOutput_)(const char *, int32_t);
34 typedef int32_t (*BShellkeyHandle)(BShellHandle, uint8_t code);
35 
36 typedef enum {
37     BSH_ERRNO_BASE = 1000,
38     BSH_SHELL_INFO,
39     BSH_INVALID_PARAM,
40     BSH_CMD_TOO_LONG,
41     BSH_SHOW_CMD_LIST,
42     BSH_CMD_NOT_EXIST,
43     BSH_CMD_PARAM_INVALID,
44     BSH_SYSTEM_ERR,
45 } BShellErrNo;
46 
47 typedef struct BShellErrInfo_ {
48     BShellErrNo err;
49     char *desc;
50 } BShellErrInfo;
51 
52 typedef struct CmdInfo_ {
53     char *name;
54     BShellCmdExecuter_ executer;
55     char *desc;
56     char *help;
57     char *multikey;
58 } CmdInfo;
59 
60 typedef enum {
61     PARAM_INT8 = 0,
62     PARAM_INT16,
63     PARAM_INT32,
64     PARAM_STRING,
65 } BShellParamType;
66 
67 typedef struct ParamInfo_ {
68     char *name;
69     char *desc;
70     BShellParamType type;
71 } ParamInfo;
72 
73 typedef struct BShellParam_ {
74     char *desc;
75     BShellParamType type;
76     union {
77         char data;
78         uint8_t data8;
79         uint16_t data16;
80         uint32_t data32;
81         char *string;
82     } value;
83     struct BShellParam_ *next;
84     char name[0];
85 } BShellParam;
86 
87 typedef struct BShellInfo_ {
88     char *prompt;
89     BShellInput_ input;
90 } BShellInfo;
91 
92 int BShellEnvInit(BShellHandle *handle, const BShellInfo *info);
93 int BShellEnvStart(BShellHandle handle);
94 void BShellEnvDestory(BShellHandle handle);
95 
96 int BShellEnvRegisterCmd(BShellHandle handle, const CmdInfo *cmdInfo);
97 int BShellEnvSetParam(BShellHandle handle, const char *name, const char *desc, BShellParamType type, void *value);
98 const BShellParam *BShellEnvGetParam(BShellHandle handle, const char *name);
99 int BShellEnvRegisterKeyHandle(BShellHandle handle, uint8_t code, BShellkeyHandle keyHandle);
100 void BShellEnvLoop(BShellHandle handle);
101 const ParamInfo *BShellEnvGetReservedParam(BShellHandle handle, const char *name);
102 
103 int32_t BShellEnvOutput(BShellHandle handle, char *fmt, ...);
104 int32_t BShellEnvOutputString(BShellHandle handle, const char *string);
105 int32_t BShellEnvOutputPrompt(BShellHandle handle, const char *prompt);
106 
107 int32_t BShellCmdHelp(BShellHandle handle, int32_t argc, char *argv[]);
108 int32_t BShellEnvDirectExecute(BShellHandle handle, int argc, char *args[]);
109 #ifdef __cplusplus
110 #if __cplusplus
111 }
112 #endif
113 #endif
114 #endif