int sercon_main(int argc, char *argv[])
#endif
{
  int ret;

  /* Check if there is a non-NULL USB mass storage device handle (meaning that the
   * USB mass storage device is already configured).
   */

  if (g_cdcacm.handle)
    {
      printf("sercon:: ERROR: Already connected\n");
      return EXIT_FAILURE;
    }

  /* Then, in any event, enable trace data collection as configured BEFORE
   * enabling the CDC/ACM device.
   */

  usbtrace_enable(TRACE_BITSET);

  /* Initialize the USB CDC/ACM serial driver */

  printf("sercon: Registering CDC/ACM serial driver\n");
  ret = cdcacm_initialize(CONFIG_SYSTEM_CDCACM_DEVMINOR, &g_cdcacm.handle);
  if (ret < 0)
    {
      printf("sercon: ERROR: Failed to create the CDC/ACM serial device: %d\n", -ret);
      return EXIT_FAILURE;
    }

  printf("sercon: Successfully registered the CDC/ACM serial driver\n");
  return EXIT_SUCCESS;
}
Exemplo n.º 2
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();
    }
}
Exemplo n.º 3
0
static int tash_nonftdi_usbconinit(void)
{
	int ret = OK;
	int nlc;
	int fd;
	char inch;
	ssize_t nbytes;

#if defined(CONFIG_CDCACM)
#define CONFIG_TASH_USBCONSOLE "/dev/ttyACM0"
#define CONFIG_USBDEV_MINOR 0
	ret = cdcacm_initialize(CONFIG_USBDEV_MINOR, NULL);
	/* For future
	   if (ret != 0)
	   RETRY logic
	 */
#endif

 restart:
	do {
		/* Try to open the console */

		fd = open(CONFIG_TASH_USBCONSOLE, 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);

	nlc = 0;
	do {
		/* Read one byte */

		inch = 0;
		nbytes = read(fd, &inch, 1);

		/* Is it a carriage return (or maybe a newline)? */

		if (nbytes == 1 && (inch == '\n' || inch == '\r')) {
			/* Yes.. increment the count */

			nlc++;
		} else {
			/* No.. Reset the count.  We need to see 3 in a row to continue. */

			nlc = 0;

			/* If a read error occurred (nbytes < 0) or an end-of-file was
			 * encountered (nbytes == 0), then close the driver and start
			 * over.
			 */

			if (nbytes <= 0) {
				(void)close(fd);
				goto restart;
			}
		}
	} while (nlc < 2);

	(void)fclose(stdin);
	(void)fclose(stdout);
	(void)fclose(stderr);
	/* Dup the fd to create standard fd 0-2 */
	(void)dup2(fd, 0);
	(void)dup2(fd, 1);
	(void)dup2(fd, 2);
	/* 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");

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

	return ret;
}
Exemplo n.º 4
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;
}
Exemplo n.º 5
0
int board_app_initialize(uintptr_t arg)
{
#if defined (HAVE_USBDEV) || defined(HAVE_HSMCI) || defined (HAVE_PROC) || \
    defined(HAVE_USBMONITOR)
  int ret;
#endif

#ifdef HAVE_USBDEV
  syslog(LOG_INFO, "Registering CDC/ACM serial driver\n");

  ret = cdcacm_initialize(CONFIG_SAM4S_XPLAINED_PRO_CDCACM_DEVMINOR, NULL);
  if (ret < 0)
    {
      syslog(LOG_ERR,
             "ERROR: Failed to create the CDC/ACM serial device: %d (%d)\n",
             ret, errno);
      return ret;
    }
#endif

#ifdef HAVE_HSMCI
  /* Initialize the HSMCI driver */

  syslog(LOG_INFO, "initializing HSMCI\n");

  ret = sam_hsmci_initialize();
  if (ret < 0)
    {
      syslog(LOG_ERR,
             "ERROR: sam_hsmci_initialize() failed: %d (%d)\n",
             ret, errno);
      return ret;
    }
#endif

#ifdef HAVE_PROC
  /* mount the proc filesystem */

  syslog(LOG_INFO, "Mounting procfs to /proc\n");

  ret = mount(NULL, "/proc", "procfs", 0, NULL);
  if (ret < 0)
    {
      syslog(LOG_ERR,
             "ERROR: Failed to mount the PROC filesystem: %d (%d)\n",
             ret, errno);
      return ret;
    }
#endif

#if HAVE_HSMCI
  syslog(LOG_INFO, "Mounting /dev/mmcsd0 to /fat\n");

  ret = mount("/dev/mmcsd0", "/fat", "vfat", 0, NULL);
  if (ret < 0)
    {
      syslog(LOG_ERR,
             "ERROR: Failed to mount the FAT filesystem: %d (%d)\n",
             ret, errno);
      return ret;
    }
#endif

#ifdef HAVE_USBMONITOR
  /* Start the USB Monitor */

  syslog(LOG_INFO, "Starting USB Monitor\n");
  ret = usbmonitor_start();
  if (ret != OK)
    {
      syslog(LOG_ERR, "ERROR: Failed to start USB monitor: %d (%d)\n", ret, errno);
      return ret;
    }
#endif

  return OK;
}
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;
}
Exemplo n.º 7
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;
}