Example #1
0
int nsh_consolemain(int argc, char *argv[])
{
  FAR struct console_stdio_s *pstate = nsh_newconsole();
  int ret;

  DEBUGASSERT(pstate);

  /* Initialize any USB tracing options that were requested */

#ifdef CONFIG_NSH_USBDEV_TRACE
  usbtrace_enable(TRACE_BITSET);
#endif

  /* Initialize the USB serial driver */

#if defined(CONFIG_PL2303) || defined(CONFIG_CDCACM)
#ifdef CONFIG_CDCACM
  ret = cdcacm_initialize(CONFIG_NSH_USBDEV_MINOR, NULL);
#else
  ret = usbdev_serialinitialize(CONFIG_NSH_USBDEV_MINOR);
#endif
  DEBUGASSERT(ret == OK);
#endif

  /* Configure to use /dev/null if we do not have a valid console. */

#ifndef CONFIG_DEV_CONSOLE
  (void)nsh_nullstdio();
#endif

  /* Execute the one-time start-up script (output may go to /dev/null) */

#ifdef CONFIG_NSH_ROMFSETC
  (void)nsh_initscript(&pstate->cn_vtbl);
#endif

  /* Now loop, executing creating a session for each USB connection */

  for (;;)
    {
      /* Wait for the USB to be connected to the host and switch
       * standard I/O to the USB serial device.
       */

      ret = nsh_waitusbready();
      DEBUGASSERT(ret == OK);

      /* Execute the session */

      (void)nsh_session(pstate);

      /* Switch to /dev/null because we probably no longer have a
       * valid console device.
       */

      (void)nsh_nullstdio();
    }
}
Example #2
0
int MAIN_NAME(int argc, char *argv[])
{
  pthread_attr_t attr;
  int ret;

  /* Initialize global data */

  memset(&g_usbterm, 0, sizeof(struct usbterm_globals_s));

  /* Initialization of the USB hardware may be performed by logic external to
   * this test.
   */

#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
  message(MAIN_STRING "Performing external device initialization\n");
  ret = usbterm_devinit();
  if (ret != OK)
    {
      message(MAIN_STRING "usbterm_devinit failed: %d\n", ret);
      goto errout;
    }
#endif

  /* Initialize the USB serial driver */

  message(MAIN_STRING "Registering USB serial driver\n");
#ifdef CONFIG_CDCACM
  ret = cdcacm_initialize(0, NULL);
#else
  ret = usbdev_serialinitialize(0);
#endif
  if (ret < 0)
    {
      message(MAIN_STRING "ERROR: Failed to create the USB serial device: %d\n", -ret);
      goto errout_with_devinit;
    }
  message(MAIN_STRING "Successfully registered the serial driver\n");

#if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0
  /* If USB tracing is enabled and tracing of initial USB events is specified,
   * then dump all collected trace data to stdout
   */

  sleep(5);
  dumptrace();
#endif

  /* Then, in any event, configure trace data collection as configured */

  usbtrace_enable(TRACE_BITSET);

  /* Open the USB serial device for writing */

  do
    {
      message(MAIN_STRING "Opening USB serial driver\n");

      g_usbterm.outstream = fopen(USBTERM_DEVNAME, "w");
      if (g_usbterm.outstream == NULL)
        {
          int errcode = errno;
          message(MAIN_STRING "ERROR: Failed to open " USBTERM_DEVNAME " for writing: %d\n",
                  errcode);

          /* ENOTCONN means that the USB device is not yet connected */

          if (errcode == ENOTCONN)
            {
              message(MAIN_STRING "       Not connected. Wait and try again.\n");
              sleep(5);
            }
          else
            {
              /* Give up on other errors */

              goto errout_with_devinit;
            }
        }

      /* If USB tracing is enabled, then dump all collected trace data to stdout */

      dumptrace();
    }
  while (g_usbterm.outstream == NULL);

  /* Open the USB serial device for reading.  Since we are already connected, this
   * should not fail.
   */

  g_usbterm.instream = fopen(USBTERM_DEVNAME, "r");
  if (g_usbterm.instream == NULL)
    {
      message(MAIN_STRING "ERROR: Failed to open " USBTERM_DEVNAME " for reading: %d\n", errno);
      goto errout_with_outstream;
    }

  message(MAIN_STRING "Successfully opened the serial driver\n");

  /* Start the USB term listener thread */

  message(MAIN_STRING "Starting the listener thread\n");
 
  ret = pthread_attr_init(&attr);
  if (ret != OK)
    {
      message(MAIN_STRING "pthread_attr_init failed: %d\n", ret);
      goto errout_with_streams;
    }

  ret = pthread_create(&g_usbterm.listener, &attr,
                       usbterm_listener, (pthread_addr_t)0);
  if (ret != 0)
    {
      message(MAIN_STRING "Error in thread creation: %d\n", ret);
      goto errout_with_streams;
    }

  /* Send messages and get responses -- forever */

  message(MAIN_STRING "Waiting for local input\n");
  for (;;)
    {
      /* Display the prompt string on stdout */

      fputs("usbterm> ", stdout);
      fflush(stdout);

      /* Get the next line of input */

#ifdef CONFIG_EXAMPLES_USBTERM_FGETS
      /* fgets returns NULL on end-of-file or any I/O error */

      if (fgets(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin) == NULL)
        {
          printf("ERROR: fgets failed: %d\n", errno);
          return 1;
        }
#else
      ret = readline(g_usbterm.outbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, stdin, stdout);

      /* Readline normally returns the number of characters read,
       * but will return 0 on end of file or a negative value
       * if an error occurs.  Either will cause the session to
       * terminate.
       */

      if (ret <= 0)
        {
          printf("ERROR: readline failed: %d\n", ret);
          return 1;
        }
#endif
      /* Is there anyone listening on the other end? */

      else if (g_usbterm.peer)
        {
          /* Yes.. Send the line of input via USB */

          fputs(g_usbterm.outbuffer, g_usbterm.outstream);

          /* Display the prompt string on the remote USB serial connection */

          fputs("\rusbterm> ", g_usbterm.outstream);
          fflush(g_usbterm.outstream);
        }
      else
        {
          printf("Still waiting for remote peer.  Please try again later.\n", ret);
        }

      /* If USB tracing is enabled, then dump all collected trace data to stdout */

      dumptrace();
    }

  /* Error exits */

errout_with_streams:
  fclose(g_usbterm.instream);
errout_with_outstream:
  fclose(g_usbterm.outstream);
errout_with_devinit:
#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
  usbterm_devuninit();
errout:
#endif
  message(MAIN_STRING "       Aborting\n");
  return 1;
}
int usbserial_main(int argc, char *argv[])
#endif
{
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
  int infd;
#endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
  int outfd;
#endif
#ifdef COUNTER_NEEDED
  int count = 0;
#endif
  ssize_t nbytes;
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
  int i, j, k;
#endif
  int ret;

  /* Initialize the USB serial driver */

  printf("usbserial_main: Registering USB serial driver\n");
#ifdef CONFIG_CDCACM
  ret = cdcacm_initialize(0, NULL);
#else
  ret = usbdev_serialinitialize(0);
#endif
  if (ret < 0)
    {
      printf("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", -ret);
      return 1;
    }
  printf("usbserial_main: Successfully registered the serial driver\n");

#if CONFIG_USBDEV_TRACE && CONFIG_USBDEV_TRACE_INITIALIDSET != 0
  /* If USB tracing is enabled and tracing of initial USB events is specified,
   * then dump all collected trace data to stdout
   */

  sleep(5);
  dumptrace();
#endif

  /* Then, in any event, configure trace data collection as configured */

  usbtrace_enable(TRACE_BITSET);

  /* Open the USB serial device for writing (blocking) */

#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
  do
    {
      printf("usbserial_main: Opening USB serial driver\n");
      outfd = open(USBSER_DEVNAME, O_WRONLY);
      if (outfd < 0)
        {
          int errcode = errno;
          printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for writing: %d\n", errcode);

          /* ENOTCONN means that the USB device is not yet connected */

          if (errcode == ENOTCONN)
            {
              printf("usbserial_main:        Not connected. Wait and try again.\n");
              sleep(5);
            }
          else
            {
              /* Give up on other errors */

              printf("usbserial_main:        Aborting\n");
              return 2;
            }
        }

      /* If USB tracing is enabled, then dump all collected trace data to stdout */

      dumptrace();
    }
  while (outfd < 0);
#endif

  /* Open the USB serial device for reading (non-blocking) */

#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
  infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK);
  if (infd < 0)
    {
      printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno);
      close(outfd);
      return 3;
    }
