123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- #include "kbf/wifi.h"
- #include "kbf/macros.h"
- kbf::wifi::AP::AP(string ssid, string password) : ssid(std::move(ssid)), password(std::move(password)) {
- ESP_LOGI(TAG, "AP()");
- std::copy(this->ssid.begin(), this->ssid.end() + 1, std::begin(config.ssid));
- std::copy(this->password.begin(), this->password.end() + 1, std::begin(config.password));
- config.ssid_len = this->ssid.length();
- config.max_connection = 5; // TODO use Kconfig
- config.authmode = WIFI_AUTH_WPA2_PSK; // TODO use AUTH_OPEN if no password is provided
- }
- kbf::wifi::AP::~AP() {
- ESP_LOGI(TAG, "~AP()");
- unregisterEventHandlers();
- }
- shared_ptr<kbf::wifi::AP> kbf::wifi::AP::create(const string &ssid, const string &password) {
- if (password.length() < 8) {
- ABORT("WiFi password needs to be at least 8 characters long.");
- }
- auto ap = new AP(ssid, password);
- return shared_ptr<AP>(ap);
- }
- void kbf::wifi::AP::registerEventHandlers() {
- ESP_LOGI(TAG, "registerEventHandlers");
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_AP_START, &handleStart, this, &startHandler
- ));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_AP_STOP, &handleStop, this, &stopHandler
- ));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &handleConnect, this, &connectHandler
- ));
- CHECK(esp_event_handler_instance_register(
- WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &handleDisconnect, this, &disconnectHandler
- ));
- }
- void kbf::wifi::AP::unregisterEventHandlers() {
- ESP_LOGI(TAG, "unregisterEventHandlers");
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, &startHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, &stopHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &connectHandler));
- CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &disconnectHandler));
- // TODO why are these necessary? shouldn't instance unregistration be enough?
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, &handleStart));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, &handleStop));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &handleConnect));
- CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &handleDisconnect));
- }
- void kbf::wifi::AP::handleStart(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGI(TAG, "handleStart");
- auto ap = static_cast<AP *>(arg);
- if (ap->onStart) ap->onStart(*ap);
- }
- void kbf::wifi::AP::handleStop(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
- ESP_LOGI(TAG, "handleStop");
- auto ap = static_cast<AP *>(arg);
- if (ap->onStop) ap->onStop(*ap);
- }
- void kbf::wifi::AP::handleConnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *eventData) {
- ESP_LOGI(TAG, "handleConnect");
- auto ap = static_cast<AP *>(arg);
- auto sta = STA(*static_cast<wifi_event_ap_staconnected_t *>(eventData));
- if (ap->stations.find(sta.aid) != ap->stations.end()) {
- ESP_LOGE(TAG, "sta.aid already in use: %d", sta.aid);
- ABORT("debug me");
- }
- if (ap->onConnect) ap->onConnect(*ap, sta);
- ap->stations.insert(std::pair<int, STA>(sta.aid, sta));
- }
- void kbf::wifi::AP::handleDisconnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *eventData) {
- ESP_LOGI(TAG, "handleDisconnect");
- auto ap = static_cast<AP *>(arg);
- auto aid = (static_cast<wifi_event_ap_stadisconnected_t *>(eventData))->aid;
- if (ap->onDisconnect) ap->onDisconnect(*ap, ap->stations[aid]);
- ap->stations.erase(aid);
- }
- kbf::wifi::AP::STA::STA(wifi_event_ap_staconnected_t &eventData) : aid(eventData.aid), mac(eventData.mac) {}
|