osmo-sdr/firmware/tests/cmd-test.c @ master
1 | 41f8d833 | Harald Welte | |
---|---|---|---|
2 | #include <unistd.h>
|
||
3 | #include <string.h>
|
||
4 | #include <stdint.h>
|
||
5 | #include <stdio.h>
|
||
6 | #include <errno.h>
|
||
7 | |||
8 | #include <uart_cmd.h>
|
||
9 | |||
10 | static void my_out(const char *format, va_list ap) |
||
11 | {
|
||
12 | vprintf(format, ap); |
||
13 | }
|
||
14 | |||
15 | 5a389db1 | Harald Welte | static int my_cb(struct cmd_state *cs, enum cmd_op op, const char *cmd, |
16 | int argc, char **argv) |
||
17 | 41f8d833 | Harald Welte | {
|
18 | 5a389db1 | Harald Welte | int i; |
19 | 41f8d833 | Harald Welte | |
20 | 5a389db1 | Harald Welte | printf("my_cb(%u,%s,%u,[", op, cmd, argc); |
21 | for (i = 0; i < argc; i++) |
||
22 | printf("%s,", argv[i]); |
||
23 | printf("])\n"); |
||
24 | 41f8d833 | Harald Welte | |
25 | return 0; |
||
26 | }
|
||
27 | |||
28 | static struct cmd cmds[] = { |
||
29 | { "foo", CMD_OP_SET|CMD_OP_GET|CMD_OP_EXEC, my_cb, |
||
30 | "the foo command" }, |
||
31 | { "bar", CMD_OP_GET, my_cb, |
||
32 | "the gettable bar command" }, |
||
33 | { "baz", CMD_OP_SET, my_cb, |
||
34 | "the settable baz command" }, |
||
35 | };
|
||
36 | |||
37 | int main(int argc, char **argv) |
||
38 | {
|
||
39 | struct cmd_state cs; |
||
40 | |||
41 | cs.out = my_out; |
||
42 | uart_cmd_reset(&cs); |
||
43 | uart_cmds_register(cmds, sizeof(cmds)/sizeof(cmds[0])); |
||
44 | |||
45 | while (1) { |
||
46 | uint8_t ch; |
||
47 | int rc; |
||
48 | |||
49 | rc = read(0, &ch, 1); |
||
50 | if (rc < 0) |
||
51 | exit(1); |
||
52 | |||
53 | uart_cmd_char(&cs, ch); |
||
54 | }
|
||
55 | }
|