add simple command parser to be used from the UART
This commit is contained in:
parent
e81bf0ed6f
commit
41f8d83364
|
@ -0,0 +1,67 @@
|
|||
/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
|
||||
*
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _UART_CMD_H
|
||||
#define _UART_CMD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdarg.h>
|
||||
#include <linuxlist.h>
|
||||
|
||||
enum cmd_op {
|
||||
CMD_OP_GET = (1 << 0),
|
||||
CMD_OP_SET = (1 << 1),
|
||||
CMD_OP_EXEC = (1 << 2),
|
||||
};
|
||||
|
||||
enum pstate {
|
||||
ST_IN_CMD,
|
||||
ST_IN_ARG,
|
||||
};
|
||||
|
||||
struct strbuf {
|
||||
uint8_t idx;
|
||||
char buf[16];
|
||||
};
|
||||
|
||||
struct cmd_state {
|
||||
struct strbuf cmd;
|
||||
struct strbuf arg;
|
||||
enum pstate state;
|
||||
void (*out)(const char *format, va_list ap);
|
||||
};
|
||||
|
||||
int uart_cmd_out(struct cmd_state *cs, char *format, ...);
|
||||
int uart_cmd_char(struct cmd_state *cs, uint8_t ch);
|
||||
int uart_cmd_reset(struct cmd_state *cs);
|
||||
|
||||
struct cmd {
|
||||
const char *cmd;
|
||||
uint32_t ops;
|
||||
int (*cb)(struct cmd_state *cs, enum cmd_op op, const char *cmd, const char *arg);
|
||||
const char *help;
|
||||
/* put list at the end for simpler initialization */
|
||||
struct llist_head list;
|
||||
};
|
||||
|
||||
void uart_cmd_register(struct cmd *c);
|
||||
void uart_cmd_unregister(struct cmd *c);
|
||||
void uart_cmds_register(struct cmd *c, unsigned int num);
|
||||
|
||||
|
||||
#endif
|
|
@ -0,0 +1,183 @@
|
|||
|
||||
/* (C) 2012 by Harald Welte <laforge@gnumonks.org>
|
||||
*
|
||||
* All Rights Reserved
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <uart_cmd.h>
|
||||
|
||||
void strbuf_reset(struct strbuf *sb)
|
||||
{
|
||||
sb->idx = 0;
|
||||
memset(sb->buf, 0, sizeof(sb->buf));
|
||||
}
|
||||
|
||||
void strbuf_append(struct strbuf *sb, char ch)
|
||||
{
|
||||
if (sb->idx < sizeof(sb->buf)-1)
|
||||
sb->buf[sb->idx++] = ch;
|
||||
}
|
||||
|
||||
static LLIST_HEAD(cmd_list);
|
||||
|
||||
void uart_cmd_register(struct cmd *c)
|
||||
{
|
||||
llist_add_tail(&c->list, &cmd_list);
|
||||
}
|
||||
|
||||
void uart_cmd_unregister(struct cmd *c)
|
||||
{
|
||||
llist_del(&c->list);
|
||||
}
|
||||
|
||||
void uart_cmds_register(struct cmd *c, unsigned int num)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
uart_cmd_register(&c[i]);
|
||||
}
|
||||
|
||||
static int handle_cb(struct cmd_state *cs, int op, char *cmd, char *arg)
|
||||
{
|
||||
struct cmd *c;
|
||||
int rc;
|
||||
|
||||
if (!arg)
|
||||
arg = "";
|
||||
|
||||
//printf("handle_cb(%s, %s)\n", cmd, arg);
|
||||
|
||||
llist_for_each_entry(c, &cmd_list, list) {
|
||||
if (!strcmp(c->cmd, cmd)) {
|
||||
if (!(c->ops & op)) {
|
||||
uart_cmd_out(cs, "Command `%s' doesn't "
|
||||
"support this operation\n\r", c->cmd);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
rc = c->cb(cs, op, cmd, arg);
|
||||
if (rc < 0)
|
||||
uart_cmd_out(cs, "Error executing command\n\r");
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
uart_cmd_out(cs, "Unknown command `%s'\n\r", cmd);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
static void print_list(struct cmd_state *cs)
|
||||
{
|
||||
struct cmd *c;
|
||||
|
||||
uart_cmd_out(cs, "Supported commands:\r\n");
|
||||
|
||||
llist_for_each_entry(c, &cmd_list, list){
|
||||
const char *help = "";
|
||||
|
||||
if (c->help)
|
||||
help = c->help;
|
||||
|
||||
uart_cmd_out(cs, "%s %s\n\r", c->cmd, c->help);
|
||||
}
|
||||
}
|
||||
|
||||
int uart_cmd_char(struct cmd_state *cs, uint8_t ch)
|
||||
{
|
||||
int rc;
|
||||
|
||||
uart_cmd_out(cs, "%c", ch);
|
||||
|
||||
switch (cs->state) {
|
||||
case ST_IN_CMD:
|
||||
switch (ch) {
|
||||
case '=':
|
||||
cs->state = ST_IN_ARG;
|
||||
break;
|
||||
case '?':
|
||||
uart_cmd_out(cs, "\n\r");
|
||||
if (cs->cmd.idx == 0)
|
||||
print_list(cs);
|
||||
else
|
||||
rc = handle_cb(cs, CMD_OP_GET, cs->cmd.buf, NULL);
|
||||
uart_cmd_reset(cs);
|
||||
break;
|
||||
case '!':
|
||||
uart_cmd_out(cs, "\n\r");
|
||||
rc = handle_cb(cs, CMD_OP_EXEC, cs->cmd.buf, NULL);
|
||||
uart_cmd_reset(cs);
|
||||
break;
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\t':
|
||||
/* ignore any whitespace */
|
||||
break;
|
||||
case '\r':
|
||||
/* new line always resets buffer */
|
||||
uart_cmd_reset(cs);
|
||||
break;
|
||||
default:
|
||||
strbuf_append(&cs->cmd, ch);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case ST_IN_ARG:
|
||||
switch (ch) {
|
||||
case '\r':
|
||||
rc = handle_cb(cs, CMD_OP_SET, cs->cmd.buf, cs->arg.buf);
|
||||
uart_cmd_reset(cs);
|
||||
break;
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\t':
|
||||
/* ignore any whitespace */
|
||||
break;
|
||||
default:
|
||||
strbuf_append(&cs->arg, ch);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (ch == '\r')
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart_cmd_out(struct cmd_state *cs, char *format, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
va_start(ap, format);
|
||||
cs->out(format, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
int uart_cmd_reset(struct cmd_state *cs)
|
||||
{
|
||||
strbuf_reset(&cs->cmd);
|
||||
strbuf_reset(&cs->arg);
|
||||
cs->state = ST_IN_CMD;
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -1,9 +1,12 @@
|
|||
CFLAGS=-I../include/ -O2
|
||||
CFLAGS=-I../include/ -O0 -g
|
||||
|
||||
all: tuner-test
|
||||
all: tuner-test cmd-test
|
||||
|
||||
%.o: %.c
|
||||
$(CC) $(CFLAGS) -o $@ -c $^
|
||||
|
||||
tuner-test: tuner-test.o ../src/tuner_e4k.o
|
||||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
||||
cmd-test: cmd-test.o ../src/uart_cmd.o
|
||||
$(CC) $(LDFLAGS) -o $@ $^
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <uart_cmd.h>
|
||||
|
||||
static void my_out(const char *format, va_list ap)
|
||||
{
|
||||
vprintf(format, ap);
|
||||
}
|
||||
|
||||
static int my_cb(struct cmd_state *cs, enum cmd_op op, const char *cmd, const char *arg)
|
||||
{
|
||||
printf("my_%s `%s', `%s'\n", op == CMD_OP_SET ? "set" : "get", cmd, arg);
|
||||
|
||||
if (!strcmp(cmd, "bar") && op == CMD_OP_SET)
|
||||
return -EINVAL;
|
||||
|
||||
else if (!strcmp(cmd, "baz") && op == CMD_OP_GET)
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct cmd cmds[] = {
|
||||
{ "foo", CMD_OP_SET|CMD_OP_GET|CMD_OP_EXEC, my_cb,
|
||||
"the foo command" },
|
||||
{ "bar", CMD_OP_GET, my_cb,
|
||||
"the gettable bar command" },
|
||||
{ "baz", CMD_OP_SET, my_cb,
|
||||
"the settable baz command" },
|
||||
};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
struct cmd_state cs;
|
||||
|
||||
cs.out = my_out;
|
||||
uart_cmd_reset(&cs);
|
||||
uart_cmds_register(cmds, sizeof(cmds)/sizeof(cmds[0]));
|
||||
|
||||
while (1) {
|
||||
uint8_t ch;
|
||||
int rc;
|
||||
|
||||
rc = read(0, &ch, 1);
|
||||
if (rc < 0)
|
||||
exit(1);
|
||||
|
||||
uart_cmd_char(&cs, ch);
|
||||
}
|
||||
}
|
Reference in New Issue