| 1 |
786 |
skrzyp |
//==========================================================================
|
| 2 |
|
|
//
|
| 3 |
|
|
// socket.c
|
| 4 |
|
|
//
|
| 5 |
|
|
// Simple test-case for the BSD socket API : echo on TCP port 7.
|
| 6 |
|
|
//
|
| 7 |
|
|
//==========================================================================
|
| 8 |
|
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
| 9 |
|
|
// -------------------------------------------
|
| 10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
| 11 |
|
|
// Copyright (C) 2009 Free Software Foundation
|
| 12 |
|
|
//
|
| 13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
| 14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
| 15 |
|
|
// Software Foundation; either version 2 or (at your option) any later version.
|
| 16 |
|
|
//
|
| 17 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
| 18 |
|
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
| 19 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 20 |
|
|
// for more details.
|
| 21 |
|
|
//
|
| 22 |
|
|
// You should have received a copy of the GNU General Public License along
|
| 23 |
|
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
| 24 |
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
| 25 |
|
|
//
|
| 26 |
|
|
// As a special exception, if other files instantiate templates or use macros
|
| 27 |
|
|
// or inline functions from this file, or you compile this file and link it
|
| 28 |
|
|
// with other works to produce a work based on this file, this file does not
|
| 29 |
|
|
// by itself cause the resulting work to be covered by the GNU General Public
|
| 30 |
|
|
// License. However the source code for this file must still be made available
|
| 31 |
|
|
// in accordance with section (3) of the GNU General Public License.
|
| 32 |
|
|
//
|
| 33 |
|
|
// This exception does not invalidate any other reasons why a work based on
|
| 34 |
|
|
// this file might be covered by the GNU General Public License.
|
| 35 |
|
|
// -------------------------------------------
|
| 36 |
|
|
//####ECOSGPLCOPYRIGHTEND####
|
| 37 |
|
|
//==========================================================================
|
| 38 |
|
|
//#####DESCRIPTIONBEGIN####
|
| 39 |
|
|
//
|
| 40 |
|
|
// Author(s): Simon Kallweit
|
| 41 |
|
|
// Contributors:
|
| 42 |
|
|
// Date: 2009-05-14
|
| 43 |
|
|
// Purpose:
|
| 44 |
|
|
// Description: Simple test-case for the BSD socket API : echo on TCP port 7.
|
| 45 |
|
|
//
|
| 46 |
|
|
//####DESCRIPTIONEND####
|
| 47 |
|
|
//
|
| 48 |
|
|
//==========================================================================
|
| 49 |
|
|
|
| 50 |
|
|
#include <cyg/hal/hal_arch.h>
|
| 51 |
|
|
#include <cyg/infra/diag.h>
|
| 52 |
|
|
#include <cyg/infra/testcase.h>
|
| 53 |
|
|
|
| 54 |
|
|
#include <pkgconf/net_lwip.h>
|
| 55 |
|
|
|
| 56 |
|
|
#include <lwip.h>
|
| 57 |
|
|
#include <lwip/sockets.h>
|
| 58 |
|
|
|
| 59 |
|
|
#ifdef CYGFUN_LWIP_MODE_SEQUENTIAL
|
| 60 |
|
|
#if LWIP_SOCKET
|
| 61 |
|
|
#if LWIP_TCP
|
| 62 |
|
|
#if LWIP_COMPAT_SOCKETS
|
| 63 |
|
|
#if LWIP_POSIX_SOCKETS_IO_NAMES
|
| 64 |
|
|
|
| 65 |
|
|
#define TCP_PORT 7
|
| 66 |
|
|
|
| 67 |
|
|
#define STACK_SIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
| 68 |
|
|
|
| 69 |
|
|
static char main_stack[STACK_SIZE];
|
| 70 |
|
|
static cyg_thread main_thread;
|
| 71 |
|
|
static cyg_handle_t main_handle;
|
| 72 |
|
|
|
| 73 |
|
|
static char socket_stack[STACK_SIZE];
|
| 74 |
|
|
|
| 75 |
|
|
static char buf[400];
|
| 76 |
|
|
|
| 77 |
|
|
static void socket_thread_entry(void *arg)
|
| 78 |
|
|
{
|
| 79 |
|
|
int sock, s;
|
| 80 |
|
|
int len;
|
| 81 |
|
|
struct sockaddr_in addr, rem;
|
| 82 |
|
|
socklen_t addrlen;
|
| 83 |
|
|
|
| 84 |
|
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
| 85 |
|
|
addr.sin_family = AF_INET;
|
| 86 |
|
|
addr.sin_port = htons(TCP_PORT);
|
| 87 |
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
| 88 |
|
|
|
| 89 |
|
|
bind(sock, (struct sockaddr *) &addr, sizeof(addr));
|
| 90 |
|
|
|
| 91 |
|
|
listen(sock, 5);
|
| 92 |
|
|
while (1) {
|
| 93 |
|
|
addrlen = sizeof(rem);
|
| 94 |
|
|
s = accept(sock, (struct sockaddr *) &rem, &addrlen);
|
| 95 |
|
|
while ((len = read(s, buf, 400)) > 0)
|
| 96 |
|
|
write(s, buf, len);
|
| 97 |
|
|
close(s);
|
| 98 |
|
|
}
|
| 99 |
|
|
}
|
| 100 |
|
|
|
| 101 |
|
|
void main_thread_entry(cyg_addrword_t p)
|
| 102 |
|
|
{
|
| 103 |
|
|
cyg_lwip_sequential_init();
|
| 104 |
|
|
cyg_lwip_thread_new("socket", socket_thread_entry, NULL,
|
| 105 |
|
|
socket_stack, STACK_SIZE, 7);
|
| 106 |
|
|
}
|
| 107 |
|
|
|
| 108 |
|
|
void socket_main(void)
|
| 109 |
|
|
{
|
| 110 |
|
|
CYG_TEST_INIT();
|
| 111 |
|
|
CYG_TEST_INFO("Testing sockets");
|
| 112 |
|
|
diag_printf("This test will echo TCP packets on local port %d\n", TCP_PORT);
|
| 113 |
|
|
|
| 114 |
|
|
// Create a main thread, so we can run the scheduler and have time 'pass'
|
| 115 |
|
|
cyg_thread_create(
|
| 116 |
|
|
10, // Priority - just a number
|
| 117 |
|
|
main_thread_entry, // Entry
|
| 118 |
|
|
0, // Entry parameter
|
| 119 |
|
|
"main", // Name
|
| 120 |
|
|
main_stack, // Stack
|
| 121 |
|
|
STACK_SIZE, // Size
|
| 122 |
|
|
&main_handle, // Handle
|
| 123 |
|
|
&main_thread // Thread data structure
|
| 124 |
|
|
);
|
| 125 |
|
|
cyg_thread_resume(main_handle);
|
| 126 |
|
|
cyg_scheduler_start();
|
| 127 |
|
|
|
| 128 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
| 129 |
|
|
}
|
| 130 |
|
|
|
| 131 |
|
|
externC void cyg_start(void)
|
| 132 |
|
|
{
|
| 133 |
|
|
socket_main();
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
#else // LWIP_POSIX_SOCKETS_IO_NAMES
|
| 137 |
|
|
#define N_A_MSG "POSIX socket function names disabled"
|
| 138 |
|
|
#endif
|
| 139 |
|
|
|
| 140 |
|
|
#else // LWIP_COMPAT_SOCKETS
|
| 141 |
|
|
#define N_A_MSG "Compatible socket support disabled"
|
| 142 |
|
|
#endif // LWIP_COMPAT_SOCKETS
|
| 143 |
|
|
|
| 144 |
|
|
#else // LWIP_TCP
|
| 145 |
|
|
#define N_A_MSG "TCP support disabled"
|
| 146 |
|
|
#endif // LWIP_TCP
|
| 147 |
|
|
|
| 148 |
|
|
#else // LWIP_SOCKET
|
| 149 |
|
|
#define N_A_MSG "Socket support disabled"
|
| 150 |
|
|
#endif // LWIP_SOCKET
|
| 151 |
|
|
|
| 152 |
|
|
#else // CYGFUN_LWIP_MODE_SEQUENTIAL
|
| 153 |
|
|
#define N_A_MSG "Not configured in 'Sequential' mode"
|
| 154 |
|
|
#endif // CYGFUN_LWIP_MODE_SEQUENTIAL
|
| 155 |
|
|
|
| 156 |
|
|
#ifdef N_A_MSG
|
| 157 |
|
|
externC void
|
| 158 |
|
|
cyg_start(void)
|
| 159 |
|
|
{
|
| 160 |
|
|
CYG_TEST_INIT();
|
| 161 |
|
|
CYG_TEST_NA(N_A_MSG);
|
| 162 |
|
|
}
|
| 163 |
|
|
#endif // N_A_MSG
|