]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/tests/net/helloworld/tcpclient/main.cpp
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / tests / net / helloworld / tcpclient / main.cpp
1 #include <algorithm>
2 #include "mbed.h"
3 #include "EthernetInterface.h"
4 #include "test_env.h"
5
6 namespace {
7     // Test connection information
8     const char *HTTP_SERVER_NAME = "developer.mbed.org";
9     const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
10     const int HTTP_SERVER_PORT = 80;
11     const int RECV_BUFFER_SIZE = 512;
12
13     // Test related data
14     const char *HTTP_OK_STR = "200 OK";
15     const char *HTTP_HELLO_STR = "Hello world!";
16
17     // Test buffers
18     char buffer[RECV_BUFFER_SIZE] = {0};
19 }
20
21 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
22     const char *f = std::search(first, last, s_first, s_last);
23     return (f != last);
24 }
25
26 int main() {
27     MBED_HOSTTEST_TIMEOUT(20);
28     MBED_HOSTTEST_SELECT(default_auto);
29     MBED_HOSTTEST_DESCRIPTION(TCP client hello world);
30     MBED_HOSTTEST_START("NET_1");
31
32     bool result = false;
33     EthernetInterface eth;
34     eth.init(); //Use DHCP
35     eth.connect();
36     printf("TCP client IP Address is %s\r\n", eth.getIPAddress());
37
38     TCPSocketConnection sock;
39     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
40         printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
41
42         // We are constructing GET command like this:
43         // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
44         strcpy(buffer, "GET http://");
45         strcat(buffer, HTTP_SERVER_NAME);
46         strcat(buffer, HTTP_SERVER_FILE_PATH);
47         strcat(buffer, " HTTP/1.0\n\n");
48         // Send GET command
49         sock.send_all(buffer, strlen(buffer));
50
51         // Server will respond with HTTP GET's success code
52         bool found_200_ok = false;
53         {
54             const int ret = sock.receive(buffer, sizeof(buffer) - 1);
55             buffer[ret] = '\0';
56             // Find 200 OK HTTP status in reply
57             found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
58             printf("HTTP: Received %d chars from server\r\n", ret);
59             printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
60             printf("HTTP: Received massage:\r\n\r\n");
61             printf("%s", buffer);
62         }
63
64         // Server will respond with requested file content
65         bool found_hello = false;
66         {
67             const int ret = sock.receive(buffer, sizeof(buffer) - 1);
68             buffer[ret] = '\0';
69             // Find Hello World! in reply
70             found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
71             printf("HTTP: Received %d chars from server\r\n", ret);
72             printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
73             printf("HTTP: Received massage:\r\n\r\n");
74             printf("%s", buffer);
75         }
76
77         if (found_200_ok && found_hello) {
78             result = true;
79         }
80     }
81
82     sock.close();
83     eth.disconnect();
84     MBED_HOSTTEST_RESULT(result);
85 }