mirror of https://github.com/ARMmbed/mbed-os.git
Add new WiFi tests
The tests try to:
* scan for available networks and check whether specified networks
are present in the results.
* connect to and disconnect from the specified network.
* repeats the scan tests while connected to a network.
* connect to a network and perform simple HTTP query.
pull/3147/head
parent
7963e8e7c1
commit
beef1d8b00
|
|
@ -0,0 +1,196 @@
|
|||
/* mbed Microcontroller Library
|
||||
* Copyright (c) 2016 ARM Limited
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "utest/utest.h"
|
||||
#include "unity/unity.h"
|
||||
#include "greentea-client/test_env.h"
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
using namespace utest::v1;
|
||||
|
||||
/**
|
||||
* WiFi tests require following macros to be defined:
|
||||
* - WIFI_TEST_SSID - SSID of a network the test will try connecting to
|
||||
* - WIFI_TEST_PASS - Passphrase that will be used to connecting to the network
|
||||
* - WIFI_TEST_NETWORKS - List of network that presence will be asserted e.g. "net1", "net2", "net3"
|
||||
*/
|
||||
#if !defined(WIFI_TEST_SSID) || !defined(WIFI_TEST_PASS) || !defined(WIFI_TEST_NETWORKS)
|
||||
#error WIFI_TEST_NETWORKS, WIFI_TEST_PASS and WIFI_TEST_NETWORKS have to be defined for this test.
|
||||
#endif
|
||||
|
||||
const char *networks[] = {WIFI_TEST_NETWORKS, NULL};
|
||||
|
||||
/* We use singletons as some of the WiFi modules don't like to be initialized multiple times and using global objects
|
||||
causes greentea serial to timeout.
|
||||
*/
|
||||
#if TARGET_UBLOX_EVK_ODIN_W2
|
||||
#include "OdinWiFiInterface.h"
|
||||
SingletonPtr<OdinWiFiInterface> wifi;
|
||||
#else
|
||||
#if !TARGET_FF_ARDUINO
|
||||
#error [NOT_SUPPORTED] Only Arduino form factor devices supported
|
||||
#endif
|
||||
#include "ESP8266Interface.h"
|
||||
ESP8266Interface wifi(D1, D0);
|
||||
#endif
|
||||
|
||||
/* That's a hack to accommodate Odin requiring a singleton */
|
||||
WiFiInterface *get_wifi()
|
||||
{
|
||||
#if TARGET_UBLOX_EVK_ODIN_W2
|
||||
return wifi.get();
|
||||
#else
|
||||
return &wifi;
|
||||
#endif
|
||||
}
|
||||
|
||||
void check_wifi(const char *ssid, bool *net_stat)
|
||||
{
|
||||
int i = 0;
|
||||
while(networks[i]) {
|
||||
if (strcmp(networks[i], ssid) == 0) {
|
||||
net_stat[i] = true;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_scan()
|
||||
{
|
||||
int count;
|
||||
WiFiAccessPoint *aps;
|
||||
const int net_len = sizeof(networks)/sizeof(networks[0]);
|
||||
bool net_stat[net_len - 1];
|
||||
|
||||
memset(net_stat, 0, sizeof(net_stat));
|
||||
|
||||
count = get_wifi()->scan(NULL, 0);
|
||||
TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
|
||||
TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
|
||||
|
||||
aps = new WiFiAccessPoint[count];
|
||||
count = get_wifi()->scan(aps, count);
|
||||
for(int i = 0; i < count; i++) {
|
||||
check_wifi(aps[i].get_ssid(), net_stat);
|
||||
}
|
||||
|
||||
delete[] aps;
|
||||
|
||||
for (unsigned i = 0; i < sizeof(net_stat); i++) {
|
||||
TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_connect()
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = get_wifi()->connect(WIFI_TEST_SSID, WIFI_TEST_PASS, NSAPI_SECURITY_WPA_WPA2);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
|
||||
|
||||
ret = get_wifi()->disconnect();
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
|
||||
}
|
||||
|
||||
void wifi_connect_scan()
|
||||
{
|
||||
int ret;
|
||||
int count;
|
||||
WiFiAccessPoint *aps;
|
||||
const int net_len = sizeof(networks)/sizeof(networks[0]);
|
||||
bool net_stat[net_len - 1];
|
||||
|
||||
memset(net_stat, 0, sizeof(net_stat));
|
||||
|
||||
ret = get_wifi()->connect(WIFI_TEST_SSID, WIFI_TEST_PASS, NSAPI_SECURITY_WPA_WPA2);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
|
||||
|
||||
count = get_wifi()->scan(NULL, 0);
|
||||
TEST_ASSERT_MESSAGE(count >= 0, "WiFi interface returned error");
|
||||
TEST_ASSERT_MESSAGE(count > 0, "Scan result empty");
|
||||
|
||||
aps = new WiFiAccessPoint[count];
|
||||
count = get_wifi()->scan(aps, count);
|
||||
for(int i = 0; i < count; i++) {
|
||||
check_wifi(aps[i].get_ssid(), net_stat);
|
||||
}
|
||||
|
||||
delete[] aps;
|
||||
|
||||
ret = get_wifi()->disconnect();
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
|
||||
|
||||
for (unsigned i = 0; i < sizeof(net_stat); i++) {
|
||||
TEST_ASSERT_MESSAGE(net_stat[i] == true, "Not all required WiFi network detected");
|
||||
}
|
||||
}
|
||||
|
||||
void wifi_http()
|
||||
{
|
||||
TCPSocket socket;
|
||||
int ret;
|
||||
|
||||
ret = get_wifi()->connect(WIFI_TEST_SSID, WIFI_TEST_PASS, NSAPI_SECURITY_WPA_WPA2);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Connect failed");
|
||||
|
||||
// Open a socket on the network interface, and create a TCP connection to www.arm.com
|
||||
ret = socket.open(get_wifi());
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Socket open failed");
|
||||
ret = socket.connect("www.arm.com", 80);
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Socket connect failed");
|
||||
|
||||
// Send a simple http request
|
||||
char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
|
||||
int scount = socket.send(sbuffer, sizeof sbuffer);
|
||||
TEST_ASSERT_MESSAGE(scount >= 0, "Socket send failed");
|
||||
|
||||
// Recieve a simple http response and check if it's not empty
|
||||
char rbuffer[64];
|
||||
int rcount = socket.recv(rbuffer, sizeof rbuffer);
|
||||
TEST_ASSERT_MESSAGE(rcount >= 0, "Socket recv error");
|
||||
TEST_ASSERT_MESSAGE(rcount > 0, "No data received");
|
||||
|
||||
ret = socket.close();
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Socket close failed");
|
||||
|
||||
ret = get_wifi()->disconnect();
|
||||
TEST_ASSERT_MESSAGE(ret == 0, "Disconnect failed");
|
||||
}
|
||||
|
||||
status_t greentea_failure_handler(const Case *const source, const failure_t reason) {
|
||||
greentea_case_failure_abort_handler(source, reason);
|
||||
return STATUS_CONTINUE;
|
||||
}
|
||||
|
||||
Case cases[] = {
|
||||
Case("Scan test", wifi_scan, greentea_failure_handler),
|
||||
Case("Connect test", wifi_connect, greentea_failure_handler),
|
||||
Case("Scan while connected test", wifi_connect_scan, greentea_failure_handler),
|
||||
Case("HTTP test", wifi_http, greentea_failure_handler),
|
||||
};
|
||||
|
||||
status_t greentea_test_setup(const size_t number_of_cases) {
|
||||
GREENTEA_SETUP(60, "default_auto");
|
||||
return greentea_test_setup_handler(number_of_cases);
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
|
||||
Harness::run(specification);
|
||||
}
|
||||
Loading…
Reference in New Issue