1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file Internal TCP test
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <thread.h>
|
---|
41 | #include <str.h>
|
---|
42 | #include "state.h"
|
---|
43 | #include "tcp_type.h"
|
---|
44 |
|
---|
45 | #include "test.h"
|
---|
46 |
|
---|
47 | #define RCV_BUF_SIZE 64
|
---|
48 |
|
---|
49 | static void test_srv(void *arg)
|
---|
50 | {
|
---|
51 | tcp_conn_t *conn;
|
---|
52 | tcp_sock_t sock;
|
---|
53 | char rcv_buf[RCV_BUF_SIZE + 1];
|
---|
54 | size_t rcvd;
|
---|
55 | xflags_t xflags;
|
---|
56 |
|
---|
57 | printf("test_srv()\n");
|
---|
58 | sock.port = 1024;
|
---|
59 | sock.addr.ipv4 = 0x7f000001;
|
---|
60 | printf("S: User open...\n");
|
---|
61 | tcp_uc_open(80, &sock, ap_passive, &conn);
|
---|
62 | conn->name = (char *) "S";
|
---|
63 |
|
---|
64 | while (true) {
|
---|
65 | printf("S: User receive...\n");
|
---|
66 | tcp_uc_receive(conn, rcv_buf, RCV_BUF_SIZE, &rcvd, &xflags);
|
---|
67 | if (rcvd == 0) {
|
---|
68 | printf("S: End of data reached.\n");
|
---|
69 | break;
|
---|
70 | }
|
---|
71 | rcv_buf[rcvd] = '\0';
|
---|
72 | printf("S: User received %zu bytes '%s'.\n", rcvd, rcv_buf);
|
---|
73 |
|
---|
74 | async_usleep(1000*1000*2);
|
---|
75 | }
|
---|
76 |
|
---|
77 | async_usleep(/*10**/1000*1000);
|
---|
78 |
|
---|
79 | printf("S: User close...\n");
|
---|
80 | tcp_uc_close(conn);
|
---|
81 |
|
---|
82 | printf("test_srv() terminating\n");
|
---|
83 | }
|
---|
84 |
|
---|
85 | static void test_cli(void *arg)
|
---|
86 | {
|
---|
87 | tcp_conn_t *conn;
|
---|
88 | tcp_sock_t sock;
|
---|
89 | const char *msg = "Hello World!";
|
---|
90 |
|
---|
91 | printf("test_cli()\n");
|
---|
92 |
|
---|
93 | sock.port = 80;
|
---|
94 | sock.addr.ipv4 = 0x7f000001;
|
---|
95 |
|
---|
96 | async_usleep(1000*1000*3);
|
---|
97 | printf("C: User open...\n");
|
---|
98 | tcp_uc_open(1024, &sock, ap_active, &conn);
|
---|
99 | conn->name = (char *) "C";
|
---|
100 |
|
---|
101 | async_usleep(1000*1000*10);
|
---|
102 | printf("C: User send...\n");
|
---|
103 | tcp_uc_send(conn, (void *)msg, str_size(msg), 0);
|
---|
104 |
|
---|
105 | async_usleep(1000*1000*3/**20*2*/);
|
---|
106 | printf("C: User close...\n");
|
---|
107 | tcp_uc_close(conn);
|
---|
108 | }
|
---|
109 |
|
---|
110 | void tcp_test(void)
|
---|
111 | {
|
---|
112 | thread_id_t srv_tid;
|
---|
113 | thread_id_t cli_tid;
|
---|
114 | int rc;
|
---|
115 |
|
---|
116 | printf("tcp_test()\n");
|
---|
117 |
|
---|
118 | rc = thread_create(test_srv, NULL, "test_srv", &srv_tid);
|
---|
119 | if (rc != EOK) {
|
---|
120 | printf("Failed to create server thread.\n");
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | rc = thread_create(test_cli, NULL, "test_cli", &cli_tid);
|
---|
125 | if (rc != EOK) {
|
---|
126 | printf("Failed to create client thread.\n");
|
---|
127 | return;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * @}
|
---|
133 | */
|
---|