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