123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- #include <unity.h>
- #include <iostream>
- #include <string>
- #include <atomic>
- #include <kbf.h>
- #include <kbf/wifi.h>
- using std::cout;
- using std::endl;
- using std::string;
- using std::atomic;
- #define KBF_TEST_WIFI_SSID "kbf_test_wifi_ap"
- #define KBF_TEST_WIFI_PASS "Pas$w0Rd1337"
- static atomic<int> state{0};
- static void fail() {
- TEST_ASSERT_EQUAL_MESSAGE(-1337, state, "fail(): unexpected call to event handler");
- }
- static void waitForState(int expectedState, int timeoutSeconds) {
- int hax = 10;
- for (int i = 1; i < timeoutSeconds * hax; i++) {
- if (state == expectedState) break;
- if (i % hax == 0) {
- cout << "waitForState: " << state << " / " << expectedState << "; ";
- cout << i << " / " << timeoutSeconds * hax << endl;
- }
- kbf::sleep(1000 / hax);
- }
- }
- TEST_CASE("WiFi STA mode reconnect", "[kbf_wifi]") {
- const string ssid = "NonExistentSSID1337";
- const string password = "whatever";
- using namespace kbf;
- state = 0;
- auto sta = wifi::STA::create();
- sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
- sta->onConnect = fail;
- sta->onIp = fail;
- sta->onReconnecting = {[](int attempt, int limit) {
- TEST_ASSERT_EQUAL(state++, attempt);
- TEST_ASSERT_EQUAL(2, limit);
- return true;
- }};
- sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
- wifi::start(sta);
- waitForState(1, 1);
- TEST_ASSERT_EQUAL(1, state);
- sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 2);
- waitForState(4, 10);
- cout << "waiting for any more reconnect attempts..." << endl;
- kbf::sleep(5000);
- TEST_ASSERT_EQUAL(4, state);
- wifi::stop();
- }
- TEST_CASE("WiFi STA mode reconnect cancel", "[kbf_wifi]") {
- const string ssid = "NonExistentSSID1337";
- const string password = "whatever";
- using namespace kbf;
- wifi::start(wifi::STA::create());
- auto sta = wifi::getSTA();
- state = 0;
- sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
- sta->onConnect = fail;
- sta->onIp = fail;
- sta->onReconnecting = {[](int attempt, int limit) {
- state++;
- TEST_ASSERT_EQUAL(1, attempt);
- TEST_ASSERT_EQUAL(5, limit);
- return false;
- }};
- sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
- waitForState(1, 1);
- TEST_ASSERT_EQUAL(1, state);
- sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 5);
- waitForState(3, 10);
- cout << "waiting for any more reconnect attempts..." << endl;
- kbf::sleep(5000);
- TEST_ASSERT_EQUAL(3, state);
- wifi::stop();
- }
- void wifiSingleMaster() {
- using namespace kbf;
- wifi::start(wifi::AP::create(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS));
- auto ap = wifi::getAP();
- TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_SSID, ap->ssid.c_str());
- TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_PASS, ap->password.c_str());
- state = 0;
- ap->onStart = {[](wifi::AP &) { TEST_ASSERT_EQUAL(0, state++); }};
- ap->onConnect = {[](wifi::AP &, wifi::AP::STA &) { TEST_ASSERT_EQUAL(1, state++); }};
- ap->onDisconnect = {[](wifi::AP &, wifi::AP::STA &) { TEST_ASSERT_EQUAL(2, state++); }};
- ap->onStop = {[](wifi::AP &) { TEST_ASSERT_EQUAL(3, state++); }};
- waitForState(3, 30);
- wifi::stop();
- waitForState(4, 1);
- TEST_ASSERT_EQUAL(4, state);
- }
- void wifiSingleSlave() {
- using namespace kbf;
- wifi::start(wifi::STA::create());
- auto sta = wifi::getSTA();
- state = 0;
- sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
- sta->onConnect = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
- sta->onIp = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
- sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
- sta->onStop = {[]() { TEST_ASSERT_EQUAL(4, state++); }};
- waitForState(1, 1);
- TEST_ASSERT_EQUAL(1, state);
- sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
- waitForState(3, 30);
- TEST_ASSERT_EQUAL(3, state);
- const string ip = sta->ip().str();
- TEST_ASSERT_GREATER_THAN(0, ip.length());
- sta->disconnect();
- waitForState(4, 1);
- TEST_ASSERT_EQUAL(4, state);
- wifi::stop();
- waitForState(5, 1);
- TEST_ASSERT_EQUAL(5, state);
- }
- TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA", "[kbf_wifi]", wifiSingleMaster, wifiSingleSlave)
|