-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.h
More file actions
167 lines (127 loc) · 3.77 KB
/
Copy pathcli.h
File metadata and controls
167 lines (127 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef __CLI_H__
#define __CLI_H__
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/****************************************************************************
*
* Constants.
*
****************************************************************************/
#ifndef __ENABLE_LOGIN__
/* allows external overwrite */
#define __ENABLE_LOGIN__ (1)
#endif
#define MAX_TOKENS (20)
/****************************************************************************
*
* Types.
*
****************************************************************************/
/**
* The function pointer prototype to get a characeter from input source.
*
* Generally, standard getchar() can be used for this purpose. But other
* implementation can also be used. For example, in semihosting mode, the
* standard getchar() is replaced by debug adapter. But to make CLI work as
* normal over telnet, a replacement getchar, which talks to telnet, can be
* used.
*
* @note The standard getch() returns int, which may return someothing
* other than a character from user. To reduce the CPU consumption
* of mini-CLI, the input source must be configured/written carefully
* to not return non-characters.
*/
typedef char (*getch_fptr)(void);
/**
* The function pointer prototype to put a characeter to an output target.
*
* Generally, standard putchar() can be used for this purpose. But other
* implementation can also be used. For example, in semihosting mode, the
* standard putchar() is replaced by debug adapter. But to make CLI work as
* normal over telnet, a replacement putchar, which talks to telnet, can be
* used.
*
* @note
*/
typedef void (*putch_fptr)(char);
#if __ENABLE_LOGIN__
/**
* If login is enabled and hardcode is not used. This is the callback function
* that mini-CLI will call to authenicate the user.
*
* @retval 0 if validation of the combination of 'id' and 'pass' failed.
* other values if succeeded.
*/
typedef uint8_t (*knock_fptr)(char *id, char *pass);
#endif
/**
* Function pointer type of CLI command handlers.
*
* All CLI handlers must follow this prototype. An example is cli_logout().
*
* @note Currently, the return value of CLI handlers are ignore. However,
* to keep backward compatiblity, CLI handlers must return 0.
*
*/
typedef uint8_t (*fp_t)(uint8_t len, char *param);
/**
* Forward declare the type of cmd_t such that ancient compilers won't
* complain.
*/
typedef struct cmd_s cmd_t;
struct cmd_s {
char *cmd;
char *help;
fp_t fptr;
cmd_t *sub; ///< sub commands
};
typedef struct cli_s {
uint8_t state; ///< 0 if not logged in
cmd_t *cmd;
getch_fptr get;
putch_fptr put;
#if __ENABLE_LOGIN__
knock_fptr knock;
#endif
char *tok[MAX_TOKENS];
} cli_t;
/****************************************************************************
*
* Function prototypes.
*
****************************************************************************/
/**
* Initialization.
*/
void cli_init(cli_t *cli);
/**
* The top-level function of the actual CLI.
*
* This function will never exit unless the user logged out.
*/
void cli_task(void);
/**
* Print a text string.
*/
void cli_puts(char *s);
/**
* The function that implements the logout function.
*
* @note Implemented in mini-CLI to reduce the need for mini-CLI users
* (developers) because it is a must.
*/
uint8_t cli_logout(uint8_t len, char *param);
void cli_putc(char c);
void cli_putd(int dec);
void cli_putln(void);
void cli_putsp(void);
void cli_putX(uint32_t hex);
void cli_putx(uint32_t hex);
void cli_put0x(uint32_t hex);
void cli_put0X(uint32_t hex);
#ifdef __cplusplus
}
#endif
#endif /* __CLI_H__ */