network_manager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 network_manager.h
  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. #ifndef WIFI_MANAGER_H_INCLUDED
  26. #define WIFI_MANAGER_H_INCLUDED
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. #include "esp_system.h"
  31. #include "esp_wifi.h"
  32. #include "esp_wifi_types.h"
  33. #include "squeezelite-ota.h"
  34. #include "cJSON.h"
  35. #include "esp_eth.h"
  36. #include "freertos/event_groups.h"
  37. #include "state_machine.h"
  38. #include "state_machine.h"
  39. /**
  40. * @brief Defines the maximum size of a SSID name. 32 is IEEE standard.
  41. * @warning limit is also hard coded in wifi_config_t. Never extend this value.
  42. */
  43. #define MAX_SSID_SIZE 32
  44. /**
  45. * @brief Defines the maximum size of a WPA2 passkey. 64 is IEEE standard.
  46. * @warning limit is also hard coded in wifi_config_t. Never extend this value.
  47. */
  48. #define MAX_PASSWORD_SIZE 64
  49. #define MAX_COMMAND_LINE_SIZE 201
  50. /**
  51. * @brief Defines the maximum number of access points that can be scanned.
  52. *
  53. * To save memory and avoid nasty out of memory errors,
  54. * we can limit the number of APs detected in a wifi scan.
  55. */
  56. #define MAX_AP_NUM 15
  57. /**
  58. * @brief Defines when a connection is lost/attempt to connect is made, how many retries should be made before giving up.
  59. * Setting it to 2 for instance means there will be 3 attempts in total (original request + 2 retries)
  60. */
  61. #define WIFI_MANAGER_MAX_RETRY CONFIG_WIFI_MANAGER_MAX_RETRY
  62. /** @brief Defines the task priority of the wifi_manager.
  63. *
  64. * Tasks spawn by the manager will have a priority of WIFI_MANAGER_TASK_PRIORITY-1.
  65. * For this particular reason, minimum task priority is 1. It it highly not recommended to set
  66. * it to 1 though as the sub-tasks will now have a priority of 0 which is the priority
  67. * of freeRTOS' idle task.
  68. */
  69. #define WIFI_MANAGER_TASK_PRIORITY CONFIG_WIFI_MANAGER_TASK_PRIORITY
  70. /** @brief Defines the auth mode as an access point
  71. * Value must be of type wifi_auth_mode_t
  72. * @see esp_wifi_types.h
  73. * @warning if set to WIFI_AUTH_OPEN, passowrd me be empty. See DEFAULT_AP_PASSWORD.
  74. */
  75. #define AP_AUTHMODE WIFI_AUTH_WPA2_PSK
  76. /** @brief Defines visibility of the access point. 0: visible AP. 1: hidden */
  77. #define DEFAULT_AP_SSID_HIDDEN 0
  78. /** @brief Defines access point's name. Default value: esp32. Run 'make menuconfig' to setup your own value or replace here by a string */
  79. #define DEFAULT_AP_SSID CONFIG_DEFAULT_AP_SSID
  80. /** @brief Defines access point's password.
  81. * @warning In the case of an open access point, the password must be a null string "" or "\0" if you want to be verbose but waste one byte.
  82. * In addition, the AP_AUTHMODE must be WIFI_AUTH_OPEN
  83. */
  84. #define DEFAULT_AP_PASSWORD CONFIG_DEFAULT_AP_PASSWORD
  85. /** @brief Defines access point's bandwidth.
  86. * Value: WIFI_BW_HT20 for 20 MHz or WIFI_BW_HT40 for 40 MHz
  87. * 20 MHz minimize channel interference but is not suitable for
  88. * applications with high data speeds
  89. */
  90. #define DEFAULT_AP_BANDWIDTH WIFI_BW_HT20
  91. /** @brief Defines access point's channel.
  92. * Channel selection is only effective when not connected to another AP.
  93. * Good practice for minimal channel interference to use
  94. * For 20 MHz: 1, 6 or 11 in USA and 1, 5, 9 or 13 in most parts of the world
  95. * For 40 MHz: 3 in USA and 3 or 11 in most parts of the world
  96. */
  97. #define DEFAULT_AP_CHANNEL CONFIG_DEFAULT_AP_CHANNEL
  98. /** @brief Defines the access point's default IP address. Default: "10.10.0.1 */
  99. #define DEFAULT_AP_IP CONFIG_DEFAULT_AP_IP
  100. /** @brief Defines the access point's gateway. This should be the same as your IP. Default: "10.10.0.1" */
  101. #define DEFAULT_AP_GATEWAY CONFIG_DEFAULT_AP_GATEWAY
  102. /** @brief Defines the access point's netmask. Default: "255.255.255.0" */
  103. #define DEFAULT_AP_NETMASK CONFIG_DEFAULT_AP_NETMASK
  104. /** @brief Defines access point's maximum number of clients. Default: 4 */
  105. #define DEFAULT_AP_MAX_CONNECTIONS CONFIG_DEFAULT_AP_MAX_CONNECTIONS
  106. /** @brief Defines access point's beacon interval. 100ms is the recommended default. */
  107. #define DEFAULT_AP_BEACON_INTERVAL CONFIG_DEFAULT_AP_BEACON_INTERVAL
  108. /** @brief Defines if esp32 shall run both AP + STA when connected to another AP.
  109. * Value: 0 will have the own AP always on (APSTA mode)
  110. * Value: 1 will turn off own AP when connected to another AP (STA only mode when connected)
  111. * Turning off own AP when connected to another AP minimize channel interference and increase throughput
  112. */
  113. #define DEFAULT_STA_ONLY 1
  114. /** @brief Defines if wifi power save shall be enabled.
  115. * Value: WIFI_PS_NONE for full power (wifi modem always on)
  116. * Value: WIFI_PS_MODEM for power save (wifi modem sleep periodically)
  117. * Note: Power save is only effective when in STA only mode
  118. */
  119. #define DEFAULT_STA_POWER_SAVE WIFI_PS_MIN_MODEM
  120. /**
  121. * @brief Defines the maximum length in bytes of a JSON representation of an access point.
  122. *
  123. * maximum ap string length with full 32 char ssid: 75 + \\n + \0 = 77\n
  124. * example: {"ssid":"abcdefghijklmnopqrstuvwxyz012345","chan":12,"rssi":-100,"auth":4},\n
  125. * BUT: we need to escape JSON. Imagine a ssid full of \" ? so it's 32 more bytes hence 77 + 32 = 99.\n
  126. * this is an edge case but I don't think we should crash in a catastrophic manner just because
  127. * someone decided to have a funny wifi name.
  128. */
  129. #define JSON_ONE_APP_SIZE 99
  130. /**
  131. * @brief Defines the complete list of all messages that the wifi_manager can process.
  132. *
  133. * Some of these message are events ("EVENT"), and some of them are action ("ORDER")
  134. * Each of these messages can trigger a callback function and each callback function is stored
  135. * in a function pointer array for convenience. Because of this behavior, it is extremely important
  136. * to maintain a strict sequence and the top level special element 'MESSAGE_CODE_COUNT'
  137. *
  138. * @see wifi_manager_set_callback
  139. */
  140. typedef enum message_code_t {
  141. NONE = 0,
  142. ORDER_START_HTTP_SERVER = 1,
  143. ORDER_STOP_HTTP_SERVER = 2,
  144. ORDER_START_DNS_SERVICE = 3,
  145. ORDER_STOP_DNS_SERVICE = 4,
  146. ORDER_START_WIFI_SCAN = 5,
  147. ORDER_LOAD_AND_RESTORE_STA = 6,
  148. ORDER_CONNECT_STA = 7,
  149. ORDER_DISCONNECT_STA = 8,
  150. ORDER_START_AP = 9,
  151. ORDER_START_HTTP = 10,
  152. ORDER_START_DNS_HIJACK = 11,
  153. EVENT_STA_DISCONNECTED = 12,
  154. EVENT_SCAN_DONE = 13,
  155. EVENT_GOT_IP = 14,
  156. ORDER_RESTART_OTA = 15,
  157. ORDER_RESTART_RECOVERY = 16,
  158. ORDER_RESTART_OTA_URL = 17,
  159. ORDER_RESTART = 18,
  160. ORDER_UPDATE_STATUS = 19,
  161. EVENT_ETH_GOT_IP = 20,
  162. EVENT_ETH_TIMEOUT = 21,
  163. EVENT_ETH_LINK_UP = 22,
  164. EVENT_ETH_LINK_DOWN = 23,
  165. MESSAGE_CODE_COUNT = 24 /* important for the callback array */
  166. }message_code_t;
  167. /* @brief indicate that the ESP32 is currently connected. */
  168. extern const int WIFI_MANAGER_WIFI_CONNECTED_BIT;
  169. extern const int WIFI_MANAGER_AP_STA_CONNECTED_BIT;
  170. /* @brief Set automatically once the SoftAP is started */
  171. extern const int WIFI_MANAGER_AP_STARTED_BIT;
  172. /* @brief When set, means a client requested to connect to an access point.*/
  173. extern const int WIFI_MANAGER_REQUEST_STA_CONNECT_BIT;
  174. /* @brief This bit is set automatically as soon as a connection was lost */
  175. extern const int WIFI_MANAGER_STA_DISCONNECT_BIT ;
  176. /* @brief When set, means the wifi manager attempts to restore a previously saved connection at startup. */
  177. extern const int WIFI_MANAGER_REQUEST_RESTORE_STA_BIT ;
  178. /* @brief When set, means a client requested to disconnect from currently connected AP. */
  179. extern const int WIFI_MANAGER_REQUEST_WIFI_DISCONNECT_BIT ;
  180. /* @brief When set, means a scan is in progress */
  181. extern const int WIFI_MANAGER_SCAN_BIT ;
  182. /* @brief When set, means user requested for a disconnect */
  183. extern const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT ;
  184. /* @brief When set, means user requested connecting to a new network and it failed */
  185. extern const int WIFI_MANAGER_REQUEST_STA_CONNECT_FAILED_BIT ;
  186. void network_manager_reboot_ota(char * url);
  187. /**
  188. * @brief simplified reason codes for a lost connection.
  189. *
  190. * esp-idf maintains a big list of reason codes which in practice are useless for most typical application.
  191. */
  192. typedef enum update_reason_code_t {
  193. UPDATE_CONNECTION_OK = 0,
  194. UPDATE_FAILED_ATTEMPT = 1,
  195. UPDATE_USER_DISCONNECT = 2,
  196. UPDATE_LOST_CONNECTION = 3,
  197. UPDATE_FAILED_ATTEMPT_AND_RESTORE = 4,
  198. UPDATE_ETHERNET_CONNECTED = 5
  199. }update_reason_code_t;
  200. typedef enum connection_request_made_by_code_t{
  201. CONNECTION_REQUEST_NONE = 0,
  202. CONNECTION_REQUEST_USER = 1,
  203. CONNECTION_REQUEST_AUTO_RECONNECT = 2,
  204. CONNECTION_REQUEST_RESTORE_CONNECTION = 3,
  205. CONNECTION_REQUEST_MAX_FAILED = 4,
  206. CONNECTION_REQUEST_MAX = 0x7fffffff /*force the creation of this enum as a 32 bit int */
  207. }connection_request_made_by_code_t;
  208. /**
  209. * The wifi manager settings in use
  210. */
  211. //struct wifi_settings_t{
  212. // bool sta_only;
  213. // bool sta_static_ip;
  214. // wifi_ps_type_t sta_power_save;
  215. // tcpip_adapter_ip_info_t sta_static_ip_config;
  216. //};
  217. //extern struct wifi_settings_t wifi_settings;
  218. // /**
  219. // * @brief Structure used to store one message in the queue.
  220. // */
  221. // typedef struct{
  222. // message_code_t code;
  223. // void *param;
  224. // } queue_message;
  225. /**
  226. * Frees up all memory allocated by the wifi_manager and kill the task.
  227. */
  228. void network_manager_destroy();
  229. /**
  230. * Filters the AP scan list to unique SSIDs
  231. */
  232. void filter_unique( wifi_ap_record_t * aplist, uint16_t * ap_num);
  233. /**
  234. * Main task for the wifi_manager
  235. */
  236. void network_manager( void * pvParameters );
  237. char* wifi_manager_alloc_get_ap_list_json();
  238. cJSON * wifi_manager_clear_ap_list_json(cJSON **old);
  239. /**
  240. * @brief A standard wifi event handler as recommended by Espressif
  241. */
  242. esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event);
  243. /**
  244. * @brief Clears the connection status json.
  245. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
  246. */
  247. cJSON * wifi_manager_clear_ip_info_json(cJSON **old);
  248. cJSON * wifi_manager_get_new_json(cJSON **old);
  249. /**
  250. * @brief Start the mDNS service
  251. */
  252. void wifi_manager_initialise_mdns();
  253. /**
  254. * @brief Register a callback to a custom function when specific event message_code happens.
  255. */
  256. void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) );
  257. bool network_manager_is_flag_set(EventBits_t bit);
  258. void network_manager_set_flag(EventBits_t bit);
  259. void network_manager_clear_flag(EventBits_t bit);
  260. #ifdef __cplusplus
  261. }
  262. #endif
  263. #endif /* WIFI_MANAGER_H_INCLUDED */