[c5808b41] | 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 | /**
|
---|
[032bbe7] | 34 | * @file Internal TCP test
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <errno.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <thread.h>
|
---|
[32105348] | 41 | #include <str.h>
|
---|
[c5808b41] | 42 | #include "tcp_type.h"
|
---|
[762b48a] | 43 | #include "ucall.h"
|
---|
[c5808b41] | 44 |
|
---|
| 45 | #include "test.h"
|
---|
| 46 |
|
---|
[d9ce049] | 47 | #define RCV_BUF_SIZE 64
|
---|
| 48 |
|
---|
[c5808b41] | 49 | static void test_srv(void *arg)
|
---|
| 50 | {
|
---|
| 51 | tcp_conn_t *conn;
|
---|
[0ac2158] | 52 | tcp_sock_t lsock;
|
---|
| 53 | tcp_sock_t fsock;
|
---|
[d9ce049] | 54 | char rcv_buf[RCV_BUF_SIZE + 1];
|
---|
| 55 | size_t rcvd;
|
---|
| 56 | xflags_t xflags;
|
---|
[c5808b41] | 57 |
|
---|
| 58 | printf("test_srv()\n");
|
---|
[0ac2158] | 59 | lsock.port = 80;
|
---|
| 60 | lsock.addr.ipv4 = 0x7f000001;
|
---|
| 61 | fsock.port = 1024;
|
---|
| 62 | fsock.addr.ipv4 = 0x7f000001;
|
---|
[eea65f4] | 63 | printf("S: User open...\n");
|
---|
[0ac2158] | 64 | tcp_uc_open(&lsock, &fsock, ap_passive, &conn);
|
---|
[7cf7ded] | 65 | conn->name = (char *) "S";
|
---|
[d9ce049] | 66 |
|
---|
| 67 | while (true) {
|
---|
[eea65f4] | 68 | printf("S: User receive...\n");
|
---|
[d9ce049] | 69 | tcp_uc_receive(conn, rcv_buf, RCV_BUF_SIZE, &rcvd, &xflags);
|
---|
[8c7a054] | 70 | if (rcvd == 0) {
|
---|
[6896409c] | 71 | printf("S: End of data reached.\n");
|
---|
[8c7a054] | 72 | break;
|
---|
| 73 | }
|
---|
[d9ce049] | 74 | rcv_buf[rcvd] = '\0';
|
---|
[eea65f4] | 75 | printf("S: User received %zu bytes '%s'.\n", rcvd, rcv_buf);
|
---|
[d9ce049] | 76 |
|
---|
| 77 | async_usleep(1000*1000*2);
|
---|
| 78 | }
|
---|
[8c7a054] | 79 |
|
---|
[7cf7ded] | 80 | async_usleep(/*10**/1000*1000);
|
---|
[6e88fea] | 81 |
|
---|
[eea65f4] | 82 | printf("S: User close...\n");
|
---|
[6e88fea] | 83 | tcp_uc_close(conn);
|
---|
| 84 |
|
---|
[8c7a054] | 85 | printf("test_srv() terminating\n");
|
---|
[c5808b41] | 86 | }
|
---|
| 87 |
|
---|
| 88 | static void test_cli(void *arg)
|
---|
| 89 | {
|
---|
| 90 | tcp_conn_t *conn;
|
---|
[0ac2158] | 91 | tcp_sock_t lsock;
|
---|
| 92 | tcp_sock_t fsock;
|
---|
[32105348] | 93 | const char *msg = "Hello World!";
|
---|
[c5808b41] | 94 |
|
---|
| 95 | printf("test_cli()\n");
|
---|
| 96 |
|
---|
[0ac2158] | 97 | lsock.port = 1024;
|
---|
| 98 | lsock.addr.ipv4 = 0x7f000001;
|
---|
| 99 | fsock.port = 80;
|
---|
| 100 | fsock.addr.ipv4 = 0x7f000001;
|
---|
[8c7a054] | 101 |
|
---|
[c5808b41] | 102 | async_usleep(1000*1000*3);
|
---|
[eea65f4] | 103 | printf("C: User open...\n");
|
---|
[0ac2158] | 104 | tcp_uc_open(&lsock, &fsock, ap_active, &conn);
|
---|
[7cf7ded] | 105 | conn->name = (char *) "C";
|
---|
[32105348] | 106 |
|
---|
| 107 | async_usleep(1000*1000*10);
|
---|
[eea65f4] | 108 | printf("C: User send...\n");
|
---|
[32105348] | 109 | tcp_uc_send(conn, (void *)msg, str_size(msg), 0);
|
---|
[8c7a054] | 110 |
|
---|
[1812a0d] | 111 | async_usleep(1000*1000*20/**20*2*/);
|
---|
[eea65f4] | 112 | printf("C: User close...\n");
|
---|
[8c7a054] | 113 | tcp_uc_close(conn);
|
---|
[c5808b41] | 114 | }
|
---|
| 115 |
|
---|
| 116 | void tcp_test(void)
|
---|
| 117 | {
|
---|
| 118 | thread_id_t srv_tid;
|
---|
| 119 | thread_id_t cli_tid;
|
---|
| 120 | int rc;
|
---|
| 121 |
|
---|
| 122 | printf("tcp_test()\n");
|
---|
| 123 |
|
---|
[1812a0d] | 124 | async_usleep(1000*1000);
|
---|
| 125 |
|
---|
[a4ee3ab2] | 126 | if (0) {
|
---|
| 127 | rc = thread_create(test_srv, NULL, "test_srv", &srv_tid);
|
---|
| 128 | if (rc != EOK) {
|
---|
| 129 | printf("Failed to create server thread.\n");
|
---|
| 130 | return;
|
---|
| 131 | }
|
---|
[c5808b41] | 132 | }
|
---|
| 133 |
|
---|
[04cd242] | 134 | if (0) {
|
---|
| 135 | rc = thread_create(test_cli, NULL, "test_cli", &cli_tid);
|
---|
| 136 | if (rc != EOK) {
|
---|
| 137 | printf("Failed to create client thread.\n");
|
---|
| 138 | return;
|
---|
| 139 | }
|
---|
[c5808b41] | 140 | }
|
---|
| 141 | }
|
---|
| 142 |
|
---|
| 143 | /**
|
---|
| 144 | * @}
|
---|
| 145 | */
|
---|