1 | /* |
---|
2 | * Copyright (c) 2011 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Obere Lagerstr. 30 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <rtems/shell.h> |
---|
20 | |
---|
21 | #include <termios.h> |
---|
22 | #include <unistd.h> |
---|
23 | |
---|
24 | static rtems_status_code change_serial_settings(int fd, struct termios *term) |
---|
25 | { |
---|
26 | rtems_status_code sc = RTEMS_UNSATISFIED; |
---|
27 | int rv = tcgetattr(fd, term); |
---|
28 | |
---|
29 | if (rv == 0) { |
---|
30 | struct termios new_term = *term; |
---|
31 | |
---|
32 | new_term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); |
---|
33 | new_term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); |
---|
34 | new_term.c_cflag &= ~(CSIZE | PARENB); |
---|
35 | new_term.c_cflag |= CS8; |
---|
36 | |
---|
37 | new_term.c_cc [VMIN] = 0; |
---|
38 | new_term.c_cc [VTIME] = 10; |
---|
39 | |
---|
40 | rv = tcsetattr(fd, TCSANOW, &new_term); |
---|
41 | if (rv == 0) { |
---|
42 | sc = RTEMS_SUCCESSFUL; |
---|
43 | } |
---|
44 | } |
---|
45 | |
---|
46 | return sc; |
---|
47 | } |
---|
48 | |
---|
49 | static rtems_status_code restore_serial_settings(int fd, struct termios *term) |
---|
50 | { |
---|
51 | int rv = tcsetattr(fd, TCSANOW, term); |
---|
52 | |
---|
53 | return rv == 0 ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED; |
---|
54 | } |
---|
55 | |
---|
56 | rtems_status_code rtems_shell_wait_for_input( |
---|
57 | int fd, |
---|
58 | int timeout_in_seconds, |
---|
59 | rtems_shell_wait_for_input_notification notification, |
---|
60 | void *notification_arg |
---|
61 | ) |
---|
62 | { |
---|
63 | struct termios term; |
---|
64 | rtems_status_code sc = change_serial_settings(fd, &term); |
---|
65 | |
---|
66 | if (sc == RTEMS_SUCCESSFUL) { |
---|
67 | bool input_detected = false; |
---|
68 | int i = 0; |
---|
69 | |
---|
70 | for (i = 0; i < timeout_in_seconds && !input_detected; ++i) { |
---|
71 | char c; |
---|
72 | |
---|
73 | (*notification)(fd, timeout_in_seconds - i, notification_arg); |
---|
74 | |
---|
75 | input_detected = read(fd, &c, sizeof(c)) > 0; |
---|
76 | } |
---|
77 | |
---|
78 | sc = restore_serial_settings(fd, &term); |
---|
79 | if (sc == RTEMS_SUCCESSFUL) { |
---|
80 | sc = input_detected ? RTEMS_SUCCESSFUL : RTEMS_TIMEOUT; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | return sc; |
---|
85 | } |
---|