Completes coding of the PWM module

git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@4200 7fd9a85b-ad96-42d3-883c-3090e2eb8679
This commit is contained in:
patacongo 2011-12-19 19:24:09 +00:00
commit add995c32e
4394 changed files with 1077774 additions and 0 deletions

148
apps/ChangeLog.txt Executable file
View File

@ -0,0 +1,148 @@
5.19 2011-03-12 Gregory Nutt <gnutt@nuttx.org>
* Initial version of the apps/ directory was released as contributed by
Uros Platise.
6.0 2011-03-21 Gregory Nutt <gnutt@nuttx.org>
* README.txt -- README cosmetics
* hello/ -- hello world minor changes
* Makefile -- Makefile cosmetics (I am slowly adding the Darjeeling JVM)
* Make.defs -- New file adds common make definitions for applications.
* hello/Makefile -- Now uses new Make.defs definitions. Added README.txt.
* apps/poweroff -- New application to turn off board power.
* Moved NSH library, netutils, and examples from the nuttx/ directory to
the apps/ directory
* Moved exec_nuttapp machinery into the nuttapp/ directory.
6.1 2011-04-10 Gregory Nutt <gnutt@nuttx.org>
* Creation of auto-generated header files now occurs during the context
build phase.
* Added sdcard insert and eject, nsh command '?' and some code remarks
* Renamed nuttapp to namedapp
* namedapp/binfs.c -- Create a tiny filesystem that can be used
to show the internal named apps under /bin.
* Numerous fixes to build system required to support building with native
Windows toolchain.
6.2 2011-05-06 Gregory Nutt <gnutt@nuttx.org>
* apps/examples/nxffs: Add a test a a configuration that will be used to
verify NXFFS.
6.3 2011-05-15 Gregory Nutt <gnutt@nuttx.org>
* apps/interpreter: Add a directory to hold interpreters. The Pascal add-
on module now installs and builds under this directory.
* apps/interpreter/ficl: Added logic to build Ficl (the "Forth Inspired
Command Language"). See http://ficl.sourceforge.net/.
* apps/netutils/dhcpc, dhcpcd, and tftp. If these directories are included
in the configuration but CONFIG_NET_UDP is disable (which is not very wise),
then a make error occurs because tools/mkdep.sh is called with no files.
* system/free: Move Uros' custom free command from vsn/free
* system/install: Add a new install command submitted by Uros Platise.
* examples/rgmp. Add a placeholder for an RGMP build example.
RGMP is a project for running GPOS and RTOS simultaneously on
multi-processor platforms. See http://rgmp.sourceforge.net/wiki/index.php/Main_Page
for further information about RGMP. NOTE: This is an empty example
on initial check-in.
6.4 2011-06-06 Gregory Nutt <gnutt@nuttx.org>
* nshlib/nsh_netcmds.c: If a network device name and IP address are provided
with the ifconfig command, then this command will now set the network address.
(Contributed by Yu Qiang).
* netutils/ftpc: A library to support client-side FTP.
* examples/ftpc: A simple add-on to the NSH. From NSH, you can start
this simple FTP shell to transfer files to/from a remote FTP server.
6.5 2011-06-21 Gregory Nutt <gnutt@nuttx.org>
* netutils/ftpc: Simpflication and size reduction.
6.6 2011-07-11 Gregory Nutt <gnutt@nuttx.org>
* Make.defs, namedapp/namedapp.c: Several structural changes made to get a
clean compile under the ez80 ZDS-II toolchain (no design changes).
* apps/examples/buttons: Add a test for the new standardized button interfaces
* apps/examples/nxtext: Add another NX graphics test. This one focus on
placing text on the background while pop-up windows occur. Text should
continue to update normally with or without the popup windows present.
6.7 2011-08-02 Gregory Nutt <gnutt@nuttx.org>
* apps/examples/nx and nxtext: These examples can now be built as NSH
"built-in" commands.
* apps/examples/nxhello: The simplest graphics example: It just says
"Hello, World!" in the center of the display. This example can also be
built as an NSH "built-in" command.
* apps/examples/nx, ntext, and nxhello: All updated to use the new
NuttX font interfaces.
* apps/examples/nximage: Another super simple graphics example: It just puts
the NuttX logo in the center of the display. This example can also be
built as an NSH "built-in" command.
* apps/examples/usbstorage: Can now be built as two NSH "built-in" commands:
'msconn' will connect the USB mass storage device; 'msdis' will disconnect
the USB storage device.
* apps/examples/nx*: All NX header files moved from nuttx/include/nuttx to
nuttx/include/nuttx/nx.
* apps/examples/usbstorage: Added instrumentation to monitor memory usage
to check for memory leaks in the USB storage driver.
* apps/examples/nxhello/nxhello_bkgd.c: Fix handling of allocated glyph
memory.
6.8 2011-08-11 Gregory Nutt <gnutt@nuttx.org>
* apps/examples/nxlines: Added a test for NX line drawing capabilities.
6.9 2011-09-11 Gregory Nutt <gnutt@nuttx.org>
* apps/examples/nxlines: Extend the line drawing text to include drawing
of circles.
* apps/system/i2c: Add an I2C test tool that should help to bring up I2C
devices (when it is fully functional).
* apps/nshlib/nsh_timcmds.c: Add the date command that can be used to
show or set the time (only if CONFIG_RTC is set).
6.10 2011-10-06 Gregory Nutt <gnutt@nuttx.org>
* apps/system/i2c: Add repitition and address auto-incrementing so that
and command can be executed numerous times. Add a new verify command
that will write to a register, read from register, and verify that
returned value.
* apps/graphics/tiff: Add a library that can be used to create TIFF files.
* apps/examples/tiff: Add a unit test for the TIFF file creation logic
* apps/examples/lcdrw: Add a test to verify if you can or can or read
data from an LCD correctly.
* apps/examples/usbterm: A USB terminal example.. more of a USB chat or
serial bridge: Data received on local console echoed via USB serial;
data received on USB serial is echoed on the local console.
* apps/examples/touchscreen: Add a simple, generic test for any
touschscreen driver.
* Makefile: The apps/ Makefile now checks for an apps/external directory
or symbolic link. If such a directory/link exists (and has a Makefile),
it will be added to the apps/ build. This allows external directories
to be included into the apps/ build by simply creating a symbolic link.
6.11 2011-11-12 Gregory Nutt <gnutt@nuttx.org>
(No major changes from 6.10)
6.12 2011-12-06 Gregory Nutt <gnutt@nuttx.org>
* apps/examples/buttons: The button test can now be executed as an NSH
built in command.
6.13 2012-xx-xx Gregory Nutt <gnutt@nuttx.org>
* apps/examples/dhcpd: May now be built as an NSH built-in application
by setting CONFIG_NSH_BUILTIN_APPS.
* apps/netutils/dhcpd/dhcpd.c: Fix several problems using host order address
where network addresses expected (and vice versa).
* apps/examples/nettest: May now be built as an NSH built-in application
by setting CONFIG_NSH_BUILTIN_APPS.
* apps/examples/nettest: Correct some build issues with the nettest is
built for performance evaluation.
* apps/examples/adc: Add a very simple test to drive and test an ADC
driver.

41
apps/Make.defs Executable file
View File

@ -0,0 +1,41 @@
############################################################################
# apps/Make.defs
# Common make definitions provided to all applications
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# 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.
#
############################################################################
define REGISTER
@echo "Register: $1"
@echo "{ \"$1\", $2, $3, $4 }," >> "$(APPDIR)/namedapp/namedapp_list.h"
@echo "EXTERN int $4(int argc, char *argv[]);" >> "$(APPDIR)/namedapp/namedapp_proto.h"
endef

132
apps/Makefile Normal file
View File

@ -0,0 +1,132 @@
############################################################################
# apps/Makefile
#
# Copyright (C) 2011 Uros Platise. All rights reserved.
# Authors: Uros Platise <uros.platise@isotel.eu>
# Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/Make.defs
APPDIR = ${shell pwd}
# Application Directories
# CONFIGURED_APPS is the list of all configured built-in directories/built
# action. It is created by the configured appconfig file (a copy of which
# appears in this directory as .config)
# SUBDIRS is the list of all directories containing Makefiles. It is used
# only for cleaning. namedapp must always be the first in the list. This
# list can be extended by the .config file as well
CONFIGURED_APPS =
SUBDIRS = examples graphics interpreters namedapp nshlib netutils system vsn
-include .config
# INSTALLED_APPS is the list of currently available application directories. It
# is the same as CONFIGURED_APPS, but filtered to exclude any non-existent
# application directory. namedapp is always in the list of applications to be
# built.
INSTALLED_APPS = namedapp
# Create the list of available applications (INSTALLED_APPS)
define ADD_BUILTIN
INSTALLED_APPS += ${shell if [ -r $1/Makefile ]; then echo "$1"; fi}
endef
$(foreach BUILTIN, $(CONFIGURED_APPS), $(eval $(call ADD_BUILTIN,$(BUILTIN))))
# The external/ directory may also be added to the INSTALLED_APPS. But there
# is no external/ directory in the repository. Rather, this directory may be
# provided by the user (possibly as a symbolic link) to add libraries and
# applications to the standard build from the repository.
INSTALLED_APPS += ${shell if [ -r external/Makefile ]; then echo "external"; fi}
SUBDIRS += ${shell if [ -r external/Makefile ]; then echo "external"; fi}
# The final build target
BIN = libapps$(LIBEXT)
# Build targets
all: $(BIN)
.PHONY: $(INSTALLED_APPS) context depend clean distclean
$(INSTALLED_APPS):
@$(MAKE) -C $@ TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)";
$(BIN): $(INSTALLED_APPS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $@, $${obj}); \
done ; )
.context:
@for dir in $(INSTALLED_APPS) ; do \
rm -f $$dir/.context ; \
$(MAKE) -C $$dir TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" context ; \
done
@touch $@
context: .context
.depend: context Makefile $(SRCS)
@for dir in $(INSTALLED_APPS) ; do \
rm -f $$dir/.depend ; \
$(MAKE) -C $$dir TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" depend ; \
done
@touch $@
depend: .depend
clean:
@for dir in $(SUBDIRS) ; do \
$(MAKE) -C $$dir clean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
@rm -f $(BIN) *~ .*.swp *.o
$(call CLEAN)
distclean: clean
@for dir in $(SUBDIRS) ; do \
$(MAKE) -C $$dir distclean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
@rm -f .config .context .depend
@( if [ -e external ]; then \
echo "********************************************************"; \
echo "* The external directory/link must be removed manually *"; \
echo "********************************************************"; \
fi; \
)

146
apps/README.txt Normal file
View File

