source: rtems/cpukit/libmisc/shell/shell-wait-for-input.c @ 6c5ee7f0

4.115
Last change on this file since 6c5ee7f0 was 6c5ee7f0, checked in by Sebastian Huber <sebastian.huber@…>, on 09/16/11 at 09:16:32

2011-09-16 Sebastian Huber <sebastian.huber@…>

  • 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().
  • Property mode set to 100644
File size: 1.9 KB
Line 
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.com/license/LICENSE.
13 *
14 * $Id$
15 */
16
17#ifdef HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/shell.h>
22
23#include <termios.h>
24#include <unistd.h>
25
26static rtems_status_code change_serial_settings(int fd, struct termios *term)
27{
28  rtems_status_code sc = RTEMS_UNSATISFIED;
29  int rv = tcgetattr(fd, term);
30
31  if (rv == 0) {
32    struct termios new_term = *term;
33
34    new_term.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
35    new_term.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
36    new_term.c_cflag &= ~(CSIZE | PARENB);
37    new_term.c_cflag |= CS8;
38
39    new_term.c_cc [VMIN] = 0;
40    new_term.c_cc [VTIME] = 10;
41
42    rv = tcsetattr(fd, TCSANOW, &new_term);
43    if (rv == 0) {
44      sc = RTEMS_SUCCESSFUL;
45    }
46  }
47
48  return sc;
49}
50
51static rtems_status_code restore_serial_settings(int fd, struct termios *term)
52{
53  int rv = tcsetattr(fd, TCSANOW, term);
54
55  return rv == 0 ? RTEMS_SUCCESSFUL : RTEMS_UNSATISFIED;
56}
57
58rtems_status_code rtems_shell_wait_for_input(
59  int fd,
60  int timeout_in_seconds,
61  rtems_shell_wait_for_input_notification notification,
62  void *notification_arg
63)
64{
65  struct termios term;
66  rtems_status_code sc = change_serial_settings(fd, &term);
67
68  if (sc == RTEMS_SUCCESSFUL) {
69    bool input_detected = false;
70    int i = 0;
71
72    for (i = 0; i < timeout_in_seconds && !input_detected; ++i) {
73      char c;
74
75      (*notification)(fd, timeout_in_seconds - i, notification_arg);
76
77      input_detected = read(fd, &c, sizeof(c)) > 0;
78    }
79
80    sc = restore_serial_settings(fd, &term);
81    if (sc == RTEMS_SUCCESSFUL) {
82      sc = input_detected ? RTEMS_SUCCESSFUL : RTEMS_TIMEOUT;
83    }
84  }
85
86  return sc;
87}
Note: See TracBrowser for help on using the repository browser.