From 236f0c0aa20e817bed9d3625ef3ebb14c44d6fae Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 13 Sep 2012 12:36:32 +0000 Subject: Rename all apps/examples/-/main.c to something unique git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5141 42af7a65-404d-4744-a932-0658087f49c3 --- apps/ChangeLog.txt | 3 + apps/examples/README.txt | 2 +- apps/examples/buttons/Makefile | 2 +- apps/examples/buttons/buttons_main.c | 499 +++++++++++++++++++++++++++++ apps/examples/buttons/main.c | 499 ----------------------------- apps/examples/discover/Makefile | 2 +- apps/examples/discover/discover_main.c | 188 +++++++++++ apps/examples/discover/main.c | 188 ----------- apps/examples/hello/Makefile | 2 +- apps/examples/hello/hello_main.c | 64 ++++ apps/examples/hello/main.c | 64 ---- apps/examples/helloxx/Makefile | 2 +- apps/examples/helloxx/helloxx_main.cxx | 165 ++++++++++ apps/examples/helloxx/main.cxx | 165 ---------- apps/examples/ostest/Makefile | 2 +- apps/examples/ostest/main.c | 522 ------------------------------- apps/examples/ostest/ostest_main.c | 522 +++++++++++++++++++++++++++++++ apps/examples/rgmp/Makefile | 2 +- apps/examples/rgmp/main.c | 66 ---- apps/examples/rgmp/rgmp_main.c | 66 ++++ apps/examples/serloop/Makefile | 2 +- apps/examples/serloop/main.c | 100 ------ apps/examples/serloop/serloop_main.c | 100 ++++++ apps/examples/thttpd/Makefile | 2 +- apps/examples/thttpd/main.c | 267 ---------------- apps/examples/thttpd/thttpd_main.c | 267 ++++++++++++++++ apps/examples/uip/Makefile | 2 +- apps/examples/uip/main.c | 210 ------------- apps/examples/uip/uip_main.c | 210 +++++++++++++ apps/examples/usbserial/Makefile | 2 +- apps/examples/usbserial/main.c | 467 --------------------------- apps/examples/usbserial/usbserial_main.c | 467 +++++++++++++++++++++++++++ apps/netutils/discover/discover.c | 2 +- apps/system/i2c/README.txt | 2 +- 34 files changed, 2564 insertions(+), 2561 deletions(-) create mode 100644 apps/examples/buttons/buttons_main.c delete mode 100644 apps/examples/buttons/main.c create mode 100644 apps/examples/discover/discover_main.c delete mode 100644 apps/examples/discover/main.c create mode 100644 apps/examples/hello/hello_main.c delete mode 100644 apps/examples/hello/main.c create mode 100644 apps/examples/helloxx/helloxx_main.cxx delete mode 100644 apps/examples/helloxx/main.cxx delete mode 100644 apps/examples/ostest/main.c create mode 100644 apps/examples/ostest/ostest_main.c delete mode 100644 apps/examples/rgmp/main.c create mode 100644 apps/examples/rgmp/rgmp_main.c delete mode 100644 apps/examples/serloop/main.c create mode 100644 apps/examples/serloop/serloop_main.c delete mode 100644 apps/examples/thttpd/main.c create mode 100644 apps/examples/thttpd/thttpd_main.c delete mode 100644 apps/examples/uip/main.c create mode 100644 apps/examples/uip/uip_main.c delete mode 100644 apps/examples/usbserial/main.c create mode 100644 apps/examples/usbserial/usbserial_main.c (limited to 'apps') diff --git a/apps/ChangeLog.txt b/apps/ChangeLog.txt index 6df66b95f..b7b71862e 100755 --- a/apps/ChangeLog.txt +++ b/apps/ChangeLog.txt @@ -323,3 +323,6 @@ by Max Holtzberg. * apps/examples/discover: A test example for the UDP network discovery utility (also contribed by Max Holtzberg). + * apps/examples/*/main.c: Too many files called main.c. Each renamed + to something unique so that they will not collide in the archive. + diff --git a/apps/examples/README.txt b/apps/examples/README.txt index 8a7a94c3e..934eded4d 100644 --- a/apps/examples/README.txt +++ b/apps/examples/README.txt @@ -1133,7 +1133,7 @@ examples/rgmp See http://rgmp.sourceforge.net/wiki/index.php/Main_Page for further - At present, the RGMP example folder contains only an empty main.c file. + At present, the RGMP example folder contains only an empty rgmp_main.c file. examples/romfs ^^^^^^^^^^^^^^ diff --git a/apps/examples/buttons/Makefile b/apps/examples/buttons/Makefile index 9c0587199..a9d7bf583 100644 --- a/apps/examples/buttons/Makefile +++ b/apps/examples/buttons/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # Hello, World! Example ASRCS = -CSRCS = main.c +CSRCS = buttons_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/buttons/buttons_main.c b/apps/examples/buttons/buttons_main.c new file mode 100644 index 000000000..a3f6449d4 --- /dev/null +++ b/apps/examples/buttons/buttons_main.c @@ -0,0 +1,499 @@ +/**************************************************************************** + * examples/buttons/buttons_main.c + * + * Copyright (C) 2011 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. + * + ****************************************************************************/ + +/**************************************************************************** + * NOTE: This test exercises internal button driver interfaces. As such, it + * it relies on internal OS interfaces that are not normally available to a + * user-space program. As a result, this example cannot be used if a + * NuttX is built as a protected, supervisor kernel (CONFIG_NUTTX_KERNEL). + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include +#include +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ + +#ifndef CONFIG_ARCH_BUTTONS +# error "CONFIG_ARCH_BUTTONS is not defined in the configuration" +#endif + +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME0 +# define CONFIG_EXAMPLE_BUTTONS_NAME0 "BUTTON0" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME1 +# define CONFIG_EXAMPLE_BUTTONS_NAME1 "BUTTON1" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME2 +# define CONFIG_EXAMPLE_BUTTONS_NAME2 "BUTTON2" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME3 +# define CONFIG_EXAMPLE_BUTTONS_NAME3 "BUTTON3" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME4 +# define CONFIG_EXAMPLE_BUTTONS_NAME4 "BUTTON4" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME5 +# define CONFIG_EXAMPLE_BUTTONS_NAME5 "BUTTON5" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME6 +# define CONFIG_EXAMPLE_BUTTONS_NAME6 "BUTTON6" +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_NAME7 +# define CONFIG_EXAMPLE_BUTTONS_NAME7 "BUTTON7" +#endif + +#define BUTTON_MIN 0 +#define BUTTON_MAX 7 + +#ifndef CONFIG_EXAMPLE_BUTTONS_MIN +# define CONFIG_EXAMPLE_BUTTONS_MIN BUTTON_MIN +#endif +#ifndef CONFIG_EXAMPLE_BUTTONS_MAX +# define CONFIG_EXAMPLE_BUTTONS_MAX BUTTON_MAX +#endif + +#if CONFIG_EXAMPLE_BUTTONS_MIN > CONFIG_EXAMPLE_BUTTONS_MAX +# error "CONFIG_EXAMPLE_BUTTONS_MIN > CONFIG_EXAMPLE_BUTTONS_MAX" +#endif +#if CONFIG_EXAMPLE_BUTTONS_MAX > 7 +# error "CONFIG_EXAMPLE_BUTTONS_MAX > 7" +#endif + +#ifndef CONFIG_EXAMPLE_IRQBUTTONS_MIN +# define CONFIG_EXAMPLE_IRQBUTTONS_MIN CONFIG_EXAMPLE_BUTTONS_MIN +#endif +#ifndef CONFIG_EXAMPLE_IRQBUTTONS_MAX +# define CONFIG_EXAMPLE_IRQBUTTONS_MAX CONFIG_EXAMPLE_BUTTONS_MAX +#endif + +#if CONFIG_EXAMPLE_IRQBUTTONS_MIN > CONFIG_EXAMPLE_IRQBUTTONS_MAX +# error "CONFIG_EXAMPLE_IRQBUTTONS_MIN > CONFIG_EXAMPLE_IRQBUTTONS_MAX" +#endif +#if CONFIG_EXAMPLE_IRQBUTTONS_MAX > 7 +# error "CONFIG_EXAMPLE_IRQBUTTONS_MAX > 7" +#endif + +#ifndef MIN +# define MIN(a,b) (a < b ? a : b) +#endif +#ifndef MAX +# define MAX(a,b) (a > b ? a : b) +#endif + +#define MIN_BUTTON MIN(CONFIG_EXAMPLE_BUTTONS_MIN, CONFIG_EXAMPLE_IRQBUTTONS_MIN) +#define MAX_BUTTON MAX(CONFIG_EXAMPLE_BUTTONS_MAX, CONFIG_EXAMPLE_IRQBUTTONS_MAX) + +#define NUM_BUTTONS (MAX_BUTTON - MIN_BUTTON + 1) +#define BUTTON_INDEX(b) ((b)-MIN_BUTTON) + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +struct button_info_s +{ + FAR const char *name; /* Name for the button */ +#ifdef CONFIG_ARCH_IRQBUTTONS + xcpt_t handler; /* Button interrupt handler */ +#endif +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static void show_buttons(uint8_t oldset, uint8_t newset); + +#ifdef CONFIG_ARCH_IRQBUTTONS +static void button_handler(int id, int irq); + +#if MIN_BUTTON < 1 +static int button0_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 2 && MAX_BUTTON > 0 +static int button1_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 3 && MAX_BUTTON > 1 +static int button2_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 4 && MAX_BUTTON > 2 +static int button3_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 5 && MAX_BUTTON > 3 +static int button4_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 6 && MAX_BUTTON > 4 +static int button5_handler(int irq, FAR void *context); +#endif +#if MIN_BUTTON < 7 && MAX_BUTTON > 5 +static int button6_handler(int irq, FAR void *context); +#endif +#if MAX_BUTTON > 6 +static int button7_handler(int irq, FAR void *context); +#endif +#endif /* CONFIG_ARCH_IRQBUTTONS */ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + + /* Button Names */ + +static const struct button_info_s g_buttoninfo[NUM_BUTTONS] = +{ +#if MIN_BUTTON < 1 + { + CONFIG_EXAMPLE_BUTTONS_NAME0, +#ifdef CONFIG_ARCH_IRQBUTTONS + button0_handler +#endif + }, +#endif +#if MIN_BUTTON < 2 && MAX_BUTTON > 0 + { + CONFIG_EXAMPLE_BUTTONS_NAME1, +#ifdef CONFIG_ARCH_IRQBUTTONS + button1_handler +#endif + }, +#endif +#if MIN_BUTTON < 3 && MAX_BUTTON > 1 + { + CONFIG_EXAMPLE_BUTTONS_NAME2, +#ifdef CONFIG_ARCH_IRQBUTTONS + button2_handler +#endif + }, +#endif +#if MIN_BUTTON < 4 && MAX_BUTTON > 2 + { + CONFIG_EXAMPLE_BUTTONS_NAME3, +#ifdef CONFIG_ARCH_IRQBUTTONS + button3_handler +#endif + }, +#endif +#if MIN_BUTTON < 5 && MAX_BUTTON > 3 + { + CONFIG_EXAMPLE_BUTTONS_NAME4, +#ifdef CONFIG_ARCH_IRQBUTTONS + button4_handler +#endif + }, +#endif +#if MIN_BUTTON < 6 && MAX_BUTTON > 4 + { + CONFIG_EXAMPLE_BUTTONS_NAME5, +#ifdef CONFIG_ARCH_IRQBUTTONS + button5_handler +#endif + }, +#endif +#if MIN_BUTTON < 7 && MAX_BUTTON > 5 + { + CONFIG_EXAMPLE_BUTTONS_NAME6, +#ifdef CONFIG_ARCH_IRQBUTTONS + button6_handler +#endif + }, +#endif +#if MAX_BUTTON > 6 + { + CONFIG_EXAMPLE_BUTTONS_NAME7, +#ifdef CONFIG_ARCH_IRQBUTTONS + button7_handler +#endif + } +#endif +}; + +/* Last sampled button set */ + +static uint8_t g_oldset; + +/* Used to limit the number of button presses */ + +#ifdef CONFIG_NSH_BUILTIN_APPS +static volatile long g_nbuttons; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static void show_buttons(uint8_t oldset, uint8_t newset) +{ + uint8_t chgset = oldset ^ newset; + int i; + + /* Update the count of button presses shown */ + +#ifdef CONFIG_NSH_BUILTIN_APPS + if ((chgset & newset) != 0) + { + g_nbuttons++; + } +#endif + + /* Show each button state change */ + + for (i = MIN_BUTTON; i <= MAX_BUTTON; i++) + { + uint8_t mask = (1 << i); + if ((chgset & mask) != 0) + { + FAR const char *state; + + /* Get the button state */ + + if ((newset & mask) != 0) + { + state = "depressed"; + } + else + { + state = "released"; + } + + /* Use lib_lowprintf() because we make be executing from an + * interrupt handler. + */ + + lib_lowprintf(" %s %s\n", g_buttoninfo[BUTTON_INDEX(i)].name, state); + } + } +} + +#ifdef CONFIG_ARCH_IRQBUTTONS +static void button_handler(int id, int irq) +{ + uint8_t newset = up_buttons(); + + lib_lowprintf("IRQ:%d Button %d:%s SET:%02x:\n", + irq, id, g_buttoninfo[BUTTON_INDEX(id)].name, newset); + show_buttons(g_oldset, newset); + g_oldset = newset; +} + +#if MIN_BUTTON < 1 +static int button0_handler(int irq, FAR void *context) +{ + button_handler(0, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 2 && MAX_BUTTON > 0 +static int button1_handler(int irq, FAR void *context) +{ + button_handler(1, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 3 && MAX_BUTTON > 1 +static int button2_handler(int irq, FAR void *context) +{ + button_handler(2, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 4 && MAX_BUTTON > 2 +static int button3_handler(int irq, FAR void *context) +{ + button_handler(3, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 5 && MAX_BUTTON > 3 +static int button4_handler(int irq, FAR void *context) +{ + button_handler(4, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 6 && MAX_BUTTON > 4 +static int button5_handler(int irq, FAR void *context) +{ + button_handler(5, irq); + return OK; +} +#endif + +#if MIN_BUTTON < 7 && MAX_BUTTON > 5 +static int button6_handler(int irq, FAR void *context) +{ + button_handler(6, irq); + return OK; +} +#endif + +#if MAX_BUTTON > 6 +static int button7_handler(int irq, FAR void *context) +{ + button_handler(7, irq); + return OK; +} +#endif +#endif /* CONFIG_ARCH_IRQBUTTONS */ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * buttons_main + ****************************************************************************/ + +int buttons_main(int argc, char *argv[]) +{ + uint8_t newset; + irqstate_t flags; + int i; + + /* If this example is configured as an NX add-on, then limit the number of + * samples that we collect before returning. Otherwise, we never return + */ + +#ifdef CONFIG_NSH_BUILTIN_APPS + long maxbuttons = 1; + g_nbuttons = 0; + if (argc > 1) + { + maxbuttons = strtol(argv[1], NULL, 10); + } + lib_lowprintf("maxbuttons: %d\n", maxbuttons); +#endif + + /* Initialize the button GPIOs */ + + up_buttoninit(); + + /* Register to recieve button interrupts */ + +#ifdef CONFIG_ARCH_IRQBUTTONS + for (i = CONFIG_EXAMPLE_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLE_IRQBUTTONS_MAX; i++) + { + xcpt_t oldhandler = up_irqbutton(i, g_buttoninfo[BUTTON_INDEX(i)].handler); + + /* Use lib_lowprintf() for compatibility with interrrupt handler output. */ + + lib_lowprintf("Attached handler at %p to button %d [%s], oldhandler:%p\n", + g_buttoninfo[BUTTON_INDEX(i)].handler, i, + g_buttoninfo[BUTTON_INDEX(i)].name, oldhandler); + + /* Some hardware multiplexes different GPIO button sources to the same + * physical interrupt. If we register multiple such multiplexed button + * interrupts, then the second registration will overwrite the first. In + * this case, the first button interrupts may be aliased to the second + * interrupt handler (or worse, could be lost). + */ + + if (oldhandler != NULL) + { + lib_lowprintf("WARNING: oldhandler:%p is not NULL! " + "Button events may be lost or aliased!\n", + oldhandler); + } + } +#endif + + /* Poll button state */ + + g_oldset = up_buttons(); +#ifdef CONFIG_NSH_BUILTIN_APPS + while (g_nbuttons < maxbuttons) +#else + for (;;) +#endif + { + /* Get the set of pressed and release buttons. */ + + newset = up_buttons(); + + /* Any changes from the last sample? */ + + if (newset != g_oldset) + { + /* Disable interrupts so that output here will not collide with + * output from an interrupt handler. + */ + + flags = irqsave(); + + /* Use lib_lowprintf() for compatibility with interrrupt handler + * output. + */ + + lib_lowprintf("POLL SET:%02x:\n", newset); + show_buttons(g_oldset, newset); + g_oldset = newset; + irqrestore(flags); + } + + /* Sleep a little... but not long. This will determine how fast we + * poll for button changes. + */ + + usleep(150000); /* 150 Milliseconds */ + } + + /* Un-register button handlers */ + +#if defined(CONFIG_ARCH_IRQBUTTONS) && defined(CONFIG_NSH_BUILTIN_APPS) + for (i = CONFIG_EXAMPLE_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLE_IRQBUTTONS_MAX; i++) + { + (void)up_irqbutton(i, NULL); + } +#endif + + return 0; +} + diff --git a/apps/examples/buttons/main.c b/apps/examples/buttons/main.c deleted file mode 100644 index 50124512c..000000000 --- a/apps/examples/buttons/main.c +++ /dev/null @@ -1,499 +0,0 @@ -/**************************************************************************** - * examples/buttons/main.c - * - * Copyright (C) 2011 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. - * - ****************************************************************************/ - -/**************************************************************************** - * NOTE: This test exercises internal button driver interfaces. As such, it - * it relies on internal OS interfaces that are not normally available to a - * user-space program. As a result, this example cannot be used if a - * NuttX is built as a protected, supervisor kernel (CONFIG_NUTTX_KERNEL). - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include -#include - -#include -#include -#include -#include - -/**************************************************************************** - * Definitions - ****************************************************************************/ -/* Configuration ************************************************************/ - -#ifndef CONFIG_ARCH_BUTTONS -# error "CONFIG_ARCH_BUTTONS is not defined in the configuration" -#endif - -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME0 -# define CONFIG_EXAMPLE_BUTTONS_NAME0 "BUTTON0" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME1 -# define CONFIG_EXAMPLE_BUTTONS_NAME1 "BUTTON1" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME2 -# define CONFIG_EXAMPLE_BUTTONS_NAME2 "BUTTON2" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME3 -# define CONFIG_EXAMPLE_BUTTONS_NAME3 "BUTTON3" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME4 -# define CONFIG_EXAMPLE_BUTTONS_NAME4 "BUTTON4" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME5 -# define CONFIG_EXAMPLE_BUTTONS_NAME5 "BUTTON5" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME6 -# define CONFIG_EXAMPLE_BUTTONS_NAME6 "BUTTON6" -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_NAME7 -# define CONFIG_EXAMPLE_BUTTONS_NAME7 "BUTTON7" -#endif - -#define BUTTON_MIN 0 -#define BUTTON_MAX 7 - -#ifndef CONFIG_EXAMPLE_BUTTONS_MIN -# define CONFIG_EXAMPLE_BUTTONS_MIN BUTTON_MIN -#endif -#ifndef CONFIG_EXAMPLE_BUTTONS_MAX -# define CONFIG_EXAMPLE_BUTTONS_MAX BUTTON_MAX -#endif - -#if CONFIG_EXAMPLE_BUTTONS_MIN > CONFIG_EXAMPLE_BUTTONS_MAX -# error "CONFIG_EXAMPLE_BUTTONS_MIN > CONFIG_EXAMPLE_BUTTONS_MAX" -#endif -#if CONFIG_EXAMPLE_BUTTONS_MAX > 7 -# error "CONFIG_EXAMPLE_BUTTONS_MAX > 7" -#endif - -#ifndef CONFIG_EXAMPLE_IRQBUTTONS_MIN -# define CONFIG_EXAMPLE_IRQBUTTONS_MIN CONFIG_EXAMPLE_BUTTONS_MIN -#endif -#ifndef CONFIG_EXAMPLE_IRQBUTTONS_MAX -# define CONFIG_EXAMPLE_IRQBUTTONS_MAX CONFIG_EXAMPLE_BUTTONS_MAX -#endif - -#if CONFIG_EXAMPLE_IRQBUTTONS_MIN > CONFIG_EXAMPLE_IRQBUTTONS_MAX -# error "CONFIG_EXAMPLE_IRQBUTTONS_MIN > CONFIG_EXAMPLE_IRQBUTTONS_MAX" -#endif -#if CONFIG_EXAMPLE_IRQBUTTONS_MAX > 7 -# error "CONFIG_EXAMPLE_IRQBUTTONS_MAX > 7" -#endif - -#ifndef MIN -# define MIN(a,b) (a < b ? a : b) -#endif -#ifndef MAX -# define MAX(a,b) (a > b ? a : b) -#endif - -#define MIN_BUTTON MIN(CONFIG_EXAMPLE_BUTTONS_MIN, CONFIG_EXAMPLE_IRQBUTTONS_MIN) -#define MAX_BUTTON MAX(CONFIG_EXAMPLE_BUTTONS_MAX, CONFIG_EXAMPLE_IRQBUTTONS_MAX) - -#define NUM_BUTTONS (MAX_BUTTON - MIN_BUTTON + 1) -#define BUTTON_INDEX(b) ((b)-MIN_BUTTON) - -/**************************************************************************** - * Private Types - ****************************************************************************/ - -struct button_info_s -{ - FAR const char *name; /* Name for the button */ -#ifdef CONFIG_ARCH_IRQBUTTONS - xcpt_t handler; /* Button interrupt handler */ -#endif -}; - -/**************************************************************************** - * Private Function Prototypes - ****************************************************************************/ - -static void show_buttons(uint8_t oldset, uint8_t newset); - -#ifdef CONFIG_ARCH_IRQBUTTONS -static void button_handler(int id, int irq); - -#if MIN_BUTTON < 1 -static int button0_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 2 && MAX_BUTTON > 0 -static int button1_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 3 && MAX_BUTTON > 1 -static int button2_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 4 && MAX_BUTTON > 2 -static int button3_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 5 && MAX_BUTTON > 3 -static int button4_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 6 && MAX_BUTTON > 4 -static int button5_handler(int irq, FAR void *context); -#endif -#if MIN_BUTTON < 7 && MAX_BUTTON > 5 -static int button6_handler(int irq, FAR void *context); -#endif -#if MAX_BUTTON > 6 -static int button7_handler(int irq, FAR void *context); -#endif -#endif /* CONFIG_ARCH_IRQBUTTONS */ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - - /* Button Names */ - -static const struct button_info_s g_buttoninfo[NUM_BUTTONS] = -{ -#if MIN_BUTTON < 1 - { - CONFIG_EXAMPLE_BUTTONS_NAME0, -#ifdef CONFIG_ARCH_IRQBUTTONS - button0_handler -#endif - }, -#endif -#if MIN_BUTTON < 2 && MAX_BUTTON > 0 - { - CONFIG_EXAMPLE_BUTTONS_NAME1, -#ifdef CONFIG_ARCH_IRQBUTTONS - button1_handler -#endif - }, -#endif -#if MIN_BUTTON < 3 && MAX_BUTTON > 1 - { - CONFIG_EXAMPLE_BUTTONS_NAME2, -#ifdef CONFIG_ARCH_IRQBUTTONS - button2_handler -#endif - }, -#endif -#if MIN_BUTTON < 4 && MAX_BUTTON > 2 - { - CONFIG_EXAMPLE_BUTTONS_NAME3, -#ifdef CONFIG_ARCH_IRQBUTTONS - button3_handler -#endif - }, -#endif -#if MIN_BUTTON < 5 && MAX_BUTTON > 3 - { - CONFIG_EXAMPLE_BUTTONS_NAME4, -#ifdef CONFIG_ARCH_IRQBUTTONS - button4_handler -#endif - }, -#endif -#if MIN_BUTTON < 6 && MAX_BUTTON > 4 - { - CONFIG_EXAMPLE_BUTTONS_NAME5, -#ifdef CONFIG_ARCH_IRQBUTTONS - button5_handler -#endif - }, -#endif -#if MIN_BUTTON < 7 && MAX_BUTTON > 5 - { - CONFIG_EXAMPLE_BUTTONS_NAME6, -#ifdef CONFIG_ARCH_IRQBUTTONS - button6_handler -#endif - }, -#endif -#if MAX_BUTTON > 6 - { - CONFIG_EXAMPLE_BUTTONS_NAME7, -#ifdef CONFIG_ARCH_IRQBUTTONS - button7_handler -#endif - } -#endif -}; - -/* Last sampled button set */ - -static uint8_t g_oldset; - -/* Used to limit the number of button presses */ - -#ifdef CONFIG_NSH_BUILTIN_APPS -static volatile long g_nbuttons; -#endif - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -static void show_buttons(uint8_t oldset, uint8_t newset) -{ - uint8_t chgset = oldset ^ newset; - int i; - - /* Update the count of button presses shown */ - -#ifdef CONFIG_NSH_BUILTIN_APPS - if ((chgset & newset) != 0) - { - g_nbuttons++; - } -#endif - - /* Show each button state change */ - - for (i = MIN_BUTTON; i <= MAX_BUTTON; i++) - { - uint8_t mask = (1 << i); - if ((chgset & mask) != 0) - { - FAR const char *state; - - /* Get the button state */ - - if ((newset & mask) != 0) - { - state = "depressed"; - } - else - { - state = "released"; - } - - /* Use lib_lowprintf() because we make be executing from an - * interrupt handler. - */ - - lib_lowprintf(" %s %s\n", g_buttoninfo[BUTTON_INDEX(i)].name, state); - } - } -} - -#ifdef CONFIG_ARCH_IRQBUTTONS -static void button_handler(int id, int irq) -{ - uint8_t newset = up_buttons(); - - lib_lowprintf("IRQ:%d Button %d:%s SET:%02x:\n", - irq, id, g_buttoninfo[BUTTON_INDEX(id)].name, newset); - show_buttons(g_oldset, newset); - g_oldset = newset; -} - -#if MIN_BUTTON < 1 -static int button0_handler(int irq, FAR void *context) -{ - button_handler(0, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 2 && MAX_BUTTON > 0 -static int button1_handler(int irq, FAR void *context) -{ - button_handler(1, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 3 && MAX_BUTTON > 1 -static int button2_handler(int irq, FAR void *context) -{ - button_handler(2, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 4 && MAX_BUTTON > 2 -static int button3_handler(int irq, FAR void *context) -{ - button_handler(3, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 5 && MAX_BUTTON > 3 -static int button4_handler(int irq, FAR void *context) -{ - button_handler(4, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 6 && MAX_BUTTON > 4 -static int button5_handler(int irq, FAR void *context) -{ - button_handler(5, irq); - return OK; -} -#endif - -#if MIN_BUTTON < 7 && MAX_BUTTON > 5 -static int button6_handler(int irq, FAR void *context) -{ - button_handler(6, irq); - return OK; -} -#endif - -#if MAX_BUTTON > 6 -static int button7_handler(int irq, FAR void *context) -{ - button_handler(7, irq); - return OK; -} -#endif -#endif /* CONFIG_ARCH_IRQBUTTONS */ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * buttons_main - ****************************************************************************/ - -int buttons_main(int argc, char *argv[]) -{ - uint8_t newset; - irqstate_t flags; - int i; - - /* If this example is configured as an NX add-on, then limit the number of - * samples that we collect before returning. Otherwise, we never return - */ - -#ifdef CONFIG_NSH_BUILTIN_APPS - long maxbuttons = 1; - g_nbuttons = 0; - if (argc > 1) - { - maxbuttons = strtol(argv[1], NULL, 10); - } - lib_lowprintf("maxbuttons: %d\n", maxbuttons); -#endif - - /* Initialize the button GPIOs */ - - up_buttoninit(); - - /* Register to recieve button interrupts */ - -#ifdef CONFIG_ARCH_IRQBUTTONS - for (i = CONFIG_EXAMPLE_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLE_IRQBUTTONS_MAX; i++) - { - xcpt_t oldhandler = up_irqbutton(i, g_buttoninfo[BUTTON_INDEX(i)].handler); - - /* Use lib_lowprintf() for compatibility with interrrupt handler output. */ - - lib_lowprintf("Attached handler at %p to button %d [%s], oldhandler:%p\n", - g_buttoninfo[BUTTON_INDEX(i)].handler, i, - g_buttoninfo[BUTTON_INDEX(i)].name, oldhandler); - - /* Some hardware multiplexes different GPIO button sources to the same - * physical interrupt. If we register multiple such multiplexed button - * interrupts, then the second registration will overwrite the first. In - * this case, the first button interrupts may be aliased to the second - * interrupt handler (or worse, could be lost). - */ - - if (oldhandler != NULL) - { - lib_lowprintf("WARNING: oldhandler:%p is not NULL! " - "Button events may be lost or aliased!\n", - oldhandler); - } - } -#endif - - /* Poll button state */ - - g_oldset = up_buttons(); -#ifdef CONFIG_NSH_BUILTIN_APPS - while (g_nbuttons < maxbuttons) -#else - for (;;) -#endif - { - /* Get the set of pressed and release buttons. */ - - newset = up_buttons(); - - /* Any changes from the last sample? */ - - if (newset != g_oldset) - { - /* Disable interrupts so that output here will not collide with - * output from an interrupt handler. - */ - - flags = irqsave(); - - /* Use lib_lowprintf() for compatibility with interrrupt handler - * output. - */ - - lib_lowprintf("POLL SET:%02x:\n", newset); - show_buttons(g_oldset, newset); - g_oldset = newset; - irqrestore(flags); - } - - /* Sleep a little... but not long. This will determine how fast we - * poll for button changes. - */ - - usleep(150000); /* 150 Milliseconds */ - } - - /* Un-register button handlers */ - -#if defined(CONFIG_ARCH_IRQBUTTONS) && defined(CONFIG_NSH_BUILTIN_APPS) - for (i = CONFIG_EXAMPLE_IRQBUTTONS_MIN; i <= CONFIG_EXAMPLE_IRQBUTTONS_MAX; i++) - { - (void)up_irqbutton(i, NULL); - } -#endif - - return 0; -} - diff --git a/apps/examples/discover/Makefile b/apps/examples/discover/Makefile index f42f310a1..3bb6b939e 100644 --- a/apps/examples/discover/Makefile +++ b/apps/examples/discover/Makefile @@ -47,7 +47,7 @@ PRIORITY = SCHED_PRIORITY_DEFAULT STACKSIZE = 2048 ASRCS = -CSRCS = main.c +CSRCS = discover_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/discover/discover_main.c b/apps/examples/discover/discover_main.c new file mode 100644 index 000000000..c3acb56df --- /dev/null +++ b/apps/examples/discover/discover_main.c @@ -0,0 +1,188 @@ +/**************************************************************************** + * examples/discover/discover_main.c + * + * Copyright (C) 2012 Max Holtzberg. All rights reserved. + * Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved. + * + * Authors: Max Holtzberg + * 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 +#include +#include +#include + +#include +#include +#include + +#include +#include + +#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC +# include +#endif + +/* Here we include the header file for the application(s) we use in + * our project as defined in the config//defconfig file + */ + +/* DHCPC may be used in conjunction with any other feature (or not) */ + +#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC +# include +# include +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * discover_main + ****************************************************************************/ + +int discover_main(int argc, char *argv[]) +{ + /* If this task is excecutated as an NSH built-in function, then the + * network has already been configured by NSH's start-up logic. + */ + +#ifndef CONFIG_NSH_BUILTIN_APPS + struct in_addr addr; +#if defined(CONFIG_EXAMPLE_DISCOVER_DHCPC) || defined(CONFIG_EXAMPLE_DISCOVER_NOMAC) + uint8_t mac[IFHWADDRLEN]; +#endif +#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC + void *handle; +#endif + +/* Many embedded network interfaces must have a software assigned MAC */ + +#ifdef CONFIG_EXAMPLE_DISCOVER_NOMAC + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xde; + mac[3] = 0xad; + mac[4] = 0xbe; + mac[5] = 0xef; + uip_setmacaddr("eth0", mac); +#endif + + /* Set up our host address */ + +#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC + addr.s_addr = 0; +#else + addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_IPADDR); +#endif + uip_sethostaddr("eth0", &addr); + + /* Set up the default router address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_DRIPADDR); + uip_setdraddr("eth0", &addr); + + /* Setup the subnet mask */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_NETMASK); + uip_setnetmask("eth0", &addr); + +#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC + /* Set up the resolver */ + + resolv_init(); + + /* Get the MAC address of the NIC */ + + uip_getmacaddr("eth0", mac); + + /* Set up the DHCPC modules */ + + handle = dhcpc_open(&mac, IFHWADDRLEN); + + /* Get an IP address. Note: there is no logic here for renewing the address in this + * example. The address should be renewed in ds.lease_time/2 seconds. + */ + + printf("Getting IP address\n"); + if (handle) + { + struct dhcpc_state ds; + (void)dhcpc_request(handle, &ds); + uip_sethostaddr("eth1", &ds.ipaddr); + + if (ds.netmask.s_addr != 0) + { + uip_setnetmask("eth0", &ds.netmask); + } + + if (ds.default_router.s_addr != 0) + { + uip_setdraddr("eth0", &ds.default_router); + } + + if (ds.dnsaddr.s_addr != 0) + { + resolv_conf(&ds.dnsaddr); + } + + dhcpc_close(handle); + printf("IP: %s\n", inet_ntoa(ds.ipaddr)); + } + +#endif /* CONFIG_EXAMPLE_DISCOVER_DHCPC */ +#endif /* CONFIG_NSH_BUILTIN_APPS */ + + if (discover_start() < 0) + { + ndbg("Could not start discover daemon.\n"); + return ERROR; + } + + return OK; +} + diff --git a/apps/examples/discover/main.c b/apps/examples/discover/main.c deleted file mode 100644 index d076deb11..000000000 --- a/apps/examples/discover/main.c +++ /dev/null @@ -1,188 +0,0 @@ -/**************************************************************************** - * examples/discover/main.c - * - * Copyright (C) 2012 Max Holtzberg. All rights reserved. - * Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved. - * - * Authors: Max Holtzberg - * 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 -#include -#include -#include - -#include -#include -#include - -#include -#include - -#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC -# include -#endif - -/* Here we include the header file for the application(s) we use in - * our project as defined in the config//defconfig file - */ - -/* DHCPC may be used in conjunction with any other feature (or not) */ - -#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC -# include -# include -#endif - -/**************************************************************************** - * Pre-processor Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * discover_main - ****************************************************************************/ - -int discover_main(int argc, char *argv[]) -{ - /* If this task is excecutated as an NSH built-in function, then the - * network has already been configured by NSH's start-up logic. - */ - -#ifndef CONFIG_NSH_BUILTIN_APPS - struct in_addr addr; -#if defined(CONFIG_EXAMPLE_DISCOVER_DHCPC) || defined(CONFIG_EXAMPLE_DISCOVER_NOMAC) - uint8_t mac[IFHWADDRLEN]; -#endif -#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC - void *handle; -#endif - -/* Many embedded network interfaces must have a software assigned MAC */ - -#ifdef CONFIG_EXAMPLE_DISCOVER_NOMAC - mac[0] = 0x00; - mac[1] = 0xe0; - mac[2] = 0xde; - mac[3] = 0xad; - mac[4] = 0xbe; - mac[5] = 0xef; - uip_setmacaddr("eth0", mac); -#endif - - /* Set up our host address */ - -#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC - addr.s_addr = 0; -#else - addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_IPADDR); -#endif - uip_sethostaddr("eth0", &addr); - - /* Set up the default router address */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_DRIPADDR); - uip_setdraddr("eth0", &addr); - - /* Setup the subnet mask */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_DISCOVER_NETMASK); - uip_setnetmask("eth0", &addr); - -#ifdef CONFIG_EXAMPLE_DISCOVER_DHCPC - /* Set up the resolver */ - - resolv_init(); - - /* Get the MAC address of the NIC */ - - uip_getmacaddr("eth0", mac); - - /* Set up the DHCPC modules */ - - handle = dhcpc_open(&mac, IFHWADDRLEN); - - /* Get an IP address. Note: there is no logic here for renewing the address in this - * example. The address should be renewed in ds.lease_time/2 seconds. - */ - - printf("Getting IP address\n"); - if (handle) - { - struct dhcpc_state ds; - (void)dhcpc_request(handle, &ds); - uip_sethostaddr("eth1", &ds.ipaddr); - - if (ds.netmask.s_addr != 0) - { - uip_setnetmask("eth0", &ds.netmask); - } - - if (ds.default_router.s_addr != 0) - { - uip_setdraddr("eth0", &ds.default_router); - } - - if (ds.dnsaddr.s_addr != 0) - { - resolv_conf(&ds.dnsaddr); - } - - dhcpc_close(handle); - printf("IP: %s\n", inet_ntoa(ds.ipaddr)); - } - -#endif /* CONFIG_EXAMPLE_DISCOVER_DHCPC */ -#endif /* CONFIG_NSH_BUILTIN_APPS */ - - if (discover_start() < 0) - { - ndbg("Could not start discover daemon.\n"); - return ERROR; - } - - return OK; -} - diff --git a/apps/examples/hello/Makefile b/apps/examples/hello/Makefile index 9c3cda894..1d78d723e 100644 --- a/apps/examples/hello/Makefile +++ b/apps/examples/hello/Makefile @@ -46,7 +46,7 @@ STACKSIZE = 2048 # Hello, World! Example ASRCS = -CSRCS = main.c +CSRCS = hello_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/hello/hello_main.c b/apps/examples/hello/hello_main.c new file mode 100644 index 000000000..229027c36 --- /dev/null +++ b/apps/examples/hello/hello_main.c @@ -0,0 +1,64 @@ +/**************************************************************************** + * examples/hello/hello_main.c + * + * Copyright (C) 2008, 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 + * 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 + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * hello_main + ****************************************************************************/ + +int hello_main(int argc, char *argv[]) +{ + printf("Hello, World!!\n"); + return 0; +} + diff --git a/apps/examples/hello/main.c b/apps/examples/hello/main.c deleted file mode 100644 index 7e07cffd1..000000000 --- a/apps/examples/hello/main.c +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** - * examples/hello/main.c - * - * Copyright (C) 2008, 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 - * 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 - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * hello_main - ****************************************************************************/ - -int hello_main(int argc, char *argv[]) -{ - printf("Hello, World!!\n"); - return 0; -} - diff --git a/apps/examples/helloxx/Makefile b/apps/examples/helloxx/Makefile index c34378d24..c9557d124 100644 --- a/apps/examples/helloxx/Makefile +++ b/apps/examples/helloxx/Makefile @@ -41,7 +41,7 @@ include $(APPDIR)/Make.defs ASRCS = CSRCS = -CXXSRCS = main.cxx +CXXSRCS = helloxx_main.cxx AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/helloxx/helloxx_main.cxx b/apps/examples/helloxx/helloxx_main.cxx new file mode 100644 index 000000000..60fd0487b --- /dev/null +++ b/apps/examples/helloxx/helloxx_main.cxx @@ -0,0 +1,165 @@ +//*************************************************************************** +// examples/helloxx/helloxx_main.cxx +// +// Copyright (C) 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 +// 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 + +//*************************************************************************** +// Definitions +//*************************************************************************** +// Debug ******************************************************************** +// Non-standard debug that may be enabled just for testing the constructors + +#ifndef CONFIG_DEBUG +# undef CONFIG_DEBUG_CXX +#endif + +#ifdef CONFIG_DEBUG_CXX +# define cxxdbg dbg +# define cxxlldbg lldbg +# ifdef CONFIG_DEBUG_VERBOSE +# define cxxvdbg vdbg +# define cxxllvdbg llvdbg +# else +# define cxxvdbg(x...) +# define cxxllvdbg(x...) +# endif +#else +# define cxxdbg(x...) +# define cxxlldbg(x...) +# define cxxvdbg(x...) +# define cxxllvdbg(x...) +#endif + +//*************************************************************************** +// Private Classes +//*************************************************************************** + +class CHelloWorld +{ + public: + CHelloWorld(void) : mSecret(42) + { + cxxdbg("Constructor: mSecret=%d\n", mSecret); + } + + ~CHelloWorld(void) + { + cxxdbg("Destructor\n"); + } + + bool HelloWorld(void) + { + cxxdbg("HelloWorld: mSecret=%d\n", mSecret); + + if (mSecret != 42) + { + printf("CHelloWorld::HelloWorld: CONSTRUCTION FAILED!\n"); + return false; + } + else + { + printf("CHelloWorld::HelloWorld: Hello, World!!\n"); + return true; + } + } + + private: + int mSecret; +}; + +//*************************************************************************** +// Private Data +//*************************************************************************** + +// Define a statically constructed CHellowWorld instance if C++ static +// initializers are supported by the platform + +#ifdef CONFIG_HAVE_CXXINITIALIZE +static CHelloWorld g_HelloWorld; +#endif + +//*************************************************************************** +// Public Functions +//*************************************************************************** + +/**************************************************************************** + * Name: helloxx_main + ****************************************************************************/ + +int helloxx_main(int argc, char *argv[]) +{ + // If C++ initialization for static constructors is supported, then do + // that first + +#ifdef CONFIG_HAVE_CXXINITIALIZE + up_cxxinitialize(); +#endif + + // Exercise an explictly instantiated C++ object + + CHelloWorld *pHelloWorld = new CHelloWorld; + printf("helloxx_main: Saying hello from the dynamically constructed instance\n"); + pHelloWorld->HelloWorld(); + + // Exercise an C++ object instantiated on the stack + +#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST + CHelloWorld HelloWorld; + + printf("helloxx_main: Saying hello from the instance constructed on the stack\n"); + HelloWorld.HelloWorld(); +#endif + + // Exercise an statically constructed C++ object + +#ifdef CONFIG_HAVE_CXXINITIALIZE + printf("helloxx_main: Saying hello from the statically constructed instance\n"); + g_HelloWorld.HelloWorld(); +#endif + + delete pHelloWorld; + return 0; +} + diff --git a/apps/examples/helloxx/main.cxx b/apps/examples/helloxx/main.cxx deleted file mode 100644 index fcec7c761..000000000 --- a/apps/examples/helloxx/main.cxx +++ /dev/null @@ -1,165 +0,0 @@ -//*************************************************************************** -// examples/helloxx/main.cxx -// -// Copyright (C) 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 -// 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 - -//*************************************************************************** -// Definitions -//*************************************************************************** -// Debug ******************************************************************** -// Non-standard debug that may be enabled just for testing the constructors - -#ifndef CONFIG_DEBUG -# undef CONFIG_DEBUG_CXX -#endif - -#ifdef CONFIG_DEBUG_CXX -# define cxxdbg dbg -# define cxxlldbg lldbg -# ifdef CONFIG_DEBUG_VERBOSE -# define cxxvdbg vdbg -# define cxxllvdbg llvdbg -# else -# define cxxvdbg(x...) -# define cxxllvdbg(x...) -# endif -#else -# define cxxdbg(x...) -# define cxxlldbg(x...) -# define cxxvdbg(x...) -# define cxxllvdbg(x...) -#endif - -//*************************************************************************** -// Private Classes -//*************************************************************************** - -class CHelloWorld -{ - public: - CHelloWorld(void) : mSecret(42) - { - cxxdbg("Constructor: mSecret=%d\n", mSecret); - } - - ~CHelloWorld(void) - { - cxxdbg("Destructor\n"); - } - - bool HelloWorld(void) - { - cxxdbg("HelloWorld: mSecret=%d\n", mSecret); - - if (mSecret != 42) - { - printf("CHelloWorld::HelloWorld: CONSTRUCTION FAILED!\n"); - return false; - } - else - { - printf("CHelloWorld::HelloWorld: Hello, World!!\n"); - return true; - } - } - - private: - int mSecret; -}; - -//*************************************************************************** -// Private Data -//*************************************************************************** - -// Define a statically constructed CHellowWorld instance if C++ static -// initializers are supported by the platform - -#ifdef CONFIG_HAVE_CXXINITIALIZE -static CHelloWorld g_HelloWorld; -#endif - -//*************************************************************************** -// Public Functions -//*************************************************************************** - -/**************************************************************************** - * Name: helloxx_main - ****************************************************************************/ - -int helloxx_main(int argc, char *argv[]) -{ - // If C++ initialization for static constructors is supported, then do - // that first - -#ifdef CONFIG_HAVE_CXXINITIALIZE - up_cxxinitialize(); -#endif - - // Exercise an explictly instantiated C++ object - - CHelloWorld *pHelloWorld = new CHelloWorld; - printf("helloxx_main: Saying hello from the dynamically constructed instance\n"); - pHelloWorld->HelloWorld(); - - // Exercise an C++ object instantiated on the stack - -#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST - CHelloWorld HelloWorld; - - printf("helloxx_main: Saying hello from the instance constructed on the stack\n"); - HelloWorld.HelloWorld(); -#endif - - // Exercise an statically constructed C++ object - -#ifdef CONFIG_HAVE_CXXINITIALIZE - printf("helloxx_main: Saying hello from the statically constructed instance\n"); - g_HelloWorld.HelloWorld(); -#endif - - delete pHelloWorld; - return 0; -} - diff --git a/apps/examples/ostest/Makefile b/apps/examples/ostest/Makefile index eab1db8b3..374964b39 100644 --- a/apps/examples/ostest/Makefile +++ b/apps/examples/ostest/Makefile @@ -46,7 +46,7 @@ STACKSIZE = 2048 # NuttX OS Test ASRCS = -CSRCS = main.c dev_null.c +CSRCS = ostest_main.c dev_null.c ifeq ($(CONFIG_ARCH_FPU),y) CSRCS += fpu.c diff --git a/apps/examples/ostest/main.c b/apps/examples/ostest/main.c deleted file mode 100644 index 61a2af19a..000000000 --- a/apps/examples/ostest/main.c +++ /dev/null @@ -1,522 +0,0 @@ -/**************************************************************************** - * apps/examples/ostest/main.c - * - * 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 - * 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 - -#include "ostest.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -#define PRIORITY 100 -#define NARGS 4 -#define HALF_SECOND_USEC 500000L - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -static const char arg1[] = "Arg1"; -static const char arg2[] = "Arg2"; -static const char arg3[] = "Arg3"; -static const char arg4[] = "Arg4"; - -#if CONFIG_NFILE_DESCRIPTORS > 0 -static const char write_data1[] = "stdio_test: write fd=1\n"; -static const char write_data2[] = "stdio_test: write fd=2\n"; -#endif - -#ifdef SDCC -/* I am not yet certain why SDCC does not like the following - * initializer. It involves some issues with 2- vs 3-byte - * pointer types. - */ - -static const char *g_argv[NARGS+1]; -#else -static const char *g_argv[NARGS+1] = { arg1, arg2, arg3, arg4, NULL }; -#endif - -#ifndef CONFIG_DISABLE_SIGNALS -static struct mallinfo g_mmbefore; -static struct mallinfo g_mmprevious; -static struct mallinfo g_mmafter; -#endif - -#ifndef CONFIG_DISABLE_ENVIRON -const char g_var1_name[] = "Variable1"; -const char g_var1_value[] = "GoodValue1"; -const char g_var2_name[] = "Variable2"; -const char g_var2_value[] = "GoodValue2"; -const char g_var3_name[] = "Variable3"; -const char g_var3_value[] = "GoodValue3"; - -const char g_bad_value1[] = "BadValue1"; -const char g_bad_value2[] = "BadValue2"; - -const char g_putenv_value[] = "Variable1=BadValue3"; - -#endif - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: show_memory_usage - ****************************************************************************/ - -#ifndef CONFIG_DISABLE_SIGNALS -static void show_memory_usage(struct mallinfo *mmbefore, - struct mallinfo *mmafter) -{ - printf("VARIABLE BEFORE AFTER\n"); - printf("======== ======== ========\n"); - printf("arena %8x %8x\n", mmbefore->arena, mmafter->arena); - printf("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks); - printf("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk); - printf("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks); - printf("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks); -} -#else -# define show_memory_usage(mm1, mm2) -#endif - -/**************************************************************************** - * Name: check_test_memory_usage - ****************************************************************************/ - -#ifndef CONFIG_DISABLE_SIGNALS -static void check_test_memory_usage(void) -{ - /* Wait a little bit to let any threads terminate */ - - usleep(HALF_SECOND_USEC); - - /* Get the current memory usage */ - -#ifdef CONFIG_CAN_PASS_STRUCTS - g_mmafter = mallinfo(); -#else - (void)mallinfo(&g_mmafter); -#endif - - /* Show the change from the previous time */ - - printf("\nEnd of test memory usage:\n"); - show_memory_usage(&g_mmprevious, &g_mmafter); - - /* Set up for the next test */ - -#ifdef CONFIG_CAN_PASS_STRUCTS - g_mmprevious = g_mmafter; -#else - memcpy(&g_mmprevious, &g_mmafter, sizeof(struct mallinfo)); -#endif - - /* If so enabled, show the use of priority inheritance resources */ - - dump_nfreeholders("user_main:"); -} -#else -# define check_test_memory_usage() -#endif - -/**************************************************************************** - * Name: show_variable - ****************************************************************************/ - -#ifndef CONFIG_DISABLE_ENVIRON -static void show_variable(const char *var_name, const char *exptd_value, bool var_valid) -{ - char *actual_value = getenv(var_name); - if (actual_value) - { - if (var_valid) - { - if (strcmp(actual_value, exptd_value) == 0) - { - printf("show_variable: Variable=%s has value=%s\n", var_name, exptd_value); - } - else - { - printf("show_variable: ERROR Variable=%s has the wrong value\n", var_name); - printf("show_variable: found=%s expected=%s\n", actual_value, exptd_value); - } - } - else - { - printf("show_variable: ERROR Variable=%s has a value when it should not\n", var_name); - printf("show_variable: value=%s\n", actual_value); - } - } - else if (var_valid) - { - printf("show_variable: ERROR Variable=%s has no value\n", var_name); - printf("show_variable: Should have had value=%s\n", exptd_value); - } - else - { - printf("show_variable: Variable=%s has no value\n", var_name); - } -} - -static void show_environment(bool var1_valid, bool var2_valid, bool var3_valid) -{ - show_variable(g_var1_name, g_var1_value, var1_valid); - show_variable(g_var2_name, g_var2_value, var2_valid); - show_variable(g_var3_name, g_var3_value, var3_valid); -} -#else -# define show_environment() -#endif - -/**************************************************************************** - * Name: user_main - ****************************************************************************/ - -static int user_main(int argc, char *argv[]) -{ - int i; - - /* Sample the memory usage now */ - -#ifndef CONFIG_DISABLE_SIGNALS - usleep(HALF_SECOND_USEC); - -#ifdef CONFIG_CAN_PASS_STRUCTS - g_mmbefore = mallinfo(); - g_mmprevious = g_mmbefore; -#else - (void)mallinfo(&g_mmbefore); - memcpy(&g_mmprevious, &g_mmbefore, sizeof(struct mallinfo)); -#endif -#endif - - printf("\nuser_main: Begin argument test\n"); - printf("user_main: Started with argc=%d\n", argc); - - /* Verify passed arguments */ - - if (argc != NARGS + 1) - { - printf("user_main: Error expected argc=%d got argc=%d\n", - NARGS+1, argc); - } - - for (i = 0; i <= NARGS; i++) - { - printf("user_main: argv[%d]=\"%s\"\n", i, argv[i]); - } - - for (i = 1; i <= NARGS; i++) - { - if (strcmp(argv[i], g_argv[i-1]) != 0) - { - printf("user_main: ERROR argv[%d]: Expected \"%s\" found \"%s\"\n", - i, g_argv[i-1], argv[i]); - } - } - check_test_memory_usage(); - - /* Check environment variables */ -#ifndef CONFIG_DISABLE_ENVIRON - show_environment(true, true, true); - - unsetenv(g_var1_name); - show_environment(false, true, true); - check_test_memory_usage(); - - clearenv(); - show_environment(false, false, false); - check_test_memory_usage(); -#endif - - /* Top of test loop */ - -#if CONFIG_EXAMPLES_OSTEST_LOOPS > 1 - for (i = 0; i < CONFIG_EXAMPLES_OSTEST_LOOPS; i++) -#elif CONFIG_EXAMPLES_OSTEST_LOOPS == 0 - for (;;) -#endif - { -#if CONFIG_NFILE_DESCRIPTORS > 0 - /* Checkout /dev/null */ - - printf("\nuser_main: /dev/null test\n"); - dev_null(); - check_test_memory_usage(); -#endif - -#ifdef CONFIG_ARCH_FPU - /* Check that the FPU is properly supported during context switching */ - - printf("\nuser_main: FPU test\n"); - fpu_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_PTHREAD - /* Verify pthreads and pthread mutex */ - - printf("\nuser_main: mutex test\n"); - mutex_test(); - check_test_memory_usage(); -#endif - -#if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_MUTEX_TYPES) - /* Verify recursive mutexes */ - - printf("\nuser_main: recursive mutex test\n"); - recursive_mutex_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_PTHREAD - /* Verify pthread cancellation */ - - printf("\nuser_main: cancel test\n"); - cancel_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_PTHREAD - /* Verify pthreads and semaphores */ - - printf("\nuser_main: semaphore test\n"); - sem_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_PTHREAD - /* Verify pthreads and condition variables */ - - printf("\nuser_main: condition variable test\n"); -#ifdef CONFIG_PRIORITY_INHERITANCE - printf("\n Skipping, Test logic incompatible with priority inheritance\n"); -#else - cond_test(); - check_test_memory_usage(); -#endif -#endif - -#if !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) - /* Verify pthreads and condition variable timed waits */ - - printf("\nuser_main: timed wait test\n"); - timedwait_test(); - check_test_memory_usage(); -#endif - -#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_PTHREAD) - /* Verify pthreads and message queues */ - - printf("\nuser_main: message queue test\n"); - mqueue_test(); - check_test_memory_usage(); -#endif - -#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) - /* Verify pthreads and message queues */ - - printf("\nuser_main: timed message queue test\n"); - timedmqueue_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_SIGNALS - /* Verify signal handlers */ - - printf("\nuser_main: signal handler test\n"); - sighand_test(); - check_test_memory_usage(); -#endif - -#if !defined(CONFIG_DISABLE_POSIX_TIMERS) && !defined(CONFIG_DISABLE_SIGNALS) - /* Verify posix timers */ - - printf("\nuser_main: POSIX timer test\n"); - timer_test(); - check_test_memory_usage(); -#endif - -#if !defined(CONFIG_DISABLE_PTHREAD) && CONFIG_RR_INTERVAL > 0 - /* Verify round robin scheduling */ - - printf("\nuser_main: round-robin scheduler test\n"); - rr_test(); - check_test_memory_usage(); -#endif - -#ifndef CONFIG_DISABLE_PTHREAD - /* Verify pthread barriers */ - - printf("\nuser_main: barrier test\n"); - barrier_test(); - check_test_memory_usage(); -#endif - -#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) - /* Verify priority inheritance */ - - printf("\nuser_main: priority inheritance test\n"); - priority_inheritance(); - check_test_memory_usage(); -#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ - - /* Compare memory usage at time ostest_main started until - * user_main exits. These should not be identical, but should - * be similar enough that we can detect any serious OS memory - * leaks. - */ - -#ifndef CONFIG_DISABLE_SIGNALS - usleep(HALF_SECOND_USEC); - -#ifdef CONFIG_CAN_PASS_STRUCTS - g_mmafter = mallinfo(); -#else - (void)mallinfo(&g_mmafter); -#endif - - printf("\nFinal memory usage:\n"); - show_memory_usage(&g_mmbefore, &g_mmafter); -#endif - } - printf("user_main: Exitting\n"); - return 0; -} - -/**************************************************************************** - * Name: stdio_test - ****************************************************************************/ - -static void stdio_test(void) -{ - /* Verify that we can communicate */ - -#if CONFIG_NFILE_DESCRIPTORS > 0 - write(1, write_data1, sizeof(write_data1)-1); -#endif - printf("stdio_test: Standard I/O Check: printf\n"); - -#if CONFIG_NFILE_DESCRIPTORS > 1 - write(2, write_data2, sizeof(write_data2)-1); -#endif -#if CONFIG_NFILE_STREAMS > 0 - fprintf(stderr, "stdio_test: Standard I/O Check: fprintf to stderr\n"); -#endif -} - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * ostest_main - ****************************************************************************/ - -int ostest_main(int argc, char *argv[]) -{ - int result; - - /* Verify that stdio works first */ - - stdio_test(); - -#ifdef SDCC - /* I am not yet certain why SDCC does not like the following initilizers. - * It involves some issues with 2- vs 3-byte pointer types. - */ - - g_argv[0] = arg1; - g_argv[1] = arg2; - g_argv[2] = arg3; - g_argv[3] = arg4; - g_argv[4] = NULL; -#endif - - /* Set up some environment variables */ - -#ifndef CONFIG_DISABLE_ENVIRON - printf("ostest_main: putenv(%s)\n", g_putenv_value); - putenv(g_putenv_value); /* Varaible1=BadValue3 */ - printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var1_name, g_var1_value); - setenv(g_var1_name, g_var1_value, TRUE); /* Variable1=GoodValue1 */ - - printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var2_name, g_bad_value1); - setenv(g_var2_name, g_bad_value1, FALSE); /* Variable2=BadValue1 */ - printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var2_name, g_var2_value); - setenv(g_var2_name, g_var2_value, TRUE); /* Variable2=GoodValue2 */ - - printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name); - setenv(g_var3_name, g_var3_value, FALSE); /* Variable3=GoodValue3 */ - printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name); - setenv(g_var3_name, g_bad_value2, FALSE); /* Variable3=GoodValue3 */ - show_environment(true, true, true); -#endif - - /* Verify that we can spawn a new task */ - -#ifndef CONFIG_CUSTOM_STACK - result = task_create("ostest", PRIORITY, STACKSIZE, user_main, g_argv); -#else - result = task_create("ostest", PRIORITY, user_main, g_argv); -#endif - if (result == ERROR) - { - printf("ostest_main: ERROR Failed to start user_main\n"); - } - else - { - printf("ostest_main: Started user_main at PID=%d\n", result); - } - - printf("ostest_main: Exitting\n"); - return 0; -} diff --git a/apps/examples/ostest/ostest_main.c b/apps/examples/ostest/ostest_main.c new file mode 100644 index 000000000..46726d515 --- /dev/null +++ b/apps/examples/ostest/ostest_main.c @@ -0,0 +1,522 @@ +/**************************************************************************** + * apps/examples/ostest/ostest_main.c + * + * 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 + * 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 + +#include "ostest.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#define PRIORITY 100 +#define NARGS 4 +#define HALF_SECOND_USEC 500000L + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const char arg1[] = "Arg1"; +static const char arg2[] = "Arg2"; +static const char arg3[] = "Arg3"; +static const char arg4[] = "Arg4"; + +#if CONFIG_NFILE_DESCRIPTORS > 0 +static const char write_data1[] = "stdio_test: write fd=1\n"; +static const char write_data2[] = "stdio_test: write fd=2\n"; +#endif + +#ifdef SDCC +/* I am not yet certain why SDCC does not like the following + * initializer. It involves some issues with 2- vs 3-byte + * pointer types. + */ + +static const char *g_argv[NARGS+1]; +#else +static const char *g_argv[NARGS+1] = { arg1, arg2, arg3, arg4, NULL }; +#endif + +#ifndef CONFIG_DISABLE_SIGNALS +static struct mallinfo g_mmbefore; +static struct mallinfo g_mmprevious; +static struct mallinfo g_mmafter; +#endif + +#ifndef CONFIG_DISABLE_ENVIRON +const char g_var1_name[] = "Variable1"; +const char g_var1_value[] = "GoodValue1"; +const char g_var2_name[] = "Variable2"; +const char g_var2_value[] = "GoodValue2"; +const char g_var3_name[] = "Variable3"; +const char g_var3_value[] = "GoodValue3"; + +const char g_bad_value1[] = "BadValue1"; +const char g_bad_value2[] = "BadValue2"; + +const char g_putenv_value[] = "Variable1=BadValue3"; + +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: show_memory_usage + ****************************************************************************/ + +#ifndef CONFIG_DISABLE_SIGNALS +static void show_memory_usage(struct mallinfo *mmbefore, + struct mallinfo *mmafter) +{ + printf("VARIABLE BEFORE AFTER\n"); + printf("======== ======== ========\n"); + printf("arena %8x %8x\n", mmbefore->arena, mmafter->arena); + printf("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks); + printf("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk); + printf("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks); + printf("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks); +} +#else +# define show_memory_usage(mm1, mm2) +#endif + +/**************************************************************************** + * Name: check_test_memory_usage + ****************************************************************************/ + +#ifndef CONFIG_DISABLE_SIGNALS +static void check_test_memory_usage(void) +{ + /* Wait a little bit to let any threads terminate */ + + usleep(HALF_SECOND_USEC); + + /* Get the current memory usage */ + +#ifdef CONFIG_CAN_PASS_STRUCTS + g_mmafter = mallinfo(); +#else + (void)mallinfo(&g_mmafter); +#endif + + /* Show the change from the previous time */ + + printf("\nEnd of test memory usage:\n"); + show_memory_usage(&g_mmprevious, &g_mmafter); + + /* Set up for the next test */ + +#ifdef CONFIG_CAN_PASS_STRUCTS + g_mmprevious = g_mmafter; +#else + memcpy(&g_mmprevious, &g_mmafter, sizeof(struct mallinfo)); +#endif + + /* If so enabled, show the use of priority inheritance resources */ + + dump_nfreeholders("user_main:"); +} +#else +# define check_test_memory_usage() +#endif + +/**************************************************************************** + * Name: show_variable + ****************************************************************************/ + +#ifndef CONFIG_DISABLE_ENVIRON +static void show_variable(const char *var_name, const char *exptd_value, bool var_valid) +{ + char *actual_value = getenv(var_name); + if (actual_value) + { + if (var_valid) + { + if (strcmp(actual_value, exptd_value) == 0) + { + printf("show_variable: Variable=%s has value=%s\n", var_name, exptd_value); + } + else + { + printf("show_variable: ERROR Variable=%s has the wrong value\n", var_name); + printf("show_variable: found=%s expected=%s\n", actual_value, exptd_value); + } + } + else + { + printf("show_variable: ERROR Variable=%s has a value when it should not\n", var_name); + printf("show_variable: value=%s\n", actual_value); + } + } + else if (var_valid) + { + printf("show_variable: ERROR Variable=%s has no value\n", var_name); + printf("show_variable: Should have had value=%s\n", exptd_value); + } + else + { + printf("show_variable: Variable=%s has no value\n", var_name); + } +} + +static void show_environment(bool var1_valid, bool var2_valid, bool var3_valid) +{ + show_variable(g_var1_name, g_var1_value, var1_valid); + show_variable(g_var2_name, g_var2_value, var2_valid); + show_variable(g_var3_name, g_var3_value, var3_valid); +} +#else +# define show_environment() +#endif + +/**************************************************************************** + * Name: user_main + ****************************************************************************/ + +static int user_main(int argc, char *argv[]) +{ + int i; + + /* Sample the memory usage now */ + +#ifndef CONFIG_DISABLE_SIGNALS + usleep(HALF_SECOND_USEC); + +#ifdef CONFIG_CAN_PASS_STRUCTS + g_mmbefore = mallinfo(); + g_mmprevious = g_mmbefore; +#else + (void)mallinfo(&g_mmbefore); + memcpy(&g_mmprevious, &g_mmbefore, sizeof(struct mallinfo)); +#endif +#endif + + printf("\nuser_main: Begin argument test\n"); + printf("user_main: Started with argc=%d\n", argc); + + /* Verify passed arguments */ + + if (argc != NARGS + 1) + { + printf("user_main: Error expected argc=%d got argc=%d\n", + NARGS+1, argc); + } + + for (i = 0; i <= NARGS; i++) + { + printf("user_main: argv[%d]=\"%s\"\n", i, argv[i]); + } + + for (i = 1; i <= NARGS; i++) + { + if (strcmp(argv[i], g_argv[i-1]) != 0) + { + printf("user_main: ERROR argv[%d]: Expected \"%s\" found \"%s\"\n", + i, g_argv[i-1], argv[i]); + } + } + check_test_memory_usage(); + + /* Check environment variables */ +#ifndef CONFIG_DISABLE_ENVIRON + show_environment(true, true, true); + + unsetenv(g_var1_name); + show_environment(false, true, true); + check_test_memory_usage(); + + clearenv(); + show_environment(false, false, false); + check_test_memory_usage(); +#endif + + /* Top of test loop */ + +#if CONFIG_EXAMPLES_OSTEST_LOOPS > 1 + for (i = 0; i < CONFIG_EXAMPLES_OSTEST_LOOPS; i++) +#elif CONFIG_EXAMPLES_OSTEST_LOOPS == 0 + for (;;) +#endif + { +#if CONFIG_NFILE_DESCRIPTORS > 0 + /* Checkout /dev/null */ + + printf("\nuser_main: /dev/null test\n"); + dev_null(); + check_test_memory_usage(); +#endif + +#ifdef CONFIG_ARCH_FPU + /* Check that the FPU is properly supported during context switching */ + + printf("\nuser_main: FPU test\n"); + fpu_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* Verify pthreads and pthread mutex */ + + printf("\nuser_main: mutex test\n"); + mutex_test(); + check_test_memory_usage(); +#endif + +#if !defined(CONFIG_DISABLE_PTHREAD) && defined(CONFIG_MUTEX_TYPES) + /* Verify recursive mutexes */ + + printf("\nuser_main: recursive mutex test\n"); + recursive_mutex_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* Verify pthread cancellation */ + + printf("\nuser_main: cancel test\n"); + cancel_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* Verify pthreads and semaphores */ + + printf("\nuser_main: semaphore test\n"); + sem_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* Verify pthreads and condition variables */ + + printf("\nuser_main: condition variable test\n"); +#ifdef CONFIG_PRIORITY_INHERITANCE + printf("\n Skipping, Test logic incompatible with priority inheritance\n"); +#else + cond_test(); + check_test_memory_usage(); +#endif +#endif + +#if !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) + /* Verify pthreads and condition variable timed waits */ + + printf("\nuser_main: timed wait test\n"); + timedwait_test(); + check_test_memory_usage(); +#endif + +#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_PTHREAD) + /* Verify pthreads and message queues */ + + printf("\nuser_main: message queue test\n"); + mqueue_test(); + check_test_memory_usage(); +#endif + +#if !defined(CONFIG_DISABLE_MQUEUE) && !defined(CONFIG_DISABLE_PTHREAD) && !defined(CONFIG_DISABLE_CLOCK) + /* Verify pthreads and message queues */ + + printf("\nuser_main: timed message queue test\n"); + timedmqueue_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_SIGNALS + /* Verify signal handlers */ + + printf("\nuser_main: signal handler test\n"); + sighand_test(); + check_test_memory_usage(); +#endif + +#if !defined(CONFIG_DISABLE_POSIX_TIMERS) && !defined(CONFIG_DISABLE_SIGNALS) + /* Verify posix timers */ + + printf("\nuser_main: POSIX timer test\n"); + timer_test(); + check_test_memory_usage(); +#endif + +#if !defined(CONFIG_DISABLE_PTHREAD) && CONFIG_RR_INTERVAL > 0 + /* Verify round robin scheduling */ + + printf("\nuser_main: round-robin scheduler test\n"); + rr_test(); + check_test_memory_usage(); +#endif + +#ifndef CONFIG_DISABLE_PTHREAD + /* Verify pthread barriers */ + + printf("\nuser_main: barrier test\n"); + barrier_test(); + check_test_memory_usage(); +#endif + +#if defined(CONFIG_PRIORITY_INHERITANCE) && !defined(CONFIG_DISABLE_SIGNALS) && !defined(CONFIG_DISABLE_PTHREAD) + /* Verify priority inheritance */ + + printf("\nuser_main: priority inheritance test\n"); + priority_inheritance(); + check_test_memory_usage(); +#endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ + + /* Compare memory usage at time ostest_main started until + * user_main exits. These should not be identical, but should + * be similar enough that we can detect any serious OS memory + * leaks. + */ + +#ifndef CONFIG_DISABLE_SIGNALS + usleep(HALF_SECOND_USEC); + +#ifdef CONFIG_CAN_PASS_STRUCTS + g_mmafter = mallinfo(); +#else + (void)mallinfo(&g_mmafter); +#endif + + printf("\nFinal memory usage:\n"); + show_memory_usage(&g_mmbefore, &g_mmafter); +#endif + } + printf("user_main: Exitting\n"); + return 0; +} + +/**************************************************************************** + * Name: stdio_test + ****************************************************************************/ + +static void stdio_test(void) +{ + /* Verify that we can communicate */ + +#if CONFIG_NFILE_DESCRIPTORS > 0 + write(1, write_data1, sizeof(write_data1)-1); +#endif + printf("stdio_test: Standard I/O Check: printf\n"); + +#if CONFIG_NFILE_DESCRIPTORS > 1 + write(2, write_data2, sizeof(write_data2)-1); +#endif +#if CONFIG_NFILE_STREAMS > 0 + fprintf(stderr, "stdio_test: Standard I/O Check: fprintf to stderr\n"); +#endif +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * ostest_main + ****************************************************************************/ + +int ostest_main(int argc, char *argv[]) +{ + int result; + + /* Verify that stdio works first */ + + stdio_test(); + +#ifdef SDCC + /* I am not yet certain why SDCC does not like the following initilizers. + * It involves some issues with 2- vs 3-byte pointer types. + */ + + g_argv[0] = arg1; + g_argv[1] = arg2; + g_argv[2] = arg3; + g_argv[3] = arg4; + g_argv[4] = NULL; +#endif + + /* Set up some environment variables */ + +#ifndef CONFIG_DISABLE_ENVIRON + printf("ostest_main: putenv(%s)\n", g_putenv_value); + putenv(g_putenv_value); /* Varaible1=BadValue3 */ + printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var1_name, g_var1_value); + setenv(g_var1_name, g_var1_value, TRUE); /* Variable1=GoodValue1 */ + + printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var2_name, g_bad_value1); + setenv(g_var2_name, g_bad_value1, FALSE); /* Variable2=BadValue1 */ + printf("ostest_main: setenv(%s, %s, TRUE)\n", g_var2_name, g_var2_value); + setenv(g_var2_name, g_var2_value, TRUE); /* Variable2=GoodValue2 */ + + printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name); + setenv(g_var3_name, g_var3_value, FALSE); /* Variable3=GoodValue3 */ + printf("ostest_main: setenv(%s, %s, FALSE)\n", g_var3_name, g_var3_name); + setenv(g_var3_name, g_bad_value2, FALSE); /* Variable3=GoodValue3 */ + show_environment(true, true, true); +#endif + + /* Verify that we can spawn a new task */ + +#ifndef CONFIG_CUSTOM_STACK + result = task_create("ostest", PRIORITY, STACKSIZE, user_main, g_argv); +#else + result = task_create("ostest", PRIORITY, user_main, g_argv); +#endif + if (result == ERROR) + { + printf("ostest_main: ERROR Failed to start user_main\n"); + } + else + { + printf("ostest_main: Started user_main at PID=%d\n", result); + } + + printf("ostest_main: Exitting\n"); + return 0; +} diff --git a/apps/examples/rgmp/Makefile b/apps/examples/rgmp/Makefile index cefe63d54..60fbf7c68 100644 --- a/apps/examples/rgmp/Makefile +++ b/apps/examples/rgmp/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # The smallest thing you can build -- the NULL example. ASRCS = -CSRCS = main.c +CSRCS = rgmp_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/rgmp/main.c b/apps/examples/rgmp/main.c deleted file mode 100644 index a1fa6cdc3..000000000 --- a/apps/examples/rgmp/main.c +++ /dev/null @@ -1,66 +0,0 @@ -/**************************************************************************** - * examples/rgmp/main.c - * - * Copyright (C) 2011 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 - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * rgmp_main - ****************************************************************************/ - -int rgmp_main(int argc, char *argv[]) -{ - // TODO: add your code here - - return 0; -} - diff --git a/apps/examples/rgmp/rgmp_main.c b/apps/examples/rgmp/rgmp_main.c new file mode 100644 index 000000000..3ef70edc8 --- /dev/null +++ b/apps/examples/rgmp/rgmp_main.c @@ -0,0 +1,66 @@ +/**************************************************************************** + * examples/rgmp/rgmp_main.c + * + * Copyright (C) 2011 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 + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * rgmp_main + ****************************************************************************/ + +int rgmp_main(int argc, char *argv[]) +{ + // TODO: add your code here + + return 0; +} + diff --git a/apps/examples/serloop/Makefile b/apps/examples/serloop/Makefile index 67c805d46..7594779ce 100644 --- a/apps/examples/serloop/Makefile +++ b/apps/examples/serloop/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # Mindlessly simple console loopack test ASRCS = -CSRCS = main.c +CSRCS = serloop_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/serloop/main.c b/apps/examples/serloop/main.c deleted file mode 100644 index f0e3ac238..000000000 --- a/apps/examples/serloop/main.c +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** - * examples/serloop/main.c - * - * Copyright (C) 2008-2009, 2011 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 -#include - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * serloop_main - ****************************************************************************/ - -int serloop_main(int argc, char *argv[]) -{ -#ifdef CONFIG_EXAMPLES_SERLOOP_BUFIO - int ch; - - for (;;) - { - ch = getchar(); - if (ch < 1) - { - ch = '!'; - } - else if ((ch < 0x20 || ch > 0x7e) && ch != '\n') - { - ch = '.'; - } - putchar(ch); - } -#else - uint8_t ch; - int ret; - - for (;;) - { - ret = read(0, &ch, 1); - if (ret < 1) - { - ch = '!'; - } - else if ((ch < 0x20 || ch > 0x7e) && ch != '\n') - { - ch = '.'; - } - ret = write(1, &ch, 1); - } -#endif - return 0; -} - diff --git a/apps/examples/serloop/serloop_main.c b/apps/examples/serloop/serloop_main.c new file mode 100644 index 000000000..6fba577a4 --- /dev/null +++ b/apps/examples/serloop/serloop_main.c @@ -0,0 +1,100 @@ +/**************************************************************************** + * examples/serloop/serloop_main.c + * + * Copyright (C) 2008-2009, 2011 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 +#include + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * serloop_main + ****************************************************************************/ + +int serloop_main(int argc, char *argv[]) +{ +#ifdef CONFIG_EXAMPLES_SERLOOP_BUFIO + int ch; + + for (;;) + { + ch = getchar(); + if (ch < 1) + { + ch = '!'; + } + else if ((ch < 0x20 || ch > 0x7e) && ch != '\n') + { + ch = '.'; + } + putchar(ch); + } +#else + uint8_t ch; + int ret; + + for (;;) + { + ret = read(0, &ch, 1); + if (ret < 1) + { + ch = '!'; + } + else if ((ch < 0x20 || ch > 0x7e) && ch != '\n') + { + ch = '.'; + } + ret = write(1, &ch, 1); + } +#endif + return 0; +} + diff --git a/apps/examples/thttpd/Makefile b/apps/examples/thttpd/Makefile index 9df26af35..606e717dc 100644 --- a/apps/examples/thttpd/Makefile +++ b/apps/examples/thttpd/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # THTTPD Web Server Example ASRCS = -CSRCS = main.c +CSRCS = thttpd_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/thttpd/main.c b/apps/examples/thttpd/main.c deleted file mode 100644 index 4f8315d95..000000000 --- a/apps/examples/thttpd/main.c +++ /dev/null @@ -1,267 +0,0 @@ -/**************************************************************************** - * examples/thttpd/main.c - * - * Copyright (C) 2009-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 Gregory Nutt 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 -#include - -#include -#include - -#include -#include -#include - -#include -#include -#include -#ifdef CONFIG_NET_SLIP -# include -#endif - -#include "content/romfs.h" -#include "content/symtab.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -/* Check configuration. This is not all of the configuration settings that - * are required -- only the more obvious. - */ - -#if CONFIG_NFILE_DESCRIPTORS < 1 -# error "You must provide file descriptors via CONFIG_NFILE_DESCRIPTORS in your configuration file" -#endif - -#ifndef CONFIG_NXFLAT -# error "You must select CONFIG_NXFLAT in your configuration file" -#endif - -#ifndef CONFIG_FS_ROMFS -# error "You must select CONFIG_FS_ROMFS in your configuration file" -#endif - -#ifdef CONFIG_DISABLE_MOUNTPOINT -# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file" -#endif - -#ifdef CONFIG_BINFMT_DISABLE -# error "You must not disable loadable modules via CONFIG_BINFMT_DISABLE in your configuration file" -#endif - -/* SLIP-specific configuration */ - -#ifdef CONFIG_NET_SLIP - - /* No MAC address operations */ - -# undef CONFIG_EXAMPLE_THTTPD_NOMAC - - /* TTY device to use */ - -# ifndef CONFIG_NET_SLIPTTY -# define CONFIG_NET_SLIPTTY "/dev/ttyS1" -# endif - -# define SLIP_DEVNO 0 -# define NET_DEVNAME "sl0" -#else - - /* Otherwise, use the standard ethernet device name */ - -# define NET_DEVNAME "eth0" -#endif - -/* Describe the ROMFS file system */ - -#define SECTORSIZE 512 -#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE) -#define ROMFSDEV "/dev/ram0" -#define MOUNTPT CONFIG_THTTPD_PATH - -#ifdef CONFIG_CPP_HAVE_VARARGS -# ifdef CONFIG_DEBUG -# define message(...) lib_lowprintf(__VA_ARGS__) -# define msgflush() -# else -# define message(...) printf(__VA_ARGS__) -# define msgflush() fflush(stdout) -# endif -#else -# ifdef CONFIG_DEBUG -# define message lib_lowprintf -# define msgflush() -# else -# define message printf -# define msgflush() fflush(stdout) -# endif -#endif - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Data - ****************************************************************************/ - -/* These values must be provided by the user before the THTTPD task daemon - * is started: - * - * g_thttpdsymtab: A symbol table describing all of the symbols exported - * from the base system. These symbols are used to bind address references - * in CGI programs to NuttX. - * g_nsymbols: The number of symbols in g_thttpdsymtab[]. - */ - -FAR const struct symtab_s *g_thttpdsymtab; -int g_thttpdnsymbols; - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * thttp_main - ****************************************************************************/ - -int thttp_main(int argc, char *argv[]) -{ - struct in_addr addr; -#ifdef CONFIG_EXAMPLE_THTTPD_NOMAC - uint8_t mac[IFHWADDRLEN]; -#endif - char *thttpd_argv = "thttpd"; - int ret; - - /* Configure SLIP */ - -#ifdef CONFIG_NET_SLIP - ret = slip_initialize(SLIP_DEVNO, CONFIG_NET_SLIPTTY); - if (ret < 0) - { - message("ERROR: SLIP initialization failed: %d\n", ret); - exit(1); - } -#endif - -/* Many embedded network interfaces must have a software assigned MAC */ - -#ifdef CONFIG_EXAMPLE_THTTPD_NOMAC - message("Assigning MAC\n"); - - mac[0] = 0x00; - mac[1] = 0xe0; - mac[2] = 0xde; - mac[3] = 0xad; - mac[4] = 0xbe; - mac[5] = 0xef; - uip_setmacaddr(NET_DEVNAME, mac); -#endif - - /* Set up our host address */ - - message("Setup network addresses\n"); - addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR); - uip_sethostaddr(NET_DEVNAME, &addr); - - /* Set up the default router address */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_DRIPADDR); - uip_setdraddr(NET_DEVNAME, &addr); - - /* Setup the subnet mask */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_NETMASK); - uip_setnetmask(NET_DEVNAME, &addr); - - /* Initialize the NXFLAT binary loader */ - - message("Initializing the NXFLAT binary loader\n"); - ret = nxflat_initialize(); - if (ret < 0) - { - message("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret); - exit(2); - } - - /* Create a ROM disk for the ROMFS filesystem */ - - message("Registering romdisk\n"); - ret = romdisk_register(0, (uint8_t*)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); - if (ret < 0) - { - message("ERROR: romdisk_register failed: %d\n", ret); - nxflat_uninitialize(); - exit(1); - } - - /* Mount the file system */ - - message("Mounting ROMFS filesystem at target=%s with source=%s\n", - MOUNTPT, ROMFSDEV); - - ret = mount(ROMFSDEV, MOUNTPT, "romfs", MS_RDONLY, NULL); - if (ret < 0) - { - message("ERROR: mount(%s,%s,romfs) failed: %s\n", - ROMFSDEV, MOUNTPT, errno); - nxflat_uninitialize(); - } - - /* Start THTTPD. At present, symbol table info is passed via global variables */ - - g_thttpdsymtab = exports; - g_thttpdnsymbols = NEXPORTS; - - message("Starting THTTPD\n"); - msgflush(); - thttpd_main(1, &thttpd_argv); - message("THTTPD terminated\n"); - msgflush(); - return 0; -} diff --git a/apps/examples/thttpd/thttpd_main.c b/apps/examples/thttpd/thttpd_main.c new file mode 100644 index 000000000..f4d5d58db --- /dev/null +++ b/apps/examples/thttpd/thttpd_main.c @@ -0,0 +1,267 @@ +/**************************************************************************** + * examples/thttpd/thttpd_main.c + * + * Copyright (C) 2009-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 Gregory Nutt 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 +#include + +#include +#include + +#include +#include +#include + +#include +#include +#include +#ifdef CONFIG_NET_SLIP +# include +#endif + +#include "content/romfs.h" +#include "content/symtab.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/* Check configuration. This is not all of the configuration settings that + * are required -- only the more obvious. + */ + +#if CONFIG_NFILE_DESCRIPTORS < 1 +# error "You must provide file descriptors via CONFIG_NFILE_DESCRIPTORS in your configuration file" +#endif + +#ifndef CONFIG_NXFLAT +# error "You must select CONFIG_NXFLAT in your configuration file" +#endif + +#ifndef CONFIG_FS_ROMFS +# error "You must select CONFIG_FS_ROMFS in your configuration file" +#endif + +#ifdef CONFIG_DISABLE_MOUNTPOINT +# error "You must not disable mountpoints via CONFIG_DISABLE_MOUNTPOINT in your configuration file" +#endif + +#ifdef CONFIG_BINFMT_DISABLE +# error "You must not disable loadable modules via CONFIG_BINFMT_DISABLE in your configuration file" +#endif + +/* SLIP-specific configuration */ + +#ifdef CONFIG_NET_SLIP + + /* No MAC address operations */ + +# undef CONFIG_EXAMPLE_THTTPD_NOMAC + + /* TTY device to use */ + +# ifndef CONFIG_NET_SLIPTTY +# define CONFIG_NET_SLIPTTY "/dev/ttyS1" +# endif + +# define SLIP_DEVNO 0 +# define NET_DEVNAME "sl0" +#else + + /* Otherwise, use the standard ethernet device name */ + +# define NET_DEVNAME "eth0" +#endif + +/* Describe the ROMFS file system */ + +#define SECTORSIZE 512 +#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE) +#define ROMFSDEV "/dev/ram0" +#define MOUNTPT CONFIG_THTTPD_PATH + +#ifdef CONFIG_CPP_HAVE_VARARGS +# ifdef CONFIG_DEBUG +# define message(...) lib_lowprintf(__VA_ARGS__) +# define msgflush() +# else +# define message(...) printf(__VA_ARGS__) +# define msgflush() fflush(stdout) +# endif +#else +# ifdef CONFIG_DEBUG +# define message lib_lowprintf +# define msgflush() +# else +# define message printf +# define msgflush() fflush(stdout) +# endif +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +/* These values must be provided by the user before the THTTPD task daemon + * is started: + * + * g_thttpdsymtab: A symbol table describing all of the symbols exported + * from the base system. These symbols are used to bind address references + * in CGI programs to NuttX. + * g_nsymbols: The number of symbols in g_thttpdsymtab[]. + */ + +FAR const struct symtab_s *g_thttpdsymtab; +int g_thttpdnsymbols; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * thttp_main + ****************************************************************************/ + +int thttp_main(int argc, char *argv[]) +{ + struct in_addr addr; +#ifdef CONFIG_EXAMPLE_THTTPD_NOMAC + uint8_t mac[IFHWADDRLEN]; +#endif + char *thttpd_argv = "thttpd"; + int ret; + + /* Configure SLIP */ + +#ifdef CONFIG_NET_SLIP + ret = slip_initialize(SLIP_DEVNO, CONFIG_NET_SLIPTTY); + if (ret < 0) + { + message("ERROR: SLIP initialization failed: %d\n", ret); + exit(1); + } +#endif + +/* Many embedded network interfaces must have a software assigned MAC */ + +#ifdef CONFIG_EXAMPLE_THTTPD_NOMAC + message("Assigning MAC\n"); + + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xde; + mac[3] = 0xad; + mac[4] = 0xbe; + mac[5] = 0xef; + uip_setmacaddr(NET_DEVNAME, mac); +#endif + + /* Set up our host address */ + + message("Setup network addresses\n"); + addr.s_addr = HTONL(CONFIG_THTTPD_IPADDR); + uip_sethostaddr(NET_DEVNAME, &addr); + + /* Set up the default router address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_DRIPADDR); + uip_setdraddr(NET_DEVNAME, &addr); + + /* Setup the subnet mask */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_THTTPD_NETMASK); + uip_setnetmask(NET_DEVNAME, &addr); + + /* Initialize the NXFLAT binary loader */ + + message("Initializing the NXFLAT binary loader\n"); + ret = nxflat_initialize(); + if (ret < 0) + { + message("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret); + exit(2); + } + + /* Create a ROM disk for the ROMFS filesystem */ + + message("Registering romdisk\n"); + ret = romdisk_register(0, (uint8_t*)romfs_img, NSECTORS(romfs_img_len), SECTORSIZE); + if (ret < 0) + { + message("ERROR: romdisk_register failed: %d\n", ret); + nxflat_uninitialize(); + exit(1); + } + + /* Mount the file system */ + + message("Mounting ROMFS filesystem at target=%s with source=%s\n", + MOUNTPT, ROMFSDEV); + + ret = mount(ROMFSDEV, MOUNTPT, "romfs", MS_RDONLY, NULL); + if (ret < 0) + { + message("ERROR: mount(%s,%s,romfs) failed: %s\n", + ROMFSDEV, MOUNTPT, errno); + nxflat_uninitialize(); + } + + /* Start THTTPD. At present, symbol table info is passed via global variables */ + + g_thttpdsymtab = exports; + g_thttpdnsymbols = NEXPORTS; + + message("Starting THTTPD\n"); + msgflush(); + thttpd_main(1, &thttpd_argv); + message("THTTPD terminated\n"); + msgflush(); + return 0; +} diff --git a/apps/examples/uip/Makefile b/apps/examples/uip/Makefile index 4407998fc..218f6f3c6 100644 --- a/apps/examples/uip/Makefile +++ b/apps/examples/uip/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # uIP very tiny web server example ASRCS = -CSRCS = main.c cgi.c httpd_fsdata.c +CSRCS = uip_main.c cgi.c httpd_fsdata.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/uip/main.c b/apps/examples/uip/main.c deleted file mode 100644 index dcad63eaa..000000000 --- a/apps/examples/uip/main.c +++ /dev/null @@ -1,210 +0,0 @@ -/**************************************************************************** - * examples/uip/main.c - * - * Copyright (C) 2007, 2009-2012 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Based on uIP which also has a BSD style license: - * - * Copyright (c) 2001, Adam Dunkels. - * All rights reserved. - * - * 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. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by Adam Dunkels. - * 4. The name of the author may not be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 - -#include -#include -#include - -#include - -#ifdef CONFIG_EXAMPLE_UIP_DHCPC -#include -#endif - -/* Here we include the header file for the application(s) we use in - * our project as defined in the config//defconfig file - */ - -/* DHCPC may be used in conjunction with any other feature (or not) */ - -#ifdef CONFIG_EXAMPLE_UIP_DHCPC -# include -# include -#endif - -/* Include uIP webserver definitions */ - -#include - -#include "cgi.h" - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -#ifdef CONFIG_CPP_HAVE_VARARGS -# ifdef CONFIG_DEBUG -# define message(...) lib_lowprintf(__VA_ARGS__) -# else -# define message(...) printf(__VA_ARGS__) -# endif -#else -# ifdef CONFIG_DEBUG -# define message lib_lowprintf -# else -# define message (void) -# endif -#endif - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * uip_main - ****************************************************************************/ - -int uip_main(int argc, char *argv[]) -{ - struct in_addr addr; -#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_NOMAC) - uint8_t mac[IFHWADDRLEN]; -#endif -#ifdef CONFIG_EXAMPLE_UIP_DHCPC - void *handle; -#endif - -/* Many embedded network interfaces must have a software assigned MAC */ - -#ifdef CONFIG_EXAMPLE_UIP_NOMAC - mac[0] = 0x00; - mac[1] = 0xe0; - mac[2] = 0xde; - mac[3] = 0xad; - mac[4] = 0xbe; - mac[5] = 0xef; - uip_setmacaddr("eth0", mac); -#endif - - /* Set up our host address */ - -#ifdef CONFIG_EXAMPLE_UIP_DHCPC - addr.s_addr = 0; -#else - addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR); -#endif - uip_sethostaddr("eth0", &addr); - - /* Set up the default router address */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DRIPADDR); - uip_setdraddr("eth0", &addr); - - /* Setup the subnet mask */ - - addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK); - uip_setnetmask("eth0", &addr); - -#ifdef CONFIG_EXAMPLE_UIP_DHCPC - /* Set up the resolver */ - - resolv_init(); - - /* Get the MAC address of the NIC */ - - uip_getmacaddr("eth0", mac); - - /* Set up the DHCPC modules */ - - handle = dhcpc_open(&mac, IFHWADDRLEN); - - /* Get an IP address. Note: there is no logic here for renewing the address in this - * example. The address should be renewed in ds.lease_time/2 seconds. - */ - - printf("Getting IP address\n"); - if (handle) - { - struct dhcpc_state ds; - (void)dhcpc_request(handle, &ds); - uip_sethostaddr("eth1", &ds.ipaddr); - if (ds.netmask.s_addr != 0) - { - uip_setnetmask("eth0", &ds.netmask); - } - if (ds.default_router.s_addr != 0) - { - uip_setdraddr("eth0", &ds.default_router); - } - if (ds.dnsaddr.s_addr != 0) - { - resolv_conf(&ds.dnsaddr); - } - dhcpc_close(handle); - printf("IP: %s\n", inet_ntoa(ds.ipaddr)); - } -#endif - -#ifdef CONFIG_NET_TCP - printf("Starting webserver\n"); - httpd_init(); - cgi_register(); - httpd_listen(); -#endif - - while(1) - { - sleep(3); - printf("main: Still running\n"); -#if CONFIG_NFILE_DESCRIPTORS > 0 - fflush(stdout); -#endif - } - return 0; -} diff --git a/apps/examples/uip/uip_main.c b/apps/examples/uip/uip_main.c new file mode 100644 index 000000000..b552aed75 --- /dev/null +++ b/apps/examples/uip/uip_main.c @@ -0,0 +1,210 @@ +/**************************************************************************** + * examples/uip/uip_main.c + * + * Copyright (C) 2007, 2009-2012 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Based on uIP which also has a BSD style license: + * + * Copyright (c) 2001, Adam Dunkels. + * All rights reserved. + * + * 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. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * This product includes software developed by Adam Dunkels. + * 4. The name of the author may not be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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 + +#include +#include +#include + +#include + +#ifdef CONFIG_EXAMPLE_UIP_DHCPC +#include +#endif + +/* Here we include the header file for the application(s) we use in + * our project as defined in the config//defconfig file + */ + +/* DHCPC may be used in conjunction with any other feature (or not) */ + +#ifdef CONFIG_EXAMPLE_UIP_DHCPC +# include +# include +#endif + +/* Include uIP webserver definitions */ + +#include + +#include "cgi.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#ifdef CONFIG_CPP_HAVE_VARARGS +# ifdef CONFIG_DEBUG +# define message(...) lib_lowprintf(__VA_ARGS__) +# else +# define message(...) printf(__VA_ARGS__) +# endif +#else +# ifdef CONFIG_DEBUG +# define message lib_lowprintf +# else +# define message (void) +# endif +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * uip_main + ****************************************************************************/ + +int uip_main(int argc, char *argv[]) +{ + struct in_addr addr; +#if defined(CONFIG_EXAMPLE_UIP_DHCPC) || defined(CONFIG_EXAMPLE_UIP_NOMAC) + uint8_t mac[IFHWADDRLEN]; +#endif +#ifdef CONFIG_EXAMPLE_UIP_DHCPC + void *handle; +#endif + +/* Many embedded network interfaces must have a software assigned MAC */ + +#ifdef CONFIG_EXAMPLE_UIP_NOMAC + mac[0] = 0x00; + mac[1] = 0xe0; + mac[2] = 0xde; + mac[3] = 0xad; + mac[4] = 0xbe; + mac[5] = 0xef; + uip_setmacaddr("eth0", mac); +#endif + + /* Set up our host address */ + +#ifdef CONFIG_EXAMPLE_UIP_DHCPC + addr.s_addr = 0; +#else + addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_IPADDR); +#endif + uip_sethostaddr("eth0", &addr); + + /* Set up the default router address */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_DRIPADDR); + uip_setdraddr("eth0", &addr); + + /* Setup the subnet mask */ + + addr.s_addr = HTONL(CONFIG_EXAMPLE_UIP_NETMASK); + uip_setnetmask("eth0", &addr); + +#ifdef CONFIG_EXAMPLE_UIP_DHCPC + /* Set up the resolver */ + + resolv_init(); + + /* Get the MAC address of the NIC */ + + uip_getmacaddr("eth0", mac); + + /* Set up the DHCPC modules */ + + handle = dhcpc_open(&mac, IFHWADDRLEN); + + /* Get an IP address. Note: there is no logic here for renewing the address in this + * example. The address should be renewed in ds.lease_time/2 seconds. + */ + + printf("Getting IP address\n"); + if (handle) + { + struct dhcpc_state ds; + (void)dhcpc_request(handle, &ds); + uip_sethostaddr("eth1", &ds.ipaddr); + if (ds.netmask.s_addr != 0) + { + uip_setnetmask("eth0", &ds.netmask); + } + if (ds.default_router.s_addr != 0) + { + uip_setdraddr("eth0", &ds.default_router); + } + if (ds.dnsaddr.s_addr != 0) + { + resolv_conf(&ds.dnsaddr); + } + dhcpc_close(handle); + printf("IP: %s\n", inet_ntoa(ds.ipaddr)); + } +#endif + +#ifdef CONFIG_NET_TCP + printf("Starting webserver\n"); + httpd_init(); + cgi_register(); + httpd_listen(); +#endif + + while(1) + { + sleep(3); + printf("uip_main: Still running\n"); +#if CONFIG_NFILE_DESCRIPTORS > 0 + fflush(stdout); +#endif + } + return 0; +} diff --git a/apps/examples/usbserial/Makefile b/apps/examples/usbserial/Makefile index 35c76ba89..cf790dbe9 100644 --- a/apps/examples/usbserial/Makefile +++ b/apps/examples/usbserial/Makefile @@ -40,7 +40,7 @@ include $(APPDIR)/Make.defs # USB serial device example ASRCS = -CSRCS = main.c +CSRCS = usbserial_main.c AOBJS = $(ASRCS:.S=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT)) diff --git a/apps/examples/usbserial/main.c b/apps/examples/usbserial/main.c deleted file mode 100644 index c73881a44..000000000 --- a/apps/examples/usbserial/main.c +++ /dev/null @@ -1,467 +0,0 @@ -/**************************************************************************** - * examples/usbserial/main.c - * - * Copyright (C) 2008, 2010-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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#ifdef CONFIG_CDCACM -# include -#endif - -/**************************************************************************** - * Definitions - ****************************************************************************/ - -#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY) -# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_INONLY and _OUTONLY" -#endif -#if defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) && defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) -# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL and _ONLYBIG" -#endif - -#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) -# ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY -# define COUNTER_NEEDED 1 -# endif -#endif - -#ifdef CONFIG_EXAMPLES_USBSERIAL_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_USBSERIAL_TRACECLASS -# define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|TRACE_CLASSSTATE_BIT) -#else -# define TRACE_CLASS_BITS (0) -#endif - -#ifdef CONFIG_EXAMPLES_USBSERIAL_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_USBSERIAL_TRACECONTROLLER -# define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT) -#else -# define TRACE_CONTROLLER_BITS (0) -#endif - -#ifdef CONFIG_EXAMPLES_USBSERIAL_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_CPP_HAVE_VARARGS -# ifdef CONFIG_DEBUG -# define message(...) lib_lowprintf(__VA_ARGS__) -# define trmessage lib_lowprintf -# 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 - -#ifdef CONFIG_CDCACM -# define USBSER_DEVNAME "/dev/ttyACM0" -#else -# define USBSER_DEVNAME "/dev/ttyUSB0" -#endif - -#define IOBUFFER_SIZE 256 - -/**************************************************************************** - * Private Data - ****************************************************************************/ - -#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYBIG -static const char g_shortmsg[] = "Hello, World!!\n"; -#endif - -#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL -static const char g_longmsg[] = - "The Spanish Armada a Speech by Queen Elizabeth I of England\n" - "Addressed to the English army at Tilbury Fort - 1588\n" - "My loving people, we have been persuaded by some, that are careful of our " - "safety, to take heed how we commit ourselves to armed multitudes, for fear " - "of treachery; but I assure you, I do not desire to live to distrust my " - "faithful and loving people.\n" - "Let tyrants fear; I have always so behaved myself that, under God, I have " - "placed my chiefest strength and safeguard in the loyal hearts and good will " - "of my subjects. And therefore I am come amongst you at this time, not as for " - "my recreation or sport, but being resolved, in the midst and heat of the " - "battle, to live or die amongst you all; to lay down, for my God, and for " - "my kingdom, and for my people, my honour and my blood, even the dust.\n" - "I know I have but the body of a weak and feeble woman; but I have the heart " - "of a king, and of a king of England, too; and think foul scorn that Parma " - "or Spain, or any prince of Europe, should dare to invade the borders of my " - "realms: to which, rather than any dishonour should grow by me, I myself will " - "take up arms; I myself will be your general, judge, and rewarder of every " - "one of your virtues in the field.\n" - "I know already, by your forwardness, that you have deserved rewards and " - "crowns; and we do assure you, on the word of a prince, they shall be duly " - "paid you. In the mean my lieutenant general shall be in my stead, than whom " - "never prince commanded a more noble and worthy subject; not doubting by " - "your obedience to my general, by your concord in the camp, and by your " - "valour in the field, we shall shortly have a famous victory over the enemies " - "of my God, of my kingdom, and of my people.\n"; -#endif - -#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY -static char g_iobuffer[IOBUFFER_SIZE]; -#endif - -/**************************************************************************** - * Private Functions - ****************************************************************************/ - -#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; -} - -static void dumptrace(void) -{ - (void)usbtrace_enumerate(trace_callback, NULL); -} -#else -# define dumptrace() -#endif - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * usbserial_main - ****************************************************************************/ - -int usbserial_main(int argc, char *argv[]) -{ -#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 */ - - message("usbserial_main: Registering USB serial driver\n"); -#ifdef CONFIG_CDCACM - ret = cdcacm_initialize(0, NULL); -#else - ret = usbdev_serialinitialize(0); -#endif - if (ret < 0) - { - message("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", -ret); - return 1; - } - message("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 - { - message("usbserial_main: Opening USB serial driver\n"); - outfd = open(USBSER_DEVNAME, O_WRONLY); - if (outfd < 0) - { - int errcode = errno; - message("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) - { - message("usbserial_main: Not connected. Wait and try again.\n"); - sleep(5); - } - else - { - /* Give up on other errors */ - - message("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) - { - message("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; - message("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) - { - message("usbserial_main: Not connected. Wait and try again.\n"); - sleep(5); - } - else - { - /* Give up on other errors */ - - message("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 - - message("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) - { - message("usbserial_main: Saying hello\n"); - nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); - count++; - } - else - { - message("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) - message("usbserial_main: Reciting QEI's speech of 1588\n"); - nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); -#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */ - message("usbserial_main: Saying hello\n"); - nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); -#endif - - /* Test if the write was successful */ - - if (nbytes < 0) - { - message("usbserial_main: ERROR: write failed: %d\n", errno); -#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY - close(infd); -#endif - close(outfd); - return 4; - } - message("usbserial_main: %d bytes sent\n", nbytes); -#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */ - - /* Test OUT (host-to-device) messages */ - -#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY - /* Poll for incoming messages */ - - message("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) - { - message("usbserial_main: ERROR: read failed: %d\n", errno); - close(infd); -#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY - close(outfd); -#endif - return 6; - } - } - else - { - message("usbserial_main: Received %d bytes:\n", nbytes); - if (nbytes > 0) - { - for (j = 0; j < nbytes; j += 16) - { - message("usbserial_main: %03x: ", j); - for (k = 0; k < 16; k++) - { - if (k == 8) - { - message(" "); - } - if (j+k < nbytes) - { - message("%02x", g_iobuffer[j+k]); - } - else - { - message(" "); - } - } - message(" "); - for (k = 0; k < 16; k++) - { - if (k == 8) - { - message(" "); - } - if (j+k < nbytes) - { - if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f) - { - message("%c", g_iobuffer[j+k]); - } - else - { - message("."); - } - } - else - { - message(" "); - } - } - message("\n"); - } - } - } - sleep(1); - } -#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ - message("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; -} - diff --git a/apps/examples/usbserial/usbserial_main.c b/apps/examples/usbserial/usbserial_main.c new file mode 100644 index 000000000..016c8b292 --- /dev/null +++ b/apps/examples/usbserial/usbserial_main.c @@ -0,0 +1,467 @@ +/**************************************************************************** + * examples/usbserial/usbserial_main.c + * + * Copyright (C) 2008, 2010-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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#ifdef CONFIG_CDCACM +# include +#endif + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +#if defined(CONFIG_EXAMPLES_USBSERIAL_INONLY) && defined(CONFIG_EXAMPLES_USBSERIAL_OUTONLY) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_INONLY and _OUTONLY" +#endif +#if defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) && defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) +# error "Cannot define both CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL and _ONLYBIG" +#endif + +#if !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) && !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL) +# ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY +# define COUNTER_NEEDED 1 +# endif +#endif + +#ifdef CONFIG_EXAMPLES_USBSERIAL_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_USBSERIAL_TRACECLASS +# define TRACE_CLASS_BITS (TRACE_CLASS_BIT|TRACE_CLASSAPI_BIT|TRACE_CLASSSTATE_BIT) +#else +# define TRACE_CLASS_BITS (0) +#endif + +#ifdef CONFIG_EXAMPLES_USBSERIAL_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_USBSERIAL_TRACECONTROLLER +# define TRACE_CONTROLLER_BITS (TRACE_EP_BIT|TRACE_DEV_BIT) +#else +# define TRACE_CONTROLLER_BITS (0) +#endif + +#ifdef CONFIG_EXAMPLES_USBSERIAL_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_CPP_HAVE_VARARGS +# ifdef CONFIG_DEBUG +# define message(...) lib_lowprintf(__VA_ARGS__) +# define trmessage lib_lowprintf +# 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 + +#ifdef CONFIG_CDCACM +# define USBSER_DEVNAME "/dev/ttyACM0" +#else +# define USBSER_DEVNAME "/dev/ttyUSB0" +#endif + +#define IOBUFFER_SIZE 256 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYBIG +static const char g_shortmsg[] = "Hello, World!!\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_ONLYSMALL +static const char g_longmsg[] = + "The Spanish Armada a Speech by Queen Elizabeth I of England\n" + "Addressed to the English army at Tilbury Fort - 1588\n" + "My loving people, we have been persuaded by some, that are careful of our " + "safety, to take heed how we commit ourselves to armed multitudes, for fear " + "of treachery; but I assure you, I do not desire to live to distrust my " + "faithful and loving people.\n" + "Let tyrants fear; I have always so behaved myself that, under God, I have " + "placed my chiefest strength and safeguard in the loyal hearts and good will " + "of my subjects. And therefore I am come amongst you at this time, not as for " + "my recreation or sport, but being resolved, in the midst and heat of the " + "battle, to live or die amongst you all; to lay down, for my God, and for " + "my kingdom, and for my people, my honour and my blood, even the dust.\n" + "I know I have but the body of a weak and feeble woman; but I have the heart " + "of a king, and of a king of England, too; and think foul scorn that Parma " + "or Spain, or any prince of Europe, should dare to invade the borders of my " + "realms: to which, rather than any dishonour should grow by me, I myself will " + "take up arms; I myself will be your general, judge, and rewarder of every " + "one of your virtues in the field.\n" + "I know already, by your forwardness, that you have deserved rewards and " + "crowns; and we do assure you, on the word of a prince, they shall be duly " + "paid you. In the mean my lieutenant general shall be in my stead, than whom " + "never prince commanded a more noble and worthy subject; not doubting by " + "your obedience to my general, by your concord in the camp, and by your " + "valour in the field, we shall shortly have a famous victory over the enemies " + "of my God, of my kingdom, and of my people.\n"; +#endif + +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY +static char g_iobuffer[IOBUFFER_SIZE]; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#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; +} + +static void dumptrace(void) +{ + (void)usbtrace_enumerate(trace_callback, NULL); +} +#else +# define dumptrace() +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * usbserial_main + ****************************************************************************/ + +int usbserial_main(int argc, char *argv[]) +{ +#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 */ + + message("usbserial_main: Registering USB serial driver\n"); +#ifdef CONFIG_CDCACM + ret = cdcacm_initialize(0, NULL); +#else + ret = usbdev_serialinitialize(0); +#endif + if (ret < 0) + { + message("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", -ret); + return 1; + } + message("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 + { + message("usbserial_main: Opening USB serial driver\n"); + outfd = open(USBSER_DEVNAME, O_WRONLY); + if (outfd < 0) + { + int errcode = errno; + message("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) + { + message("usbserial_main: Not connected. Wait and try again.\n"); + sleep(5); + } + else + { + /* Give up on other errors */ + + message("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) + { + message("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; + message("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) + { + message("usbserial_main: Not connected. Wait and try again.\n"); + sleep(5); + } + else + { + /* Give up on other errors */ + + message("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 + + message("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) + { + message("usbserial_main: Saying hello\n"); + nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); + count++; + } + else + { + message("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) + message("usbserial_main: Reciting QEI's speech of 1588\n"); + nbytes = write(outfd, g_longmsg, sizeof(g_longmsg)); +#else /* !defined(CONFIG_EXAMPLES_USBSERIAL_ONLYBIG) */ + message("usbserial_main: Saying hello\n"); + nbytes = write(outfd, g_shortmsg, sizeof(g_shortmsg)); +#endif + + /* Test if the write was successful */ + + if (nbytes < 0) + { + message("usbserial_main: ERROR: write failed: %d\n", errno); +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY + close(infd); +#endif + close(outfd); + return 4; + } + message("usbserial_main: %d bytes sent\n", nbytes); +#endif /* CONFIG_EXAMPLES_USBSERIAL_OUTONLY */ + + /* Test OUT (host-to-device) messages */ + +#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY + /* Poll for incoming messages */ + + message("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) + { + message("usbserial_main: ERROR: read failed: %d\n", errno); + close(infd); +#ifndef CONFIG_EXAMPLES_USBSERIAL_OUTONLY + close(outfd); +#endif + return 6; + } + } + else + { + message("usbserial_main: Received %d bytes:\n", nbytes); + if (nbytes > 0) + { + for (j = 0; j < nbytes; j += 16) + { + message("usbserial_main: %03x: ", j); + for (k = 0; k < 16; k++) + { + if (k == 8) + { + message(" "); + } + if (j+k < nbytes) + { + message("%02x", g_iobuffer[j+k]); + } + else + { + message(" "); + } + } + message(" "); + for (k = 0; k < 16; k++) + { + if (k == 8) + { + message(" "); + } + if (j+k < nbytes) + { + if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f) + { + message("%c", g_iobuffer[j+k]); + } + else + { + message("."); + } + } + else + { + message(" "); + } + } + message("\n"); + } + } + } + sleep(1); + } +#else /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ + message("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; +} + diff --git a/apps/netutils/discover/discover.c b/apps/netutils/discover/discover.c index 67df02b3f..8e2612dbb 100644 --- a/apps/netutils/discover/discover.c +++ b/apps/netutils/discover/discover.c @@ -1,5 +1,5 @@ /**************************************************************************** - * examples/discover/main.c + * netutils/discover/discover.c * * Copyright (C) 2012 Max Holtzberg. All rights reserved. * Copyright (C) 2008, 2011-2012 Gregory Nutt. All rights reserved. diff --git a/apps/system/i2c/README.txt b/apps/system/i2c/README.txt index 97801bcaa..7b432533b 100755 --- a/apps/system/i2c/README.txt +++ b/apps/system/i2c/README.txt @@ -211,7 +211,7 @@ COMMAND SUMMARY =============== We have already seen the I2C help (or ?) commands above. This section will -discusse the remaining commands. +discuss the remaining commands. List buses: bus [OPTIONS] -------------------------- -- cgit v1.2.3