@ -0,0 +1,146 @@
Application Folder
==================
Contents
--------
General
Directory Location
Named Applications
Named Startup main() function
NuttShell (NSH) Built-In Commands
Synchronous Built-In Commands
Application Configuration File
Example Named Application
General
-------
This folder provides various applications found in sub-directories. These
applications are not inherently a part of NuttX but are provided you help
you develop your own applications. The apps/ directory is a "break away"
part of the configuration that you may chose to use or not.
Directory Location
------------------
The default application directory used by the NuttX build should be named
apps/ (or apps-x.y/ where x.y is the NuttX version number). This apps/
directoy should appear in the directory tree at the same level as the
NuttX directory. Like:
.
|- nuttx
|
`- apps
If all of the above conditions are TRUE, then NuttX will be able to
find the application directory. If your application directory has a
different name or is location at a different position, then you will
have to inform the NuttX build system of that location. There are several
ways to do that:
1) You can define CONFIG_APPS_DIR to be the full path to your application
directory in the NuttX configuration file.
2) You can provide the path to the application directory on the command line
like: make APPDIR=<path> or make CONFIG_APPS_DIR=<path>
3) When you configure NuttX using tools/configure.sh, you can provide that
path to the application directory on the configuration command line
like: ./configure.sh -a <app-dir> <board-name>/<config-name>
Named Applications
------------------
NuttX also supports applications that can be started using a name string.
In this case, zpplication entry points with their requirements are gathered
together in two files:
- namedapp/namedapp_proto.h Entry points, prototype function
- namedapp/namedapp_list.h Application specific information and requirements
The build occurs in several phases as different build targets are executed:
(1) context, (2) depend, and (3) default (all). Application information is
collected during the make context build phase.
To execute an application function:
exec_namedapp() is defined in the nuttx/include/apps/apps.h
NuttShell (NSH) Built-In Commands
---------------------------------
One use of named applications is to provide a way of invoking your custom
application through the NuttShell (NSH) command line. NSH will support
a seamless method invoking the applications, when the following option is
enabled in the NuttX configuration file:
CONFIG_NSH_BUILTIN_APPS=y
Applications registered in the apps/namedapp/namedapp_list.h file will then
be accessible from the NSH command line. If you type 'help' at the NSH
prompt, you will see a list of the registered commands.
Synchronous Built-In Commands
-----------------------------
By default, built-in commands started from the NSH command line will run
asynchronously with NSH. If you want to force NSH to execute commands
then wait for the command to execute, you can enable that feature by
adding the following to the NuttX configuration file:
CONFIG_SCHED_WAITPID=y
The configuration option enables support for the waitpid() RTOS interface.
When that interface is enabled, NSH will use it to wait, sleeping until
the built-in command executes to completion.
Of course, even with CONFIG_SCHED_WAITPID=y defined, specific commands
can still be forced to run asynchronously by adding the ampersand (&)
after the NSH command.
Application Configuration File
------------------------------
A special configuration file is used to configure which applications
are to be included in the build. The source for this file is
configs/<board>/<configuration>/appconfig. The existence of the appconfig
file in the board configuration directory is sufficient to enable building
of applications.
The appconfig file is copied into the apps/ directory as .config when
NuttX is configured. .config is included in the toplevel apps/Makefile.
As a minimum, this configuration file must define files to add to the
CONFIGURED_APPS list like:
CONFIGURED_APPS += vsn/hello vsn/poweroff vsn/jvm
Named Start-Up main() function
------------------------------
A named application can even be used as the main, start-up entry point
into your embedded software. When the user defines this option in
the NuttX configuration file:
CONFIG_BUILTIN_APP_START=<application name>
that application shall be invoked immediately after system starts
*instead* of the normal, default "user_start" entry point.
Note that <application name> must be provided as: "hello",
will call:
int hello_main(int argc, char *argv[])
Example Named Application
-------------------------
An example application skeleton can be found under the vsn/hello
sub-directory. This example shows how a named application can be added
to the project. One must define:
1. create sub-directory as: appname
2. provide entry point: appname_main()
3. set the requirements in the file: Makefile, specially the lines:
APPNAME = appname
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 768
ASRCS = asm source file list as a.asm b.asm ...
CSRCS = C source file list as foo1.c foo2.c ..
4. add application in the apps/.config

118
apps/examples/Makefile Normal file
View File

@ -0,0 +1,118 @@
############################################################################
# apps/examples/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
# Sub-directories
SUBDIRS = adc buttons dhcpd ftpc hello helloxx hidkbd igmp lcdrw mm mount \
nettest nsh null nx nxffs nxflat nxhello nximage nxlines \
nxtext ostest pashello pipe poll rgmp romfs sendmail serloop \
thttpd tiff touchscreen udp uip usbserial usbstorage usbterm wget wlan
# Sub-directories that might need context setup
CNTXTDIRS =
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
CNTXTDIRS += adc
endif
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
CNTXTDIRS += dhcpd
endif
ifeq ($(CONFIG_EXAMPLES_HELLOXX_BUILTIN),y)
CNTXTDIRS += helloxx
endif
ifeq ($(CONFIG_EXAMPLES_LCDRW_BUILTIN),y)
CNTXTDIRS += lcdrw
endif
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
CNTXTDIRS += nettest
endif
ifeq ($(CONFIG_EXAMPLES_NX_BUILTIN),y)
CNTXTDIRS += nx
endif
ifeq ($(CONFIG_EXAMPLES_NXHELLO_BUILTIN),y)
CNTXTDIRS += nxhello
endif
ifeq ($(CONFIG_EXAMPLES_NXIMAGE_BUILTIN),y)
CNTXTDIRS += nximage
endif
ifeq ($(CONFIG_EXAMPLES_LINES_BUILTIN),y)
CNTXTDIRS += nxlines
endif
ifeq ($(CONFIG_EXAMPLES_NXTEXT_BUILTIN),y)
CNTXTDIRS += nxtext
endif
ifeq ($(CONFIG_EXAMPLES_TIFF_BUILTIN),y)
CNTXTDIRS += tiff
endif
ifeq ($(CONFIG_EXAMPLES_TOUCHSCREEN_BUILTIN),y)
CNTXTDIRS += touchscreen
endif
ifeq ($(CONFIG_EXAMPLES_USBSTRG_BUILTIN),y)
CNTXTDIRS += usbstorage
endif
ifeq ($(CONFIG_EXAMPLES_USBTERM_BUILTIN),y)
CNTXTDIRS += usbterm
endif
all: nothing
.PHONY: nothing context depend clean distclean
nothing:
context:
@for dir in $(CNTXTDIRS) ; do \
$(MAKE) -C $$dir context TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
depend:
@for dir in $(SUBDIRS) ; do \
$(MAKE) -C $$dir depend TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
clean:
@for dir in $(SUBDIRS) ; do \
$(MAKE) -C $$dir clean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
distclean: clean
@for dir in $(SUBDIRS) ; do \
$(MAKE) -C $$dir distclean TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"; \
done
-include Make.dep

1140
apps/examples/README.txt Normal file

File diff suppressed because it is too large Load Diff

105
apps/examples/adc/Makefile Normal file
View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/touchscreen/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = adc_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Touchscreen built-in application info
APPNAME = adc
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

116
apps/examples/adc/adc.h Executable file
View File

@ -0,0 +1,116 @@
/****************************************************************************
* examples/examples/adc.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_ADC_ADC_H
#define __APPS_EXAMPLES_ADC_ADC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* CONFIG_NSH_BUILTIN_APPS - Build the ADC test as an NSH built-in function.
* Default: Built as a standalone problem
* CONFIG_EXAMPLES_ADC_DEVPATH - The path to the ADC device. Default: /dev/adc0
* CONFIG_EXAMPLES_ADC_NSAMPLES - If CONFIG_NSH_BUILTIN_APPS
* is defined, then the number of samples is provided on the command line
* and this value is ignored. Otherwise, this number of samples is
* collected and the program terminates. Default: Samples are collected
* indefinitely.
* CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
* Default: 4
*/
#ifndef CONFIG_ADC
# error "ADC device support is not enabled (CONFIG_ADC)"
#endif
#ifndef CONFIG_EXAMPLES_ADC_DEVPATH
# define CONFIG_EXAMPLES_ADC_DEVPATH "/dev/adc0"
#endif
#ifndef CONFIG_EXAMPLES_ADC_GROUPSIZE
# define CONFIG_EXAMPLES_ADC_GROUPSIZE 4
#endif
/* Debug ********************************************************************/
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG
# define message(...) lib_rawprintf(__VA_ARGS__)
# define msgflush()
# else
# define message(...) printf(__VA_ARGS__)
# define msgflush() fflush(stdout)
# endif
#else
# ifdef CONFIG_DEBUG
# define message lib_rawprintf
# define msgflush()
# else
# define message printf
# define msgflush() fflush(stdout)
# endif
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Variables
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: adc_devinit()
*
* Description:
* Perform architecuture-specific initialization of the ADC hardware. This
* interface must be provided by all configurations using apps/examples/adc
*
****************************************************************************/
int adc_devinit(void);
#endif /* __APPS_EXAMPLES_ADC_ADC_H */

View File

@ -0,0 +1,215 @@
/****************************************************************************
* examples/adc/adc_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/analog/adc.h>
#include "adc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_NSH_BUILTIN_APPS
# define MAIN_NAME adc_main
# define MAIN_STRING "adc_main: "
#else
# define MAIN_NAME user_start
# define MAIN_STRING "user_start: "
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/adc_main
****************************************************************************/
int MAIN_NAME(int argc, char *argv[])
{
struct adc_msg_s sample[CONFIG_EXAMPLES_ADC_GROUPSIZE];
size_t readsize;
ssize_t nbytes;
#if defined(CONFIG_NSH_BUILTIN_APPS) || defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
long nsamples;
#endif
int fd;
int errval = 0;
int ret;
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
*/
#if defined(CONFIG_NSH_BUILTIN_APPS)
nsamples = 1;
if (argc > 1)
{
nsamples = strtol(argv[1], NULL, 10);
}
message(MAIN_STRING "nsamples: %d\n", nsamples);
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
message(MAIN_STRING "nsamples: %d\n", CONFIG_EXAMPLES_ADC_NSAMPLES);
#endif
/* Initialization of the ADC hardware is performed by logic external to
* this test.
*/
message(MAIN_STRING "Initializing external ADC device\n");
ret = adc_devinit();
if (ret != OK)
{
message(MAIN_STRING "adc_devinit failed: %d\n", ret);
errval = 1;
goto errout;
}
message(MAIN_STRING "Hardware initialized. Opening the ADC device\n");
/* Open the ADC device for reading */
fd = open(CONFIG_EXAMPLES_ADC_DEVPATH, O_RDONLY);
if (fd < 0)
{
message(MAIN_STRING "open %s failed: %d\n",
CONFIG_EXAMPLES_ADC_DEVPATH, errno);
errval = 2;
goto errout_with_dev;
}
/* Now loop the appropriate number of times, displaying the collected
* ADC samples.
*/
message(MAIN_STRING "Entering the main loop\n");
#if defined(CONFIG_NSH_BUILTIN_APPS)
for (; nsamples > 0; nsamples--)
#elif defined(CONFIG_EXAMPLES_ADC_NSAMPLES)
for (nsamples = 0; nsamples < CONFIG_EXAMPLES_ADC_NSAMPLES; nsamples++)
#else
for (;;)
#endif
{
/* Flush any output before the loop entered or from the previous pass
* through the loop.
*/
msgflush();
/* Read CONFIG_EXAMPLES_ADC_GROUPSIZE samples */
readsize = CONFIG_EXAMPLES_ADC_GROUPSIZE * sizeof(struct adc_msg_s);
nbytes = read(fd, sample, readsize);
message("Bytes read: %d\n", nbytes);
/* Handle unexpected return values */
if (nbytes < 0)
{
errval = errno;
if (errval != EINTR)
{
message(MAIN_STRING "read %s failed: %d\n",
CONFIG_EXAMPLES_ADC_DEVPATH, errval);
errval = 3;
goto errout_with_dev;
}
message(MAIN_STRING "Interrupted read...\n");
}
else if (nbytes != readsize)
{
message(MAIN_STRING "Unexpected read size=%d, expected=%d, Ignoring\n",
nbytes, readsize);
}
/* Print the sample data on successful return */
else
{
message("Sample :\n");
for (i = 0; i < CONFIG_EXAMPLES_ADC_GROUPSIZE; i++)
{
message("%d: channel: %d value: %d\n",
i, sample[i].am_channel, sample[i].am_data);
}
}
}
errout_with_dev:
close(fd);
errout:
message("Terminating!\n");
msgflush();
return errval;
}

View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/buttons/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Hello, World! Example
ASRCS =
CSRCS = main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Buttons built-in application info
APPNAME = buttons
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,505 @@
/****************************************************************************
* examples/buttons/main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <nuttx/arch.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <debug.h>
/****************************************************************************
* 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)
/* Is this being built as an NSH built-in application? */
#ifdef CONFIG_NSH_BUILTIN_APPS
# define MAIN_NAME buttons_main
# define MAIN_STRING "buttons_main: "
#else
# define MAIN_NAME user_start
# define MAIN_STRING "user_start: "
#endif
/****************************************************************************
* 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
****************************************************************************/
/****************************************************************************
* user_start/buttons_main
****************************************************************************/
int MAIN_NAME(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
/* 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;
}

View File

@ -0,0 +1,106 @@
############################################################################
# apps/examples/dhcpd/Makefile
#
# Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# DHCP Daemon Example
ASRCS =
CSRCS = target.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# DHCPD built-in application info
APPNAME = dhcpd
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,62 @@
############################################################################
# apps/examples/dhcpd/Makefile.host
#
# Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# 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.
#
############################################################################
# TOPDIR must be defined on the make command line
include $(TOPDIR)/Make.defs
OBJS = host.o1 dhcpd.o1
BIN = dhcpd
HOSTCFLAGS += -DCONFIG_NETUTILS_DHCPD_HOST=1
HOSTCFLAGS += -DCONFIG_NETUTILS_DHCPD_INTERFACE=\"eth1\"
HOSTCFLAGS += -DHAVE_SO_REUSEADDR=1
HOSTCFLAGS += -DHAVE_SO_BROADCAST=1
VPATH = $(TOPDIR)/netutils/dhcpd:.
all: $(BIN)
.PHONY: clean context clean_context distclean
$(OBJS): %.o1: %.c
$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
$(BIN): $(OBJS)
$(HOSTCC) $(HOSTLDFLAGS) $^ -o $@
clean:
@rm -f $(BIN).* *.o1 *~

View File

@ -0,0 +1,58 @@
/****************************************************************************
* examples/dhcpd/host.c
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name 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
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
extern int dhcpd_run(void);
/****************************************************************************
* main
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
dhcpd_run();
return 0;
}

View File

@ -0,0 +1,142 @@
/****************************************************************************
* examples/dhcpd/target.c
*
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name 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 <nuttx/config.h>
#include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <apps/netutils/uiplib.h>
#include <apps/netutils/dhcpd.h>
/****************************************************************************
* Preprocessor Definitions
****************************************************************************/
/* Configuation Checkes *****************************************************/
/* BEWARE:
* There are other configuration settings needed in netutils/dhcpd/dhcpdc.c,
* but there are default values for those so we cannot check them here.
*/
#ifndef CONFIG_EXAMPLE_DHCPD_IPADDR
# error "You must define CONFIG_EXAMPLE_DHCPD_IPADDR"
#endif
#ifndef CONFIG_EXAMPLE_DHCPD_DRIPADDR
# error "You must define CONFIG_EXAMPLE_DHCPD_DRIPADDR"
#endif
#ifndef CONFIG_EXAMPLE_DHCPD_NETMASK
# error "You must define CONFIG_EXAMPLE_DHCPD_NETMASK"
#endif
#ifndef CONFIG_NET
# error "You must define CONFIG_NET"
#endif
#ifndef CONFIG_NET_UDP
# error "You must define CONFIG_NET_UDP"
#endif
#ifndef CONFIG_NET_BROADCAST
# error "You must define CONFIG_NET_BROADCAST"
#endif
/* If CONFIG_NSH_BUILTIN_APPS is defined, then it is assumed that you want
* to execute the DHCPD daemon as an NSH built-in task.
*/
#ifdef CONFIG_NSH_BUILTIN_APPS
# define MAIN_NAME dhcpd_main
# define MAIN_NAME_STRING "dhcpd_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Name: user_start/dhcpd_main
****************************************************************************/
int MAIN_NAME(int argc, char *argv[])
{
struct in_addr addr;
#if defined(CONFIG_EXAMPLE_DHCPD_NOMAC)
uint8_t mac[IFHWADDRLEN];
#endif
/* Many embedded network interfaces must have a software assigned MAC */
#ifdef CONFIG_EXAMPLE_DHCPD_NOMAC
mac[0] = 0x00;
mac[1] = 0xe0;
mac[2] = 0xb0;
mac[3] = 0x0b;
mac[4] = 0xba;
mac[5] = 0xbe;
uip_setmacaddr("eth0", mac);
#endif
/* Set up our host address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_DHCPD_IPADDR);
uip_sethostaddr("eth0", &addr);
/* Set up the default router address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_DHCPD_DRIPADDR);
uip_setdraddr("eth0", &addr);
/* Setup the subnet mask */
addr.s_addr = HTONL(CONFIG_EXAMPLE_DHCPD_NETMASK);
uip_setnetmask("eth0", &addr);
/* Then start the server */
dhcpd_run();
return 0;
}

105
apps/examples/ftpc/Makefile Normal file
View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/ftpc/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# FTPC Client Application
APPNAME = ftpc
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 4096
ASRCS =
CSRCS = ftpc_main.c ftpc_cmds.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: context depend clean distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
# Register application
.context:
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
context: .context
# Create dependencies
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f .context Make.dep .depend
-include Make.dep

112
apps/examples/ftpc/ftpc.h Normal file
View File

@ -0,0 +1,112 @@
/****************************************************************************
* apps/examples/ftpc/ftpc.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_FTPC_FTPC_H
#define __APPS_EXAMPLES_FTPC_FTPC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <errno.h>
#include <apps/ftpc.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Maximum size of one command line */
#ifndef CONFIG_FTPC_LINELEN
# define CONFIG_FTPC_LINELEN 80
#endif
/* If CONFIG_STDIO_LINEBUFFER is defined, the STDIO buffer will be flushed
* on each new line. Otherwise, STDIO needs to be explicitly flushed to
* see the output in context.
*/
#if CONFIG_NFILE_DESCRIPTORS > 0 && CONFIG_NFILE_STREAMS > 0 && \
CONFIG_STDIO_BUFFER_SIZE > 0 && !defined(CONFIG_STDIO_LINEBUFFER)
# define FFLUSH() fflush(stdout)
#else
# define FFLUSH()
#endif
/****************************************************************************
* Public Types
****************************************************************************/
typedef int (*cmd_t)(SESSION handle, int argc, char **argv);
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/* FTP command handlers */
extern int cmd_rlogin(SESSION handle, int argc, char **argv);
extern int cmd_rquit(SESSION handle, int argc, char **argv);
extern int cmd_rchdir(SESSION handle, int argc, char **argv);
extern int cmd_rpwd(SESSION handle, int argc, char **argv);
extern int cmd_rcdup(SESSION handle, int argc, char **argv);
extern int cmd_rmkdir(SESSION handle, int argc, char **argv);
extern int cmd_rrmdir(SESSION handle, int argc, char **argv);
extern int cmd_runlink(SESSION handle, int argc, char **argv);
extern int cmd_rchmod(SESSION handle, int argc, char **argv);
extern int cmd_rrename(SESSION handle, int argc, char **argv);
extern int cmd_rsize(SESSION handle, int argc, char **argv);
extern int cmd_rtime(SESSION handle, int argc, char **argv);
extern int cmd_ridle(SESSION handle, int argc, char **argv);
extern int cmd_rnoop(SESSION handle, int argc, char **argv);
extern int cmd_rhelp(SESSION handle, int argc, char **argv);
extern int cmd_rls(SESSION handle, int argc, char **argv);
extern int cmd_rget(SESSION handle, int argc, char **argv);
extern int cmd_rput(SESSION handle, int argc, char **argv);
#endif /* __APPS_EXAMPLES_FTPC_FTPC_H */

398
apps/examples/ftpc/ftpc_cmds.c Executable file
View File

@ -0,0 +1,398 @@
/****************************************************************************
* examples/ftpc/ftpc_cmds.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <apps/ftpc.h>
#include "ftpc.h"
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_rlogin
****************************************************************************/
int cmd_rlogin(SESSION handle, int argc, char **argv)
{
struct ftpc_login_s login = {NULL, NULL, NULL, true};
login.uname = argv[1];
if (argc > 2)
{
login.pwd = argv[2];
}
return ftpc_login(handle, &login);
}
/****************************************************************************
* Name: cmd_rquit
****************************************************************************/
int cmd_rquit(SESSION handle, int argc, char **argv)
{
int ret = ftpc_quit(handle);
if (ret < 0)
{
printf("quit failed: %d\n", errno);
}
printf("Exitting...\n");
exit(0);
return ERROR;
}
/****************************************************************************
* Name: cmd_rchdir
****************************************************************************/
int cmd_rchdir(SESSION handle, int argc, char **argv)
{
return ftpc_chdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_rpwd
****************************************************************************/
int cmd_rpwd(SESSION handle, int argc, char **argv)
{
FAR char *pwd = ftpc_rpwd(handle);
if (pwd)
{
printf("PWD: %s\n", pwd);
free(pwd);
return OK;
}
return ERROR;
}
/****************************************************************************
* Name: cmd_rcdup
****************************************************************************/
int cmd_rcdup(SESSION handle, int argc, char **argv)
{
return ftpc_cdup(handle);
}
/****************************************************************************
* Name: cmd_rmkdir
****************************************************************************/
int cmd_rmkdir(SESSION handle, int argc, char **argv)
{
return ftpc_mkdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_rrmdir
****************************************************************************/
int cmd_rrmdir(SESSION handle, int argc, char **argv)
{
return ftpc_rmdir(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_runlink
****************************************************************************/
int cmd_runlink(SESSION handle, int argc, char **argv)
{
return ftpc_unlink(handle, argv[1]);
}
/****************************************************************************
* Name: cmd_rchmod
****************************************************************************/
int cmd_rchmod(SESSION handle, int argc, char **argv)
{
return ftpc_chmod(handle, argv[1], argv[2]);
}
/****************************************************************************
* Name: cmd_rrename
****************************************************************************/
int cmd_rrename(SESSION handle, int argc, char **argv)
{
return ftpc_rename(handle, argv[1], argv[2]);
}
/****************************************************************************
* Name: cmd_rsize
****************************************************************************/
int cmd_rsize(SESSION handle, int argc, char **argv)
{
off_t size = ftpc_filesize(handle, argv[1]);
printf("SIZE: %lu\n", size);
return OK;
}
/****************************************************************************
* Name: cmd_rtime
****************************************************************************/
int cmd_rtime(SESSION handle, int argc, char **argv)
{
time_t filetime = ftpc_filetime(handle, argv[1]);
printf("TIME: %lu\n", (long)filetime);
return OK;
}
/****************************************************************************
* Name: cmd_ridle
****************************************************************************/
int cmd_ridle(SESSION handle, int argc, char **argv)
{
unsigned int idletime = 0;
if (argc > 1)
{
idletime = atoi(argv[1]);
}
return ftpc_idle(handle, idletime);
}
/****************************************************************************
* Name: cmd_rnoop
****************************************************************************/
int cmd_rnoop(SESSION handle, int argc, char **argv)
{
return ftpc_noop(handle);
}
/****************************************************************************
* Name: cmd_rhelp
****************************************************************************/
int cmd_rhelp(SESSION handle, int argc, char **argv)
{
FAR const char *cmd = NULL;
int ret;
if (argc > 1)
{
cmd = argv[1];
}
ret = ftpc_help(handle, cmd);
if (ret == OK)
{
FAR char *msg = ftpc_response(handle);
puts(msg);
free(msg);
}
return ret;
}
/****************************************************************************
* Name: cmd_rls
****************************************************************************/
int cmd_rls(SESSION handle, int argc, char **argv)
{
FAR struct ftpc_dirlist_s *dirlist;
FAR char *dirname = NULL;
int i;
/* Get the directory listing */
if (argc > 1)
{
dirname = argv[1];
}
dirlist = ftpc_listdir(handle, dirname);
if (!dirlist)
{
return ERROR;
}
/* Print the directory listing */
printf("%s/\n", dirname ? dirname : ".");
for (i = 0; i < dirlist->nnames; i++)
{
printf(" %s\n", dirlist->name[i]);
}
FFLUSH();
/* We are responsible for freeing the directory structure allocated by
* ftpc_listdir().
*/
ftpc_dirfree(dirlist);
return OK;
}
/****************************************************************************
* Name: cmd_rget
****************************************************************************/
int cmd_rget(SESSION handle, int argc, char **argv)
{
FAR const char *rname;
FAR const char *lname = NULL;
int xfrmode = FTPC_XFRMODE_ASCII;
int option;
while ((option = getopt(argc, argv, "ab")) != ERROR)
{
if (option == 'a')
{
xfrmode = FTPC_XFRMODE_ASCII;
}
else if (option == 'b')
{
xfrmode = FTPC_XFRMODE_BINARY;
}
else
{
printf("%s: Unrecognized option: '%c'\n", "put", option);
return ERROR;
}
}
/* There should be one or two parameters remaining on the command line */
if (optind >= argc)
{
printf("%s: Missing required arguments\n", "get");
return ERROR;
}
rname = argv[optind];
optind++;
if (optind < argc)
{
lname = argv[optind];
optind++;
}
if (optind != argc)
{
printf("%s: Too many arguments\n", "get");
return ERROR;
}
/* Perform the transfer */
return ftpc_getfile(handle, rname, lname, FTPC_GET_NORMAL, xfrmode);
}
/****************************************************************************
* Name: cmd_rput
****************************************************************************/
int cmd_rput(SESSION handle, int argc, char **argv)
{
FAR const char *lname;
FAR const char *rname = NULL;
int xfrmode = FTPC_XFRMODE_ASCII;
int option;
while ((option = getopt(argc, argv, "ab")) != ERROR)
{
if (option == 'a')
{
xfrmode = FTPC_XFRMODE_ASCII;
}
else if (option == 'b')
{
xfrmode = FTPC_XFRMODE_BINARY;
}
else
{
printf("%s: Unrecognized option: '%c'\n", "put", option);
return ERROR;
}
}
/* There should be one or two parameters remaining on the command line */
if (optind >= argc)
{
printf("%s: Missing required arguments\n", "get");
return ERROR;
}
lname = argv[optind];
optind++;
if (optind < argc)
{
rname = argv[optind];
optind++;
}
if (optind != argc)
{
printf("%s: Too many arguments\n ");
return ERROR;
}
/* Perform the transfer */
return ftp_putfile(handle, lname, rname, FTPC_PUT_NORMAL, xfrmode);
}

426
apps/examples/ftpc/ftpc_main.c Executable file
View File

@ -0,0 +1,426 @@
/****************************************************************************
* examples/ftpc/ftpc_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <apps/ftpc.h>
#include "ftpc.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define FTPC_MAX_ARGUMENTS 4
/****************************************************************************
* Public Types
****************************************************************************/
struct cmdmap_s
{
const char *cmd; /* Name of the command */
cmd_t handler; /* Function that handles the command */
uint8_t minargs; /* Minimum number of arguments (including command) */
uint8_t maxargs; /* Maximum number of arguments (including command) */
const char *usage; /* Usage instructions for 'help' command */
};
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_delim[] = " \t\n";
static int cmd_lhelp(SESSION handle, int argc, char **argv);
static int cmd_lunrecognized(SESSION handle, int argc, char **argv);
static const struct cmdmap_s g_cmdmap[] =
{
{ "cd", cmd_rchdir, 2, 2, "<directory>" },
{ "chmod", cmd_rchmod, 3, 3, "<permissions> <path>" },
{ "get", cmd_rget, 2, 4, "[-a|b] <rname> [<lname>]" },
{ "help", cmd_lhelp, 1, 2, "" },
{ "idle", cmd_ridle, 1, 2, "[<idletime>]" },
{ "login", cmd_rlogin, 2, 3, "<uname> [<password>]" },
{ "ls", cmd_rls, 1, 2, "[<dirpath>]" },
{ "quit", cmd_rquit, 1, 1, "" },
{ "mkdir", cmd_rmkdir, 2, 2, "<directory>" },
{ "noop", cmd_rnoop, 1, 1, "" },
{ "put", cmd_rput, 2, 4, "[-a|b] <lname> [<rname>]" },
{ "pwd", cmd_rpwd, 1, 1, "" },
{ "rename", cmd_rrename, 3, 3, "<oldname> <newname>" },
{ "rhelp", cmd_rhelp, 1, 2, "[<command>]" },
{ "rm", cmd_runlink, 2, 2, "" },
{ "rmdir", cmd_rrmdir, 2, 2, "<directory>" },
{ "size", cmd_rsize, 2, 2, "<filepath>" },
{ "time", cmd_rtime, 2, 2, "<filepath>" },
{ "up", cmd_rcdup, 1, 1, "" },
{ NULL, NULL, 1, 1, NULL }
};
static char g_line[CONFIG_FTPC_LINELEN];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: cmd_lhelp
****************************************************************************/
static int cmd_lhelp(SESSION handle, int argc, char **argv)
{
const struct cmdmap_s *ptr;
printf("Local FTPC commands:\n");
for (ptr = g_cmdmap; ptr->cmd; ptr++)
{
if (ptr->usage)
{
printf(" %s %s\n", ptr->cmd, ptr->usage);
}
else
{
printf(" %s\n", ptr->cmd);
}
}
return OK;
}
/****************************************************************************
* Name: cmd_lunrecognized
****************************************************************************/
static int cmd_lunrecognized(SESSION handle, int argc, char **argv)
{
printf("Command %s unrecognized\n", argv[0]);
return ERROR;
}
/****************************************************************************
* Name: ftpc_argument
****************************************************************************/
char *ftpc_argument(char **saveptr)
{
char *pbegin = *saveptr;
char *pend = NULL;
const char *term;
/* Find the beginning of the next token */
for (;
*pbegin && strchr(g_delim, *pbegin) != NULL;
pbegin++);
/* If we are at the end of the string with nothing
* but delimiters found, then return NULL.
*/
if (!*pbegin)
{
return NULL;
}
/* Does the token begin with '#' -- comment */
else if (*pbegin == '#')
{
/* Return NULL meaning that we are at the end of the line */
*saveptr = pbegin;
pbegin = NULL;
}
else
{
/* Otherwise, we are going to have to parse to find the end of
* the token. Does the token begin with '"'?
*/
if (*pbegin == '"')
{
/* Yes.. then only another '"' can terminate the string */
pbegin++;
term = "\"";
}
else
{
/* No, then any of the usual terminators will terminate the argument */
term = g_delim;
}
/* Find the end of the string */
for (pend = pbegin + 1;
*pend && strchr(term, *pend) == NULL;
pend++);
/* pend either points to the end of the string or to
* the first delimiter after the string.
*/
if (*pend)
{
/* Turn the delimiter into a null terminator */
*pend++ = '\0';
}
/* Save the pointer where we left off */
*saveptr = pend;
}
/* Return the beginning of the token. */
return pbegin;
}
/****************************************************************************
* Name: ftpc_execute
****************************************************************************/
static int ftpc_execute(SESSION handle, int argc, char *argv[])
{
const struct cmdmap_s *cmdmap;
const char *cmd;
cmd_t handler = cmd_lunrecognized;
int ret;
/* The form of argv is:
*
* argv[0]: The command name. This is argv[0] when the arguments
* are, finally, received by the command handler
* argv[1]: The beginning of argument (up to FTPC_MAX_ARGUMENTS)
* argv[argc]: NULL terminating pointer
*/
cmd = argv[0];
/* See if the command is one that we understand */
for (cmdmap = g_cmdmap; cmdmap->cmd; cmdmap++)
{
if (strcmp(cmdmap->cmd, cmd) == 0)
{
/* Check if a valid number of arguments was provided. We
* do this simple, imperfect checking here so that it does
* not have to be performed in each command.
*/
if (argc < cmdmap->minargs)
{
/* Fewer than the minimum number were provided */
printf("Too few arguments for '%s'\n", cmd);
return ERROR;
}
else if (argc > cmdmap->maxargs)
{
/* More than the maximum number were provided */
printf("Too many arguments for '%s'\n", cmd);
return ERROR;
}
else
{
/* A valid number of arguments were provided (this does
* not mean they are right).
*/
handler = cmdmap->handler;
break;
}
}
}
ret = handler(handle, argc, argv);
if (ret < 0)
{
printf("%s failed: %d\n", cmd, errno);
}
return ret;
}
/****************************************************************************
* Name: ftpc_parse
****************************************************************************/
int ftpc_parse(SESSION handle, char *cmdline)
{
FAR char *argv[FTPC_MAX_ARGUMENTS];
FAR char *saveptr;
FAR char *cmd;
int argc;
int ret;
/* Initialize parser state */
memset(argv, 0, FTPC_MAX_ARGUMENTS*sizeof(FAR char *));
/* Parse out the command at the beginning of the line */
saveptr = cmdline;
cmd = ftpc_argument(&saveptr);
/* Check if any command was provided -OR- if command processing is
* currently disabled.
*/
if (!cmd)
{
/* An empty line is not an error */
return OK;
}
/* Parse all of the arguments following the command name. */
argv[0] = cmd;
for (argc = 1; argc < FTPC_MAX_ARGUMENTS; argc++)
{
argv[argc] = ftpc_argument(&saveptr);
if (!argv[argc])
{
break;
}
}
argv[argc] = NULL;
/* Check if the maximum number of arguments was exceeded */
if (argc > FTPC_MAX_ARGUMENTS)
{
printf("Too many arguments\n");
ret = -EINVAL;
}
else
{
/* Then execute the command */
ret = ftpc_execute(handle, argc, argv);
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int ftpc_main(int argc, char **argv, char **envp)
{
struct ftpc_connect_s connect = {{0}, 0};
SESSION handle;
FAR char *ptr;
if (argc != 2)
{
printf("Usage:\n");
printf(" %s xx.xx.xx.xx[:pp]\n", argv[0]);
printf("Where\n");
printf(" xx.xx.xx.xx is the IP address of the FTP server\n");
printf(" pp is option port to use with the FTP server\n");
exit(1);
}
/* Check if the argument includes a port number */
ptr = strchr(argv[1], ':');
if (ptr)
{
*ptr = '\0';
connect.port = atoi(ptr+1);
}
/* In any event, we can now extract the IP address from the comman-line */
connect.addr.s_addr = inet_addr(argv[1]);
/* Connect to the FTP server */
handle = ftpc_connect(&connect);
if (!handle)
{
printf("Failed to connect to the server: %d\n", errno);
exit(1);
}
/* Present a greeting */
printf("NuttX FTP Client:\n");
FFLUSH();
/* Setting optind to -1 is a non-standard, backdoor way to reinitialize
* getopt(). getopt() is not thread safe and we have no idea what state
* it is in now!
*/
optind = -1;
/* Then enter the command line parsing loop */
for (;;)
{
/* Display the prompt string */
fputs("nfc> ", stdout);
FFLUSH();
/* Get the next line of input */
if (fgets(g_line, CONFIG_FTPC_LINELEN, stdin))
{
/* Parse process the command */
(void)ftpc_parse(handle, g_line);
FFLUSH();
}
}
return 0;
}

View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/hello/Makefile
#
# Copyright (C) 2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Hello, World! Example
ASRCS =
CSRCS = main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,64 @@
/****************************************************************************
* examples/hello/main.c
*
* Copyright (C) 2008, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
printf("Hello, World!!\n");
return 0;
}

View File

@ -0,0 +1,122 @@
############################################################################
# apps/examples/helloxx/Makefile
#
# Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Hello, World! C++ Example
ASRCS =
CSRCS =
CXXSRCS = main.cxx
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
CXXOBJS = $(CXXSRCS:.cxx=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS) $(CXXSRCS)
OBJS = $(AOBJS) $(COBJS) $(CXXOBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# helloxx built-in application info
APPNAME = helloxx
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: clean depend disclean chkcxx
chkcxx:
ifneq ($(CONFIG_HAVE_CXX),y)
@echo ""
@echo "In order to use this example, you toolchain must support must"
@echo ""
@echo " (1) Explicitly select CONFIG_HAVE_CXX to build in C++ support"
@echo " (2) Define CXX, CXXFLAGS, and COMPILEXX in the Make.defs file"
@echo " of the configuration that you are using."
@echo ""
@exit 1
endif
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
$(CXXOBJS): %$(OBJEXT): %.cxx
$(call COMPILEXX, $<, $@)
.built: chkcxx $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_HELLOXX_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,129 @@
//***************************************************************************
// examples/helloxx/main.c
//
// Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
// Author: Gregory Nutt <spudmonkey@racsa.co.cr>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
// 3. Neither the name NuttX nor the names of its contributors may be
// used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
// OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
//***************************************************************************
//***************************************************************************
// Included Files
//***************************************************************************
#include <nuttx/config.h>
#include <nuttx/init.h>
#include <cstdio>
#include <debug.h>
//***************************************************************************
// Definitions
//***************************************************************************
//***************************************************************************
// Private Classes
//***************************************************************************
class CHelloWorld
{
public:
CHelloWorld(void) : mSecret(42) { lldbg("Constructor\n"); };
~CHelloWorld(void) { lldbg("Destructor\n"); };
bool HelloWorld(void)
{
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
//***************************************************************************
#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTATICCONST
static CHelloWorld g_HelloWorld;
#endif
//***************************************************************************
// Public Functions
//***************************************************************************
//***************************************************************************
// user_start
//***************************************************************************
/****************************************************************************
* Name: user_start/nxhello_main
****************************************************************************/
#ifdef CONFIG_EXAMPLES_HELLOXX_BUILTIN
extern "C" int helloxx_main(int argc, char *argv[]);
# define MAIN_NAME helloxx_main
# define MAIN_STRING "helloxx_main: "
#else
# define MAIN_NAME user_start
# define MAIN_STRING "user_start: "
#endif
int MAIN_NAME(int argc, char *argv[])
{
#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST
CHelloWorld HelloWorld;
#endif
CHelloWorld *pHelloWorld = new CHelloWorld;
printf(MAIN_STRING "Saying hello from the dynamically constructed instance\n");
pHelloWorld->HelloWorld();
#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTACKCONST
printf(MAIN_STRING "Saying hello from the instance constructed on the stack\n");
HelloWorld.HelloWorld();
#endif
#ifndef CONFIG_EXAMPLES_HELLOXX_NOSTATICCONST
printf(MAIN_STRING "Saying hello from the statically constructed instance\n");
g_HelloWorld.HelloWorld();
#endif
delete pHelloWorld;
return 0;
}

View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/hidkbd/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# USB Host HID keyboard Example
ASRCS =
CSRCS = hidkbd_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,231 @@
/****************************************************************************
* examples/hidkbd/null_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <errno.h>
#include <nuttx/usb/usbhost.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Sanity checking */
#ifndef CONFIG_USBHOST
# error "CONFIG_USBHOST is not defined"
#endif
#ifdef CONFIG_USBHOST_INT_DISABLE
# error "Interrupt endpoints are disabled (CONFIG_USBHOST_INT_DISABLE)"
#endif
#ifndef CONFIG_NFILE_DESCRIPTORS
# error "CONFIG_NFILE_DESCRIPTORS > 0 needed"
#endif
/* Provide some default values for other configuration settings */
#ifndef CONFIG_EXAMPLES_HIDKBD_DEFPRIO
# define CONFIG_EXAMPLES_HIDKBD_DEFPRIO 50
#endif
#ifndef CONFIG_EXAMPLES_HIDKBD_STACKSIZE
# define CONFIG_EXAMPLES_HIDKBD_STACKSIZE 1024
#endif
#ifndef CONFIG_EXAMPLES_HIDKBD_DEVNAME
# define CONFIG_EXAMPLES_HIDKBD_DEVNAME "/dev/kbda"
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static struct usbhost_driver_s *g_drvr;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: hidkbd_waiter
*
* Description:
* Wait for USB devices to be connected.
*
****************************************************************************/
static int hidkbd_waiter(int argc, char *argv[])
{
bool connected = false;
int ret;
printf("hidkbd_waiter: Running\n");
for (;;)
{
/* Wait for the device to change state */
ret = DRVR_WAIT(g_drvr, connected);
DEBUGASSERT(ret == OK);
connected = !connected;
printf("hidkbd_waiter: %s\n", connected ? "connected" : "disconnected");
/* Did we just become connected? */
if (connected)
{
/* Yes.. enumerate the newly connected device */
(void)DRVR_ENUMERATE(g_drvr);
}
}
/* Keep the compiler from complaining */
return 0;
}
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
char buffer[256];
pid_t pid;
ssize_t nbytes;
int fd;
int ret;
/* First, register all of the USB host HID keyboard class driver */
printf("user_start: Register class drivers\n");
ret = usbhost_kbdinit();
if (ret != OK)
{
printf("user_start: Failed to register the KBD class\n");
}
/* Then get an instance of the USB host interface */
printf("user_start: Initialize USB host keyboard driver\n");
g_drvr = usbhost_initialize(0);
if (g_drvr)
{
/* Start a thread to handle device connection. */
printf("user_start: Start hidkbd_waiter\n");
#ifndef CONFIG_CUSTOM_STACK
pid = task_create("usbhost", CONFIG_EXAMPLES_HIDKBD_DEFPRIO,
CONFIG_EXAMPLES_HIDKBD_STACKSIZE,
(main_t)hidkbd_waiter, (const char **)NULL);
#else
pid = task_create("usbhost", CONFIG_EXAMPLES_HIDKBD_DEFPRIO,
(main_t)hidkbd_waiter, (const char **)NULL);
#endif
/* Now just sleep. Eventually logic here will open the kbd device and
* perform the HID keyboard test.
*/
for (;;)
{
/* Open the keyboard device. Loop until the device is successfully
* opened.
*/
do
{
printf("Opening device %s\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME);
fd = open(CONFIG_EXAMPLES_HIDKBD_DEVNAME, O_RDONLY);
if (fd < 0)
{
printf("Failed: %d\n", errno);
fflush(stdout);
sleep(3);
}
}
while (fd < 0);
printf("Device %s opened\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME);
fflush(stdout);
/* Loop until there is a read failure */
do
{
/* Read a buffer of data */
nbytes = read(fd, buffer, 256);
if (nbytes > 0)
{
/* On success, echo the buffer to stdout */
(void)write(1, buffer, nbytes);
}
}
while (nbytes >= 0);
printf("Closing device %s: %d\n", CONFIG_EXAMPLES_HIDKBD_DEVNAME, (int)nbytes);
fflush(stdout);
close(fd);
}
}
return 0;
}

93
apps/examples/igmp/Makefile Executable file
View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/igmp/Makefile
#
# Copyright (C) 2010 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# IGMP Networking Example
ASRCS =
CSRCS = igmp.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

142
apps/examples/igmp/igmp.c Executable file
View File

@ -0,0 +1,142 @@
/****************************************************************************
* examples/igmp/igmp.c
*
* Copyright (C) 2010-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <debug.h>
#include <net/if.h>
#include <net/uip/uip.h>
#include <apps/netutils/uiplib.h>
#include <apps/netutils/ipmsfilter.h>
#include "igmp.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* Check if the destination address is a multicast address
*
* - IPv4: multicast addresses lie in the class D group -- The address range
* 224.0.0.0 to 239.255.255.255 (224.0.0.0/4)
*
* - IPv6 multicast addresses are have the high-order octet of the
* addresses=0xff (ff00::/8.)
*/
#if ((CONFIG_EXAMPLE_IGMP_GRPADDR & 0xffff0000) < 0xe0000000ul) || \
((CONFIG_EXAMPLE_IGMP_GRPADDR & 0xffff0000) > 0xeffffffful)
# error "Bad range for IGMP group address"
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
struct in_addr addr;
#if defined(CONFIG_EXAMPLE_IGMP_NOMAC)
uint8_t mac[IFHWADDRLEN];
#endif
message("Configuring Ethernet...\n");
/* Many embedded network interfaces must have a software assigned MAC */
#ifdef CONFIG_EXAMPLE_IGMP_NOMAC
mac[0] = 0x00;
mac[1] = 0xe0;
mac[2] = 0xb0;
mac[3] = 0x0b;
mac[4] = 0xba;
mac[5] = 0xbe;
uip_setmacaddr("eth0", mac);
#endif
/* Set up our host address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_IGMP_IPADDR);
uip_sethostaddr("eth0", &addr);
/* Set up the default router address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_IGMP_DRIPADDR);
uip_setdraddr("eth0", &addr);
/* Setup the subnet mask */
addr.s_addr = HTONL(CONFIG_EXAMPLE_IGMP_NETMASK);
uip_setnetmask("eth0", &addr);
/* Not much of a test for now */
/* Join the group */
message("Join group...\n");
addr.s_addr = HTONL(CONFIG_EXAMPLE_IGMP_GRPADDR);
ipmsfilter("eth0", &addr, MCAST_INCLUDE);
/* Wait a while */
message("Wait for timeout...\n");
sleep(5);
/* Leave the group */
message("Leave group...\n");
ipmsfilter("eth0", &addr, MCAST_EXCLUDE);
/* Wait a while */
sleep(5);
message("Exiting...\n");
return 0;
}

61
apps/examples/igmp/igmp.h Executable file
View File

@ -0,0 +1,61 @@
/****************************************************************************
* examples/igmp/igmp.h
*
* Copyright (C) 2010 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_IGMP_H
#define __EXAMPLES_IGMP_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <debug.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Used lib_rawprintf() so that there is no confusion from buffered IO */
#ifdef CONFIG_CPP_HAVE_VARARGS
# define message(...) lib_rawprintf(__VA_ARGS__)
#else
# define message lib_rawprintf
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#endif /* __EXAMPLES_IGMP_H */

View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/lcdrw/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# LCD Read/Write Test
ASRCS =
CSRCS = lcdrw_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# LCD R/W built-in application info
APPNAME = lcdrw
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_LCDRW_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,262 @@
/****************************************************************************
* examples/lcdrw/lcdrw_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <nuttx/lcd/lcd.h>
#include <nuttx/nx/nxglib.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* Most of the NX configuration settings are probbably *not* needed by this
* example. But, presumeably you are using NX too and so the checks might
* be good for you.
*/
#ifndef CONFIG_NX
# error "CONFIG_NX must be defined to use this test"
#endif
#ifndef CONFIG_NX_LCDDRIVER
# error "CONFIG_NX_LCDDRIVER must be defined to use this test"
#endif
#ifndef CONFIG_EXAMPLES_LCDRW_BPP
# define CONFIG_EXAMPLES_LCDRW_BPP 16
#endif
#if CONFIG_EXAMPLES_LCDRW_BPP != 16
# error "Currently only RGB565 is supported -- feel free to extend"
#endif
#ifdef CONFIG_NX_DISABLE_16BPP
# error "CONFIG_NX_DISABLE_16BPP disables 16-bit support"
#endif
#ifndef CONFIG_EXAMPLES_LDCRW_DEVNO
# define CONFIG_EXAMPLES_LDCRW_DEVNO 0
#endif
#ifndef CONFIG_EXAMPLES_LDCRW_XRES
# define CONFIG_EXAMPLES_LDCRW_XRES 240
#endif
#ifndef CONFIG_EXAMPLES_LDCRW_YRES
# define CONFIG_EXAMPLES_LDCRW_YRES 320
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct lcdrw_instance_s
{
/* LCD device handle and planeinfo */
FAR struct lcd_dev_s *dev;
struct lcd_planeinfo_s pinfo;
};
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: lcdrw_initialize
****************************************************************************/
static inline int lcdrw_initialize(FAR struct lcdrw_instance_s *inst)
{
int ret;
/* Initialize the LCD device */
printf("screens_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
fprintf(stderr, "screens_initialize: up_lcdinitialize failed: %d\n", -ret);
return ret;
}
/* Get the device instance. */
printf("Get LCD instance\n");
inst->dev = up_lcdgetdev(CONFIG_EXAMPLES_LDCRW_DEVNO);
if (!inst->dev)
{
fprintf(stderr, "up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_LDCRW_DEVNO);
return ret;
}
/* Turn the LCD on at 75% power. This should not be necessary. */
(void)inst->dev->setpower(inst->dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
/* Get the planeinfo structure */
ret = inst->dev->getplaneinfo(inst->dev, 0, &inst->pinfo);
if (ret < 0)
{
fprintf(stderr, "getplaneinfo failed: %d\n", ret);
}
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: lcdrw_main/user_start
****************************************************************************/
#ifdef CONFIG_EXAMPLES_LCDRW_BUILTIN
# define MAIN_NAME lcdrw_main
#else
# define MAIN_NAME user_start
#endif
int MAIN_NAME(int argc, char *argv[])
{
struct lcdrw_instance_s inst;
nxgl_coord_t row;
nxgl_coord_t col;
uint16_t value;
uint32_t offset;
FAR uint16_t *ptr;
int ret;
/* Initialize the LCD driver */
ret = lcdrw_initialize(&inst);
if (ret < 0)
{
exit(1);
}
/* Then read each line from the LCD and write to the TIFF file */
value = 0;
for (row = 0; row < CONFIG_EXAMPLES_LDCRW_YRES; row++)
{
/* Create a dummy row. The important thing is to try all
* bit combinations in a predictable way.
*/
ptr = (FAR uint16_t*)inst.pinfo.buffer;
for (col = 0; col < CONFIG_EXAMPLES_LDCRW_XRES; col++)
{
*ptr++ = value++;
}
/* Write the row to the LCD */
ret = inst.pinfo.putrun(row, 0, inst.pinfo.buffer,
CONFIG_EXAMPLES_LDCRW_XRES);
if (ret < 0)
{
fprintf(stderr, "putrun failed: %d\n", ret);
exit(1);
}
}
/* Print a header */
printf(" ");
for (col = 0; col < 15; col++)
{
printf("---%x ", col);
}
printf("---f\n");
/* Then read each line back from the LCD (this would hook into
* the TIFF file creation library *very* easily to create a
* TIFF file of the returned values).
*/
offset = 0;
for (row = 0; row < CONFIG_EXAMPLES_LDCRW_YRES; row++)
{
/* Read the row */
ret = inst.pinfo.getrun(row, 0, inst.pinfo.buffer,
CONFIG_EXAMPLES_LDCRW_XRES);
if (ret < 0)
{
fprintf(stderr, "getrun failed: %d\n", ret);
exit(1);
}
/* Then dump the row to the display */
ptr = (FAR uint16_t*)inst.pinfo.buffer;
for (col = 0; col < CONFIG_EXAMPLES_LDCRW_XRES; col++)
{
if ((offset & 15) == 0)
{
printf("%06x ", offset);
}
value = *ptr++;
offset++;
if ((offset & 15) == 0)
{
printf("%04x\n", value);
}
else
{
printf("%04x ", value);
}
}
}
fflush(stdout);
return 0;
}

93
apps/examples/mm/Makefile Normal file
View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/mm/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Memory Management Test
ASRCS =
CSRCS = mm_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

300
apps/examples/mm/mm_main.c Normal file
View File

@ -0,0 +1,300 @@
/****************************************************************************
* examples/mm/mm_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define NTEST_ALLOCS 32
/* #define STOP_ON_ERRORS do{}while(0) */
#define STOP_ON_ERRORS exit(1)
/* All other definitions derive from these two */
#define MM_MIN_SHIFT 4 /* 16 bytes */
#define MM_MIN_CHUNK (1 << MM_MIN_SHIFT)
#define MM_GRAN_MASK (MM_MIN_CHUNK-1)
#define MM_ALIGN_UP(a) (((a) + MM_GRAN_MASK) & ~MM_GRAN_MASK)
#define MM_ALIGN_DOWN(a) ((a) & ~MM_GRAN_MASK)
#ifdef CONFIG_SMALL_MEMORY
# define SIZEOF_MM_ALLOCNODE 4
#else
# define SIZEOF_MM_ALLOCNODE 8
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/* Test allocations */
static const int alloc_sizes[NTEST_ALLOCS] =
{
1024, 12, 962, 5692, 10254, 111, 9932, 601,
222, 2746, 3, 124321, 68, 776, 6750, 852,
4732, 28, 901, 480, 5011, 1536, 2011, 81647,
646, 1646, 69179, 194, 2590, 7, 969, 70
};
static const int realloc_sizes[NTEST_ALLOCS] =
{
18, 3088, 963, 123, 511, 11666, 3723, 42,
9374, 1990, 1412, 6, 592, 4088, 11, 5040,
8663, 91255, 28, 4346, 9172, 168, 229, 4734,
59139, 221, 7830, 30421, 1666, 4, 812, 416
};
static const int random1[NTEST_ALLOCS] =
{
20, 11, 3, 31, 9, 29, 7, 17,
21, 2, 26, 18, 14, 25, 0, 10,
27, 19, 22, 28, 8, 30, 12, 15,
4, 1, 24, 6, 16, 13, 5, 23
};
static const int random2[NTEST_ALLOCS] =
{
2, 19, 12, 23, 30, 11, 27, 4,
20, 7, 0, 16, 28, 15, 5, 24,
10, 17, 25, 31, 8, 29, 3, 26,
9, 18, 22, 13, 1, 21, 14, 6
};
static const int random3[NTEST_ALLOCS] =
{
8, 17, 3, 18, 26, 23, 30, 11,
12, 22, 4, 20, 25, 10, 27, 1,
29, 14, 19, 21, 0, 31, 7, 24,
9, 15, 2, 28, 16, 6, 13, 5
};
static const int alignment[NTEST_ALLOCS/2] =
{
128, 2048, 131072, 8192, 32, 32768, 16384 , 262144,
512, 4096, 65536, 8, 64, 1024, 16, 4
};
static void *allocs[NTEST_ALLOCS];
static struct mallinfo alloc_info;
/****************************************************************************
* Private Functions
****************************************************************************/
static void mm_showmallinfo(void)
{
alloc_info = mallinfo();
printf(" mallinfo:\n");
printf(" Total space allocated from system = %ld\n",
alloc_info.arena);
printf(" Number of non-inuse chunks = %ld\n",
alloc_info.ordblks);
printf(" Largest non-inuse chunk = %ld\n",
alloc_info.mxordblk);
printf(" Total allocated space = %ld\n",
alloc_info.uordblks);
printf(" Total non-inuse space = %ld\n",
alloc_info.fordblks);
}
static void do_mallocs(void **mem, const int *size, const int *seq, int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
j = seq[i];
if (!mem[j])
{
printf("(%d)Allocating %d bytes\n", i, size[j]);
mem[j] = malloc(size[j]);
printf("(%d)Memory allocated at %p\n", i, mem[j]);
if (mem[j] == NULL)
{
int allocsize = MM_ALIGN_UP(size[j] + SIZEOF_MM_ALLOCNODE);
fprintf(stderr, "(%d)malloc failed for allocsize=%d\n", i, allocsize);
if (allocsize > alloc_info.mxordblk)
{
fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
}
else
{
fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
exit(1);
}
}
else
{
memset(mem[j], 0xAA, size[j]);
}
mm_showmallinfo();
}
}
}
static void do_reallocs(void **mem, const int *oldsize, const int *newsize, const int *seq, int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
j = seq[i];
printf("(%d)Re-allocating at %p from %d to %d bytes\n",
i, mem[j], oldsize[j], newsize[j]);
mem[j] = realloc(mem[j], newsize[j]);
printf("(%d)Memory re-allocated at %p\n", i, mem[j]);
if (mem[j] == NULL)
{
int allocsize = MM_ALIGN_UP(newsize[j] + SIZEOF_MM_ALLOCNODE);
fprintf(stderr, "(%d)realloc failed for allocsize=%d\n", i, allocsize);
if (allocsize > alloc_info.mxordblk)
{
fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
}
else
{
fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
exit(1);
}
}
else
{
memset(mem[j], 0x55, newsize[j]);
}
mm_showmallinfo();
}
}
static void do_memaligns(void **mem, const int *size, const int *align, const int *seq, int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
j = seq[i];
printf("(%d)Allocating %d bytes aligned to 0x%08x\n",
i, size[j], align[i]);
mem[j] = memalign(align[i], size[j]);
printf("(%d)Memory allocated at %p\n", i, mem[j]);
if (mem[j] == NULL)
{
int allocsize = MM_ALIGN_UP(size[j] + SIZEOF_MM_ALLOCNODE) + 2*align[i];
fprintf(stderr, "(%d)memalign failed for allocsize=%d\n", i, allocsize);
if (allocsize > alloc_info.mxordblk)
{
fprintf(stderr, " Normal, largest free block is only %ld\n", alloc_info.mxordblk);
}
else
{
fprintf(stderr, " ERROR largest free block is %ld\n", alloc_info.mxordblk);
exit(1);
}
}
else
{
memset(mem[j], 0x33, size[j]);
}
mm_showmallinfo();
}
}
static void do_frees(void **mem, const int *size, const int *seq, int n)
{
int i;
int j;
for (i = 0; i < n; i++)
{
j = seq[i];
printf("(%d)Releasing memory at %p (size=%d bytes)\n",
i, mem[j], size[j]);
free(mem[j]);
mem[j] = NULL;
mm_showmallinfo();
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
mm_showmallinfo();
/* Allocate some memory */
do_mallocs(allocs, alloc_sizes, random1, NTEST_ALLOCS);
/* Re-allocate the memory */
do_reallocs(allocs, alloc_sizes, realloc_sizes, random2, NTEST_ALLOCS);
/* Release the memory */
do_frees(allocs, realloc_sizes, random3, NTEST_ALLOCS);
/* Allocate aligned memory */
do_memaligns(allocs, alloc_sizes, alignment, random2, NTEST_ALLOCS/2);
do_memaligns(allocs, alloc_sizes, alignment, &random2[NTEST_ALLOCS/2], NTEST_ALLOCS/2);
/* Release aligned memory */
do_frees(allocs, alloc_sizes, random1, NTEST_ALLOCS);
printf("TEST COMPLETE\n");
return 0;
}

View File

@ -0,0 +1,93 @@
############################################################################
# apps/Makefile
#
# Copyright (C) 2007-2008, 2010-2010 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# mount() test
ASRCS =
CSRCS = mount_main.c ramdisk.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,96 @@
/****************************************************************************
* examples/mount/mount.h
*
* Copyright (C) 2008 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_MOUNT_MOUNT_H
#define __EXAMPLES_MOUNT_MOUNT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configure the test */
#if defined(CONFIG_EXAMPLES_MOUNT_DEVNAME)
# if !defined(CONFIG_FS_WRITABLE)
# error "Writable filesystem required in this configuration"
# endif
# undef CONFIG_EXAMPLES_MOUNT_NSECTORS
# undef CONFIG_EXAMPLES_MOUNT_SECTORSIZE
# undef CONFIG_EXAMPLES_MOUNT_RAMDEVNO
# define MOUNT_DEVNAME CONFIG_EXAMPLES_MOUNT_DEVNAME
#else
# if !defined(CONFIG_FS_FAT)
# error "CONFIG_FS_FAT required in this configuration"
# endif
# if !defined(CONFIG_EXAMPLES_MOUNT_SECTORSIZE)
# define CONFIG_EXAMPLES_MOUNT_SECTORSIZE 512
# endif
# if !defined(CONFIG_EXAMPLES_MOUNT_NSECTORS)
# define CONFIG_EXAMPLES_MOUNT_NSECTORS 2048
# endif
# if !defined(CONFIG_EXAMPLES_MOUNT_RAMDEVNO)
# define CONFIG_EXAMPLES_MOUNT_RAMDEVNO 0
# endif
# define STR_RAMDEVNO(m) #m
# define MKMOUNT_DEVNAME(m) "/dev/ram" STR_RAMDEVNO(m)
# define MOUNT_DEVNAME MKMOUNT_DEVNAME(CONFIG_EXAMPLES_MOUNT_RAMDEVNO)
#endif
/****************************************************************************
* Public Types
****************************************************************************/
/****************************************************************************
* Public Variables
****************************************************************************/
extern const char g_source[]; /* Mount 'source' path */
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifndef CONFIG_EXAMPLES_MOUNT_DEVNAME
extern int create_ramdisk(void);
#endif
#endif /* __EXAMPLES_MOUNT_MOUNT_H */

View File

@ -0,0 +1,754 @@
/****************************************************************************
* examples/mount/mount_main.c
*
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <sys/mount.h>
#include <sys/stat.h>
#include <sys/statfs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#include "mount.h"
/****************************************************************************
* Definitions
****************************************************************************/
#define TEST_USE_STAT 1
#define TEST_SHOW_DIRECTORIES 1
#define TEST_USE_STATFS 1
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_mntdir[] = "/mnt";
static const char g_target[] = "/mnt/fs";
static const char g_filesystemtype[] = "vfat";
static const char g_testdir1[] = "/mnt/fs/TestDir";
static const char g_testdir2[] = "/mnt/fs/NewDir1";
static const char g_testdir3[] = "/mnt/fs/NewDir2";
static const char g_testdir4[] = "/mnt/fs/NewDir3";
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
static const char g_testfile1[] = "/mnt/fs/TestDir/TestFile.txt";
#endif
static const char g_testfile2[] = "/mnt/fs/TestDir/WrTest1.txt";
static const char g_testfile3[] = "/mnt/fs/NewDir1/WrTest2.txt";
static const char g_testfile4[] = "/mnt/fs/NewDir3/Renamed.txt";
static const char g_testmsg[] = "This is a write test";
static int g_nerrors = 0;
static char g_namebuffer[256];
/****************************************************************************
* Public Data
****************************************************************************/
const char g_source[] = MOUNT_DEVNAME;
/****************************************************************************
* Private Functions
****************************************************************************/
#ifdef TEST_USE_STAT
static void show_stat(const char *path, struct stat *ps)
{
printf("%s stat:\n", path);
printf("\tmode : %08x\n", ps->st_mode);
if (S_ISREG(ps->st_mode))
{
printf("\ttype : File\n");
}
else if (S_ISDIR(ps->st_mode))
{
printf("\ttype : Directory\n");
}
else if (S_ISCHR(ps->st_mode))
{
printf("\ttype : Character driver\n");
}
else if (S_ISBLK(ps->st_mode))
{
printf("\ttype : Block driver\n");
}
else
{
printf("\ttype : Unknown\n");
}
printf("\tsize : %d (bytes)\n", ps->st_size);
printf("\tblock size : %d (bytes)\n", ps->st_blksize);
printf("\tsize : %d (blocks)\n", ps->st_blocks);
printf("\taccess time : %d\n", ps->st_atime);
printf("\tmodify time : %d\n", ps->st_mtime);
printf("\tchange time : %d\n", ps->st_ctime);
}
#endif
/****************************************************************************
* Name: show_statfs
****************************************************************************/
#ifdef TEST_USE_STATFS
static void show_statfs(const char *path)
{
struct statfs buf;
int ret;
/* Try stat() against a file or directory. It should fail with expectederror */
printf("show_statfs: Try statfs(%s)\n", path);
ret = statfs(path, &buf);
if (ret == 0)
{
printf("show_statfs: statfs(%s) succeeded\n", path);
printf("\tFS Type : %0x\n", buf.f_type);
printf("\tBlock size : %d\n", buf.f_bsize);
printf("\tNumber of blocks : %d\n", buf.f_blocks);
printf("\tFree blocks : %d\n", buf.f_bfree);
printf("\tFree user blocks : %d\n", buf.f_bavail);
printf("\tNumber file nodes : %d\n", buf.f_files);
printf("\tFree file nodes : %d\n", buf.f_ffree);
printf("\tFile name length : %d\n", buf.f_namelen);
}
else
{
printf("show_statfs: ERROR statfs(%s) failed with errno=%d\n",
path, errno);
g_nerrors++;
}
}
#else
# define show_statfs(p)
#endif
/****************************************************************************
* Name: show_directories
****************************************************************************/
#ifdef TEST_SHOW_DIRECTORIES
static void show_directories(const char *path, int indent)
{
DIR *dirp;
struct dirent *direntry;
int i;
dirp = opendir(path);
if ( !dirp )
{
printf("show_directories: ERROR opendir(\"%s\") failed with errno=%d\n",
path, errno);
g_nerrors++;
return;
}
for (direntry = readdir(dirp); direntry; direntry = readdir(dirp))
{
for (i = 0; i < 2*indent; i++)
{
putchar(' ');
}
if (DIRENT_ISDIRECTORY(direntry->d_type))
{
char *subdir;
printf("%s/\n", direntry->d_name);
sprintf(g_namebuffer, "%s/%s", path, direntry->d_name);
subdir = strdup(g_namebuffer);
show_directories( subdir, indent + 1);
free(subdir);
}
else
{
printf("%s\n", direntry->d_name);
}
}
closedir(dirp);
}
#else
# define show_directories(p,i)
#endif
/****************************************************************************
* Name: fail_read_open
****************************************************************************/
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
static void fail_read_open(const char *path, int expectederror)
{
int fd;
printf("fail_read_open: Try open(%s) for reading\n", path);
fd = open(path, O_RDONLY);
if (fd >= 0)
{
printf("fail_read_open: ERROR open(%s) succeeded\n", path);
g_nerrors++;
close(fd);
}
else if (errno != expectederror)
{
printf("fail_read_open: ERROR open(%s) failed with errno=%d (expected %d)\n",
path, errno, expectederror);
g_nerrors++;
}
}
#endif
/****************************************************************************
* Name: read_test_file
****************************************************************************/
static void read_test_file(const char *path)
{
char buffer[128];
int nbytes;
int fd;
/* Read a test file that is already on the test file system image */
printf("read_test_file: opening %s for reading\n", path);
fd = open(path, O_RDONLY);
if (fd < 0)
{
printf("read_test_file: ERROR failed to open %s, errno=%d\n",
path, errno);
g_nerrors++;
}
else
{
memset(buffer, 0, 128);
nbytes = read(fd, buffer, 128);
if (nbytes < 0)
{
printf("read_test_file: ERROR failed to read from %s, errno=%d\n",
path, errno);
g_nerrors++;
}
else
{
buffer[127]='\0';
printf("read_test_file: Read \"%s\" from %s\n", buffer, path);
}
close(fd);
}
}
/****************************************************************************
* Name: write_test_file
****************************************************************************/
static void write_test_file(const char *path)
{
int fd;
/* Write a test file into a pre-existing file on the test file system */
printf("write_test_file: opening %s for writing\n", path);
fd = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (fd < 0)
{
printf("write_test_file: ERROR failed to open %s for writing, errno=%d\n",
path, errno);
g_nerrors++;
}
else
{
int nbytes = write(fd, g_testmsg, strlen(g_testmsg));
if (nbytes < 0)
{
printf("write_test_file: ERROR failed to write to %s, errno=%d\n",
path, errno);
g_nerrors++;
}
else
{
printf("write_test_file: wrote %d bytes to %s\n", nbytes, path);
}
close(fd);
}
}
/****************************************************************************
* Name: fail_mkdir
****************************************************************************/
static void fail_mkdir(const char *path, int expectederror)
{
int ret;
/* Try mkdir() against a file or directory. It should fail with expectederror */
printf("fail_mkdir: Try mkdir(%s)\n", path);
ret = mkdir(path, 0666);
if (ret == 0)
{
printf("fail_mkdir: ERROR mkdir(%s) succeeded\n", path);
g_nerrors++;
}
else if (errno != expectederror)
{
printf("fail_mkdir: ERROR mkdir(%s) failed with errno=%d (expected %d)\n",
path, errno, expectederror);
g_nerrors++;
}
}
/****************************************************************************
* Name: succeed_mkdir
****************************************************************************/
static void succeed_mkdir(const char *path)
{
int ret;
printf("succeed_mkdir: Try mkdir(%s)\n", path);
ret = mkdir(path, 0666);
if (ret != 0)
{
printf("succeed_mkdir: ERROR mkdir(%s) failed with errno=%d\n",
path, errno);
g_nerrors++;
}
}
/****************************************************************************
* Name: fail_rmdir
****************************************************************************/
static void fail_rmdir(const char *path, int expectederror)
{
int ret;
/* Try rmdir() against a file or directory. It should fail with expectederror */
printf("fail_rmdir: Try rmdir(%s)\n", path);
ret = rmdir(path);
if (ret == 0)
{
printf("fail_rmdir: ERROR rmdir(%s) succeeded\n", path);
g_nerrors++;
}
else if (errno != expectederror)
{
printf("fail_rmdir: ERROR rmdir(%s) failed with errno=%d (expected %d)\n",
path, errno, expectederror);
g_nerrors++;
}
}
/****************************************************************************
* Name: succeed_rmdir
****************************************************************************/
static void succeed_rmdir(const char *path)
{
int ret;
printf("succeed_rmdir: Try rmdir(%s)\n", path);
ret = rmdir(path);
if (ret != 0)
{
printf("succeed_rmdir: ERROR rmdir(%s) failed with errno=%d\n",
path, errno);
g_nerrors++;
}
}
/****************************************************************************
* Name: fail_unlink
****************************************************************************/
static void fail_unlink(const char *path, int expectederror)
{
int ret;
/* Try unlink() against a file or directory. It should fail with expectederror */
printf("fail_unlink: Try unlink(%s)\n", path);
ret = unlink(path);
if (ret == 0)
{
printf("fail_unlink: ERROR unlink(%s) succeeded\n", path);
g_nerrors++;
}
else if (errno != expectederror)
{
printf("fail_unlink: ERROR unlink(%s) failed with errno=%d (expected %d)\n",
path, errno, expectederror);
g_nerrors++;
}
}
/****************************************************************************
* Name: succeed_unlink
****************************************************************************/
static void succeed_unlink(const char *path)
{
int ret;
/* Try unlink() against the test file. It should succeed. */
printf("succeed_unlink: Try unlink(%s)\n", path);
ret = unlink(path);
if (ret != 0)
{
printf("succeed_unlink: ERROR unlink(%s) failed with errno=%d\n",
path, errno);
g_nerrors++;
}
}
/****************************************************************************
* Name: fail_rename
****************************************************************************/
static void fail_rename(const char *oldpath, const char *newpath, int expectederror)
{
int ret;
/* Try rename() against a file or directory. It should fail with expectederror */
printf("fail_rename: Try rename(%s->%s)\n", oldpath, newpath);
ret = rename(oldpath, newpath);
if (ret == 0)
{
printf("fail_rename: ERROR rename(%s->%s) succeeded\n",
oldpath, newpath);
g_nerrors++;
}
else if (errno != expectederror)
{
printf("fail_rename: ERROR rename(%s->%s) failed with errno=%d (expected %d)\n",
oldpath, newpath, errno, expectederror);
g_nerrors++;
}
}
/****************************************************************************
* Name: succeed_rename
****************************************************************************/
static void succeed_rename(const char *oldpath, const char *newpath)
{
int ret;
printf("succeed_rename: Try rename(%s->%s)\n", oldpath, newpath);
ret = rename(oldpath, newpath);
if (ret != 0)
{
printf("succeed_rename: ERROR rename(%s->%s) failed with errno=%d\n",
oldpath, newpath, errno);
g_nerrors++;
}
}
/****************************************************************************
* Name: fail_stat
****************************************************************************/
#ifdef TEST_USE_STAT
static void fail_stat(const char *path, int expectederror)
{
struct stat buf;
int ret;
/* Try stat() against a file or directory. It should fail with expectederror */
printf("fail_stat: Try stat(%s)\n", path);
ret = stat(path, &buf);
if (ret == 0)
{
printf("fail_stat: ERROR stat(%s) succeeded\n", path);
show_stat(path, &buf);
g_nerrors++;
}
else if (errno != expectederror)
{
printf("fail_stat: ERROR stat(%s) failed with errno=%d (expected %d)\n",
path, errno, expectederror);
g_nerrors++;
}
}
#else
# define fail_stat(p,e);
#endif
/****************************************************************************
* Name: succeed_stat
****************************************************************************/
#ifdef TEST_USE_STAT
static void succeed_stat(const char *path)
{
struct stat buf;
int ret;
printf("succeed_stat: Try stat(%s)\n", path);
ret = stat(path, &buf);
if (ret != 0)
{
printf("succeed_stat: ERROR stat(%s) failed with errno=%d\n",
path, errno);
g_nerrors++;
}
else
{
printf("succeed_stat: stat(%s) succeeded\n", path);
show_stat(path, &buf);
}
}
#else
#define succeed_stat(p)
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
int ret;
#ifndef CONFIG_EXAMPLES_MOUNT_DEVNAME
/* Create a RAM disk for the test */
ret = create_ramdisk();
if (ret < 0)
{
printf("user_start: ERROR failed to create RAM disk\n");
return 1;
}
#endif
/* Mount the test file system (see arch/sim/src/up_deviceimage.c */
printf("user_start: mounting %s filesystem at target=%s with source=%s\n",
g_filesystemtype, g_target, g_source);
ret = mount(g_source, g_target, g_filesystemtype, 0, NULL);
printf("user_start: mount() returned %d\n", ret);
if (ret == 0)
{
show_statfs(g_mntdir);
show_statfs(g_target);
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
/* Read a test file that is already on the test file system image */
show_directories("", 0);
succeed_stat(g_testfile1);
show_statfs(g_testfile1);
read_test_file(g_testfile1);
#else
/* Create the test directory that would have been on the canned filesystem */
succeed_mkdir(g_testdir1);
show_directories("", 0);
succeed_stat(g_testdir1);
show_statfs(g_testdir1);
#endif
/* Write a test file into a pre-existing directory on the test file system */
fail_stat(g_testfile2, ENOENT);
write_test_file(g_testfile2);
show_directories("", 0);
succeed_stat(g_testfile2);
show_statfs(g_testfile2);
/* Read the file that we just wrote */
read_test_file(g_testfile2);
/* Try rmdir() against a file on the directory. It should fail with ENOTDIR */
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
fail_rmdir(g_testfile1, ENOTDIR);
#endif
/* Try rmdir() against the test directory. It should fail with ENOTEMPTY */
fail_rmdir(g_testdir1, ENOTEMPTY);
/* Try unlink() against the test directory. It should fail with EISDIR */
fail_unlink(g_testdir1, EISDIR);
/* Try unlink() against the test file1. It should succeed. */
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
succeed_unlink(g_testfile1);
fail_stat(g_testfile1, ENOENT);
show_directories("", 0);
#endif
/* Attempt to open testfile1 should fail with ENOENT */
#ifdef CONFIG_EXAMPLES_MOUNT_DEVNAME
fail_read_open(g_testfile1, ENOENT);
#endif
/* Try rmdir() against the test directory. It should still fail with ENOTEMPTY */
fail_rmdir(g_testdir1, ENOTEMPTY);
/* Try mkdir() against the test file2. It should fail with EEXIST. */
fail_mkdir(g_testfile2, EEXIST);
/* Try unlink() against the test file2. It should succeed. */
succeed_unlink(g_testfile2);
show_directories("", 0);
fail_stat(g_testfile2, ENOENT);
/* Try mkdir() against the test dir1. It should fail with EEXIST. */
fail_mkdir(g_testdir1, EEXIST);
/* Try rmdir() against the test directory. mkdir should now succeed. */
succeed_rmdir(g_testdir1);
show_directories("", 0);
fail_stat(g_testdir1, ENOENT);
/* Try mkdir() against the test dir2. It should succeed */
succeed_mkdir(g_testdir2);
show_directories("", 0);
succeed_stat(g_testdir2);
show_statfs(g_testdir2);
/* Try mkdir() against the test dir2. It should fail with EXIST */
fail_mkdir(g_testdir2, EEXIST);
/* Write a test file into a new directory on the test file system */
fail_stat(g_testfile3, ENOENT);
write_test_file(g_testfile3);
show_directories("", 0);
succeed_stat(g_testfile3);
show_statfs(g_testfile3);
/* Read the file that we just wrote */
read_test_file(g_testfile3);
/* Use mkdir() to create test dir3. It should succeed */
fail_stat(g_testdir3, ENOENT);
succeed_mkdir(g_testdir3);
show_directories("", 0);
succeed_stat(g_testdir3);
show_statfs(g_testdir3);
/* Try rename() on the root directory. Should fail with EXDEV*/
fail_rename(g_target, g_testdir4, EXDEV);
/* Try rename() to an existing directory. Should fail with EEXIST */
fail_rename(g_testdir2, g_testdir3, EEXIST);
/* Try rename() to a non-existing directory. Should succeed */
fail_stat(g_testdir4, ENOENT);
succeed_rename(g_testdir3, g_testdir4);
show_directories("", 0);
fail_stat(g_testdir3, ENOENT);
succeed_stat(g_testdir4);
show_statfs(g_testdir4);
/* Try rename() of file. Should work. */
fail_stat(g_testfile4, ENOENT);
succeed_rename(g_testfile3, g_testfile4);
show_directories("", 0);
fail_stat(g_testfile3, ENOENT);
succeed_stat(g_testfile4);
show_statfs(g_testfile4);
/* Make sure that we can still read the renamed file */
read_test_file(g_testfile4);
/* Unmount the file system */
printf("user_start: Try unmount(%s)\n", g_target);
ret = umount(g_target);
if (ret != 0)
{
printf("user_start: ERROR umount() failed, errno %d\n", errno);
g_nerrors++;
}
printf("user_start: %d errors reported\n", g_nerrors);
}
fflush(stdout);
return 0;
}

View File

@ -0,0 +1,141 @@
/****************************************************************************
* examples/mount/ramdisk.c
*
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <nuttx/ramdisk.h>
#include <nuttx/mkfatfs.h>
#include "mount.h"
#ifndef CONFIG_EXAMPLES_MOUNT_DEVNAME
/****************************************************************************
* Private Definitions
****************************************************************************/
#define BUFFER_SIZE (CONFIG_EXAMPLES_MOUNT_NSECTORS*CONFIG_EXAMPLES_MOUNT_SECTORSIZE)
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static struct fat_format_s g_fmt = FAT_FORMAT_INITIALIZER;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: create_ramdisk
*
* Description:
* Create a RAM disk of the specified size formatting with a FAT file
* system
*
* Input Parameters:
* None
*
* Return:
* Zero on success, a negated errno on failure.
*
****************************************************************************/
int create_ramdisk(void)
{
char *pbuffer;
int ret;
/* Allocate a buffer to hold the file system image. */
pbuffer = (char*)malloc(BUFFER_SIZE);
if (!pbuffer)
{
printf("create_ramdisk: Failed to allocate ramdisk of size %d\n",
BUFFER_SIZE);
return -ENOMEM;
}
/* Register a RAMDISK device to manage this RAM image */
ret = ramdisk_register(CONFIG_EXAMPLES_MOUNT_RAMDEVNO,
pbuffer,
CONFIG_EXAMPLES_MOUNT_NSECTORS,
CONFIG_EXAMPLES_MOUNT_SECTORSIZE,
true);
if (ret < 0)
{
printf("create_ramdisk: Failed to register ramdisk at %s: %d\n",
g_source, -ret);
free(pbuffer);
return ret;
}
/* Create a FAT filesystem on the ramdisk */
ret = mkfatfs(g_source, &g_fmt);
if (ret < 0)
{
printf("create_ramdisk: Failed to create FAT filesystem on ramdisk at %s\n",
g_source);
/* free(pbuffer); -- RAM disk is registered */
return ret;
}
return 0;
}
#endif /* !CONFIG_EXAMPLES_MOUNT_DEVNAME */

View File

@ -0,0 +1,142 @@
############################################################################
# examples/nettest/Makefile
#
# Copyright (C) 2007-2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Basic TCP networking test
TARG_ASRCS =
TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT))
TARG_CSRCS = nettest.c
ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y)
TARG_CSRCS += nettest_server.c
else
TARG_CSRCS += nettest_client.c
endif
TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT))
TARG_SRCS = $(TARG_ASRCS) $(TARG_CSRCS)
TARG_OBJS = $(TARG_AOBJS) $(TARG_COBJS)
TARG_POSIX = "$(APPDIR)/libapps$(LIBEXT)"
ifeq ($(WINTOOL),y)
TARG_BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
TARG_BIN = "$(TARG_POSIX)"
endif
HOSTCFLAGS += -DCONFIG_EXAMPLE_NETTEST_HOST=1
ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y)
HOSTCFLAGS += -DCONFIG_EXAMPLE_NETTEST_SERVER=1 \
-DCONFIG_EXAMPLE_NETTEST_CLIENTIP="$(CONFIG_EXAMPLE_NETTEST_CLIENTIP)"
endif
ifeq ($(CONFIG_EXAMPLE_NETTEST_PERFORMANCE),y)
HOSTCFLAGS += -DCONFIG_EXAMPLE_NETTEST_PERFORMANCE=1
endif
HOST_SRCS = host.c
ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y)
HOST_SRCS += nettest_client.c
else
HOST_SRCS += nettest_server.c
endif
HOST_OBJS = $(HOST_SRCS:.c=.o)
HOST_BIN = host
ROOTDEPPATH = --dep-path .
# NET test built-in application info
APPNAME = nettest
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built $(HOST_BIN)
.PHONY: clean depend disclean
$(TARG_AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(TARG_COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
$(HOST_OBJS): %.o: %.c
@echo "CC: $<"
@$(HOSTCC) -c $(HOSTCFLAGS) $< -o $@
$(HOST_BIN): $(HOST_OBJS)
@echo "LD: $@"
@$(HOSTCC) $(HOSTLDFLAGS) $(HOST_OBJS) -o $@
.built: $(TARG_OBJS)
@( for obj in $(TARG_OBJS) ; do \
$(call ARCHIVE, $(TARG_BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_NSH_BUILTIN_APPS),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(TARG_SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(TARG_SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f $(TARG_POSIX) $(HOST_BIN) .built *.o *~ .*.swp
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,63 @@
/****************************************************************************
* examples/nettest/host.c
*
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name 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 "nettest.h"
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* main
****************************************************************************/
int main(int argc, char **argv, char **envp)
{
#ifdef CONFIG_EXAMPLE_NETTEST_SERVER
send_client();
#else
recv_server();
#endif
return 0;
}

View File

@ -0,0 +1,121 @@
/****************************************************************************
* examples/nettest/nettest.c
*
* Copyright (C) 2007, 2009-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <debug.h>
#include <net/if.h>
#include <net/uip/uip.h>
#include <apps/netutils/uiplib.h>
#include "nettest.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* If CONFIG_NSH_BUILTIN_APPS is defined, then it is assumed that you want
* to execute the DHCPD daemon as an NSH built-in task.
*/
#ifdef CONFIG_NSH_BUILTIN_APPS
# define MAIN_NAME nettest_main
# define MAIN_NAME_STRING "nettest_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* user_start
****************************************************************************/
int MAIN_NAME(int argc, char *argv[])
{
struct in_addr addr;
#ifdef CONFIG_EXAMPLE_NETTEST_NOMAC
uint8_t mac[IFHWADDRLEN];
#endif
/* Many embedded network interfaces must have a software assigned MAC */
#ifdef CONFIG_EXAMPLE_NETTEST_NOMAC
mac[0] = 0x00;
mac[1] = 0xe0;
mac[2] = 0xb0;
mac[3] = 0x0b;
mac[4] = 0xba;
mac[5] = 0xbe;
uip_setmacaddr("eth0", mac);
#endif
/* Set up our host address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_IPADDR);
uip_sethostaddr("eth0", &addr);
/* Set up the default router address */
addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_DRIPADDR);
uip_setdraddr("eth0", &addr);
/* Setup the subnet mask */
addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_NETMASK);
uip_setnetmask("eth0", &addr);
#ifdef CONFIG_EXAMPLE_NETTEST_SERVER
recv_server();
#else
send_client();
#endif
return 0;
}

View File

@ -0,0 +1,95 @@
/****************************************************************************
* examples/nettest/nettest.h
*
* Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_NETTEST_H
#define __EXAMPLES_NETTEST_H
/****************************************************************************
* Included Files
****************************************************************************/
#ifdef CONFIG_EXAMPLE_NETTEST_HOST
#else
# include <debug.h>
#endif
/****************************************************************************
* Definitions
****************************************************************************/
#ifdef CONFIG_EXAMPLE_NETTEST_HOST
/* HTONS/L macros are unique to uIP */
# define HTONS(a) htons(a)
# define HTONL(a) htonl(a)
/* Used printf for debug output */
# ifdef CONFIG_CPP_HAVE_VARARGS
# define message(...) printf(__VA_ARGS__)
# else
# define message printf
# endif
/* Have SO_LINGER */
# define NETTEST_HAVE_SOLINGER 1
#else
/* Used lib_rawprintf() so that there is not confusion from buffered IO */
# ifdef CONFIG_CPP_HAVE_VARARGS
# define message(...) lib_rawprintf(__VA_ARGS__)
# else
# define message lib_rawprintf
# endif
/* At present, uIP does only abortive disconnects */
# undef NETTEST_HAVE_SOLINGER
#endif
#define PORTNO 5471
#define SENDSIZE 4096
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
extern void send_client(void);
extern void recv_server(void);
#endif /* __EXAMPLES_NETTEST_H */

View File

@ -0,0 +1,205 @@
/****************************************************************************
* examples/nettest/nettest-client.c
*
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name 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 <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include "nettest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
void send_client(void)
{
struct sockaddr_in myaddr;
char *outbuf;
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
char *inbuf;
#endif
int sockfd;
int nbytessent;
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
int nbytesrecvd;
int totalbytesrecvd;
#endif
int ch;
int i;
/* Allocate buffers */
outbuf = (char*)malloc(SENDSIZE);
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
inbuf = (char*)malloc(SENDSIZE);
if (!outbuf || !inbuf)
#else
if (!outbuf)
#endif
{
message("client: failed to allocate buffers\n");
exit(1);
}
/* Create a new TCP socket */
sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
message("client socket failure %d\n", errno);
goto errout_with_buffers;
}
/* Connect the socket to the server */
myaddr.sin_family = AF_INET;
myaddr.sin_port = HTONS(PORTNO);
#if 0
myaddr.sin_addr.s_addr = HTONL(INADDR_LOOPBACK);
#else
myaddr.sin_addr.s_addr = HTONL(CONFIG_EXAMPLE_NETTEST_CLIENTIP);
#endif
message("client: Connecting...\n");
if (connect( sockfd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
{
message("client: connect failure: %d\n", errno);
goto errout_with_socket;
}
message("client: Connected\n");
/* Initialize the buffer */
ch = 0x20;
for (i = 0; i < SENDSIZE; i++ )
{
outbuf[i] = ch;
if (++ch > 0x7e)
{
ch = 0x20;
}
}
#ifdef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
/* Then send messages forever */
for (;;)
{
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
if (nbytessent < 0)
{
message("client: send failed: %d\n", errno);
goto errout_with_socket;
}
else if (nbytessent != SENDSIZE)
{
message("client: Bad send length=%d: %d of \n",
nbytessent, SENDSIZE);
goto errout_with_socket;
}
message("Sent %d bytes\n", nbytessent);
}
#else
/* Then send and receive one message */
message("client: Sending %d bytes\n", SENDSIZE);
nbytessent = send(sockfd, outbuf, SENDSIZE, 0);
message("client: Sent %d bytes\n", nbytessent);
if (nbytessent < 0)
{
message("client: send failed: %d\n", errno);
goto errout_with_socket;
}
else if (nbytessent != SENDSIZE)
{
message("client: Bad send length: %d Expected: %d\n", nbytessent, SENDSIZE);
goto errout_with_socket;
}
totalbytesrecvd = 0;
do
{
message("client: Receiving...\n");
nbytesrecvd = recv(sockfd, &inbuf[totalbytesrecvd], SENDSIZE - totalbytesrecvd, 0);
if (nbytesrecvd < 0)
{
message("client: recv failed: %d\n", errno);
goto errout_with_socket;
}
totalbytesrecvd += nbytesrecvd;
message("client: Received %d of %d bytes\n", totalbytesrecvd, SENDSIZE);
}
while (totalbytesrecvd < SENDSIZE);
if (totalbytesrecvd != SENDSIZE)
{
message("client: Bad recv length: %d Expected: %d\n", totalbytesrecvd, SENDSIZE);
goto errout_with_socket;
}
else if (memcmp(inbuf, outbuf, SENDSIZE) != 0)
{
message("client: Received buffer does not match sent buffer\n");
goto errout_with_socket;
}
close(sockfd);
free(outbuf);
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
free(inbuf);
#endif
return;
#endif
errout_with_socket:
close(sockfd);
errout_with_buffers:
free(outbuf);
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
free(inbuf);
#endif
exit(1);
}

View File

@ -0,0 +1,235 @@
/****************************************************************************
* examples/nettest/nettest-server.c
*
* Copyright (C) 2007, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name 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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "nettest.h"
/****************************************************************************
* Public Functions
****************************************************************************/
void recv_server(void)
{
struct sockaddr_in myaddr;
#ifdef NETTEST_HAVE_SOLINGER
struct linger ling;
#endif
char *buffer;
int listensd;
int acceptsd;
socklen_t addrlen;
int nbytesread;
#ifndef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
int totalbytesread;
int nbytessent;
int ch;
int i;
#endif
int optval;
/* Allocate a BIG buffer */
buffer = (char*)malloc(2*SENDSIZE);
if (!buffer)
{
message("server: failed to allocate buffer\n");
exit(1);
}
/* Create a new TCP socket */
listensd = socket(PF_INET, SOCK_STREAM, 0);
if (listensd < 0)
{
message("server: socket failure: %d\n", errno);
goto errout_with_buffer;
}
/* Set socket to reuse address */
optval = 1;
if (setsockopt(listensd, SOL_SOCKET, SO_REUSEADDR, (void*)&optval, sizeof(int)) < 0)
{
message("server: setsockopt SO_REUSEADDR failure: %d\n", errno);
goto errout_with_listensd;
}
/* Bind the socket to a local address */
myaddr.sin_family = AF_INET;
myaddr.sin_port = HTONS(PORTNO);
myaddr.sin_addr.s_addr = INADDR_ANY;
if (bind(listensd, (struct sockaddr*)&myaddr, sizeof(struct sockaddr_in)) < 0)
{
message("server: bind failure: %d\n", errno);
goto errout_with_listensd;
}
/* Listen for connections on the bound TCP socket */
if (listen(listensd, 5) < 0)
{
message("server: listen failure %d\n", errno);
goto errout_with_listensd;
}
/* Accept only one connection */
message("server: Accepting connections on port %d\n", PORTNO);
addrlen = sizeof(struct sockaddr_in);
acceptsd = accept(listensd, (struct sockaddr*)&myaddr, &addrlen);
if (acceptsd < 0)
{
message("server: accept failure: %d\n", errno);
goto errout_with_listensd;
}
message("server: Connection accepted -- receiving\n");
/* Configure to "linger" until all data is sent when the socket is closed */
#ifdef NETTEST_HAVE_SOLINGER
ling.l_onoff = 1;
ling.l_linger = 30; /* timeout is seconds */
if (setsockopt(acceptsd, SOL_SOCKET, SO_LINGER, &ling, sizeof(struct linger)) < 0)
{
message("server: setsockopt SO_LINGER failure: %d\n", errno);
goto errout_with_acceptsd;
}
#endif
#ifdef CONFIG_EXAMPLE_NETTEST_PERFORMANCE
/* Then receive data forever */
for (;;)
{
nbytesread = recv(acceptsd, buffer, 2*SENDSIZE, 0);
if (nbytesread <= 0)
{
message("server: recv failed: %d\n", errno);
goto errout_with_acceptsd;
}
message("Received %d bytes\n", nbytesread);
}
#else
/* Receive canned message */
totalbytesread = 0;
while (totalbytesread < SENDSIZE)
{
message("server: Reading...\n");
nbytesread = recv(acceptsd, &buffer[totalbytesread], 2*SENDSIZE - totalbytesread, 0);
if (nbytesread <= 0)
{
message("server: recv failed: %d\n", errno);
goto errout_with_acceptsd;
}
totalbytesread += nbytesread;
message("server: Received %d of %d bytes\n", totalbytesread, SENDSIZE);
}
/* Verify the message */
if (totalbytesread != SENDSIZE)
{
message("server: Received %d / Expected %d bytes\n", totalbytesread, SENDSIZE);
goto errout_with_acceptsd;
}
ch = 0x20;
for (i = 0; i < SENDSIZE; i++ )
{
if (buffer[i] != ch)
{
message("server: Byte %d is %02x / Expected %02x\n", i, buffer[i], ch);
goto errout_with_acceptsd;
}
if (++ch > 0x7e)
{
ch = 0x20;
}
}
/* Then send the same data back to the client */
message("server: Sending %d bytes\n", totalbytesread);
nbytessent = send(acceptsd, buffer, totalbytesread, 0);
if (nbytessent <= 0)
{
message("server: send failed: %d\n", errno);
goto errout_with_acceptsd;
}
message("server: Sent %d bytes\n", nbytessent);
/* If this platform only does abortive disconnects, then wait a bit to get the
* client side a change to receive the data.
*/
#if 1 /* Do it for all platforms */
message("server: Wait before closing\n");
sleep(60);
#endif
close(listensd);
close(acceptsd);
free(buffer);
return;
#endif
errout_with_acceptsd:
close(acceptsd);
errout_with_listensd:
close(listensd);
errout_with_buffer:
free(buffer);
exit(1);
}

View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/nsh/Makefile
#
# Copyright (C) 2007-2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttShell (NSH) Example
ASRCS =
CSRCS = nsh_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,136 @@
/****************************************************************************
* examples/nsh/nsh_main.c
*
* Copyright (C) 2007-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/stat.h>
#include <stdint.h>
#include <stdio.h>
#include <sched.h>
#include <errno.h>
#include <apps/nsh.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
int mid_priority;
#if defined(CONFIG_NSH_CONSOLE) && defined(CONFIG_NSH_TELNET)
int ret;
#endif
/* Initialize the NSH library */
nsh_initialize();
/* Set the priority of this task to something in the middle so that 'nice'
* can both raise and lower the priority.
*/
mid_priority = (sched_get_priority_max(SCHED_NSH) + sched_get_priority_min(SCHED_NSH)) >> 1;
{
struct sched_param param;
param.sched_priority = mid_priority;
(void)sched_setscheduler(0, SCHED_NSH, &param);
}
/* If both the console and telnet are selected as front-ends, then run
* the telnet front end on another thread.
*/
#if defined(CONFIG_NSH_CONSOLE) && defined(CONFIG_NSH_TELNET)
# ifndef CONFIG_CUSTOM_STACK
ret = task_create("nsh_telnetmain", mid_priority, CONFIG_NSH_STACKSIZE,
nsh_telnetmain, NULL);
# else
ret = task_create("nsh_telnetmain", mid_priority, nsh_telnetmain, NULL);
# endif
if (ret < 0)
{
/* The daemon is NOT running. Report the the error then fail...
* either with the serial console up or just exiting.
*/
fprintf(stderr, "ERROR: Failed to start TELNET daemon: %d\n", errno);
}
/* If only the telnet front-end is selected, run it on this thread */
#elif defined(CONFIG_NSH_TELNET)
return nsh_telnetmain(0, NULL);
#endif
/* If the serial console front end is selected, then run it on this thread */
#ifdef CONFIG_NSH_CONSOLE
return nsh_consolemain(0, NULL);
#endif
}

View File

@ -0,0 +1,93 @@
############################################################################
# examples/null/Makefile
#
# Copyright (C) 2007-2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# The smallest thing you can build -- the NULL example.
ASRCS =
CSRCS = null_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,67 @@
/****************************************************************************
* examples/null/null_main.c
*
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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
****************************************************************************/
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
return 0;
}

108
apps/examples/nx/Makefile Normal file
View File

@ -0,0 +1,108 @@
############################################################################
# apps/examples/nx/Makefile
#
# Copyright (C) 2009-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = nx_main.c nx_events.c nx_kbdin.c
ifeq ($(CONFIG_NX_MULTIUSER),y)
CSRCS += nx_server.c
endif
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# NX built-in application info
APPNAME = nx
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_NX_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,337 @@
/****************************************************************************
* examples/nx/nx_events.c
*
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxtk.h>
#include "nx_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nxeg_redraw(NXEGWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxeg_position(NXEGWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxeg_mousein(NXEGWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static void nxeg_tbredraw(NXEGWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxeg_tbposition(NXEGWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxeg_tbmousein(NXEGWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
const struct nx_callback_s g_nxcb =
{
nxeg_redraw, /* redraw */
nxeg_position /* position */
#ifdef CONFIG_NX_MOUSE
, nxeg_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxeg_kbdin /* my kbdin */
#endif
};
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
const struct nx_callback_s g_tbcb =
{
nxeg_tbredraw, /* redraw */
nxeg_tbposition /* position */
#ifdef CONFIG_NX_MOUSE
, nxeg_tbmousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxeg_tbkbdin /* my kbdin */
#endif
};
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxeg_fillwindow
****************************************************************************/
static inline void nxeg_fillwindow(NXEGWINDOW hwnd,
FAR const struct nxgl_rect_s *rect,
FAR struct nxeg_state_s *st)
{
int ret;
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
ret = nx_fill(hwnd, rect, st->color);
if (ret < 0)
{
message("nxeg_fillwindow: nx_fill failed: %d\n", errno);
}
#else
ret = nxtk_fillwindow(hwnd, rect, st->color);
if (ret < 0)
{
message("nxeg_fillwindow: nxtk_fillwindow failed: %d\n", errno);
}
#endif
#ifdef CONFIG_NX_KBD
nxeg_filltext(hwnd, rect, st);
#endif
}
/****************************************************************************
* Name: nxeg_fillwindow
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline void nxeg_filltoolbar(NXTKWINDOW htb,
FAR const struct nxgl_rect_s *rect,
nxgl_mxpixel_t color[CONFIG_NX_NPLANES])
{
int ret;
ret = nxtk_filltoolbar(htb, rect, color);
if (ret < 0)
{
message("nxeg_filltoolbar: nxtk_filltoolbar failed: %d\n", errno);
}
}
#endif
/****************************************************************************
* Name: nxeg_redraw
****************************************************************************/
static void nxeg_redraw(NXEGWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_redraw%d: hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
st->wnum, hwnd,
rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
nxeg_fillwindow(hwnd, rect, st);
}
/****************************************************************************
* Name: nxeg_position
****************************************************************************/
static void nxeg_position(NXEGWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
/* Report the position */
message("nxeg_position%d: hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
st->wnum, hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!b_haveresolution)
{
/* Save the window limits (these should be the same for all places and all windows */
g_xres = bounds->pt2.x;
g_yres = bounds->pt2.y;
b_haveresolution = true;
sem_post(&g_semevent);
message("nxeg_position2: Have xres=%d yres=%d\n", g_xres, g_yres);
}
}
/****************************************************************************
* Name: nxeg_mousein
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxeg_mousein(NXEGWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_mousein%d: hwnd=%p pos=(%d,%d) button=%02x\n",
st->wnum, hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nxeg_tbredraw
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static void nxeg_tbredraw(NXEGWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_tbredraw%d: hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
st->wnum, hwnd,
rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
nxeg_filltoolbar(hwnd, rect, g_tbcolor);
}
#endif
/****************************************************************************
* Name: nxeg_position
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static void nxeg_tbposition(NXEGWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
/* Report the position */
message("nxeg_ptbosition%d: hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
st->wnum, hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
}
#endif
/****************************************************************************
* Name: nxeg_tbmousein
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
#ifdef CONFIG_NX_MOUSE
static void nxeg_tbmousein(NXEGWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_tbmousein%d: hwnd=%p pos=(%d,%d) button=%02x\n",
st->wnum, hwnd, pos->x, pos->y, buttons);
}
#endif
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nx_listenerthread
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
FAR void *nx_listenerthread(FAR void *arg)
{
int ret;
/* Process events forever */
for (;;)
{
/* Handle the next event. If we were configured blocking, then
* we will stay right here until the next event is received. Since
* we have dedicated a while thread to servicing events, it would
* be most natural to also select CONFIG_NX_BLOCKING -- if not, the
* following would be a tight infinite loop (unless we added addition
* logic with nx_eventnotify and sigwait to pace it).
*/
ret = nx_eventhandler(g_hnx);
if (ret < 0)
{
/* An error occurred... assume that we have lost connection with
* the server.
*/
message("nx_listenerthread: Lost server connection: %d\n", errno);
exit(NXEXIT_LOSTSERVERCONN);
}
/* If we received a message, we must be connected */
if (!g_connected)
{
g_connected = true;
sem_post(&g_semevent);
message("nx_listenerthread: Connected\n");
}
}
}
#endif

View File

@ -0,0 +1,317 @@
/****************************************************************************
* examples/nx/nx_internal.h
*
* Copyright (C) 2008-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_NX_NX_INTERNAL_H
#define __EXAMPLES_NX_NX_INTERNAL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxtk.h>
#include <nuttx/nx/nxfonts.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_NX
# error "NX is not enabled (CONFIG_NX)"
#endif
#ifndef CONFIG_EXAMPLES_NX_VPLANE
# define CONFIG_EXAMPLES_NX_VPLANE 0
#endif
#ifndef CONFIG_EXAMPLES_NX_BPP
# define CONFIG_EXAMPLES_NX_BPP 32
#endif
#ifndef CONFIG_EXAMPLES_NX_BGCOLOR
# if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32
# define CONFIG_EXAMPLES_NX_BGCOLOR 0x007b68ee
# elif CONFIG_EXAMPLES_NX_BPP == 16
# define CONFIG_EXAMPLES_NX_BGCOLOR 0x7b5d
# else
# define CONFIG_EXAMPLES_NX_BGCOLOR ' '
# endif
#endif
#ifndef CONFIG_EXAMPLES_NX_COLOR1
# if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32
# define CONFIG_EXAMPLES_NX_COLOR1 0x00e6e6fa
# elif CONFIG_EXAMPLES_NX_BPP == 16
# define CONFIG_EXAMPLES_NX_COLOR1 0xe73f
# else
# define CONFIG_EXAMPLES_NX_COLOR1 '1'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NX_COLOR2
# if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32
# define CONFIG_EXAMPLES_NX_COLOR2 0x00dcdcdc
# elif CONFIG_EXAMPLES_NX_BPP == 16
# define CONFIG_EXAMPLES_NX_COLOR2 0xdefb
# else
# define CONFIG_EXAMPLES_NX_COLOR2 '2'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NX_TBCOLOR
# if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32
# define CONFIG_EXAMPLES_NX_TBCOLOR 0x00a9a9a9
# elif CONFIG_EXAMPLES_NX_BPP == 16
# define CONFIG_EXAMPLES_NX_TBCOLOR 0xad55
# else
# define CONFIG_EXAMPLES_NX_TBCOLOR 'T'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NX_FONTID
# define CONFIG_EXAMPLES_NX_FONTID NXFONT_DEFAULT
#endif
#ifndef CONFIG_EXAMPLES_NX_FONTCOLOR
# if CONFIG_EXAMPLES_NX_BPP == 24 || CONFIG_EXAMPLES_NX_BPP == 32
# define CONFIG_EXAMPLES_NX_FONTCOLOR 0x00000000
# elif CONFIG_EXAMPLES_NX_BPP == 16
# define CONFIG_EXAMPLES_NX_FONTCOLOR 0x0000
# else
# define CONFIG_EXAMPLES_NX_FONTCOLOR 'F'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NX_TOOLBAR_HEIGHT
# define CONFIG_EXAMPLES_NX_TOOLBAR_HEIGHT 16
#endif
#ifdef CONFIG_NX_MULTIUSER
# ifdef CONFIG_DISABLE_MQUEUE
# error "The multi-threaded example requires MQ support (CONFIG_DISABLE_MQUEUE=n)"
# endif
# ifdef CONFIG_DISABLE_SIGNALS
# error "This example requires signal support (CONFIG_DISABLE_SIGNALS=n)"
# endif
# ifdef CONFIG_DISABLE_PTHREAD
# error "This example requires pthread support (CONFIG_DISABLE_PTHREAD=n)"
# endif
# ifndef CONFIG_NX_BLOCKING
# error "This example depends on CONFIG_NX_BLOCKING"
# endif
# ifndef CONFIG_EXAMPLES_NX_STACKSIZE
# define CONFIG_EXAMPLES_NX_STACKSIZE 2048
# endif
# ifndef CONFIG_EXAMPLES_NX_LISTENERPRIO
# define CONFIG_EXAMPLES_NX_LISTENERPRIO 100
# endif
# ifndef CONFIG_EXAMPLES_NX_CLIENTPRIO
# define CONFIG_EXAMPLES_NX_CLIENTPRIO 100
# endif
# ifndef CONFIG_EXAMPLES_NX_SERVERPRIO
# define CONFIG_EXAMPLES_NX_SERVERPRIO 120
# endif
# ifndef CONFIG_EXAMPLES_NX_NOTIFYSIGNO
# define CONFIG_EXAMPLES_NX_NOTIFYSIGNO 4
# endif
#endif
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
# define NXEGWINDOW NXWINDOW
#else
# define NXEGWINDOW NXTKWINDOW
#endif
#define NXTK_MAXKBDCHARS 16
/* Debug ********************************************************************/
#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
/****************************************************************************
* Public Types
****************************************************************************/
enum exitcode_e
{
NXEXIT_SUCCESS = 0,
NXEXIT_SIGPROCMASK,
NXEXIT_SCHEDSETPARAM,
NXEXIT_EVENTNOTIFY,
NXEXIT_TASKCREATE,
NXEXIT_PTHREADCREATE,
NXEXIT_EXTINITIALIZE,
NXEXIT_FBINITIALIZE,
NXEXIT_FBGETVPLANE,
NXEXIT_LCDINITIALIZE,
NXEXIT_LCDGETDEV,
NXEXIT_NXOPEN,
NXEXIT_FONTOPEN,
NXEXIT_NXOPENTOOLBAR,
NXEXIT_NXCONNECT,
NXEXIT_NXSETBGCOLOR,
NXEXIT_NXOPENWINDOW,
NXEXIT_NXSETSIZE,
NXEXIT_NXSETPOSITION,
NXEXIT_NXLOWER,
NXEXIT_NXRAISE,
NXEXIT_NXCLOSEWINDOW,
NXEXIT_LOSTSERVERCONN
};
/* Describes one cached glyph bitmap */
struct nxeg_glyph_s
{
uint8_t code; /* Character code */
uint8_t height; /* Height of this glyph (in rows) */
uint8_t width; /* Width of this glyph (in pixels) */
uint8_t stride; /* Width of the glyph row (in bytes) */
FAR uint8_t *bitmap; /* Allocated bitmap memory */
};
/* Describes on character on the display */
struct nxeg_bitmap_s
{
struct nxgl_rect_s bounds; /* Size/position of bitmap */
FAR const struct nxeg_glyph_s *glyph; /* The cached glyph */
};
/* Describes the overall state of on one window */
struct nxeg_state_s
{
uint8_t wnum; /* Window number */
nxgl_mxpixel_t color[CONFIG_NX_NPLANES]; /* Window color */
#ifdef CONFIG_NX_KBD
uint8_t height; /* Max height of a font in pixels */
uint8_t width; /* Max width of a font in pixels */
uint8_t spwidth; /* The width of a space */
uint8_t nchars; /* Number of KBD chars received */
uint8_t nglyphs; /* Number of glyphs cached */
struct nxeg_bitmap_s bm[NXTK_MAXKBDCHARS];
struct nxeg_glyph_s glyph[NXTK_MAXKBDCHARS];
#endif
};
/****************************************************************************
* Public Variables
****************************************************************************/
/* The connecton handle */
extern NXHANDLE g_hnx;
/* NX callback vtables */
extern const struct nx_callback_s g_nxcb;
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
extern const struct nx_callback_s g_tbcb;
#endif
/* The font handle */
extern NXHANDLE g_fonthandle;
/* The screen resolution */
extern nxgl_coord_t g_xres;
extern nxgl_coord_t g_yres;
extern bool b_haveresolution;
#ifdef CONFIG_NX_MULTIUSER
extern bool g_connected;
#endif
extern sem_t g_semevent;
/* Colors used to fill window 1 & 2 */
extern nxgl_mxpixel_t g_color1[CONFIG_NX_NPLANES];
extern nxgl_mxpixel_t g_color2[CONFIG_NX_NPLANES];
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
extern nxgl_mxpixel_t g_tbcolor[CONFIG_NX_NPLANES];
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_EXTERNINIT
extern FAR NX_DRIVERTYPE *up_nxdrvinit(unsigned int devno);
#endif
#if defined(CONFIG_NX) && defined(CONFIG_NX_MULTIUSER)
extern int nx_servertask(int argc, char *argv[]);
extern FAR void *nx_listenerthread(FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
extern void nxeg_kbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch, FAR void *arg);
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
extern void nxeg_tbkbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch, FAR void *arg);
#endif
extern void nxeg_filltext(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
FAR struct nxeg_state_s *st);
#endif
#endif /* __EXAMPLES_NX_NX_INTERNAL_H */

467
apps/examples/nx/nx_kbdin.c Normal file
View File

@ -0,0 +1,467 @@
/****************************************************************************
* examples/nx/nx_kbdin.c
*
* Copyright (C) 2008, 2010-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxtk.h>
#include <nuttx/nx/nxfonts.h>
#include "nx_internal.h"
#ifdef CONFIG_NX_KBD
/****************************************************************************
* Definitions
****************************************************************************/
/* Select renderer -- Some additional logic would be required to support
* pixel depths that are not directly addressable (1,2,4, and 24).
*/
#if CONFIG_EXAMPLES_NX_BPP == 1
# define RENDERER nxf_convert_1bpp
#elif CONFIG_EXAMPLES_NX_BPP == 2
# define RENDERER nxf_convert_2bpp
#elif CONFIG_EXAMPLES_NX_BPP == 4
# define RENDERER nxf_convert_4bpp
#elif CONFIG_EXAMPLES_NX_BPP == 8
# define RENDERER nxf_convert_8bpp
#elif CONFIG_EXAMPLES_NX_BPP == 16
# define RENDERER nxf_convert_16bpp
#elif CONFIG_EXAMPLES_NX_BPP == 24
# define RENDERER nxf_convert_24bpp
#elif CONFIG_EXAMPLES_NX_BPP == 32
# define RENDERER nxf_convert_32bpp
#else
# error "Unsupported CONFIG_EXAMPLES_NX_BPP"
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxeg_fillchar
****************************************************************************/
static void nxeg_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
FAR const struct nxeg_bitmap_s *bm)
{
FAR void *src = (FAR void *)bm->glyph->bitmap;
struct nxgl_rect_s intersection;
int ret;
/* Handle the special case of spaces which have no glyph bitmap */
if (src)
{
/* Get the intersection of the redraw region and the character bitmap */
nxgl_rectintersect(&intersection, rect, &bm->bounds);
if (!nxgl_nullrect(&intersection))
{
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
ret = nxtk_bitmapwindow((NXTKWINDOW)hwnd, &intersection, (FAR const void **)&src,
&bm->bounds.pt1,
(unsigned int)bm->glyph->stride);
if (ret < 0)
{
message("nxeg_fillchar: nxtk_bitmapwindow failed: %d\n", errno);
}
#else
ret = nx_bitmap((NXWINDOW)hwnd, &intersection, &src,
&bm->bounds.pt1,
(unsigned int)bm->glyph->stride);
if (ret < 0)
{
message("nxeg_fillchar: nx_bitmapwindow failed: %d\n", errno);
}
#endif
}
}
}
/****************************************************************************
* Name: nxeg_renderglyph
****************************************************************************/
static inline FAR const struct nxeg_glyph_s *
nxeg_renderglyph(FAR struct nxeg_state_s *st,
FAR const struct nx_fontbitmap_s *bm, uint8_t ch)
{
FAR struct nxeg_glyph_s *glyph = NULL;
FAR nxgl_mxpixel_t *ptr;
#if CONFIG_EXAMPLES_NX_BPP < 8
nxgl_mxpixel_t pixel;
#endif
int bmsize;
int row;
int col;
int ret;
/* Make sure that there is room for another glyph */
message("nxeg_renderglyph: ch=%02x\n", ch);
if (st->nglyphs < NXTK_MAXKBDCHARS)
{
/* Allocate the glyph */
glyph = &st->glyph[st->nglyphs];
glyph->code = ch;
/* Get the dimensions of the glyph */
glyph->width = bm->metric.width + bm->metric.xoffset;
glyph->height = bm->metric.height + bm->metric.yoffset;
/* Allocate memory to hold the glyph with its offsets */
glyph->stride = (glyph->width * CONFIG_EXAMPLES_NX_BPP + 7) / 8;
bmsize = glyph->stride * glyph->height;
glyph->bitmap = (FAR uint8_t *)malloc(bmsize);
if (glyph->bitmap)
{
/* Initialize the glyph memory to the background color */
#if CONFIG_EXAMPLES_NX_BPP < 8
pixel = st->color[0];
# if CONFIG_EXAMPLES_NX_BPP == 1
/* Pack 1-bit pixels into a 2-bits */
pixel &= 0x01;
pixel = (pixel) << 1 |pixel;
# endif
# if CONFIG_EXAMPLES_NX_BPP < 4
/* Pack 2-bit pixels into a nibble */
pixel &= 0x03;
pixel = (pixel) << 2 |pixel;
# endif
/* Pack 4-bit nibbles into a byte */
pixel &= 0x0f;
pixel = (pixel) << 4 | pixel;
ptr = (FAR nxgl_mxpixel_t *)glyph->bitmap;
for (row = 0; row < glyph->height; row++)
{
for (col = 0; col < glyph->stride; col++)
{
/* Transfer the packed bytes into the buffer */
*ptr++ = pixel;
}
}
#elif CONFIG_EXAMPLES_NX_BPP == 24
# error "Additional logic is needed here for 24bpp support"
#else /* CONFIG_EXAMPLES_NX_BPP = {8,16,32} */
ptr = (FAR nxgl_mxpixel_t *)glyph->bitmap;
for (row = 0; row < glyph->height; row++)
{
/* Just copy the color value into the glyph memory */
for (col = 0; col < glyph->width; col++)
{
*ptr++ = st->color[0];
}
}
#endif
/* Then render the glyph into the allocated memory */
ret = RENDERER((FAR nxgl_mxpixel_t*)glyph->bitmap,
glyph->height, glyph->width, glyph->stride,
bm, CONFIG_EXAMPLES_NX_FONTCOLOR);
if (ret < 0)
{
/* Actually, the RENDERER never returns a failure */
message("nxeg_renderglyph: RENDERER failed\n");
free(glyph->bitmap);
glyph->bitmap = NULL;
glyph = NULL;
}
else
{
/* Make it permanent */
st->nglyphs++;
}
}
}
return glyph;
}
/****************************************************************************
* Name: nxeg_addspace
****************************************************************************/
static inline FAR const struct nxeg_glyph_s *
nxeg_addspace(FAR struct nxeg_state_s *st, uint8_t ch)
{
FAR struct nxeg_glyph_s *glyph = NULL;
/* Make sure that there is room for another glyph */
if (st->nglyphs < NXTK_MAXKBDCHARS)
{
/* Allocate the NULL glyph */
glyph = &st->glyph[st->nglyphs];
memset(glyph, 0, sizeof(struct nxeg_glyph_s));
glyph->code = ' ';
glyph->width = st->spwidth;
st->nglyphs++;
}
return glyph;
}
/****************************************************************************
* Name: nxeg_findglyph
****************************************************************************/
static FAR const struct nxeg_glyph_s *
nxeg_findglyph(FAR struct nxeg_state_s *st, uint8_t ch)
{
int i;
/* First, try to find the glyph in the cache of pre-rendered glyphs */
for (i = 0; i < st->nglyphs; i++)
{
if (st->glyph[i].code == ch)
{
return &st->glyph[i];
}
}
return NULL;
}
/****************************************************************************
* Name: nxeg_getglyph
****************************************************************************/
static FAR const struct nxeg_glyph_s *
nxeg_getglyph(FAR struct nxeg_state_s *st, uint8_t ch)
{
FAR const struct nxeg_glyph_s *glyph;
FAR const struct nx_fontbitmap_s *bm;
/* First, try to find the glyph in the cache of pre-rendered glyphs */
glyph = nxeg_findglyph(st, ch);
if (!glyph)
{
/* No, it is not cached... Does the code map to a glyph? */
bm = nxf_getbitmap(g_fonthandle, ch);
if (!bm)
{
/* No, there is no glyph for this code. Use space */
glyph = nxeg_findglyph(st, ' ');
if (!glyph)
{
/* There isn't fake glyph for ' ' yet... create one */
glyph = nxeg_addspace(st, ' ');
}
}
else
{
glyph = nxeg_renderglyph(st, bm, ch);
}
}
return glyph;
}
/****************************************************************************
* Name: nxeg_addchar
****************************************************************************/
static FAR const struct nxeg_bitmap_s *
nxeg_addchar(FAR struct nxeg_state_s *st, uint8_t ch)
{
FAR struct nxeg_bitmap_s *bm = NULL;
FAR struct nxeg_bitmap_s *bmleft;
nxgl_coord_t leftx;
/* Is there space for another character on the display? */
if (st->nchars < NXTK_MAXKBDCHARS)
{
/* Yes, setup the bitmap */
bm = &st->bm[st->nchars];
/* Find the matching glyph */
bm->glyph = nxeg_getglyph(st, ch);
if (!bm->glyph)
{
return NULL;
}
/* Set up the bounds for the bitmap */
if (st->nchars <= 0)
{
/* The first character is one space from the left */
leftx = st->spwidth;
}
else
{
/* Otherwise, it is to the left of the preceding char */
bmleft = &st->bm[st->nchars-1];
leftx = bmleft->bounds.pt2.x + 1;
}
bm->bounds.pt1.x = leftx;
bm->bounds.pt1.y = 2;
bm->bounds.pt2.x = leftx + bm->glyph->width - 1;
bm->bounds.pt2.y = 2 + bm->glyph->height - 1;
st->nchars++;
}
return bm;
}
/****************************************************************************
* Name: nxeg_addchars
****************************************************************************/
static inline void nxeg_addchars(NXWINDOW hwnd, FAR struct nxeg_state_s *st,
uint8_t nch, FAR const uint8_t *ch)
{
FAR const struct nxeg_bitmap_s *bm;
while (nch--)
{
bm = nxeg_addchar(st, *ch++);
if (bm)
{
nxeg_fillchar(hwnd, &bm->bounds, bm);
}
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxeg_kbdin
****************************************************************************/
void nxeg_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_kbdin%d: hwnd=%p nch=%d\n", st->wnum, hwnd, nch);
nxeg_addchars(hwnd, st, nch, ch);
}
/****************************************************************************
* Name: nxeg_tbkbdin
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
void nxeg_tbkbdin(NXWINDOW hwnd, uint8_t nch, const uint8_t *ch, FAR void *arg)
{
FAR struct nxeg_state_s *st = (FAR struct nxeg_state_s *)arg;
message("nxeg_tbkbdin: ERROR -- toolbar should not received keyboard input\n");
message("nxeg_tbkbdin%d: hwnd=%p nch=%d\n", st->wnum, hwnd, nch);
}
#endif
/****************************************************************************
* Name: nxeg_filltext
****************************************************************************/
void nxeg_filltext(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
FAR struct nxeg_state_s *st)
{
int i;
/* Fill each character on the display (Only the characters within rect
* will actually be redrawn).
*/
for (i = 0; i < st->nchars; i++)
{
nxeg_fillchar(hwnd, rect, &st->bm[i]);
}
}
#endif /* CONFIG_NX_KBD */

908
apps/examples/nx/nx_main.c Normal file
View File

@ -0,0 +1,908 @@
/****************************************************************************
* examples/nx/nx_main.c
*
* Copyright (C) 2008-2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxtk.h>
#include <nuttx/nx/nxfonts.h>
#include "nx_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */
#ifndef CONFIG_EXAMPLES_NX_VPLANE
# define CONFIG_EXAMPLES_NX_VPLANE 0
#endif
/* If not specified, assume that the hardware supports one LCD device */
#ifndef CONFIG_EXAMPLES_NX_DEVNO
# define CONFIG_EXAMPLES_NX_DEVNO 0
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static int g_exitcode = NXEXIT_SUCCESS;
static struct nxeg_state_s g_wstate[2];
#ifdef CONFIG_NX_KBD
static const uint8_t g_kbdmsg1[] = "NuttX is cool!";
static const uint8_t g_kbdmsg2[] = "NuttX is fun!";
#endif
/* The font handle */
NXHANDLE g_fonthandle;
/****************************************************************************
* Public Data
****************************************************************************/
/* The connecton handler */
NXHANDLE g_hnx = NULL;
/* The screen resolution */
nxgl_coord_t g_xres;
nxgl_coord_t g_yres;
bool b_haveresolution = false;
#ifdef CONFIG_NX_MULTIUSER
bool g_connected = false;
#endif
sem_t g_semevent = {0};
/* Colors used to fill window 1 & 2 */
nxgl_mxpixel_t g_color1[CONFIG_NX_NPLANES];
nxgl_mxpixel_t g_color2[CONFIG_NX_NPLANES];
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
nxgl_mxpixel_t g_tbcolor[CONFIG_NX_NPLANES];
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxeg_drivemouse
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxeg_drivemouse(void)
{
nxgl_coord_t x;
nxgl_coord_t y;
nxgl_coord_t xstep = g_xres / 8;
nxgl_coord_t ystep = g_yres / 8;
for (x = 0; x < g_xres; x += xstep)
{
for (y = 0; y < g_yres; y += ystep)
{
message("nxeg_drivemouse: Mouse left button at (%d,%d)\n", x, y);
(void)nx_mousein(g_hnx, x, y, NX_MOUSE_LEFTBUTTON);
}
}
}
#endif
/****************************************************************************
* Name: nxeg_initstate
****************************************************************************/
static void nxeg_initstate(FAR struct nxeg_state_s *st, int wnum,
nxgl_mxpixel_t color)
{
#ifdef CONFIG_NX_KBD
FAR const struct nx_font_s *fontset;
#endif
/* Initialize the window number (used for debug output only) and color
* (used for redrawing the window)
*/
st->wnum = wnum;
st->color[0] = color;
/* Get information about the font set being used and save this in the
* state structure
*/
#ifdef CONFIG_NX_KBD
fontset = nxf_getfontset(g_fonthandle);
st->nchars = 0;
st->nglyphs = 0;
st->height = fontset->mxheight;
st->width = fontset->mxwidth;
st->spwidth = fontset->spwidth;
#endif
}
/****************************************************************************
* Name: nxeg_freestate
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static void nxeg_freestate(FAR struct nxeg_state_s *st)
{
#ifdef CONFIG_NX_KBD
int i;
if (st)
{
for (i = 0; i < st->nglyphs; i++)
{
if (st->glyph[i].bitmap)
{
free(st->glyph[i].bitmap);
}
st->glyph[i].bitmap = NULL;
}
st->nchars = 0;
}
#endif
}
#endif
/****************************************************************************
* Name: nxeg_openwindow
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline NXEGWINDOW nxeg_openwindow(FAR const struct nx_callback_s *cb,
FAR struct nxeg_state_s *state)
{
NXEGWINDOW hwnd;
hwnd = nx_openwindow(g_hnx, cb, (FAR void *)state);
if (!hwnd)
{
message("nxeg_openwindow: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENWINDOW;
}
return hwnd;
}
#else
static inline NXEGWINDOW nxeg_openwindow(FAR const struct nx_callback_s *cb,
FAR struct nxeg_state_s *state)
{
NXEGWINDOW hwnd;
hwnd = nxtk_openwindow(g_hnx, cb, (FAR void *)state);
if (!hwnd)
{
message("nxeg_openwindow: nxtk_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENWINDOW;
}
return hwnd;
}
#endif
/****************************************************************************
* Name: nxeg_closewindow
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeg_closewindow(NXEGWINDOW hwnd, FAR struct nxeg_state_s *state)
{
int ret = nx_closewindow(hwnd);
if (ret < 0)
{
message("nxeg_closewindow: nx_closewindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXCLOSEWINDOW;
}
return ret;
}
#else
static inline int nxeg_closewindow(NXEGWINDOW hwnd, FAR struct nxeg_state_s *state)
{
int ret = nxtk_closewindow(hwnd);
if (ret < 0)
{
message("nxeg_closewindow: nxtk_closewindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXCLOSEWINDOW;
}
nxeg_freestate(state);
return ret;
}
#endif
/****************************************************************************
* Name: nxeg_setsize
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeg_setsize(NXEGWINDOW hwnd, FAR struct nxgl_size_s *size)
{
int ret = nx_setsize(hwnd, size);
if (ret < 0)
{
message("nxeg_setsize: nx_setsize failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETSIZE;
}
return ret;
}
#else
static inline int nxeg_setsize(NXEGWINDOW hwnd, FAR struct nxgl_size_s *size)
{
int ret = nxtk_setsize(hwnd, size);
if (ret < 0)
{
message("nxeg_setsize: nxtk_setsize failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETSIZE;
}
return ret;
}
#endif
/****************************************************************************
* Name: nxeg_setposition
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeg_setposition(NXEGWINDOW hwnd, FAR struct nxgl_point_s *pos)
{
int ret = nx_setposition(hwnd, pos);
if (ret < 0)
{
message("nxeg_setposition: nx_setposition failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
}
return ret;
}
#else
static inline int nxeg_setposition(NXEGWINDOW hwnd, FAR struct nxgl_point_s *pos)
{
int ret = nxtk_setposition(hwnd, pos);
if (ret < 0)
{
message("nxeg_setposition: nxtk_setposition failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
}
return ret;
}
#endif
/****************************************************************************
* Name: nxeq_opentoolbar
****************************************************************************/
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeq_opentoolbar(NXEGWINDOW hwnd, nxgl_coord_t height,
FAR const struct nx_callback_s *cb,
FAR struct nxeg_state_s *state)
{
int ret;
ret = nxtk_opentoolbar(hwnd, height, cb, (FAR void *)state);
if (ret < 0)
{
message("nxeq_opentoolbar: nxtk_opentoolbar failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENTOOLBAR;
}
return ret;
}
#endif
/****************************************************************************
* Name: nxeg_lower
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeg_lower(NXEGWINDOW hwnd)
{
int ret = nx_lower(hwnd);
if (ret < 0)
{
message("nxeg_lower: nx_lower failed: %d\n", errno);
g_exitcode = NXEXIT_NXLOWER;
}
return ret;
}
#else
static inline int nxeg_lower(NXEGWINDOW hwnd)
{
int ret = nxtk_lower(hwnd);
if (ret < 0)
{
message("nxeg_lower: nxtk_lower failed: %d\n", errno);
g_exitcode = NXEXIT_NXLOWER;
}
return ret;
}
#endif
/****************************************************************************
* Name: nxeg_raise
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_RAWWINDOWS
static inline int nxeg_raise(NXEGWINDOW hwnd)
{
int ret = nx_raise(hwnd);
if (ret < 0)
{
message("nxeg_raise: nx_raise failed: %d\n", errno);
g_exitcode = NXEXIT_NXRAISE;
}
return ret;
}
#else
static inline int nxeg_raise(NXEGWINDOW hwnd)
{
int ret = nxtk_raise(hwnd);
if (ret < 0)
{
message("nxeg_raise: nxtk_raise failed: %d\n", errno);
g_exitcode = NXEXIT_NXRAISE;
}
return ret;
}
#endif
/****************************************************************************
* Name: nxeg_suinitialize
****************************************************************************/
#ifndef CONFIG_NX_MULTIUSER
static inline int nxeg_suinitialize(void)
{
FAR NX_DRIVERTYPE *dev;
#if defined(CONFIG_EXAMPLES_NX_EXTERNINIT)
/* Use external graphics driver initialization */
message("nxeg_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NX_DEVNO);
if (!dev)
{
message("nxeg_initialize: up_nxdrvinit failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
g_exitcode = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
int ret;
/* Initialize the LCD device */
message("nxeg_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nxeg_initialize: up_lcdinitialize failed: %d\n", -ret);
g_exitcode = NXEXIT_LCDINITIALIZE;
return ERROR;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NX_DEVNO);
if (!dev)
{
message("nxeg_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
g_exitcode = NXEXIT_LCDGETDEV;
return ERROR;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
int ret;
/* Initialize the frame buffer device */
message("nxeg_initialize: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nxeg_initialize: up_fbinitialize failed: %d\n", -ret);
g_exitcode = NXEXIT_FBINITIALIZE;
return ERROR;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NX_VPLANE);
if (!dev)
{
message("nxeg_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NX_VPLANE);
g_exitcode = NXEXIT_FBGETVPLANE;
return ERROR;
}
#endif
/* Then open NX */
message("nxeg_initialize: Open NX\n");
g_hnx = nx_open(dev);
if (!g_hnx)
{
message("nxeg_suinitialize: nx_open failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* Name: nxeg_initialize
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
static inline int nxeg_muinitialize(void)
{
struct sched_param param;
pthread_t thread;
pid_t servrid;
int ret;
/* Set the client task priority */
param.sched_priority = CONFIG_EXAMPLES_NX_CLIENTPRIO;
ret = sched_setparam(0, &param);
if (ret < 0)
{
message("nxeg_initialize: sched_setparam failed: %d\n" , ret);
g_exitcode = NXEXIT_SCHEDSETPARAM;
return ERROR;
}
/* Start the server task */
message("nxeg_initialize: Starting nx_servertask task\n");
servrid = task_create("NX Server", CONFIG_EXAMPLES_NX_SERVERPRIO,
CONFIG_EXAMPLES_NX_STACKSIZE, nx_servertask, NULL);
if (servrid < 0)
{
message("nxeg_initialize: Failed to create nx_servertask task: %d\n", errno);
g_exitcode = NXEXIT_TASKCREATE;
return ERROR;
}
/* Wait a bit to let the server get started */
sleep(1);
/* Connect to the server */
g_hnx = nx_connect();
if (g_hnx)
{
pthread_attr_t attr;
/* Start a separate thread to listen for server events. This is probably
* the least efficient way to do this, but it makes this example flow more
* smoothly.
*/
(void)pthread_attr_init(&attr);
param.sched_priority = CONFIG_EXAMPLES_NX_LISTENERPRIO;
(void)pthread_attr_setschedparam(&attr, &param);
(void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NX_STACKSIZE);
ret = pthread_create(&thread, &attr, nx_listenerthread, NULL);
if (ret != 0)
{
printf("nxeg_initialize: pthread_create failed: %d\n", ret);
g_exitcode = NXEXIT_PTHREADCREATE;
return ERROR;
}
/* Don't return until we are connected to the server */
while (!g_connected)
{
/* Wait for the listener thread to wake us up when we really
* are connected.
*/
(void)sem_wait(&g_semevent);
}
}
else
{
message("nxeg_initialize: nx_connect failed: %d\n", errno);
g_exitcode = NXEXIT_NXCONNECT;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* Name: nxeg_initialize
****************************************************************************/
static int nxeg_initialize(void)
{
int i;
/* Initialize window colors */
for (i = 0; i < CONFIG_NX_NPLANES; i++)
{
g_color1[i] = CONFIG_EXAMPLES_NX_COLOR1;
g_color2[i] = CONFIG_EXAMPLES_NX_COLOR2;
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
g_tbcolor[i] = CONFIG_EXAMPLES_NX_TBCOLOR;
#endif
}
#ifdef CONFIG_NX_MULTIUSER
return nxeg_muinitialize();
#else
return nxeg_suinitialize();
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/nx_main
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NX_BUILTIN
# define MAIN_NAME nx_main
# define MAIN_NAME_STRING "nx_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
int MAIN_NAME(int argc, char *argv[])
{
NXEGWINDOW hwnd1;
NXEGWINDOW hwnd2;
struct nxgl_size_s size;
struct nxgl_point_s pt;
nxgl_mxpixel_t color;
int ret;
/* Initialize */
ret = nxeg_initialize();
message(MAIN_NAME_STRING ": NX handle=%p\n", g_hnx);
if (!g_hnx || ret < 0)
{
message(MAIN_NAME_STRING ": Failed to get NX handle: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
goto errout;
}
/* Get the default font handle */
g_fonthandle = nxf_getfonthandle(CONFIG_EXAMPLES_NX_FONTID);
if (!g_fonthandle)
{
message(MAIN_NAME_STRING ": Failed to get font handle: %d\n", errno);
g_exitcode = NXEXIT_FONTOPEN;
goto errout;
}
/* Set the background to the configured background color */
message(MAIN_NAME_STRING ": Set background color=%d\n", CONFIG_EXAMPLES_NX_BGCOLOR);
color = CONFIG_EXAMPLES_NX_BGCOLOR;
ret = nx_setbgcolor(g_hnx, &color);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Create window #1 */
message(MAIN_NAME_STRING ": Create window #1\n");
nxeg_initstate(&g_wstate[0], 1, CONFIG_EXAMPLES_NX_COLOR1);
hwnd1 = nxeg_openwindow(&g_nxcb, &g_wstate[0]);
message(MAIN_NAME_STRING ": hwnd1=%p\n", hwnd1);
if (!hwnd1)
{
goto errout_with_nx;
}
/* Wait until we have the screen resolution */
while (!b_haveresolution)
{
(void)sem_wait(&g_semevent);
}
message(MAIN_NAME_STRING ": Screen resolution (%d,%d)\n", g_xres, g_yres);
/* Set the size of the window 1 */
size.w = g_xres / 2;
size.h = g_yres / 2;
message(MAIN_NAME_STRING ": Set window #1 size to (%d,%d)\n", size.w, size.h);
ret = nxeg_setsize(hwnd1, &size);
if (ret < 0)
{
goto errout_with_hwnd1;
}
/* Sleep a bit -- both so that we can see the result of the above operations
* but also, in the multi-user case, so that the server can get a chance to
* actually do them!
*/
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
/* Set the position of window #1 */
pt.x = g_xres / 8;
pt.y = g_yres / 8;
message(MAIN_NAME_STRING ": Set window #1 postion to (%d,%d)\n", pt.x, pt.y);
ret = nxeg_setposition(hwnd1, &pt);
if (ret < 0)
{
goto errout_with_hwnd1;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
/* Open the toolbar */
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
message(MAIN_NAME_STRING ": Add toolbar to window #1\n");
ret = nxeq_opentoolbar(hwnd1, CONFIG_EXAMPLES_NX_TOOLBAR_HEIGHT, &g_tbcb, &g_wstate[0]);
if (ret < 0)
{
goto errout_with_hwnd1;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#endif
/* Create window #2 */
message(MAIN_NAME_STRING ": Create window #2\n");
nxeg_initstate(&g_wstate[1], 2, CONFIG_EXAMPLES_NX_COLOR2);
hwnd2 = nxeg_openwindow(&g_nxcb, &g_wstate[1]);
message(MAIN_NAME_STRING ": hwnd2=%p\n", hwnd2);
if (!hwnd2)
{
goto errout_with_hwnd1;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
/* Set the size of the window 2 == size of window 1*/
message(MAIN_NAME_STRING ": Set hwnd2 size to (%d,%d)\n", size.w, size.h);
ret = nxeg_setsize(hwnd2, &size);
if (ret < 0)
{
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
/* Set the position of window #2 */
pt.x = g_xres - size.w - pt.x;
pt.y = g_yres - size.h - pt.y;
message(MAIN_NAME_STRING ": Set hwnd2 postion to (%d,%d)\n", pt.x, pt.y);
ret = nxeg_setposition(hwnd2, &pt);
if (ret < 0)
{
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#ifndef CONFIG_EXAMPLES_NX_RAWWINDOWS
message(MAIN_NAME_STRING ": Add toolbar to window #2\n");
ret = nxeq_opentoolbar(hwnd2, CONFIG_EXAMPLES_NX_TOOLBAR_HEIGHT, &g_tbcb, &g_wstate[1]);
if (ret < 0)
{
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#endif
/* Give keyboard input to the top window -- should be window #2 */
#ifdef CONFIG_NX_KBD
message(MAIN_NAME_STRING ": Send keyboard input: %s\n", g_kbdmsg1);
ret = nx_kbdin(g_hnx, strlen((FAR const char *)g_kbdmsg1), g_kbdmsg1);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_kbdin failed: %d\n", errno);
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#endif
/* Lower window 2 */
message(MAIN_NAME_STRING ": Lower window #2\n");
ret = nxeg_lower(hwnd2);
if (ret < 0)
{
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
/* Put mouse left-button clicks all over the screen and see who responds */
#ifdef CONFIG_NX_MOUSE
nxeg_drivemouse();
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#endif
/* Give keyboard input to the top window -- should be window #1 */
#ifdef CONFIG_NX_KBD
message(MAIN_NAME_STRING ": Send keyboard input: %s\n", g_kbdmsg2);
ret = nx_kbdin(g_hnx, strlen((FAR const char *)g_kbdmsg2), g_kbdmsg2);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_kbdin failed: %d\n", errno);
goto errout_with_hwnd2;
}
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(1);
#endif
/* Raise window 2 */
message(MAIN_NAME_STRING ": Raise window #2\n");
ret = nxeg_raise(hwnd2);
if (ret < 0)
{
goto errout_with_hwnd2;
}
/* Put mouse left-button clicks all over the screen and see who responds */
#ifdef CONFIG_NX_MOUSE
nxeg_drivemouse();
#endif
/* Sleep a bit */
message(MAIN_NAME_STRING ": Sleeping\n\n");
sleep(2);
/* Close the window 2 */
errout_with_hwnd2:
message(MAIN_NAME_STRING ": Close window #2\n");
(void)nxeg_closewindow(hwnd2, &g_wstate[1]);
/* Close the window1 */
errout_with_hwnd1:
message(MAIN_NAME_STRING ": Close window #1\n");
(void)nxeg_closewindow(hwnd1, &g_wstate[0]);
errout_with_nx:
#ifdef CONFIG_NX_MULTIUSER
/* Disconnect from the server */
message(MAIN_NAME_STRING ": Disconnect from the server\n");
nx_disconnect(g_hnx);
#else
/* Close the server */
message(MAIN_NAME_STRING ": Close NX\n");
nx_close(g_hnx);
#endif
errout:
return g_exitcode;
}

View File

@ -0,0 +1,152 @@
/****************************************************************************
* examples/nx/nx_server.c
*
* Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include "nx_internal.h"
#ifdef CONFIG_NX_MULTIUSER
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nx_servertask
****************************************************************************/
int nx_servertask(int argc, char *argv[])
{
FAR NX_DRIVERTYPE *dev;
int ret;
#if defined(CONFIG_EXAMPLES_NX_EXTERNINIT)
/* Use external graphics driver initialization */
message("nxeg_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NX_DEVNO);
if (!dev)
{
message("nxeg_initialize: up_nxdrvinit failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
g_exitcode = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
/* Initialize the LCD device */
message("nx_servertask: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nx_servertask: up_lcdinitialize failed: %d\n", -ret);
return 1;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NX_DEVNO);
if (!dev)
{
message("nx_servertask: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NX_DEVNO);
return 2;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
/* Initialize the frame buffer device */
message("nx_servertask: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nx_servertask: up_fbinitialize failed: %d\n", -ret);
return 1;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NX_VPLANE);
if (!dev)
{
message("nx_servertask: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NX_VPLANE);
return 2;
}
#endif
/* Then start the server */
ret = nx_run(dev);
message("nx_servertask: nx_run returned: %d\n", errno);
return 3;
}
#endif /* CONFIG_NX_MULTIUSER */

View File

@ -0,0 +1,93 @@
############################################################################
# apps/examples/nxffs/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# Hello, World! Example
ASRCS =
CSRCS = nxffs_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,947 @@
/****************************************************************************
* examples/nxffs/nxffs_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/mount.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <crc32.h>
#include <debug.h>
#include <nuttx/mtd.h>
#include <nuttx/nxffs.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* The default is to use the RAM MTD device at drivers/mtd/rammtd.c. But
* an architecture-specific MTD driver can be used instead by defining
* CONFIG_EXAMPLES_NXFFS_ARCHINIT. In this case, the initialization logic
* will call nxffs_archinitialize() to obtain the MTD driver instance.
*/
#ifndef CONFIG_EXAMPLES_NXFFS_ARCHINIT
/* This must exactly match the default configuration in drivers/mtd/rammtd.c */
# ifndef CONFIG_RAMMTD_BLOCKSIZE
# define CONFIG_RAMMTD_BLOCKSIZE 512
# endif
# ifndef CONFIG_RAMMTD_ERASESIZE
# define CONFIG_RAMMTD_ERASESIZE 4096
# endif
# ifndef CONFIG_EXAMPLES_NXFFS_NEBLOCKS
# define CONFIG_EXAMPLES_NXFFS_NEBLOCKS (32)
# endif
# undef CONFIG_EXAMPLES_NXFFS_BUFSIZE
# define CONFIG_EXAMPLES_NXFFS_BUFSIZE \
(CONFIG_RAMMTD_ERASESIZE * CONFIG_EXAMPLES_NXFFS_NEBLOCKS)
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_MAXNAME
# define CONFIG_EXAMPLES_NXFFS_MAXNAME 128
#endif
#if CONFIG_EXAMPLES_NXFFS_MAXNAME > 255
# undef CONFIG_EXAMPLES_NXFFS_MAXNAME
# define CONFIG_EXAMPLES_NXFFS_MAXNAME 255
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_MAXFILE
# define CONFIG_EXAMPLES_NXFFS_MAXFILE 8192
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_MAXIO
# define CONFIG_EXAMPLES_NXFFS_MAXIO 347
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_MAXOPEN
# define CONFIG_EXAMPLES_NXFFS_MAXOPEN 512
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_MOUNTPT
# define CONFIG_EXAMPLES_NXFFS_MOUNTPT "/mnt/nxffs"
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_NLOOPS
# define CONFIG_EXAMPLES_NXFFS_NLOOPS 100
#endif
#ifndef CONFIG_EXAMPLES_NXFFS_VERBOSE
# define CONFIG_EXAMPLES_NXFFS_VERBOSE 0
#endif
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_FS)
# define message lib_rawprintf
# define msgflush()
#else
# define message printf
# define msgflush() fflush(stdout);
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct nxffs_filedesc_s
{
FAR char *name;
bool deleted;
size_t len;
uint32_t crc;
};
/****************************************************************************
* Private Data
****************************************************************************/
/* Pre-allocated simulated flash */
#ifndef CONFIG_EXAMPLES_NXFFS_ARCHINIT
static uint8_t g_simflash[CONFIG_EXAMPLES_NXFFS_BUFSIZE];
#endif
static uint8_t g_fileimage[CONFIG_EXAMPLES_NXFFS_MAXFILE];
static struct nxffs_filedesc_s g_files[CONFIG_EXAMPLES_NXFFS_MAXOPEN];
static const char g_mountdir[] = CONFIG_EXAMPLES_NXFFS_MOUNTPT "/";
static int g_nfiles;
static int g_ndeleted;
static struct mallinfo g_mmbefore;
static struct mallinfo g_mmprevious;
static struct mallinfo g_mmafter;
/****************************************************************************
* External Functions
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXFFS_ARCHINIT
extern FAR struct mtd_dev_s *nxffs_archinitialize(void);
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxffs_memusage
****************************************************************************/
static void nxffs_showmemusage(struct mallinfo *mmbefore,
struct mallinfo *mmafter)
{
message("VARIABLE BEFORE AFTER\n");
message("======== ======== ========\n");
message("arena %8x %8x\n", mmbefore->arena, mmafter->arena);
message("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks);
message("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk);
message("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks);
message("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks);
}
/****************************************************************************
* Name: nxffs_loopmemusage
****************************************************************************/
static void nxffs_loopmemusage(void)
{
/* 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 loop */
message("\nEnd of loop memory usage:\n");
nxffs_showmemusage(&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
}
/****************************************************************************
* Name: nxffs_endmemusage
****************************************************************************/
static void nxffs_endmemusage(void)
{
#ifdef CONFIG_CAN_PASS_STRUCTS
g_mmafter = mallinfo();
#else
(void)mallinfo(&g_mmafter);
#endif
message("\nFinal memory usage:\n");
nxffs_showmemusage(&g_mmbefore, &g_mmafter);
}
/****************************************************************************
* Name: nxffs_randchar
****************************************************************************/
static inline char nxffs_randchar(void)
{
int value = rand() % 63;
if (value == 0)
{
return '/';
}
else if (value <= 10)
{
return value + '0' - 1;
}
else if (value <= 36)
{
return value + 'a' - 11;
}
else /* if (value <= 62) */
{
return value + 'A' - 37;
}
}
/****************************************************************************
* Name: nxffs_randname
****************************************************************************/
static inline void nxffs_randname(FAR struct nxffs_filedesc_s *file)
{
int dirlen;
int maxname;
int namelen;
int alloclen;
int i;
dirlen = strlen(g_mountdir);
maxname = CONFIG_EXAMPLES_NXFFS_MAXNAME - dirlen;
namelen = (rand() % maxname) + 1;
alloclen = namelen + dirlen;
file->name = (FAR char*)malloc(alloclen + 1);
if (!file->name)
{
message("ERROR: Failed to allocate name, length=%d\n", namelen);
msgflush();
exit(5);
}
memcpy(file->name, g_mountdir, dirlen);
for (i = dirlen; i < alloclen; i++)
{
file->name[i] = nxffs_randchar();
}
file->name[alloclen] = '\0';
}
/****************************************************************************
* Name: nxffs_randfile
****************************************************************************/
static inline void nxffs_randfile(FAR struct nxffs_filedesc_s *file)
{
int i;
file->len = (rand() % CONFIG_EXAMPLES_NXFFS_MAXFILE) + 1;
for (i = 0; i < file->len; i++)
{
g_fileimage[i] = nxffs_randchar();
}
file->crc = crc32(g_fileimage, file->len);
}
/****************************************************************************
* Name: nxffs_freefile
****************************************************************************/
static void nxffs_freefile(FAR struct nxffs_filedesc_s *file)
{
if (file->name)
{
free(file->name);
}
memset(file, 0, sizeof(struct nxffs_filedesc_s));
}
/****************************************************************************
* Name: nxffs_wrfile
****************************************************************************/
static inline int nxffs_wrfile(FAR struct nxffs_filedesc_s *file)
{
size_t offset;
int fd;
int ret;
/* Create a random file */
nxffs_randname(file);
nxffs_randfile(file);
fd = open(file->name, O_WRONLY | O_CREAT | O_EXCL, 0666);
if (fd < 0)
{
/* If it failed because there is no space on the device, then don't
* complain.
*/
if (errno != ENOSPC)
{
message("ERROR: Failed to open file for writing: %d\n", errno);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
}
nxffs_freefile(file);
return ERROR;
}
/* Write a random amount of data to the file */
for (offset = 0; offset < file->len; )
{
size_t maxio = (rand() % CONFIG_EXAMPLES_NXFFS_MAXIO) + 1;
size_t nbytestowrite = file->len - offset;
ssize_t nbyteswritten;
if (nbytestowrite > maxio)
{
nbytestowrite = maxio;
}
nbyteswritten = write(fd, &g_fileimage[offset], nbytestowrite);
if (nbyteswritten < 0)
{
int err = errno;
/* If the write failed because there is no space on the device,
* then don't complain.
*/
if (err != ENOSPC)
{
message("ERROR: Failed to write file: %d\n", err);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Write offset: %d\n", offset);
message(" Write size: %d\n", nbytestowrite);
ret = ERROR;
}
close(fd);
/* Remove any garbage file that might have been left behind */
ret = unlink(file->name);
if (ret < 0)
{
message(" Failed to remove partial file\n");
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message(" Successfully removed partial file\n");
#endif
}
nxffs_freefile(file);
return ERROR;
}
else if (nbyteswritten != nbytestowrite)
{
message("ERROR: Partial write:\n");
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Write offset: %d\n", offset);
message(" Write size: %d\n", nbytestowrite);
message(" Written: %d\n", nbyteswritten);
}
offset += nbyteswritten;
}
close(fd);
return OK;
}
/****************************************************************************
* Name: nxffs_fillfs
****************************************************************************/
static int nxffs_fillfs(void)
{
FAR struct nxffs_filedesc_s *file;
int ret;
int i;
/* Create a file for each unused file structure */
for (i = 0; i < CONFIG_EXAMPLES_NXFFS_MAXOPEN; i++)
{
file = &g_files[i];
if (file->name == NULL)
{
ret = nxffs_wrfile(file);
if (ret < 0)
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message("ERROR: Failed to write file %d\n", i);
#endif
return ERROR;
}
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message(" Created file %s\n", file->name);
#endif
g_nfiles++;
}
}
return OK;
}
/****************************************************************************
* Name: nxffs_rdblock
****************************************************************************/
static ssize_t nxffs_rdblock(int fd, FAR struct nxffs_filedesc_s *file,
size_t offset, size_t len)
{
size_t maxio = (rand() % CONFIG_EXAMPLES_NXFFS_MAXIO) + 1;
ssize_t nbytesread;
if (len > maxio)
{
len = maxio;
}
nbytesread = read(fd, &g_fileimage[offset], len);
if (nbytesread < 0)
{
message("ERROR: Failed to read file: %d\n", errno);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Read offset: %d\n", offset);
message(" Read size: %d\n", len);
return ERROR;
}
else if (nbytesread == 0)
{
#if 0 /* No... we do this on purpose sometimes */
message("ERROR: Unexpected end-of-file:\n");
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Read offset: %d\n", offset);
message(" Read size: %d\n", len);
#endif
return ERROR;
}
else if (nbytesread != len)
{
message("ERROR: Partial read:\n");
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Read offset: %d\n", offset);
message(" Read size: %d\n", len);
message(" Bytes read: %d\n", nbytesread);
}
return nbytesread;
}
/****************************************************************************
* Name: nxffs_rdfile
****************************************************************************/
static inline int nxffs_rdfile(FAR struct nxffs_filedesc_s *file)
{
size_t ntotalread;
ssize_t nbytesread;
uint32_t crc;
int fd;
/* Open the file for reading */
fd = open(file->name, O_RDONLY);
if (fd < 0)
{
if (!file->deleted)
{
message("ERROR: Failed to open file for reading: %d\n", errno);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
}
return ERROR;
}
/* Read all of the data info the fileimage buffer using random read sizes */
for (ntotalread = 0; ntotalread < file->len; )
{
nbytesread = nxffs_rdblock(fd, file, ntotalread, file->len - ntotalread);
if (nbytesread < 0)
{
close(fd);
return ERROR;
}
ntotalread += nbytesread;
}
/* Verify the file image CRC */
crc = crc32(g_fileimage, file->len);
if (crc != file->crc)
{
message("ERROR: Bad CRC: %d vs %d\n", crc, file->crc);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
close(fd);
return ERROR;
}
/* Try reading past the end of the file */
nbytesread = nxffs_rdblock(fd, file, ntotalread, 1024) ;
if (nbytesread > 0)
{
message("ERROR: Read past the end of file\n");
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" Bytes read: %d\n", nbytesread);
close(fd);
return ERROR;
}
close(fd);
return OK;
}
/****************************************************************************
* Name: nxffs_verifyfs
****************************************************************************/
static int nxffs_verifyfs(void)
{
FAR struct nxffs_filedesc_s *file;
int ret;
int i;
/* Create a file for each unused file structure */
for (i = 0; i < CONFIG_EXAMPLES_NXFFS_MAXOPEN; i++)
{
file = &g_files[i];
if (file->name != NULL)
{
ret = nxffs_rdfile(file);
if (ret < 0)
{
if (file->deleted)
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message("Deleted file %d OK\n", i);
#endif
nxffs_freefile(file);
g_ndeleted--;
g_nfiles--;
}
else
{
message("ERROR: Failed to read a file: %d\n", i);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
return ERROR;
}
}
else
{
if (file->deleted)
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message("Succesffully read a deleted file\n");
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
#endif
nxffs_freefile(file);
g_ndeleted--;
g_nfiles--;
return ERROR;
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message(" Verifed file %s\n", file->name);
#endif
}
}
}
}
return OK;
}
/****************************************************************************
* Name: nxffs_delfiles
****************************************************************************/
static int nxffs_delfiles(void)
{
FAR struct nxffs_filedesc_s *file;
int ndel;
int ret;
int i;
int j;
/* Are there any files to be deleted? */
int nfiles = g_nfiles - g_ndeleted;
if (nfiles < 1)
{
return 0;
}
/* Yes... How many files should we delete? */
ndel = (rand() % nfiles) + 1;
/* Now pick which files to delete */
for (i = 0; i < ndel; i++)
{
/* Guess a file index */
int ndx = (rand() % (g_nfiles - g_ndeleted));
/* And delete the next undeleted file after that random index */
for (j = ndx + 1; j != ndx;)
{
file = &g_files[j];
if (file->name && !file->deleted)
{
ret = unlink(file->name);
if (ret < 0)
{
message("ERROR: Unlink %d failed: %d\n", i+1, errno);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" File index: %d\n", j);
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message(" Deleted file %s\n", file->name);
#endif
file->deleted = true;
g_ndeleted++;
break;
}
}
/* Increment the index and test for wrap-around */
if (++j >= CONFIG_EXAMPLES_NXFFS_MAXOPEN)
{
j = 0;
}
}
}
return OK;
}
/****************************************************************************
* Name: nxffs_delallfiles
****************************************************************************/
static int nxffs_delallfiles(void)
{
FAR struct nxffs_filedesc_s *file;
int ret;
int i;
for (i = 0; i < CONFIG_EXAMPLES_NXFFS_MAXOPEN; i++)
{
file = &g_files[i];
if (file->name)
{
ret = unlink(file->name);
if (ret < 0)
{
message("ERROR: Unlink %d failed: %d\n", i+1, errno);
message(" File name: %s\n", file->name);
message(" File size: %d\n", file->len);
message(" File index: %d\n", i);
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message(" Deleted file %s\n", file->name);
#endif
nxffs_freefile(file);
}
}
}
g_nfiles = 0;
g_ndeleted = 0;
return OK;
}
/****************************************************************************
* Name: nxffs_directory
****************************************************************************/
static int nxffs_directory(void)
{
DIR *dirp;
FAR struct dirent *entryp;
int number;
/* Open the directory */
dirp = opendir(CONFIG_EXAMPLES_NXFFS_MOUNTPT);
if (!dirp)
{
/* Failed to open the directory */
message("ERROR: Failed to open directory '%s': %d\n",
CONFIG_EXAMPLES_NXFFS_MOUNTPT, errno);
return ERROR;
}
/* Read each directory entry */
message("Directory:\n");
number = 1;
do
{
entryp = readdir(dirp);
if (entryp)
{
message("%2d. Type[%d]: %s Name: %s\n",
number, entryp->d_type,
entryp->d_type == DTYPE_FILE ? "File " : "Error",
entryp->d_name);
}
number++;
}
while (entryp != NULL);
closedir(dirp);
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
FAR struct mtd_dev_s *mtd;
unsigned int i;
int ret;
/* Seed the random number generated */
srand(0x93846);
/* Create and initialize a RAM MTD device instance */
#ifdef CONFIG_EXAMPLES_NXFFS_ARCHINIT
mtd = nxffs_archinitialize();
#else
mtd = rammtd_initialize(g_simflash, CONFIG_EXAMPLES_NXFFS_BUFSIZE);
#endif
if (!mtd)
{
message("ERROR: Failed to create RAM MTD instance\n");
msgflush();
exit(1);
}
/* Initialize to provide NXFFS on an MTD interface */
ret = nxffs_initialize(mtd);
if (ret < 0)
{
message("ERROR: NXFFS initialization failed: %d\n", -ret);
msgflush();
exit(2);
}
/* Mount the file system */
ret = mount(NULL, CONFIG_EXAMPLES_NXFFS_MOUNTPT, "nxffs", 0, NULL);
if (ret < 0)
{
message("ERROR: Failed to mount the NXFFS volume: %d\n", errno);
msgflush();
exit(3);
}
/* Set up memory monitoring */
#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
/* Loop a few times ... file the file system with some random, files,
* delete some files randomly, fill the file system with more random file,
* delete, etc. This beats the FLASH very hard!
*/
#if CONFIG_EXAMPLES_NXFFS_NLOOPS == 0
for (i = 0; ; i++)
#else
for (i = 1; i <= CONFIG_EXAMPLES_NXFFS_NLOOPS; i++)
#endif
{
/* Write a files to the NXFFS file system until either (1) all of the
* open file structures are utilized or until (2) NXFFS reports an error
* (hopefully that the file system is full)
*/
message("\n=== FILLING %d =============================\n", i);
ret = nxffs_fillfs();
message("Filled file system\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
nxffs_dump(mtd, CONFIG_EXAMPLES_NXFFS_VERBOSE);
/* Directory listing */
nxffs_directory();
/* Verify all files written to FLASH */
ret = nxffs_verifyfs();
if (ret < 0)
{
message("ERROR: Failed to verify files\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message("Verified!\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
#endif
}
/* Delete some files */
message("\n=== DELETING %d ============================\n", i);
ret = nxffs_delfiles();
if (ret < 0)
{
message("ERROR: Failed to delete files\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
}
else
{
message("Deleted some files\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
}
nxffs_dump(mtd, CONFIG_EXAMPLES_NXFFS_VERBOSE);
/* Directory listing */
nxffs_directory();
/* Verify all files written to FLASH */
ret = nxffs_verifyfs();
if (ret < 0)
{
message("ERROR: Failed to verify files\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
}
else
{
#if CONFIG_EXAMPLES_NXFFS_VERBOSE != 0
message("Verified!\n");
message(" Number of files: %d\n", g_nfiles);
message(" Number deleted: %d\n", g_ndeleted);
#endif
}
/* Show memory usage */
nxffs_loopmemusage();
msgflush();
}
/* Delete all files then show memory usage again */
nxffs_delallfiles();
nxffs_endmemusage();
msgflush();
return 0;
}

View File

@ -0,0 +1,98 @@
############################################################################
# apps/examples/nxflat/Makefile
#
# Copyright (C) 2008, 2010-2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NXFLAT Example
ASRCS =
CSRCS = nxflat_main.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# Common build
VPATH =
all: .built
.PHONY: headers clean depend disclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
headers:
@$(MAKE) -C tests TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" CROSSDEV=$(CROSSDEV)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
context:
# We can't make dependencies in this directory because the required
# header files may not yet exist.
.depend:
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,224 @@
/****************************************************************************
* examples/nxflat/nxflat_main.c
*
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <sys/mount.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/ramdisk.h>
#include <nuttx/binfmt.h>
#include <nuttx/nxflat.h>
#include "tests/romfs.h"
#include "tests/dirlist.h"
#include "tests/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
/* Describe the ROMFS file system */
#define SECTORSIZE 512
#define NSECTORS(b) (((b)+SECTORSIZE-1)/SECTORSIZE)
#define ROMFSDEV "/dev/ram0"
#define MOUNTPT "/mnt/romfs"
/* If CONFIG_DEBUG is enabled, use dbg instead of printf so that the
* output will be synchronous with the debug output.
*/
#ifdef CONFIG_CPP_HAVE_VARARGS
# ifdef CONFIG_DEBUG
# define message(format, arg...) dbg(format, ##arg)
# define err(format, arg...) dbg(format, ##arg)
# else
# define message(format, arg...) printf(format, ##arg)
# define err(format, arg...) fprintf(stderr, format, ##arg)
# endif
#else
# ifdef CONFIG_DEBUG
# define message dbg
# define err dbg
# else
# define message printf
# define err printf
# endif
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
static const char delimiter[] =
"****************************************************************************";
static char path[128];
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: testheader
****************************************************************************/
static inline void testheader(FAR const char *progname)
{
message("\n%s\n* Executing %s\n%s\n\n", delimiter, progname, delimiter);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start
****************************************************************************/
int user_start(int argc, char *argv[])
{
struct binary_s bin;
int ret;
int i;
/* Initialize the NXFLAT binary loader */
message("Initializing the NXFLAT binary loader\n");
ret = nxflat_initialize();
if (ret < 0)
{
err("ERROR: Initialization of the NXFLAT loader failed: %d\n", ret);
exit(1);
}
/* Create a ROM disk for the ROMFS filesystem */
message("Registering romdisk\n");
ret = romdisk_register(0, romfs_img, NSECTORS(romfs_img_len), SECTORSIZE);
if (ret < 0)
{
err("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)
{
err("ERROR: mount(%s,%s,romfs) failed: %s\n",
ROMFSDEV, MOUNTPT, errno);
nxflat_uninitialize();
}
/* Now excercise every progrm in the ROMFS file system */
for (i = 0; dirlist[i]; i++)
{
testheader(dirlist[i]);
memset(&bin, 0, sizeof(struct binary_s));
snprintf(path, 128, "%s/%s", MOUNTPT, dirlist[i]);
bin.filename = path;
bin.exports = exports;
bin.nexports = NEXPORTS;
ret = load_module(&bin);
if (ret < 0)
{
err("ERROR: Failed to load program '%s'\n", dirlist[i]);
exit(1);
}
ret = exec_module(&bin, 50);
if (ret < 0)
{
err("ERROR: Failed to execute program '%s'\n", dirlist[i]);
unload_module(&bin);
}
message("Wait a bit for test completion\n");
sleep(4);
}
message("End-of-Test.. Exit-ing\n");
return 0;
}

View File

@ -0,0 +1,103 @@
############################################################################
# apps/examples/nxflat/tests/Makefile
#
# Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# 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.
#
############################################################################
# Most of these do no build yet
#SUBDIRS = errno hello hello++ longjmp mutex pthread signal task struct
SUBDIRS = errno hello mutex pthread task struct
NXFLAT_DIR = $(APPDIR)/examples/nxflat
TESTS_DIR = $(NXFLAT_DIR)/tests
ROMFS_DIR = $(TESTS_DIR)/romfs
ROMFS_IMG = $(TESTS_DIR)/romfs.img
ROMFS_HDR = $(TESTS_DIR)/romfs.h
ROMFS_DIRLIST = $(TESTS_DIR)/dirlist.h
SYMTAB = $(TESTS_DIR)/symtab.h
define DIR_template
$(1)_$(2):
@$(MAKE) -C $(1) $(3) TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)" ROMFS_DIR="$(ROMFS_DIR)" CROSSDEV=$(CROSSDEV)
endef
all: $(ROMFS_HDR) $(ROMFS_DIRLIST) $(SYMTAB)
.PHONY: all build clean install populate
$(foreach DIR, $(SUBDIRS), $(eval $(call DIR_template,$(DIR),build, all)))
$(foreach DIR, $(SUBDIRS), $(eval $(call DIR_template,$(DIR),clean,clean)))
$(foreach DIR, $(SUBDIRS), $(eval $(call DIR_template,$(DIR),install,install)))
# Build program(s) in each sud-directory
build: $(foreach DIR, $(SUBDIRS), $(DIR)_build)
# Install each program in the romfs directory
install: $(foreach DIR, $(SUBDIRS), $(DIR)_install)
# Create the romfs directory
$(ROMFS_DIR):
@mkdir $(ROMFS_DIR)
# Populate the romfs directory
populate: $(ROMFS_DIR) build install
# Create the romfs.img file from the populated romfs directory
$(ROMFS_IMG): populate
@genromfs -f $@ -d $(ROMFS_DIR) -V "NXFLATTEST"
# Create the romfs.h header file from the romfs.img file
$(ROMFS_HDR) : $(ROMFS_IMG)
@(cd $(TESTS_DIR); xxd -i romfs.img | sed -e "s/^unsigned/static const unsigned/g" >$@)
# Create the dirlist.h header file from the romfs directory
$(ROMFS_DIRLIST) : populate
@$(TESTS_DIR)/mkdirlist.sh $(ROMFS_DIR) >$@
# Create the exported symbol table list from the derived *-thunk.S files
$(SYMTAB): build
@$(TESTS_DIR)/mksymtab.sh $(TESTS_DIR) >$@
# Clean each subdirectory
clean: $(foreach DIR, $(SUBDIRS), $(DIR)_clean)
@rm -f $(ROMFS_HDR) $(ROMFS_IMG) $(SYMTAB)
@rm -rf $(ROMFS_DIR)

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/hello/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = errno
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,83 @@
/****************************************************************************
* examples/nxflat/tests/errno/errno.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <errno.h>
/****************************************************************************
* Included Files
****************************************************************************/
static const char g_nonexistent[] = "aflav-sautga-ay";
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
FILE *test_stream;
/* Try using stdout and stderr explicitly. These are global variables
* exported from the base code.
*/
fprintf(stdout, "Hello, World on stdout\n");
fprintf(stderr, "Hello, World on stderr\n");
/* Try opening a non-existent file using buffered IO. */
test_stream = fopen(g_nonexistent, "r");
if (test_stream)
{
fprintf(stderr, "Hmm... Delete \"%s\" and try this again\n",
g_nonexistent);
exit(1);
}
/* Now print the errno on stderr. Errno is also a global
* variable exported by the base code.
*/
fprintf(stderr, "We failed to open \"%s!\" errno is %d\n",
g_nonexistent, errno);
return 0;
}

View File

@ -0,0 +1,180 @@
############################################################################
# examples/nxflat/tests/hello/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN1 = hello++1
BIN2 = hello++2
BIN3 = hello++3
#BIN4 = hello++4
ALL_BIN = $(BIN1) $(BIN2) $(BIN3) $(BIN4)
R1SRCS1 = $(BIN1).c
R1OBJS1 = $(R1SRCS1:.c=.o)
R2SRC1 = $(BIN1)-thunk.S
R2OBJ1 = $(R2SRC1:.S=.o)
R1SRCS2 = $(BIN2).c
R1OBJS2 = $(R1SRCS2:.c=.o)
R2SRC2 = $(BIN2)-thunk.S
R2OBJ2 = $(R2SRC2:.S=.o)
R1SRCS3 = $(BIN3).c
R1OBJS3 = $(R1SRCS3:.c=.o)
R2SRC3 = $(BIN3)-thunk.S
R2OBJ3 = $(R2SRC3:.S=.o)
#R1SRCS4 = $(BIN4).c
#R1OBJS4 = $(R1SRCS4:.c=.o)
#R2SRC4 = $(BIN4)-thunk.S
#R2OBJ4 = $(R2SRC4:.S=.o)
DERIVED = $(R2SRC1) $(R2SRC2) $(R2SRC3) $(R2SRC4)
R1CXXOBJS = $(R1OBJS1) $(R1OBJS2) $(R1OBJS3) # $(R1OBJS4)
R2AOBJS = $(R2OBJ1) $(R2OBJ2) $(R2OBJ3) # $(R2OBJ4)
LIBSTDC_STUBS_DIR = $(TOPDIR)/libxx
LIBSTDC_STUBS_LIB = $(LIBSTDC_STUBS_DIR)/liblibxx.a
all: $(BIN1) $(BIN2) $(BIN3) # $(BIN4)
$(R1CXXOBJS): %.o: %.cpp
@echo "CC: $<"
@$(CXX) -c $(CXXPICFLAGS) $< -o $@
$(R2AOBJS): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
# This contains libstdc++ stubs to that you can build C++ code
# without actually having libstdc++
$(LIBSTDC_STUBS_LIB):
@$(MAKE) -C $(LIBSTDC_STUBS_DIR) TOPDIR=$(TOPDIR)
# BIN1 and BIN2 link just like C code because they contain no
# static constructors. BIN1 is equivalent to a C hello world;
# BIN2 contains a class that implements hello world, but it is
# not statically initialized.
$(BIN1).r1: $(R1OBJS1)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC1): $(BIN1).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN1).r2: $(R2OBJ1)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS1) $(R2OBJ1)
$(BIN1): $(BIN1).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
$(BIN2).r1: $(R1OBJS2) $(LIBSTDC_STUBS_LIB)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC2): $(BIN2).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN2).r2: $(R2OBJ2)
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS2) $(R2OBJ2)
$(BIN2): $(BIN2).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
# BIN3 and BIN4 require that we include --cxx in the xflat-ld command.
# This will instruct xflat-ld that we want it to put together the correct
# startup files to handle the C++ static initializers.
#
# BIN3 is equivalent to BIN2 except that is uses static initializers
$(BIN3).r1: $(R1OBJS3) $(LIBSTDC_STUBS_LIB)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC3): $(BIN3).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN3).r2: $(R2OBJ3)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS3) $(R2OBJ3)
$(BIN3): $(BIN3).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
# BIN4 is similar to BIN3 except that it uses the streams code from libstdc++
#
# NOTE: libstdc++ is not available for XFLAT as of this writing
#
#$(BIN4).r1: $(R1OBJS4) $(LIBSTDC_STUBS_LIB)
# @echo "LD: $<"
# $(LD) $(NXFLATLDFLAGS1) -o $@ $^
#
#$(R2SRC4): $(BIN4).r1
# @echo "MK: $<"
# $(MKNXFLAT) -o $@ $^
#
#$(BIN4).r2: $(R2OBJ4)# @echo "LD: $<"
# $(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS4) $(R2OBJ4)
#
#$(BIN4): $(BIN4).r2
# @echo "LD: $<"
# $(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(ALL_BIN) $(DERIVED) *.o *.r1 *.r2 *~ .*.swp core
install: $(ALL_BIN)
@install -D $(BIN1) $(ROMFS_DIR)/$(BIN1)
@install -D $(BIN2) $(ROMFS_DIR)/$(BIN2)
@install -D $(BIN3) $(ROMFS_DIR)/$(BIN3)
# @install -D $(BIN4) $(ROMFS_DIR)/$(BIN4)

View File

@ -0,0 +1,60 @@
/////////////////////////////////////////////////////////////////////////////
// examples/nxflat/tests/hello++/hello++1.c
//
// Copyright (C) 2009 Gregory Nutt. All rights reserved.
// Author: Gregory Nutt <spudmonkey@racsa.co.cr>
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////
//
// This is an trivial version of "Hello, World" program. It illustrates
// that we can build C programs using the C++ compiler.
//
// - Building a C++ program to use the C library
// - No class creation
// - NO Streams
// - NO Static constructor and destructors
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Included Files
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
/////////////////////////////////////////////////////////////////////////////
// Public Functions
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
printf("Hello, World!\n");
return 0;
}

View File

@ -0,0 +1,123 @@
/////////////////////////////////////////////////////////////////////////////
// examples/nxflat/tests/hello++/hello++2.c
//
// Copyright (C) 2009 Gregory Nutt. All rights reserved.
// Author: Gregory Nutt <spudmonkey@racsa.co.cr>
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////
//
// This is an another trivial version of "Hello, World" design. It illustrates
//
// - Building a C++ program to use the C library
// - Basic class creation
// - NO Streams
// - NO Static constructor and destructors
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Included Files
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
/////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////
class CThingSayer
{
const char *szWhatToSay;
public:
CThingSayer(void)
{
printf("CThingSayer::CThingSayer: I am!\n");
szWhatToSay = (const char*)NULL;
}
~CThingSayer(void)
{
printf("CThingSayer::~CThingSayer: I cease to be\n");
if (szWhatToSay)
{
printf("CThingSayer::~CThingSayer: I will never say '%s' again\n",
szWhatToSay);
}
szWhatToSay = (const char*)NULL;
}
void Initialize(const char *czSayThis)
{
printf("CThingSayer::Initialize: When told, I will say '%s'\n",
czSayThis);
szWhatToSay = czSayThis;
}
void SayThing(void)
{
printf("CThingSayer::SayThing: I am now saying '%s'\n", szWhatToSay);
}
};
/////////////////////////////////////////////////////////////////////////////
// Public Functions
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
CThingSayer *MyThingSayer;
printf("main: Started. Creating MyThingSayer\n");
// Create an instance of the CThingSayer class
// We should see the message from constructor, CThingSayer::CThingSayer(),
MyThingSayer = new CThingSayer;
printf("main: Created MyThingSayer=0x%08lx\n", (long)MyThingSayer);
// Tell MyThingSayer that "Hello, World!" is the string to be said
printf("main: Calling MyThingSayer->Initialize\n");;
MyThingSayer->Initialize("Hello, World!");
// Tell MyThingSayer to say the thing we told it to say
printf("main: Calling MyThingSayer->SayThing\n");;
MyThingSayer->SayThing();
// We should see the message from the destructor,
// CThingSayer::~CThingSayer(), AFTER we see the following
printf("main: Destroying MyThingSayer\n");
delete MyThingSayer;
printf("main: Returning\n");;
return 0;
}

View File

@ -0,0 +1,132 @@
/////////////////////////////////////////////////////////////////////////////
// examples/nxflat/tests/hello++/hello++3.c
//
// Copyright (C) 2009 Gregory Nutt. All rights reserved.
// Author: Gregory Nutt <spudmonkey@racsa.co.cr>
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////
//
// This is an another trivial version of "Hello, World" design. It illustrates
//
// - Building a C++ program to use the C library and stdio
// - Basic class creation with virtual methods.
// - Static constructor and destructors (in main program only)
// - NO Streams
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Included Files
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
/////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////
class CThingSayer
{
const char *szWhatToSay;
public:
CThingSayer(void);
virtual ~CThingSayer(void);
virtual void Initialize(const char *czSayThis);
virtual void SayThing(void);
};
// A static instance of the CThingSayer class. This instance MUST
// be constructed by the system BEFORE the program is started at
// main() and must be destructed by the system AFTER the main()
// returns to the system
static CThingSayer MyThingSayer;
// These are implementations of the methods of the CThingSayer class
CThingSayer::CThingSayer(void)
{
printf("CThingSayer::CThingSayer: I am!\n");
szWhatToSay = (const char*)NULL;
}
CThingSayer::~CThingSayer(void)
{
printf("CThingSayer::~CThingSayer: I cease to be\n");
if (szWhatToSay)
{
printf("CThingSayer::~CThingSayer: I will never say '%s' again\n",
szWhatToSay);
}
szWhatToSay = (const char*)NULL;
}
void CThingSayer::Initialize(const char *czSayThis)
{
printf("CThingSayer::Initialize: When told, I will say '%s'\n",
czSayThis);
szWhatToSay = czSayThis;
}
void CThingSayer::SayThing(void)
{
printf("CThingSayer::SayThing: I am now saying '%s'\n", szWhatToSay);
}
/////////////////////////////////////////////////////////////////////////////
// Public Functions
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
// We should see the message from constructor, CThingSayer::CThingSayer(),
// BEFORE we see the following messages. That is proof that the
// C++ static initializer is working
printf("main: Started. MyThingSayer should already exist\n");
// Tell MyThingSayer that "Hello, World!" is the string to be said
printf("main: Calling MyThingSayer.Initialize\n");;
MyThingSayer.Initialize("Hello, World!");
// Tell MyThingSayer to say the thing we told it to say
printf("main: Calling MyThingSayer.SayThing\n");;
MyThingSayer.SayThing();
// We are finished, return. We should see the message from the
// destructor, CThingSayer::~CThingSayer(), AFTER we see the following
// message. That is proof that the C++ static destructor logic
// is working
printf("main: Returning. MyThingSayer should be destroyed\n");;
return 0;
}

View File

@ -0,0 +1,150 @@
/////////////////////////////////////////////////////////////////////////////
// examples/nxflat/tests/hello++/hello++4.c
//
// Copyright (C) 2009 Gregory Nutt. All rights reserved.
// Author: Gregory Nutt <spudmonkey@racsa.co.cr>
//
// 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.
//
/////////////////////////////////////////////////////////////////////////////
//
// This is an excessively complex version of "Hello, World" design to
// illustrate some basic properties of C++:
//
// - Building a C++ program
// - Streams / statically linked libstdc++
// - Static constructor and destructors (in main program only)
//
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Included Files
/////////////////////////////////////////////////////////////////////////////
#include <cstdio>
#include <iostream>
#ifndef NULL
# define NULL ((void*)0L)
#endif
/////////////////////////////////////////////////////////////////////////////
// Classes
/////////////////////////////////////////////////////////////////////////////
using namespace std;
// A hello world sayer class
class CThingSayer
{
const char *szWhatToSay;
public:
CThingSayer(void);
virtual ~CThingSayer(void);
virtual void Initialize(const char *czSayThis);
virtual void SayThing(void);
};
/////////////////////////////////////////////////////////////////////////////
// Private Data
/////////////////////////////////////////////////////////////////////////////
// A static instance of the CThingSayer class. This instance MUST
// be constructed by the system BEFORE the program is started at
// main() and must be destructed by the system AFTER the main()
// returns to the system
static CThingSayer MyThingSayer;
/////////////////////////////////////////////////////////////////////////////
// Method Implementations
/////////////////////////////////////////////////////////////////////////////
// These are implementations of the methods of the CThingSayer class
CThingSayer::CThingSayer(void)
{
cout << "CThingSayer::CThingSayer: I am!" << endl;
szWhatToSay = (const char*)NULL;
}
CThingSayer::~CThingSayer(void)
{
cout << "CThingSayer::~CThingSayer: I cease to be" << endl;
if (szWhatToSay)
{
cout << "CThingSayer::~CThingSayer: I will never say '"
<< szWhatToSay << "' again" << endl;
}
szWhatToSay = (const char*)NULL;
}
void CThingSayer::Initialize(const char *czSayThis)
{
cout << "CThingSayer::Initialize: When told, I will say '"
<< czSayThis << "'" << endl;
szWhatToSay = czSayThis;
}
void CThingSayer::SayThing(void)
{
cout << "CThingSayer::SayThing: I am now saying '"
<< szWhatToSay << "'" << endl;
}
/////////////////////////////////////////////////////////////////////////////
// Public Functions
/////////////////////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
// We should see the message from constructor, CThingSayer::CThingSayer(),
// BEFORE we see the following messages. That is proof that the
// C++ static initializer is working
cout << "main: Started" << endl;
// Tell MyThingSayer that "Hello, World!" is the string to be said
cout << "main: Calling MyThingSayer.Initialize" << endl;
MyThingSayer.Initialize("Hello, World!");
// Tell MyThingSayer to say the thing we told it to say
cout << "main: Calling MyThingSayer.SayThing" << endl;
MyThingSayer.SayThing();
// We are finished, return. We should see the message from the
// destructor, CThingSayer::~CThingSayer(), AFTER we see the following
// message. That is proof that the C++ static destructor logic
// is working
cout << "main: Returning" << endl;
return 0;
}

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/hello/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = hello
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,78 @@
/****************************************************************************
* examples/nxflat/tests/hello/hello.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
int i;
/* Mandatory "Hello, world!" */
puts("Getting ready to say \"Hello, world\"\n");
printf("Hello, world!\n");
puts("It has been said.\n");
/* Print arguments */
printf("argc\t= %d\n", argc);
printf("argv\t= 0x%p\n", argv);
for (i = 0; i < argc; i++)
{
printf("argv[%d]\t= ", i);
if (argv[i])
{
printf("(0x%p) \"%s\"\n", argv[i], argv[i]);
}
else
{
printf("NULL?\n");
}
}
printf("argv[%d]\t= 0x%p\n", argc, argv[argc]);
printf("Goodbye, world!\n");
return 0;
}

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/longjmp/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = longjmp
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,128 @@
/****************************************************************************
* examples/nxflat/tests/longjmp/longjmp.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <setjmp.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define MAIN_VAL 47
#define FUNC_VAL 92
#define LEAF_VAL 163
#define FUNCTION_ARG MAIN_VAL
#define LEAF_ARG (FUNCTION_ARG + FUNC_VAL)
#define SETJMP_RETURN (LEAF_ARG + LEAF_VAL)
/****************************************************************************
* Private Data
****************************************************************************/
static jmp_buf env;
/****************************************************************************
* Private Functions
****************************************************************************/
static int leaf(int *some_arg)
{
int some_local_variable = *some_arg + LEAF_VAL;
printf("leaf: received %d\n", *some_arg);
if (*some_arg != LEAF_ARG)
printf("leaf: ERROR: expected %d\n", LEAF_ARG);
printf("leaf: Calling longjmp() with %d\n", some_local_variable);
longjmp(env, some_local_variable);
}
static int function(int some_arg)
{
int some_local_variable = some_arg + FUNC_VAL;
int retval;
printf("function: received %d\n", some_arg);
if (some_arg != FUNCTION_ARG)
printf("function: ERROR: expected %d\n", FUNCTION_ARG);
printf("function: Calling leaf() with %d\n", some_local_variable);
retval = leaf(&some_local_variable);
printf("function: ERROR -- leaf returned!\n");
return retval;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
int value;
printf("main: Calling setjmp\n");
value = setjmp(env);
printf("main: setjmp returned %d\n", value);
if (value == 0)
{
printf("main: Normal setjmp return\n");
printf("main: Calling function with %d\n", MAIN_VAL);
function(MAIN_VAL);
printf("main: ERROR -- function returned!\n");
return 1;
}
else if (value != SETJMP_RETURN)
{
printf("main: ERROR: Expected %d\n", SETJMP_RETURN);
return 1;
}
else
{
printf("main: SUCCESS: setjmp return from longjmp call\n");
return 0;
}
}

View File

@ -0,0 +1,35 @@
#!/bin/bash
usage="Usage: %0 <romfs-dir-path>"
dir=$1
if [ -z "$dir" ]; then
echo "ERROR: Missing <romfs-dir-path>"
echo ""
echo $usage
exit 1
fi
if [ ! -d "$dir" ]; then
echo "ERROR: Directory $dir does not exist"
echo ""
echo $usage
exit 1
fi
echo "#ifndef __EXAMPLES_NXFLAT_TESTS_DIRLIST_H"
echo "#define __EXAMPLES_NXFLAT_TESTS_DIRLIST_H"
echo ""
echo "static const char *dirlist[] ="
echo "{"
for file in `ls $dir`; do
echo " \"$file\","
done
echo " NULL"
echo "};"
echo ""
echo "#endif /* __EXAMPLES_NXFLAT_TESTS_DIRLIST_H */"

View File

@ -0,0 +1,39 @@
#!/bin/bash
usage="Usage: %0 <test-dir-path>"
dir=$1
if [ -z "$dir" ]; then
echo "ERROR: Missing <test-dir-path>"
echo ""
echo $usage
exit 1
fi
if [ ! -d "$dir" ]; then
echo "ERROR: Directory $dir does not exist"
echo ""
echo $usage
exit 1
fi
varlist=`find $dir -name "*-thunk.S"| xargs grep -h asciz | cut -f3 | sort | uniq`
echo "#ifndef __EXAMPLES_NXFLAT_TESTS_SYMTAB_H"
echo "#define __EXAMPLES_NXFLAT_TESTS_SYMTAB_H"
echo ""
echo "#include <nuttx/symtab.h>"
echo ""
echo "static const struct symtab_s exports[] = "
echo "{"
for string in $varlist; do
var=`echo $string | sed -e "s/\"//g"`
echo " {$string, $var},"
done
echo "};"
echo "#define NEXPORTS (sizeof(exports)/sizeof(struct symtab_s))"
echo ""
echo "#endif /* __EXAMPLES_NXFLAT_TESTS_SYMTAB_H */"

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/mutex/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = mutex
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,149 @@
/****************************************************************************
* examples/nxflat/tests/mutex/mutex.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
/****************************************************************************
* Private Data
****************************************************************************/
static pthread_mutex_t mut;
static volatile int my_mutex = 0;
static unsigned long nloops[2] = {0, 0};
static unsigned long nerrors[2] = {0, 0};
static volatile bool bendoftest;
/****************************************************************************
* Private Functions
****************************************************************************/
/* NOTE: it is necessary for functions that are referred to by function pointers
* pointer to be declared with global scope (at least for ARM). Otherwise,
* a relocation type that is not supported by NXFLAT is generated by GCC.
*/
void thread_func(void *parameter)
{
int my_id = (int)parameter;
int my_ndx = my_id - 1;
int i;
/* Loop 20 times. There is a 100 MS delay in the loop so this should
* take about 2 seconds. The main thread will stop this thread after
* 2 seconds by setting bendoftest in any event.
*/
for (i = 0; i < 20 && !bendoftest; i++);
{
if ((pthread_mutex_lock(&mut)) != 0)
{
printf("ERROR thread %d: pthread_mutex_lock failed\n", my_id);
}
if (my_mutex == 1)
{
printf("ERROR thread=%d: "
"my_mutex should be zero, instead my_mutex=%d\n",
my_id, my_mutex);
nerrors[my_ndx]++;
}
my_mutex = 1;
usleep(100000);
my_mutex = 0;
if ((pthread_mutex_unlock(&mut)) != 0)
{
printf("ERROR thread %d: pthread_mutex_unlock failed\n", my_id);
}
nloops[my_ndx]++;
}
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
pthread_t thread1;
pthread_t thread2;
/* Initialize the mutex */
pthread_mutex_init(&mut, NULL);
/* Start two thread instances */
printf("Starting thread 1\n");
bendoftest = false;
if ((pthread_create(&thread1, NULL, (void*)thread_func, (void*)1)) != 0)
{
fprintf(stderr, "Error in thread#1 creation\n");
}
printf("Starting thread 2\n");
if ((pthread_create(&thread2, NULL, (void*)thread_func, (void*)2)) != 0)
{
fprintf(stderr, "Error in thread#2 creation\n");
}
/* Wait a bit for the threads to do their thing. */
sleep(2);
/* Then ask them politely to stop running */
printf("Stopping threads\n");
bendoftest = true;
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
printf("\tThread1\tThread2\n");
printf("Loops\t%ld\t%ld\n", nloops[0], nloops[1]);
printf("Errors\t%ld\t%ld\n", nerrors[0], nerrors[1]);
return 0;
}

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/pthread/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = pthread
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,143 @@
/****************************************************************************
* examples/nxflat/tests/pthread/pthread.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define CHILD_ARG ((void*)0x12345678)
#define CHILD_RET ((void*)0x87654321)
/****************************************************************************
* Private Types
****************************************************************************/
enum exit_values_e
{
TESTRESULT_SUCCESS = 0,
TESTRESULT_PTHREAD_ATTR_INIT_FAIL,
TESTRESULT_PTHREAD_CREATE_FAIL,
TESTRESULT_PTHREAD_JOIN_FAIL,
TESTRESULT_CHILD_ARG_FAIL,
TESTRESULT_CHILD_RETVAL_FAIL,
};
/****************************************************************************
* External Functions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/* NOTE: it is necessary for functions that are referred to by function pointers
* pointer to be declared with global scope (at least for ARM). Otherwise,
* a relocation type that is not supported by NXFLAT is generated by GCC.
*/
void *child_start_routine(void *arg)
{
printf("CHILD: started with arg=%d\n", (int)arg);
if (arg != CHILD_ARG)
{
printf("CHILD: expected arg=%d\n", (int)CHILD_ARG);
return (void*)TESTRESULT_CHILD_ARG_FAIL;
}
sleep(2);
printf("CHILD: returning %d\n", (int)CHILD_RET);
pthread_exit(CHILD_RET);
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
pthread_attr_t attr;
pthread_t thread;
void *retval;
int status;
puts("PARENT: started\n");
status = pthread_attr_init(&attr);
if (status != 0)
{
printf("PARENT: pthread_attr_init() returned %d\n", status);
exit(TESTRESULT_PTHREAD_ATTR_INIT_FAIL);
}
printf("PARENT: calling pthread_start with arg=%d\n", (int)CHILD_ARG);
status = pthread_create(&thread, &attr, child_start_routine, CHILD_ARG);
if (status != 0)
{
printf("PARENT: pthread_create() returned %d\n", status);
exit(TESTRESULT_PTHREAD_CREATE_FAIL);
}
status = pthread_join(thread, &retval);
if (status != 0)
{
printf("PARENT pthread_join() returned %d\n", status);
exit(TESTRESULT_PTHREAD_JOIN_FAIL);
}
printf("PARENT child exitted with %d\n", (int)retval);
if (retval != CHILD_RET)
{
printf("PARENT child thread did not exit with %d\n", (int)CHILD_RET);
exit(TESTRESULT_CHILD_RETVAL_FAIL);
}
puts("PARENT returning success\n");
return TESTRESULT_SUCCESS;
}

View File

@ -0,0 +1,78 @@
############################################################################
# examples/nxflat/tests/signal/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = signal
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,308 @@
/****************************************************************************
* examples/nxflat/tests/signal/signal.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
/****************************************************************************
* Definitions
****************************************************************************/
#define USEC_PER_MSEC 1000
#define MSEC_PER_SEC 1000
#define USEC_PER_SEC (USEC_PER_MSEC * MSEC_PER_SEC)
#define SHORT_DELAY (USEC_PER_SEC / 3)
/****************************************************************************
* Private Data
****************************************************************************/
static int sigusr1_rcvd = 0;
static int sigusr2_rcvd = 0;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: sigusr1_sighandler
****************************************************************************/
/* NOTE: it is necessary for functions that are referred to by function pointers
* pointer to be declared with global scope (at least for ARM). Otherwise,
* a relocation type that is not supported by NXFLAT is generated by GCC.
*/
void sigusr1_sighandler(int signo)
{
printf("sigusr1_sighandler: Received SIGUSR1, signo=%d\n", signo);
sigusr1_rcvd = 1;
}
/****************************************************************************
* Name: sigusr2_sigaction
***************************************************************************/
/* NOTE: it is necessary for functions that are referred to by function pointers
* pointer to be declared with global scope (at least for ARM). Otherwise,
* a relocation type that is not supported by NXFLAT is generated by GCC.
*/
#ifdef __USE_POSIX199309
void sigusr2_sigaction(int signo, siginfo_t *siginfo, void *arg)
{
printf("sigusr2_sigaction: Received SIGUSR2, signo=%d siginfo=%p arg=%p\n",
signo, siginfo, arg);
#ifdef HAVE_SIGQUEUE
if (siginfo)
{
printf(" si_signo = %d\n", siginfo->si_signo);
printf(" si_errno = %d\n", siginfo->si_errno);
printf(" si_code = %d\n", siginfo->si_code);
printf(" si_pid = %d\n", siginfo->si_pid);
printf(" si_uid = %d\n", siginfo->si_uid);
printf(" si_status = %d\n", siginfo->si_status);
printf(" si_utime = %ld\n", (long)siginfo->si_utime);
printf(" si_stime = %ld\n", (long)siginfo->si_stime);
printf(" si_value = %d\n", siginfo->si_value.sival_int);
printf(" si_int = %d\n", siginfo->si_int);
printf(" si_ptr = %p\n", siginfo->si_ptr);
printf(" si_addr = %p\n", siginfo->si_addr);
printf(" si_band = %ld\n", siginfo->si_band);
printf(" si_fd = %d\n", siginfo->si_fd);
}
#endif
sigusr2_rcvd = 1;
}
#else
void sigusr2_sigaction(int signo)
{
printf("sigusr2_sigaction: Received SIGUSR2, signo=%d\n", signo);
sigusr2_rcvd = 1;
}
#endif
/****************************************************************************
* Name: sigusr2_sighandler
****************************************************************************/
static void sigusr2_sighandler(int signo)
{
printf("sigusr2_sighandler: Received SIGUSR2, signo=%d\n", signo);
sigusr2_rcvd = 1;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: main
****************************************************************************/
int main(int argc, char **argv)
{
struct sigaction act;
struct sigaction oact;
void (*old_sigusr1_sighandler)(int signo);
void (*old_sigusr2_sighandler)(int signo);
pid_t mypid = getpid();
#if defined(__USE_POSIX199309) && defined(HAVE_SIGQUEUE)
sigval_t sigval;
#endif
int status;
printf("Setting up signal handlers from pid=%d\n", mypid);
/* Set up so that sigusr1_sighandler will respond to SIGUSR1 */
old_sigusr1_sighandler = signal(SIGUSR1, sigusr1_sighandler);
if (old_sigusr1_sighandler == SIG_ERR)
{
fprintf(stderr, "Failed to install SIGUSR1 handler, errno=%d\n",
errno);
exit(1);
}
printf("Old SIGUSR1 sighandler at %p\n", old_sigusr1_sighandler);
printf("New SIGUSR1 sighandler at %p\n", sigusr1_sighandler);
/* Set up so that sigusr2_sigaction will respond to SIGUSR2 */
memset(&act, 0, sizeof(struct sigaction));
act.sa_sigaction = sigusr2_sigaction;
act.sa_flags = SA_SIGINFO;
(void)sigemptyset(&act.sa_mask);
status = sigaction(SIGUSR2, &act, &oact);
if (status != 0)
{
fprintf(stderr, "Failed to install SIGUSR2 handler, errno=%d\n",
errno);
exit(2);
}
printf("Old SIGUSR2 sighandler at %p\n", oact.sa_handler);
printf("New SIGUSR2 sighandler at %p\n", sigusr2_sigaction);
printf("Raising SIGUSR1 from pid=%d\n", mypid);
fflush(stdout); usleep(SHORT_DELAY);
/* Send SIGUSR1 to ourselves via raise() */
status = raise(SIGUSR1);
if (status != 0)
{
fprintf(stderr, "Failed to raise SIGUSR1, errno=%d\n", errno);
exit(3);
}
usleep(SHORT_DELAY);
printf("SIGUSR1 raised from pid=%d\n", mypid);
/* Verify that we received SIGUSR1 */
if (sigusr1_rcvd == 0)
{
fprintf(stderr, "SIGUSR1 not received\n");
exit(4);
}
sigusr1_rcvd = 0;
/* Send SIGUSR2 to ourselves */
printf("Killing SIGUSR2 from pid=%d\n", mypid);
fflush(stdout); usleep(SHORT_DELAY);
#if defined(__USE_POSIX199309) && defined(HAVE_SIGQUEUE)
/* Send SIGUSR2 to ourselves via sigqueue() */
sigval.sival_int = 87;
status = sigqueue(mypid, SIGUSR2, sigval);
if (status != 0)
{
fprintf(stderr, "Failed to queue SIGUSR2, errno=%d\n", errno);
exit(5);
}
usleep(SHORT_DELAY);
printf("SIGUSR2 queued from pid=%d, sigval=97\n", mypid);
#else
/* Send SIGUSR2 to ourselves via kill() */
status = kill(mypid, SIGUSR2);
if (status != 0)
{
fprintf(stderr, "Failed to kill SIGUSR2, errno=%d\n", errno);
exit(5);
}
usleep(SHORT_DELAY);
printf("SIGUSR2 killed from pid=%d\n", mypid);
#endif
/* Verify that SIGUSR2 was received */
if (sigusr2_rcvd == 0)
{
fprintf(stderr, "SIGUSR2 not received\n");
exit(6);
}
sigusr2_rcvd = 0;
/* Remove the sigusr2_sigaction handler and replace the SIGUSR2
* handler with sigusr2_sighandler.
*/
printf("Resetting SIGUSR2 signal handler from pid=%d\n", mypid);
old_sigusr2_sighandler = signal(SIGUSR2, sigusr2_sighandler);
if (old_sigusr2_sighandler == SIG_ERR)
{
fprintf(stderr, "Failed to install SIGUSR2 handler, errno=%d\n",
errno);
exit(7);
}
printf("Old SIGUSR2 sighandler at %p\n", old_sigusr2_sighandler);
printf("New SIGUSR2 sighandler at %p\n", sigusr2_sighandler);
/* Verify that the handler that was removed was sigusr2_sigaction */
if ((void*)old_sigusr2_sighandler != (void*)sigusr2_sigaction)
{
fprintf(stderr,
"Old SIGUSR2 signhanlder (%p) is not sigusr2_sigation (%p)\n",
old_sigusr2_sighandler, sigusr2_sigaction);
exit(8);
}
/* Send SIGUSR2 to ourselves via kill() */
printf("Killing SIGUSR2 from pid=%d\n", mypid);
fflush(stdout); usleep(SHORT_DELAY);
status = kill(mypid, SIGUSR2);
if (status != 0)
{
fprintf(stderr, "Failed to kill SIGUSR2, errno=%d\n", errno);
exit(9);
}
usleep(SHORT_DELAY);
printf("SIGUSR2 killed from pid=%d\n", mypid);
/* Verify that SIGUSR2 was received */
if (sigusr2_rcvd == 0)
{
fprintf(stderr, "SIGUSR2 not received\n");
exit(10);
}
sigusr2_rcvd = 0;
return 0;
}

View File

@ -0,0 +1,80 @@
############################################################################
# examples/nxflat/tests/hello/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
CFLAGS += -I.
BIN = struct
R1SRCS = struct_main.c struct_dummy.c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,89 @@
/****************************************************************************
* examples/nxflat/tests/struct/struct.h
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_NXFLAT_TESTS_STRUCT_STRUCT_H
#define __EXAMPLES_NXFLAT_TESTS_STRUCT_STRUCT_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define DUMMY_SCALAR_VALUE1 42
#define DUMMY_SCALAR_VALUE2 87
#define DUMMY_SCALAR_VALUE3 117
/****************************************************************************
* Public Types
****************************************************************************/
typedef void (*dummy_t)(void);
struct struct_dummy_s
{
int n; /* This is a simple scalar value (DUMMY_SCALAR_VALUE3) */
};
struct struct_s
{
int n; /* This is a simple scalar value (DUMMY_SCALAR_VALUE1) */
const int *pn; /* This is a pointer to a simple scalar value */
const struct struct_dummy_s *ps; /* This is a pointer to a structure */
dummy_t pf; /* This is a pointer to a function */
};
/****************************************************************************
* Public Data
****************************************************************************/
extern int dummy_scalar; /* (DUMMY_SCALAR_VALUE2) */
extern const struct struct_dummy_s dummy_struct;
/****************************************************************************
* Public Functions
****************************************************************************/
extern void dummyfunc(void);
extern const struct struct_s *getstruct(void);
#endif /* __EXAMPLES_NXFLAT_TESTS_STRUCT_STRUCT_H */

View File

@ -0,0 +1,65 @@
/****************************************************************************
* examples/nxflat/tests/struct/struct_dummy.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include "struct.h"
/****************************************************************************
* Public Data
****************************************************************************/
const struct struct_s dummy =
{
DUMMY_SCALAR_VALUE1,
&dummy_scalar,
&dummy_struct,
(dummy_t)dummyfunc
};
/****************************************************************************
* Public Functions
****************************************************************************/
const struct struct_s *getstruct(void)
{
return &dummy;
}

View File

@ -0,0 +1,109 @@
/****************************************************************************
* examples/nxflat/tests/struct/struct_main.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <stdio.h>
#include <stdlib.h>
#include "struct.h"
/****************************************************************************
* Public Data
****************************************************************************/
const struct struct_dummy_s dummy_struct =
{
DUMMY_SCALAR_VALUE3
};
int dummy_scalar = DUMMY_SCALAR_VALUE2;
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
const struct struct_s *mystruct = getstruct();
printf("Calling getstruct()\n");
mystruct = getstruct();
printf("getstruct returned %p\n", mystruct);
printf(" n = %d (vs %d) %s\n",
mystruct->n, DUMMY_SCALAR_VALUE1,
mystruct->n == DUMMY_SCALAR_VALUE1 ? "PASS" : "FAIL");
printf(" pn = %p (vs %p) %s\n",
mystruct->pn, &dummy_scalar,
mystruct->pn == &dummy_scalar ? "PASS" : "FAIL");
if (mystruct->pn == &dummy_scalar)
{
printf(" *pn = %d (vs %d) %s\n",
*mystruct->pn, DUMMY_SCALAR_VALUE2,
*mystruct->pn == DUMMY_SCALAR_VALUE2 ? "PASS" : "FAIL");
}
printf(" ps = %p (vs %p) %s\n",
mystruct->ps, &dummy_struct,
mystruct->ps == &dummy_struct ? "PASS" : "FAIL");
if (mystruct->ps == &dummy_struct)
{
printf(" ps->n = %d (vs %d) %s\n",
mystruct->ps->n, DUMMY_SCALAR_VALUE3,
mystruct->ps->n == DUMMY_SCALAR_VALUE3 ? "PASS" : "FAIL");
}
printf(" pf = %p (vs %p) %s\n",
mystruct->pf, dummyfunc,
mystruct->pf == dummyfunc ? "PASS" : "FAIL");
if (mystruct->pf == dummyfunc)
{
printf("Calling mystruct->pf()\n");
mystruct->pf();
}
printf("Exit-ing\n");
return 0;
}
void dummyfunc(void)
{
printf("In dummyfunc() -- PASS\n");
}

View File

@ -0,0 +1,79 @@
############################################################################
# examples/nxflat/tests/task/Makefile
#
# Copyright (C) 2009 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config # Current configuration
-include $(TOPDIR)/Make.defs # Basic make info
BIN = task
R1SRCS = $(BIN).c
R1OBJS = $(R1SRCS:.c=.o)
R2SRC = $(BIN)-thunk.S
R2OBJ = $(R2SRC:.S=.o)
all: $(BIN)
$(R1OBJS): %.o: %.c
@echo "CC: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(R2OBJ): %.o: %.S
@echo "AS: $<"
@$(CC) -c $(CPICFLAGS) $< -o $@
$(BIN).r1: $(R1OBJS)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS1) -o $@ $^
$(R2SRC): $(BIN).r1
@echo "MK: $<"
@$(MKNXFLAT) -o $@ $^
$(BIN).r2: $(R2OBJ)
@echo "LD: $<"
@$(LD) $(NXFLATLDFLAGS2) -o $@ $(R1OBJS) $(R2OBJ)
$(BIN): $(BIN).r2
@echo "LD: $<"
@$(LDNXFLAT) $(LDNXFLATFLAGS) -o $@ $^
clean:
@rm -f $(BIN) $(R2SRC) *.o *.r1 *.r2 *~ .*.swp core
install:
@install -D $(BIN) $(ROMFS_DIR)/$(BIN)

View File

@ -0,0 +1,143 @@
/****************************************************************************
* examples/nxflat/tests/task/parent.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* 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 <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sched.h>
#include <semaphore.h>
#include <errno.h>
/****************************************************************************
* Private Data
****************************************************************************/
static char child_name[] = "child";
static char child_arg[] = "Hello from your parent!";
static sem_t g_sem;
#if CONFIG_TASK_NAME_SIZE == 0
static char no_name[] = "<noname>";
#endif
/****************************************************************************
* Privite Functions
****************************************************************************/
/* NOTE: it is necessary for functions that are referred to by function pointers
* pointer to be declared with global scope (at least for ARM). Otherwise,
* a relocation type that is not supported by NXFLAT is generated by GCC.
*/
int child_task(int argc, char **argv)
{
printf("Child: execv was successful!\n");
printf("Child: argc=%d\n", argc);
if (argc != 2)
{
printf("Child: expected argc to be 2\n");
printf("Child: Exit-ting with status=2\n");
exit(2);
}
printf("Child: argv[0]=\"%s\"\n", argv[0]);
#if CONFIG_TASK_NAME_SIZE == 0
if (strcmp(argv[0], no_name) != 0)
{
printf("Child: expected argv[0] to be \"%s\"\n", no_name);
printf("Child: Exit-ting with status=3\n");
exit(3);
}
#else
if (strncmp(argv[0], child_name, CONFIG_TASK_NAME_SIZE) != 0)
{
printf("Child: expected argv[0] to be \"%s\"\n", child_name);
printf("Child: Exit-ting with status=3\n");
exit(3);
}
#endif
printf("Child: argv[1]=\"%s\"\n", argv[1]);
if (strcmp(argv[1], child_arg) != 0)
{
printf("Child: expected argv[1] to be \"%s\"\n", child_arg);
printf("Child: Exit-ting with status=4\n");
exit(4);
}
printf("Child: Exit-ting with status=0\n");
sem_post(&g_sem);
return 0;
}
/****************************************************************************
* Public Functions
****************************************************************************/
int main(int argc, char **argv)
{
pid_t parent_pid = getpid();
char *child_argv[2];
pid_t child_pid;
printf("Parent: Started, pid=%d\n", parent_pid);
sem_init(&g_sem, 0, 0);
printf("Parent: Calling task_create()\n");
child_argv[0] = child_arg;
child_argv[1] = 0;
child_pid = task_create(child_name, 50, 512, child_task, (const char**)child_argv);
if (child_pid < 0)
{
printf("Parent: task_create failed: %d\n", errno);
}
printf("Parent: Waiting for child (pid=%d)\n", child_pid);
sem_wait(&g_sem);
printf("Parent: Exit-ing\n");
sem_destroy(&g_sem);
return 0;
}

View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/nxhello/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = nxhello_main.c nxhello_bkgd.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# NXHELLO built-in application info
APPNAME = nxhello
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_NXHELLO_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,196 @@
/****************************************************************************
* examples/nxhello/nxhello.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_NXHELLO_NXHELLO_H
#define __APPS_EXAMPLES_NXHELLO_NXHELLO_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxfonts.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_NX
# error "NX is not enabled (CONFIG_NX)"
#endif
#ifndef CONFIG_EXAMPLES_NXHELLO_VPLANE
# define CONFIG_EXAMPLES_NXHELLO_VPLANE 0
#endif
#ifndef CONFIG_EXAMPLES_NXHELLO_BPP
# define CONFIG_EXAMPLES_NXHELLO_BPP 32
#endif
#ifndef CONFIG_EXAMPLES_NXHELLO_BGCOLOR
# if CONFIG_EXAMPLES_NXHELLO_BPP == 24 || CONFIG_EXAMPLES_NXHELLO_BPP == 32
# define CONFIG_EXAMPLES_NXHELLO_BGCOLOR 0x007b68ee
# elif CONFIG_EXAMPLES_NXHELLO_BPP == 16
# define CONFIG_EXAMPLES_NXHELLO_BGCOLOR 0x7b5d
# else
# define CONFIG_EXAMPLES_NXHELLO_BGCOLOR ' '
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXHELLO_FONTID
# define CONFIG_EXAMPLES_NXHELLO_FONTID NXFONT_DEFAULT
#endif
#ifndef CONFIG_EXAMPLES_NXHELLO_FONTCOLOR
# if CONFIG_EXAMPLES_NXHELLO_BPP == 24 || CONFIG_EXAMPLES_NXHELLO_BPP == 32
# define CONFIG_EXAMPLES_NXHELLO_FONTCOLOR 0x00000000
# elif CONFIG_EXAMPLES_NXHELLO_BPP == 16
# define CONFIG_EXAMPLES_NXHELLO_FONTCOLOR 0x0000
# else
# define CONFIG_EXAMPLES_NXHELLO_FONTCOLOR 'F'
# endif
#endif
/* Debug ********************************************************************/
#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
/****************************************************************************
* Public Types
****************************************************************************/
enum exitcode_e
{
NXEXIT_SUCCESS = 0,
NXEXIT_EXTINITIALIZE,
NXEXIT_FBINITIALIZE,
NXEXIT_FBGETVPLANE,
NXEXIT_LCDINITIALIZE,
NXEXIT_LCDGETDEV,
NXEXIT_NXOPEN,
NXEXIT_FONTOPEN,
NXEXIT_NXREQUESTBKGD,
NXEXIT_NXSETBGCOLOR
};
/* Describes one cached glyph bitmap */
struct nxhello_glyph_s
{
uint8_t code; /* Character code */
uint8_t height; /* Height of this glyph (in rows) */
uint8_t width; /* Width of this glyph (in pixels) */
uint8_t stride; /* Width of the glyph row (in bytes) */
uint8_t usecnt; /* Use count */
FAR uint8_t *bitmap; /* Allocated bitmap memory */
};
/* Describes on character on the display */
struct nxhello_bitmap_s
{
uint8_t code; /* Character code */
uint8_t flags; /* See BMFLAGS_* */
struct nxgl_point_s pos; /* Character position */
};
struct nxhello_data_s
{
/* The NX handles */
NXHANDLE hnx;
NXHANDLE hbkgd;
NXHANDLE hfont;
/* The screen resolution */
nxgl_coord_t xres;
nxgl_coord_t yres;
volatile bool havepos;
sem_t sem;
volatile int code;
};
/****************************************************************************
* Public Variables
****************************************************************************/
/* NXHELLO state data */
extern struct nxhello_data_s g_nxhello;
/* NX callback vtables */
extern const struct nx_callback_s g_bgcb;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXHELLO_EXTERNINIT
extern FAR NX_DRIVERTYPE *up_nxdrvinit(unsigned int devno);
#endif
/* Background window interfaces */
extern void nxhello_hello(NXWINDOW hwnd);
#endif /* __APPS_EXAMPLES_NXHELLO_NXHELLO_H */

View File

@ -0,0 +1,443 @@
/****************************************************************************
* examples/nxhello/nxhello_bkgd.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include "nxhello.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* Select renderer -- Some additional logic would be required to support
* pixel depths that are not directly addressable (1,2,4, and 24).
*/
#if CONFIG_EXAMPLES_NXHELLO_BPP == 1
# define RENDERER nxf_convert_1bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 2
# define RENDERER nxf_convert_2bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 4
# define RENDERER nxf_convert_4bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 8
# define RENDERER nxf_convert_8bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 16
# define RENDERER nxf_convert_16bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 24
# define RENDERER nxf_convert_24bpp
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 32
# define RENDERER nxf_convert_32bpp
#else
# error "Unsupported CONFIG_EXAMPLES_NXHELLO_BPP"
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nxhello_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxhello_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxhello_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void nxhello_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_hello[] = "Hello, World!";
/****************************************************************************
* Public Data
****************************************************************************/
/* Background window call table */
const struct nx_callback_s g_bgcb =
{
nxhello_redraw, /* redraw */
nxhello_position /* position */
#ifdef CONFIG_NX_MOUSE
, nxhello_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxhello_kbdin /* my kbdin */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxhello_redraw
****************************************************************************/
static void nxhello_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
}
/****************************************************************************
* Name: nxhello_position
****************************************************************************/
static void nxhello_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!g_nxhello.havepos)
{
/* Save the background window handle */
g_nxhello.hbkgd = hwnd;
/* Save the window limits */
g_nxhello.xres = bounds->pt2.x + 1;
g_nxhello.yres = bounds->pt2.y + 1;
g_nxhello.havepos = true;
sem_post(&g_nxhello.sem);
gvdbg("Have xres=%d yres=%d\n", g_nxhello.xres, g_nxhello.yres);
}
}
/****************************************************************************
* Name: nxhello_mousein
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxhello_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
message("nxhello_mousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nxhello_kbdin
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void nxhello_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
/* In this example, there is no keyboard so a keyboard event is not
* expected.
*/
message("nxhello_kbdin: Unexpected keyboard callback\n");
}
#endif
/****************************************************************************
* Name: nxhello_center
****************************************************************************/
static void nxhello_center(FAR struct nxgl_point_s *pos,
FAR const struct nx_font_s *fontset)
{
FAR const struct nx_fontbitmap_s *fbm;
FAR uint8_t *ptr;
unsigned int width;
/* Get the width of the collection of characters so that we can center the
* hello world message.
*/
for (ptr = (uint8_t*)g_hello, width = 0; *ptr; ptr++)
{
/* Get the font bitmap for this character */
fbm = nxf_getbitmap(g_nxhello.hfont, *ptr);
if (fbm)
{
/* Add the font size */
width += fbm->metric.width + fbm->metric.xoffset;
}
else
{
/* Use the width of a space */
width += fontset->spwidth;
}
}
/* Now we know how to center the string. Create a the position and
* the bounding box
*/
pos->x = (g_nxhello.xres - width) / 2;
pos->y = (g_nxhello.yres - fontset->mxheight) / 2;
}
/****************************************************************************
* Name: nxhello_initglyph
****************************************************************************/
static void nxhello_initglyph(FAR uint8_t *glyph, uint8_t height,
uint8_t width, uint8_t stride)
{
FAR nxgl_mxpixel_t *ptr;
#if CONFIG_EXAMPLES_NXHELLO_BPP < 8
nxgl_mxpixel_t pixel;
#endif
unsigned int row;
unsigned int col;
/* Initialize the glyph memory to the background color */
#if CONFIG_EXAMPLES_NXHELLO_BPP < 8
pixel = CONFIG_EXAMPLES_NXHELLO_BGCOLOR;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
# if CONFIG_EXAMPLES_NXHELLO_BPP == 1
/* Pack 1-bit pixels into a 2-bits */
pixel &= 0x01;
pixel = (pixel) << 1 |pixel;
# endif
# if CONFIG_EXAMPLES_NXHELLO_BPP < 4
/* Pack 2-bit pixels into a nibble */
pixel &= 0x03;
pixel = (pixel) << 2 |pixel;
# endif
/* Pack 4-bit nibbles into a byte */
pixel &= 0x0f;
pixel = (pixel) << 4 | pixel;
ptr = (FAR nxgl_mxpixel_t *)glyph;
for (row = 0; row < fheight; row++)
{
for (col = 0; col < stride; col++)
{
/* Transfer the packed bytes into the buffer */
*ptr++ = pixel;
}
}
#elif CONFIG_EXAMPLES_NXHELLO_BPP == 24
# error "Additional logic is needed here for 24bpp support"
#else /* CONFIG_EXAMPLES_NXHELLO_BPP = {8,16,32} */
ptr = (FAR nxgl_mxpixel_t *)glyph;
for (row = 0; row < height; row++)
{
/* Just copy the color value into the glyph memory */
for (col = 0; col < width; col++)
{
*ptr++ = CONFIG_EXAMPLES_NXHELLO_BGCOLOR;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
}
}
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxhello_hello
*
* Description:
* Print "Hello, World!" in the center of the display.
*
****************************************************************************/
void nxhello_hello(NXWINDOW hwnd)
{
FAR const struct nx_font_s *fontset;
FAR const struct nx_fontbitmap_s *fbm;
FAR uint8_t *glyph;
FAR const char *ptr;
FAR struct nxgl_point_s pos;
FAR struct nxgl_rect_s dest;
FAR const void *src[CONFIG_NX_NPLANES];
unsigned int glyphsize;
unsigned int mxstride;
int ret;
/* Get information about the font we are going to use */
fontset = nxf_getfontset(g_nxhello.hfont);
/* Allocate a bit of memory to hold the largest rendered font */
mxstride = (fontset->mxwidth * CONFIG_EXAMPLES_NXHELLO_BPP + 7) >> 3;
glyphsize = (unsigned int)fontset->mxheight * mxstride;
glyph = (FAR uint8_t*)malloc(glyphsize);
/* NOTE: no check for failure to allocate the memory. In a real application
* you would need to handle that event.
*/
/* Get a position so the the "Hello, World!" string will be centered on the
* display.
*/
nxhello_center(&pos, fontset);
message("nxhello_hello: Position (%d,%d)\n", pos.x, pos.y);
/* Now we can say "hello" in the center of the display. */
for (ptr = g_hello; *ptr; ptr++)
{
/* Get the bitmap font for this ASCII code */
fbm = nxf_getbitmap(g_nxhello.hfont, *ptr);
if (fbm)
{
uint8_t fheight; /* Height of this glyph (in rows) */
uint8_t fwidth; /* Width of this glyph (in pixels) */
uint8_t fstride; /* Width of the glyph row (in bytes) */
/* Get information about the font bitmap */
fwidth = fbm->metric.width + fbm->metric.xoffset;
fheight = fbm->metric.height + fbm->metric.yoffset;
fstride = (fwidth * CONFIG_EXAMPLES_NXHELLO_BPP + 7) >> 3;
/* Initialize the glyph memory to the background color */
nxhello_initglyph(glyph, fheight, fwidth, fstride);
/* Then render the glyph into the allocated memory */
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
(void)RENDERER((FAR nxgl_mxpixel_t*)glyph, fheight, fwidth,
fstride, fbm, CONFIG_EXAMPLES_NXHELLO_FONTCOLOR);
/* Describe the destination of the font with a rectangle */
dest.pt1.x = pos.x;
dest.pt1.y = pos.y;
dest.pt2.x = pos.x + fwidth - 1;
dest.pt2.y = pos.y + fheight - 1;
/* Then put the font on the display */
src[0] = (FAR const void *)glyph;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
ret = nx_bitmap((NXWINDOW)hwnd, &dest, src, &pos, fstride);
if (ret < 0)
{
message("nxhello_write: nx_bitmapwindow failed: %d\n", errno);
}
/* Skip to the right the width of the font */
pos.x += fwidth;
}
else
{
/* No bitmap (probably because the font is a space). Skip to the
* right the width of a space.
*/
pos.x += fontset->spwidth;
}
}
/* Free the allocated glyph */
free(glyph);
}

View File

@ -0,0 +1,294 @@
/****************************************************************************
* examples/nxhello/nxhello_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include "nxhello.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */
#ifndef CONFIG_EXAMPLES_NXHELLO_VPLANE
# define CONFIG_EXAMPLES_NXHELLO_VPLANE 0
#endif
/* If not specified, assume that the hardware supports one LCD device */
#ifndef CONFIG_EXAMPLES_NXHELLO_DEVNO
# define CONFIG_EXAMPLES_NXHELLO_DEVNO 0
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
struct nxhello_data_s g_nxhello =
{
NULL, /* hnx */
NULL, /* hbkgd */
NULL, /* hfont */
0, /* xres */
0, /* yres */
false, /* havpos */
{ 0 }, /* sem */
NXEXIT_SUCCESS /* exit code */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxhello_initialize
****************************************************************************/
static inline int nxhello_initialize(void)
{
FAR NX_DRIVERTYPE *dev;
#if defined(CONFIG_EXAMPLES_NXHELLO_EXTERNINIT)
/* Use external graphics driver initialization */
message("nxhello_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NXHELLO_DEVNO);
if (!dev)
{
message("nxhello_initialize: up_nxdrvinit failed, devno=%d\n",
CONFIG_EXAMPLES_NXHELLO_DEVNO);
g_nxhello.code = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
int ret;
/* Initialize the LCD device */
message("nxhello_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nxhello_initialize: up_lcdinitialize failed: %d\n", -ret);
g_nxhello.code = NXEXIT_LCDINITIALIZE;
return ERROR;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NXHELLO_DEVNO);
if (!dev)
{
message("nxhello_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NXHELLO_DEVNO);
g_nxhello.code = NXEXIT_LCDGETDEV;
return ERROR;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
int ret;
/* Initialize the frame buffer device */
message("nxhello_initialize: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nxhello_initialize: up_fbinitialize failed: %d\n", -ret);
g_nxhello.code = NXEXIT_FBINITIALIZE;
return ERROR;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NXHELLO_VPLANE);
if (!dev)
{
message("nxhello_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NXHELLO_VPLANE);
g_nxhello.code = NXEXIT_FBGETVPLANE;
return ERROR;
}
#endif
/* Then open NX */
message("nxhello_initialize: Open NX\n");
g_nxhello.hnx = nx_open(dev);
if (!g_nxhello.hnx)
{
message("nxhello_initialize: nx_open failed: %d\n", errno);
g_nxhello.code = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/nxhello_main
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXHELLO_BUILTIN
# define MAIN_NAME nxhello_main
# define MAIN_NAME_STRING "nxhello_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
int MAIN_NAME(int argc, char *argv[])
{
nxgl_mxpixel_t color;
int ret;
/* Initialize NX */
ret = nxhello_initialize();
message(MAIN_NAME_STRING ": NX handle=%p\n", g_nxhello.hnx);
if (!g_nxhello.hnx || ret < 0)
{
message(MAIN_NAME_STRING ": Failed to get NX handle: %d\n", errno);
g_nxhello.code = NXEXIT_NXOPEN;
goto errout;
}
/* Get the default font handle */
g_nxhello.hfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXHELLO_FONTID);
if (!g_nxhello.hfont)
{
message(MAIN_NAME_STRING ": Failed to get font handle: %d\n", errno);
g_nxhello.code = NXEXIT_FONTOPEN;
goto errout;
}
/* Set the background to the configured background color */
message(MAIN_NAME_STRING ": Set background color=%d\n",
CONFIG_EXAMPLES_NXHELLO_BGCOLOR);
color = CONFIG_EXAMPLES_NXHELLO_BGCOLOR;
ret = nx_setbgcolor(g_nxhello.hnx, &color);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nxhello.code = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Get the background window */
ret = nx_requestbkgd(g_nxhello.hnx, &g_bgcb, NULL);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nxhello.code = NXEXIT_NXREQUESTBKGD;
goto errout_with_nx;
}
/* Wait until we have the screen resolution. We'll have this immediately
* unless we are dealing with the NX server.
*/
while (!g_nxhello.havepos)
{
(void)sem_wait(&g_nxhello.sem);
}
message(MAIN_NAME_STRING ": Screen resolution (%d,%d)\n", g_nxhello.xres, g_nxhello.yres);
/* Now, say hello and exit, sleeping a little before each. */
sleep(1);
nxhello_hello(g_nxhello.hbkgd);
sleep(1);
/* Release background */
(void)nx_releasebkgd(g_nxhello.hbkgd);
/* Close NX */
errout_with_nx:
message(MAIN_NAME_STRING ": Close NX\n");
nx_close(g_nxhello.hnx);
errout:
return g_nxhello.code;
}

105
apps/examples/nximage/Makefile Executable file
View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/nximage/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = nximage_main.c nximage_bkgd.c nximage_bitmap.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# NXIMAGE built-in application info
APPNAME = nximage
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_NXIMAGE_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

219
apps/examples/nximage/nximage.h Executable file
View File

@ -0,0 +1,219 @@
/****************************************************************************
* examples/nximage/nximage.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_NXIMAGE_NXIMAGE_H
#define __APPS_EXAMPLES_NXIMAGE_NXIMAGE_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_NX
# error "NX is not enabled (CONFIG_NX)"
#endif
#ifndef CONFIG_EXAMPLES_NXIMAGE_VPLANE
# define CONFIG_EXAMPLES_NXIMAGE_VPLANE 0
#endif
#ifndef CONFIG_EXAMPLES_NXIMAGE_BPP
# define CONFIG_EXAMPLES_NXIMAGE_BPP 16
#endif
#if defined(CONFIG_EXAMPLES_NXIMAGE_XSCALEp5)
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p5
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE2p0
#elif defined(CONFIG_EXAMPLES_NXIMAGE_XSCALE1p5)
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE2p0
#elif defined(CONFIG_EXAMPLES_NXIMAGE_XSCALE2p0)
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p5
#else
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE1p5
# undef CONFIG_EXAMPLES_NXIMAGE_XSCALE2p0
# define CONFIG_EXAMPLES_NXIMAGE_XSCALE1p0 1
#endif
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALEp5)
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0)
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5
#else
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALEp5
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p0
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5
# undef CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0
# define CONFIG_EXAMPLES_NXIMAGE_YSCALE1p0 1
#endif
/* Debug ********************************************************************/
#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
/* Image Information ********************************************************/
#define IMAGE_HEIGHT 160 /* Number of rows in the raw image */
#define IMAGE_WIDTH 160 /* Number of columns in the raw image */
#if defined(CONFIG_EXAMPLES_NXIMAGE_XSCALEp5)
# define SCALED_WIDTH 80 /* Number of columns in the scaled image */
#elif defined(CONFIG_EXAMPLES_NXIMAGE_XSCALE1p5)
# define SCALED_WIDTH 240 /* Number of columns in the scaled image */
#elif defined(CONFIG_EXAMPLES_NXIMAGE_XSCALE2p0)
# define SCALED_WIDTH 320 /* Number of columns in the scaled image */
#else
# define SCALED_WIDTH 160 /* Number of columns in the scaled image */
#endif
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALEp5)
# define SCALED_HEIGHT 80 /* Number of rows in the scaled image */
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
# define SCALED_HEIGHT 240 /* Number of rows in the scaled image */
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0)
# define SCALED_HEIGHT 320 /* Number of rows in the scaled image */
#else
# define SCALED_HEIGHT 160 /* Number of rows in the scaled image */
#endif
/****************************************************************************
* Public Types
****************************************************************************/
enum exitcode_e
{
NXEXIT_SUCCESS = 0,
NXEXIT_EXTINITIALIZE,
NXEXIT_FBINITIALIZE,
NXEXIT_FBGETVPLANE,
NXEXIT_LCDINITIALIZE,
NXEXIT_LCDGETDEV,
NXEXIT_NXOPEN,
NXEXIT_NXREQUESTBKGD,
NXEXIT_NXSETBGCOLOR
};
struct nximage_data_s
{
/* The NX handles */
NXHANDLE hnx;
NXHANDLE hbkgd;
/* The screen resolution */
nxgl_coord_t xres;
nxgl_coord_t yres;
volatile bool havepos;
sem_t sem;
volatile int code;
};
/****************************************************************************
* Public Variables
****************************************************************************/
/* NXIMAGE state data */
extern struct nximage_data_s g_nximage;
/* NX callback vtables */
extern const struct nx_callback_s g_bgcb;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXIMAGE_EXTERNINIT
extern FAR NX_DRIVERTYPE *up_nxdrvinit(unsigned int devno);
#endif
/* Background window interfaces */
extern void nximage_image(NXWINDOW hwnd);
/* Image interfaces */
extern nxgl_mxpixel_t nximage_bgcolor(void);
extern nxgl_mxpixel_t nximage_avgcolor(nxgl_mxpixel_t color1, nxgl_mxpixel_t color2);
extern void nximage_blitrow(FAR nxgl_mxpixel_t *run, FAR const void **state);
#endif /* __APPS_EXAMPLES_NXIMAGE_NXIMAGE_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,397 @@
/****************************************************************************
* examples/nximage/nximage_bkgd.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include "nximage.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* Select renderer -- Some additional logic would be required to support
* pixel depths that are not directly addressable (1,2,4, and 24).
*/
#if CONFIG_EXAMPLES_NXIMAGE_BPP == 1
# define RENDERER nxf_convert_1bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 2
# define RENDERER nxf_convert_2bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 4
# define RENDERER nxf_convert_4bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 8
# define RENDERER nxf_convert_8bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 16
# define RENDERER nxf_convert_16bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 24
# define RENDERER nxf_convert_24bpp
#elif CONFIG_EXAMPLES_NXIMAGE_BPP == 32
# define RENDERER nxf_convert_32bpp
#else
# error "Unsupported CONFIG_EXAMPLES_NXIMAGE_BPP"
#endif
/* Vertical scaling */
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALEp5)
/* Read two rows, output one averaged row */
#define NINPUT_ROWS 2
#define NOUTPUT_ROWS 1
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
/* Read two rows, output three rows */
#define NINPUT_ROWS 2
#define NOUTPUT_ROWS 3
#elif defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0)
/* Read one row, output two rows */
#define NINPUT_ROWS 1
#define NOUTPUT_ROWS 2
#else
/* Read one rows, output one or two rows */
#define NINPUT_ROWS 1
#define NOUTPUT_ROWS 1
#endif
/****************************************************************************
* Private Types
****************************************************************************/
struct nximage_run_t
{
nxgl_mxpixel_t run[SCALED_WIDTH];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nximage_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg);
static void nximage_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nximage_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void nximage_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static const char g_hello[] = "Hello, World!";
/* Read one or two rows, output one tow or three rows */
static struct nximage_run_t g_runs[NINPUT_ROWS];
/****************************************************************************
* Public Data
****************************************************************************/
/* Background window call table */
const struct nx_callback_s g_bgcb =
{
nximage_redraw, /* redraw */
nximage_position /* position */
#ifdef CONFIG_NX_MOUSE
, nximage_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nximage_kbdin /* my kbdin */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nximage_redraw
*
* Description:
* NX re-draw handler
*
****************************************************************************/
static void nximage_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
}
/****************************************************************************
* Name: nximage_position
*
* Description:
* NX position change handler
*
****************************************************************************/
static void nximage_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!g_nximage.havepos)
{
/* Save the background window handle */
g_nximage.hbkgd = hwnd;
/* Save the window limits */
g_nximage.xres = bounds->pt2.x + 1;
g_nximage.yres = bounds->pt2.y + 1;
g_nximage.havepos = true;
sem_post(&g_nximage.sem);
gvdbg("Have xres=%d yres=%d\n", g_nximage.xres, g_nximage.yres);
}
}
/****************************************************************************
* Name: nximage_mousein
*
* Description:
* NX mouse input handler
*
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nximage_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
message("nximage_mousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nximage_kbdin
*
* Description:
* NX keyboard input handler
*
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void nximage_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
/* In this example, there is no keyboard so a keyboard event is not
* expected.
*/
message("nximage_kbdin: Unexpected keyboard callback\n");
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nximage_image
*
* Description:
* Put the NuttX logo in the center of the display.
*
****************************************************************************/
void nximage_image(NXWINDOW hwnd)
{
FAR const void *state = NULL;
FAR struct nxgl_point_s pos;
FAR struct nxgl_rect_s dest;
FAR const void *src[CONFIG_NX_NPLANES];
nxgl_coord_t row;
int ret;
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALEp5) || defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
int i;
#endif
/* Center the image. Note: these may extend off the display. */
pos.x = (g_nximage.xres - SCALED_WIDTH) / 2;
pos.y = (g_nximage.yres - SCALED_HEIGHT) / 2;
/* Set up the invariant part of the destination bounding box */
dest.pt1.x = pos.x;
dest.pt2.x = pos.x + SCALED_WIDTH - 1;
/* Now output the rows */
for (row = 0; row < IMAGE_HEIGHT; row += NINPUT_ROWS)
{
/* Read input row(s) */
nximage_blitrow(g_runs[0].run, &state);
#if NINPUT_ROWS > 1
nximage_blitrow(g_runs[1].run, &state);
#endif
/* Output rows before averaging */
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5) || defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE2p0)
/* Output row[0] */
dest.pt1.y = pos.y;
dest.pt2.y = pos.y;
src[0] = (FAR const void *)g_runs[0].run;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
ret = nx_bitmap((NXWINDOW)hwnd, &dest, src, &pos, SCALED_WIDTH*sizeof(nxgl_mxpixel_t));
if (ret < 0)
{
message("nximage_image: nx_bitmapwindow failed: %d\n", errno);
}
/* Increment the vertical position */
pos.y++;
#endif
/* Perform averaging */
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALEp5) || defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
/* Average row[0] and row[1], output results in row[0] */
for (i = 0; i < SCALED_WIDTH; i++)
{
/* Only average if the corresponding pixels in each row differ */
nxgl_mxpixel_t pix0 = g_runs[0].run[i];
nxgl_mxpixel_t pix1 = g_runs[1].run[i];
if (pix0 != pix1)
{
g_runs[0].run[i] = nximage_avgcolor(pix0, pix1);
}
}
#endif
/* Output rows after averaging */
/* Output row[0] */
dest.pt1.y = pos.y;
dest.pt2.y = pos.y;
src[0] = (FAR const void *)g_runs[0].run;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
ret = nx_bitmap((NXWINDOW)hwnd, &dest, src, &pos, SCALED_WIDTH*sizeof(nxgl_mxpixel_t));
if (ret < 0)
{
message("nximage_image: nx_bitmapwindow failed: %d\n", errno);
}
/* Increment the vertical position */
pos.y++;
#if defined(CONFIG_EXAMPLES_NXIMAGE_YSCALE1p5)
/* Output row[0] and row[1] */
dest.pt1.y = pos.y;
dest.pt2.y = pos.y;
src[0] = (FAR const void *)g_runs[1].run;
#if CONFIG_NX_NPLANES > 1
# warning "More logic is needed for the case where CONFIG_NX_PLANES > 1"
#endif
ret = nx_bitmap((NXWINDOW)hwnd, &dest, src, &pos, SCALED_WIDTH*sizeof(nxgl_mxpixel_t));
if (ret < 0)
{
message("nximage_image: nx_bitmapwindow failed: %d\n", errno);
}
/* Increment the vertical position */
pos.y++;
#endif
}
}

View File

@ -0,0 +1,289 @@
/****************************************************************************
* examples/nximage/nximage_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include "nximage.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */
#ifndef CONFIG_EXAMPLES_NXIMAGE_VPLANE
# define CONFIG_EXAMPLES_NXIMAGE_VPLANE 0
#endif
/* If not specified, assume that the hardware supports one LCD device */
#ifndef CONFIG_EXAMPLES_NXIMAGE_DEVNO
# define CONFIG_EXAMPLES_NXIMAGE_DEVNO 0
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
struct nximage_data_s g_nximage =
{
NULL, /* hnx */
NULL, /* hbkgd */
0, /* xres */
0, /* yres */
false, /* havpos */
{ 0 }, /* sem */
NXEXIT_SUCCESS /* exit code */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nximage_initialize
*
* Description:
* Initialize the LCD or framebuffer device (single user mode only), then
* open NX.
*
****************************************************************************/
static inline int nximage_initialize(void)
{
FAR NX_DRIVERTYPE *dev;
#if defined(CONFIG_EXAMPLES_NXIMAGE_EXTERNINIT)
/* Use external graphics driver initialization */
message("nximage_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NXIMAGE_DEVNO);
if (!dev)
{
message("nximage_initialize: up_nxdrvinit failed, devno=%d\n",
CONFIG_EXAMPLES_NXIMAGE_DEVNO);
g_nximage.code = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
int ret;
/* Initialize the LCD device */
message("nximage_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nximage_initialize: up_lcdinitialize failed: %d\n", -ret);
g_nximage.code = NXEXIT_LCDINITIALIZE;
return ERROR;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NXIMAGE_DEVNO);
if (!dev)
{
message("nximage_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NXIMAGE_DEVNO);
g_nximage.code = NXEXIT_LCDGETDEV;
return ERROR;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
int ret;
/* Initialize the frame buffer device */
message("nximage_initialize: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nximage_initialize: up_fbinitialize failed: %d\n", -ret);
g_nximage.code = NXEXIT_FBINITIALIZE;
return ERROR;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NXIMAGE_VPLANE);
if (!dev)
{
message("nximage_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NXIMAGE_VPLANE);
g_nximage.code = NXEXIT_FBGETVPLANE;
return ERROR;
}
#endif
/* Then open NX */
message("nximage_initialize: Open NX\n");
g_nximage.hnx = nx_open(dev);
if (!g_nximage.hnx)
{
message("nximage_initialize: nx_open failed: %d\n", errno);
g_nximage.code = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/nximage_main
*
* Description:
* Main entry pointer. Configures the basic display resources.
*
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXIMAGE_BUILTIN
# define MAIN_NAME nximage_main
# define MAIN_NAME_STRING "nximage_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
int MAIN_NAME(int argc, char *argv[])
{
nxgl_mxpixel_t color;
int ret;
/* Initialize NX */
ret = nximage_initialize();
message(MAIN_NAME_STRING ": NX handle=%p\n", g_nximage.hnx);
if (!g_nximage.hnx || ret < 0)
{
message(MAIN_NAME_STRING ": Failed to get NX handle: %d\n", errno);
g_nximage.code = NXEXIT_NXOPEN;
goto errout;
}
/* Set the background to the configured background color */
color = nximage_bgcolor();
message(MAIN_NAME_STRING ": Set background color=%d\n", color);
ret = nx_setbgcolor(g_nximage.hnx, &color);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nximage.code = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Get the background window */
ret = nx_requestbkgd(g_nximage.hnx, &g_bgcb, NULL);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nximage.code = NXEXIT_NXREQUESTBKGD;
goto errout_with_nx;
}
/* Wait until we have the screen resolution. We'll have this immediately
* unless we are dealing with the NX server.
*/
while (!g_nximage.havepos)
{
(void)sem_wait(&g_nximage.sem);
}
message(MAIN_NAME_STRING ": Screen resolution (%d,%d)\n", g_nximage.xres, g_nximage.yres);
/* Now, put up the NuttX logo. */
nximage_image(g_nximage.hbkgd);
/* Release background */
(void)nx_releasebkgd(g_nximage.hbkgd);
/* Close NX */
errout_with_nx:
message(MAIN_NAME_STRING ": Close NX\n");
nx_close(g_nximage.hnx);
errout:
return g_nximage.code;
}

View File

@ -0,0 +1,105 @@
############################################################################
# apps/examples/nxlines/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = nxlines_main.c nxlines_bkgd.c
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# NXLINES built-in application info
APPNAME = nxlines
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_NXLINES_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,197 @@
/****************************************************************************
* examples/nxlines/nxlines.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __APPS_EXAMPLES_NXLINES_NXLINES_H
#define __APPS_EXAMPLES_NXLINES_NXLINES_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nx.h>
#include <nuttx/rgbcolors.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_NX
# error "NX is not enabled (CONFIG_NX)"
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_VPLANE
# define CONFIG_EXAMPLES_NXLINES_VPLANE 0
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BPP
# define CONFIG_EXAMPLES_NXLINES_BPP 16
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BGCOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB24_DARKGREEN
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB16_DARKGREEN
# else
# define CONFIG_EXAMPLES_NXLINES_BGCOLOR RGB8_DARKGREEN
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_LINEWIDTH
# define CONFIG_EXAMPLES_NXLINES_LINEWIDTH 16
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_LINECOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB24_YELLOW
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB16_YELLOW
# else
# define CONFIG_EXAMPLES_NXLINES_LINECOLOR RGB8_YELLOW
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BORDERWIDTH
# define CONFIG_EXAMPLES_NXLINES_BORDERWIDTH 16
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_BORDERCOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB24_YELLOW
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB16_YELLOW
# else
# define CONFIG_EXAMPLES_NXLINES_BORDERCOLOR RGB8_YELLOW
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR
# if CONFIG_EXAMPLES_NXLINES_BPP == 24 || CONFIG_EXAMPLES_NXLINES_BPP == 32
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB24_BEIGE
# elif CONFIG_EXAMPLES_NXLINES_BPP == 16
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB16_BEIGE
# else
# define CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR RGB8_YELLOW
# endif
#endif
/* Debug ********************************************************************/
#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
/****************************************************************************
* Public Types
****************************************************************************/
enum exitcode_e
{
NXEXIT_SUCCESS = 0,
NXEXIT_EXTINITIALIZE,
NXEXIT_FBINITIALIZE,
NXEXIT_FBGETVPLANE,
NXEXIT_LCDINITIALIZE,
NXEXIT_LCDGETDEV,
NXEXIT_NXOPEN,
NXEXIT_NXREQUESTBKGD,
NXEXIT_NXSETBGCOLOR
};
struct nxlines_data_s
{
/* The NX handles */
NXHANDLE hnx;
NXHANDLE hbkgd;
/* The screen resolution */
nxgl_coord_t xres;
nxgl_coord_t yres;
volatile bool havepos;
sem_t sem;
volatile int code;
};
/****************************************************************************
* Public Variables
****************************************************************************/
/* NXLINES state data */
extern struct nxlines_data_s g_nxlines;
/* NX callback vtables */
extern const struct nx_callback_s g_bgcb;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXLINES_EXTERNINIT
extern FAR NX_DRIVERTYPE *up_nxdrvinit(unsigned int devno);
#endif
/* Background window interfaces */
extern void nxlines_test(NXWINDOW hwnd);
#endif /* __APPS_EXAMPLES_NXLINES_NXLINES_H */

View File

@ -0,0 +1,335 @@
/****************************************************************************
* examples/nxlines/nxlines_bkgd.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <debug.h>
#include <fixedmath.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include "nxlines.h"
/****************************************************************************
* Definitions
****************************************************************************/
#ifndef MIN
# define MIN(a,b) (a < b ? a : b)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nxlines_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxlines_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxlines_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void nxlines_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
/* Background window call table */
const struct nx_callback_s g_bgcb =
{
nxlines_redraw, /* redraw */
nxlines_position /* position */
#ifdef CONFIG_NX_MOUSE
, nxlines_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxlines_kbdin /* my kbdin */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxlines_redraw
****************************************************************************/
static void nxlines_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
}
/****************************************************************************
* Name: nxlines_position
****************************************************************************/
static void nxlines_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!g_nxlines.havepos)
{
/* Save the background window handle */
g_nxlines.hbkgd = hwnd;
/* Save the window limits */
g_nxlines.xres = bounds->pt2.x + 1;
g_nxlines.yres = bounds->pt2.y + 1;
g_nxlines.havepos = true;
sem_post(&g_nxlines.sem);
gvdbg("Have xres=%d yres=%d\n", g_nxlines.xres, g_nxlines.yres);
}
}
/****************************************************************************
* Name: nxlines_mousein
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxlines_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
message("nxlines_mousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nxlines_kbdin
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void nxlines_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
/* In this example, there is no keyboard so a keyboard event is not
* expected.
*/
message("nxlines_kbdin: Unexpected keyboard callback\n");
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxlines_test
*
* Description:
* Print "Hello, World!" in the center of the display.
*
****************************************************************************/
void nxlines_test(NXWINDOW hwnd)
{
struct nxgl_point_s center;
struct nxgl_vector_s vector;
struct nxgl_vector_s previous;
nxgl_mxpixel_t color[CONFIG_NX_NPLANES];
nxgl_coord_t maxradius;
nxgl_coord_t radius;
nxgl_coord_t halfx;
nxgl_coord_t halfy;
b16_t angle;
int ret;
/* Get the maximum radius and center of the circle */
maxradius = MIN(g_nxlines.yres, g_nxlines.xres) >> 1;
center.x = g_nxlines.xres >> 1;
center.y = g_nxlines.yres >> 1;
/* Draw a circular background */
radius = maxradius - ((CONFIG_EXAMPLES_NXLINES_BORDERWIDTH+1)/2);
color[0] = CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR;
ret = nx_fillcircle((NXWINDOW)hwnd, &center, radius, color);
if (ret < 0)
{
message("nxlines_test: nx_fillcircle failed: %d\n", ret);
}
/* Draw the circular border */
color[0] = CONFIG_EXAMPLES_NXLINES_BORDERCOLOR;
ret = nx_drawcircle((NXWINDOW)hwnd, &center, radius,
CONFIG_EXAMPLES_NXLINES_BORDERWIDTH, color);
if (ret < 0)
{
message("nxlines_test: nx_fillcircle failed: %d\n", ret);
}
/* Back off the radius to account for the thickness of border line
* and with a big fudge factor that will (hopefully) prevent the corners
* of the lines from overwriting the border. This is overly complicated
* here because we don't assume anything about the screen resolution or
* the borderwidth or the line thickness (and there are certainly some
* smarter ways to do this).
*/
if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 80))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 40);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 60))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 30);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 40))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 20);
}
else if (maxradius > (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 20))
{
radius = maxradius - (CONFIG_EXAMPLES_NXLINES_BORDERWIDTH + 10);
}
else
{
radius = maxradius - CONFIG_EXAMPLES_NXLINES_BORDERWIDTH;
}
/* The loop, showing the line in various orientations */
angle = 0;
previous.pt1.x = center.x;
previous.pt1.y = center.y;
previous.pt2.x = center.x;
previous.pt2.y = center.y;
for (;;)
{
/* Determine the position of the line on this pass */
halfx = b16toi(b16muli(b16sin(angle), radius));
halfy = b16toi(b16muli(b16cos(angle), radius));
vector.pt1.x = center.x + halfx;
vector.pt1.y = center.y + halfy;
vector.pt2.x = center.x - halfx;
vector.pt2.y = center.y - halfy;
message("Angle: %08x vector: (%d,%d)->(%d,%d)\n",
angle, vector.pt1.x, vector.pt1.y, vector.pt2.x, vector.pt2.y);
/* Clear the previous line by overwriting it with the circle color */
color[0] = CONFIG_EXAMPLES_NXLINES_CIRCLECOLOR;
ret = nx_drawline((NXWINDOW)hwnd, &previous, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color);
if (ret < 0)
{
message("nxlines_test: nx_drawline failed clearing: %d\n", ret);
}
/* Draw the new line */
color[0] = CONFIG_EXAMPLES_NXLINES_LINECOLOR;
ret = nx_drawline((NXWINDOW)hwnd, &vector, CONFIG_EXAMPLES_NXLINES_LINEWIDTH, color);
if (ret < 0)
{
message("nxlines_test: nx_drawline failed clearing: %d\n", ret);
}
/* Set up for the next time through the loop then sleep for a bit. */
angle += b16PI / 16; /* 32 angular positions in full circle */
/* Check if we have gone all the way around */
if (angle > (31 * (2 * b16PI) / 32))
{
#ifdef CONFIG_EXAMPLES_NXLINES_BUILTIN
/* If this example was built as an NSH add-on, then exit after we
* have gone all the way around once.
*/
return;
#else
/* Wrap back to zero and continue with the test */
angle = 0;
#endif
}
memcpy(&previous, &vector, sizeof(struct nxgl_vector_s));
usleep(500*1000);
}
}

View File

@ -0,0 +1,280 @@
/****************************************************************************
* examples/nxlines/nxlines_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sched.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include "nxlines.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */
#ifndef CONFIG_EXAMPLES_NXLINES_VPLANE
# define CONFIG_EXAMPLES_NXLINES_VPLANE 0
#endif
/* If not specified, assume that the hardware supports one LCD device */
#ifndef CONFIG_EXAMPLES_NXLINES_DEVNO
# define CONFIG_EXAMPLES_NXLINES_DEVNO 0
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Public Data
****************************************************************************/
struct nxlines_data_s g_nxlines =
{
NULL, /* hnx */
NULL, /* hbkgd */
0, /* xres */
0, /* yres */
false, /* havpos */
{ 0 }, /* sem */
NXEXIT_SUCCESS /* exit code */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxlines_initialize
****************************************************************************/
static inline int nxlines_initialize(void)
{
FAR NX_DRIVERTYPE *dev;
#if defined(CONFIG_EXAMPLES_NXLINES_EXTERNINIT)
/* Use external graphics driver initialization */
message("nxlines_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NXLINES_DEVNO);
if (!dev)
{
message("nxlines_initialize: up_nxdrvinit failed, devno=%d\n",
CONFIG_EXAMPLES_NXLINES_DEVNO);
g_nxlines.code = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
int ret;
/* Initialize the LCD device */
message("nxlines_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nxlines_initialize: up_lcdinitialize failed: %d\n", -ret);
g_nxlines.code = NXEXIT_LCDINITIALIZE;
return ERROR;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NXLINES_DEVNO);
if (!dev)
{
message("nxlines_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NXLINES_DEVNO);
g_nxlines.code = NXEXIT_LCDGETDEV;
return ERROR;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
int ret;
/* Initialize the frame buffer device */
message("nxlines_initialize: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nxlines_initialize: up_fbinitialize failed: %d\n", -ret);
g_nxlines.code = NXEXIT_FBINITIALIZE;
return ERROR;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NXLINES_VPLANE);
if (!dev)
{
message("nxlines_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NXLINES_VPLANE);
g_nxlines.code = NXEXIT_FBGETVPLANE;
return ERROR;
}
#endif
/* Then open NX */
message("nxlines_initialize: Open NX\n");
g_nxlines.hnx = nx_open(dev);
if (!g_nxlines.hnx)
{
message("nxlines_initialize: nx_open failed: %d\n", errno);
g_nxlines.code = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/nxlines_main
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXLINES_BUILTIN
# define MAIN_NAME nxlines_main
# define MAIN_NAME_STRING "nxlines_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
int MAIN_NAME(int argc, char *argv[])
{
nxgl_mxpixel_t color;
int ret;
/* Initialize NX */
ret = nxlines_initialize();
message(MAIN_NAME_STRING ": NX handle=%p\n", g_nxlines.hnx);
if (!g_nxlines.hnx || ret < 0)
{
message(MAIN_NAME_STRING ": Failed to get NX handle: %d\n", errno);
g_nxlines.code = NXEXIT_NXOPEN;
goto errout;
}
/* Set the background to the configured background color */
message(MAIN_NAME_STRING ": Set background color=%d\n",
CONFIG_EXAMPLES_NXLINES_BGCOLOR);
color = CONFIG_EXAMPLES_NXLINES_BGCOLOR;
ret = nx_setbgcolor(g_nxlines.hnx, &color);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nxlines.code = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Get the background window */
ret = nx_requestbkgd(g_nxlines.hnx, &g_bgcb, NULL);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_nxlines.code = NXEXIT_NXREQUESTBKGD;
goto errout_with_nx;
}
/* Wait until we have the screen resolution. We'll have this immediately
* unless we are dealing with the NX server.
*/
while (!g_nxlines.havepos)
{
(void)sem_wait(&g_nxlines.sem);
}
message(MAIN_NAME_STRING ": Screen resolution (%d,%d)\n", g_nxlines.xres, g_nxlines.yres);
/* Now, say perform the lines (these test does not return so the remaining
* logic is cosmetic).
*/
nxlines_test(g_nxlines.hbkgd);
/* Release background */
(void)nx_releasebkgd(g_nxlines.hbkgd);
/* Close NX */
errout_with_nx:
message(MAIN_NAME_STRING ": Close NX\n");
nx_close(g_nxlines.hnx);
errout:
return g_nxlines.code;
}

View File

@ -0,0 +1,109 @@
############################################################################
# apps/examples/nxtext/Makefile
#
# Copyright (C) 2011 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <spudmonkey@racsa.co.cr>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name NuttX nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################
-include $(TOPDIR)/.config
-include $(TOPDIR)/Make.defs
include $(APPDIR)/Make.defs
# NuttX NX Graphics Example.
ASRCS =
CSRCS = nxtext_main.c nxtext_bkgd.c nxtext_popup.c nxtext_putc.c
ifeq ($(CONFIG_NX_MULTIUSER),y)
CSRCS += nxtext_server.c
endif
AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT))
SRCS = $(ASRCS) $(CSRCS)
OBJS = $(AOBJS) $(COBJS)
ifeq ($(WINTOOL),y)
BIN = "${shell cygpath -w $(APPDIR)/libapps$(LIBEXT)}"
else
BIN = "$(APPDIR)/libapps$(LIBEXT)"
endif
ROOTDEPPATH = --dep-path .
# NXTEXT built-in application info
APPNAME = nxtext
PRIORITY = SCHED_PRIORITY_DEFAULT
STACKSIZE = 2048
# Common build
VPATH =
all: .built
.PHONY: context clean depend distclean
$(AOBJS): %$(OBJEXT): %.S
$(call ASSEMBLE, $<, $@)
$(COBJS): %$(OBJEXT): %.c
$(call COMPILE, $<, $@)
.built: $(OBJS)
@( for obj in $(OBJS) ; do \
$(call ARCHIVE, $(BIN), $${obj}); \
done ; )
@touch .built
.context:
ifeq ($(CONFIG_EXAMPLES_NXTEXT_BUILTIN),y)
$(call REGISTER,$(APPNAME),$(PRIORITY),$(STACKSIZE),$(APPNAME)_main)
@touch $@
endif
context: .context
.depend: Makefile $(SRCS)
@$(MKDEP) $(ROOTDEPPATH) $(CC) -- $(CFLAGS) -- $(SRCS) >Make.dep
@touch $@
depend: .depend
clean:
@rm -f *.o *~ .*.swp .built
$(call CLEAN)
distclean: clean
@rm -f Make.dep .depend
-include Make.dep

View File

@ -0,0 +1,467 @@
/****************************************************************************
* examples/nxtext/nxtext_bkgd.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxfonts.h>
#include "nxtext_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nxbg_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxbg_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxbg_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void nxbg_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static struct nxtext_state_s g_bgstate;
static struct nxtext_bitmap_s g_bgbm[CONFIG_EXAMPLES_NXTEXT_BMCACHE];
static struct nxtext_glyph_s g_bgglyph[CONFIG_EXAMPLES_NXTEXT_GLCACHE];
/****************************************************************************
* Public Data
****************************************************************************/
/* Background window call table */
const struct nx_callback_s g_bgcb =
{
nxbg_redraw, /* redraw */
nxbg_position /* position */
#ifdef CONFIG_NX_MOUSE
, nxbg_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxbg_kbdin /* my kbdin */
#endif
};
/* Background window handle */
NXHANDLE g_bgwnd;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxbg_redrawrect
****************************************************************************/
static void nxbg_redrawrect(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect)
{
int ret;
int i;
ret = nx_fill(hwnd, rect, g_bgstate.wcolor);
if (ret < 0)
{
message("nxbg_redrawrect: nx_fill failed: %d\n", errno);
}
/* Fill each character on the display (Only the characters within rect
* will actually be redrawn).
*/
for (i = 0; i < g_bgstate.nchars; i++)
{
nxtext_fillchar(hwnd, rect, &g_bgstate, g_bghfont, &g_bgstate.bm[i]);
}
}
/****************************************************************************
* Name: nxbg_redraw
****************************************************************************/
static void nxbg_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
nxbg_redrawrect(hwnd, rect);
}
/****************************************************************************
* Name: nxbg_position
****************************************************************************/
static void nxbg_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
FAR struct nxtext_state_s *st = (FAR struct nxtext_state_s *)arg;
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Have we picked off the window bounds yet? */
if (!b_haveresolution)
{
/* Save the background window handle */
g_bgwnd = hwnd;
/* Save the background window size */
st->wsize.w = size->w;
st->wsize.h = size->h;
/* Save the window limits (these should be the same for all places and all windows */
g_xres = bounds->pt2.x + 1;
g_yres = bounds->pt2.y + 1;
b_haveresolution = true;
sem_post(&g_semevent);
gvdbg("Have xres=%d yres=%d\n", g_xres, g_yres);
}
}
/****************************************************************************
* Name: nxbg_mousein
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxbg_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
message("nxbg_mousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nxbg_kbdin
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void nxbg_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
nxbg_write(hwnd, ch, nch);
}
#endif
/****************************************************************************
* Name: nxbg_movedisplay
*
* Description:
* This function implements the data movement for the scroll operation. If
* we can read the displays framebuffer memory, then the job is pretty
* easy. However, many displays (such as SPI-based LCDs) are often read-
* only.
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXTEXT_NOGETRUN
static inline void nxbg_movedisplay(NXWINDOW hwnd, int bottom, int lineheight)
{
FAR struct nxtext_bitmap_s *bm;
struct nxgl_rect_s rect;
nxgl_coord_t row;
int ret;
int i;
/* Move each row, one at a time. They could all be moved at once (by calling
* nxbg_redrawrect), but the since the region is cleared, then re-written, the
* effect would not be good. Below the region is also cleared and re-written,
* however, in much smaller chunks.
*/
rect.pt1.x = 0;
rect.pt2.x = g_bgstate.wsize.w - 1;
for (row = LINE_SEPARATION; row < bottom; row += lineheight)
{
/* Create a bounding box the size of one row of characters */
rect.pt1.y = row;
rect.pt2.y = row + lineheight - 1;
/* Clear the region */
ret = nx_fill(hwnd, &rect, g_bgstate.wcolor);
if (ret < 0)
{
message("nxbg_movedisplay: nx_fill failed: %d\n", errno);
}
/* Fill each character that might lie within in the bounding box */
for (i = 0; i < g_bgstate.nchars; i++)
{
bm = &g_bgstate.bm[i];
if (bm->pos.y <= rect.pt2.y && bm->pos.y + g_bgstate.fheight >= rect.pt1.y)
{
nxtext_fillchar(hwnd, &rect, &g_bgstate, g_bghfont, bm);
}
}
}
/* Finally, clear the bottom part of the display */
rect.pt1.y = bottom;
rect.pt2.y = g_bgstate.wsize.h- 1;
ret = nx_fill(hwnd, &rect, g_bgstate.wcolor);
if (ret < 0)
{
message("nxbg_movedisplay: nx_fill failed: %d\n", errno);
}
}
#else
static inline void nxbg_movedisplay(NXWINDOW hwnd, int bottom, int lineheight)
{
struct nxgl_rect_s rect;
struct nxgl_point_s offset;
int ret;
/* Move the display in the range of 0-height up one lineheight. The
* line at the bottom will be reset to the background color automatically.
*
* The source rectangle to be moved.
*/
rect.pt1.x = 0;
rect.pt1.y = lineheight + LINE_SEPARATION;
rect.pt2.x = g_bgstate.wsize.w - 1;
rect.pt2.y = g_bgstate.wsize.h - 1;
/* The offset that determines how far to move the source rectangle */
offset.x = 0;
offset.y = -lineheight;
/* Move the source rectangle */
ret = nx_move(hwnd, &rect, &offset);
if (ret < 0)
{
message("nxbg_movedisplay: nx_move failed: %d\n", errno);
}
}
#endif
/****************************************************************************
* Name: nxbg_scroll
****************************************************************************/
static inline void nxbg_scroll(NXWINDOW hwnd, int lineheight)
{
int i;
int j;
/* Adjust the vertical position of each character */
for (i = 0; i < g_bgstate.nchars; )
{
FAR struct nxtext_bitmap_s *bm = &g_bgstate.bm[i];
/* Has any part of this character scrolled off the screen? */
if (bm->pos.y < lineheight + LINE_SEPARATION)
{
/* Yes... Delete the character by moving all of the data */
for (j = i; j < g_bgstate.nchars-1; j++)
{
memcpy(&g_bgstate.bm[j], &g_bgstate.bm[j+1], sizeof(struct nxtext_bitmap_s));
}
/* Decrement the number of cached characters ('i' is not incremented
* in this case because it already points to the next character)
*/
g_bgstate.nchars--;
}
/* No.. just decrement its vertical position (moving it "up" the
* display by one line).
*/
else
{
bm->pos.y -= lineheight;
/* We are keeping this one so increment to the next character */
i++;
}
}
/* And move the next display position up by one line as well */
g_bgstate.fpos.y -= lineheight;
/* Move the display in the range of 0-height up one lineheight. */
nxbg_movedisplay(hwnd, g_bgstate.fpos.y, lineheight);
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxbg_getstate
*
* Description:
* Initialize the background window state structure.
*
****************************************************************************/
FAR struct nxtext_state_s *nxbg_getstate(void)
{
FAR const struct nx_font_s *fontset;
/* Initialize the color (used for redrawing the window) */
memset(&g_bgstate, 0, sizeof(struct nxtext_state_s));
g_bgstate.wcolor[0] = CONFIG_EXAMPLES_NXTEXT_BGCOLOR;
g_bgstate.fcolor[0] = CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR;
/* Get information about the font set being used and save this in the
* state structure
*/
fontset = nxf_getfontset(g_bghfont);
g_bgstate.fheight = fontset->mxheight;
g_bgstate.fwidth = fontset->mxwidth;
g_bgstate.spwidth = fontset->spwidth;
/* Set up the text caches */
g_bgstate.maxchars = CONFIG_EXAMPLES_NXTEXT_BMCACHE;
g_bgstate.maxglyphs = CONFIG_EXAMPLES_NXTEXT_GLCACHE;
g_bgstate.bm = g_bgbm;
g_bgstate.glyph = g_bgglyph;
/* Set the first display position */
nxtext_home(&g_bgstate);
return &g_bgstate;
}
/****************************************************************************
* Name: nxbg_write
*
* Description:
* Put a sequence of bytes in the window.
*
****************************************************************************/
void nxbg_write(NXWINDOW hwnd, FAR const uint8_t *buffer, size_t buflen)
{
int lineheight = (g_bgstate.fheight + LINE_SEPARATION);
while (buflen-- > 0)
{
/* Will another character fit on this line? */
if (g_bgstate.fpos.x + g_bgstate.fwidth > g_bgstate.wsize.w)
{
/* No.. move to the next line */
nxtext_newline(&g_bgstate);
/* If we were about to output a newline character, then don't */
if (*buffer == '\n')
{
buffer++;
continue;
}
}
/* Check if we need to scroll up (handling a corner case where
* there may be more than one newline).
*/
while (g_bgstate.fpos.y >= g_bgstate.wsize.h - lineheight)
{
nxbg_scroll(hwnd, lineheight);
}
/* Finally, we can output the character */
nxtext_putc(hwnd, &g_bgstate, g_bghfont, (uint8_t)*buffer++);
}
}

View File

@ -0,0 +1,330 @@
/****************************************************************************
* examples/nxtext/nxtext_internal.h
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
#ifndef __EXAMPLES_NXTEXT_NXTEXT_INTERNAL_H
#define __EXAMPLES_NXTEXT_NXTEXT_INTERNAL_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <semaphore.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxtk.h>
#include <nuttx/nx/nxfonts.h>
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_NX
# error "NX is not enabled (CONFIG_NX)"
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_VPLANE
# define CONFIG_EXAMPLES_NXTEXT_VPLANE 0
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_BPP
# define CONFIG_EXAMPLES_NXTEXT_BPP 32
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_BGCOLOR
# if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32
# define CONFIG_EXAMPLES_NXTEXT_BGCOLOR 0x007b68ee
# elif CONFIG_EXAMPLES_NXTEXT_BPP == 16
# define CONFIG_EXAMPLES_NXTEXT_BGCOLOR 0x7b5d
# else
# define CONFIG_EXAMPLES_NXTEXT_BGCOLOR ' '
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_PUFONTID
# define CONFIG_EXAMPLES_NXTEXT_PUFONTID NXFONT_DEFAULT
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_PUCOLOR
# if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32
# define CONFIG_EXAMPLES_NXTEXT_PUCOLOR 0x00dcdcdc
# elif CONFIG_EXAMPLES_NXTEXT_BPP == 16
# define CONFIG_EXAMPLES_NXTEXT_PUCOLOR 0xdefb
# else
# define CONFIG_EXAMPLES_NXTEXT_PUCOLOR '2'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_BGFONTID
# define CONFIG_EXAMPLES_NXTEXT_BGFONTID NXFONT_DEFAULT
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR
# if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32
# define CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR 0x00000000
# elif CONFIG_EXAMPLES_NXTEXT_BPP == 16
# define CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR 0x0000
# else
# define CONFIG_EXAMPLES_NXTEXT_BGFONTCOLOR 'F'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR
# if CONFIG_EXAMPLES_NXTEXT_BPP == 24 || CONFIG_EXAMPLES_NXTEXT_BPP == 32
# define CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR 0x00000000
# elif CONFIG_EXAMPLES_NXTEXT_BPP == 16
# define CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR 0x0000
# else
# define CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR 'F'
# endif
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_BMCACHE
# define CONFIG_EXAMPLES_NXTEXT_BMCACHE 128
#endif
#ifndef CONFIG_EXAMPLES_NXTEXT_GLCACHE
# define CONFIG_EXAMPLES_NXTEXT_BMCACHE 16
#endif
#ifdef CONFIG_NX_MULTIUSER
# ifdef CONFIG_DISABLE_MQUEUE
# error "The multi-threaded example requires MQ support (CONFIG_DISABLE_MQUEUE=n)"
# endif
# ifdef CONFIG_DISABLE_SIGNALS
# error "This example requires signal support (CONFIG_DISABLE_SIGNALS=n)"
# endif
# ifdef CONFIG_DISABLE_PTHREAD
# error "This example requires pthread support (CONFIG_DISABLE_PTHREAD=n)"
# endif
# ifndef CONFIG_NX_BLOCKING
# error "This example depends on CONFIG_NX_BLOCKING"
# endif
# ifndef CONFIG_EXAMPLES_NXTEXT_STACKSIZE
# define CONFIG_EXAMPLES_NXTEXT_STACKSIZE 2048
# endif
# ifndef CONFIG_EXAMPLES_NXTEXT_LISTENERPRIO
# define CONFIG_EXAMPLES_NXTEXT_LISTENERPRIO 100
# endif
# ifndef CONFIG_EXAMPLES_NXTEXT_CLIENTPRIO
# define CONFIG_EXAMPLES_NXTEXT_CLIENTPRIO 100
# endif
# ifndef CONFIG_EXAMPLES_NXTEXT_SERVERPRIO
# define CONFIG_EXAMPLES_NXTEXT_SERVERPRIO 120
# endif
# ifndef CONFIG_EXAMPLES_NXTEXT_NOTIFYSIGNO
# define CONFIG_EXAMPLES_NXTEXT_NOTIFYSIGNO 4
# endif
#endif
/* Debug ********************************************************************/
#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
/* Bitmap flags */
#define BMFLAGS_NOGLYPH (1 << 0) /* No glyph available, use space */
#define BM_ISSPACE(bm) (((bm)->flags & BMFLAGS_NOGLYPH) != 0)
/* Sizes and maximums */
#define MAX_USECNT 255 /* Limit to range of a uint8_t */
#define LINE_SEPARATION 2 /* Space (in rows) between lines */
/****************************************************************************
* Public Types
****************************************************************************/
enum exitcode_e
{
NXEXIT_SUCCESS = 0,
NXEXIT_SCHEDSETPARAM,
NXEXIT_TASKCREATE,
NXEXIT_PTHREADCREATE,
NXEXIT_EXTINITIALIZE,
NXEXIT_FBINITIALIZE,
NXEXIT_FBGETVPLANE,
NXEXIT_LCDINITIALIZE,
NXEXIT_LCDGETDEV,
NXEXIT_NXOPEN,
NXEXIT_FONTOPEN,
NXEXIT_NXREQUESTBKGD,
NXEXIT_NXCONNECT,
NXEXIT_NXSETBGCOLOR,
NXEXIT_NXOPENWINDOW,
NXEXIT_NXSETSIZE,
NXEXIT_NXSETPOSITION,
NXEXIT_NXCLOSEWINDOW,
NXEXIT_LOSTSERVERCONN
};
/* Describes one cached glyph bitmap */
struct nxtext_glyph_s
{
uint8_t code; /* Character code */
uint8_t height; /* Height of this glyph (in rows) */
uint8_t width; /* Width of this glyph (in pixels) */
uint8_t stride; /* Width of the glyph row (in bytes) */
uint8_t usecnt; /* Use count */
FAR uint8_t *bitmap; /* Allocated bitmap memory */
};
/* Describes on character on the display */
struct nxtext_bitmap_s
{
uint8_t code; /* Character code */
uint8_t flags; /* See BMFLAGS_* */
struct nxgl_point_s pos; /* Character position */
};
/* Describes the state of one text display */
struct nxtext_state_s
{
/* The following describe the window */
nxgl_mxpixel_t wcolor[CONFIG_NX_NPLANES]; /* Window color */
struct nxgl_size_s wsize; /* Window size */
struct nxgl_point_s wpos; /* Window position */
/* These characterize the font in use */
nxgl_mxpixel_t fcolor[CONFIG_NX_NPLANES]; /* Font color */
uint8_t fheight; /* Max height of a font in pixels */
uint8_t fwidth; /* Max width of a font in pixels */
uint8_t spwidth; /* The width of a space */
struct nxgl_point_s fpos; /* Next display position */
/* These describe all text already added to the display */
uint8_t maxglyphs; /* Size of the glyph[] array */
uint16_t maxchars; /* Size of the bm[] array */
uint16_t nchars; /* Number of chars in the bm[] array */
FAR struct nxtext_bitmap_s *bm; /* List of characters on the display */
FAR struct nxtext_glyph_s *glyph; /* Cache of rendered fonts in use */
};
/****************************************************************************
* Public Variables
****************************************************************************/
/* The connecton handler */
extern NXHANDLE g_hnx;
/* Background window handle */
extern NXHANDLE g_bgwnd;
/* The font handlse */
extern NXHANDLE g_bghfont;
extern NXHANDLE g_puhfont;
/* NX callback vtables */
extern const struct nx_callback_s g_bgcb;
/* The screen resolution */
extern nxgl_coord_t g_xres;
extern nxgl_coord_t g_yres;
extern bool b_haveresolution;
#ifdef CONFIG_NX_MULTIUSER
extern bool g_connected;
#endif
extern sem_t g_semevent;
extern int g_exitcode;
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXTEXT_EXTERNINIT
extern FAR NX_DRIVERTYPE *up_nxdrvinit(unsigned int devno);
#endif
#if defined(CONFIG_NX) && defined(CONFIG_NX_MULTIUSER)
extern int nxtext_server(int argc, char *argv[]);
extern FAR void *nxtext_listener(FAR void *arg);
#endif
/* Background window interfaces */
extern FAR struct nxtext_state_s *nxbg_getstate(void);
extern void nxbg_write(NXWINDOW hwnd, FAR const uint8_t *buffer, size_t buflen);
/* Pop-up window interfaces */
extern NXWINDOW nxpu_open(void);
extern int nxpu_close(NXWINDOW hwnd);
/* Generic text helpers */
extern void nxtext_home(FAR struct nxtext_state_s *st);
extern void nxtext_newline(FAR struct nxtext_state_s *st);
extern void nxtext_putc(NXWINDOW hwnd, FAR struct nxtext_state_s *st,
NXHANDLE hfont, uint8_t ch);
extern void nxtext_fillchar(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
FAR struct nxtext_state_s *st, NXHANDLE hfont,
FAR const struct nxtext_bitmap_s *bm);
#endif /* __EXAMPLES_NXTEXT_NXTEXT_INTERNAL_H */

View File

@ -0,0 +1,512 @@
/****************************************************************************
* examples/nxtext/nxtext_main.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <sched.h>
#include <pthread.h>
#include <errno.h>
#include <debug.h>
#ifdef CONFIG_NX_LCDDRIVER
# include <nuttx/lcd/lcd.h>
#else
# include <nuttx/fb.h>
#endif
#include <nuttx/arch.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxglib.h>
#include <nuttx/nx/nxfonts.h>
#include "nxtext_internal.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
/* If not specified, assume that the hardware supports one video plane */
#ifndef CONFIG_EXAMPLES_NXTEXT_VPLANE
# define CONFIG_EXAMPLES_NXTEXT_VPLANE 0
#endif
/* If not specified, assume that the hardware supports one LCD device */
#ifndef CONFIG_EXAMPLES_NXTEXT_DEVNO
# define CONFIG_EXAMPLES_NXTEXT_DEVNO 0
#endif
#define BGMSG_LINES 24
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_NX_KBD
static const uint8_t g_pumsg[] = "Pop-Up!";
static const char *g_bgmsg[BGMSG_LINES] =
{
"\nJULIET\n", /* Line 1 */
"Wilt thou be gone?\n", /* Line 2 */
" It is not yet near day:\n", /* Line 3 */
"It was the nightingale,\n", /* Line 4 */
" and not the lark,\n", /* Line 5 */
"That pierced the fearful hollow\n", /* Line 6 */
" of thine ear;\n", /* Line 7 */
"Nightly she sings\n", /* Line 8 */
" on yon pomegranate-tree:\n", /* Line 9 */
"Believe me, love,\n", /* Line 10 */
" it was the nightingale.\n", /* Line 11 */
"\nROMEO\n", /* Line 12 */
"It was the lark,\n", /* Line 13 */
" the herald of the morn,\n", /* Line 14 */
"No nightingale:\n", /* Line 15 */
" look, love, what envious streaks\n", /* Line 16 */
"Do lace the severing clouds\n", /* Line 17 */
" in yonder east:\n", /* Line 18 */
"Night's candles are burnt out,\n", /* Line 19 */
" and jocund day\n", /* Line 20 */
"Stands tiptoe\n", /* Line 21 */
" on the misty mountain tops.\n", /* Line 22 */
"I must be gone and live,\n", /* Line 23 */
" or stay and die.\n" /* Line 24 */
};
#endif
/****************************************************************************
* Public Data
****************************************************************************/
/* The connecton handler */
NXHANDLE g_hnx = NULL;
/* The font handles */
NXHANDLE g_bghfont = NULL;
NXHANDLE g_puhfont = NULL;
/* The screen resolution */
nxgl_coord_t g_xres;
nxgl_coord_t g_yres;
bool b_haveresolution = false;
#ifdef CONFIG_NX_MULTIUSER
bool g_connected = false;
#endif
sem_t g_semevent = {0};
int g_exitcode = NXEXIT_SUCCESS;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxtext_suinitialize
****************************************************************************/
#ifndef CONFIG_NX_MULTIUSER
static inline int nxtext_suinitialize(void)
{
FAR NX_DRIVERTYPE *dev;
#if defined(CONFIG_EXAMPLES_NXTEXT_EXTERNINIT)
/* Use external graphics driver initialization */
message("nxtext_initialize: Initializing external graphics device\n");
dev = up_nxdrvinit(CONFIG_EXAMPLES_NXTEXT_DEVNO);
if (!dev)
{
message("nxtext_initialize: up_nxdrvinit failed, devno=%d\n", CONFIG_EXAMPLES_NXTEXT_DEVNO);
g_exitcode = NXEXIT_EXTINITIALIZE;
return ERROR;
}
#elif defined(CONFIG_NX_LCDDRIVER)
int ret;
/* Initialize the LCD device */
message("nxtext_initialize: Initializing LCD\n");
ret = up_lcdinitialize();
if (ret < 0)
{
message("nxtext_initialize: up_lcdinitialize failed: %d\n", -ret);
g_exitcode = NXEXIT_LCDINITIALIZE;
return ERROR;
}
/* Get the device instance */
dev = up_lcdgetdev(CONFIG_EXAMPLES_NXTEXT_DEVNO);
if (!dev)
{
message("nxtext_initialize: up_lcdgetdev failed, devno=%d\n", CONFIG_EXAMPLES_NXTEXT_DEVNO);
g_exitcode = NXEXIT_LCDGETDEV;
return ERROR;
}
/* Turn the LCD on at 75% power */
(void)dev->setpower(dev, ((3*CONFIG_LCD_MAXPOWER + 3)/4));
#else
int ret;
/* Initialize the frame buffer device */
message("nxtext_initialize: Initializing framebuffer\n");
ret = up_fbinitialize();
if (ret < 0)
{
message("nxtext_initialize: up_fbinitialize failed: %d\n", -ret);
g_exitcode = NXEXIT_FBINITIALIZE;
return ERROR;
}
dev = up_fbgetvplane(CONFIG_EXAMPLES_NXTEXT_VPLANE);
if (!dev)
{
message("nxtext_initialize: up_fbgetvplane failed, vplane=%d\n", CONFIG_EXAMPLES_NXTEXT_VPLANE);
g_exitcode = NXEXIT_FBGETVPLANE;
return ERROR;
}
#endif
/* Then open NX */
message("nxtext_initialize: Open NX\n");
g_hnx = nx_open(dev);
if (!g_hnx)
{
message("nxtext_initialize: nx_open failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* Name: nxtext_initialize
****************************************************************************/
#ifdef CONFIG_NX_MULTIUSER
static inline int nxtext_muinitialize(void)
{
struct sched_param param;
pthread_t thread;
pid_t servrid;
int ret;
/* Set the client task priority */
param.sched_priority = CONFIG_EXAMPLES_NXTEXT_CLIENTPRIO;
ret = sched_setparam(0, &param);
if (ret < 0)
{
message("nxtext_initialize: sched_setparam failed: %d\n" , ret);
g_exitcode = NXEXIT_SCHEDSETPARAM;
return ERROR;
}
/* Start the server task */
message("nxtext_initialize: Starting nxtext_server task\n");
servrid = task_create("NX Server", CONFIG_EXAMPLES_NXTEXT_SERVERPRIO,
CONFIG_EXAMPLES_NXTEXT_STACKSIZE, nxtext_server, NULL);
if (servrid < 0)
{
message("nxtext_initialize: Failed to create nxtext_server task: %d\n", errno);
g_exitcode = NXEXIT_TASKCREATE;
return ERROR;
}
/* Wait a bit to let the server get started */
sleep(1);
/* Connect to the server */
g_hnx = nx_connect();
if (g_hnx)
{
pthread_attr_t attr;
/* Start a separate thread to listen for server events. This is probably
* the least efficient way to do this, but it makes this example flow more
* smoothly.
*/
(void)pthread_attr_init(&attr);
param.sched_priority = CONFIG_EXAMPLES_NXTEXT_LISTENERPRIO;
(void)pthread_attr_setschedparam(&attr, &param);
(void)pthread_attr_setstacksize(&attr, CONFIG_EXAMPLES_NXTEXT_STACKSIZE);
ret = pthread_create(&thread, &attr, nxtext_listener, NULL);
if (ret != 0)
{
printf("nxtext_initialize: pthread_create failed: %d\n", ret);
g_exitcode = NXEXIT_PTHREADCREATE;
return ERROR;
}
/* Don't return until we are connected to the server */
while (!g_connected)
{
/* Wait for the listener thread to wake us up when we really
* are connected.
*/
(void)sem_wait(&g_semevent);
}
}
else
{
message("nxtext_initialize: nx_connect failed: %d\n", errno);
g_exitcode = NXEXIT_NXCONNECT;
return ERROR;
}
return OK;
}
#endif
/****************************************************************************
* Name: nxtext_initialize
****************************************************************************/
static int nxtext_initialize(void)
{
#ifdef CONFIG_NX_MULTIUSER
return nxtext_muinitialize();
#else
return nxtext_suinitialize();
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: user_start/nxtext_main
****************************************************************************/
#ifdef CONFIG_EXAMPLES_NXTEXT_BUILTIN
# define MAIN_NAME nxtext_main
# define MAIN_NAME_STRING "nxtext_main"
#else
# define MAIN_NAME user_start
# define MAIN_NAME_STRING "user_start"
#endif
int MAIN_NAME(int argc, char **argv)
{
FAR struct nxtext_state_s *bgstate;
NXWINDOW hwnd = NULL;
nxgl_mxpixel_t color;
int popcnt;
int bkgndx;
int ret;
/* Initialize NX */
ret = nxtext_initialize();
message(MAIN_NAME_STRING ": NX handle=%p\n", g_hnx);
if (!g_hnx || ret < 0)
{
message(MAIN_NAME_STRING ": Failed to get NX handle: %d\n", errno);
g_exitcode = NXEXIT_NXOPEN;
goto errout;
}
/* Get the configured font handles */
g_bghfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXTEXT_BGFONTID);
if (!g_bghfont)
{
message(MAIN_NAME_STRING ": Failed to get background font handle: %d\n", errno);
g_exitcode = NXEXIT_FONTOPEN;
goto errout;
}
g_puhfont = nxf_getfonthandle(CONFIG_EXAMPLES_NXTEXT_PUFONTID);
if (!g_puhfont)
{
message(MAIN_NAME_STRING ": Failed to get pop-up font handle: %d\n", errno);
g_exitcode = NXEXIT_FONTOPEN;
goto errout;
}
/* Set the background to the configured background color */
message(MAIN_NAME_STRING ": Set background color=%d\n", CONFIG_EXAMPLES_NXTEXT_BGCOLOR);
color = CONFIG_EXAMPLES_NXTEXT_BGCOLOR;
ret = nx_setbgcolor(g_hnx, &color);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETBGCOLOR;
goto errout_with_nx;
}
/* Get the background window */
bgstate = nxbg_getstate();
ret = nx_requestbkgd(g_hnx, &g_bgcb, bgstate);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_setbgcolor failed: %d\n", errno);
g_exitcode = NXEXIT_NXREQUESTBKGD;
goto errout_with_nx;
}
/* Wait until we have the screen resolution. We'll have this immediately
* unless we are dealing with the NX server.
*/
while (!b_haveresolution)
{
(void)sem_wait(&g_semevent);
}
message(MAIN_NAME_STRING ": Screen resolution (%d,%d)\n", g_xres, g_yres);
/* Now loop, adding text to the background and periodically presenting
* a pop-up window.
*/
popcnt = 0;
bkgndx = 0;
for (;;)
{
/* Sleep for one second */
sleep(1);
popcnt++;
/* Each three seconds, create a pop-up window. Destroy the pop-up
* window after two more seconds.
*/
if (popcnt == 3)
{
/* Create a pop-up window */
hwnd = nxpu_open();
/* Give keyboard input to the top window (which should be the pop-up) */
#ifdef CONFIG_NX_KBD
message(MAIN_NAME_STRING ": Send keyboard input: %s\n", g_pumsg);
ret = nx_kbdin(g_hnx, strlen((FAR const char *)g_pumsg), g_pumsg);
if (ret < 0)
{
message(MAIN_NAME_STRING ": nx_kbdin failed: %d\n", errno);
goto errout_with_hwnd;
}
#endif
}
else if (popcnt == 5)
{
/* Destroy the pop-up window and restart the sequence */
message(MAIN_NAME_STRING ": Close pop-up\n");
(void)nxpu_close(hwnd);
popcnt = 0;
}
/* Give another line of text to the background window. Force this
* text to go the background by calling the background window interfaces
* directly.
*/
nxbg_write(g_bgwnd, (FAR const uint8_t *)g_bgmsg[bkgndx], strlen(g_bgmsg[bkgndx]));
if (++bkgndx >= BGMSG_LINES)
{
bkgndx = 0;
}
}
/* Close the pop-up window */
errout_with_hwnd:
if (popcnt >= 3)
{
message(MAIN_NAME_STRING ": Close pop-up\n");
(void)nxpu_close(hwnd);
}
//errout_with_bkgd:
(void)nx_releasebkgd(g_bgwnd);
errout_with_nx:
#ifdef CONFIG_NX_MULTIUSER
/* Disconnect from the server */
message(MAIN_NAME_STRING ": Disconnect from the server\n");
nx_disconnect(g_hnx);
#else
/* Close the server */
message(MAIN_NAME_STRING ": Close NX\n");
nx_close(g_hnx);
#endif
errout:
return g_exitcode;
}

View File

@ -0,0 +1,408 @@
/****************************************************************************
* examples/nxtext/nxtext_popup.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <semaphore.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/nx/nx.h>
#include <nuttx/nx/nxfonts.h>
#include "nxtext_internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
#define NBM_CACHE 8
#define NGLYPH_CACHE 8
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static void nxpu_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool morem, FAR void *arg);
static void nxpu_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg);
#ifdef CONFIG_NX_MOUSE
static void nxpu_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg);
#endif
#ifdef CONFIG_NX_KBD
static void nxpu_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
/* Pop-up NX callbacks */
static const struct nx_callback_s g_pucb =
{
nxpu_redraw, /* redraw */
nxpu_position /* position */
#ifdef CONFIG_NX_MOUSE
, nxpu_mousein /* mousein */
#endif
#ifdef CONFIG_NX_KBD
, nxpu_kbdin /* my kbdin */
#endif
};
/* Pop-up state information */
static struct nxtext_state_s g_pustate;
#ifdef CONFIG_NX_KBD
static struct nxtext_bitmap_s g_pubm[NBM_CACHE];
static struct nxtext_glyph_s g_puglyph[NGLYPH_CACHE];
#endif
/* Some random numbers */
static const uint8_t g_rand8[9] =
{
0x18, 0x8d, 0x60, 0x42, 0xb7, 0xc2, 0x2d, 0xea, 0x6b
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nxpu_randpos
****************************************************************************/
static fb_coord_t nxpu_randpos(fb_coord_t value)
{
static uint8_t ndx = 0;
uint8_t rand8 = g_rand8[ndx];
if (++ndx >= 9)
{
ndx = 0;
}
return (fb_coord_t)(((uint32_t)value * (uint32_t)rand8) >> 8);
}
/****************************************************************************
* Name: nxpu_setsize
****************************************************************************/
static inline int nxpu_setsize(NXWINDOW hwnd, FAR struct nxgl_size_s *size)
{
int ret = nx_setsize(hwnd, size);
if (ret < 0)
{
message("nxpu_setsize: nx_setsize failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETSIZE;
}
return ret;
}
/****************************************************************************
* Name: nxpu_setposition
****************************************************************************/
static inline int nxpu_setposition(NXWINDOW hwnd, FAR struct nxgl_point_s *pos)
{
int ret = nx_setposition(hwnd, pos);
if (ret < 0)
{
message("nxpu_setposition: nx_setposition failed: %d\n", errno);
g_exitcode = NXEXIT_NXSETPOSITION;
}
return ret;
}
/****************************************************************************
* Name: nxpu_fillwindow
****************************************************************************/
static inline void nxpu_fillwindow(NXWINDOW hwnd,
FAR const struct nxgl_rect_s *rect,
FAR struct nxtext_state_s *st)
{
int ret;
int i;
ret = nx_fill(hwnd, rect, st->wcolor);
if (ret < 0)
{
message("nxpu_fillwindow: nx_fill failed: %d\n", errno);
}
/* Fill each character on the display (Only the characters within rect
* will actually be redrawn).
*/
#ifdef CONFIG_NX_KBD
nxtext_home(st);
for (i = 0; i < st->nchars; i++)
{
nxtext_fillchar(hwnd, rect, st, g_puhfont, &st->bm[i]);
}
#endif
}
/****************************************************************************
* Name: nxpu_redraw
****************************************************************************/
static void nxpu_redraw(NXWINDOW hwnd, FAR const struct nxgl_rect_s *rect,
bool more, FAR void *arg)
{
FAR struct nxtext_state_s *st = (FAR struct nxtext_state_s *)arg;
gvdbg("hwnd=%p rect={(%d,%d),(%d,%d)} more=%s\n",
hwnd, rect->pt1.x, rect->pt1.y, rect->pt2.x, rect->pt2.y,
more ? "true" : "false");
nxpu_fillwindow(hwnd, rect, st);
}
/****************************************************************************
* Name: nxpu_position
****************************************************************************/
static void nxpu_position(NXWINDOW hwnd, FAR const struct nxgl_size_s *size,
FAR const struct nxgl_point_s *pos,
FAR const struct nxgl_rect_s *bounds,
FAR void *arg)
{
FAR struct nxtext_state_s *st = (FAR struct nxtext_state_s *)arg;
/* Report the position */
gvdbg("hwnd=%p size=(%d,%d) pos=(%d,%d) bounds={(%d,%d),(%d,%d)}\n",
hwnd, size->w, size->h, pos->x, pos->y,
bounds->pt1.x, bounds->pt1.y, bounds->pt2.x, bounds->pt2.y);
/* Save the window position and size */
st->wpos.x = pos->x;
st->wpos.y = pos->y;
st->wsize.w = size->w;
st->wsize.h = size->h;
}
/****************************************************************************
* Name: nxpu_mousein
****************************************************************************/
#ifdef CONFIG_NX_MOUSE
static void nxpu_mousein(NXWINDOW hwnd, FAR const struct nxgl_point_s *pos,
uint8_t buttons, FAR void *arg)
{
message("nxpu_mousein: hwnd=%p pos=(%d,%d) button=%02x\n",
hwnd, pos->x, pos->y, buttons);
}
#endif
/****************************************************************************
* Name: nxpu_puts
****************************************************************************/
static inline void nxpu_puts(NXWINDOW hwnd, FAR struct nxtext_state_s *st,
uint8_t nch, FAR const uint8_t *ch)
{
nxtext_home(st);
while (nch--)
{
nxtext_putc(hwnd, st, g_puhfont, *ch++);
}
}
/****************************************************************************
* Name: nxpu_kbdin
****************************************************************************/
#ifdef CONFIG_NX_KBD
static void nxpu_kbdin(NXWINDOW hwnd, uint8_t nch, FAR const uint8_t *ch,
FAR void *arg)
{
FAR struct nxtext_state_s *st = (FAR struct nxtext_state_s *)arg;
gvdbg("hwnd=%p nch=%d\n", hwnd, nch);
nxpu_puts(hwnd, st, nch, ch);
}
#endif
/****************************************************************************
* Name: nxpu_initstate
****************************************************************************/
static inline void nxpu_initstate(void)
{
#ifdef CONFIG_NX_KBD
FAR const struct nx_font_s *fontset;
#endif
/* Initialize the color (used for redrawing the window) */
memset(&g_pustate, 0, sizeof(struct nxtext_state_s));
g_pustate.wcolor[0] = CONFIG_EXAMPLES_NXTEXT_PUCOLOR;
g_pustate.fcolor[0] = CONFIG_EXAMPLES_NXTEXT_PUFONTCOLOR;
/* Get information about the font set being used and save this in the
* state structure
*/
#ifdef CONFIG_NX_KBD
fontset = nxf_getfontset(g_puhfont);
g_pustate.fheight = fontset->mxheight;
g_pustate.fwidth = fontset->mxwidth;
g_pustate.spwidth = fontset->spwidth;
/* Set up the text caches */
g_pustate.maxchars = NBM_CACHE;
g_pustate.maxglyphs = NGLYPH_CACHE;
g_pustate.bm = g_pubm;
g_pustate.glyph = g_puglyph;
/* Set the first display position */
nxtext_home(&g_pustate);
#endif
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: nxpu_open
****************************************************************************/
NXWINDOW nxpu_open(void)
{
NXWINDOW hwnd;
struct nxgl_size_s size;
struct nxgl_point_s pt;
int ret;
/* Create a pop-up window */
message("nxpu_open: Create pop-up\n");
nxpu_initstate();
hwnd = nx_openwindow(g_hnx, &g_pucb, (FAR void *)&g_pustate);
gvdbg("hwnd=%p\n", hwnd);
if (!hwnd)
{
message("nxpu_open: nx_openwindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXOPENWINDOW;
goto errout_with_state;
}
/* Select the size of the pop-up window */
size.w = g_xres / 4;
size.h = g_yres / 4;
/* Select a random position for pop-up window */
pt.x = nxpu_randpos(g_xres - size.w);
pt.y = nxpu_randpos(g_yres - size.h);
/* Set the position for the pop-up window */
message("nxpu_open: Set pop-up postion to (%d,%d)\n", pt.x, pt.y);
ret = nxpu_setposition(hwnd, &pt);
if (ret < 0)
{
goto errout_with_hwnd;
}
/* Set the size of the pop-up window */
gvdbg("Set pop-up size to (%d,%d)\n", size.w, size.h);
ret = nxpu_setsize(hwnd, &size);
if (ret < 0)
{
goto errout_with_hwnd;
}
return hwnd;
errout_with_hwnd:
(void)nx_closewindow(hwnd);
errout_with_state:
return NULL;
}
/****************************************************************************
* Name: nxpu_close
****************************************************************************/
int nxpu_close(NXWINDOW hwnd)
{
int ret;
ret = nx_closewindow(hwnd);
if (ret < 0)
{
message("nxpu_close: nx_closewindow failed: %d\n", errno);
g_exitcode = NXEXIT_NXCLOSEWINDOW;
return ret;
}
return OK;
}

Some files were not shown because too many files have changed in this diff Show More