test_wifi.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <atomic>
  2. #include <vector>
  3. #include "kbf.h"
  4. #include "kbf/wifi.h"
  5. #include "kbf/rtos.h"
  6. #include <unity.h>
  7. #define TEST_SSID "kbf_test_wifi_ap"
  8. #define MASTER_SSID "kbf_test_wifi_master"
  9. #define SLAVE_SSID "kbf_test_wifi_slave"
  10. #define WIFI_PASS "Pas$w0Rd1337"
  11. using namespace kbf;
  12. using std::vector;
  13. static std::atomic<int> state{0};
  14. static rtos::EventGroup event; // NOLINT(cert-err58-cpp)
  15. static void singleMaster() {
  16. state = 0;
  17. event.clear();
  18. wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(0, state++); }};
  19. wifi::ap::onDisconnect = {[](wifi::STA &) {
  20. TEST_ASSERT_EQUAL(1, state++);
  21. event.setBit(0);
  22. }};
  23. wifi::ap::start(TEST_SSID, WIFI_PASS);
  24. event.waitForBit(0);
  25. TEST_ASSERT_EQUAL(2, state);
  26. kbf::sleep(100); // wait for slave tests to finish
  27. wifi::stop();
  28. }
  29. static void singleSlave() {
  30. state = 0;
  31. event.clear();
  32. wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  33. wifi::sta::onIp = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
  34. wifi::sta::onDisconnect = {[]() { event.setBit(0); }};
  35. wifi::sta::start();
  36. wifi::sta::connect(TEST_SSID, WIFI_PASS);
  37. TEST_ASSERT_EQUAL(2, state);
  38. event.waitForBit(0);
  39. TEST_ASSERT_EQUAL(2, state);
  40. wifi::stop();
  41. }
  42. TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA", "[kbf_wifi]", singleMaster, singleSlave);
  43. static void asyncMaster() {
  44. state = 0;
  45. event.clear();
  46. wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(0, state++); }};
  47. wifi::ap::onDisconnect = {[](wifi::STA &) {
  48. TEST_ASSERT_EQUAL(1, state++);
  49. event.setBit(0);
  50. }};
  51. wifi::ap::start(TEST_SSID, WIFI_PASS);
  52. event.waitForBit(0);
  53. TEST_ASSERT_EQUAL(2, state);
  54. kbf::sleep(100); // wait for slave tests to finish
  55. wifi::stop();
  56. }
  57. static void asyncSlave() {
  58. state = 0;
  59. event.clear();
  60. wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  61. wifi::sta::onIp = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
  62. wifi::sta::onDisconnect = {[]() { event.setBit(0); }};
  63. wifi::sta::start();
  64. wifi::sta::connect(TEST_SSID, WIFI_PASS, true);
  65. TEST_ASSERT_EQUAL(0, state);
  66. event.waitForBit(0);
  67. TEST_ASSERT_EQUAL(2, state);
  68. wifi::stop();
  69. }
  70. TEST_CASE_MULTIPLE_DEVICES("WiFi AP <--> STA async", "[kbf_wifi]", asyncMaster, asyncSlave);
  71. static void dualMaster() {
  72. state = 0;
  73. event.clear();
  74. wifi::ap::onConnect = {[](wifi::STA &) {
  75. TEST_ASSERT_EQUAL(0, state++);
  76. wifi::sta::connect(SLAVE_SSID, WIFI_PASS);
  77. }};
  78. wifi::sta::onConnect = {[]() {
  79. TEST_ASSERT_EQUAL(1, state++);
  80. wifi::sta::disconnect();
  81. }};
  82. wifi::sta::onDisconnect = {[]() {
  83. TEST_ASSERT_EQUAL(2, state++);
  84. }};
  85. wifi::ap::onDisconnect = {[](wifi::STA &) {
  86. TEST_ASSERT_EQUAL(3, state++);
  87. event.setBit(0);
  88. }};
  89. wifi::dual::start(MASTER_SSID, WIFI_PASS);
  90. event.waitForBit(0);
  91. TEST_ASSERT_EQUAL(4, state);
  92. wifi::stop();
  93. }
  94. static void dualSlave() {
  95. state = 0;
  96. event.clear();
  97. wifi::sta::onConnect = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  98. wifi::ap::onConnect = {[](wifi::STA &) { TEST_ASSERT_EQUAL(1, state++); }};
  99. wifi::ap::onDisconnect = {[](wifi::STA &) {
  100. TEST_ASSERT_EQUAL(2, state++);
  101. wifi::sta::disconnect();
  102. }};
  103. wifi::sta::onDisconnect = {[]() {
  104. TEST_ASSERT_EQUAL(3, state++);
  105. event.setBit(0);
  106. }};
  107. wifi::dual::start(SLAVE_SSID, WIFI_PASS);
  108. wifi::sta::connect(MASTER_SSID, WIFI_PASS);
  109. event.waitForBit(0);
  110. TEST_ASSERT_EQUAL(4, state);
  111. wifi::stop();
  112. }
  113. TEST_CASE_MULTIPLE_DEVICES("WiFi dual <--> dual", "[kbf_wifi]", dualMaster, dualSlave);
  114. static void scanMaster() {
  115. event.clear();
  116. wifi::ap::start(TEST_SSID, WIFI_PASS);
  117. wifi::ap::onDisconnect = {[](wifi::STA &){ event.setBit(0); }};
  118. event.waitForBit(0);
  119. wifi::stop();
  120. }
  121. static void scanSlave() {
  122. state = 0;
  123. event.clear();
  124. wifi::sta::onScanDone = {[](vector<wifi::APInfo> &apList, void* data) {
  125. TEST_ASSERT_EQUAL(0, state++);
  126. TEST_ASSERT_EQUAL_STRING("test", static_cast<char *>(data));
  127. bool found = false;
  128. for (const auto &ap : apList) {
  129. if (ap.ssid == TEST_SSID) {
  130. found = true;
  131. }
  132. }
  133. TEST_ASSERT_TRUE(found);
  134. wifi::sta::connect(TEST_SSID, WIFI_PASS);
  135. }};
  136. wifi::sta::onConnect = {[]() { event.setBit(0); }};
  137. wifi::sta::start();
  138. wifi::sta::startScan((void *) "test");
  139. event.waitForBit(0);
  140. TEST_ASSERT_EQUAL(1, state);
  141. wifi::stop();
  142. }
  143. TEST_CASE_MULTIPLE_DEVICES("WiFi scan", "[kbf_wifi]", scanMaster, scanSlave);
  144. static void ipMaster() {
  145. event.clear();
  146. wifi::ap::start(TEST_SSID, WIFI_PASS, net::IP("192.168.100.1"));
  147. wifi::ap::onDisconnect = {[](wifi::STA &){ event.setBit(0); }};
  148. event.waitForBit(0);
  149. wifi::stop();
  150. }
  151. static void ipSlave() {
  152. wifi::sta::start();
  153. wifi::sta::connect(TEST_SSID, WIFI_PASS);
  154. auto ip = wifi::sta::ip().str();
  155. TEST_ASSERT_EQUAL_STRING("192.168.100.2", ip.c_str());
  156. wifi::stop();
  157. }
  158. TEST_CASE_MULTIPLE_DEVICES("WiFi custom IP address", "[kbf_wifi]", ipMaster, ipSlave);