#else
  do
    {
      infd = open(USBSER_DEVNAME, O_RDONLY|O_NONBLOCK);
      if (infd < 0)
        {
          int errcode = errno;
          printf("usbserial_main: ERROR: Failed to open " USBSER_DEVNAME " for reading: %d\n", errno);

          /* ENOTCONN means that the USB device is not yet connected */

          if (errcode == ENOTCONN)
            {
              printf("usbserial_main:        Not connected. Wait and try again.\n");
              sleep(5);
            }
          else
            {
              /* Give up on other errors */

              printf("usbserial_main:        Aborting\n");
              return 3;
            }
        }

      /* If USB tracing is enabled, then dump all collected trace data to stdout */

      dumptrace();
    }
  while (infd < 0);
#endif
#endif

  printf("usbserial_main: Successfully opened the serial driver\n");

  /* Send messages and get responses -- forever */

  for (;;)
    {
     /* Test IN (device-to-host) messages */

#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL)
      if (count < 8)
        {
          printf("usbserial_main: Saying hello\n");
          nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg));
          count++;
        }
      else
        {
          printf("usbserial_main: Reciting QEI's speech of 1588\n");
          nbytes = write(outfd, g_longmsg, sizeof(g_longmsg));
          count = 0;
        }
#elif !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL)
      printf("usbserial_main: Reciting QEI's speech of 1588\n");
      nbytes = write(outfd, g_longmsg, sizeof(g_longmsg));
