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     Semaphore recvd;
00051     NetworkInterface *net;
00052 }
00053 
00054 void net_bringup() {
00055     net = MBED_CONF_APP_OBJECT_CONSTRUCTION;
00056     int err =  MBED_CONF_APP_CONNECT_STATEMENT;
00057     TEST_ASSERT_EQUAL(0, err);
00058 }
00059 
00060 bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
00061     const char *f = std::search(first, last, s_first, s_last);
00062     return (f != last);
00063 }
00064 
00065 void get_data(TCPSocket* sock){
00066     bool result = false;
00067     // Server will respond with HTTP GET's success code
00068     const int ret = sock->recv(buffer, sizeof(buffer) - 1);
00069     if(ret <= 0)
00070         return;
00071 
00072     buffer[ret] = '\0';
00073 
00074     // Find 200 OK HTTP status in reply
00075     bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
00076     // Find "Hello World!" string in reply
00077     bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
00078 
00079     TEST_ASSERT_TRUE(found_200_ok);
00080     TEST_ASSERT_TRUE(found_hello);
00081 
00082     if (found_200_ok && found_hello) result = true;
00083 
00084     TEST_ASSERT_EQUAL(result, true);
00085 
00086     printf("HTTP: Received %d chars from server\r\n", ret);
00087     printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
00088     printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
00089     printf("HTTP: Received message:\r\n");
00090     printf("%s", buffer);
00091     // Signal that we have recvd
00092     recvd.release();
00093 }
00094 
00095 void prep_buffer() {
00096     memset(buffer, 0, sizeof(buffer));
00097     // We are constructing GET command like this:
00098     // GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
00099     strcpy(buffer, "GET http://");
00100     strcat(buffer, HTTP_SERVER_NAME);
00101     strcat(buffer, HTTP_SERVER_FILE_PATH);
00102     strcat(buffer, " HTTP/1.0\n\n");
00103 }
00104 
00105 void test_socket_attach() {
00106     // Dispatch event queue
00107     Thread eventThread;
00108     EventQueue queue(4*EVENTS_EVENT_SIZE);
00109     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00110 
00111     printf("TCP client IP Address is %s\r\n", net->get_ip_address());
00112 
00113     TCPSocket sock(net);
00114     printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00115     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00116         printf("HTTP: OK\r\n");
00117 
00118         prep_buffer();
00119         // Attach a sigio function that adds function to event queue
00120         sock.sigio(queue.event(get_data, &sock));
00121         // Send GET command
00122         sock.send(buffer, strlen(buffer));
00123         // wait for recv data
00124         recvd.wait();
00125     } else {
00126         printf("HTTP: ERROR\r\n");
00127     }
00128     sock.close();
00129 }
00130 
00131 void cb_fail() {
00132     TEST_ASSERT(false);
00133 }
00134 
00135 void cb_pass() {
00136     recvd.release();
00137 }
00138 
00139 void test_socket_detach() {
00140     // Dispatch event queue
00141     Thread eventThread;
00142     EventQueue queue(4*EVENTS_EVENT_SIZE);
00143     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00144 
00145     printf("TCP client IP Address is %s\r\n", net->get_ip_address());
00146 
00147     TCPSocket sock(net);
00148     printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00149     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00150         printf("HTTP: OK\r\n");
00151 
00152         prep_buffer();
00153         // Attach a sigio function that adds function to event queue
00154         sock.sigio(queue.event(cb_fail));
00155         // Detach function
00156         sock.sigio(NULL);
00157         // Send GET command
00158         sock.send(buffer, strlen(buffer));
00159         wait(5);
00160     } else {
00161         printf("HTTP: ERROR\r\n");
00162     }
00163     sock.close();
00164 }
00165 
00166 void test_socket_reattach() {
00167     // Dispatch event queue
00168     Thread eventThread;
00169     EventQueue queue(4*EVENTS_EVENT_SIZE);
00170     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00171 
00172     printf("TCP client IP Address is %s\r\n", net->get_ip_address());
00173 
00174     TCPSocket sock(net);
00175     printf("HTTP: Connection to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
00176     if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
00177         printf("HTTP: OK\r\n");
00178 
00179         prep_buffer();
00180         // Attach a sigio function that adds function to event queue
00181         sock.sigio(queue.event(cb_fail));
00182         // Override previous attach
00183         sock.sigio(queue.event(cb_pass));
00184         // Send GET command
00185         sock.send(buffer, strlen(buffer));
00186         recvd.wait();
00187         TEST_ASSERT(true);
00188     } else {
00189         printf("HTTP: ERROR\r\n");
00190     }
00191     sock.close();
00192 }
00193 
00194 
00195 // Test setup
00196 utest::v1::status_t test_setup(const size_t number_of_cases) {
00197     GREENTEA_SETUP(120, "default_auto");
00198     net_bringup();
00199     return verbose_test_setup_handler(number_of_cases);
00200 }
00201 
00202 Case cases[] = {
00203     Case("Socket Attach Test", test_socket_attach),
00204     Case("Socket Detach Test", test_socket_detach),
00205     Case("Socket Reattach Test", test_socket_reattach),
00206 };
00207 
00208 Specification specification(test_setup, cases);
00209 
00210 int main() {
00211     return !Harness::run(specification);
00212 }