123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #include "kbf/wifi.h"
- #include "kbf/macros.h"
- #include "kbf/nvs.h"
- using namespace kbf;
- wifi::STA::STA() {
- ESP_LOGD(TAG, "STA()");
- config.pmf_cfg = {true, false};
- }
- wifi::STA::~STA() {
- ESP_LOGD(TAG, "~STA()");
- unregisterEventHandlers();
- }
- shared_ptr<wifi::STA> wifi::STA::create() {
- return shared_ptr<STA>(new STA());
- }
- void wifi::STA::registerEventHandlers() {
- ESP_LOGD(TAG, "registerEventHandlers");
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_STA_START, &handleStart, this, &startHandler));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_STA_STOP, &handleStop, this, &stopHandler));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &handleConnect, this, &connectHandler));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &handleDisconnect, this, &disconnectHandler));
- CHECK(esp_event_handler_instance_register(
- IP_EVENT, IP_EVENT_STA_GOT_IP, &handleGotIp, this, &gotIpHandler));
- }
- void wifi::STA::unregisterEventHandlers() {
- ESP_LOGD(TAG, "unregisterEventHandlers");
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, &startHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, &stopHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &connectHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnectHandler));
- CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &gotIpHandler));
- // TODO why are these necessary? shouldn't instance unregistration be enough?
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, &handleStart));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, &handleStop));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &handleConnect));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &handleDisconnect));
- CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &handleGotIp));
- }
- void wifi::STA::connect(const string &ssid, const string &password, int maxRetryAttempts) {
- ESP_LOGI(TAG, "connect()");
- wifi_config_t wifiConfig = {.sta = config};
- std::copy(ssid.begin(), ssid.end() + 1, std::begin(wifiConfig.sta.ssid));
- std::copy(password.begin(), password.end() + 1, std::begin(wifiConfig.sta.password));
- retryNum = 0;
- retryMax = maxRetryAttempts;
- ESP_LOGD(TAG, "SSID: \"%s\"; pass: \"%s\"; maxRetry = %d", wifiConfig.sta.ssid, wifiConfig.sta.password,
- maxRetryAttempts);
- // TODO check station_example_main.c; they use ESP_IF_WIFI_STA which doesn't compile in C++
- CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifiConfig));
- CHECK(esp_wifi_connect());
- }
- void wifi::STA::disconnect() {
- ESP_LOGI(TAG, "disconnect()");
- retryMax = 0;
- CHECK(esp_wifi_disconnect());
- }
- void wifi::STA::handleStart(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGD(TAG, "handleStart()");
- auto instance = static_cast<STA *>(arg);
- if (instance->onStart) instance->onStart();
- }
- void wifi::STA::handleStop(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGD(TAG, "handleStop()");
- auto instance = static_cast<STA *>(arg);
- if (instance->onStop) instance->onStop();
- }
- void wifi::STA::handleConnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGD(TAG, "handleConnect()");
- auto instance = static_cast<STA *>(arg);
- if (instance->onConnect) instance->onConnect();
- }
- void wifi::STA::handleDisconnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGD(TAG, "handleDisconnect()");
- auto instance = static_cast<STA *>(arg);
- if (instance->retryNum++ < instance->retryMax) {
- if (instance->onReconnecting && !instance->onReconnecting(instance->retryNum, instance->retryMax)) {
- ESP_LOGI(TAG, "reconnect attempt cancelled");
- if (instance->onDisconnect) instance->onDisconnect();
- } else {
- ESP_LOGI(TAG, "reconnecting, attempt %d / %d", instance->retryNum, instance->retryMax);
- esp_wifi_connect();
- }
- } else {
- ESP_LOGI(TAG, "disconnected");
- if (instance->onDisconnect) instance->onDisconnect();
- }
- }
- void wifi::STA::handleGotIp(void *arg, esp_event_base_t baseType, int32_t eventId, void *pEventData) {
- ESP_LOGD(TAG, "handleGotIp()");
- auto instance = static_cast<STA *>(arg);
- auto eventData = static_cast<ip_event_got_ip_t *>(pEventData);
- instance->m_ip = net::IP(eventData->ip_info.ip);
- if (instance->onIp) instance->onIp();
- instance->retryNum = 0;
- }
|