From 45fe3f79433403d54626d830d2f0932e0af57f2d Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 7 Feb 2009 02:46:14 +0000 Subject: Fix compilation problems with eZ80 targets git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1473 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/ChangeLog | 3 +- nuttx/Documentation/NuttX.html | 2 +- nuttx/configs/ez80f910200kitg/ostest/Make.defs | 1 - nuttx/examples/nettest/Makefile | 8 +- nuttx/examples/nettest/nettest-client.c | 205 ---------------------- nuttx/examples/nettest/nettest-server.c | 234 ------------------------- nuttx/examples/nettest/nettest.h | 12 +- nuttx/examples/nettest/nettest_client.c | 205 ++++++++++++++++++++++ nuttx/examples/nettest/nettest_server.c | 234 +++++++++++++++++++++++++ 9 files changed, 456 insertions(+), 448 deletions(-) delete mode 100644 nuttx/examples/nettest/nettest-client.c delete mode 100644 nuttx/examples/nettest/nettest-server.c create mode 100644 nuttx/examples/nettest/nettest_client.c create mode 100644 nuttx/examples/nettest/nettest_server.c (limited to 'nuttx') diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index f8651c111..52f03f447 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -609,5 +609,6 @@ * Updated all ARM Make.def files to work with gcc 2.4.2 (However, there are still some build issues associated with that toolchain in use of arm-elf-objcopy -- see the TODO.txt list for details) - * Fix problems with Z16F compilation introduced with recent changes. + * Fix problems with Z16F and eZ80 compilation introduced with recent changes. + diff --git a/nuttx/Documentation/NuttX.html b/nuttx/Documentation/NuttX.html index d19308721..4ae19800c 100644 --- a/nuttx/Documentation/NuttX.html +++ b/nuttx/Documentation/NuttX.html @@ -1297,7 +1297,7 @@ nuttx-0.4.1 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> * Updated all ARM Make.def files to work with gcc 2.4.2 (However, there are still some build issues associated with that toolchain in use of arm-elf-objcopy -- see the TODO.txt list for details) - * Fix problems with Z16F compilation introduced with recent changes. + * Fix problems with Z16F and eZ80 compilation introduced with recent changes. pascal-0.1.3 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> diff --git a/nuttx/configs/ez80f910200kitg/ostest/Make.defs b/nuttx/configs/ez80f910200kitg/ostest/Make.defs index 83ae3abf8..c7fcf2f4e 100644 --- a/nuttx/configs/ez80f910200kitg/ostest/Make.defs +++ b/nuttx/configs/ez80f910200kitg/ostest/Make.defs @@ -147,7 +147,6 @@ endef define ASSEMBLE @#echo "AS: $1" - @$(AS) -c $(AFLAGS) $1 @(wfile=`cygpath -w $1`; $(AS) $(AFLAGS) $$wfile) endef diff --git a/nuttx/examples/nettest/Makefile b/nuttx/examples/nettest/Makefile index 0dd12ccbf..6677f847f 100644 --- a/nuttx/examples/nettest/Makefile +++ b/nuttx/examples/nettest/Makefile @@ -41,9 +41,9 @@ TARG_AOBJS = $(TARG_ASRCS:.S=$(OBJEXT)) TARG_CSRCS = nettest.c ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y) -TARG_CSRCS += nettest-server.c +TARG_CSRCS += nettest_server.c else -TARG_CSRCS += nettest-client.c +TARG_CSRCS += nettest_client.c endif TARG_COBJS = $(TARG_CSRCS:.c=$(OBJEXT)) @@ -61,9 +61,9 @@ endif HOST_SRCS = host.c ifeq ($(CONFIG_EXAMPLE_NETTEST_SERVER),y) -HOST_SRCS += nettest-client.c +HOST_SRCS += nettest_client.c else -HOST_SRCS += nettest-server.c +HOST_SRCS += nettest_server.c endif HOST_OBJS = $(HOST_SRCS:.c=.o) diff --git a/nuttx/examples/nettest/nettest-client.c b/nuttx/examples/nettest/nettest-client.c deleted file mode 100644 index 371f4d4f8..000000000 --- a/nuttx/examples/nettest/nettest-client.c +++ /dev/null @@ -1,205 +0,0 @@ -/**************************************************************************** - * examples/nettest/nettest-client.c - * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include -#include -#include - -#include -#include -#include -#include -#include - -#include "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 receive messages forever */ - - for (;;) - { - nbytessent = send(sockfd, outbuf, 512, 0); - if (nbytessent < 0) - { - message("client: send failed: %d\n", errno); - goto errout_with_socket; - } - else if (nbytessent != 512) - { - message("client: Bad send length=%d: %d\n", nbytessent); - goto errout_with_socket; - } - } -#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); -} diff --git a/nuttx/examples/nettest/nettest-server.c b/nuttx/examples/nettest/nettest-server.c deleted file mode 100644 index 6d9ccaeba..000000000 --- a/nuttx/examples/nettest/nettest-server.c +++ /dev/null @@ -1,234 +0,0 @@ -/**************************************************************************** - * examples/nettest/nettest-server.c - * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. - * Author: Gregory Nutt - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be - * used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS - * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, - * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS - * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED - * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN - * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include -#include -#include - -#include -#include -#include -#include - -#include "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; - } - } -#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); -} diff --git a/nuttx/examples/nettest/nettest.h b/nuttx/examples/nettest/nettest.h index db84ac495..fafe29355 100644 --- a/nuttx/examples/nettest/nettest.h +++ b/nuttx/examples/nettest/nettest.h @@ -57,7 +57,11 @@ /* Used printf for debug output */ -# define message(...) printf(__VA_ARGS__) +# ifdef CONFIG_CPP_HAVE_VARARGS +# define message(...) printf(__VA_ARGS__) +# else +# define message printf +# endif /* Have SO_LINGER */ @@ -67,7 +71,11 @@ /* Used lib_rawprintf() so that there is not confusion from buffered IO */ -# define message(...) lib_rawprintf(__VA_ARGS__) +# ifdef CONFIG_CPP_HAVE_VARARGS +# define message(...) lib_rawprintf(__VA_ARGS__) +# else +# define message lib_rawprintf +# endif /* At present, uIP does only abortive disconnects */ diff --git a/nuttx/examples/nettest/nettest_client.c b/nuttx/examples/nettest/nettest_client.c new file mode 100644 index 000000000..371f4d4f8 --- /dev/null +++ b/nuttx/examples/nettest/nettest_client.c @@ -0,0 +1,205 @@ +/**************************************************************************** + * examples/nettest/nettest-client.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "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 receive messages forever */ + + for (;;) + { + nbytessent = send(sockfd, outbuf, 512, 0); + if (nbytessent < 0) + { + message("client: send failed: %d\n", errno); + goto errout_with_socket; + } + else if (nbytessent != 512) + { + message("client: Bad send length=%d: %d\n", nbytessent); + goto errout_with_socket; + } + } +#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); +} diff --git a/nuttx/examples/nettest/nettest_server.c b/nuttx/examples/nettest/nettest_server.c new file mode 100644 index 000000000..6d9ccaeba --- /dev/null +++ b/nuttx/examples/nettest/nettest_server.c @@ -0,0 +1,234 @@ +/**************************************************************************** + * examples/nettest/nettest-server.c + * + * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include + +#include "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; + } + } +#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); +} -- cgit v1.2.3