2011-09-26 18:52:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* examples/usbterm/usbterm_main.c
|
|
|
|
*
|
2013-02-10 01:46:27 +01:00
|
|
|
* Copyright (C) 2011-2013 Gregory Nutt. All rights reserved.
|
2011-12-30 15:54:43 +01:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-09-26 18:52:20 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2012-02-01 20:47:12 +01:00
|
|
|
#include <apps/readline.h>
|
|
|
|
|
2011-09-26 18:52:20 +02:00
|
|
|
#include <nuttx/usb/usbdev.h>
|
|
|
|
#include <nuttx/usb/usbdev_trace.h>
|
|
|
|
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifdef CONFIG_CDCACM
|
|
|
|
# include <nuttx/usb/cdcacm.h>
|
2011-09-26 18:52:20 +02:00
|
|
|
#endif
|
|
|
|
|
2012-03-06 21:21:57 +01:00
|
|
|
#ifdef CONFIG_CDCACM
|
|
|
|
# include <nuttx/usb/pl2303.h>
|
|
|
|
#endif
|
|
|
|
|
2011-09-26 18:52:20 +02:00
|
|
|
#include "usbterm.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* USB terminal state data */
|
|
|
|
|
|
|
|
struct usbterm_globals_s g_usbterm;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: trace_callback
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Callback from USB trace instrumentation.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
static int trace_callback(struct usbtrace_s *trace, void *arg)
|
|
|
|
{
|
|
|
|
usbtrace_trprintf((trprintf_t)trmessage, trace->event, trace->value);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dumptrace
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Dump collected trace data.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_USBDEV_TRACE
|
|
|
|
static void dumptrace(void)
|
|
|
|
{
|
|
|
|
(void)usbtrace_enumerate(trace_callback, NULL);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define dumptrace()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: dumptrace
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Entry point for the listener thread.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR void *usbterm_listener(FAR void *parameter)
|
|
|
|
{
|
|
|
|
message("usbterm_listener: Waiting for remote input\n");
|
|
|
|
for (;;)
|
|
|
|
{
|
2012-03-04 18:38:00 +01:00
|
|
|
/* Display the prompt string on the remote USB serial connection -- only
|
|
|
|
* if we know that there is someone listening at the other end. The
|
|
|
|
* remote side must initiate the the conversation.
|
|
|
|
*/
|
2011-09-26 18:52:20 +02:00
|
|
|
|
2012-03-04 18:38:00 +01:00
|
|
|
if (g_usbterm.peer)
|
|
|
|
{
|
|
|
|
fputs("\rusbterm> ", g_usbterm.outstream);
|
|
|
|
fflush(g_usbterm.outstream);
|
|
|
|
}
|
2011-09-26 18:52:20 +02:00
|
|
|
|
2012-03-06 16:51:03 +01:00
|
|
|
/* Get the next line of input from the remote USB serial connection */
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
if (fgets(g_usbterm.inbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, g_usbterm.instream))
|
|
|
|
{
|
2012-03-04 18:38:00 +01:00
|
|
|
/* If we receive anything, then we can be assured that there is someone
|
|
|
|
* with the serial driver open on the remote host.
|
|
|
|
*/
|
|
|
|
|
|
|
|
g_usbterm.peer = true;
|
|
|
|
|
2011-09-27 19:05:24 +02:00
|
|
|
/* Echo the line on the local stdout */
|
|
|
|
|
|
|
|
fputs(g_usbterm.inbuffer, stdout);
|
|
|
|
|
|
|
|
/* Display the prompt string on stdout */
|
2011-09-26 18:52:20 +02:00
|
|
|
|
2011-09-27 19:05:24 +02:00
|
|
|
fputs("usbterm> ", stdout);
|
2011-09-26 18:52:20 +02:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If USB tracing is enabled, then dump all collected trace data to stdout */
|
|
|
|
|
|
|
|
dumptrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Won't get here */
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2012-08-30 22:13:50 +02:00
|
|
|
* Name: usbterm_main
|
2011-09-26 18:52:20 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Main entry point for the USB serial terminal example.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
int usbterm_main(int argc, char *argv[])
|
2011-09-26 18:52:20 +02:00
|
|
|
{
|
|
|
|
pthread_attr_t attr;
|
|
|
|
int ret;
|
|
|
|
|
2012-03-04 18:38:00 +01:00
|
|
|
/* Initialize global data */
|
|
|
|
|
|
|
|
memset(&g_usbterm, 0, sizeof(struct usbterm_globals_s));
|
|
|
|
|
2011-12-30 15:54:43 +01:00
|
|
|
/* Initialization of the USB hardware may be performed by logic external to
|
|
|
|
* this test.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Performing external device initialization\n");
|
2011-12-30 15:54:43 +01:00
|
|
|
ret = usbterm_devinit();
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: usbterm_devinit failed: %d\n", ret);
|
2011-12-30 15:54:43 +01:00
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-09-26 18:52:20 +02:00
|
|
|
/* Initialize the USB serial driver */
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Registering USB serial driver\n");
|
2012-01-26 00:04:17 +01:00
|
|
|
#ifdef CONFIG_CDCACM
|
2012-02-28 19:14:55 +01:00
|
|
|
ret = cdcacm_initialize(0, NULL);
|
2011-09-26 18:52:20 +02:00
|
|
|
#else
|
|
|
|
ret = usbdev_serialinitialize(0);
|
|
|
|
#endif
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: ERROR: Failed to create the USB serial device: %d\n", -ret);
|
2011-12-30 15:54:43 +01:00
|
|
|
goto errout_with_devinit;
|
2011-09-26 18:52:20 +02:00
|
|
|
}
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Successfully registered the serial driver\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
#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
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Opening USB serial driver\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
|
2012-02-29 22:53:28 +01:00
|
|
|
g_usbterm.outstream = fopen(USBTERM_DEVNAME, "w");
|
2011-09-26 18:52:20 +02:00
|
|
|
if (g_usbterm.outstream == NULL)
|
|
|
|
{
|
|
|
|
int errcode = errno;
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for writing: %d\n",
|
2011-09-26 18:52:20 +02:00
|
|
|
errcode);
|
|
|
|
|
|
|
|
/* ENOTCONN means that the USB device is not yet connected */
|
|
|
|
|
|
|
|
if (errcode == ENOTCONN)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Not connected. Wait and try again.\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
sleep(5);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Give up on other errors */
|
|
|
|
|
2011-12-30 15:54:43 +01:00
|
|
|
goto errout_with_devinit;
|
2011-09-26 18:52:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If USB tracing is enabled, then dump all collected trace data to stdout */
|
|
|
|
|
|
|
|
dumptrace();
|
|
|
|
}
|
2011-09-27 19:05:24 +02:00
|
|
|
while (g_usbterm.outstream == NULL);
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
/* Open the USB serial device for reading. Since we are already connected, this
|
|
|
|
* should not fail.
|
|
|
|
*/
|
|
|
|
|
2012-02-29 22:53:28 +01:00
|
|
|
g_usbterm.instream = fopen(USBTERM_DEVNAME, "r");
|
2011-09-27 19:05:24 +02:00
|
|
|
if (g_usbterm.instream == NULL)
|
2011-09-26 18:52:20 +02:00
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for reading: %d\n", errno);
|
2011-09-26 18:52:20 +02:00
|
|
|
goto errout_with_outstream;
|
|
|
|
}
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Successfully opened the serial driver\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
/* Start the USB term listener thread */
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Starting the listener thread\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
ret = pthread_attr_init(&attr);
|
|
|
|
if (ret != OK)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: pthread_attr_init failed: %d\n", ret);
|
2011-09-26 18:52:20 +02:00
|
|
|
goto errout_with_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = pthread_create(&g_usbterm.listener, &attr,
|
|
|
|
usbterm_listener, (pthread_addr_t)0);
|
|
|
|
if (ret != 0)
|
|
|
|
{
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Error in thread creation: %d\n", ret);
|
2011-09-26 18:52:20 +02:00
|
|
|
goto errout_with_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send messages and get responses -- forever */
|
|
|
|
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Waiting for local input\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
/* Display the prompt string on stdout */
|
|
|
|
|
|
|
|
fputs("usbterm> ", stdout);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2012-02-01 20:47:12 +01:00
|
|
|
/* 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,
|
2013-02-10 01:46:27 +01:00
|
|
|
* but will return EOF on end of file or if an error occurs. Either
|
|
|
|
* will cause the session to terminate.
|
2012-02-01 20:47:12 +01:00
|
|
|
*/
|
2011-09-26 18:52:20 +02:00
|
|
|
|
2013-02-10 01:46:27 +01:00
|
|
|
if (ret == EOF)
|
2012-02-01 20:47:12 +01:00
|
|
|
{
|
|
|
|
printf("ERROR: readline failed: %d\n", ret);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-04 18:38:00 +01:00
|
|
|
/* Is there anyone listening on the other end? */
|
|
|
|
|
|
|
|
else if (g_usbterm.peer)
|
2011-09-26 18:52:20 +02:00
|
|
|
{
|
2012-03-04 18:38:00 +01:00
|
|
|
/* Yes.. Send the line of input via USB */
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
fputs(g_usbterm.outbuffer, g_usbterm.outstream);
|
2011-09-27 19:05:24 +02:00
|
|
|
|
|
|
|
/* Display the prompt string on the remote USB serial connection */
|
|
|
|
|
2011-09-27 22:11:14 +02:00
|
|
|
fputs("\rusbterm> ", g_usbterm.outstream);
|
2011-09-26 18:52:20 +02:00
|
|
|
fflush(g_usbterm.outstream);
|
|
|
|
}
|
2012-03-04 18:38:00 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Still waiting for remote peer. Please try again later.\n", ret);
|
|
|
|
}
|
2011-09-26 18:52:20 +02:00
|
|
|
|
|
|
|
/* 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);
|
2011-12-30 15:54:43 +01:00
|
|
|
errout_with_devinit:
|
|
|
|
#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
|
|
|
|
usbterm_devuninit();
|
2011-09-26 18:52:20 +02:00
|
|
|
errout:
|
2011-12-30 15:54:43 +01:00
|
|
|
#endif
|
2012-08-30 22:13:50 +02:00
|
|
|
message("usbterm_main: Aborting\n");
|
2011-09-26 18:52:20 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|