#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */
      printf("usbserial_main: Saying hello\n");
      nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg));
#endif

      /* Test if the write was successful */

      if (nbytes < 0)
        {
          printf("usbserial_main: ERROR: write failed: %d\n", errno);
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
          close(infd);
#endif
          close(outfd);
          return 4;
        }
      printf("usbserial_main: %ld bytes sent\n", (long)nbytes);
#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */

      /* Test OUT (host-to-device) messages */

#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
      /* Poll for incoming messages */

      printf("usbserial_main: Polling for OUT messages\n");
      for (i = 0; i < 5; i++)
        {
          memset(g_iobuffer, 'X', IOBUFFER_SIZE);
          nbytes = read(infd, g_iobuffer, IOBUFFER_SIZE);
          if (nbytes < 0)
            {
              int errorcode = errno;
              if (errorcode != EAGAIN)
                {
                  printf("usbserial_main: ERROR: read failed: %d\n", errno);
                  close(infd);
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
                  close(outfd);
#endif
                  return 6;
                }
            }
          else
            {
              printf("usbserial_main: Received l%d bytes:\n", (long)nbytes);
              if (nbytes > 0)
                {
                  for (j = 0; j < nbytes; j += 16)
                    {
                      printf("usbserial_main: %03x: ", j);
                      for (k = 0; k < 16; k++)
                        {
                          if (k == 8)
                            {
                              printf(" ");
                            }
                          if (j+k < nbytes)
                            {
                              printf("%02x", g_iobuffer[j+k]);
                            }
                          else
                            {
                              printf("  ");
                            }
                        }
                      printf(" ");
                      for (k = 0; k < 16; k++)
                        {
                          if (k == 8)
                            {
                              printf(" ");
                            }
                          if (j+k < nbytes)
                            {
                              if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f)
                                {
                                  printf("%c", g_iobuffer[j+k]);
                                }
                              else
                                {
                                  printf(".");
                                }
                            }
                           else
                            {
                              printf(" ");
                            }
                        }
                      printf("\n");
                    }
                }
            }
          sleep(1);
        }
#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
      printf("usbserial_main: Waiting\n");
      sleep(5);
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */

      /* If USB tracing is enabled, then dump all collected trace data to stdout */

      dumptrace();
    }

  /* Won't get here, but if we did this what we would have to do */

#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
  close(infd);
#endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY
  close(outfd);
#endif
  return 0;
}
Example #4
0
int nsh_usbconsole(void)
{
  int fd;
  int ret;

  /* Don't start the NSH console until the console device is ready.  Chances
   * are, we get here with no functional console.  The USB console will not
   * be available until the device is connected to the host and until the
   * host-side application opens the connection.
   */

  /* Initialize the USB serial driver */

#ifdef CONFIG_CDCACM
  ret = cdcacm_initialize(0, NULL);
#else
  ret = usbdev_serialinitialize(0);
#endif
  DEBUGASSERT(ret == OK);

  /* Make sure the stdin, stdout, and stderr are closed */

  (void)fclose(stdin);
  (void)fclose(stdout);
  (void)fclose(stderr);

  /* Open the USB serial device for writing */

  do
    {
      /* Try to open the console */

      fd = open("/dev/console", O_RDWR);
      if (fd < 0)
        {
          /* ENOTCONN means that the USB device is not yet connected. Anything
           * else is bad.
           */

          DEBUGASSERT(errno == ENOTCONN);

          /* Sleep a bit and try again */

          sleep(2);
        }
    }
  while (fd < 0);

  /* Dup the fd to create standard fd 0-2 */

  (void)dup2(fd, 0);
  (void)dup2(fd, 1);
  (void)dup2(fd, 2);

  /* We can close the original file descriptor now (unless it was one of 0-2) */

  if (fd > 2)
    {
      close(fd);
    }

  /* fdopen to get the stdin, stdout and stderr streams. The following logic depends
   * on the fact that the library layer will allocate FILEs in order.  And since
   * we closed stdin, stdout, and stderr above, that is what we should get.
   *
   * fd = 0 is stdin  (read-only)
   * fd = 1 is stdout (write-only, append)
   * fd = 2 is stderr (write-only, append)
   */

  (void)fdopen(0, "r");
  (void)fdopen(1, "a");
  (void)fdopen(2, "a");
  return OK;
}