-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.c
More file actions
157 lines (137 loc) · 4.29 KB
/
cli.c
File metadata and controls
157 lines (137 loc) · 4.29 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
#include "cli.h"
#include "ucterm.h"
#include <stdint.h>
#include <string.h>
// Number of avaiable commands
#define MAX_CLI_COMMANDS 2
// TODO modify according to your needs
// CLI command handler function type
typedef void (*CliCommandHandle_t)(uint8_t argc, const uint8_t *argv[]);
// CLI command definition type
typedef struct
{
const char *name; // command name
CliCommandHandle_t handler; // function to execute
const char *help; // short help string
} CliCommand_t;
/* Command handler fuction prototypes (internal) */
static void cmd_help(uint8_t argc, const uint8_t *argv[]);
static void cmd_uname(uint8_t argc, const uint8_t *argv[]);
// TODO add yours
/* Command handler fuction prototypes (external) */
// TODO add your definitions to cli.h and implement outside
/* Ucterm callback fuction prototypes - interface-specific */
static inline uint8_t _uart_get_char(void); // TODO your implementation
static inline void _uart_send_char(uint8_t c); // TODO your implementation
static inline void _uart_send_str(const uint8_t *str); // TODO your implementation
static inline void _execute(uint8_t argc, uint8_t *argv[]);
/* Internal state storage */
static UcTerm_HandleTypeDef _hterm;
static const CliCommand_t _commands[MAX_CLI_COMMANDS] = {
{
"help",
cmd_help,
"List available commands or show details with \x1B[1mhelp <command>\x1B[0m.",
},
{
"uname",
cmd_uname,
"Display system info."
},
// TODO add your commands here
};
/* Public interface implementation */
void CliInit(void)
{
UcTerm_Init(&_hterm);
UcTerm_RegisterPrintCharCallback(&_hterm, &_uart_send_char);
UcTerm_RegisterPrintStrCallback(&_hterm, &_uart_send_str);
UcTerm_RegisterExecuteCallback(&_hterm, &_execute);
UcTerm_ShowPrompt(&_hterm);
}
void CliUpdate(void)
{
// read char from UART RX buffer and pass it to Ucterm
uint8_t c = _uart_get_char();
if ('\0' == c)
{
return;
}
UcTerm_IngestChar(&_hterm, c);
}
/* Private functions implementation */
static inline void _execute(uint8_t argc, uint8_t *argv[])
{
if (argc == 0)
{
return;
}
for (uint8_t i = 0; i < MAX_CLI_COMMANDS; i++)
{
// first symbol pre-filter
if (argv[0][0] != _commands[i].name[0])
{
continue;
}
// call handler on command name match
// or show help if requested
if (strcmp((char *)argv[0], _commands[i].name) == 0)
{
if (argc == 2 && argv[1][0] == '-')
{
if (strcmp((char *)argv[1], "-h") == 0 ||
strcmp((char *)argv[1], "--help") == 0)
{
_uart_send_str(_commands[i].help);
return;
}
}
_commands[i].handler(argc, (const uint8_t **)argv);
return;
}
}
_uart_send_str("Unknown command!");
}
static inline uint8_t _uart_get_char(void)
{
// TODO read a char from UART or
// whatever interface you use
}
static inline void _uart_send_char(uint8_t c)
{
// TODO send ch via UART or
// whatever interface you use
}
static inline void _uart_send_str(const uint8_t *str)
{
// TODO send *str via UART or
// whatever interface you use
}
/* Command handlers implementation */
static void cmd_help(uint8_t argc, const uint8_t *argv[])
{
// if "help <command>" - print help of a particular command;
// else list available commands
if (argc == 2)
{
for (uint8_t i = 0; i < MAX_CLI_COMMANDS; i++)
{
if (strcmp((char *)argv[1], _commands[i].name) == 0)
{
_uart_send_str(_commands[i].help);
return;
}
}
}
_uart_send_str("Available commands:\x1B[1m");
for (uint8_t i = 0; i < MAX_CLI_COMMANDS; i++)
{
_uart_send_char('\t');
_uart_send_str(_commands[i].name);
}
_uart_send_str("\x1B[0m\r\nTry \x1B[1m-h\x1B[0m, \x1B[1m--help\x1B[0m or \x1B[1mhelp <command>\x1B[0m for details.");
}
static void cmd_uname(uint8_t argc, const uint8_t *argv[])
{
_uart_send_str("Hello world!\r\n");
}