From c21d640eaf6df47e7599990d1898dfdcf97feae0 Mon Sep 17 00:00:00 2001 From: Alan Carvalho de Assis Date: Thu, 29 Nov 2018 18:41:42 -0600 Subject: [PATCH] apps/nshlib: Add support to use SLCD as NSH Console --- nshlib/Makefile | 4 + nshlib/nsh.h | 13 +++ nshlib/nsh_consolemain.c | 5 +- nshlib/nsh_slcd.c | 166 +++++++++++++++++++++++++++++++++++++++ nshlib/nsh_usbkeyboard.c | 20 ----- 5 files changed, 186 insertions(+), 22 deletions(-) create mode 100644 nshlib/nsh_slcd.c diff --git a/nshlib/Makefile b/nshlib/Makefile index 3b2ada2a2..e5d8f6a20 100644 --- a/nshlib/Makefile +++ b/nshlib/Makefile @@ -126,6 +126,10 @@ ifeq ($(CONFIG_USBHOST),y) CSRCS += nsh_usbkeyboard.c endif +ifeq ($(CONFIG_SLCD),y) +CSRCS += nsh_slcd.c +endif + ifeq ($(CONFIG_NSH_USBDEV_TRACE),y) CSRCS += nsh_usbtrace.c endif diff --git a/nshlib/nsh.h b/nshlib/nsh.h index 6c14adf41..e53a85b0e 100644 --- a/nshlib/nsh.h +++ b/nshlib/nsh.h @@ -291,6 +291,19 @@ # endif #endif /* HAVE_USB_KEYBOARD */ +#undef HAVE_SLCD_CONSOLE + +/* Check if SLCD is configured as alternative console */ + +#if defined(CONFIG_SLCD) && defined(CONFIG_NSH_ALTCONDEV) + +# define HAVE_SLCD_CONSOLE 1 + +# ifndef CONFIG_NSH_CONDEV +# define CONFIG_NSH_CONDEV "/dev/slcd0" +# endif +#endif /* HAVE_SLCD_CONSOLE */ + /* USB trace settings */ #ifndef CONFIG_USBDEV_TRACE diff --git a/nshlib/nsh_consolemain.c b/nshlib/nsh_consolemain.c index 14ca7c83b..1c06e03ce 100644 --- a/nshlib/nsh_consolemain.c +++ b/nshlib/nsh_consolemain.c @@ -47,7 +47,8 @@ #include "nsh.h" #include "nsh_console.h" -#if !defined(HAVE_USB_CONSOLE) && !defined(HAVE_USB_KEYBOARD) +#if !defined(HAVE_USB_CONSOLE) && !defined(HAVE_USB_KEYBOARD) && \ + !defined(HAVE_SLCD_CONSOLE) /**************************************************************************** * Public Functions @@ -117,4 +118,4 @@ int nsh_consolemain(int argc, char *argv[]) return ret; } -#endif /* !HAVE_USB_CONSOLE && !HAVE_USB_KEYBOARD */ +#endif /* !HAVE_USB_CONSOLE && !HAVE_USB_KEYBOARD !HAVE_SLCD_CONSOLE */ diff --git a/nshlib/nsh_slcd.c b/nshlib/nsh_slcd.c new file mode 100644 index 000000000..5a73021fd --- /dev/null +++ b/nshlib/nsh_slcd.c @@ -0,0 +1,166 @@ +/**************************************************************************** + * apps/nshlib/nsh_slcd.c + * + * Copyright (C) 2018 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * Author: Alan Carvalho de Assis + * + * 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 +#include +#include +#include +#include + +#include "nsh.h" +#include "nsh_console.h" + +#if defined(HAVE_SLCD_CONSOLE) + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_clone_console + * + * Description: + * Wait for the USB keyboard device to be ready + * + ****************************************************************************/ + +static int nsh_clone_console(FAR struct console_stdio_s *pstate) +{ + int fd; + + /* Close stdin */ + + (void)close(0); + + /* Open the console */ + + fd = open("/dev/console", O_RDONLY); + if (fd < 0) + { + return -ENODEV; + } + + /* Associate /dev/console as stdin */ + + (void)dup2(fd, 0); + + /* Close the console device that we just opened */ + + if (fd != 0) + { + close(fd); + } + + /* Use /dev/console as console input */ + + pstate->cn_confd = fd; + + /* Create a standard C stream on the console device */ + + pstate->cn_constream = fdopen(pstate->cn_confd, "r+"); + + return OK; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: nsh_consolemain (USB console version) + * + * Description: + * This interfaces maybe to called or started with task_start to start a + * single an NSH instance that operates on stdin and stdout. This + * function does not return. + * + * This function handles generic /dev/console character devices for output + * but uses a special USB keyboard device for input. The USB keyboard + * requires some special operations to handle the cases where the session + * input is lost when the USB keyboard is unplugged and restarted when the + * USB keyboard is plugged in again. + * + * Input Parameters: + * Standard task start-up arguments. These are not used. argc may be + * zero and argv may be NULL. + * + * Returned Values: + * This function does not return nor does it ever exit (unless the user + * executes the NSH exit command). + * + ****************************************************************************/ + +int nsh_consolemain(int argc, char *argv[]) +{ + FAR struct console_stdio_s *pstate = nsh_newconsole(); + int ret; + + DEBUGASSERT(pstate); + + /* Execute the one-time start-up script. Any output will go to /dev/console. */ + +#ifdef CONFIG_NSH_ROMFSETC + (void)nsh_initscript(&pstate->cn_vtbl); +#endif + +#if defined(CONFIG_NSH_ARCHINIT) && defined(CONFIG_BOARDCTL_FINALINIT) + /* Perform architecture-specific final-initialization (if configured) */ + + (void)boardctl(BOARDIOC_FINALINIT, 0); +#endif + + /* Try to associate /dev/console as stdin because otherwise /dev/slcd0 will be it */ + + ret = nsh_clone_console(pstate); + + if (ret < 0) + { + return ret; + } + + /* Execute the session */ + + (void)nsh_session(pstate); +} + +#endif /* HAVE_SLCD_CONSOLE */ diff --git a/nshlib/nsh_usbkeyboard.c b/nshlib/nsh_usbkeyboard.c index ab9a93bc6..4a135d438 100644 --- a/nshlib/nsh_usbkeyboard.c +++ b/nshlib/nsh_usbkeyboard.c @@ -51,26 +51,6 @@ #if defined(HAVE_USB_KEYBOARD) && !defined(HAVE_USB_CONSOLE) -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - /**************************************************************************** * Private Functions ****************************************************************************/