ap.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "kbf/wifi.h"
  2. #include "kbf/macros.h"
  3. kbf::wifi::AP::AP(string ssid, string password) : ssid(std::move(ssid)), password(std::move(password)) {
  4. ESP_LOGI(TAG, "AP()");
  5. std::copy(this->ssid.begin(), this->ssid.end() + 1, std::begin(config.ssid));
  6. std::copy(this->password.begin(), this->password.end() + 1, std::begin(config.password));
  7. config.ssid_len = this->ssid.length();
  8. config.max_connection = 5; // TODO use Kconfig
  9. config.authmode = WIFI_AUTH_WPA2_PSK; // TODO use AUTH_OPEN if no password is provided
  10. }
  11. kbf::wifi::AP::~AP() {
  12. ESP_LOGI(TAG, "~AP()");
  13. unregisterEventHandlers();
  14. }
  15. shared_ptr<kbf::wifi::AP> kbf::wifi::AP::create(const string &ssid, const string &password) {
  16. if (password.length() < 8) {
  17. ABORT("WiFi password needs to be at least 8 characters long.");
  18. }
  19. auto ap = new AP(ssid, password);
  20. return shared_ptr<AP>(ap);
  21. }
  22. void kbf::wifi::AP::registerEventHandlers() {
  23. ESP_LOGI(TAG, "registerEventHandlers");
  24. CHECK(esp_event_handler_instance_register(
  25. WIFI_EVENT, WIFI_EVENT_AP_START, &handleStart, this, &startHandler
  26. ));
  27. CHECK(esp_event_handler_instance_register(
  28. WIFI_EVENT, WIFI_EVENT_AP_STOP, &handleStop, this, &stopHandler
  29. ));
  30. CHECK(esp_event_handler_instance_register(
  31. WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &handleConnect, this, &connectHandler
  32. ));
  33. CHECK(esp_event_handler_instance_register(
  34. WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &handleDisconnect, this, &disconnectHandler
  35. ));
  36. }
  37. void kbf::wifi::AP::unregisterEventHandlers() {
  38. ESP_LOGI(TAG, "unregisterEventHandlers");
  39. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, &startHandler));
  40. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, &stopHandler));
  41. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &connectHandler));
  42. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &disconnectHandler));
  43. // TODO why are these necessary? shouldn't instance unregistration be enough?
  44. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_START, &handleStart));
  45. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STOP, &handleStop));
  46. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STACONNECTED, &handleConnect));
  47. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_AP_STADISCONNECTED, &handleDisconnect));
  48. }
  49. void kbf::wifi::AP::handleStart(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  50. ESP_LOGI(TAG, "handleStart");
  51. auto ap = static_cast<AP *>(arg);
  52. if (ap->onStart) ap->onStart(*ap);
  53. }
  54. void kbf::wifi::AP::handleStop(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  55. ESP_LOGI(TAG, "handleStop");
  56. auto ap = static_cast<AP *>(arg);
  57. if (ap->onStop) ap->onStop(*ap);
  58. }
  59. void kbf::wifi::AP::handleConnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *eventData) {
  60. ESP_LOGI(TAG, "handleConnect");
  61. auto ap = static_cast<AP *>(arg);
  62. auto sta = STA(*static_cast<wifi_event_ap_staconnected_t *>(eventData));
  63. if (ap->stations.find(sta.aid) != ap->stations.end()) {
  64. ESP_LOGE(TAG, "sta.aid already in use: %d", sta.aid);
  65. ABORT("debug me");
  66. }
  67. if (ap->onConnect) ap->onConnect(*ap, sta);
  68. ap->stations.insert(std::pair<int, STA>(sta.aid, sta));
  69. }
  70. void kbf::wifi::AP::handleDisconnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *eventData) {
  71. ESP_LOGI(TAG, "handleDisconnect");
  72. auto ap = static_cast<AP *>(arg);
  73. auto aid = (static_cast<wifi_event_ap_stadisconnected_t *>(eventData))->aid;
  74. if (ap->onDisconnect) ap->onDisconnect(*ap, ap->stations[aid]);
  75. ap->stations.erase(aid);
  76. }
  77. kbf::wifi::AP::STA::STA(wifi_event_ap_staconnected_t &eventData) : aid(eventData.aid), mac(eventData.mac) {}