wifi_manager.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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.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. /**
  36. * @brief Defines the maximum size of a SSID name. 32 is IEEE standard.
  37. * @warning limit is also hard coded in wifi_config_t. Never extend this value.
  38. */
  39. #define MAX_SSID_SIZE 32
  40. /**
  41. * @brief Defines the maximum size of a WPA2 passkey. 64 is IEEE standard.
  42. * @warning limit is also hard coded in wifi_config_t. Never extend this value.
  43. */
  44. #define MAX_PASSWORD_SIZE 64
  45. #define MAX_COMMAND_LINE_SIZE 201
  46. /**
  47. * @brief Defines the maximum number of access points that can be scanned.
  48. *
  49. * To save memory and avoid nasty out of memory errors,
  50. * we can limit the number of APs detected in a wifi scan.
  51. */
  52. #define MAX_AP_NUM 15
  53. /**
  54. * @brief Defines when a connection is lost/attempt to connect is made, how many retries should be made before giving up.
  55. * Setting it to 2 for instance means there will be 3 attempts in total (original request + 2 retries)
  56. */
  57. #define WIFI_MANAGER_MAX_RETRY CONFIG_WIFI_MANAGER_MAX_RETRY
  58. /** @brief Defines the task priority of the wifi_manager.
  59. *
  60. * Tasks spawn by the manager will have a priority of WIFI_MANAGER_TASK_PRIORITY-1.
  61. * For this particular reason, minimum task priority is 1. It it highly not recommended to set
  62. * it to 1 though as the sub-tasks will now have a priority of 0 which is the priority
  63. * of freeRTOS' idle task.
  64. */
  65. #define WIFI_MANAGER_TASK_PRIORITY CONFIG_WIFI_MANAGER_TASK_PRIORITY
  66. /** @brief Defines the auth mode as an access point
  67. * Value must be of type wifi_auth_mode_t
  68. * @see esp_wifi_types.h
  69. * @warning if set to WIFI_AUTH_OPEN, passowrd me be empty. See DEFAULT_AP_PASSWORD.
  70. */
  71. #define AP_AUTHMODE WIFI_AUTH_WPA2_PSK
  72. /** @brief Defines visibility of the access point. 0: visible AP. 1: hidden */
  73. #define DEFAULT_AP_SSID_HIDDEN 0
  74. /** @brief Defines access point's name. Default value: esp32. Run 'make menuconfig' to setup your own value or replace here by a string */
  75. #define DEFAULT_AP_SSID CONFIG_DEFAULT_AP_SSID
  76. /** @brief Defines access point's password.
  77. * @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.
  78. * In addition, the AP_AUTHMODE must be WIFI_AUTH_OPEN
  79. */
  80. #define DEFAULT_AP_PASSWORD CONFIG_DEFAULT_AP_PASSWORD
  81. /** @brief Defines access point's bandwidth.
  82. * Value: WIFI_BW_HT20 for 20 MHz or WIFI_BW_HT40 for 40 MHz
  83. * 20 MHz minimize channel interference but is not suitable for
  84. * applications with high data speeds
  85. */
  86. #define DEFAULT_AP_BANDWIDTH WIFI_BW_HT20
  87. /** @brief Defines access point's channel.
  88. * Channel selection is only effective when not connected to another AP.
  89. * Good practice for minimal channel interference to use
  90. * For 20 MHz: 1, 6 or 11 in USA and 1, 5, 9 or 13 in most parts of the world
  91. * For 40 MHz: 3 in USA and 3 or 11 in most parts of the world
  92. */
  93. #define DEFAULT_AP_CHANNEL CONFIG_DEFAULT_AP_CHANNEL
  94. /** @brief Defines the access point's default IP address. Default: "10.10.0.1 */
  95. #define DEFAULT_AP_IP CONFIG_DEFAULT_AP_IP
  96. /** @brief Defines the access point's gateway. This should be the same as your IP. Default: "10.10.0.1" */
  97. #define DEFAULT_AP_GATEWAY CONFIG_DEFAULT_AP_GATEWAY
  98. /** @brief Defines the access point's netmask. Default: "255.255.255.0" */
  99. #define DEFAULT_AP_NETMASK CONFIG_DEFAULT_AP_NETMASK
  100. /** @brief Defines access point's maximum number of clients. Default: 4 */
  101. #define DEFAULT_AP_MAX_CONNECTIONS CONFIG_DEFAULT_AP_MAX_CONNECTIONS
  102. /** @brief Defines access point's beacon interval. 100ms is the recommended default. */
  103. #define DEFAULT_AP_BEACON_INTERVAL CONFIG_DEFAULT_AP_BEACON_INTERVAL
  104. /** @brief Defines if esp32 shall run both AP + STA when connected to another AP.
  105. * Value: 0 will have the own AP always on (APSTA mode)
  106. * Value: 1 will turn off own AP when connected to another AP (STA only mode when connected)
  107. * Turning off own AP when connected to another AP minimize channel interference and increase throughput
  108. */
  109. #define DEFAULT_STA_ONLY 1
  110. /** @brief Defines if wifi power save shall be enabled.
  111. * Value: WIFI_PS_NONE for full power (wifi modem always on)
  112. * Value: WIFI_PS_MODEM for power save (wifi modem sleep periodically)
  113. * Note: Power save is only effective when in STA only mode
  114. */
  115. #define DEFAULT_STA_POWER_SAVE WIFI_PS_MIN_MODEM
  116. /**
  117. * @brief Defines the maximum length in bytes of a JSON representation of an access point.
  118. *
  119. * maximum ap string length with full 32 char ssid: 75 + \\n + \0 = 77\n
  120. * example: {"ssid":"abcdefghijklmnopqrstuvwxyz012345","chan":12,"rssi":-100,"auth":4},\n
  121. * BUT: we need to escape JSON. Imagine a ssid full of \" ? so it's 32 more bytes hence 77 + 32 = 99.\n
  122. * this is an edge case but I don't think we should crash in a catastrophic manner just because
  123. * someone decided to have a funny wifi name.
  124. */
  125. #define JSON_ONE_APP_SIZE 99
  126. /**
  127. * @brief Defines the complete list of all messages that the wifi_manager can process.
  128. *
  129. * Some of these message are events ("EVENT"), and some of them are action ("ORDER")
  130. * Each of these messages can trigger a callback function and each callback function is stored
  131. * in a function pointer array for convenience. Because of this behavior, it is extremely important
  132. * to maintain a strict sequence and the top level special element 'MESSAGE_CODE_COUNT'
  133. *
  134. * @see wifi_manager_set_callback
  135. */
  136. typedef enum message_code_t {
  137. NONE = 0,
  138. ORDER_START_HTTP_SERVER = 1,
  139. ORDER_STOP_HTTP_SERVER = 2,
  140. ORDER_START_DNS_SERVICE = 3,
  141. ORDER_STOP_DNS_SERVICE = 4,
  142. ORDER_START_WIFI_SCAN = 5,
  143. ORDER_LOAD_AND_RESTORE_STA = 6,
  144. ORDER_CONNECT_STA = 7,
  145. ORDER_DISCONNECT_STA = 8,
  146. ORDER_START_AP = 9,
  147. ORDER_START_HTTP = 10,
  148. ORDER_START_DNS_HIJACK = 11,
  149. EVENT_STA_DISCONNECTED = 12,
  150. EVENT_SCAN_DONE = 13,
  151. EVENT_STA_GOT_IP = 14,
  152. ORDER_RESTART_OTA = 15,
  153. ORDER_RESTART_RECOVERY = 16,
  154. ORDER_RESTART_OTA_URL = 17,
  155. ORDER_RESTART = 18,
  156. ORDER_UPDATE_STATUS = 19,
  157. MESSAGE_CODE_COUNT = 20 /* important for the callback array */
  158. }message_code_t;
  159. typedef enum reboot_type_t{
  160. OTA,
  161. RECOVERY,
  162. RESTART,
  163. } reboot_type_t;
  164. void wifi_manager_reboot(reboot_type_t rtype);
  165. void wifi_manager_reboot_ota(char * url);
  166. void wifi_manager_update_status();
  167. /**
  168. * @brief simplified reason codes for a lost connection.
  169. *
  170. * esp-idf maintains a big list of reason codes which in practice are useless for most typical application.
  171. */
  172. typedef enum update_reason_code_t {
  173. UPDATE_CONNECTION_OK = 0,
  174. UPDATE_FAILED_ATTEMPT = 1,
  175. UPDATE_USER_DISCONNECT = 2,
  176. UPDATE_LOST_CONNECTION = 3,
  177. UPDATE_FAILED_ATTEMPT_AND_RESTORE = 4,
  178. }update_reason_code_t;
  179. typedef enum connection_request_made_by_code_t{
  180. CONNECTION_REQUEST_NONE = 0,
  181. CONNECTION_REQUEST_USER = 1,
  182. CONNECTION_REQUEST_AUTO_RECONNECT = 2,
  183. CONNECTION_REQUEST_RESTORE_CONNECTION = 3,
  184. CONNECTION_REQUEST_MAX = 0x7fffffff /*force the creation of this enum as a 32 bit int */
  185. }connection_request_made_by_code_t;
  186. /**
  187. * The wifi manager settings in use
  188. */
  189. //struct wifi_settings_t{
  190. // bool sta_only;
  191. // bool sta_static_ip;
  192. // wifi_ps_type_t sta_power_save;
  193. // tcpip_adapter_ip_info_t sta_static_ip_config;
  194. //};
  195. //extern struct wifi_settings_t wifi_settings;
  196. /**
  197. * @brief Structure used to store one message in the queue.
  198. */
  199. typedef struct{
  200. message_code_t code;
  201. void *param;
  202. } queue_message;
  203. /**
  204. * Allocate heap memory for the wifi manager and start the wifi_manager RTOS task
  205. */
  206. void wifi_manager_start();
  207. /**
  208. * Frees up all memory allocated by the wifi_manager and kill the task.
  209. */
  210. void wifi_manager_destroy();
  211. /**
  212. * Filters the AP scan list to unique SSIDs
  213. */
  214. void filter_unique( wifi_ap_record_t * aplist, uint16_t * ap_num);
  215. /**
  216. * Main task for the wifi_manager
  217. */
  218. void wifi_manager( void * pvParameters );
  219. char* wifi_manager_alloc_get_ap_list_json();
  220. char* wifi_manager_alloc_get_ip_info_json();
  221. cJSON * wifi_manager_clear_ap_list_json(cJSON **old);
  222. /**
  223. * @brief saves the current STA wifi config to flash ram storage.
  224. */
  225. esp_err_t wifi_manager_save_sta_config();
  226. /**
  227. * @brief fetch a previously STA wifi config in the flash ram storage.
  228. * @return true if a previously saved config was found, false otherwise.
  229. */
  230. bool wifi_manager_fetch_wifi_sta_config();
  231. wifi_config_t* wifi_manager_get_wifi_sta_config();
  232. /**
  233. * @brief A standard wifi event handler as recommended by Espressif
  234. */
  235. esp_err_t wifi_manager_event_handler(void *ctx, system_event_t *event);
  236. /**
  237. * @brief Registers handler for wifi and ip events
  238. */
  239. void wifi_manager_register_handlers();
  240. /**
  241. * @brief requests a connection to an access point that will be process in the main task thread.
  242. */
  243. void wifi_manager_connect_async();
  244. /**
  245. * @brief requests a wifi scan
  246. */
  247. void wifi_manager_scan_async();
  248. /**
  249. * @brief requests to disconnect and forget about the access point.
  250. */
  251. void wifi_manager_disconnect_async();
  252. /**
  253. * @brief Tries to get access to json buffer mutex.
  254. *
  255. * The HTTP server can try to access the json to serve clients while the wifi manager thread can try
  256. * to update it. These two tasks are synchronized through a mutex.
  257. *
  258. * The mutex is used by both the access point list json and the connection status json.\n
  259. * These two resources should technically have their own mutex but we lose some flexibility to save
  260. * on memory.
  261. *
  262. * This is a simple wrapper around freeRTOS function xSemaphoreTake.
  263. *
  264. * @param xTicksToWait The time in ticks to wait for the semaphore to become available.
  265. * @return true in success, false otherwise.
  266. */
  267. bool wifi_manager_lock_json_buffer(TickType_t xTicksToWait);
  268. /**
  269. * @brief Releases the json buffer mutex.
  270. */
  271. void wifi_manager_unlock_json_buffer();
  272. /**
  273. * @brief Generates the connection status json: ssid and IP addresses.
  274. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
  275. */
  276. void wifi_manager_generate_ip_info_json(update_reason_code_t update_reason_code);
  277. /**
  278. * @brief Clears the connection status json.
  279. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
  280. */
  281. cJSON * wifi_manager_clear_ip_info_json(cJSON **old);
  282. cJSON * wifi_manager_get_new_json(cJSON **old);
  283. /**
  284. * @brief Generates the list of access points after a wifi scan.
  285. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
  286. */
  287. void wifi_manager_generate_access_points_json(cJSON ** ap_list);
  288. /**
  289. * @brief Clear the list of access points.
  290. * @note This is not thread-safe and should be called only if wifi_manager_lock_json_buffer call is successful.
  291. */
  292. void wifi_manager_clear_access_points_json();
  293. /**
  294. * @brief Start the mDNS service
  295. */
  296. void wifi_manager_initialise_mdns();
  297. bool wifi_manager_lock_sta_ip_string(TickType_t xTicksToWait);
  298. void wifi_manager_unlock_sta_ip_string();
  299. /**
  300. * @brief gets the string representation of the STA IP address, e.g.: "192.168.1.69"
  301. */
  302. char* wifi_manager_get_sta_ip_string();
  303. /**
  304. * @brief thread safe char representation of the STA IP update
  305. */
  306. void wifi_manager_safe_update_sta_ip_string(struct ip4_addr * ip4);
  307. /**
  308. * @brief Register a callback to a custom function when specific event message_code happens.
  309. */
  310. void wifi_manager_set_callback(message_code_t message_code, void (*func_ptr)(void*) );
  311. BaseType_t wifi_manager_send_message(message_code_t code, void *param);
  312. BaseType_t wifi_manager_send_message_to_front(message_code_t code, void *param);
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif /* WIFI_MANAGER_H_INCLUDED */