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