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