Skip to content

Commit 42df56c

Browse files
author
Jamie Smith
authored
Make network tests properly support boards with wifi again (mbed-ce#317)
* Make network tests properly support boards with wifi again * Style fixes * Style fix again
1 parent 5354079 commit 42df56c

File tree

80 files changed

+301
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+301
-582
lines changed

connectivity/netsocket/tests/TESTS/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,24 @@ if(DEFAULT_IFC_IDX EQUAL -1)
55
set(TEST_SKIPPED "No default network interface on this target")
66
endif()
77

8+
# Set up variables for wi-fi SSID and password
9+
if("MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE=WIFI" IN_LIST MBED_CONFIG_DEFINITIONS)
10+
set(MBED_GREENTEA_WIFI_SECURE_SSID "" CACHE STRING "SSID of a secured wi-fi network with internet access, for greentea tests which need to connect to wifi")
11+
set(MBED_GREENTEA_WIFI_SECURE_PASSWORD "" CACHE STRING "Password to the network given by MBED_GREENTEA_WIFI_SECURE_SSID")
12+
set(MBED_GREENTEA_WIFI_SECURE_PROTOCOL "WPA2" CACHE STRING "WiFi security protocol, valid values are WEP, WPA, WPA2, WPA_WPA2, WPA3, WPA3_WPA2, NONE")
13+
14+
if("${MBED_GREENTEA_WIFI_SECURE_SSID}" STREQUAL "" OR "${MBED_GREENTEA_WIFI_SECURE_PASSWORD}" STREQUAL "")
15+
message(WARNING "MBED_GREENTEA_WIFI_SECURE_SSID and MBED_GREENTEA_WIFI_SECURE_PASSWORD must be set in order for wi-fi greentea tests to pass")
16+
else()
17+
add_compile_definitions(
18+
"MBED_GREENTEA_WIFI_SECURE_SSID=\"${MBED_GREENTEA_WIFI_SECURE_SSID}\""
19+
"MBED_GREENTEA_WIFI_SECURE_PASSWORD=\"${MBED_GREENTEA_WIFI_SECURE_PASSWORD}\""
20+
MBED_GREENTEA_WIFI_SECURE_PROTOCOL=${MBED_GREENTEA_WIFI_SECURE_PROTOCOL})
21+
endif()
22+
endif()
23+
24+
# Pull in common util header
25+
include_directories(common)
26+
827
add_subdirectory(netsocket)
928
add_subdirectory(network)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright (c) 2024 Jamie Smith, All Rights Reserved
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
* not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
19+
#ifndef MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H
20+
#define MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H
21+
22+
#include "WiFiInterface.h"
23+
#include "NetworkInterface.h"
24+
25+
#define WIFI 2
26+
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
27+
(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_GREENTEA_WIFI_SECURE_SSID))
28+
#error [NOT_SUPPORTED] No network configuration found for this target.
29+
#endif
30+
31+
/*
32+
* Read the MBED_GREENTEA_WIFI_SECURE_PROTOCOL define from CMake and get the type of wifi
33+
* security in use
34+
*/
35+
inline nsapi_security get_wifi_security()
36+
{
37+
#define JOIN(x, y) JOIN_AGAIN(x, y)
38+
#define JOIN_AGAIN(x, y) x ## y
39+
return JOIN(NSAPI_SECURITY_, MBED_GREENTEA_WIFI_SECURE_PROTOCOL);
40+
#undef JOIN
41+
#undef JOIN_AGAIN
42+
}
43+
44+
/*
45+
* Get the wifi interface for this board, or nullptr if there is none
46+
*/
47+
inline WiFiInterface *get_wifi_interface()
48+
{
49+
auto *const wifi_interface = WiFiInterface::get_default_instance();
50+
if (wifi_interface == nullptr) {
51+
return nullptr;
52+
}
53+
54+
// Set the credentials based on CMake settings so it actually can connect to the internet
55+
wifi_interface->set_credentials(MBED_GREENTEA_WIFI_SECURE_SSID,
56+
MBED_GREENTEA_WIFI_SECURE_PASSWORD,
57+
get_wifi_security());
58+
59+
return wifi_interface;
60+
}
61+
62+
/*
63+
* Get the default network interface for this board.
64+
* For wifi, this also configures in the credentials passed to CMake.
65+
*/
66+
inline NetworkInterface *get_network_interface()
67+
{
68+
#if MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI
69+
return get_wifi_interface();
70+
#else
71+
return NetworkInterface::get_default_instance();
72+
#endif
73+
74+
}
75+
76+
#endif //MBED_OS_GREENTEA_GET_NETWORK_INTERFACE_H

