From 58a755a096e3c671096f7477320d0f5304415ec1 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 24 Jan 2012 00:00:31 +0000 Subject: If we are using a USB serial console, then NSH must wait for the USB device to be connected git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4327 42af7a65-404d-4744-a932-0658087f49c3 --- apps/ChangeLog.txt | 4 ++ apps/nshlib/Makefile | 3 + apps/nshlib/nsh.h | 116 ++++++++++++++++++++---------------- apps/nshlib/nsh_mmcmds.c | 8 +-- apps/nshlib/nsh_serial.c | 12 +++- apps/nshlib/nsh_usbdev.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 237 insertions(+), 55 deletions(-) create mode 100644 apps/nshlib/nsh_usbdev.c (limited to 'apps') diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index a157b5809..486639fb8 100755 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -159,3 +159,7 @@ added to the PWM interface. 6.15 2012-xx-xx Gregory Nutt + + * apps/nshlib/nsh_serial.c and nsh_usbdev.c: If NuttX is configured to use + a USB serial console, then NSH needs to wait until the USB console is + connected and available. diff --git a/apps/nshlib/Makefile b/apps/nshlib/Makefile index 91ffc2c5e..70c27a414 100644 --- a/apps/nshlib/Makefile +++ b/apps/nshlib/Makefile @@ -71,6 +71,9 @@ ifneq ($(CONFIG_NSH_DISABLESCRIPT),y) CSRCS += nsh_test.c endif +ifneq ($(CONFIG_USBDEV),y) +CSRCS += nsh_usbdev.c +endif AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/nshlib/nsh.h b/apps/nshlib/nsh.h index fd265982f..2ade559b1 100644 --- a/apps/nshlib/nsh.h +++ b/apps/nshlib/nsh.h @@ -76,6 +76,19 @@ # error "No NSH front end defined" #endif +/* If a USB device is selected for the NSH console then we need to handle some + * special start-up conditions. + */ + +#undef HAVE_USB_CONSOLE +#if defined(CONFIG_USBDEV) +# if defined(CONFIG_USBSER) && defined(CONFIG_USBSER_CONSOLE) +# define HAVE_USB_CONSOLE 1 +# elif defined(CONFIG_CDCSER) && defined(CONFIG_CDCSER_CONSOLE) +# define HAVE_USB_CONSOLE 1 +# endif +#endif + /* Verify support for ROMFS /etc directory support options */ #ifdef CONFIG_NSH_ROMFSETC @@ -300,197 +313,202 @@ extern const char g_fmtsignalrecvd[]; /* Initialization */ #ifdef CONFIG_NSH_ROMFSETC -extern int nsh_romfsetc(void); +int nsh_romfsetc(void); #else # define nsh_romfsetc() (-ENOSYS) #endif #ifdef CONFIG_NET -extern int nsh_netinit(void); +int nsh_netinit(void); #else # define nsh_netinit() (-ENOSYS) #endif +#ifdef HAVE_USB_CONSOLE +int nsh_usbconsole(void); +#else +# define nsh_usbconsole() (-ENOSYS) +#endif + #if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT) -extern int nsh_script(FAR struct nsh_vtbl_s *vtbl, const char *cmd, const char *path); +int nsh_script(FAR struct nsh_vtbl_s *vtbl, const char *cmd, const char *path); #endif /* Architecture-specific initialization */ #ifdef CONFIG_NSH_ARCHINIT -extern int nsh_archinitialize(void); +int nsh_archinitialize(void); #else # define nsh_archinitialize() (-ENOSYS) #endif /* Message handler */ -extern int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline); +int nsh_parse(FAR struct nsh_vtbl_s *vtbl, char *cmdline); /* Application interface */ #ifdef CONFIG_NSH_BUILTIN_APPS -extern int nsh_execapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, - FAR char **argv); +int nsh_execapp(FAR struct nsh_vtbl_s *vtbl, FAR const char *cmd, FAR char **argv); #endif /* Working directory support */ #if CONFIG_NFILE_DESCRIPTORS > 0 && !defined(CONFIG_DISABLE_ENVIRON) -extern FAR const char *nsh_getcwd(void); -extern char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl, const char *relpath); -extern void nsh_freefullpath(char *relpath); +FAR const char *nsh_getcwd(void); +char *nsh_getfullpath(FAR struct nsh_vtbl_s *vtbl, const char *relpath); +void nsh_freefullpath(char *relpath); #endif /* Debug */ -extern void nsh_dumpbuffer(FAR struct nsh_vtbl_s *vtbl, const char *msg, - const uint8_t *buffer, ssize_t nbytes); +void nsh_dumpbuffer(FAR struct nsh_vtbl_s *vtbl, const char *msg, + const uint8_t *buffer, ssize_t nbytes); /* Shell command handlers */ #ifndef CONFIG_NSH_DISABLE_ECHO - extern int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_echo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_EXEC - extern int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_exec(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_MB - extern int cmd_mb(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mb(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_MH - extern int cmd_mh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_MW - extern int cmd_mw(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mw(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_FREE - extern int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_PS - extern int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ps(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_NSH_DISABLE_XD - extern int cmd_xd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_xd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #if !defined(CONFIG_NSH_DISABLESCRIPT) && !defined(CONFIG_NSH_DISABLE_TEST) -extern int cmd_test(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); -extern int cmd_lbracket(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_test(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_lbracket(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); #endif #ifndef CONFIG_DISABLE_CLOCK # if defined (CONFIG_RTC) && !defined(CONFIG_NSH_DISABLE_DATE) -extern int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_date(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif #if CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_CAT - extern int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_cat(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_CP - extern int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_cp(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_DD - extern int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_dd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_LS - extern int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ls(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # if CONFIG_NFILE_STREAMS > 0 && !defined(CONFIG_NSH_DISABLESCRIPT) # ifndef CONFIG_NSH_DISABLE_SH - extern int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_sh(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # endif /* CONFIG_NFILE_STREAMS && !CONFIG_NSH_DISABLESCRIPT */ # ifndef CONFIG_DISABLE_MOUNTPOINT # ifndef CONFIG_NSH_DISABLE_LOSETUP - extern int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_losetup(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_MKFIFO - extern int cmd_mkfifo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mkfifo(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifdef CONFIG_FS_READABLE # ifndef CONFIG_NSH_DISABLE_MOUNT - extern int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_UMOUNT - extern int cmd_umount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_umount(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifdef CONFIG_FS_WRITABLE # ifndef CONFIG_NSH_DISABLE_MKDIR - extern int cmd_mkdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mkdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_MKRD - extern int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mkrd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_RM - extern int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_rm(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_RMDIR - extern int cmd_rmdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_rmdir(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # endif /* CONFIG_FS_WRITABLE */ # endif /* CONFIG_FS_READABLE */ # ifdef CONFIG_FS_FAT # ifndef CONFIG_NSH_DISABLE_MKFATFS - extern int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_mkfatfs(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # endif /* CONFIG_FS_FAT */ # endif /* !CONFIG_DISABLE_MOUNTPOINT */ # if !defined(CONFIG_DISABLE_ENVIRON) # ifndef CONFIG_NSH_DISABLE_CD - extern int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_cd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_PWD - extern int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_pwd(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # endif /* !CONFIG_DISABLE_MOUNTPOINT */ #endif /* CONFIG_NFILE_DESCRIPTORS */ #if defined(CONFIG_NET) # ifndef CONFIG_NSH_DISABLE_IFCONFIG - extern int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ifconfig(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #if defined(CONFIG_NET_UDP) && CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_GET - extern int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_get(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_PUT - extern int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_put(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif #if defined(CONFIG_NET_TCP) && CONFIG_NFILE_DESCRIPTORS > 0 # ifndef CONFIG_NSH_DISABLE_WGET - extern int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_wget(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif #if defined(CONFIG_NET_ICMP) && defined(CONFIG_NET_ICMP_PING) && \ !defined(CONFIG_DISABLE_CLOCK) && !defined(CONFIG_DISABLE_SIGNALS) # ifndef CONFIG_NSH_DISABLE_PING - extern int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_ping(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif #endif #ifndef CONFIG_DISABLE_ENVIRON # ifndef CONFIG_NSH_DISABLE_SET - extern int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_set(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_UNSET - extern int cmd_unset(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_unset(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif /* CONFIG_DISABLE_ENVIRON */ #ifndef CONFIG_DISABLE_SIGNALS # ifndef CONFIG_NSH_DISABLE_KILL - extern int cmd_kill(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_kill(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_SLEEP - extern int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_sleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif # ifndef CONFIG_NSH_DISABLE_USLEEP - extern int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); + int cmd_usleep(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv); # endif #endif /* CONFIG_DISABLE_SIGNALS */ diff --git a/apps/nshlib/nsh_mmcmds.c b/apps/nshlib/nsh_mmcmds.c index 4b6e0bf41..fd37df168 100644 --- a/apps/nshlib/nsh_mmcmds.c +++ b/apps/nshlib/nsh_mmcmds.c @@ -1,8 +1,8 @@ /**************************************************************************** - * apps/nshlib/dbg_mmcmds.c + * apps/nshlib/nsh_mmcmds.c * - * Copyright (C) 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -92,4 +92,4 @@ int cmd_free(FAR struct nsh_vtbl_s *vtbl, int argc, char **argv) return OK; } -#endif +#endif /* !CONFIG_NSH_DISABLE_FREE */ diff --git a/apps/nshlib/nsh_serial.c b/apps/nshlib/nsh_serial.c index ccb25477d..43b5e0f53 100644 --- a/apps/nshlib/nsh_serial.c +++ b/apps/nshlib/nsh_serial.c @@ -1,8 +1,8 @@ /**************************************************************************** * apps/nshlib/nsh_serial.c * - * Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt + * Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -474,6 +474,14 @@ int nsh_consolemain(int argc, char *argv[]) FAR struct serial_s *pstate = nsh_allocstruct(); DEBUGASSERT(pstate); + /* If we are using a USB console, then we will have to wait for the USB to + * be connected/ + */ + +#ifdef HAVE_USB_CONSOLE + DEBUGASSERT(nsh_usbconsole() == OK); +#endif + /* Present a greeting */ fputs(g_nshgreeting, pstate->ss_outstream); diff --git a/apps/nshlib/nsh_usbdev.c b/apps/nshlib/nsh_usbdev.c new file mode 100644 index 000000000..bb17244c1 --- /dev/null +++ b/apps/nshlib/nsh_usbdev.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * apps/nshlib/nsh_usbdev.c + * + * Copyright (C) 2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include + +#include "nsh.h" + +#ifdef CONFIG_USBDEV + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_usbconsole + ****************************************************************************/ + +#ifdef HAVE_USB_CONSOLE +int nsh_usbconsole(void) +{ + int errval; + int fd; + + /* 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. + */ + + /* 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; +} + +#endif /* HAVE_USB_CONSOLE */ +#endif /* CONFIG_USBDEV */ \ No newline at end of file -- cgit v1.2.3