diff options
author | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2011-09-16 09:16:32 +0000 |
---|---|---|
committer | Sebastian Huber <sebastian.huber@embedded-brains.de> | 2011-09-16 09:16:32 +0000 |
commit | 6c5ee7f00913a5c6516fa3048da9b65bf50cef98 (patch) | |
tree | 7ee6c04105e112ad758f8e64076b39c4289bc63a /cpukit | |
parent | 2011-09-15 Joel Sherrill <joel.sherrill@oarcorp.com> (diff) | |
download | rtems-6c5ee7f00913a5c6516fa3048da9b65bf50cef98.tar.bz2 |
2011-09-16 Sebastian Huber <sebastian.huber@embedded-brains.de>
* libmisc/shell/shell-wait-for-input.c: New file.
* libmisc/Makefile.am: Reflect change above.
* libmisc/shell/shell.h: Declare rtems_shell_wait_for_input().
Diffstat (limited to '')
-rw-r--r-- | cpukit/ChangeLog | 6 | ||||
-rw-r--r-- | cpukit/libmisc/Makefile.am | 3 | ||||
-rw-r--r-- | cpukit/libmisc/shell/shell-wait-for-input.c | 87 | ||||
-rw-r--r-- | cpukit/libmisc/shell/shell.h | 20 |
4 files changed, 115 insertions, 1 deletions
diff --git a/cpukit/ChangeLog b/cpukit/ChangeLog index 5e8705b3ca..35ef5c6dc5 100644 --- a/cpukit/ChangeLog +++ b/cpukit/ChangeLog @@ -1,3 +1,9 @@ +2011-09-16 Sebastian Huber <sebastian.huber@embedded-brains.de> + + * libmisc/shell/shell-wait-for-input.c: New file. + * libmisc/Makefile.am: Reflect change above. + * libmisc/shell/shell.h: Declare rtems_shell_wait_for_input(). + 2011-09-15 Joel Sherrill <joel.sherrill@oarcorp.com> * score/include/rtems/score/thread.h: Ensure CBS builds with POSIX diff --git a/cpukit/libmisc/Makefile.am b/cpukit/libmisc/Makefile.am index 207ee8bbc2..26b7e4d918 100644 --- a/cpukit/libmisc/Makefile.am +++ b/cpukit/libmisc/Makefile.am @@ -103,7 +103,8 @@ libshell_a_SOURCES = shell/cat_file.c shell/cmds.c shell/internal.h \ shell/hexdump-odsyntax.c shell/hexdump-parse.c shell/hexsyntax.c \ shell/main_time.c shell/main_mknod.c \ shell/main_setenv.c shell/main_getenv.c shell/main_unsetenv.c \ - shell/main_mkrfs.c shell/main_debugrfs.c + shell/main_mkrfs.c shell/main_debugrfs.c \ + shell/shell-wait-for-input.c if LIBNETWORKING libshell_a_SOURCES += \ diff --git a/cpukit/libmisc/shell/shell-wait-for-input.c b/cpukit/libmisc/shell/shell-wait-for-input.c new file mode 100644 index 0000000000..3ddc389213 --- /dev/null +++ b/cpukit/libmisc/shell/shell-wait-for-input.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2011 embedded brains GmbH. All rights reserved. + * + * embedded brains GmbH + * Obere Lagerstr. 30 + * 82178 Puchheim + * Germany + * <rtems@embedded-brains.de> + * + * The license and distribution terms for this file may be + * found in the file LICENSE in this distribution or at + * http://www.rtems.com/license/LICENSE. + * + * $Id$ + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <rtems/shell.h> + +#include <termios.h> +#include <unistd.h> + +static rtems_status_code change_serial_settings(int fd, struct termios *term) +{ + rtems_status_code sc = RTEMS_UNSATISFIED; + int rv = tcgetattr(fd, term); + + if (rv == 0) { + struct termios new_term = *term; + + new_term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + new_term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + new_term.c_cflag &= ~(CSIZE | PARENB); + new_term.c_cflag |= CS8; + + new_term.c_cc [VMIN] = 0; + new_term.c_cc [VTIME] = 10; + + rv = tcsetattr(fd, TCSANOW, &new_term); + if (rv == 0) { + sc = RTEMS_SUCCESSFUL; + } + } + + return sc; +} + +static rtems_status_code restore_serial_settings(int fd, struct termios *term) +{ + int rv = tcsetattr(fd, TCSANOW, term); + + return rv == 0 ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED; +} + +rtems_status_code rtems_shell_wait_for_input( + int fd, + int timeout_in_seconds, + rtems_shell_wait_for_input_notification notification, + void *notification_arg +) +{ + struct termios term; + rtems_status_code sc = change_serial_settings(fd, &term); + + if (sc == RTEMS_SUCCESSFUL) { + bool input_detected = false; + int i = 0; + + for (i = 0; i < timeout_in_seconds && !input_detected; ++i) { + char c; + + (*notification)(fd, timeout_in_seconds - i, notification_arg); + + input_detected = read(fd, &c, sizeof(c)) > 0; + } + + sc = restore_serial_settings(fd, &term); + if (sc == RTEMS_SUCCESSFUL) { + sc = input_detected ? RTEMS_SUCCESSFUL : RTEMS_TIMEOUT; + } + } + + return sc; +} diff --git a/cpukit/libmisc/shell/shell.h b/cpukit/libmisc/shell/shell.h index 759bf71589..8624161177 100644 --- a/cpukit/libmisc/shell/shell.h +++ b/cpukit/libmisc/shell/shell.h @@ -285,6 +285,26 @@ void rtems_shell_mount_add_fsys(rtems_shell_filesystems_t* fs); */ void rtems_shell_mount_del_fsys(rtems_shell_filesystems_t* fs); +typedef void (*rtems_shell_wait_for_input_notification)( + int fd, + int seconds_remaining, + void *arg +); + +/** + * @brief Waits for input. + * + * @retval RTEMS_SUCCESSFUL Input detected. + * @retval RTEMS_TIMEOUT Timeout expired. + * @retval RTEMS_UNSATISFIED Cannot change or restore termios attributes. + */ +rtems_status_code rtems_shell_wait_for_input( + int fd, + int timeout_in_seconds, + rtems_shell_wait_for_input_notification notification, + void *notification_arg +); + #ifdef __cplusplus } #endif |