|
@@ -0,0 +1,205 @@
|
|
|
+#include <atomic>
|
|
|
+#include <vector>
|
|
|
+
|
|
|
+#include "kbf.h"
|
|
|
+#include "kbf/wifi.h"
|
|
|
+#include "kbf/rtos.h"
|
|
|
+
|
|
|
+#include <unity.h>
|
|
|
+
|
|
|
+#define TEST_SSID "kbf_test_wifi_ap"
|
|
|
+#define MASTER_SSID "kbf_test_wifi_master"
|
|
|
+#define SLAVE_SSID "kbf_test_wifi_slave"
|
|
|
+#define WIFI_PASS "Pas$w0Rd1337"
|
|
|
+
|
|
|
+using namespace kbf;
|
|
|
+using std::vector;
|
|
|
+
|
|
|
+static std::atomic<int> state{0};
|
|
|
+static rtos::EventGroup event; // NOLINT(cert-err58-cpp)
|
|
|
+
|
|
|
+static void singleMaster() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(0, state++); }};
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &) {
|
|
|
+ TEST_ASSERT_EQUAL(1, state++);
|
|
|
+ event.setBit(0);
|
|
|
+ }};
|
|
|
+ wifi::ap::start(TEST_SSID, WIFI_PASS);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(2, state);
|
|
|
+
|
|
|
+ kbf::sleep(100); // wait for slave tests to finish
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+static void singleSlave() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
|
|
|
+ wifi::sta::onIp = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
|
|
|
+ wifi::sta::onDisconnect = {[]() { event.setBit(0); }};
|
|
|
+ wifi::sta::start();
|
|
|
+
|
|
|
+ wifi::sta::connect(TEST_SSID, WIFI_PASS);
|
|
|
+ TEST_ASSERT_EQUAL(2, state);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(2, state);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA", "[kbf_wifi]", singleMaster, singleSlave);
|
|
|
+
|
|
|
+static void asyncMaster() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(0, state++); }};
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &) {
|
|
|
+ TEST_ASSERT_EQUAL(1, state++);
|
|
|
+ event.setBit(0);
|
|
|
+ }};
|
|
|
+ wifi::ap::start(TEST_SSID, WIFI_PASS);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(2, state);
|
|
|
+
|
|
|
+ kbf::sleep(100); // wait for slave tests to finish
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+static void asyncSlave() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
|
|
|
+ wifi::sta::onIp = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
|
|
|
+ wifi::sta::onDisconnect = {[]() { event.setBit(0); }};
|
|
|
+ wifi::sta::start();
|
|
|
+
|
|
|
+ wifi::sta::connect(TEST_SSID, WIFI_PASS, true);
|
|
|
+ TEST_ASSERT_EQUAL(0, state);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(2, state);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA async", "[kbf_wifi]", asyncMaster, asyncSlave);
|
|
|
+
|
|
|
+static void dualMaster() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::ap::onConnect = {[](wifi::STA &) {
|
|
|
+ TEST_ASSERT_EQUAL(0, state++);
|
|
|
+ wifi::sta::connect(SLAVE_SSID, WIFI_PASS);
|
|
|
+ }};
|
|
|
+ wifi::sta::onConnect = {[]() {
|
|
|
+ TEST_ASSERT_EQUAL(1, state++);
|
|
|
+ wifi::sta::disconnect();
|
|
|
+ }};
|
|
|
+ wifi::sta::onDisconnect = {[]() {
|
|
|
+ TEST_ASSERT_EQUAL(2, state++);
|
|
|
+ }};
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &) {
|
|
|
+ TEST_ASSERT_EQUAL(3, state++);
|
|
|
+ event.setBit(0);
|
|
|
+ }};
|
|
|
+ wifi::dual::start(MASTER_SSID, WIFI_PASS);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(4, state);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+static void dualSlave() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
|
|
|
+ wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(1, state++); }};
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &) {
|
|
|
+ TEST_ASSERT_EQUAL(2, state++);
|
|
|
+ wifi::sta::disconnect();
|
|
|
+ }};
|
|
|
+ wifi::sta::onDisconnect = {[]() {
|
|
|
+ TEST_ASSERT_EQUAL(3, state++);
|
|
|
+ event.setBit(0);
|
|
|
+ }};
|
|
|
+ wifi::dual::start(SLAVE_SSID, WIFI_PASS);
|
|
|
+ wifi::sta::connect(MASTER_SSID, WIFI_PASS);
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(4, state);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE_MULTIPLE_DEVICES("WiFi dual <--> dual", "[kbf_wifi]", dualMaster, dualSlave);
|
|
|
+
|
|
|
+static void scanMaster() {
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::ap::start(TEST_SSID, WIFI_PASS);
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &){ event.setBit(0); }};
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+static void scanSlave() {
|
|
|
+ state = 0;
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::sta::onScanDone = {[](vector<wifi::APInfo> &apList, void* data) {
|
|
|
+ TEST_ASSERT_EQUAL(0, state++);
|
|
|
+ TEST_ASSERT_EQUAL_STRING("test", static_cast<char *>(data));
|
|
|
+
|
|
|
+ bool found = false;
|
|
|
+ for (const auto &ap : apList) {
|
|
|
+ if (ap.ssid == TEST_SSID) {
|
|
|
+ found = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ TEST_ASSERT_TRUE(found);
|
|
|
+
|
|
|
+ wifi::sta::connect(TEST_SSID, WIFI_PASS);
|
|
|
+ }};
|
|
|
+ wifi::sta::onConnect = {[]() { event.setBit(0); }};
|
|
|
+
|
|
|
+ wifi::sta::start();
|
|
|
+ wifi::sta::startScan((void *) "test");
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ TEST_ASSERT_EQUAL(1, state);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE_MULTIPLE_DEVICES("WiFi scan", "[kbf_wifi]", scanMaster, scanSlave);
|
|
|
+
|
|
|
+static void ipMaster() {
|
|
|
+ event.clear();
|
|
|
+
|
|
|
+ wifi::ap::start(TEST_SSID, WIFI_PASS, net::IP("192.168.100.1"));
|
|
|
+ wifi::ap::onDisconnect = {[](wifi::STA &){ event.setBit(0); }};
|
|
|
+
|
|
|
+ event.waitForBit(0);
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+static void ipSlave() {
|
|
|
+ wifi::sta::start();
|
|
|
+ wifi::sta::connect(TEST_SSID, WIFI_PASS);
|
|
|
+
|
|
|
+ auto ip = wifi::sta::ip().str();
|
|
|
+ TEST_ASSERT_EQUAL_STRING("192.168.100.2", ip.c_str());
|
|
|
+
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE_MULTIPLE_DEVICES("WiFi custom IP address", "[kbf_wifi]", ipMaster, ipSlave);
|