sta.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include "kbf/wifi.h"
  2. #include "kbf/macros.h"
  3. #include "kbf/nvs.h"
  4. using namespace kbf;
  5. wifi::STA::STA() {
  6. ESP_LOGD(TAG, "STA()");
  7. config.pmf_cfg = {true, false};
  8. }
  9. wifi::STA::~STA() {
  10. ESP_LOGD(TAG, "~STA()");
  11. unregisterEventHandlers();
  12. }
  13. shared_ptr<wifi::STA> wifi::STA::create() {
  14. return shared_ptr<STA>(new STA());
  15. }
  16. void wifi::STA::registerEventHandlers() {
  17. ESP_LOGD(TAG, "registerEventHandlers");
  18. CHECK(esp_event_handler_instance_register(
  19. WIFI_EVENT, WIFI_EVENT_STA_START, &handleStart, this, &startHandler));
  20. CHECK(esp_event_handler_instance_register(
  21. WIFI_EVENT, WIFI_EVENT_STA_STOP, &handleStop, this, &stopHandler));
  22. CHECK(esp_event_handler_instance_register(
  23. WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &handleConnect, this, &connectHandler));
  24. CHECK(esp_event_handler_instance_register(
  25. WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &handleDisconnect, this, &disconnectHandler));
  26. CHECK(esp_event_handler_instance_register(
  27. IP_EVENT, IP_EVENT_STA_GOT_IP, &handleGotIp, this, &gotIpHandler));
  28. }
  29. void wifi::STA::unregisterEventHandlers() {
  30. ESP_LOGD(TAG, "unregisterEventHandlers");
  31. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, &startHandler));
  32. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, &stopHandler));
  33. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &connectHandler));
  34. CHECK(esp_event_handler_instance_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnectHandler));
  35. CHECK(esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &gotIpHandler));
  36. // TODO why are these necessary? shouldn't instance unregistration be enough?
  37. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_START, &handleStart));
  38. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_STOP, &handleStop));
  39. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_CONNECTED, &handleConnect));
  40. CHECK(esp_event_handler_unregister(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &handleDisconnect));
  41. CHECK(esp_event_handler_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, &handleGotIp));
  42. }
  43. void wifi::STA::connect(const string &ssid, const string &password, int maxRetryAttempts) {
  44. ESP_LOGI(TAG, "connect()");
  45. wifi_config_t wifiConfig = {.sta = config};
  46. std::copy(ssid.begin(), ssid.end() + 1, std::begin(wifiConfig.sta.ssid));
  47. std::copy(password.begin(), password.end() + 1, std::begin(wifiConfig.sta.password));
  48. retryNum = 0;
  49. retryMax = maxRetryAttempts;
  50. ESP_LOGD(TAG, "SSID: \"%s\"; pass: \"%s\"; maxRetry = %d", wifiConfig.sta.ssid, wifiConfig.sta.password,
  51. maxRetryAttempts);
  52. // TODO check station_example_main.c; they use ESP_IF_WIFI_STA which doesn't compile in C++
  53. CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifiConfig));
  54. CHECK(esp_wifi_connect());
  55. }
  56. void wifi::STA::disconnect() {
  57. ESP_LOGI(TAG, "disconnect()");
  58. retryMax = 0;
  59. CHECK(esp_wifi_disconnect());
  60. }
  61. void wifi::STA::handleStart(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  62. ESP_LOGD(TAG, "handleStart()");
  63. auto instance = static_cast<STA *>(arg);
  64. if (instance->onStart) instance->onStart();
  65. }
  66. void wifi::STA::handleStop(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  67. ESP_LOGD(TAG, "handleStop()");
  68. auto instance = static_cast<STA *>(arg);
  69. if (instance->onStop) instance->onStop();
  70. }
  71. void wifi::STA::handleConnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  72. ESP_LOGD(TAG, "handleConnect()");
  73. auto instance = static_cast<STA *>(arg);
  74. if (instance->onConnect) instance->onConnect();
  75. }
  76. void wifi::STA::handleDisconnect(void *arg, esp_event_base_t baseType, int32_t eventId, void *) {
  77. ESP_LOGD(TAG, "handleDisconnect()");
  78. auto instance = static_cast<STA *>(arg);
  79. if (instance->retryNum++ < instance->retryMax) {
  80. if (instance->onReconnecting && !instance->onReconnecting(instance->retryNum, instance->retryMax)) {
  81. ESP_LOGI(TAG, "reconnect attempt cancelled");
  82. if (instance->onDisconnect) instance->onDisconnect();
  83. } else {
  84. ESP_LOGI(TAG, "reconnecting, attempt %d / %d", instance->retryNum, instance->retryMax);
  85. esp_wifi_connect();
  86. }
  87. } else {
  88. ESP_LOGI(TAG, "disconnected");
  89. if (instance->onDisconnect) instance->onDisconnect();
  90. }
  91. }
  92. void wifi::STA::handleGotIp(void *arg, esp_event_base_t baseType, int32_t eventId, void *pEventData) {
  93. ESP_LOGD(TAG, "handleGotIp()");
  94. auto instance = static_cast<STA *>(arg);
  95. auto eventData = static_cast<ip_event_got_ip_t *>(pEventData);
  96. instance->m_ip = net::IP(eventData->ip_info.ip);
  97. if (instance->onIp) instance->onIp();
  98. instance->retryNum = 0;
  99. }