123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include <vector>
- #include <esp_log.h>
- #include "kbf.h"
- #include "kbf/wifi.h"
- #include "kbf/rtos.h"
- #include "kbf/web_service.h"
- #include "kbf/http/client.h"
- #include "kbf/http/exception.h"
- #include <unity.h>
- #define MASTER_SSID "kbf_test_wifi_master"
- #define SLAVE_SSID "kbf_test_wifi_slave"
- #define WIFI_PASS "Pas$w0Rd1337"
- #define MASTER_AP "http://192.168.1.1/ping"
- #define MASTER_STA "http://192.168.2.2/ping"
- #define SLAVE_AP "http://192.168.2.1/ping"
- #define SLAVE_STA "http://192.168.1.2/ping"
- #define PING_RESPONSE "P0NG"
- #define PING 0
- using namespace kbf;
- using std::vector;
- static rtos::EventGroup event; // NOLINT(cert-err58-cpp)
- static string lastPingID;
- class PingController : public WebService::Controller {
- public:
- PingController() : Controller("/ping") {}
- protected:
- http::Response get(const http::Request &request) override {
- lastPingID = request.query.at("id");
- ESP_LOGI("PingController", "%s", lastPingID.c_str());
- event.setBit(PING);
- return http::Response(PING_RESPONSE);
- }
- };
- static auto client = new http::Client(); // NOLINT(cert-err58-cpp)
- static void pingOK(const string &url, int id) {
- const auto response = client->get(url + "?id=" + std::to_string(id));
- TEST_ASSERT_EQUAL(200, response->status);
- TEST_ASSERT_EQUAL_STRING(PING_RESPONSE, response->body.c_str());
- }
- static void pingFail(const string &url) {
- try {
- client->get(url);
- } catch (http::exception::ConnectionError &) {
- return;
- }
- TEST_FAIL();
- }
- static void waitForPing(int id) {
- ESP_LOGI(__func__, "%d", id);
- event.waitForBit(PING);
- event.clear();
- TEST_ASSERT_EQUAL_STRING(std::to_string(id).c_str(), lastPingID.c_str());
- }
- static void fail() {
- TEST_FAIL();
- }
- static void fail(wifi::STA &) {
- TEST_FAIL();
- }
- void master() {
- constexpr const char *const TAG = __func__;
- auto pingService = WebService();
- pingService.controller<PingController>();
- ESP_LOGI(TAG, "0 - starting AP");
- wifi::ap::onDisconnect = fail;
- wifi::sta::onDisconnect = fail;
- wifi::ap::start(MASTER_SSID, WIFI_PASS);
- TEST_ASSERT_EQUAL(wifi::Mode::AP, wifi::mode());
- pingService.start();
- ESP_LOGI(TAG, "1 - master is AP, slave is STA");
- waitForPing(0);
- pingOK(SLAVE_STA, 1);
- pingFail(SLAVE_AP);
- ESP_LOGI(TAG, "2 - master is AP, slave switches to DUAL");
- waitForPing(2);
- pingOK(SLAVE_STA, 3);
- pingFail(SLAVE_AP);
- ESP_LOGI(TAG, "3 - master switches to DUAL, slave is DUAL");
- wifi::sta::start();
- TEST_ASSERT_EQUAL(wifi::Mode::DUAL, wifi::mode());
- wifi::sta::connect(SLAVE_SSID, WIFI_PASS);
- pingOK(SLAVE_STA, 4);
- pingOK(SLAVE_AP, 5);
- waitForPing(6);
- waitForPing(7);
- ESP_LOGI(TAG, "4 - master switches to STA, slave is DUAL");
- kbf::sleep(100); // wait for slave to disable onDisconnect
- wifi::ap::stop();
- TEST_ASSERT_EQUAL(wifi::Mode::STA, wifi::mode());
- pingOK(SLAVE_AP, 8);
- waitForPing(9);
- pingFail(SLAVE_STA);
- ESP_LOGI(TAG, "5 - master is STA, slave switches to AP");
- waitForPing(10);
- pingOK(SLAVE_AP, 11);
- pingFail(SLAVE_STA);
- ESP_LOGI(TAG, "finished");
- wifi::sta::onDisconnect = nullptr;
- kbf::sleep(100); // wait for slave to disable onDisconnect
- pingService.stop();
- wifi::stop();
- TEST_ASSERT_EQUAL(wifi::Mode::OFF, wifi::mode());
- }
- void slave() {
- constexpr const char *const TAG = __func__;
- auto pingService = WebService();
- pingService.controller<PingController>();
- ESP_LOGI(TAG, "0 - starting STA");
- wifi::ap::onDisconnect = fail;
- wifi::sta::onDisconnect = fail;
- wifi::sta::start();
- TEST_ASSERT_EQUAL(wifi::Mode::STA, wifi::mode());
- pingService.start();
- ESP_LOGI(TAG, "1 - master: AP, slave is STA");
- wifi::sta::connect(MASTER_SSID, WIFI_PASS);
- pingOK(MASTER_AP, 0);
- waitForPing(1);
- pingFail(MASTER_STA);
- ESP_LOGI(TAG, "2 - master is AP, slave switches to DUAL");
- wifi::ap::start(SLAVE_SSID, WIFI_PASS);
- TEST_ASSERT_EQUAL(wifi::Mode::DUAL, wifi::mode());
- pingOK(MASTER_AP, 2);
- waitForPing(3);
- pingFail(MASTER_STA);
- ESP_LOGI(TAG, "3 - master switches to DUAL, slave is DUAL");
- waitForPing(4);
- waitForPing(5);
- pingOK(MASTER_STA, 6);
- pingOK(MASTER_AP, 7);
- ESP_LOGI(TAG, "4 - master switches to STA, slave is DUAL");
- wifi::sta::onDisconnect = nullptr;
- waitForPing(8);
- pingOK(MASTER_STA, 9);
- pingFail(MASTER_AP);
- ESP_LOGI(TAG, "5 - master is STA, slave switches to AP");
- wifi::sta::stop();
- TEST_ASSERT_EQUAL(wifi::Mode::AP, wifi::mode());
- pingOK(MASTER_STA, 10);
- waitForPing(11);
- pingFail(MASTER_AP);
- ESP_LOGI(TAG, "finished");
- wifi::ap::onDisconnect = nullptr;
- kbf::sleep(100); // wait for master to disable onDisconnect
- pingService.stop();
- wifi::stop();
- TEST_ASSERT_EQUAL(wifi::Mode::OFF, wifi::mode());
- }
- TEST_CASE_MULTIPLE_DEVICES("WiFi mode STA to DUAL", "[kbf_wifi]", master, slave);
|