From 88ee8caaddccc2b9f33b7e23de09d67c82bca246 Mon Sep 17 00:00:00 2001 From: Przemek Wirkus Date: Tue, 7 Jun 2016 14:03:14 +0100 Subject: [PATCH] Add TCP Client Download --- .../tcp_client_hello_world/main.cpp | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 TESTS/mbedmicro-net/tcp_client_hello_world/main.cpp diff --git a/TESTS/mbedmicro-net/tcp_client_hello_world/main.cpp b/TESTS/mbedmicro-net/tcp_client_hello_world/main.cpp new file mode 100644 index 0000000000..3f094a90e6 --- /dev/null +++ b/TESTS/mbedmicro-net/tcp_client_hello_world/main.cpp @@ -0,0 +1,78 @@ +#include +#include "mbed.h" +#include "LWIPInterface.h" +#include "TCPSocket.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" + +namespace { + // Test connection information + const char *HTTP_SERVER_NAME = "developer.mbed.org"; + const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt"; + const int HTTP_SERVER_PORT = 80; +#if defined(TARGET_VK_RZ_A1H) + const int RECV_BUFFER_SIZE = 300; +#else + const int RECV_BUFFER_SIZE = 512; +#endif + // Test related data + const char *HTTP_OK_STR = "200 OK"; + const char *HTTP_HELLO_STR = "Hello world!"; + + // Test buffers + char buffer[RECV_BUFFER_SIZE] = {0}; +} + +bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { + const char *f = std::search(first, last, s_first, s_last); + return (f != last); +} + +int main() { + GREENTEA_SETUP(20, "default_auto"); + + bool result = true; + LWIPInterface eth; + //eth.init(); //Use DHCP + eth.connect(); + printf("TCP client IP Address is %s\r\n", eth.get_ip_address()); + + TCPSocket sock(ð); + if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { + printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); + + // We are constructing GET command like this: + // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n + strcpy(buffer, "GET http://"); + strcat(buffer, HTTP_SERVER_NAME); + strcat(buffer, HTTP_SERVER_FILE_PATH); + strcat(buffer, " HTTP/1.0\n\n"); + // Send GET command + sock.send(buffer, strlen(buffer)); + + // Server will respond with HTTP GET's success code + const int ret = sock.recv(buffer, sizeof(buffer) - 1); + buffer[ret] = '\0'; + + // Find 200 OK HTTP status in reply + bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); + // Find "Hello World!" string in reply + bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); + + TEST_ASSERT_TRUE(found_200_ok); + TEST_ASSERT_TRUE(found_hello); + + if (!found_200_ok) result = false; + if (!found_hello) result = false; + + printf("HTTP: Received %d chars from server\r\n", ret); + printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); + printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); + printf("HTTP: Received massage:\r\n\r\n"); + printf("%s", buffer); + } + + sock.close(); + eth.disconnect(); + GREENTEA_TESTSUITE_RESULT(result); +}