forked from achilikin/RdSpi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.c
182 lines (157 loc) · 5.2 KB
/
cli.c
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* BSD License
Copyright (c) 2015 Andrey Chilikin https://github.com/achilikin
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <unistd.h>
#include "cli.h"
#include "cmd.h"
static const char *shelp =
"general commands\n"
" exit|quit|q\n"
" cli debug on|off\n"
" cls: clear screen\n"
"si4703 commands\n"
"";
static int cli_proc(console_io_t *cli, char *arg, void *ptr);
static int cls_proc(console_io_t *cli, char *arg, void *ptr);
static int exit_proc(console_io_t *cli, char *arg, void *ptr);
static int help_proc(console_io_t *cli, char *arg, void *ptr);
static int reset_proc(console_io_t *cli, char *arg, void *ptr);
static int power_proc(console_io_t *cli, char *arg, void *ptr);
static int dump_proc(console_io_t *cli, char *arg, void *ptr);
static int spacing_proc(console_io_t *cli, char *arg, void *ptr);
static int scan_proc(console_io_t *cli, char *arg, void *ptr);
static int spectrum_proc(console_io_t *cli, char *arg, void *ptr);
static int tune_proc(console_io_t *cli, char *arg, void *ptr);
static int seek_proc(console_io_t *cli, char *arg, void *ptr);
static int volume_proc(console_io_t *cli, char *arg, void *ptr);
static int set_proc(console_io_t *cli, char *arg, void *ptr);
static int rds_proc(console_io_t *cli, char *arg, void *ptr);
typedef int (cmd_proc)(console_io_t *cli, char *arg, void *ptr);
static struct command_s {
const char *cmd;
cmd_proc *proc;
} clis[] = {
{ "cli", cli_proc },
{ "cls", cls_proc },
{ "help", help_proc },
{ "exit", exit_proc },
{ "quit", exit_proc },
{ "q", exit_proc },
{ "reset", reset_proc },
{ "power", power_proc },
{ "dump", dump_proc },
{ "spacing", spacing_proc },
{ "scan", scan_proc },
{ "spectrum", spectrum_proc },
{ "tune", tune_proc },
{ "seek", seek_proc },
{ "volume", volume_proc },
{ "set", set_proc },
{ "rds", rds_proc },
{ NULL, NULL }
};
extern cmd_t commands[];
int stdio_cli_handler(console_io_t *cli, void *ptr)
{
char *arg;
cmd_line_t *cl = (cmd_line_t *)cli->data;
for(unsigned i = 0; clis[i].proc != NULL; i++) {
if (cmd_arg(cl->cmd, clis[i].cmd, &arg))
return clis[i].proc(cli, arg, ptr);
}
return CLI_ENOTSUP;
}
int help_proc(console_io_t *cli, UNUSED(char *arg), UNUSED(void *ptr))
{
cli->puts(cli, shelp);
for(uint32_t i = 0; commands[i].name != NULL; i++) {
dprintf(cli->ofd, " %s: %s\n", commands[i].name, commands[i].help);
}
return CLI_EOK;
}
int exit_proc(UNUSED(console_io_t *cli), UNUSED(char *arg), UNUSED(void *ptr))
{
stop = 1;
return CLI_EOK;
}
int cls_proc(console_io_t *cli, UNUSED(char *arg), UNUSED(void *ptr))
{
cli->puts(cli, clrs);
return CLI_EOK;
}
int cli_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
cmd_line_t *cl = (cmd_line_t *)cli->data;
if (cmd_arg(arg, "debug", &arg)) {
if (cmd_is(arg, "on"))
cl->flags |= CLI_DEBUG;
else if (cmd_is(arg, "off"))
cl->flags &= ~CLI_DEBUG;
else
return CLI_EARG;
}
return CLI_EOK;
}
int reset_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_reset(cli->ofd, arg);
}
int power_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_power(cli->ofd, arg);
}
int dump_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_dump(cli->ofd, arg);
}
int spacing_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_spacing(cli->ofd, arg);
}
int scan_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_scan(cli->ofd, arg);
}
int spectrum_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_spectrum(cli->ofd, arg);
}
int tune_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_tune(cli->ofd, arg);
}
int seek_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_seek(cli->ofd, arg);
}
int volume_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_volume(cli->ofd, arg);
}
int set_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_set(cli->ofd, arg);
}
int rds_proc(console_io_t *cli, char *arg, UNUSED(void *ptr))
{
return cmd_monitor(cli->ofd, arg);
}