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;
}
Esempio n. 2
0
int serdis_main(int argc, char *argv[])
#endif
{
  struct boardioc_usbdev_ctrl_s ctrl;

  /* First check if the USB mass storage device is already connected */

  if (!g_cdcacm.handle)
    {
      printf("serdis: ERROR: Not connected\n");
      return EXIT_FAILURE;
    }

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

  usbtrace_enable(0);

  /* Then disconnect the device and uninitialize the USB mass storage driver */

  ctrl.usbdev   = BOARDIOC_USBDEV_CDCACM;
  ctrl.action   = BOARDIOC_USBDEV_DISCONNECT;
  ctrl.instance = CONFIG_SYSTEM_CDCACM_DEVMINOR;
  ctrl.handle   = &g_cdcacm.handle;

  (void)boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
  g_cdcacm.handle = NULL;
  printf("serdis: Disconnected\n");
  return EXIT_SUCCESS;
}
Esempio n. 3
0
int nsh_telnetstart(void)
{
  struct telnetd_config_s config;
  int ret;

  /* Initialize any USB tracing options that were requested.  If standard
   * console is also defined, then we will defer this step to the standard
   * console.
   */

#if defined(CONFIG_NSH_USBDEV_TRACE) && !defined(CONFIG_NSH_CONSOLE)
  usbtrace_enable(TRACE_BITSET);
#endif

  /* Configure the telnet daemon */

  config.d_port      = HTONS(CONFIG_NSH_TELNETD_PORT);
  config.d_priority  = CONFIG_NSH_TELNETD_DAEMONPRIO;
  config.d_stacksize = CONFIG_NSH_TELNETD_DAEMONSTACKSIZE;
  config.t_priority  = CONFIG_NSH_TELNETD_CLIENTPRIO;
  config.t_stacksize = CONFIG_NSH_TELNETD_CLIENTSTACKSIZE;
  config.t_entry     = nsh_telnetmain;

  /* Start the telnet daemon */

  _info("Starting the Telnet daemon\n");
  ret = telnetd_start(&config);
  if (ret < 0)
    {
      _err("ERROR: Failed to tart the Telnet daemon: %d\n", ret);
    }

  return ret;
}
Esempio n. 4
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();
    }
}
Esempio n. 5
0
int usbtrace_enumerate(trace_callback_t callback, void *arg)
{
  uint16_t ndx;
  uint32_t idset;
  int ret = OK;

  /* Temporarily disable tracing */

  idset = usbtrace_enable(0);

  /* Visit every entry, starting with the tail */

  for (ndx = g_tail; ndx != g_head; )
    {
      /* Call the user provided callback */

      ret = callback(&g_trace[ndx], arg);
      if (ret != OK)
        {
          /* Abort the enumeration */

          break;
        }

      /* Increment the index */

      if (++ndx >= CONFIG_USBDEV_TRACE_NRECORDS)
        {
          ndx = 0;
        }
    }

  /* Discard the trace data after it has been reported */

  g_tail = g_head;

  /* Restore tracing state */

  (void)usbtrace_enable(idset);
  return ret;
}
Esempio n. 6
0
int sercon_main(int argc, char *argv[])
#endif
{
  struct boardioc_usbdev_ctrl_s ctrl;
  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");

  ctrl.usbdev   = BOARDIOC_USBDEV_CDCACM;
  ctrl.action   = BOARDIOC_USBDEV_CONNECT;
  ctrl.instance = CONFIG_SYSTEM_CDCACM_DEVMINOR;
  ctrl.handle   = &g_cdcacm.handle;

  ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
  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;
}
int serdis_main(int argc, char *argv[])
{
  /* First check if the USB mass storage device is already connected */

  if (!g_cdcacm.handle)
    {
      message("serdis: ERROR: Not connected\n");
      return EXIT_FAILURE;
    }

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

  usbtrace_enable(0);

  /* Then disconnect the device and uninitialize the USB mass storage driver */

   cdcacm_uninitialize(g_cdcacm.handle);
   g_cdcacm.handle = NULL;
   message("serdis: Disconnected\n");
   return EXIT_SUCCESS;
}
Esempio n. 8
0
int board_mscclassobject(FAR struct usbdevclass_driver_s **classdev)
{
  int ret;

  DEBUGASSERT(g_composite.mschandle == NULL);

  /* Initialize USB trace output IDs */

  usbtrace_enable(TRACE_BITSET);
  check_test_memory_usage("After usbtrace_enable()");

  /* Configure the mass storage device */

  printf("board_mscclassobject: Configuring with NLUNS=%d\n", CONFIG_SYSTEM_COMPOSITE_NLUNS);
  ret = usbmsc_configure(CONFIG_SYSTEM_COMPOSITE_NLUNS, &g_composite.mschandle);
  if (ret < 0)
    {
      printf("board_mscclassobject: usbmsc_configure failed: %d\n", -ret);
      return ret;
    }

  printf("board_mscclassobject: MSC handle=%p\n", g_composite.mschandle);
  check_test_memory_usage("After usbmsc_configure()");

  /* Bind the LUN(s) */

  printf("board_mscclassobject: Bind LUN=0 to %s\n", CONFIG_SYSTEM_COMPOSITE_DEVPATH1);
  ret = usbmsc_bindlun(g_composite.mschandle, CONFIG_SYSTEM_COMPOSITE_DEVPATH1, 0, 0, 0, false);
  if (ret < 0)
    {
      printf("board_mscclassobject: usbmsc_bindlun failed for LUN 1 using %s: %d\n",
               CONFIG_SYSTEM_COMPOSITE_DEVPATH1, -ret);
      usbmsc_uninitialize(g_composite.mschandle);
      return ret;
    }

  check_test_memory_usage("After usbmsc_bindlun()");

#if CONFIG_SYSTEM_COMPOSITE_NLUNS > 1

  printf("board_mscclassobject: Bind LUN=1 to %s\n", CONFIG_SYSTEM_COMPOSITE_DEVPATH2);
  ret = usbmsc_bindlun(g_composite.mschandle, CONFIG_SYSTEM_COMPOSITE_DEVPATH2, 1, 0, 0, false);
  if (ret < 0)
    {
      printf("board_mscclassobject: usbmsc_bindlun failed for LUN 2 using %s: %d\n",
               CONFIG_SYSTEM_COMPOSITE_DEVPATH2, -ret);
      usbmsc_uninitialize(g_composite.mschandle);
      return ret;
    }

  check_test_memory_usage("After usbmsc_bindlun() #2");

#if CONFIG_SYSTEM_COMPOSITE_NLUNS > 2

  printf("board_mscclassobject: Bind LUN=2 to %s\n", CONFIG_SYSTEM_COMPOSITE_DEVPATH3);
  ret = usbmsc_bindlun(g_composite.mschandle, CONFIG_SYSTEM_COMPOSITE_DEVPATH3, 2, 0, 0, false);
  if (ret < 0)
    {
      printf("board_mscclassobject: usbmsc_bindlun failed for LUN 3 using %s: %d\n",
               CONFIG_SYSTEM_COMPOSITE_DEVPATH3, -ret);
      usbmsc_uninitialize(g_composite.mschandle);
      return ret;
    }

  check_test_memory_usage("After usbmsc_bindlun() #3");

#endif
#endif

  /* Get the mass storage device's class object */

  ret = usbmsc_classobject(g_composite.mschandle, classdev);
  if (ret < 0)
    {
      printf("board_mscclassobject: usbmsc_classobject failed: %d\n", -ret);
      usbmsc_uninitialize(g_composite.mschandle);
    }

  check_test_memory_usage("After usbmsc_classobject()");
  return ret;
}
Esempio n. 9
0
int user_start(int argc, char *argv[])
{
  void *handle;
  int ret;

  /* Initialize USB trace output IDs */

  usbtrace_enable(TRACE_BITSET);

  /* Register block drivers (architecture-specific) */

  message("user_start: Creating block drivers\n");
  ret = usbstrg_archinitialize();
  if (ret < 0)
    {
      message("user_start: usbstrg_archinitialize failed: %d\n", -ret);
      return 1;
    }

  /* Then exports the LUN(s) */

  message("user_start: Configuring with NLUNS=%d\n", CONFIG_EXAMPLES_USBSTRG_NLUNS);
  ret = usbstrg_configure(CONFIG_EXAMPLES_USBSTRG_NLUNS, &handle);
  if (ret < 0)
    {
      message("user_start: usbstrg_configure failed: %d\n", -ret);
      usbstrg_uninitialize(handle);
      return 2;
    }
  message("user_start: handle=%p\n", handle);

  message("user_start: Bind LUN=0 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH1, 0, 0, 0, false);
  if (ret < 0)
    {
      message("user_start: usbstrg_bindlun failed for LUN 1 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH1, -ret);
      usbstrg_uninitialize(handle);
      return 2;
    }

#if CONFIG_EXAMPLES_USBSTRG_NLUNS > 1

  message("user_start: Bind LUN=1 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH2, 1, 0, 0, false);
  if (ret < 0)
    {
      message("user_start: usbstrg_bindlun failed for LUN 2 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH2, -ret);
      usbstrg_uninitialize(handle);
      return 3;
    }

#if CONFIG_EXAMPLES_USBSTRG_NLUNS > 2

  message("user_start: Bind LUN=2 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH3, 2, 0, 0, false);
  if (ret < 0)
    {
      message("user_start: usbstrg_bindlun failed for LUN 3 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH3, -ret);
      usbstrg_uninitialize(handle);
      return 4;
    }

#endif
#endif

  ret = usbstrg_exportluns(handle);
  if (ret < 0)
    {
      message("user_start: usbstrg_exportluns failed: %d\n", -ret);
      usbstrg_uninitialize(handle);
      return 5;
    }

  /* Now just hang around and monitor the USB storage activity */

#ifndef CONFIG_DISABLE_SIGNALS
  for (;;)
    {
      msgflush();
      sleep(5);

#ifdef CONFIG_USBDEV_TRACE
      message("\nuser_start: USB TRACE DATA:\n");
      ret =  usbtrace_enumerate(usbstrg_enumerate, NULL);
      if (ret < 0)
        {
          message("user_start: usbtrace_enumerate failed: %d\n", -ret);
          usbstrg_uninitialize(handle);
          return 6;
        }
#else
      message("user_start: Still alive\n");
#endif
    }
#else
     message("user_start: Exiting\n");
 #endif
}
Esempio n. 10
0
int usbterm_main(int argc, char *argv[])
#endif
{
  struct boardioc_usbdev_ctrl_s ctrl;
  FAR void *handle;
  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
  printf("usbterm_main: Performing external device initialization\n");
  ret = usbterm_devinit();
  if (ret != OK)
    {
      printf("usbterm_main: usbterm_devinit failed: %d\n", ret);
      goto errout;
    }
#endif

  /* Initialize the USB serial driver */

  printf("usbterm_main: Registering USB serial driver\n");

#ifdef CONFIG_CDCACM

  ctrl.usbdev   = BOARDIOC_USBDEV_CDCACM;
  ctrl.action   = BOARDIOC_USBDEV_CONNECT;
  ctrl.instance = 0;
  ctrl.handle   = &handle;

#else

  ctrl.usbdev   = BOARDIOC_USBDEV_PL2303;
  ctrl.action   = BOARDIOC_USBDEV_CONNECT;
  ctrl.instance = 0;
  ctrl.handle   = &handle;

#endif

  ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
  if (ret < 0)
    {
      printf("usbterm_main: ERROR: Failed to create the USB serial device: %d\n", -ret);
      goto errout_with_devinit;
    }

  printf("usbterm_main: Successfully registered the serial driver\n");

#if defined(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
    {
      printf("usbterm_main: Opening USB serial driver\n");

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

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

          if (errcode == ENOTCONN)
            {
              printf("usbterm_main:        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)
    {
      printf("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for reading: %d\n", errno);
      goto errout_with_outstream;
    }

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

  /* Start the USB term listener thread */

  printf("usbterm_main: Starting the listener thread\n");

  ret = pthread_attr_init(&attr);
  if (ret != OK)
    {
      printf("usbterm_main: 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)
    {
      printf("usbterm_main: Error in thread creation: %d\n", ret);
      goto errout_with_streams;
    }

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

  printf("usbterm_main: 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 EOF on end of file or if an error occurs.  Either
       * will cause the session to terminate.
       */

      if (ret == EOF)
        {
          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");
        }

      /* 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
  printf("usbterm_main:        Aborting\n");
  return 1;
}
Esempio n. 11
0
int MAIN_NAME(int argc, char *argv[])
{
  FAR void *handle;
  int ret;

  /* If this program is implemented as the NSH 'msconn' command, then we need to
   * do a little error checking to assure that we are not being called re-entrantly.
   */

#ifdef CONFIG_EXAMPLES_USBSTRG_BUILTIN

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

   if (g_usbstrg.mshandle)
     {
       message(MAIN_NAME_STRING ": ERROR: Already connected\n");
       return 1;
     }
#endif

#ifdef CONFIG_EXAMPLES_USBSTRG_DEBUGMM
#  ifdef CONFIG_CAN_PASS_STRUCTS
  g_usbstrg.mmstart    = mallinfo();
  g_usbstrg.mmprevious = g_usbstrg.mmstart;
#  else
  (void)mallinfo(&g_usbstrg.mmstart);
  memcpy(&g_usbstrg.mmprevious, &g_usbstrg.mmstart, sizeof(struct mallinfo));
#  endif
#endif

  /* Initialize USB trace output IDs */

  usbtrace_enable(TRACE_BITSET);
  check_test_memory_usage("After usbtrace_enable()");

  /* Register block drivers (architecture-specific) */

  message(MAIN_NAME_STRING ": Creating block drivers\n");
  ret = usbstrg_archinitialize();
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_archinitialize failed: %d\n", -ret);
      return 2;
    }
  check_test_memory_usage("After usbstrg_archinitialize()");

  /* Then exports the LUN(s) */

  message(MAIN_NAME_STRING ": Configuring with NLUNS=%d\n", CONFIG_EXAMPLES_USBSTRG_NLUNS);
  ret = usbstrg_configure(CONFIG_EXAMPLES_USBSTRG_NLUNS, &handle);
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_configure failed: %d\n", -ret);
      usbstrg_uninitialize(handle);
      return 3;
    }
  message(MAIN_NAME_STRING ": handle=%p\n", handle);
  check_test_memory_usage("After usbstrg_configure()");

  message(MAIN_NAME_STRING ": Bind LUN=0 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH1);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH1, 0, 0, 0, false);
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 1 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH1, -ret);
      usbstrg_uninitialize(handle);
      return 4;
    }
  check_test_memory_usage("After usbstrg_bindlun()");

#if CONFIG_EXAMPLES_USBSTRG_NLUNS > 1

  message(MAIN_NAME_STRING ": Bind LUN=1 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH2);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH2, 1, 0, 0, false);
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 2 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH2, -ret);
      usbstrg_uninitialize(handle);
      return 5;
    }
  check_test_memory_usage("After usbstrg_bindlun() #2");

#if CONFIG_EXAMPLES_USBSTRG_NLUNS > 2

  message(MAIN_NAME_STRING ": Bind LUN=2 to %s\n", CONFIG_EXAMPLES_USBSTRG_DEVPATH3);
  ret = usbstrg_bindlun(handle, CONFIG_EXAMPLES_USBSTRG_DEVPATH3, 2, 0, 0, false);
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_bindlun failed for LUN 3 using %s: %d\n",
               CONFIG_EXAMPLES_USBSTRG_DEVPATH3, -ret);
      usbstrg_uninitialize(handle);
      return 6;
    }
  check_test_memory_usage("After usbstrg_bindlun() #3");

#endif
#endif

  ret = usbstrg_exportluns(handle);
  if (ret < 0)
    {
      message(MAIN_NAME_STRING ": usbstrg_exportluns failed: %d\n", -ret);
      usbstrg_uninitialize(handle);
      return 7;
    }
  check_test_memory_usage("After usbstrg_exportluns()");

  /* It this program was configued as an NSH command, then just exit now.
   * Also, if signals are not enabled (and, hence, sleep() is not supported.
   * then we have not real option but to exit now.
   */

#if !defined(CONFIG_EXAMPLES_USBSTRG_BUILTIN) && !defined(CONFIG_DISABLE_SIGNALS)

  /* Otherwise, this thread will hang around and monitor the USB storage activity */

  for (;;)
    {
      msgflush();
      sleep(5);

#  ifdef CONFIG_USBDEV_TRACE
      message("\nuser_start: USB TRACE DATA:\n");
      ret =  usbtrace_enumerate(usbstrg_enumerate, NULL);
      if (ret < 0)
        {
          message(MAIN_NAME_STRING ": usbtrace_enumerate failed: %d\n", -ret);
          usbstrg_uninitialize(handle);
          return 8;
        }
      check_test_memory_usage("After usbtrace_enumerate()");
#  else
      message(MAIN_NAME_STRING ": Still alive\n");
#  endif
    }
#elif defined(CONFIG_EXAMPLES_USBSTRG_BUILTIN)

   /* Return the USB mass storage device handle so it can be used by the 'misconn'
    * command.
    */

   message(MAIN_NAME_STRING ": Connected\n");
   g_usbstrg.mshandle = handle;
   check_test_memory_usage("After MS connection");

#else /* defined(CONFIG_DISABLE_SIGNALS) */

  /* Just exit */
 
   message(MAIN_NAME_STRING ": Exiting\n");

   /* Dump debug memory usage */
 
   final_memory_usage("Final memory usage");
#endif
   return 0;
}
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;
}