diff options
author | Jun Wako <wakojun@gmail.com> | 2015-04-24 16:26:14 +0900 |
---|---|---|
committer | Jun Wako <wakojun@gmail.com> | 2015-04-24 16:26:14 +0900 |
commit | a3d96d3aa96318d339a67de1085e0ae495d57c84 (patch) | |
tree | db85c16d03b52399d6c109eda7ea0341a0de0b1d /tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo | |
parent | 1d5bac21dc6f1425b8ef4bbe7935330c37c3a93e (diff) | |
parent | 1fe4406f374291ab2e86e95a97341fd9c475fcb8 (diff) |
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
Diffstat (limited to 'tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo')
6 files changed, 454 insertions, 0 deletions
diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client/main.cpp new file mode 100644 index 0000000000..17193f08d0 --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client/main.cpp @@ -0,0 +1,59 @@ +#include "mbed.h" +#include "test_env.h" +#include "EthernetInterface.h" + +struct s_ip_address { + int ip_1; + int ip_2; + int ip_3; + int ip_4; +}; + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(tcpecho_client_auto); + MBED_HOSTTEST_DESCRIPTION(TCP echo client); + MBED_HOSTTEST_START("NET_4"); + + char buffer[256] = {0}; + char out_buffer[] = "Hello World\n"; + char out_success[] = "{{success}}\n{{end}}\n"; + char out_failure[] = "{{failure}}\n{{end}}\n"; + s_ip_address ip_addr = {0, 0, 0, 0}; + int port = 0; + + printf("TCPCllient waiting for server IP and port..." NL); + scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); + printf("Address received:%d.%d.%d.%d:%d" NL, ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + printf("TCPClient IP Address is %s" NL, eth.getIPAddress()); + sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); + + TCPSocketConnection socket; + while (socket.connect(buffer, port) < 0) { + printf("TCPCllient unable to connect to %s:%d" NL, buffer, port); + wait(1); + } + + socket.send_all(out_buffer, sizeof(out_buffer) - 1); + + int n = socket.receive(buffer, sizeof(buffer)); + if (n > 0) + { + buffer[n] = '\0'; + printf("%s", buffer); + if (strncmp(out_buffer, buffer, sizeof(out_buffer) - 1) == 0) { + socket.send_all(out_success, sizeof(out_success) - 1); + } + } + + socket.send_all(out_failure, sizeof(out_failure) - 1); + + socket.close(); + eth.disconnect(); + return 0; +} diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client_loop/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client_loop/main.cpp new file mode 100644 index 0000000000..6c797e7443 --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_client_loop/main.cpp @@ -0,0 +1,78 @@ +#include "mbed.h" +#include "test_env.h" +#include "EthernetInterface.h" +#include <algorithm> + +namespace { + const int BUFFER_SIZE = 64; + const int MAX_ECHO_LOOPS = 1000; + const char ASCII_MAX = '~' - ' '; + + struct s_ip_address + { + int ip_1; + int ip_2; + int ip_3; + int ip_4; + }; +} + +char char_rand() { + return (rand() % ASCII_MAX) + ' '; +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(tcpecho_client_auto); + MBED_HOSTTEST_DESCRIPTION(TCP client echo loop); + MBED_HOSTTEST_START("NET_13"); + + char buffer[BUFFER_SIZE] = {0}; + char out_buffer[BUFFER_SIZE] = {0}; + s_ip_address ip_addr = {0, 0, 0, 0}; + int port = 0; + + printf("MBED: TCPCllient waiting for server IP and port...\r\n"); + scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); + printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + + printf("MBED: TCPClient IP Address is %s\r\n", eth.getIPAddress()); + sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); + + TCPSocketConnection socket; + while (socket.connect(buffer, port) < 0) { + printf("MBED: TCPCllient unable to connect to %s:%d\r\n", buffer, port); + wait(1); + } + + // Test loop for multiple client connections + bool result = true; + int count_error = 0; + for (int i = 0; i < MAX_ECHO_LOOPS; i++) { + std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand); + socket.send_all(out_buffer, BUFFER_SIZE); + + int n = socket.receive(buffer, BUFFER_SIZE); + if (n > 0) + { + bool echoed = memcmp(out_buffer, buffer, BUFFER_SIZE) == 0; + result = result && echoed; + if (echoed == false) { + count_error++; // Count error messages + } + } + } + + printf("MBED: Loop messages passed: %d / %d\r\n", MAX_ECHO_LOOPS - count_error, MAX_ECHO_LOOPS); + + if (notify_completion_str(result, buffer)) { + socket.send_all(buffer, strlen(buffer)); + } + socket.close(); + eth.disconnect(); + MBED_HOSTTEST_RESULT(result); +} diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_server/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_server/main.cpp new file mode 100644 index 0000000000..421fb0dbec --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/tcp_server/main.cpp @@ -0,0 +1,47 @@ +#include "mbed.h" +#include "test_env.h" +#include "EthernetInterface.h" + +namespace { + const int ECHO_SERVER_PORT = 7; + const int BUFFER_SIZE = 64; +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(tcpecho_server_auto); + MBED_HOSTTEST_DESCRIPTION(TCP echo server); + MBED_HOSTTEST_START("NET_3"); + + char buffer[BUFFER_SIZE] = {0}; + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + printf("MBED: Server IP Address is %s:%d" NL, eth.getIPAddress(), ECHO_SERVER_PORT); + + TCPSocketServer server; + server.bind(ECHO_SERVER_PORT); + server.listen(); + + while (true) { + printf("MBED: Wait for new connection..." NL); + TCPSocketConnection client; + server.accept(client); + client.set_blocking(false, 1500); // Timeout after (1.5)s + printf("MBED: Connection from: %s" NL, client.get_address()); + + while (true) { + const int n = client.receive(buffer, sizeof(buffer)); + if (n <= 0) { + break; + } + const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n; + buffer[buffer_string_end_index] = '\0'; + client.send_all(buffer, n); + if (n <= 0) { + break; + } + } + client.close(); + } +} diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_client/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_client/main.cpp new file mode 100644 index 0000000000..97f6050508 --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_client/main.cpp @@ -0,0 +1,82 @@ +#include "mbed.h" +#include "rtos.h" +#include "test_env.h" +#include "EthernetInterface.h" +#include <algorithm> + +#define CHECK(RC, STEP) if (RC < 0) error(STEP": %d\r\n", RC) + +namespace { + const int BUFFER_SIZE = 64; + const int MAX_ECHO_LOOPS = 100; + const char ASCII_MAX = '~' - ' '; + + struct s_ip_address { + int ip_1; + int ip_2; + int ip_3; + int ip_4; + }; +} + +char char_rand() { + return (rand() % ASCII_MAX) + ' '; +} + +int main() { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(udpecho_client_auto); + MBED_HOSTTEST_DESCRIPTION(UDP echo client); + MBED_HOSTTEST_START("NET_6"); + + char buffer[BUFFER_SIZE] = {0}; + char out_buffer[BUFFER_SIZE] = {0}; + s_ip_address ip_addr = {0, 0, 0, 0}; + int port = 0; + bool result = true; + + printf("MBED: UDPCllient waiting for server IP and port...\r\n"); + scanf("%d.%d.%d.%d:%d", &ip_addr.ip_1, &ip_addr.ip_2, &ip_addr.ip_3, &ip_addr.ip_4, &port); + printf("MBED: Address received: %d.%d.%d.%d:%d\r\n", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4, port); + + EthernetInterface eth; + int rc = eth.init(); //Use DHCP + CHECK(rc, "eth init"); + + rc = eth.connect(); + CHECK(rc, "connect"); + printf("IP: %s\n", eth.getIPAddress()); + + UDPSocket socket; + rc = socket.init(); + CHECK(rc, "socket init"); + + printf("MBED: UDPClient IP Address is %s\r\n", eth.getIPAddress()); + sprintf(buffer, "%d.%d.%d.%d", ip_addr.ip_1, ip_addr.ip_2, ip_addr.ip_3, ip_addr.ip_4); + + Endpoint echo_server; + rc = echo_server.set_address(buffer, port); + CHECK(rc, "set_address"); + + for (int i =0; i < MAX_ECHO_LOOPS; i++) { + std::generate(out_buffer, out_buffer + BUFFER_SIZE, char_rand); + rc = socket.sendTo(echo_server, out_buffer, sizeof(out_buffer)); + CHECK(rc, "sendTo"); + + const int n = socket.receiveFrom(echo_server, buffer, sizeof(buffer)); + CHECK(n, "receiveFrom"); + + if (memcmp(buffer, out_buffer, BUFFER_SIZE) != 0) { + result = false; + break; + } + } + + if (notify_completion_str(result, buffer)) { + socket.sendTo(echo_server, buffer, strlen(buffer)); + } + + socket.close(); + eth.disconnect(); + MBED_HOSTTEST_RESULT(result); +} diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_link_layer/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_link_layer/main.cpp new file mode 100644 index 0000000000..3e40499dfa --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_link_layer/main.cpp @@ -0,0 +1,151 @@ +#include "mbed.h" +#include "rtos.h" +#include "EthernetInterface.h" +#include <list> +#include <string> + +/** +* How to use: +* make.py -m LPC1768 -t ARM -d E:\ -n NET_14 +* udp_link_layer_auto.py -p COM20 -d E:\ -t 10 +*/ + +// Evil globals +namespace { + // IP and port used to store HOST address info + char host_address[32] = {0}; + volatile int host_port = 0; + + const int ECHO_SERVER_PORT = 7; + const int BUFFER_SIZE = 64; + + // Forwarding packet queue + std::list<std::string> datagram_queue; + + // Statistics (mbed) + volatile int received_packets = 0; + volatile int forwarded_packets = 0; + volatile int max_queue_len = 0; + + Mutex cli_serv_mutex; + // cli_serv_mutex.lock(); // LOCK + // cli_serv_mutex.unlock(); // LOCK +} + +void udp_server_task(void const *argument) +{ + DigitalOut indicator(LED1); + UDPSocket server; + + server.bind(ECHO_SERVER_PORT); + // printf("[udp_server_task] Start\r\n"); + + Endpoint client; + char buffer[BUFFER_SIZE] = { 0 }; + while (true) { + //printf("[udp_server_task] Wait for packet...\r\n"); + int n = server.receiveFrom(client, buffer, sizeof(buffer)); + if (n > 0) { + //printf("[udp_server_task] Received packet from: %s\r\n", client.get_address()); + const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; + buffer[buffer_string_end_index] = '\0'; + //printf("[udp_server_task] Server received: %s\r\n", buffer); + if (host_port == 0) { + strcpy(host_address, client.get_address()); + host_port = ECHO_SERVER_PORT + 1; + //printf("[udp_server_task] Set host address and port: %s:%d\r\n", host_address, host_port); + } + // Dispatch data to client for sending to test HOST + cli_serv_mutex.lock(); // LOCK + // Push to datagram queue + datagram_queue.push_front(std::string(buffer)); + max_queue_len = datagram_queue.size() > max_queue_len ? datagram_queue.size() : max_queue_len; + received_packets++; + cli_serv_mutex.unlock(); // LOCK + indicator = !indicator; + } + } +} + +void udp_client_task(void const *argument) +{ + while (host_port == 0) { + // Waiting for HOST port notification + } + + DigitalOut indicator(LED2); + UDPSocket socket; + socket.init(); + + Endpoint echo_server; + echo_server.set_address(host_address, host_port); + //printf("[udp_client_task] Start: %s:%d\r\n", host_address, host_port); + + while (1) { + std::string datagram; + bool sent_datagram = false; + cli_serv_mutex.lock(); // LOCK + if (datagram_queue.size() > 0) { + // POP from datagram queue + datagram = datagram_queue.back(); + datagram_queue.pop_back(); + sent_datagram = true; + } + cli_serv_mutex.unlock(); // LOCK + if (sent_datagram) { + //printf("[udp_client_task] Forwarded datagram: %s\r\n", datagram.c_str()); + socket.sendTo(echo_server, (char *)datagram.c_str(), datagram.length()); + forwarded_packets++; + indicator = !indicator; + } + } +} + +int main(void) +{ + EthernetInterface eth; + + eth.init(); //Use DHCP + eth.connect(); + printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); + + Thread UdpServerTask(udp_server_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); + Thread UdpClientTask(udp_client_task, NULL, osPriorityNormal, DEFAULT_STACK_SIZE * 2.25); + + // Control TCP server to get MBED statistics + { + char buffer[BUFFER_SIZE] = {0}; + const int TELNET_SERVER_PORT = 23; + const int BUFFER_SIZE = 256; + TCPSocketServer server; + server.bind(TELNET_SERVER_PORT); + server.listen(); + + while (true) { + printf("MBED: Wait for new connection...\r\n"); + TCPSocketConnection client; + server.accept(client); + client.set_blocking(false, 1500); // Timeout after (1.5)s + printf("MBED: Connection from: %s\r\n", client.get_address()); + + while (true) { + int n = client.receive(buffer, sizeof(buffer)); + //if (n <= 0) break; + if (n > 0) { + // printf("Recv %d chars\r\n", n); + const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE - 1 : n; + buffer[buffer_string_end_index] = '\0'; + // client.send_all(buffer, strlen(buffer)); + if (strncmp(buffer, "stat", 4) == 0) { + sprintf(buffer, "received_packets %d\nforwarded_packets %d\nmax_queue_len %d", + received_packets, forwarded_packets, max_queue_len); + client.send_all(buffer, strlen(buffer)); + // printf("%s", buffer); + } + } + //if (n <= 0) break; + } + client.close(); + } + } +} diff --git a/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_server/main.cpp b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_server/main.cpp new file mode 100644 index 0000000000..ea90ff7d4a --- /dev/null +++ b/tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/echo/udp_server/main.cpp @@ -0,0 +1,37 @@ +#include "mbed.h" +#include "test_env.h" +#include "EthernetInterface.h" + +namespace { + const int ECHO_SERVER_PORT = 7; + const int BUFFER_SIZE = 64; +} + +int main (void) { + MBED_HOSTTEST_TIMEOUT(20); + MBED_HOSTTEST_SELECT(udpecho_server_auto); + MBED_HOSTTEST_DESCRIPTION(UDP echo server); + MBED_HOSTTEST_START("NET_5"); + + EthernetInterface eth; + eth.init(); //Use DHCP + eth.connect(); + printf("MBED: Server IP Address is %s:%d\r\n", eth.getIPAddress(), ECHO_SERVER_PORT); + + UDPSocket server; + server.bind(ECHO_SERVER_PORT); + + Endpoint client; + char buffer[BUFFER_SIZE] = {0}; + printf("MBED: Waiting for packet...\r\n"); + while (true) { + int n = server.receiveFrom(client, buffer, sizeof(buffer)); + if (n > 0) { + //printf("Received packet from: %s\n", client.get_address()); + const int buffer_string_end_index = n >= BUFFER_SIZE ? BUFFER_SIZE-1 : n; + buffer[buffer_string_end_index] = '\0'; + //printf("Server received: %s\n", buffer); + server.sendTo(client, buffer, n); + } + } +} |