|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2016 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "utest/utest.h" |
| 18 | +#include "unity/unity.h" |
| 19 | +#include "greentea-client/test_env.h" |
| 20 | + |
| 21 | +#include "mbed.h" |
| 22 | + |
| 23 | +#if TARGET_UBLOX_EVK_ODIN_W2 |
| 24 | +#include "OdinWiFiInterface.h" |
| 25 | +#else |
| 26 | +#error [NOT_SUPPORTED] Only built in WiFi modules are supported at this time. |
| 27 | +#endif |
| 28 | + |
| 29 | +using namespace utest::v1; |
| 30 | + |
| 31 | +/** |
| 32 | + * WiFi tests require following macros to be defined: |
| 33 | + * - MBED_CONF_APP_WIFI_SSID - SSID of a network the test will try connecting to |
| 34 | + * - MBED_CONF_APP_WIFI_PASSWORD - Passphrase that will be used to connecting to the network |
| 35 | + * - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3" |
| 36 | + */ |
| 37 | +#if !defined(MBED_CONF_APP_WIFI_SSID) || !defined(MBED_CONF_APP_WIFI_PASSWORD) || !defined(MBED_CONF_APP_WIFI_NETWORKS) |
| 38 | +#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. |
| 39 | +#endif |
| 40 | + |
| 41 | +const char *networks[] = {MBED_CONF_APP_WIFI_NETWORKS, NULL}; |
| 42 | + |
| 43 | +WiFiInterface *wifi; |
| 44 | + |
| 45 | +/* In normal circumstances the WiFi object could be global, but the delay introduced by WiFi initialization is an issue |
| 46 | + for the tests. It causes Greentea to timeout on syncing with the board. To solve it we defer the actual object |
| 47 | + creation till we actually need it. |
| 48 | + */ |
| 49 | +WiFiInterface *get_wifi() |
| 50 | +{ |
| 51 | + if (wifi == NULL) { |
| 52 | + /* We don't really care about freeing this, as its lifetime is through the full test suit run. */ |
| 53 | +#if TARGET_UBLOX_EVK_ODIN_W2 |
| 54 | + wifi = new OdinWiFiInterface; |
| 55 | +#endif |
| 56 | + } |
| 57 | + |
| 58 | + return wifi; |
| 59 | +} |
| 60 | + |
| 61 | +void check_wifi(const char *ssid, bool *net_stat) |
| 62 | +{ |
| 63 | + int i = 0; |
| 64 | + while(networks[i]) { |
| 65 | + if (strcmp(networks[i], ssid) == 0) { |
| 66 | + net_stat[i] = true; |
| 67 | + break; |
| 68 | + } |
| 69 | + i++; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +void wifi_scan() |
| 74 | +{ |
| 75 | + int count; |
| 76 | + WiFiAccessPoint *aps; |
| 77 | + const int net_len = sizeof(networks)/sizeof(networks[0]); |
| 78 | + bool net_stat[net_len - 1]; |
| 79 | + |
| 80 | + memset(net_stat, 0, sizeof(net_stat)); |
| 81 | + |
| 82 | + count = get_wifi()->scan(NULL, 0); |
| 83 | + TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error"); |
| 84 | + TEST_ASSERT_MESSAGE(count > 0, "Scan result empty"); |
| 85 | + |
| 86 | + aps = new WiFiAccessPoint[count]; |
| 87 | + count = get_wifi()->scan(aps, count); |
| 88 | + for(int i = 0; i < count; i++) { |
| 89 | + check_wifi(aps[i].get_ssid(), net_stat); |
| 90 | + } |
| 91 | + |
| 92 | + delete[] aps; |
| 93 | + |
| 94 | + for (unsigned i = 0; i < sizeof(net_stat); i++) { |
| 95 | + TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +void wifi_connect() |
| 100 | +{ |
| 101 | + int ret; |
| 102 | + |
| 103 | + ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
| 104 | + TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); |
| 105 | + |
| 106 | + ret = get_wifi()->disconnect(); |
| 107 | + TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); |
| 108 | +} |
| 109 | + |
| 110 | +void wifi_connect_scan() |
| 111 | +{ |
| 112 | + int ret; |
| 113 | + int count; |
| 114 | + WiFiAccessPoint *aps; |
| 115 | + const int net_len = sizeof(networks)/sizeof(networks[0]); |
| 116 | + bool net_stat[net_len - 1]; |
| 117 | + |
| 118 | + memset(net_stat, 0, sizeof(net_stat)); |
| 119 | + |
| 120 | + ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
| 121 | + TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); |
| 122 | + |
| 123 | + count = get_wifi()->scan(NULL, 0); |
| 124 | + TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error"); |
| 125 | + TEST_ASSERT_MESSAGE(count > 0, "Scan result empty"); |
| 126 | + |
| 127 | + aps = new WiFiAccessPoint[count]; |
| 128 | + count = get_wifi()->scan(aps, count); |
| 129 | + for(int i = 0; i < count; i++) { |
| 130 | + check_wifi(aps[i].get_ssid(), net_stat); |
| 131 | + } |
| 132 | + |
| 133 | + delete[] aps; |
| 134 | + |
| 135 | + ret = get_wifi()->disconnect(); |
| 136 | + TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); |
| 137 | + |
| 138 | + for (unsigned i = 0; i < sizeof(net_stat); i++) { |
| 139 | + TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected"); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +void wifi_http() |
| 144 | +{ |
| 145 | + TCPSocket socket; |
| 146 | + int ret; |
| 147 | + |
| 148 | + ret = get_wifi()->connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
| 149 | + TEST_ASSERT_MESSAGE(ret == 0, "Connect failed"); |
| 150 | + |
| 151 | + // Open a socket on the network interface, and create a TCP connection to www.arm.com |
| 152 | + ret = socket.open(get_wifi()); |
| 153 | + TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed"); |
| 154 | + ret = socket.connect("www.arm.com", 80); |
| 155 | + TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed"); |
| 156 | + |
| 157 | + // Send a simple http request |
| 158 | + char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
| 159 | + int scount = socket.send(sbuffer, sizeof sbuffer); |
| 160 | + TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed"); |
| 161 | + |
| 162 | + // Recieve a simple http response and check if it's not empty |
| 163 | + char rbuffer[64]; |
| 164 | + int rcount = socket.recv(rbuffer, sizeof rbuffer); |
| 165 | + TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error"); |
| 166 | + TEST_ASSERT_MESSAGE(rcount > 0, "No data received"); |
| 167 | + |
| 168 | + ret = socket.close(); |
| 169 | + TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed"); |
| 170 | + |
| 171 | + ret = get_wifi()->disconnect(); |
| 172 | + TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed"); |
| 173 | +} |
| 174 | + |
| 175 | +status_t greentea_failure_handler(const Case *const source, const failure_t reason) { |
| 176 | + greentea_case_failure_abort_handler(source, reason); |
| 177 | + return STATUS_CONTINUE; |
| 178 | +} |
| 179 | + |
| 180 | +Case cases[] = { |
| 181 | + Case("Scan test", wifi_scan, greentea_failure_handler), |
| 182 | + Case("Connect test", wifi_connect, greentea_failure_handler), |
| 183 | + Case("Scan while connected test", wifi_connect_scan, greentea_failure_handler), |
| 184 | + Case("HTTP test", wifi_http, greentea_failure_handler), |
| 185 | +}; |
| 186 | + |
| 187 | +status_t greentea_test_setup(const size_t number_of_cases) { |
| 188 | + GREENTEA_SETUP(90, "default_auto"); |
| 189 | + return greentea_test_setup_handler(number_of_cases); |
| 190 | +} |
| 191 | + |
| 192 | + |
| 193 | +int main() { |
| 194 | + Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
| 195 | + Harness::run(specification); |
| 196 | +} |
0 commit comments