network_manager.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. Copyright (c) 2017-2019 Tony Pottier
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. @file wifi_manager.c
  19. @author Tony Pottier
  20. @brief Defines all functions necessary for esp32 to connect to a wifi/scan wifis
  21. Contains the freeRTOS task and all necessary support
  22. @see https://idyl.io
  23. @see https://github.com/tonyp7/esp32-wifi-manager
  24. */
  25. #include "network_manager.h"
  26. #include <stdbool.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include "network_ethernet.h"
  31. #include "network_status.h"
  32. #include "network_wifi.h"
  33. #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
  34. #include "esp_log.h"
  35. #include "platform_esp32.h"
  36. #include "esp_system.h"
  37. #include "freertos/FreeRTOS.h"
  38. #include "freertos/task.h"
  39. #include "esp_netif.h"
  40. #include "cJSON.h"
  41. #include "cmd_system.h"
  42. #include "esp_app_format.h"
  43. #include "esp_event.h"
  44. #include "esp_ota_ops.h"
  45. #include "esp_wifi.h"
  46. #include "esp_wifi_types.h"
  47. #include "lwip/api.h"
  48. #include "lwip/err.h"
  49. #include "lwip/ip4_addr.h"
  50. #include "lwip/netdb.h"
  51. #include "mdns.h"
  52. #include "messaging.h"
  53. #include "state_machine.h"
  54. #include "platform_config.h"
  55. #include "trace.h"
  56. #include "accessors.h"
  57. #include "globdefs.h"
  58. #include "http_server_handlers.h"
  59. #ifndef STR_OR_BLANK
  60. #define STR_OR_BLANK(p) p == NULL ? "" : p
  61. #endif
  62. //EventGroupHandle_t wifi_manager_event_group;
  63. void (**cb_ptr_arr)(void*) = NULL;
  64. /* @brief tag used for ESP serial console messages */
  65. //static const char TAG[] = "network_manager";
  66. /* @brief indicate that the ESP32 is currently connected. */
  67. const int WIFI_MANAGER_WIFI_CONNECTED_BIT = BIT0;
  68. const int WIFI_MANAGER_AP_STA_CONNECTED_BIT = BIT1;
  69. /* @brief Set automatically once the SoftAP is started */
  70. const int WIFI_MANAGER_AP_STARTED_BIT = BIT2;
  71. /* @brief When set, means a client requested to connect to an access point.*/
  72. const int WIFI_MANAGER_REQUEST_STA_CONNECT_BIT = BIT3;
  73. /* @brief This bit is set automatically as soon as a connection was lost */
  74. const int WIFI_MANAGER_STA_DISCONNECT_BIT = BIT4;
  75. /* @brief When set, means the wifi manager attempts to restore a previously saved connection at startup. */
  76. const int WIFI_MANAGER_REQUEST_RESTORE_STA_BIT = BIT5;
  77. /* @brief When set, means a client requested to disconnect from currently connected AP. */
  78. const int WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT = BIT6;
  79. /* @brief When set, means a scan is in progress */
  80. const int WIFI_MANAGER_SCAN_BIT = BIT7;
  81. /* @brief When set, means user requested for a disconnect */
  82. const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT = BIT8;
  83. /* @brief When set, means user requested connecting to a new network and it failed */
  84. const int WIFI_MANAGER_REQUEST_STA_CONNECT_FAILED_BIT = BIT9;
  85. /* @brief task handle for the main wifi_manager task */
  86. void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*)) {
  87. if (cb_ptr_arr && message_code < MESSAGE_CODE_COUNT) {
  88. cb_ptr_arr[message_code] = func_ptr;
  89. }
  90. }