Rtos API example

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 
00033 namespace {
00034     // Test connection information
00035     const char *HTTP_SERVER_NAME = "os.mbed.com";
00036     const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
00037     const int HTTP_SERVER_PORT = 80;
00038 #if defined(TARGET_VK_RZ_A1H)
00039     const int RECV_BUFFER_SIZE = 300;
00040 #else
00041     const int RECV_BUFFER_SIZE = 512;
00042 #endif
00043     // Test related data
00044     const char *HTTP_OK_STR = "200 OK";
00045     const char *HTTP_HELLO_STR = "Hello world!";
00046 
00047     // Test buffers
00048     char buffer[RECV_BUFFER_SIZE] = {0};
00049 }
00050 
00051 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
00052     const char *f = std::search(first, last, s_first, s_last);
00053     return (f != last);
00054 }
00055 
00056 void test_tcp_hello_world() {
00057     bool result = false;
00058     NetworkInterface* net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00059     MBED_CONF_APP_CONNECT_STATEMENT;
00060     printf("TCP client IP Address is %s\r\n", net->get_ip_address());
00061 
00062     TCPSocket sock(net);
00063     printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00064     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00065         printf("HTTP: OK\r\n");
00066 
00067         // We are constructing GET command like this:
00068         // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
00069         strcpy(buffer, "GET http://");
00070         strcat(buffer, HTTP_SERVER_NAME);
00071         strcat(buffer, HTTP_SERVER_FILE_PATH);
00072         strcat(buffer, " HTTP/1.0\n\n");
00073         // Send GET command
00074         sock.send(buffer, strlen(buffer));
00075 
00076         // Server will respond with HTTP GET's success code
00077         const int ret = sock.recv(buffer, sizeof(buffer) - 1);
00078         buffer[ret] = '\0';
00079 
00080         // Find 200 OK HTTP status in reply
00081         bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
00082         // Find "Hello World!" string in reply
00083         bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
00084 
00085         TEST_ASSERT_TRUE(found_200_ok);
00086         TEST_ASSERT_TRUE(found_hello);
00087 
00088         if (found_200_ok && found_hello) result = true;
00089 
00090         printf("HTTP: Received %d chars from server\r\n", ret);
00091         printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
00092         printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
00093         printf("HTTP: Received message:\r\n");
00094         printf("%s", buffer);
00095         sock.close();
00096     } else {
00097         printf("HTTP: ERROR\r\n");
00098     }
00099 
00100     net->disconnect();
00101     TEST_ASSERT_EQUAL(true, result);
00102 }
00103 
00104 
00105 // Test setup
00106 utest::v1::status_t test_setup(const size_t number_of_cases) {
00107     GREENTEA_SETUP(120, "default_auto");
00108     return verbose_test_setup_handler(number_of_cases);
00109 }
00110 
00111 Case cases[] = {
00112     Case("TCP hello world", test_tcp_hello_world),
00113 };
00114 
00115 Specification specification(test_setup, cases);
00116 
00117 int main() {
00118     return !Harness::run(specification);
00119 }