mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-18 00:03:33 +08:00
Resync new repository with old repo r5166
git-svn-id: http://svn.code.sf.net/p/nuttx/code/trunk@5153 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
#
|
||||
# For a description of the syntax of this configuration file,
|
||||
# see misc/tools/kconfig-language.txt.
|
||||
#
|
||||
|
||||
config EXAMPLES_USBTERM
|
||||
bool "USB serial terminal example"
|
||||
default n
|
||||
---help---
|
||||
Enable the USB serial terminal example
|
||||
|
||||
if EXAMPLES_USBTERM
|
||||
endif
|
||||
@@ -0,0 +1,106 @@
|
||||
############################################################################
|
||||
# apps/examples/usbterm/Makefile
|
||||
#
|
||||
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
-include $(TOPDIR)/.config
|
||||
-include $(TOPDIR)/Make.defs
|
||||
include $(APPDIR)/Make.defs
|
||||
|
||||
# USB terminal example
|
||||
|
||||
ASRCS =
|
||||
CSRCS = usbterm_main.c
|
||||
|
||||
AOBJS = $(ASRCS:.S=$(OBJEXT))
|
||||
COBJS = $(CSRCS:.c=$(OBJEXT))
|
||||
|
||||
SRCS = $(ASRCS) $(CSRCS)
|
||||
OBJS = $(AOBJS) $(COBJS)
|
||||
|
||||
ifeq ($(WINTOOL),y)
|
||||
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
|
||||
else
|
||||
BIN = "$(APPDIR)/libapps$(LIBEXT)"
|
||||
endif
|
||||
|
||||
ROOTDEPPATH = --dep-path .
|
||||
|
||||
# Built-in application info
|
||||
|
||||
APPNAME = usbterm
|
||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
STACKSIZE = 2048
|
||||
|
||||
# Common build
|
||||
|
||||
VPATH =
|
||||
|
||||
all: .built
|
||||
.PHONY: context clean depend distclean
|
||||
|
||||
$(AOBJS): %$(OBJEXT): %.S
|
||||
$(call ASSEMBLE, $<, $@)
|
||||
|
||||
$(COBJS): %$(OBJEXT): %.c
|
||||
$(call COMPILE, $<, $@)
|
||||
|
||||
.built: $(OBJS)
|
||||
@( for obj in $(OBJS) ; do \
|
||||
$(call ARCHIVE, $(BIN), $${obj}); \
|
||||
done ; )
|
||||
@touch .built
|
||||
|
||||
.context:
|
||||
ifeq ($(CONFIG_EXAMPLES_USBTERM_BUILTIN),y)
|
||||
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
|
||||
@touch $@
|
||||
endif
|
||||
|
||||
context: .context
|
||||
|
||||
.depend: Makefile $(SRCS)
|
||||
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
|
||||
@touch $@
|
||||
|
||||
depend: .depend
|
||||
|
||||
clean:
|
||||
@rm -f *.o *~ .*.swp .built
|
||||
$(call CLEAN)
|
||||
|
||||
distclean: clean
|
||||
@rm -f Make.dep .depend
|
||||
|
||||
-include Make.dep
|
||||
|
||||
@@ -0,0 +1,181 @@
|
||||
/****************************************************************************
|
||||
* examples/usbterm/usbterm.h
|
||||
*
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __APPS_EXAMPLES_USBTERM_USBTERM_H
|
||||
#define __APPS_EXAMPLES_USBTERM_USBTERM_H
|
||||
|
||||
/****************************************************************************
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <nuttx/config.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <semaphore.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
/* Configuration ************************************************************/
|
||||
|
||||
#ifndef CONFIG_EXAMPLES_USBTERM_BUFLEN
|
||||
# define CONFIG_EXAMPLES_USBTERM_BUFLEN 256
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_TRACEINIT
|
||||
# define TRACE_INIT_BITS (TRACE_INIT_BIT)
|
||||
#else
|
||||
# define TRACE_INIT_BITS (0)
|
||||
#endif
|
||||
|
||||
#define TRACE_ERROR_BITS (TRACE_DEVERROR_BIT|TRACE_CLSERROR_BIT)
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_TRACECLASS
|
||||
# define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|TRACE_CLASSSTATE_BIT)
|
||||
#else
|
||||
# define TRACE_CLASS_BITS (0)
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_TRACETRANSFERS
|
||||
# define TRACE_TRANSFER_BITS (TRACE_OUTREQQUEUED_BIT|TRACE_INREQQUEUED_BIT|TRACE_READ_BIT|\
|
||||
TRACE_WRITE_BIT|TRACE_COMPLETE_BIT)
|
||||
#else
|
||||
# define TRACE_TRANSFER_BITS (0)
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_TRACECONTROLLER
|
||||
# define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT)
|
||||
#else
|
||||
# define TRACE_CONTROLLER_BITS (0)
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_TRACEINTERRUPTS
|
||||
# define TRACE_INTERRUPT_BITS (TRACE_INTENTRY_BIT|TRACE_INTDECODE_BIT|TRACE_INTEXIT_BIT)
|
||||
#else
|
||||
# define TRACE_INTERRUPT_BITS (0)
|
||||
#endif
|
||||
|
||||
#define TRACE_BITSET (TRACE_INIT_BITS|TRACE_ERROR_BITS|TRACE_CLASS_BITS|\
|
||||
TRACE_TRANSFER_BITS|TRACE_CONTROLLER_BITS|TRACE_INTERRUPT_BITS)
|
||||
|
||||
#ifdef CONFIG_CDCACM
|
||||
# define USBTERM_DEVNAME "/dev/ttyACM0"
|
||||
#else
|
||||
# define USBTERM_DEVNAME "/dev/ttyUSB0"
|
||||
#endif
|
||||
|
||||
/* Debug ********************************************************************/
|
||||
|
||||
#ifdef CONFIG_CPP_HAVE_VARARGS
|
||||
# ifdef CONFIG_DEBUG
|
||||
# define message(...) lib_rawprintf(__VA_ARGS__)
|
||||
# define trmessage lib_rawprintf
|
||||
# else
|
||||
# define message(...) printf(__VA_ARGS__)
|
||||
# define trmessage printf
|
||||
# endif
|
||||
#else
|
||||
# ifdef CONFIG_DEBUG
|
||||
# define message lib_lowprintf
|
||||
# define trmessage lib_lowprintf
|
||||
# else
|
||||
# define message printf
|
||||
# define trmessage printf
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#define IOBUFFER_SIZE 256
|
||||
|
||||
/****************************************************************************
|
||||
* Public Types
|
||||
****************************************************************************/
|
||||
|
||||
/* All USB terminal state data is packaged in a single structure to minimize
|
||||
* name conflicts with other global symbols -- a poor man's name space.
|
||||
*/
|
||||
|
||||
struct usbterm_globals_s
|
||||
{
|
||||
FILE *instream; /* Stream for incoming USB data */
|
||||
FILE *outstream; /* Stream for outgoing USB data */
|
||||
pthread_t listener; /* USB terminal listener thread */
|
||||
bool peer; /* True: A peer is connected to the serial port on
|
||||
* the remote host */
|
||||
|
||||
/* Buffers for incoming and outgoing data */
|
||||
|
||||
char inbuffer[CONFIG_EXAMPLES_USBTERM_BUFLEN];
|
||||
char outbuffer[CONFIG_EXAMPLES_USBTERM_BUFLEN];
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/* USB terminal state data */
|
||||
|
||||
extern struct usbterm_globals_s g_usbterm;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Function Prototypes
|
||||
****************************************************************************/
|
||||
/****************************************************************************
|
||||
* Name:
|
||||
*
|
||||
* Description:
|
||||
* If CONFIG_EXAMPLES_USBTERM_DEVINIT is defined, then the example will
|
||||
* call this user provided function as part of its initialization.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
|
||||
int usbterm_devinit(void);
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Name:
|
||||
*
|
||||
* Description:
|
||||
* If CONFIG_EXAMPLES_USBTERM_DEVINIT is defined, then the example will
|
||||
* call this user provided function as part of its termination sequeunce.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_EXAMPLES_USBTERM_DEVINIT
|
||||
void usbterm_devuninit(void);
|
||||
#endif
|
||||
|
||||
#endif /* __APPS_EXAMPLES_USBTERM_USBTERM_H */
|
||||
@@ -0,0 +1,376 @@
|
||||
/****************************************************************************
|
||||
* examples/usbterm/usbterm_main.c
|
||||
*
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
|
||||
#include <apps/readline.h>
|
||||
|
||||
#include <nuttx/usb/usbdev.h>
|
||||
#include <nuttx/usb/usbdev_trace.h>
|
||||
|
||||
#ifdef CONFIG_CDCACM
|
||||
# include <nuttx/usb/cdcacm.h>
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_CDCACM
|
||||
# include <nuttx/usb/pl2303.h>
|
||||
#endif
|
||||
|
||||
#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 (;;)
|
||||
{
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
if (g_usbterm.peer)
|
||||
{
|
||||
fputs("\rusbterm> ", g_usbterm.outstream);
|
||||
fflush(g_usbterm.outstream);
|
||||
}
|
||||
|
||||
/* Get the next line of input from the remote USB serial connection */
|
||||
|
||||
if (fgets(g_usbterm.inbuffer, CONFIG_EXAMPLES_USBTERM_BUFLEN, g_usbterm.instream))
|
||||
{
|
||||
/* 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;
|
||||
|
||||
/* Echo the line on the local stdout */
|
||||
|
||||
fputs(g_usbterm.inbuffer, stdout);
|
||||
|
||||
/* Display the prompt string on stdout */
|
||||
|
||||
fputs("usbterm> ", stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
/* If USB tracing is enabled, then dump all collected trace data to stdout */
|
||||
|
||||
dumptrace();
|
||||
}
|
||||
|
||||
/* Won't get here */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Name: usbterm_main
|
||||
*
|
||||
* Description:
|
||||
* Main entry point for the USB serial terminal example.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int usbterm_main(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("usbterm_main: Performing external device initialization\n");
|
||||
ret = usbterm_devinit();
|
||||
if (ret != OK)
|
||||
{
|
||||
message("usbterm_main: usbterm_devinit failed: %d\n", ret);
|
||||
goto errout;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Initialize the USB serial driver */
|
||||
|
||||
message("usbterm_main: Registering USB serial driver\n");
|
||||
#ifdef CONFIG_CDCACM
|
||||
ret = cdcacm_initialize(0, NULL);
|
||||
#else
|
||||
ret = usbdev_serialinitialize(0);
|
||||
#endif
|
||||
if (ret < 0)
|
||||
{
|
||||
message("usbterm_main: ERROR: Failed to create the USB serial device: %d\n", -ret);
|
||||
goto errout_with_devinit;
|
||||
}
|
||||
message("usbterm_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 */
|
||||
|
||||
do
|
||||
{
|
||||
message("usbterm_main: Opening USB serial driver\n");
|
||||
|
||||
g_usbterm.outstream = fopen(USBTERM_DEVNAME, "w");
|
||||
if (g_usbterm.outstream == NULL)
|
||||
{
|
||||
int errcode = errno;
|
||||
message("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)
|
||||
{
|
||||
message("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)
|
||||
{
|
||||
message("usbterm_main: ERROR: Failed to open " USBTERM_DEVNAME " for reading: %d\n", errno);
|
||||
goto errout_with_outstream;
|
||||
}
|
||||
|
||||
message("usbterm_main: Successfully opened the serial driver\n");
|
||||
|
||||
/* Start the USB term listener thread */
|
||||
|
||||
message("usbterm_main: Starting the listener thread\n");
|
||||
|
||||
ret = pthread_attr_init(&attr);
|
||||
if (ret != OK)
|
||||
{
|
||||
message("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)
|
||||
{
|
||||
message("usbterm_main: Error in thread creation: %d\n", ret);
|
||||
goto errout_with_streams;
|
||||
}
|
||||
|
||||
/* Send messages and get responses -- forever */
|
||||
|
||||
message("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 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("usbterm_main: Aborting\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user