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.
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2016 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "utest/utest.h" 00018 #include "unity/unity.h" 00019 #include "greentea-client/test_env.h" 00020 00021 #include "mbed.h" 00022 00023 #if TARGET_UBLOX_EVK_ODIN_W2 00024 #include "OdinWiFiInterface.h" 00025 #else 00026 #error [NOT_SUPPORTED] Only built in WiFi modules are supported at this time. 00027 #endif 00028 00029 using namespace utest::v1; 00030 00031 /** 00032 * WiFi tests require following macros to be defined: 00033 * - MBED_CONF_APP_WIFI_SSID - SSID of a network the test will try connecting to 00034 * - MBED_CONF_APP_WIFI_PASSWORD - Passphrase that will be used to connecting to the network 00035 * - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3" 00036 */ 00037 #if !defined(MBED_CONF_APP_WIFI_SSID) || !defined(MBED_CONF_APP_WIFI_PASSWORD) || !defined(MBED_CONF_APP_WIFI_NETWORKS) 00038 #error [NOT_SUPPORTED] MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD and MBED_CONF_APP_WIFI_NETWORKS have to be defined for this test. 00039 #endif 00040 00041 const char *networks[] = {MBED_CONF_APP_WIFI_NETWORKS, NULL}; 00042 00043 WiFiInterface *wifi; 00044 00045 /* In normal circumstances the WiFi object could be global, but the delay introduced by WiFi initialization is an issue 00046 for the tests. It causes Greentea to timeout on syncing with the board. To solve it we defer the actual object 00047 creation till we actually need it. 00048 */ 00049 WiFiInterface *get_wifi() 00050 { 00051 if (wifi == NULL) { 00052 /* We don't really care about freeing this, as its lifetime is through the full test suit run. */ 00053 #if TARGET_UBLOX_EVK_ODIN_W2 00054 wifi = new OdinWiFiInterface; 00055 #endif 00056 } 00057 00058 return wifi; 00059 } 00060 00061 void check_wifi(const char *ssid, bool *net_stat) 00062 { 00063 int i = 0; 00064 while(networks[i]) { 00065 if (strcmp(networks[i], ssid) == 0) { 00066 net_stat[i] = true; 00067 break; 00068 } 00069 i++; 00070 } 00071 } 00072 00073 void wifi_scan() 00074 { 00075 int count; 00076 WiFiAccessPoint *aps; 00077 const int net_len = sizeof(networks)/sizeof(networks[0]); 00078 bool net_stat[net_len - 1]; 00079 00080 memset(net_stat, 0, sizeof(net_stat)); 00081 00082 count = get_wifi()->scan(NULL, 0); 00083 TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error"); 00084 TEST_ASSERT_MESSAGE(count > 0, "Scan result empty"); 00085 00086 aps = new WiFiAccessPoint[count]; 00087 count = get_wifi()->scan(aps, count); 00088 for(int i = 0; i < count; i++) { 00089 check_wifi(aps[i].get_ssid(), net_stat); 00090 } 00091 00092 delete[] aps; 00093 00094 for (unsigned i = 0; i < sizeof(net_stat); i++) { 00095 TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected"); 00096 } 00097 } 00098 00099 void wifi_connect() 00100 { 00101 int ret; 00102 00103 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2 ); 00104 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); 00105 00106 ret = get_wifi()->disconnect(); 00107 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); 00108 } 00109 00110 void wifi_connect_scan() 00111 { 00112 int ret; 00113 int count; 00114 WiFiAccessPoint *aps; 00115 const int net_len = sizeof(networks)/sizeof(networks[0]); 00116 bool net_stat[net_len - 1]; 00117 00118 memset(net_stat, 0, sizeof(net_stat)); 00119 00120 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2 ); 00121 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); 00122 00123 count = get_wifi()->scan(NULL, 0); 00124 TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error"); 00125 TEST_ASSERT_MESSAGE(count > 0, "Scan result empty"); 00126 00127 aps = new WiFiAccessPoint[count]; 00128 count = get_wifi()->scan(aps, count); 00129 for(int i = 0; i < count; i++) { 00130 check_wifi(aps[i].get_ssid(), net_stat); 00131 } 00132 00133 delete[] aps; 00134 00135 ret = get_wifi()->disconnect(); 00136 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); 00137 00138 for (unsigned i = 0; i < sizeof(net_stat); i++) { 00139 TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected"); 00140 } 00141 } 00142 00143 void wifi_http() 00144 { 00145 TCPSocket socket; 00146 int ret; 00147 00148 ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2 ); 00149 TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); 00150 00151 // Open a socket on the network interface, and create a TCP connection to www.arm.com 00152 ret = socket.open(get_wifi()); 00153 TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed"); 00154 ret = socket.connect("www.arm.com", 80); 00155 TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed"); 00156 00157 // Send a simple http request 00158 char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; 00159 int scount = socket.send(sbuffer, sizeof sbuffer); 00160 TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed"); 00161 00162 // Recieve a simple http response and check if it's not empty 00163 char rbuffer[64]; 00164 int rcount = socket.recv(rbuffer, sizeof rbuffer); 00165 TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error"); 00166 TEST_ASSERT_MESSAGE(rcount > 0, "No data received"); 00167 00168 ret = socket.close(); 00169 TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed"); 00170 00171 ret = get_wifi()->disconnect(); 00172 TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); 00173 } 00174 00175 status_t greentea_failure_handler(const Case *const source, const failure_t reason) { 00176 greentea_case_failure_abort_handler(source, reason); 00177 return STATUS_CONTINUE; 00178 } 00179 00180 Case cases[] = { 00181 Case("Scan test", wifi_scan, greentea_failure_handler), 00182 Case("Connect test", wifi_connect, greentea_failure_handler), 00183 Case("Scan while connected test", wifi_connect_scan, greentea_failure_handler), 00184 Case("HTTP test", wifi_http, greentea_failure_handler), 00185 }; 00186 00187 status_t greentea_test_setup(const size_t number_of_cases) { 00188 GREENTEA_SETUP(90, "default_auto"); 00189 return greentea_test_setup_handler(number_of_cases); 00190 } 00191 00192 00193 int main() { 00194 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00195 Harness::run(specification); 00196 }
Generated on Tue Jul 12 2022 11:02:46 by
