
NuMaker Ethernet TCP
main.cpp@14:7a9e90b34298, 2018-10-16 (annotated)
- Committer:
- ccli8
- Date:
- Tue Oct 16 14:09:51 2018 +0800
- Revision:
- 14:7a9e90b34298
- Parent:
- 10:717012361a47
- Child:
- 16:5df36a9f0f60
Substitute MBED_HEAP_STATS_ENABLED for __sbrk for heap statistics
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cyliang | 3:861e6f618bd3 | 1 | //#if !FEATURE_IPV4 |
cyliang | 3:861e6f618bd3 | 2 | // #error [NOT_SUPPORTED] IPV4 not supported for this target |
cyliang | 3:861e6f618bd3 | 3 | //#endif |
cyliang | 0:45a173aa6aa3 | 4 | |
cyliang | 0:45a173aa6aa3 | 5 | #include <algorithm> |
cyliang | 0:45a173aa6aa3 | 6 | #include "mbed.h" |
cyliang | 0:45a173aa6aa3 | 7 | #include "EthernetInterface.h" |
cyliang | 0:45a173aa6aa3 | 8 | #include "TCPSocket.h" |
cyliang | 0:45a173aa6aa3 | 9 | #include "greentea-client/test_env.h" |
cyliang | 0:45a173aa6aa3 | 10 | #include "unity/unity.h" |
cyliang | 0:45a173aa6aa3 | 11 | |
cyliang | 1:621d4620acd2 | 12 | //#define LOCAL_LAN |
cyliang | 0:45a173aa6aa3 | 13 | |
cyliang | 0:45a173aa6aa3 | 14 | namespace { |
cyliang | 0:45a173aa6aa3 | 15 | // Test connection information |
cyliang | 2:291ff97f421e | 16 | #ifndef LOCAL_LAN |
ccli8 |
9:7bea0a488b3f | 17 | const char *HTTP_SERVER_NAME = "os.mbed.com"; |
cyliang | 0:45a173aa6aa3 | 18 | #else |
cyliang | 2:291ff97f421e | 19 | const char *HTTP_SERVER_NAME = "pt22_winserver2.nuvoton.com"; |
cyliang | 0:45a173aa6aa3 | 20 | #endif |
cyliang | 2:291ff97f421e | 21 | |
cyliang | 2:291ff97f421e | 22 | #ifndef LOCAL_LAN |
cyliang | 0:45a173aa6aa3 | 23 | const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt"; |
cyliang | 0:45a173aa6aa3 | 24 | const int HTTP_SERVER_PORT = 80; |
cyliang | 2:291ff97f421e | 25 | #else |
cyliang | 0:45a173aa6aa3 | 26 | const char *HTTP_SERVER_FILE_PATH = "/examples/arm_mbed/hello.txt"; |
cyliang | 0:45a173aa6aa3 | 27 | const int HTTP_SERVER_PORT = 8080; |
cyliang | 2:291ff97f421e | 28 | #endif |
cyliang | 0:45a173aa6aa3 | 29 | |
cyliang | 0:45a173aa6aa3 | 30 | #if defined(TARGET_VK_RZ_A1H) |
cyliang | 0:45a173aa6aa3 | 31 | const int RECV_BUFFER_SIZE = 300; |
cyliang | 0:45a173aa6aa3 | 32 | #else |
cyliang | 0:45a173aa6aa3 | 33 | const int RECV_BUFFER_SIZE = 512; |
cyliang | 0:45a173aa6aa3 | 34 | #endif |
cyliang | 0:45a173aa6aa3 | 35 | |
cyliang | 0:45a173aa6aa3 | 36 | // Test related data |
cyliang | 0:45a173aa6aa3 | 37 | const char *HTTP_OK_STR = "200 OK"; |
cyliang | 0:45a173aa6aa3 | 38 | const char *HTTP_HELLO_STR = "Hello world!"; |
cyliang | 0:45a173aa6aa3 | 39 | |
cyliang | 0:45a173aa6aa3 | 40 | // Test buffers |
cyliang | 0:45a173aa6aa3 | 41 | char buffer[RECV_BUFFER_SIZE] = {0}; |
cyliang | 0:45a173aa6aa3 | 42 | } |
cyliang | 0:45a173aa6aa3 | 43 | |
cyliang | 0:45a173aa6aa3 | 44 | bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { |
cyliang | 0:45a173aa6aa3 | 45 | const char *f = std::search(first, last, s_first, s_last); |
cyliang | 0:45a173aa6aa3 | 46 | return (f != last); |
cyliang | 0:45a173aa6aa3 | 47 | } |
cyliang | 0:45a173aa6aa3 | 48 | |
cyliang | 0:45a173aa6aa3 | 49 | int main() { |
ccli8 |
14:7a9e90b34298 | 50 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
14:7a9e90b34298 | 51 | mbed_stats_heap_t heap_stats; |
ccli8 |
14:7a9e90b34298 | 52 | #endif |
ccli8 |
14:7a9e90b34298 | 53 | |
cyliang | 2:291ff97f421e | 54 | printf(" Start TCP test \r\n"); |
cyliang | 0:45a173aa6aa3 | 55 | // GREENTEA_SETUP(20, "default_auto"); |
cyliang | 2:291ff97f421e | 56 | |
cyliang | 0:45a173aa6aa3 | 57 | bool result = true; |
cyliang | 2:291ff97f421e | 58 | |
cyliang | 0:45a173aa6aa3 | 59 | EthernetInterface eth; |
cyliang | 2:291ff97f421e | 60 | |
cyliang | 2:291ff97f421e | 61 | eth.connect(); //Use DHCP |
cyliang | 0:45a173aa6aa3 | 62 | |
cyliang | 0:45a173aa6aa3 | 63 | printf("TCP client IP Address is %s\r\n", eth.get_ip_address()); |
cyliang | 0:45a173aa6aa3 | 64 | |
cyliang | 0:45a173aa6aa3 | 65 | TCPSocket sock(ð); |
cyliang | 2:291ff97f421e | 66 | |
cyliang | 0:45a173aa6aa3 | 67 | if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) { |
cyliang | 0:45a173aa6aa3 | 68 | printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT); |
cyliang | 0:45a173aa6aa3 | 69 | |
cyliang | 0:45a173aa6aa3 | 70 | // We are constructing GET command like this: |
cyliang | 0:45a173aa6aa3 | 71 | // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n |
cyliang | 0:45a173aa6aa3 | 72 | strcpy(buffer, "GET http://"); |
cyliang | 0:45a173aa6aa3 | 73 | strcat(buffer, HTTP_SERVER_NAME); |
cyliang | 0:45a173aa6aa3 | 74 | strcat(buffer, HTTP_SERVER_FILE_PATH); |
cyliang | 0:45a173aa6aa3 | 75 | strcat(buffer, " HTTP/1.0\n\n"); |
cyliang | 0:45a173aa6aa3 | 76 | // Send GET command |
cyliang | 0:45a173aa6aa3 | 77 | sock.send(buffer, strlen(buffer)); |
cyliang | 0:45a173aa6aa3 | 78 | |
cyliang | 0:45a173aa6aa3 | 79 | // Server will respond with HTTP GET's success code |
cyliang | 0:45a173aa6aa3 | 80 | const int ret = sock.recv(buffer, sizeof(buffer) - 1); |
cyliang | 0:45a173aa6aa3 | 81 | buffer[ret] = '\0'; |
cyliang | 0:45a173aa6aa3 | 82 | |
cyliang | 0:45a173aa6aa3 | 83 | // Find 200 OK HTTP status in reply |
cyliang | 0:45a173aa6aa3 | 84 | bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); |
cyliang | 0:45a173aa6aa3 | 85 | // Find "Hello World!" string in reply |
cyliang | 0:45a173aa6aa3 | 86 | bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); |
cyliang | 0:45a173aa6aa3 | 87 | |
cyliang | 0:45a173aa6aa3 | 88 | if (!found_200_ok) result = false; |
cyliang | 0:45a173aa6aa3 | 89 | if (!found_hello) result = false; |
cyliang | 0:45a173aa6aa3 | 90 | |
cyliang | 0:45a173aa6aa3 | 91 | printf("HTTP: Received %d chars from server\r\n", ret); |
cyliang | 0:45a173aa6aa3 | 92 | printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); |
cyliang | 0:45a173aa6aa3 | 93 | printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); |
cyliang | 0:45a173aa6aa3 | 94 | printf("HTTP: Received massage:\r\n\r\n"); |
cyliang | 0:45a173aa6aa3 | 95 | printf("%s", buffer); |
cyliang | 0:45a173aa6aa3 | 96 | } |
ccli8 |
14:7a9e90b34298 | 97 | |
cyliang | 0:45a173aa6aa3 | 98 | sock.close(); |
cyliang | 0:45a173aa6aa3 | 99 | eth.disconnect(); |
cyliang | 0:45a173aa6aa3 | 100 | // GREENTEA_TESTSUITE_RESULT(result); |
ccli8 |
14:7a9e90b34298 | 101 | |
ccli8 |
14:7a9e90b34298 | 102 | #if MBED_HEAP_STATS_ENABLED |
ccli8 |
14:7a9e90b34298 | 103 | mbed_stats_heap_get(&heap_stats); |
ccli8 |
14:7a9e90b34298 | 104 | printf("Current heap: %lu\r\n", heap_stats.current_size); |
ccli8 |
14:7a9e90b34298 | 105 | printf("Max heap size: %lu\r\n", heap_stats.max_size); |
ccli8 |
14:7a9e90b34298 | 106 | #endif |
cyliang | 0:45a173aa6aa3 | 107 | } |