You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ag...@apache.org on 2020/06/15 20:22:28 UTC

[incubator-nuttx-apps] 02/03: pty: support waiting for underlying serial device to appear, useful for pty over USBDEV serial device

This is an automated email from the ASF dual-hosted git repository.

aguettouche pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx-apps.git

commit f88e4af8afe4b1cac0e7c369d60e206b3718607e
Author: Matias Nitsche <mn...@dc.uba.ar>
AuthorDate: Mon Jun 15 12:27:13 2020 -0300

    pty: support waiting for underlying serial device to appear, useful for pty over USBDEV serial device
---
 examples/pty_test/Kconfig    |  8 ++++++++
 examples/pty_test/pty_test.c | 28 +++++++++++++++++++++++++++-
 2 files changed, 35 insertions(+), 1 deletion(-)

diff --git a/examples/pty_test/Kconfig b/examples/pty_test/Kconfig
index 8448693..1f3921c 100644
--- a/examples/pty_test/Kconfig
+++ b/examples/pty_test/Kconfig
@@ -40,4 +40,12 @@ config EXAMPLES_PTYTEST_DAEMONPRIO
 	int "PTY_Test daemon task priority"
 	default 100
 
+config EXAMPLES_PTYTEST_WAIT_CONNECTED
+  bool "Keep retrying open serial device"
+  ---help---
+     For USB based serial devices, open will fail
+     if the other end is not connected (USB cable unplugged).
+     Enabling this option will retry the open() call every second
+     until connected.
+
 endif
diff --git a/examples/pty_test/pty_test.c b/examples/pty_test/pty_test.c
index 262e576..8a285fd 100644
--- a/examples/pty_test/pty_test.c
+++ b/examples/pty_test/pty_test.c
@@ -315,9 +315,35 @@ int main(int argc, FAR char *argv[])
                           O_RDWR | O_NONBLOCK);
   if (termpair.fd_uart < 0)
     {
-      fprintf(stderr, "Failed to open %s: %\n",
+#ifdef CONFIG_EXAMPLES_PTYTEST_WAIT_CONNECTED
+      /* if ENOTCONN is received, re-attempt to open periodically */
+
+      if (errno == ENOTCONN)
+        {
+          fprintf(stderr, "ERROR: device not connected, will continue trying\n");
+        }
+
+      while (termpair.fd_uart < 0 && errno == ENOTCONN)
+        {
+          sleep(1);
+
+          termpair.fd_uart = open(CONFIG_EXAMPLES_PTYTEST_SERIALDEV,
+                                  O_RDWR | O_NONBLOCK);
+        }
+
+      /* if we exited due to an error different than ENOTCONN */
+
+      if (termpair.fd_uart < 0)
+        {
+          fprintf(stderr, "Failed to open %s: %i\n",
+                 CONFIG_EXAMPLES_PTYTEST_SERIALDEV, errno);
+          goto error_serial;
+        }
+#else
+      fprintf(stderr, "Failed to open %s: %i\n",
              CONFIG_EXAMPLES_PTYTEST_SERIALDEV, errno);
       goto error_serial;
+#endif
     }
 
 #ifdef CONFIG_SERIAL_TERMIOS