connectivity/netsocket/tests/TESTS/netsocket/README.md

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ git checkout master
307307
cd ..
308308
```
309309

310-
Also, building socket test cases requires a special macro to enable all tests, so create an `mbed_app.json` file with the following content at minimum:
310+
To configure the echo server to a non-default IP and port, you can create an mbed_app.json with the following content:
311311

312312
```
313313
{
@@ -336,77 +336,10 @@ Also, building socket test cases requires a special macro to enable all tests, s
336336
}
337337
```
338338

339-
Wi-Fi tests require some more configuration, so for Wi-Fi purposes, the `mbed_app.json` might look like this:
340-
339+
Wi-Fi tests require configuration of your wifi network, so you might run CMake like this:
341340
```
342-
{
343-
"config": {
344-
"wifi-secure-ssid": {
345-
"help": "WiFi SSID for WPA2 secured network",
346-
"value": "\"test-network\""
347-
},
348-
"wifi-unsecure-ssid": {
349-
"help": "WiFi SSID for unsecure netwrok",
350-
"value": "\"unsecure-test-net\""
351-
},
352-
"wifi-password": {
353-
"help": "WiFi Password",
354-
"value": "\"password\""
355-
},
356-
"wifi-secure-protocol": {
357-
"help": "WiFi security protocol, valid values are WEP, WPA, WPA2, WPA_WPA2",
358-
"value": "\"WPA2\""
359-
},
360-
"wifi-ch-secure": {
361-
"help": "Channel number of secure SSID",
362-
"value": 6
363-
},
364-
"wifi-ch-unsecure": {
365-
"help": "Channel number of unsecure SSID",
366-
"value": 6
367-
},
368-
"ap-mac-secure": {
369-
"help": "BSSID of secure AP in form of AA:BB:CC:DD:EE:FF",
370-
"value": "\"58:8b:f3:99:f2:9c\""
371-
},
372-
"ap-mac-unsecure": {
373-
"help": "BSSID of unsecure AP in form of \"AA:BB:CC:DD:EE:FF\"",
374-
"value": "\"58:8b:f3:99:c2:08\""
375-
},
376-
"max-scan-size": {
377-
"help": "How many networks may appear in Wifi scan result",
378-
"value": 30
379-
},
380-
"echo-server-addr" : {
381-
"help" : "IP address of echo server",
382-
"value" : "\"echo.mbedcloudtesting.com\""
383-
},
384-
"echo-server-port" : {
385-
"help" : "Port of echo server",
386-
"value" : "7"
387-
},
388-
"echo-server-discard-port" : {
389-
"help" : "Discard port of echo server",
390-
"value" : "9"
391-
},
392-
"echo-server-port-tls" : {
393-
"help" : "Port of echo server for TLS",
394-
"value" : "2007"
395-
},
396-
"echo-server-discard-port-tls" : {
397-
"help" : "Discard port of echo server for TLS",
398-
"value" : "2009"
399-
}
400-
},
401-
"target_overrides": {
402-
"*": {
403-
"target.network-default-interface-type": "WIFI",
404-
"nsapi.default-wifi-ssid": "\"WIFI_SSID\"",
405-
"nsapi.default-wifi-password": "\"WIFI_PASSWORD\"",
406-
"nsapi.default-wifi-security": "WPA_WPA2"
407-
}
408-
}
409-
}
341+
cmake "-DMBED_GREENTEA_WIFI_SECURE_SSID=My Wifi Network Name" -DMBED_GREENTEA_WIFI_SECURE_PASSWORD=MyWifiNetworkPassword
342+
-DMBED_GREENTEA_WIFI_SECURE_CHANNEL=5
410343
```
411344

412345
Cellular tests require some more configuration, so for cellular purposes, the `mbed_app.json` might look like this:

connectivity/netsocket/tests/TESTS/netsocket/dns/asynchronous_dns_multi_ip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void do_getaddrinfo_async(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op
6565
unsigned int count = 0;
6666
for (unsigned int i = 0; i < op_count; i++) {
6767
data[i].semaphore = &semaphore;
68-
nsapi_error_t err = NetworkInterface::get_default_instance()->getaddrinfo_async(hosts[i], &hints, mbed::Callback<void(nsapi_error_t, SocketAddress *)>(getaddrinfo_cb, (void *) &data[i]));
68+
nsapi_error_t err = get_network_interface()->getaddrinfo_async(hosts[i], &hints, mbed::Callback<void(nsapi_error_t, SocketAddress *)>(getaddrinfo_cb, (void *) &data[i]));
6969
TEST_ASSERT(err >= 0 || err == NSAPI_ERROR_NO_MEMORY || err == NSAPI_ERROR_BUSY);
7070
if (err >= 0) {
7171
// Callback will be called

connectivity/netsocket/tests/TESTS/netsocket/dns/asynchronous_dns_timeouts.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ASYNCHRONOUS_DNS_TIMEOUTS()
7474
nsapi_error_t result;
7575
int count = MAX_TRIAL_ATTEMPTS;
7676
do {
77-
result = NetworkInterface::get_default_instance()->gethostbyname(dns_test_hosts[0], &address);
77+
result = get_network_interface()->gethostbyname(dns_test_hosts[0], &address);
7878
if (result == NSAPI_ERROR_OK) {
7979
return;
8080
}

connectivity/netsocket/tests/TESTS/netsocket/dns/dns_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "nsapi_dns.h"
2222
#include "mbed_trace.h"
23+
#include "greentea_get_network_interface.h"
2324

2425
#define TRACE_GROUP "GRNT"
2526

connectivity/netsocket/tests/TESTS/netsocket/dns/main.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@
1818
#if !defined(MBED_CONF_RTOS_PRESENT)
1919
#error [NOT_SUPPORTED] dns test cases require a RTOS to run.
2020
#else
21-
22-
#define WIFI 2
23-
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
24-
(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
25-
#error [NOT_SUPPORTED] No network configuration found for this target.
26-
#else
27-
2821
#include "mbed.h"
2922
#include "greentea-client/test_env.h"
3023
#include "unity.h"
@@ -167,7 +160,7 @@ static void net_bringup()
167160
nsapi_dns_reset();
168161
MBED_ASSERT(MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_NSAPI_DNS_CACHE_SIZE && MBED_CONF_APP_DNS_TEST_HOSTS_NUM >= MBED_CONF_APP_DNS_SIMULT_QUERIES + 1);
169162

170-
net = NetworkInterface::get_default_instance();
163+
net = get_network_interface();
171164
TEST_ASSERT_NOT_NULL_MESSAGE(net, "No NetworkInterface configured");
172165
nsapi_error_t err = net->connect();
173166
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
@@ -186,7 +179,7 @@ static void net_bringup()
186179

187180
static void net_bringdown()
188181
{
189-
NetworkInterface::get_default_instance()->disconnect();
182+
get_network_interface()->disconnect();
190183
tr_info("MBED: ifdown");
191184
}
192185

@@ -238,5 +231,4 @@ int main()
238231
return !Harness::run(specification);
239232
}
240233

241-
#endif // !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
242234
#endif // !defined(MBED_CONF_RTOS_PRESENT)

connectivity/netsocket/tests/TESTS/netsocket/dns/synchronous_dns_multi_ip.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void do_getaddrinfo(const char hosts[][DNS_TEST_HOST_LEN], unsigned int op_count
4747
SocketAddress hints{{NSAPI_UNSPEC}, 80};
4848
for (unsigned int i = 0; i < op_count; i++) {
4949
SocketAddress *result;
50-
nsapi_error_t err = NetworkInterface::get_default_instance()->get_default_instance()->getaddrinfo(hosts[i], &hints, &result);
50+
nsapi_error_t err = get_network_interface()->getaddrinfo(hosts[i], &hints, &result);
5151

5252
if (err == NSAPI_ERROR_DNS_FAILURE) {
5353
(*exp_dns_failure)++;

connectivity/netsocket/tests/TESTS/netsocket/tcp/main.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
#error [NOT_SUPPORTED] tcp test cases require a RTOS to run
2020
#else
2121

22-
#define WIFI 2
23-
#if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \
24-
(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
25-
#error [NOT_SUPPORTED] No network configuration found for this target.
26-
#else
27-
2822
#include "mbed.h"
2923
#include "greentea-client/test_env.h"
3024
#include "unity/unity.h"
@@ -66,15 +60,15 @@ void drop_bad_packets(TCPSocket &sock, int orig_timeout)
6660
nsapi_version_t get_ip_version()
6761
{
6862
SocketAddress test;
69-
if (NetworkInterface::get_default_instance()->get_ip_address(&test) != NSAPI_ERROR_OK) {
63+
if (get_network_interface()->get_ip_address(&test) != NSAPI_ERROR_OK) {
7064
return NSAPI_UNSPEC;
7165
}
7266
return test.get_ip_version();
7367
}
7468

7569
static void _ifup()
7670
{
77-
NetworkInterface *net = NetworkInterface::get_default_instance();
71+
NetworkInterface *net = get_network_interface();
7872
TEST_ASSERT_NOT_NULL_MESSAGE(net, "No NetworkInterface configured");
7973
nsapi_error_t err = net->connect();
8074
TEST_ASSERT_EQUAL(NSAPI_ERROR_OK, err);
@@ -93,20 +87,20 @@ static void _ifup()
9387

9488
static void _ifdown()
9589
{
96-
NetworkInterface::get_default_instance()->disconnect();
90+
get_network_interface()->disconnect();
9791
tr_info("MBED: ifdown");
9892
}
9993

10094
nsapi_error_t tcpsocket_connect_to_srv(TCPSocket &sock, uint16_t port)
10195
{
10296
SocketAddress tcp_addr;
10397

104-
NetworkInterface::get_default_instance()->gethostbyname(ECHO_SERVER_ADDR, &tcp_addr);
98+
get_network_interface()->gethostbyname(ECHO_SERVER_ADDR, &tcp_addr);
10599
tcp_addr.set_port(port);
106100

107101
tr_info("MBED: Server '%s', port %d", tcp_addr.get_ip_address(), tcp_addr.get_port());
108102

109-
nsapi_error_t err = sock.open(NetworkInterface::get_default_instance());
103+
nsapi_error_t err = sock.open(get_network_interface());
110104
if (err != NSAPI_ERROR_OK) {
111105
tr_error("Error from sock.open: %d", err);
112106
return err;
@@ -137,7 +131,7 @@ bool is_tcp_supported()
137131
static bool tested = false;
138132
if (!tested) {
139133
TCPSocket socket;
140-
supported = socket.open(NetworkInterface::get_default_instance()) == NSAPI_ERROR_OK;
134+
supported = socket.open(get_network_interface()) == NSAPI_ERROR_OK;
141135
}
142136
return supported;
143137
}
@@ -253,5 +247,4 @@ int main()
253247
}
254248

255249
#endif // ECHO_SERVER_ADDR
256-
#endif // !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID))
257250
#endif // !defined(MBED_CONF_RTOS_PRESENT)

connectivity/netsocket/tests/TESTS/netsocket/tcp/tcp_tests.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "../test_params.h"
2222
#include "mbed_trace.h"
23+
#include "greentea_get_network_interface.h"
2324

2425
#define TRACE_GROUP "GRNT"
2526

0 commit comments

Comments
 (0)