init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2013-2017, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018  #ifndef MBED_CONF_APP_CONNECT_STATEMENT
00019      #error [NOT_SUPPORTED] No network configuration found for this target.
00020  #endif
00021 
00022 #include <algorithm>
00023 #include "mbed.h"
00024 #include MBED_CONF_APP_HEADER_FILE
00025 #include "TCPSocket.h"
00026 #include "greentea-client/test_env.h"
00027 #include "unity/unity.h"
00028 #include "utest.h"
00029 
00030 using namespace utest::v1;
00031 
00032 #ifndef MBED_CONF_APP_HTTP_SERVER_NAME
00033 #define MBED_CONF_APP_HTTP_SERVER_NAME "os.mbed.com"
00034 #define MBED_CONF_APP_HTTP_SERVER_FILE_PATH "/media/uploads/mbed_official/hello.txt"
00035 #endif
00036 
00037 namespace {
00038     // Test connection information
00039     const int HTTP_SERVER_PORT = 80;
00040 #if defined(TARGET_VK_RZ_A1H)
00041     const int RECV_BUFFER_SIZE = 300;
00042 #else
00043     const int RECV_BUFFER_SIZE = 512;
00044 #endif
00045     // Test related data
00046     const char *HTTP_OK_STR = "200 OK";
00047     const char *HTTP_HELLO_STR = "Hello world!";
00048 
00049     // Test buffers
00050     char buffer[RECV_BUFFER_SIZE] = {0};
00051 }
00052 
00053 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
00054     const char *f = std::search(first, last, s_first, s_last);
00055     return (f != last);
00056 }
00057 
00058 void test_tcp_hello_world() {
00059     bool result = false;
00060     NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00061     MBED_CONF_APP_CONNECT_STATEMENT;
00062     printf("TCP client IP Address is %s\r\n", net->get_ip_address());
00063 
00064     TCPSocket sock(net);
00065     printf("HTTP: Connection to %s:%d\r\n", MBED_CONF_APP_HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00066     if (sock.connect(MBED_CONF_APP_HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00067         printf("HTTP: OK\r\n");
00068 
00069         // We are constructing GET command like this:
00070         // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
00071         strcpy(buffer, "GET http://");
00072         strcat(buffer, MBED_CONF_APP_HTTP_SERVER_NAME);
00073         strcat(buffer, MBED_CONF_APP_HTTP_SERVER_FILE_PATH);
00074         strcat(buffer, " HTTP/1.0\n\n");
00075         // Send GET command
00076         sock.send(buffer, strlen(buffer));
00077 
00078         // Server will respond with HTTP GET's success code
00079         int ret = 0;
00080         int bytes_recvd = 0;
00081 
00082         do {
00083             ret += bytes_recvd;
00084             bytes_recvd = sock.recv(buffer+ret, sizeof(buffer) - 1 - ret);
00085         }while(bytes_recvd > 0);
00086         buffer[ret] = '\0';
00087         
00088         // Find 200 OK HTTP status in reply
00089         bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
00090         // Find "Hello World!" string in reply
00091         bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
00092 
00093         TEST_ASSERT_TRUE(found_200_ok);
00094         TEST_ASSERT_TRUE(found_hello);
00095 
00096         if (found_200_ok && found_hello) result = true;
00097 
00098         printf("HTTP: Received %d chars from server\r\n", ret);
00099         printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
00100         printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
00101         printf("HTTP: Received message:\r\n");
00102         printf("%s", buffer);
00103         sock.close();
00104     } else {
00105         printf("HTTP: ERROR\r\n");
00106     }
00107 
00108     net->disconnect();
00109     TEST_ASSERT_EQUAL(true, result);
00110 }
00111 
00112 
00113 // Test setup
00114 utest::v1::status_t test_setup(const size_t number_of_cases) {
00115     GREENTEA_SETUP(120, "default_auto");
00116     return verbose_test_setup_handler(number_of_cases);
00117 }
00118 
00119 Case cases[] = {
00120     Case("TCP hello world", test_tcp_hello_world),
00121 };
00122 
00123 Specification specification(test_setup, cases);
00124 
00125 int main() {
00126     return !Harness::run(specification);
00127 }