2014-01-10 16:21:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2013 Kaspar Schleiser <kaspar@schleiser.de>
|
2014-02-03 23:03:13 +01:00
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
2014-01-10 16:21:35 +01:00
|
|
|
*
|
2014-07-31 19:45:27 +02:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
|
|
* directory for more details.
|
2014-01-10 16:21:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief shows how to set up own and use the system shell commands.
|
|
|
|
* By typing help in the serial console, all the supported commands
|
|
|
|
* are listed.
|
|
|
|
*
|
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
|
|
|
* @author Zakaria Kasmi <zkasmi@inf.fu-berlin.de>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2014-03-01 09:54:09 +01:00
|
|
|
|
|
|
|
#include "shell.h"
|
2014-01-10 16:21:35 +01:00
|
|
|
|
2019-05-29 12:07:38 +02:00
|
|
|
#if MODULE_STDIO_RTT
|
|
|
|
#include "xtimer.h"
|
|
|
|
#endif
|
|
|
|
|
2022-11-11 23:00:58 +01:00
|
|
|
/* define buffer to be used by the shell. Note: This is intentionally
|
|
|
|
* smaller than 64 bytes, as the EDBG integrated UART bridge of the samr21-xpro
|
|
|
|
* (and likely all other EDBG boards) drops chars when sending more than 64
|
|
|
|
* bytes at a time. This results in the buffer overflow test failing. */
|
|
|
|
static char line_buf[60];
|
|
|
|
|
2020-07-07 11:03:33 +02:00
|
|
|
#if MODULE_SHELL_HOOKS
|
|
|
|
void shell_post_readline_hook(void)
|
|
|
|
{
|
|
|
|
puts("shell_post_readline_hook");
|
|
|
|
}
|
|
|
|
|
|
|
|
void shell_pre_command_hook(int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
puts("shell_pre_command_hook");
|
|
|
|
}
|
|
|
|
|
|
|
|
void shell_post_command_hook(int ret, int argc, char **argv)
|
|
|
|
{
|
|
|
|
(void)ret;
|
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
|
|
|
puts("shell_post_command_hook");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
static int print_teststart(int argc, char **argv)
|
2014-01-10 16:21:35 +01:00
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("[TEST_START]\n");
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
static int print_testend(int argc, char **argv)
|
2014-01-10 16:21:35 +01:00
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2014-01-10 16:21:35 +01:00
|
|
|
printf("[TEST_END]\n");
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-01-10 16:21:35 +01:00
|
|
|
}
|
|
|
|
|
2015-03-20 08:51:45 +01:00
|
|
|
static int print_echo(int argc, char **argv)
|
2014-02-21 19:40:58 +01:00
|
|
|
{
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
2017-09-05 20:40:44 +02:00
|
|
|
printf("\"%s\"", argv[i]);
|
2014-02-21 19:40:58 +01:00
|
|
|
}
|
|
|
|
puts("");
|
2015-03-20 08:51:45 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-02-21 19:40:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-07 12:56:16 +01:00
|
|
|
static int print_shell_bufsize(int argc, char **argv)
|
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2022-11-11 23:00:58 +01:00
|
|
|
printf("%d\n", sizeof(line_buf));
|
2020-02-07 12:56:16 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-02-09 15:03:19 +01:00
|
|
|
static int print_empty(int argc, char **argv)
|
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2021-02-09 15:03:19 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-21 19:40:58 +01:00
|
|
|
static const shell_command_t shell_commands[] = {
|
2020-02-07 12:56:16 +01:00
|
|
|
{ "bufsize", "Get the shell's buffer size", print_shell_bufsize },
|
2014-01-10 16:21:35 +01:00
|
|
|
{ "start_test", "starts a test", print_teststart },
|
|
|
|
{ "end_test", "ends a test", print_testend },
|
2014-02-21 19:40:58 +01:00
|
|
|
{ "echo", "prints the input command", print_echo },
|
2021-02-09 15:03:19 +01:00
|
|
|
{ "empty", "print nothing on command", print_empty },
|
2014-01-10 16:21:35 +01:00
|
|
|
{ NULL, NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2021-02-22 12:37:04 +01:00
|
|
|
static int _xfa_test1(int argc, char **argv)
|
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2021-02-22 12:37:04 +01:00
|
|
|
printf("[XFA TEST 1 OK]\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int _xfa_test2(int argc, char **argv)
|
|
|
|
{
|
2021-02-23 11:07:34 +01:00
|
|
|
(void)argc;
|
|
|
|
(void)argv;
|
2021-02-22 12:37:04 +01:00
|
|
|
printf("[XFA TEST 2 OK]\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add above commands to the shell commands XFA using helper macro.
|
|
|
|
* Intentionally reversed order to test linker script based alphanumeric
|
|
|
|
* ordering. */
|
2021-02-23 11:07:34 +01:00
|
|
|
SHELL_COMMAND(xfa_test2, "xfa test command 2", _xfa_test2);
|
|
|
|
SHELL_COMMAND(xfa_test1, "xfa test command 1", _xfa_test1);
|
2021-02-22 12:37:04 +01:00
|
|
|
|
2014-01-10 16:21:35 +01:00
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
printf("test_shell.\n");
|
|
|
|
|
|
|
|
/* define own shell commands */
|
2022-11-11 23:00:58 +01:00
|
|
|
shell_run_once(shell_commands, line_buf, sizeof(line_buf));
|
2020-10-21 17:39:58 +02:00
|
|
|
|
|
|
|
puts("shell exited");
|
|
|
|
|
|
|
|
/* Restart the shell after the previous one exits, so that we can test
|
|
|
|
* Ctrl-D exit */
|
2022-11-11 23:00:58 +01:00
|
|
|
shell_run(shell_commands, line_buf, sizeof(line_buf));
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
/* or use only system shell commands */
|
2022-11-11 23:00:58 +01:00
|
|
|
/* shell_run(NULL, line_buf, sizeof(line_buf)); */
|
2014-01-10 16:21:35 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|