test_wifi.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <unity.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <atomic>
  5. #include <kbf.h>
  6. #include <kbf/wifi.h>
  7. using std::cout;
  8. using std::endl;
  9. using std::string;
  10. using std::atomic;
  11. #define KBF_TEST_WIFI_SSID "kbf_test_wifi_ap"
  12. #define KBF_TEST_WIFI_PASS "Pas$w0Rd1337"
  13. static atomic<int> state{0};
  14. static void fail() {
  15. TEST_ASSERT_EQUAL_MESSAGE(-1337, state, "fail(): unexpected call to event handler");
  16. }
  17. static void waitForState(int expectedState, int timeoutSeconds) {
  18. int hax = 10;
  19. for (int i = 1; i < timeoutSeconds * hax; i++) {
  20. if (state == expectedState) break;
  21. if (i % hax == 0) {
  22. cout << "waitForState: " << state << " / " << expectedState << "; ";
  23. cout << i << " / " << timeoutSeconds * hax << endl;
  24. }
  25. kbf::sleep(1000 / hax);
  26. }
  27. }
  28. TEST_CASE("WiFi STA mode reconnect", "[kbf_wifi]") {
  29. const string ssid = "NonExistentSSID1337";
  30. const string password = "whatever";
  31. using namespace kbf;
  32. state = 0;
  33. auto sta = wifi::STA::create();
  34. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  35. sta->onConnect = fail;
  36. sta->onIp = fail;
  37. sta->onReconnecting = {[](int attempt, int limit) {
  38. TEST_ASSERT_EQUAL(state++, attempt);
  39. TEST_ASSERT_EQUAL(2, limit);
  40. return true;
  41. }};
  42. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
  43. wifi::start(sta);
  44. waitForState(1, 1);
  45. TEST_ASSERT_EQUAL(1, state);
  46. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 2);
  47. waitForState(4, 10);
  48. cout << "waiting for any more reconnect attempts..." << endl;
  49. kbf::sleep(5000);
  50. TEST_ASSERT_EQUAL(4, state);
  51. wifi::stop();
  52. }
  53. TEST_CASE("WiFi STA mode reconnect cancel", "[kbf_wifi]") {
  54. const string ssid = "NonExistentSSID1337";
  55. const string password = "whatever";
  56. using namespace kbf;
  57. wifi::start(wifi::STA::create());
  58. auto sta = wifi::getSTA();
  59. state = 0;
  60. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  61. sta->onConnect = fail;
  62. sta->onIp = fail;
  63. sta->onReconnecting = {[](int attempt, int limit) {
  64. state++;
  65. TEST_ASSERT_EQUAL(1, attempt);
  66. TEST_ASSERT_EQUAL(5, limit);
  67. return false;
  68. }};
  69. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
  70. waitForState(1, 1);
  71. TEST_ASSERT_EQUAL(1, state);
  72. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 5);
  73. waitForState(3, 10);
  74. cout << "waiting for any more reconnect attempts..." << endl;
  75. kbf::sleep(5000);
  76. TEST_ASSERT_EQUAL(3, state);
  77. wifi::stop();
  78. }
  79. void wifiSingleMaster() {
  80. using namespace kbf;
  81. wifi::start(wifi::AP::create(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS));
  82. auto ap = wifi::getAP();
  83. TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_SSID, ap->ssid.c_str());
  84. TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_PASS, ap->password.c_str());
  85. state = 0;
  86. ap->onStart = {[](wifi::AP &) { TEST_ASSERT_EQUAL(0, state++); }};
  87. ap->onConnect = {[](wifi::AP &, wifi::AP::STA &) { TEST_ASSERT_EQUAL(1, state++); }};
  88. ap->onDisconnect = {[](wifi::AP &, wifi::AP::STA &) { TEST_ASSERT_EQUAL(2, state++); }};
  89. ap->onStop = {[](wifi::AP &) { TEST_ASSERT_EQUAL(3, state++); }};
  90. waitForState(3, 30);
  91. wifi::stop();
  92. waitForState(4, 1);
  93. TEST_ASSERT_EQUAL(4, state);
  94. }
  95. void wifiSingleSlave() {
  96. using namespace kbf;
  97. wifi::start(wifi::STA::create());
  98. auto sta = wifi::getSTA();
  99. state = 0;
  100. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  101. sta->onConnect = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
  102. sta->onIp = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
  103. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
  104. sta->onStop = {[]() { TEST_ASSERT_EQUAL(4, state++); }};
  105. waitForState(1, 1);
  106. TEST_ASSERT_EQUAL(1, state);
  107. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
  108. waitForState(3, 30);
  109. TEST_ASSERT_EQUAL(3, state);
  110. const string ip = sta->ip().str();
  111. TEST_ASSERT_GREATER_THAN(0, ip.length());
  112. sta->disconnect();
  113. waitForState(4, 1);
  114. TEST_ASSERT_EQUAL(4, state);
  115. wifi::stop();
  116. waitForState(5, 1);
  117. TEST_ASSERT_EQUAL(5, state);
  118. }
  119. TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA", "[kbf_wifi]", wifiSingleMaster, wifiSingleSlave)