dhgdh

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by joey shelton

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include <algorithm>
00002 #include "mbed.h"
00003 #include "EthernetInterface.h"
00004 #include "test_env.h"
00005 
00006 namespace {
00007     // Test connection information
00008     const char *HTTP_SERVER_NAME = "developer.mbed.org";
00009     const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
00010     const int HTTP_SERVER_PORT = 80;
00011 #if defined(TARGET_VK_RZ_A1H)
00012     const int RECV_BUFFER_SIZE = 300;
00013 #else
00014     const int RECV_BUFFER_SIZE = 512;
00015 #endif
00016     // Test related data
00017     const char *HTTP_OK_STR = "200 OK";
00018     const char *HTTP_HELLO_STR = "Hello world!";
00019 
00020     // Test buffers
00021     char buffer[RECV_BUFFER_SIZE] = {0};
00022 }
00023 
00024 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
00025     const char *f = std::search(first, last, s_first, s_last);
00026     return (f != last);
00027 }
00028 
00029 int main() {
00030     MBED_HOSTTEST_TIMEOUT(20);
00031     MBED_HOSTTEST_SELECT(default_auto);
00032     MBED_HOSTTEST_DESCRIPTION(TCP client hello world);
00033     MBED_HOSTTEST_START("NET_1");
00034 
00035     bool result = false;
00036     EthernetInterface eth;
00037     eth.init(); //Use DHCP
00038     eth.connect();
00039     printf("TCP client IP Address is %s\r\n", eth.getIPAddress());
00040 
00041     TCPSocketConnection sock;
00042     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00043         printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00044 
00045         // We are constructing GET command like this:
00046         // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
00047         strcpy(buffer, "GET http://");
00048         strcat(buffer, HTTP_SERVER_NAME);
00049         strcat(buffer, HTTP_SERVER_FILE_PATH);
00050         strcat(buffer, " HTTP/1.0\n\n");
00051         // Send GET command
00052         sock.send_all(buffer, strlen(buffer));
00053 
00054         // Server will respond with HTTP GET's success code
00055         bool found_200_ok = false;
00056         {
00057             const int ret = sock.receive(buffer, sizeof(buffer) - 1);
00058             buffer[ret] = '\0';
00059             // Find 200 OK HTTP status in reply
00060             found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
00061             printf("HTTP: Received %d chars from server\r\n", ret);
00062             printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
00063             printf("HTTP: Received massage:\r\n\r\n");
00064             printf("%s", buffer);
00065         }
00066 
00067         // Server will respond with requested file content
00068         bool found_hello = false;
00069         {
00070             const int ret = sock.receive(buffer, sizeof(buffer) - 1);
00071             buffer[ret] = '\0';
00072             // Find Hello World! in reply
00073             found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
00074             printf("HTTP: Received %d chars from server\r\n", ret);
00075             printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
00076             printf("HTTP: Received massage:\r\n\r\n");
00077             printf("%s", buffer);
00078         }
00079 
00080         if (found_200_ok && found_hello) {
00081             result = true;
00082         }
00083     }
00084 
00085     sock.close();
00086     eth.disconnect();
00087     MBED_HOSTTEST_RESULT(result);
00088 }