test_wifi_legacy.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. #include <iostream>
  2. #include <string>
  3. #include <atomic>
  4. #include <vector>
  5. #include <kbf.h>
  6. #include <kbf/wifi_legacy.h>
  7. #include <kbf/rtos.h>
  8. #include <kbf/http/client.h>
  9. #include <kbf/web_service.h>
  10. #include <unity.h>
  11. using std::cout;
  12. using std::endl;
  13. using std::string;
  14. using std::atomic;
  15. #define KBF_TEST_WIFI_SSID "kbf_test_wifi_ap"
  16. #define KBF_TEST_WIFI_DUAL_MASTER_SSID "kbf_test_wifi_master"
  17. #define KBF_TEST_WIFI_DUAL_SLAVE_SSID "kbf_test_wifi_slave"
  18. #define KBF_TEST_WIFI_PASS "Pas$w0Rd1337"
  19. static atomic<int> state{0};
  20. static void fail() {
  21. TEST_ASSERT_EQUAL_MESSAGE(-1337, state, "fail(): unexpected call to event handler");
  22. }
  23. static void waitForState(int expectedState, int timeoutSeconds) {
  24. int hax = 10;
  25. for (int i = 1; i < timeoutSeconds * hax; i++) {
  26. if (state == expectedState) break;
  27. if (i % hax == 0) {
  28. cout << "waitForState: " << state << " / " << expectedState << "; ";
  29. cout << i << " / " << timeoutSeconds * hax << endl;
  30. }
  31. kbf::sleep(1000 / hax);
  32. }
  33. }
  34. TEST_CASE("WiFi legacy STA mode reconnect", "[kbf_wifi_legacy]") {
  35. using namespace kbf;
  36. state = 0;
  37. auto sta = wifi::STA::create();
  38. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  39. sta->onConnect = fail;
  40. sta->onIp = fail;
  41. sta->onReconnecting = {[](int attempt, int limit) {
  42. TEST_ASSERT_EQUAL(state++, attempt);
  43. TEST_ASSERT_EQUAL(2, limit);
  44. return true;
  45. }};
  46. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
  47. wifi::start(sta);
  48. waitForState(1, 1);
  49. TEST_ASSERT_EQUAL(1, state);
  50. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 2);
  51. waitForState(4, 10);
  52. cout << "waiting for any more reconnect attempts..." << endl;
  53. kbf::sleep(5000);
  54. TEST_ASSERT_EQUAL(4, state);
  55. wifi::stop();
  56. }
  57. TEST_CASE("WiFi legacy STA mode reconnect cancel", "[kbf_wifi_legacy]") {
  58. using namespace kbf;
  59. wifi::start(wifi::STA::create());
  60. auto sta = wifi::getSTA();
  61. state = 0;
  62. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  63. sta->onConnect = fail;
  64. sta->onIp = fail;
  65. sta->onReconnecting = {[](int attempt, int limit) {
  66. state++;
  67. TEST_ASSERT_EQUAL(1, attempt);
  68. TEST_ASSERT_EQUAL(5, limit);
  69. return false;
  70. }};
  71. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
  72. waitForState(1, 1);
  73. TEST_ASSERT_EQUAL(1, state);
  74. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, 5);
  75. waitForState(3, 10);
  76. cout << "waiting for any more reconnect attempts..." << endl;
  77. kbf::sleep(5000);
  78. TEST_ASSERT_EQUAL(3, state);
  79. wifi::stop();
  80. }
  81. void wifiSingleMaster() {
  82. using namespace kbf;
  83. wifi::start(wifi::AP::create(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS));
  84. auto ap = wifi::getAP();
  85. TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_SSID, ap->ssid.c_str());
  86. TEST_ASSERT_EQUAL_STRING(KBF_TEST_WIFI_PASS, ap->password.c_str());
  87. state = 0;
  88. ap->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  89. ap->onConnect = {[](wifi::AP::STA &) { TEST_ASSERT_EQUAL(1, state++); }};
  90. ap->onDisconnect = {[](wifi::AP::STA &) { TEST_ASSERT_EQUAL(2, state++); }};
  91. ap->onStop = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
  92. waitForState(3, 30);
  93. wifi::stop();
  94. waitForState(4, 1);
  95. TEST_ASSERT_EQUAL(4, state);
  96. }
  97. void wifiSingleSlave() {
  98. using namespace kbf;
  99. wifi::start(wifi::STA::create());
  100. auto sta = wifi::getSTA();
  101. state = 0;
  102. sta->onStart = {[]() { TEST_ASSERT_EQUAL(0, state++); }};
  103. sta->onConnect = {[]() { TEST_ASSERT_EQUAL(1, state++); }};
  104. sta->onIp = {[]() { TEST_ASSERT_EQUAL(2, state++); }};
  105. sta->onDisconnect = {[]() { TEST_ASSERT_EQUAL(3, state++); }};
  106. sta->onStop = {[]() { TEST_ASSERT_EQUAL(4, state++); }};
  107. waitForState(1, 1);
  108. TEST_ASSERT_EQUAL(1, state);
  109. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
  110. waitForState(3, 30);
  111. TEST_ASSERT_EQUAL(3, state);
  112. const string ip = sta->ip().str();
  113. TEST_ASSERT_GREATER_THAN(0, ip.length());
  114. sta->disconnect();
  115. waitForState(4, 1);
  116. TEST_ASSERT_EQUAL(4, state);
  117. wifi::stop();
  118. waitForState(5, 1);
  119. TEST_ASSERT_EQUAL(5, state);
  120. }
  121. TEST_CASE_MULTIPLE_DEVICES("WiFi legacy AP <--> STA", "[kbf_wifi_legacy]", wifiSingleMaster, wifiSingleSlave)
  122. void wifiDualMaster() {
  123. using namespace kbf;
  124. auto ap = wifi::AP::create(KBF_TEST_WIFI_DUAL_MASTER_SSID, KBF_TEST_WIFI_PASS);
  125. auto sta = wifi::STA::create();
  126. state = 0;
  127. ap->onStart = {[]() {
  128. TEST_ASSERT_EQUAL(0, state++);
  129. }};
  130. ap->onConnect = {[](wifi::AP::STA &) {
  131. TEST_ASSERT_EQUAL(1, state++);
  132. auto sta = wifi::getSTA();
  133. sta->connect(KBF_TEST_WIFI_DUAL_SLAVE_SSID, KBF_TEST_WIFI_PASS);
  134. }};
  135. sta->onConnect = {[]() {
  136. TEST_ASSERT_EQUAL(2, state++);
  137. auto sta = wifi::getSTA();
  138. sta->disconnect();
  139. }};
  140. sta->onDisconnect = {[]() {
  141. TEST_ASSERT_EQUAL(3, state++);
  142. }};
  143. ap->onDisconnect = {[](wifi::AP::STA &) {
  144. TEST_ASSERT_EQUAL(4, state++);
  145. }};
  146. wifi::start(ap, sta);
  147. waitForState(5, 30);
  148. wifi::stop();
  149. }
  150. void wifiDualSlave() {
  151. using namespace kbf;
  152. auto ap = wifi::AP::create(KBF_TEST_WIFI_DUAL_SLAVE_SSID, KBF_TEST_WIFI_PASS);
  153. auto sta = wifi::STA::create();
  154. state = 0;
  155. ap->onStart = {[]() {
  156. TEST_ASSERT_EQUAL(0, state++);
  157. auto sta = wifi::getSTA();
  158. sta->connect(KBF_TEST_WIFI_DUAL_MASTER_SSID, KBF_TEST_WIFI_PASS);
  159. }};
  160. sta->onConnect = {[]() {
  161. TEST_ASSERT_EQUAL(1, state++);
  162. }};
  163. ap->onConnect = {[](wifi::AP::STA &) {
  164. TEST_ASSERT_EQUAL(2, state++);
  165. }};
  166. ap->onDisconnect = {[](wifi::AP::STA &) {
  167. TEST_ASSERT_EQUAL(3, state++);
  168. auto sta = wifi::getSTA();
  169. sta->disconnect();
  170. }};
  171. sta->onDisconnect = {[]() {
  172. TEST_ASSERT_EQUAL(4, state++);
  173. }};
  174. wifi::start(ap, sta);
  175. waitForState(5, 30);
  176. wifi::stop();
  177. }
  178. TEST_CASE_MULTIPLE_DEVICES("WiFi legacy AP+STA <--> AP+STA", "[kbf_wifi_legacy]", wifiDualMaster, wifiDualSlave)
  179. void wifiScanMaster() {
  180. using namespace kbf;
  181. auto ap = wifi::AP::create(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
  182. state = 0;
  183. ap->onDisconnect = {[](wifi::AP::STA &) {
  184. TEST_ASSERT_EQUAL(0, state++);
  185. }};
  186. wifi::start(ap);
  187. waitForState(1, 30);
  188. wifi::stop();
  189. }
  190. void wifiScanSlave() {
  191. using namespace kbf;
  192. using std::vector;
  193. state = 0;
  194. wifi::start();
  195. auto sta = wifi::getSTA();
  196. sta->onScanDone = {[](vector<wifi::APInfo> &apList, void *data) {
  197. TEST_ASSERT_EQUAL(0, state++);
  198. TEST_ASSERT_EQUAL_STRING("foobar!", (char *) data);
  199. bool found = false;
  200. for (const auto &ap : apList) {
  201. if (ap.ssid == KBF_TEST_WIFI_SSID) {
  202. found = true;
  203. break;
  204. }
  205. }
  206. TEST_ASSERT_TRUE(found)
  207. wifi::getSTA()->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
  208. }};
  209. sta->onConnect = {[]() {
  210. TEST_ASSERT_EQUAL(1, state++);
  211. }};
  212. sta->startScan((void *) "foobar!");
  213. waitForState(2, 30);
  214. wifi::stop();
  215. }
  216. TEST_CASE_MULTIPLE_DEVICES("WiFi legacy scan", "[kbf_wifi_legacy]", wifiScanMaster, wifiScanSlave)
  217. static kbf::rtos::EventGroup eventGroup;
  218. void wifiIpMaster() {
  219. using namespace kbf;
  220. eventGroup = rtos::EventGroup();
  221. auto ap = wifi::AP::create(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS, net::IP("192.168.1.1"));
  222. ap->onDisconnect = {[](wifi::AP::STA &) {
  223. eventGroup.setBit(0);
  224. }};
  225. wifi::start(ap);
  226. eventGroup.waitForBit(0);
  227. wifi::stop();
  228. }
  229. void wifiIpSlave() {
  230. using namespace kbf;
  231. using namespace std;
  232. eventGroup = rtos::EventGroup();
  233. wifi::start();
  234. auto sta = wifi::getSTA();
  235. sta->onIp = {[]() {
  236. auto ip = wifi::getSTA()->ip();
  237. eventGroup.setBit(0);
  238. TEST_ASSERT_EQUAL(192, ip[0]);
  239. TEST_ASSERT_EQUAL(168, ip[1]);
  240. TEST_ASSERT_EQUAL(1, ip[2]);
  241. TEST_ASSERT_EQUAL(2, ip[3]);
  242. }};
  243. sta->connect(KBF_TEST_WIFI_SSID, KBF_TEST_WIFI_PASS);
  244. eventGroup.waitForBit(0);
  245. wifi::stop();
  246. }
  247. TEST_CASE_MULTIPLE_DEVICES("WiFi legacy custom IP address", "[kbf_wifi_legacy]", wifiIpMaster, wifiIpSlave)
  248. static kbf::http::Client *client;
  249. void wifiModeSwitchMaster() {
  250. using namespace kbf;
  251. eventGroup = rtos::EventGroup();
  252. auto ap = wifi::AP::create(
  253. KBF_TEST_WIFI_DUAL_MASTER_SSID,
  254. KBF_TEST_WIFI_PASS,
  255. net::IP("192.168.1.1")
  256. );
  257. ap->onConnect = {[](wifi::AP::STA &) {
  258. wifi::getSTA()->connect(KBF_TEST_WIFI_DUAL_SLAVE_SSID, KBF_TEST_WIFI_PASS);
  259. }};
  260. delete client;
  261. client = new http::Client();
  262. auto sta = wifi::STA::create();
  263. sta->onIp = {[]() {
  264. wifi::stopAP();
  265. kbf::sleep(1000);
  266. auto response = client->get("http://192.168.2.1/");
  267. TEST_ASSERT_EQUAL(200, response->status);
  268. eventGroup.setBit(0);
  269. }};
  270. wifi::start(ap, sta);
  271. eventGroup.waitForBit(0);
  272. wifi::stop();
  273. }
  274. class TestController : public kbf::WebService::Controller {
  275. public:
  276. TestController() : kbf::WebService::Controller("/") {}
  277. kbf::http::Response get(const kbf::http::Request &request) override {
  278. eventGroup.setBit(0);
  279. if (eventGroup.getBit(1)) return kbf::http::Response("", 501);
  280. if (!eventGroup.getBit(2)) return kbf::http::Response("", 502);
  281. return kbf::http::Response("", 200);
  282. }
  283. };
  284. void wifiModeSwitchSlave() {
  285. using namespace kbf;
  286. using namespace std;
  287. eventGroup = rtos::EventGroup();
  288. auto ap = wifi::AP::create(
  289. KBF_TEST_WIFI_DUAL_SLAVE_SSID,
  290. KBF_TEST_WIFI_PASS,
  291. net::IP("192.168.2.1")
  292. );
  293. ap->onDisconnect = {[](wifi::AP::STA &) {
  294. eventGroup.setBit(1);
  295. }};
  296. auto sta = wifi::STA::create();
  297. sta->onReconnecting = {[](int, int) {
  298. return false;
  299. }};
  300. sta->onDisconnect = {[]() {
  301. eventGroup.setBit(2);
  302. }};
  303. wifi::start(ap, sta);
  304. auto webService = WebService();
  305. webService.controller<TestController>();
  306. webService.start();
  307. sta->connect(KBF_TEST_WIFI_DUAL_MASTER_SSID, KBF_TEST_WIFI_PASS);
  308. eventGroup.waitForBit(0);
  309. kbf::sleep(1000);
  310. wifi::stop();
  311. }
  312. TEST_CASE_MULTIPLE_DEVICES("WiFi legacy mode switching", "[kbf_wifi_legacy]", wifiModeSwitchMaster, wifiModeSwitchSlave)
  313. TEST_CASE("WiFi legacy state handling", "[kbf_wifi_legacy]") {
  314. using namespace kbf;
  315. wifi::stop();
  316. TEST_ASSERT_FALSE(wifi::isRunning())
  317. wifi::start();
  318. kbf::sleep(1000);
  319. TEST_ASSERT_TRUE(wifi::isRunning())
  320. wifi::stop();
  321. kbf::sleep(1000);
  322. TEST_ASSERT_FALSE(wifi::isRunning())
  323. wifi::stop();
  324. }