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