network_manager_handlers.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245
  1. #ifdef NETWORK_HANDLERS_LOG_LEVEL
  2. #define LOG_LOCAL_LEVEL NETWORK_HANDLERS_LOG_LEVEL
  3. #pragma message("Log Level overwritten to " LOG_LOCAL_LEVEL)
  4. #else
  5. #pragma message("Log Level set to " LOG_LOCAL_LEVEL)
  6. #endif
  7. #include "network_manager.h"
  8. #include <stdbool.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include "network_ethernet.h"
  13. #include "network_status.h"
  14. #include "network_wifi.h"
  15. #include "dns_server.h"
  16. #include "esp_log.h"
  17. #include "esp_system.h"
  18. #include "freertos/FreeRTOS.h"
  19. #include "platform_esp32.h"
  20. #include "esp_netif.h"
  21. #include "freertos/task.h"
  22. #include "cJSON.h"
  23. #include "cmd_system.h"
  24. #include "esp_app_format.h"
  25. #include "esp_event.h"
  26. #include "esp_ota_ops.h"
  27. #include "esp_wifi.h"
  28. #include "esp_wifi_types.h"
  29. #include "lwip/api.h"
  30. #include "lwip/err.h"
  31. #include "lwip/ip4_addr.h"
  32. #include "lwip/netdb.h"
  33. #include "mdns.h"
  34. #include "messaging.h"
  35. #include "platform_config.h"
  36. #include "trace.h"
  37. #include "accessors.h"
  38. #include "esp_err.h"
  39. #include "tools.h"
  40. #include "http_server_handlers.h"
  41. #include "network_manager.h"
  42. #include "improv.h"
  43. static const char TAG[]="network_handlers";
  44. EXT_RAM_ATTR static state_t network_states[TOTAL_NM_STATE];
  45. EXT_RAM_ATTR static state_t Wifi_Active_State[TOTAL_WIFI_ACTIVE_STATE];
  46. EXT_RAM_ATTR static state_t Eth_Active_State[TOTAL_ETH_ACTIVE_STATE];
  47. EXT_RAM_ATTR static state_t Wifi_Configuring_State[TOTAL_WIFI_CONFIGURING_STATE];
  48. static void network_interface_coexistence(state_machine_t* state_machine);
  49. static state_machine_result_t local_traverse_state(state_machine_t* const state_machine,
  50. const state_t* const target_state, const char * caller) ;
  51. static state_machine_result_t local_switch_state(state_machine_t* state_machine,
  52. const state_t* const target_state, const char * caller);
  53. void network_execute_cb(state_machine_t* const state_machine, const char * caller);
  54. int get_root_id(const state_t * state);
  55. const state_t* get_root( const state_t* const state);
  56. #define ADD_ROOT(name,...) ADD_ROOT_FORWARD_DECLARATION(name,...)
  57. #define ADD_ROOT_LEAF(name,...) ADD_ROOT_LEAF_FORWARD_DECLARATION(name,...)
  58. #define ADD_LEAF(name,...) ADD_LEAF_FORWARD_DECLARATION(name,...)
  59. ALL_NM_STATE
  60. ALL_ETH_STATE(, )
  61. ALL_WIFI_STATE(, )
  62. ALL_WIFI_CONFIGURING_STATE(, )
  63. #undef ADD_ROOT
  64. #undef ADD_ROOT_LEAF
  65. #undef ADD_LEAF
  66. /*
  67. * --------------------- Global variables ---------------------
  68. */
  69. #define ADD_ROOT(NAME, CHILD) ADD_ROOT_(NAME, CHILD)
  70. #define ADD_LEAF(NAME, PARENT, LEVEL) ADD_LEAF_(NAME, PARENT, LEVEL)
  71. #define ADD_ROOT_LEAF(NAME) ADD_ROOT_LEAF_(NAME)
  72. #define ADD_ROOT_(NAME, CHILD) \
  73. [NAME] = { \
  74. .Handler = NAME##_handler, \
  75. .Entry = NAME##_entry_handler, \
  76. .Exit = NAME##_exit_handler, \
  77. .Level = 0, \
  78. .Parent = NULL, \
  79. .Id = NAME, \
  80. .Node = CHILD, \
  81. },
  82. #define ADD_ROOT_LEAF_(NAME) \
  83. [NAME] = { \
  84. .Handler = NAME##_handler, \
  85. .Entry = NAME##_entry_handler, \
  86. .Exit = NAME##_exit_handler, \
  87. .Id = NAME, \
  88. .Parent = NULL, \
  89. },
  90. #define ADD_LEAF_(NAME, PARENT, LEVEL) \
  91. [NAME] = { \
  92. .Handler = NAME##_handler, \
  93. .Entry = NAME##_entry_handler, \
  94. .Exit = NAME##_exit_handler, \
  95. .Id = NAME, \
  96. .Level = LEVEL, \
  97. .Parent = PARENT, \
  98. },
  99. static void network_initialize_state_machine_globals(){
  100. static state_t loc_network_states[] =
  101. {
  102. ALL_NM_STATE};
  103. static state_t loc_Wifi_Active_State[] = {
  104. ALL_WIFI_STATE(&network_states[NETWORK_WIFI_ACTIVE_STATE], 1)
  105. };
  106. static state_t loc_Eth_Active_State[]={
  107. ALL_ETH_STATE(&network_states[NETWORK_ETH_ACTIVE_STATE], 1)
  108. };
  109. static state_t loc_Wifi_Configuring_State[]={
  110. ALL_WIFI_CONFIGURING_STATE(&network_states[NETWORK_WIFI_CONFIGURING_ACTIVE_STATE], 1)
  111. };
  112. memcpy(&network_states,&loc_network_states,sizeof(network_states));
  113. memcpy(&Eth_Active_State,&loc_Eth_Active_State,sizeof(Eth_Active_State));
  114. memcpy(&Wifi_Active_State,&loc_Wifi_Active_State,sizeof(Wifi_Active_State));
  115. memcpy(&Wifi_Configuring_State,&loc_Wifi_Configuring_State,sizeof(Wifi_Configuring_State));
  116. }
  117. #undef ADD_ROOT
  118. #undef ADD_ROOT_LEAF
  119. #undef ADD_LEAF
  120. #define HANDLE_GLOBAL_EVENT(m) \
  121. if (handle_global_event(m) == EVENT_HANDLED) \
  122. return EVENT_HANDLED;
  123. static void network_connect_active_ssid(state_machine_t* const State_Machine) {
  124. network_t* const nm = (network_t*)State_Machine;
  125. if (network_wifi_connect_active_ssid() != ESP_OK) {
  126. ESP_LOGE(TAG, "Oups. Something went wrong!");
  127. nm->wifi_connected = false;
  128. ESP_LOGD(TAG, "Checking if ethernet interface is connected");
  129. if (network_is_interface_connected(nm->eth_netif)) {
  130. ESP_LOGD(TAG, "Ethernet connection is found. Try to fallback there");
  131. network_async(EN_ETHERNET_FALLBACK);
  132. } else {
  133. // returning to AP mode
  134. nm->STA_duration = nm->sta_polling_min_ms;
  135. ESP_LOGD(TAG, "No ethernet and no wifi configured. Go to configuration mode");
  136. network_async_configure();
  137. }
  138. }
  139. }
  140. void initialize_network_handlers(state_machine_t* state_machine){
  141. MEMTRACE_PRINT_DELTA();
  142. network_initialize_state_machine_globals();
  143. MEMTRACE_PRINT_DELTA();
  144. NETWORK_INSTANTIATED_STATE_handler(state_machine);
  145. MEMTRACE_PRINT_DELTA();
  146. }
  147. static state_machine_result_t handle_global_event(state_machine_t* state_machine) {
  148. network_t * net_sm= ((network_t *)state_machine);
  149. switch (net_sm->Machine.Event) {
  150. case EN_UPDATE_STATUS:
  151. // handle the event, but don't swicth
  152. MEMTRACE_PRINT_DELTA_MESSAGE("handle EN_UPDATE_STATUS - start");
  153. network_status_update_basic_info();
  154. MEMTRACE_PRINT_DELTA_MESSAGE("handle EN_UPDATE_STATUS - end");
  155. return EVENT_HANDLED;
  156. /* code */
  157. break;
  158. case EN_REBOOT:
  159. ESP_LOGD(TAG,"Called for reboot type %d",net_sm->event_parameters->rtype);
  160. switch (net_sm->event_parameters->rtype) {
  161. case OTA:
  162. ESP_LOGD(TAG, " Calling guided_restart_ota.");
  163. guided_restart_ota();
  164. break;
  165. case RECOVERY:
  166. ESP_LOGD(TAG, " Calling guided_factory.");
  167. guided_factory();
  168. break;
  169. case RESTART:
  170. ESP_LOGD(TAG, " Calling simple_restart.");
  171. simple_restart();
  172. break;
  173. default:
  174. break;
  175. }
  176. return EVENT_UN_HANDLED;
  177. break;
  178. case EN_REBOOT_URL:
  179. if (net_sm->event_parameters->strval) {
  180. start_ota(net_sm->event_parameters->strval, NULL, 0);
  181. FREE_AND_NULL(net_sm->event_parameters->strval);
  182. }
  183. return EVENT_UN_HANDLED;
  184. break;
  185. default:
  186. break;
  187. }
  188. return EVENT_UN_HANDLED;
  189. }
  190. /*********************************************************************************************
  191. * INSTANTIATED_STATE
  192. */
  193. static state_machine_result_t NETWORK_INSTANTIATED_STATE_entry_handler(state_machine_t* const State_Machine) {
  194. network_handler_entry_print(State_Machine,true);
  195. NETWORK_EXECUTE_CB(State_Machine);
  196. network_handler_entry_print(State_Machine,false);
  197. return EVENT_HANDLED;
  198. }
  199. static state_machine_result_t NETWORK_INSTANTIATED_STATE_handler(state_machine_t* const State_Machine) {
  200. network_handler_print(State_Machine,true);
  201. state_machine_result_t result = EVENT_UN_HANDLED;
  202. network_t* const nm = (network_t *)State_Machine;
  203. State_Machine->State = &network_states[NETWORK_INSTANTIATED_STATE];
  204. State_Machine->Event = EN_START;
  205. config_get_uint16t_from_str("pollmx",&nm->sta_polling_max_ms,600);
  206. nm->sta_polling_max_ms = nm->sta_polling_max_ms * 1000;
  207. config_get_uint16t_from_str("apdelay",&nm->ap_duration_ms,20);
  208. nm->ap_duration_ms = nm->ap_duration_ms * 1000;
  209. config_get_uint16t_from_str("pollmin",&nm->sta_polling_min_ms,15);
  210. nm->sta_polling_min_ms = nm->sta_polling_min_ms*1000;
  211. config_get_uint16t_from_str("ethtmout",&nm->eth_link_down_reboot_ms,30);
  212. nm->eth_link_down_reboot_ms = nm->eth_link_down_reboot_ms*1000;
  213. config_get_uint16t_from_str("dhcp_tmout",&nm->dhcp_timeout,30);
  214. nm->dhcp_timeout = nm->dhcp_timeout*1000;
  215. ESP_LOGI(TAG,"Network manager configuration: polling max %d, polling min %d, ap delay %d, dhcp timeout %d, eth timeout %d",
  216. nm->sta_polling_max_ms,nm->sta_polling_min_ms,nm->ap_duration_ms,nm->dhcp_timeout, nm->eth_link_down_reboot_ms);
  217. HANDLE_GLOBAL_EVENT(State_Machine);
  218. if (State_Machine->Event == EN_START) {
  219. result= local_traverse_state(State_Machine, &network_states[NETWORK_INITIALIZING_STATE],__FUNCTION__);
  220. }
  221. network_handler_print(State_Machine,false);
  222. return result;
  223. }
  224. static state_machine_result_t NETWORK_INSTANTIATED_STATE_exit_handler(state_machine_t* const State_Machine) {
  225. network_exit_handler_print(State_Machine,true);
  226. network_exit_handler_print(State_Machine,false);
  227. return EVENT_HANDLED;
  228. }
  229. /*********************************************************************************************
  230. * INITIALIZING_STATE
  231. */
  232. static state_machine_result_t NETWORK_INITIALIZING_STATE_entry_handler(state_machine_t* const State_Machine) {
  233. network_handler_entry_print(State_Machine,true);
  234. MEMTRACE_PRINT_DELTA_MESSAGE(" Initializing tcp_ip adapter");
  235. esp_netif_init();
  236. MEMTRACE_PRINT_DELTA_MESSAGE(" Creating the default event loop");
  237. ESP_ERROR_CHECK(esp_event_loop_create_default());
  238. MEMTRACE_PRINT_DELTA_MESSAGE("Initializing network status");
  239. init_network_status();
  240. MEMTRACE_PRINT_DELTA_MESSAGE("Loading wifi global configuration");
  241. network_wifi_global_init();
  242. MEMTRACE_PRINT_DELTA_MESSAGE(" Registering event handler");
  243. esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &network_ip_event_handler, NULL);
  244. MEMTRACE_PRINT_DELTA_MESSAGE(" Initializing network done. Starting http server");
  245. // send a message to start the connections
  246. http_server_start();
  247. MEMTRACE_PRINT_DELTA_MESSAGE("Executing Callback");
  248. NETWORK_EXECUTE_CB(State_Machine);
  249. network_handler_entry_print(State_Machine,false);
  250. return EVENT_HANDLED;
  251. }
  252. static state_machine_result_t NETWORK_INITIALIZING_STATE_handler(state_machine_t* const State_Machine) {
  253. network_handler_print(State_Machine,true);
  254. state_machine_result_t result = EVENT_UN_HANDLED;
  255. HANDLE_GLOBAL_EVENT(State_Machine);
  256. switch (State_Machine->Event) {
  257. case EN_START:
  258. if (network_is_wifi_prioritized()) {
  259. ESP_LOGI(TAG, "WiFi connection is prioritized. Starting WiFi");
  260. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE],__FUNCTION__);
  261. }
  262. else if(is_recovery_running){
  263. ESP_LOGI(TAG, "Running recovery. Skipping ethernet, starting WiFi");
  264. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE],__FUNCTION__);
  265. }
  266. else {
  267. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_STARTING_STATE],__FUNCTION__);
  268. }
  269. break;
  270. default:
  271. result= EVENT_UN_HANDLED;
  272. }
  273. network_handler_print(State_Machine,false);
  274. return result;
  275. }
  276. static state_machine_result_t NETWORK_INITIALIZING_STATE_exit_handler(state_machine_t* const State_Machine) {
  277. network_exit_handler_print(State_Machine,true);
  278. network_exit_handler_print(State_Machine,false);
  279. return EVENT_HANDLED;
  280. }
  281. /*********************************************************************************************
  282. * ETH_STARTING_STATE
  283. */
  284. static state_machine_result_t ETH_STARTING_STATE_entry_handler(state_machine_t* const State_Machine) {
  285. network_handler_entry_print(State_Machine,true);
  286. ESP_LOGD(TAG, "Looking for ethernet Interface");
  287. network_t* const nm = (network_t *)State_Machine;
  288. init_network_ethernet();
  289. if (!network_ethernet_enabled()) {
  290. network_async_fail();
  291. } else {
  292. nm->eth_netif = network_ethernet_get_interface();
  293. }
  294. NETWORK_EXECUTE_CB(State_Machine);
  295. network_handler_entry_print(State_Machine,false);
  296. return EVENT_HANDLED;
  297. }
  298. static state_machine_result_t ETH_STARTING_STATE_handler(state_machine_t* const State_Machine) {
  299. state_machine_result_t result = EVENT_HANDLED;
  300. network_handler_print(State_Machine,true);
  301. HANDLE_GLOBAL_EVENT(State_Machine);
  302. switch (State_Machine->Event) {
  303. case EN_FAIL:
  304. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE],__FUNCTION__);
  305. break;
  306. case EN_SUCCESS:
  307. result= local_traverse_state(State_Machine, &network_states[NETWORK_ETH_ACTIVE_STATE],__FUNCTION__);
  308. break;
  309. default:
  310. ESP_LOGE(TAG, "No handler");
  311. result= EVENT_UN_HANDLED;
  312. }
  313. network_handler_print(State_Machine,false);
  314. return result;
  315. }
  316. static state_machine_result_t ETH_STARTING_STATE_exit_handler(state_machine_t* const State_Machine) {
  317. network_exit_handler_print(State_Machine,true);
  318. network_exit_handler_print(State_Machine,false);
  319. return EVENT_HANDLED;
  320. }
  321. /*********************************************************************************************
  322. * NETWORK_ETH_ACTIVE_STATE
  323. */
  324. static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
  325. network_handler_entry_print(State_Machine,true);
  326. network_t* const nm = (network_t *)State_Machine;
  327. network_set_timer(nm->eth_link_down_reboot_ms,"Ethernet link not detected" );
  328. NETWORK_EXECUTE_CB(State_Machine);
  329. network_handler_entry_print(State_Machine,false);
  330. return EVENT_HANDLED;
  331. }
  332. static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
  333. network_handler_print(State_Machine,true);
  334. state_machine_result_t result = EVENT_UN_HANDLED;
  335. network_t* const nm = (network_t *)State_Machine;
  336. switch (State_Machine->Event) {
  337. case EN_CONNECT_NEW:
  338. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_CONNECTING_NEW_STATE],__FUNCTION__);
  339. break;
  340. case EN_LINK_UP:
  341. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_LINKUP_STATE],__FUNCTION__);
  342. break;
  343. case EN_LINK_DOWN:
  344. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_LINKDOWN_STATE],__FUNCTION__);
  345. break;
  346. case EN_ETH_GOT_IP:
  347. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE],__FUNCTION__);
  348. break;
  349. case EN_ETHERNET_FALLBACK:
  350. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE],__FUNCTION__);
  351. break;
  352. case EN_TIMER:
  353. ESP_LOGW(TAG, "Timeout %s. Rebooting to wifi",STR_OR_ALT(nm->timer_tag,"Ethernet link not detected"));
  354. network_prioritize_wifi(true);
  355. simple_restart();
  356. //result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_INITIALIZING_STATE],__FUNCTION__);
  357. break;
  358. case EN_SCAN:
  359. ESP_LOGW(TAG,"Wifi scan cannot be executed in this state");
  360. network_wifi_built_known_ap_list();
  361. result = EVENT_HANDLED;
  362. break;
  363. case EN_DELETE: {
  364. ESP_LOGD(TAG, "WiFi disconnected by user");
  365. network_wifi_clear_config();
  366. network_status_update_ip_info(UPDATE_USER_DISCONNECT);
  367. result= EVENT_HANDLED;
  368. } break;
  369. default:
  370. HANDLE_GLOBAL_EVENT(State_Machine);
  371. result=EVENT_UN_HANDLED;
  372. }
  373. network_handler_print(State_Machine,false);
  374. return result;
  375. }
  376. static state_machine_result_t NETWORK_ETH_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
  377. network_exit_handler_print(State_Machine,true);
  378. network_set_timer(0,NULL);
  379. network_exit_handler_print(State_Machine,false);
  380. return EVENT_HANDLED;
  381. }
  382. /*********************************************************************************************
  383. * ETH_CONNECTING_NEW_STATE
  384. */
  385. static state_machine_result_t ETH_CONNECTING_NEW_STATE_entry_handler(state_machine_t* const State_Machine) {
  386. network_t* const nm = (network_t *)State_Machine;
  387. network_handler_entry_print(State_Machine,true);
  388. network_start_stop_dhcp_client(nm->wifi_netif, true);
  389. if(network_wifi_connect(nm->event_parameters->ssid,nm->event_parameters->password) == ESP_ERR_INVALID_ARG){
  390. network_async_fail();
  391. }
  392. FREE_AND_NULL(nm->event_parameters->ssid);
  393. FREE_AND_NULL(nm->event_parameters->password);
  394. NETWORK_EXECUTE_CB(State_Machine);
  395. network_handler_entry_print(State_Machine,false);
  396. return EVENT_HANDLED;
  397. }
  398. static state_machine_result_t ETH_CONNECTING_NEW_STATE_handler(state_machine_t* const State_Machine) {
  399. HANDLE_GLOBAL_EVENT(State_Machine);
  400. network_handler_print(State_Machine,true);
  401. state_machine_result_t result = EVENT_HANDLED;
  402. switch (State_Machine->Event) {
  403. case EN_FAIL:
  404. ESP_LOGW(TAG,"Error connecting to access point");
  405. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  406. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  407. break;
  408. case EN_GOT_IP:
  409. result= local_traverse_state(State_Machine, &network_states[WIFI_CONNECTED_STATE],__FUNCTION__);
  410. break;
  411. case EN_LOST_CONNECTION:
  412. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  413. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Unable to connect to new WiFi access point.");
  414. // no existing configuration, or wifi wasn't the active connection when connection
  415. // attempt was made
  416. network_async(EN_ETHERNET_FALLBACK);
  417. result = EVENT_HANDLED;
  418. break;
  419. default:
  420. result= EVENT_UN_HANDLED;
  421. }
  422. network_handler_print(State_Machine,false);
  423. return result;
  424. }
  425. static state_machine_result_t ETH_CONNECTING_NEW_STATE_exit_handler(state_machine_t* const State_Machine) {
  426. network_exit_handler_print(State_Machine,true);
  427. network_set_timer(0,NULL);
  428. network_exit_handler_print(State_Machine,false);
  429. return EVENT_HANDLED;
  430. }
  431. /*********************************************************************************************
  432. * ETH_ACTIVE_LINKDOWN
  433. */
  434. static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_entry_handler(state_machine_t* const State_Machine) {
  435. network_handler_entry_print(State_Machine,true);
  436. network_t* const nm = (network_t *)State_Machine;
  437. network_set_timer(nm->eth_link_down_reboot_ms, "Ethernet link down" );
  438. NETWORK_EXECUTE_CB(State_Machine);
  439. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Ethernet link down.");
  440. network_handler_entry_print(State_Machine,false);
  441. return EVENT_HANDLED;
  442. }
  443. static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_handler(state_machine_t* const State_Machine) {
  444. network_handler_print(State_Machine,true);
  445. HANDLE_GLOBAL_EVENT(State_Machine);
  446. network_handler_print(State_Machine,false);
  447. return EVENT_UN_HANDLED;
  448. }
  449. static state_machine_result_t ETH_ACTIVE_LINKDOWN_STATE_exit_handler(state_machine_t* const State_Machine) {
  450. network_exit_handler_print(State_Machine,true);
  451. network_set_timer(0,NULL);
  452. network_exit_handler_print(State_Machine,false);
  453. return EVENT_HANDLED;
  454. }
  455. /*********************************************************************************************
  456. * ETH_ACTIVE_LINKUP_STATE
  457. */
  458. static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_entry_handler(state_machine_t* const State_Machine) {
  459. network_handler_entry_print(State_Machine,true);
  460. network_t* const nm = (network_t *)State_Machine;
  461. network_set_timer(nm->dhcp_timeout, "DHCP timeout" );
  462. NETWORK_EXECUTE_CB(State_Machine);
  463. messaging_post_message(MESSAGING_INFO, MESSAGING_CLASS_SYSTEM, "Ethernet link up.");
  464. network_handler_entry_print(State_Machine,false);
  465. return EVENT_HANDLED;
  466. }
  467. static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_handler(state_machine_t* const State_Machine) {
  468. state_machine_result_t result = EVENT_UN_HANDLED;
  469. network_handler_print(State_Machine,true);
  470. HANDLE_GLOBAL_EVENT(State_Machine);
  471. network_handler_print(State_Machine,false);
  472. return result;
  473. }
  474. static state_machine_result_t ETH_ACTIVE_LINKUP_STATE_exit_handler(state_machine_t* const State_Machine) {
  475. network_exit_handler_print(State_Machine,true);
  476. network_exit_handler_print(State_Machine,false);
  477. return EVENT_HANDLED;
  478. }
  479. /*********************************************************************************************
  480. * WIFI_UP_STATE
  481. */
  482. static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
  483. network_handler_entry_print(State_Machine,true);
  484. NETWORK_EXECUTE_CB(State_Machine);
  485. network_handler_entry_print(State_Machine,false);
  486. return EVENT_HANDLED;
  487. }
  488. static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
  489. HANDLE_GLOBAL_EVENT(State_Machine);
  490. network_handler_print(State_Machine,true);
  491. state_machine_result_t result = EVENT_UN_HANDLED;
  492. switch (State_Machine->Event)
  493. {
  494. case EN_LINK_UP:
  495. ESP_LOGW(TAG, "Ethernet link up in wifi mode");
  496. break;
  497. case EN_ETH_GOT_IP:
  498. network_interface_coexistence(State_Machine);
  499. break;
  500. case EN_GOT_IP:
  501. network_status_update_ip_info(UPDATE_CONNECTION_OK);
  502. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE],__FUNCTION__);
  503. break;
  504. case EN_SCAN:
  505. if (network_wifi_start_scan() == ESP_OK) {
  506. result= EVENT_HANDLED;
  507. }
  508. break;
  509. case EN_SCAN_DONE:
  510. if(wifi_scan_done() == ESP_OK) {
  511. result= EVENT_HANDLED;
  512. }
  513. break;
  514. case EN_CONNECT_NEW:
  515. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_NEW_STATE],__FUNCTION__);
  516. break;
  517. case EN_DELETE:
  518. result= local_traverse_state(State_Machine,&Wifi_Active_State[WIFI_USER_DISCONNECTED_STATE],__FUNCTION__);
  519. break;
  520. case EN_ETHERNET_FALLBACK:
  521. result= local_traverse_state(State_Machine, &Eth_Active_State[ETH_ACTIVE_CONNECTED_STATE],__FUNCTION__);
  522. break;
  523. default:
  524. break;
  525. }
  526. network_handler_print(State_Machine,false);
  527. return result;
  528. }
  529. static state_machine_result_t NETWORK_WIFI_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
  530. network_exit_handler_print(State_Machine,true);
  531. network_exit_handler_print(State_Machine,false);
  532. return EVENT_HANDLED;
  533. }
  534. /*********************************************************************************************
  535. * WIFI_INITIALIZING_STATE
  536. */
  537. static state_machine_result_t WIFI_INITIALIZING_STATE_entry_handler(state_machine_t* const State_Machine) {
  538. network_t* const nm = (network_t *)State_Machine;
  539. network_handler_entry_print(State_Machine,true);
  540. if(!nm->wifi_netif){
  541. nm->wifi_netif = network_wifi_start();
  542. }
  543. if (!is_wifi_up()) {
  544. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi not started. Load Configuration");
  545. return EVENT_UN_HANDLED;
  546. }
  547. if (network_wifi_get_known_count()>0) {
  548. ESP_LOGI(TAG, "Existing wifi config found. Attempting to connect.");
  549. network_async_success();
  550. } else {
  551. /* no wifi saved: start soft AP! This is what should happen during a first run */
  552. ESP_LOGW(TAG, "No saved wifi. Starting AP configuration mode.");
  553. network_async_configure();
  554. }
  555. NETWORK_EXECUTE_CB(State_Machine);
  556. network_handler_entry_print(State_Machine,false);
  557. return EVENT_HANDLED;
  558. }
  559. static state_machine_result_t WIFI_INITIALIZING_STATE_handler(state_machine_t* const State_Machine) {
  560. HANDLE_GLOBAL_EVENT(State_Machine);
  561. network_handler_print(State_Machine,true);
  562. state_machine_result_t result = EVENT_HANDLED;
  563. switch (State_Machine->Event) {
  564. case EN_CONFIGURE:
  565. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  566. break;
  567. case EN_SUCCESS:
  568. result= local_switch_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_STATE],__FUNCTION__);
  569. break;
  570. default:
  571. result= EVENT_UN_HANDLED;
  572. }
  573. network_handler_print(State_Machine,false);
  574. return result;
  575. }
  576. static state_machine_result_t WIFI_INITIALIZING_STATE_exit_handler(state_machine_t* const State_Machine) {
  577. network_exit_handler_print(State_Machine,true);
  578. network_exit_handler_print(State_Machine,false);
  579. return EVENT_HANDLED;
  580. }
  581. /*********************************************************************************************
  582. * WIFI_CONFIGURING_ACTIVE_STATE
  583. */
  584. static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_entry_handler(state_machine_t* const State_Machine) {
  585. network_handler_entry_print(State_Machine,true);
  586. network_t* const nm = (network_t *)State_Machine;
  587. nm->wifi_ap_netif = network_wifi_config_ap();
  588. dns_server_start(nm->wifi_ap_netif);
  589. network_wifi_start_scan();
  590. NETWORK_EXECUTE_CB(State_Machine);
  591. network_handler_entry_print(State_Machine,false);
  592. return EVENT_HANDLED;
  593. }
  594. static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_handler(state_machine_t* const State_Machine) {
  595. HANDLE_GLOBAL_EVENT(State_Machine);
  596. network_handler_print(State_Machine,true);
  597. state_machine_result_t result = EVENT_HANDLED;
  598. switch (State_Machine->Event) {
  599. case EN_SCAN:
  600. if (network_wifi_start_scan() == ESP_OK) {
  601. result= EVENT_HANDLED;
  602. }
  603. break;
  604. case EN_SCAN_DONE:
  605. ESP_LOGD(TAG,"Network configuration active, wifi scan completed");
  606. if(wifi_scan_done() == ESP_OK) {
  607. result= EVENT_HANDLED;
  608. }
  609. break;
  610. case EN_CONNECT_NEW:
  611. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_STATE],__FUNCTION__);
  612. break;
  613. case EN_LINK_UP:
  614. ESP_LOGW(TAG, "Ethernet link up in wifi mode");
  615. break;
  616. case EN_ETH_GOT_IP:
  617. network_interface_coexistence(State_Machine);
  618. break;
  619. case EN_TIMER:
  620. result= EVENT_HANDLED;
  621. break;
  622. default:
  623. result =EVENT_UN_HANDLED;
  624. }
  625. network_handler_print(State_Machine,false);
  626. return result;
  627. }
  628. static state_machine_result_t NETWORK_WIFI_CONFIGURING_ACTIVE_STATE_exit_handler(state_machine_t* const State_Machine) {
  629. network_exit_handler_print(State_Machine, true);
  630. /* bring down DNS hijack */
  631. ESP_LOGD(TAG, " Stopping DNS.");
  632. dns_server_stop();
  633. ESP_LOGD(TAG, "Changing wifi mode to STA.");
  634. network_wifi_set_sta_mode();
  635. network_exit_handler_print(State_Machine, false);
  636. return EVENT_HANDLED;
  637. }
  638. /*********************************************************************************************
  639. * WIFI_CONFIGURING_STATE
  640. */
  641. static state_machine_result_t WIFI_CONFIGURING_STATE_entry_handler(state_machine_t* const State_Machine) {
  642. network_handler_entry_print(State_Machine,true);
  643. NETWORK_EXECUTE_CB(State_Machine);
  644. network_handler_entry_print(State_Machine,false);
  645. return EVENT_HANDLED;
  646. }
  647. static state_machine_result_t WIFI_CONFIGURING_STATE_handler(state_machine_t* const State_Machine) {
  648. network_handler_print(State_Machine,true);
  649. HANDLE_GLOBAL_EVENT(State_Machine);
  650. network_handler_print(State_Machine,false);
  651. return EVENT_UN_HANDLED;
  652. }
  653. static state_machine_result_t WIFI_CONFIGURING_STATE_exit_handler(state_machine_t* const State_Machine) {
  654. network_exit_handler_print(State_Machine,true);
  655. network_exit_handler_print(State_Machine,false);
  656. return EVENT_HANDLED;
  657. }
  658. /*********************************************************************************************
  659. * WIFI_CONFIGURING_CONNECT_STATE
  660. */
  661. static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_entry_handler(state_machine_t* const State_Machine) {
  662. network_t* const nm = (network_t *)State_Machine;
  663. network_handler_entry_print(State_Machine,true);
  664. network_start_stop_dhcp_client(nm->wifi_netif, true);
  665. if(network_wifi_connect(nm->event_parameters->ssid,nm->event_parameters->password) == ESP_ERR_INVALID_ARG){
  666. network_async_fail();
  667. }
  668. FREE_AND_NULL(nm->event_parameters->ssid);
  669. FREE_AND_NULL(nm->event_parameters->password);
  670. NETWORK_EXECUTE_CB(State_Machine);
  671. network_handler_entry_print(State_Machine,false);
  672. return EVENT_HANDLED;
  673. }
  674. static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_handler(state_machine_t* const State_Machine) {
  675. HANDLE_GLOBAL_EVENT(State_Machine);
  676. network_handler_print(State_Machine,true);
  677. network_t* const nm = (network_t *)State_Machine;
  678. state_machine_result_t result = EVENT_HANDLED;
  679. switch (State_Machine->Event) {
  680. case EN_CONNECTED:
  681. result=EVENT_HANDLED;
  682. ESP_LOGI(TAG,"Wifi was connected. Waiting for IP address");
  683. network_set_timer(nm->dhcp_timeout,"DHCP Timeout");
  684. break;
  685. case EN_GOT_IP:
  686. network_status_update_ip_info(UPDATE_CONNECTION_OK);
  687. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_SUCCESS_STATE],__FUNCTION__);
  688. break;
  689. case EN_FAIL:
  690. ESP_LOGW(TAG,"Error connecting to access point");
  691. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_FAILED_STATE],__FUNCTION__);
  692. break;
  693. case EN_LOST_CONNECTION:
  694. if(nm->event_parameters->disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE) {
  695. ESP_LOGI(TAG,"Wifi was disconnected from previous access point. Waiting to connect.");
  696. }
  697. else {
  698. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_CONNECT_FAILED_STATE],__FUNCTION__);
  699. }
  700. break;
  701. case EN_TIMER:
  702. ESP_LOGW(TAG,"Connection timeout. (%s)",STR_OR_ALT(nm->timer_tag, "Unknown"));
  703. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  704. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  705. break;
  706. default:
  707. result= EVENT_UN_HANDLED;
  708. }
  709. network_handler_print(State_Machine,false);
  710. return result;
  711. }
  712. static state_machine_result_t WIFI_CONFIGURING_CONNECT_STATE_exit_handler(state_machine_t* const State_Machine) {
  713. network_exit_handler_print(State_Machine,true);
  714. FREE_AND_NULL(((network_t *)State_Machine)->event_parameters->disconnected_event);
  715. network_set_timer(0,NULL);
  716. network_exit_handler_print(State_Machine,false);
  717. return EVENT_HANDLED;
  718. }
  719. /*********************************************************************************************
  720. * WIFI_CONFIGURING_CONNECT_FAILED_STATE
  721. */
  722. static state_machine_result_t WIFI_CONFIGURING_CONNECT_FAILED_STATE_entry_handler(state_machine_t* const State_Machine) {
  723. network_handler_entry_print(State_Machine,true);
  724. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  725. ESP_LOGE(TAG, "Connecting Failed.");
  726. NETWORK_EXECUTE_CB(State_Machine);
  727. network_async_fail();
  728. network_handler_entry_print(State_Machine,false);
  729. return EVENT_HANDLED;
  730. }
  731. static state_machine_result_t WIFI_CONFIGURING_CONNECT_FAILED_STATE_handler(state_machine_t* const State_Machine) {
  732. network_handler_print(State_Machine,true);
  733. state_machine_result_t result = EVENT_HANDLED;
  734. network_t* const nm = (network_t *)State_Machine;
  735. switch (State_Machine->Event) {
  736. case EN_FAIL:
  737. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  738. break;
  739. default:
  740. result= EVENT_HANDLED;
  741. }
  742. // Process global handler at the end, since we want to overwrite
  743. // UPDATE_STATUS with our own logic above
  744. HANDLE_GLOBAL_EVENT(State_Machine);
  745. network_handler_print(State_Machine,false);
  746. return result;
  747. }
  748. static state_machine_result_t WIFI_CONFIGURING_CONNECT_FAILED_STATE_exit_handler(state_machine_t* const State_Machine) {
  749. network_exit_handler_print(State_Machine,true);
  750. network_set_timer(0,NULL);
  751. network_exit_handler_print(State_Machine,false);
  752. return EVENT_HANDLED;
  753. }
  754. /*********************************************************************************************
  755. * WIFI_CONFIGURING_CONNECT_SUCCESS_STATE
  756. */
  757. static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_entry_handler(state_machine_t* const State_Machine) {
  758. network_handler_entry_print(State_Machine,true);
  759. network_status_update_ip_info(UPDATE_CONNECTION_OK);
  760. ESP_LOGD(TAG, "Saving wifi configuration.");
  761. network_wifi_save_sta_config();
  762. NETWORK_EXECUTE_CB(State_Machine);
  763. network_handler_entry_print(State_Machine,false);
  764. return EVENT_HANDLED;
  765. }
  766. static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_handler(state_machine_t* const State_Machine) {
  767. network_handler_print(State_Machine,true);
  768. state_machine_result_t result = EVENT_HANDLED;
  769. network_t* const nm = (network_t *)State_Machine;
  770. switch (State_Machine->Event) {
  771. case EN_UPDATE_STATUS:
  772. network_status_update_basic_info();
  773. network_set_timer(nm->ap_duration_ms,"Access point teardown"); // set a timer to tear down the AP mode
  774. break;
  775. case EN_TIMER:
  776. network_status_update_basic_info();
  777. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE],__FUNCTION__);
  778. break;
  779. default:
  780. result= EVENT_UN_HANDLED;
  781. }
  782. // Process global handler at the end, since we want to overwrite
  783. // UPDATE_STATUS with our own logic above
  784. HANDLE_GLOBAL_EVENT(State_Machine);
  785. network_handler_print(State_Machine,false);
  786. return result;
  787. }
  788. static state_machine_result_t WIFI_CONFIGURING_CONNECT_SUCCESS_STATE_exit_handler(state_machine_t* const State_Machine) {
  789. network_exit_handler_print(State_Machine,true);
  790. network_set_timer(0,NULL);
  791. network_exit_handler_print(State_Machine,false);
  792. return EVENT_HANDLED;
  793. }
  794. /*********************************************************************************************
  795. * WIFI_CONNECTING_STATE
  796. */
  797. static state_machine_result_t WIFI_CONNECTING_STATE_entry_handler(state_machine_t* const State_Machine) {
  798. network_t* const nm = (network_t *)State_Machine;
  799. network_handler_entry_print(State_Machine,true);
  800. network_start_stop_dhcp_client(nm->wifi_netif, true);
  801. network_connect_active_ssid(State_Machine);
  802. nm->STA_duration = nm->sta_polling_min_ms;
  803. /* create timer for background STA connection */
  804. network_set_timer(nm->STA_duration,"Wifi Polling timeout");
  805. NETWORK_EXECUTE_CB(State_Machine);
  806. network_handler_entry_print(State_Machine,false);
  807. return EVENT_HANDLED;
  808. }
  809. static state_machine_result_t WIFI_CONNECTING_STATE_handler(state_machine_t* const State_Machine) {
  810. HANDLE_GLOBAL_EVENT(State_Machine);
  811. state_machine_result_t result = EVENT_HANDLED;
  812. network_t* const nm = (network_t *)State_Machine;
  813. network_handler_print(State_Machine,true);
  814. switch (State_Machine->Event) {
  815. case EN_CONNECTED:
  816. // nothing to do here. Let's wait for IP address
  817. break;
  818. case EN_TIMER:
  819. // try connecting again.
  820. // todo: implement multi-ap logic
  821. ESP_LOGI(TAG, "Timer: %s ",STR_OR_ALT(nm->timer_tag,"Ethernet link not detected"));
  822. network_connect_active_ssid(State_Machine);
  823. break;
  824. case EN_LOST_CONNECTION:
  825. if(nm->event_parameters->disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE || nm->event_parameters->disconnected_event->reason == WIFI_REASON_AUTH_EXPIRE || nm->event_parameters->disconnected_event->reason == WIFI_REASON_ASSOC_EXPIRE) {
  826. ESP_LOGI(TAG,"Wifi was disconnected from previous access point. Waiting to connect.");
  827. }
  828. else if(nm->event_parameters->disconnected_event->reason != WIFI_REASON_4WAY_HANDSHAKE_TIMEOUT) {
  829. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  830. network_wifi_set_connect_state(NETWORK_WIFI_STATE_FAILED);
  831. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  832. }
  833. break;
  834. default:
  835. result = EVENT_UN_HANDLED;
  836. }
  837. network_handler_print(State_Machine,false);
  838. return result;
  839. }
  840. static state_machine_result_t WIFI_CONNECTING_STATE_exit_handler(state_machine_t* const State_Machine) {
  841. network_exit_handler_print(State_Machine,true);
  842. network_set_timer(0,NULL);
  843. network_exit_handler_print(State_Machine,false);
  844. return EVENT_HANDLED;
  845. }
  846. /*********************************************************************************************
  847. * WIFI_CONNECTING_NEW_STATE
  848. */
  849. static state_machine_result_t WIFI_CONNECTING_NEW_STATE_entry_handler(state_machine_t* const State_Machine) {
  850. network_t* const nm = (network_t *)State_Machine;
  851. network_handler_entry_print(State_Machine,true);
  852. network_start_stop_dhcp_client(nm->wifi_netif, true);
  853. if(network_wifi_connect(nm->event_parameters->ssid,nm->event_parameters->password) == ESP_ERR_INVALID_ARG){
  854. network_async_fail();
  855. }
  856. FREE_AND_NULL(nm->event_parameters->ssid);
  857. FREE_AND_NULL(nm->event_parameters->password);
  858. NETWORK_EXECUTE_CB(State_Machine);
  859. network_handler_entry_print(State_Machine,false);
  860. return EVENT_HANDLED;
  861. }
  862. static state_machine_result_t WIFI_CONNECTING_NEW_STATE_handler(state_machine_t* const State_Machine) {
  863. HANDLE_GLOBAL_EVENT(State_Machine);
  864. network_handler_print(State_Machine,true);
  865. network_t* const nm = (network_t *)State_Machine;
  866. state_machine_result_t result = EVENT_HANDLED;
  867. switch (State_Machine->Event) {
  868. case EN_FAIL:
  869. ESP_LOGW(TAG,"Error connecting to access point");
  870. result = local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONNECTING_NEW_FAILED_STATE],__FUNCTION__);
  871. break;
  872. case EN_GOT_IP:
  873. network_status_update_ip_info(UPDATE_CONNECTION_OK);
  874. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE],__FUNCTION__);
  875. break;
  876. case EN_CONNECTED:
  877. ESP_LOGD(TAG,"Successfully connected to the new access point. Waiting for IP Address");
  878. result = EVENT_HANDLED;
  879. break;
  880. case EN_LOST_CONNECTION:
  881. if(((network_t *)State_Machine)->event_parameters->disconnected_event->reason == WIFI_REASON_ASSOC_LEAVE){
  882. ESP_LOGD(TAG,"Successfully disconnected from the existing access point. ");
  883. return EVENT_HANDLED;
  884. }
  885. ESP_LOGW(TAG,"Trying to connect failed");
  886. result = local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTING_NEW_FAILED_STATE],__FUNCTION__);
  887. break;
  888. default:
  889. result= EVENT_UN_HANDLED;
  890. }
  891. network_handler_print(State_Machine,false);
  892. return result;
  893. }
  894. static state_machine_result_t WIFI_CONNECTING_NEW_STATE_exit_handler(state_machine_t* const State_Machine) {
  895. network_exit_handler_print(State_Machine,true);
  896. network_set_timer(0,NULL);
  897. FREE_AND_NULL(((network_t *)State_Machine)->event_parameters->disconnected_event);
  898. network_exit_handler_print(State_Machine,false);
  899. return EVENT_HANDLED;
  900. }
  901. /*********************************************************************************************
  902. * WIFI_CONNECTING_NEW_FAILED_STATE
  903. */
  904. static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_entry_handler(state_machine_t* const State_Machine) {
  905. network_t* const nm = (network_t *)State_Machine;
  906. network_handler_entry_print(State_Machine,true);
  907. network_wifi_set_connect_state(NETWORK_WIFI_STATE_FAILED);
  908. if (nm->wifi_connected ) {
  909. // Wifi was already connected to an existing access point. Restore connection
  910. network_connect_active_ssid(State_Machine);
  911. }
  912. NETWORK_EXECUTE_CB(State_Machine);
  913. network_handler_entry_print(State_Machine,false);
  914. return EVENT_HANDLED;
  915. }
  916. static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_handler(state_machine_t* const State_Machine) {
  917. HANDLE_GLOBAL_EVENT(State_Machine);
  918. network_handler_print(State_Machine,true);
  919. state_machine_result_t result = EVENT_HANDLED;
  920. switch (State_Machine->Event) {
  921. case EN_GOT_IP:
  922. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT_AND_RESTORE);
  923. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_CONNECTED_STATE],__FUNCTION__);
  924. break;
  925. case EN_CONNECTED:
  926. ESP_LOGD(TAG,"Successfully connected to the previous access point. Waiting for IP Address");
  927. result = EVENT_HANDLED;
  928. break;
  929. case EN_LOST_CONNECTION:
  930. network_status_update_ip_info(UPDATE_FAILED_ATTEMPT);
  931. messaging_post_message(MESSAGING_ERROR, MESSAGING_CLASS_SYSTEM, "Unable to fall back to previous access point.");
  932. result = EVENT_HANDLED;
  933. break;
  934. default:
  935. result= EVENT_UN_HANDLED;
  936. }
  937. network_handler_print(State_Machine,false);
  938. return result;
  939. }
  940. static state_machine_result_t WIFI_CONNECTING_NEW_FAILED_STATE_exit_handler(state_machine_t* const State_Machine) {
  941. network_exit_handler_print(State_Machine,true);
  942. network_set_timer(0,NULL);
  943. FREE_AND_NULL(((network_t *)State_Machine)->event_parameters->disconnected_event);
  944. network_exit_handler_print(State_Machine,false);
  945. return EVENT_HANDLED;
  946. }
  947. /*********************************************************************************************
  948. * WIFI_CONNECTED_STATE
  949. */
  950. static state_machine_result_t WIFI_CONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
  951. network_t* const nm = (network_t *)State_Machine;
  952. network_handler_entry_print(State_Machine,true);
  953. nm->last_connected = esp_timer_get_time();
  954. // cancel timeout pulse
  955. network_set_timer(0,NULL);
  956. ESP_LOGD(TAG, "Checking if wifi config changed.");
  957. if (network_wifi_sta_config_changed()) {
  958. ESP_LOGD(TAG, "Wifi Config changed. Saving it.");
  959. network_wifi_save_sta_config();
  960. }
  961. ESP_LOGD(TAG, "Updating the ip info json.");
  962. network_interface_coexistence(State_Machine);
  963. nm->wifi_connected = true;
  964. NETWORK_EXECUTE_CB(State_Machine);
  965. network_handler_entry_print(State_Machine,false);
  966. return EVENT_HANDLED;
  967. }
  968. static state_machine_result_t WIFI_CONNECTED_STATE_handler(state_machine_t* const State_Machine) {
  969. HANDLE_GLOBAL_EVENT(State_Machine);
  970. network_handler_print(State_Machine,true);
  971. state_machine_result_t result = EVENT_HANDLED;
  972. switch (State_Machine->Event) {
  973. case EN_LOST_CONNECTION:
  974. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_LOST_CONNECTION_STATE],__FUNCTION__);
  975. break;
  976. default:
  977. result = EVENT_UN_HANDLED;
  978. break;
  979. }
  980. network_handler_print(State_Machine,false);
  981. return result;
  982. }
  983. static state_machine_result_t WIFI_CONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
  984. network_exit_handler_print(State_Machine,true);
  985. FREE_AND_NULL(((network_t *)State_Machine)->event_parameters->disconnected_event);
  986. network_exit_handler_print(State_Machine,false);
  987. return EVENT_HANDLED;
  988. }
  989. /*********************************************************************************************
  990. * WIFI_USER_DISCONNECTED_STATE
  991. */
  992. static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
  993. network_handler_entry_print(State_Machine,true);
  994. ESP_LOGD(TAG, " WiFi disconnected by user");
  995. network_wifi_clear_config();
  996. network_status_update_ip_info(UPDATE_USER_DISCONNECT);
  997. NETWORK_EXECUTE_CB(State_Machine);
  998. network_handler_entry_print(State_Machine,false);
  999. return EVENT_HANDLED;
  1000. }
  1001. static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_handler(state_machine_t* const State_Machine) {
  1002. HANDLE_GLOBAL_EVENT(State_Machine);
  1003. network_handler_print(State_Machine,true);
  1004. state_machine_result_t result = EVENT_HANDLED;
  1005. switch (State_Machine->Event) {
  1006. case EN_LOST_CONNECTION:
  1007. // this is a success! we're actually asking to disconnect
  1008. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  1009. break;
  1010. default:
  1011. result= EVENT_UN_HANDLED;
  1012. }
  1013. network_handler_print(State_Machine,false);
  1014. return result;
  1015. }
  1016. static state_machine_result_t WIFI_USER_DISCONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
  1017. network_exit_handler_print(State_Machine,true);
  1018. network_exit_handler_print(State_Machine,false);
  1019. return EVENT_HANDLED;
  1020. }
  1021. /*********************************************************************************************
  1022. * WIFI_LOST_CONNECTION_STATE
  1023. */
  1024. static state_machine_result_t WIFI_LOST_CONNECTION_STATE_entry_handler(state_machine_t* const State_Machine) {
  1025. network_t* const nm = (network_t *)State_Machine;
  1026. network_handler_entry_print(State_Machine,true);
  1027. ESP_LOGE(TAG, " WiFi Connection lost.");
  1028. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "WiFi Connection lost");
  1029. network_status_update_ip_info(UPDATE_LOST_CONNECTION);
  1030. network_status_safe_reset_sta_ip_string();
  1031. if (nm->last_connected > 0)
  1032. nm->total_connected_time += ((esp_timer_get_time() - nm->last_connected) / (1000 * 1000));
  1033. nm->last_connected = 0;
  1034. nm->num_disconnect++;
  1035. ESP_LOGW(TAG, " Wifi disconnected. Number of disconnects: %d, Average time connected: %d", nm->num_disconnect, nm->num_disconnect > 0 ? (nm->total_connected_time / nm->num_disconnect) : 0);
  1036. if (nm->retries < WIFI_MANAGER_MAX_RETRY) {
  1037. nm->retries++;
  1038. ESP_LOGD(TAG, " Retrying connection connection, %d/%d.", nm->retries, WIFI_MANAGER_MAX_RETRY);
  1039. network_async(EN_CONNECT);
  1040. } else {
  1041. /* In this scenario the connection was lost beyond repair */
  1042. nm->retries = 0;
  1043. ESP_LOGD(TAG,"Checking if ethernet interface is connected");
  1044. if (network_is_interface_connected(nm->eth_netif)) {
  1045. ESP_LOGW(TAG, "Cannot connect to Wifi. Falling back to Ethernet ");
  1046. network_async(EN_ETHERNET_FALLBACK);
  1047. } else {
  1048. network_status_update_ip_info(UPDATE_LOST_CONNECTION);
  1049. wifi_mode_t mode;
  1050. ESP_LOGW(TAG, " All connect retry attempts failed.");
  1051. /* put us in softAP mode first */
  1052. esp_wifi_get_mode(&mode);
  1053. if (WIFI_MODE_APSTA != mode) {
  1054. nm->STA_duration = nm->sta_polling_min_ms;
  1055. network_async_configure();
  1056. } else if (nm->STA_duration < nm->sta_polling_max_ms) {
  1057. nm->STA_duration *= 1.25;
  1058. }
  1059. /* keep polling for existing connection */
  1060. network_set_timer(nm->STA_duration, "Wifi Polling timeout");
  1061. ESP_LOGD(TAG, " STA search slow polling of %d", nm->STA_duration);
  1062. }
  1063. }
  1064. NETWORK_EXECUTE_CB(State_Machine);
  1065. network_handler_entry_print(State_Machine,false);
  1066. return EVENT_HANDLED;
  1067. }
  1068. static state_machine_result_t WIFI_LOST_CONNECTION_STATE_handler(state_machine_t* const State_Machine) {
  1069. HANDLE_GLOBAL_EVENT(State_Machine);
  1070. network_t* const nm = (network_t *)State_Machine;
  1071. state_machine_result_t result = EVENT_HANDLED;
  1072. network_handler_print(State_Machine,true);
  1073. switch (State_Machine->Event) {
  1074. case EN_CONFIGURE:
  1075. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONFIGURING_STATE],__FUNCTION__);
  1076. break;
  1077. case EN_TIMER:
  1078. ESP_LOGI(TAG, "Timer: %s ",STR_OR_ALT(nm->timer_tag,"Lost connection"));
  1079. result= local_traverse_state(State_Machine, &Wifi_Active_State[WIFI_LOST_CONNECTION_STATE],__FUNCTION__);
  1080. break;
  1081. case EN_CONNECT:
  1082. result= local_traverse_state(State_Machine, &Wifi_Configuring_State[WIFI_CONNECTING_STATE],__FUNCTION__);
  1083. break;
  1084. default:
  1085. result= EVENT_UN_HANDLED;
  1086. }
  1087. network_handler_print(State_Machine,false);
  1088. return result;
  1089. }
  1090. static state_machine_result_t WIFI_LOST_CONNECTION_STATE_exit_handler(state_machine_t* const State_Machine) {
  1091. network_exit_handler_print(State_Machine,true);
  1092. FREE_AND_NULL(((network_t *)State_Machine)->event_parameters->disconnected_event);
  1093. network_exit_handler_print(State_Machine,false);
  1094. return EVENT_HANDLED;
  1095. }
  1096. /*********************************************************************************************
  1097. * ETH_ACTIVE_CONNECTED_STATE
  1098. */
  1099. static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_entry_handler(state_machine_t* const State_Machine) {
  1100. network_t* const nm = (network_t *)State_Machine;
  1101. network_handler_entry_print(State_Machine,true);
  1102. network_status_update_ip_info(UPDATE_ETHERNET_CONNECTED);
  1103. nm->ethernet_connected = true;
  1104. // start a wifi Scan so web ui is populated with available entries
  1105. NETWORK_EXECUTE_CB(State_Machine);
  1106. network_handler_entry_print(State_Machine,false);
  1107. return EVENT_HANDLED;
  1108. }
  1109. static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_handler(state_machine_t* const State_Machine) {
  1110. HANDLE_GLOBAL_EVENT(State_Machine);
  1111. state_machine_result_t result = EVENT_HANDLED;
  1112. network_handler_print(State_Machine,true);
  1113. switch (State_Machine->Event) {
  1114. case EN_TIMER:
  1115. ESP_LOGD(TAG, "Ignoring ethernet link up timer check");
  1116. result= EVENT_HANDLED;
  1117. break;
  1118. default:
  1119. result= EVENT_UN_HANDLED;
  1120. }
  1121. network_handler_print(State_Machine,false);
  1122. return result;
  1123. }
  1124. static state_machine_result_t ETH_ACTIVE_CONNECTED_STATE_exit_handler(state_machine_t* const State_Machine) {
  1125. network_exit_handler_print(State_Machine,true);
  1126. network_exit_handler_print(State_Machine,false);
  1127. return EVENT_HANDLED;
  1128. }
  1129. static state_machine_result_t local_switch_state(state_machine_t* state_machine,
  1130. const state_t* const target_state, const char * caller) {
  1131. const state_t* source = state_machine->State;
  1132. NETWORK_PRINT_TRANSITION(true, "switch.begin", ((network_t *)state_machine)->source_state, target_state, state_machine->Event, true,caller);
  1133. state_machine_result_t result = switch_state(state_machine, target_state);
  1134. NETWORK_PRINT_TRANSITION( false,"switch.end", ((network_t *)state_machine)->source_state, target_state, state_machine->Event, true,caller);
  1135. ((network_t *)state_machine)->source_state = source;
  1136. return result;
  1137. }
  1138. static state_machine_result_t local_traverse_state(state_machine_t* const state_machine,
  1139. const state_t* const target_state, const char * caller) {
  1140. const state_t * source = state_machine->State;
  1141. NETWORK_PRINT_TRANSITION( true,"traverse.begin", ((network_t *)state_machine)->source_state, target_state, state_machine->Event, true, caller);
  1142. state_machine_result_t result = traverse_state(state_machine, target_state);
  1143. NETWORK_PRINT_TRANSITION( false,"traverse.end", ((network_t *)state_machine)->source_state, target_state, state_machine->Event, true,caller);
  1144. ((network_t *)state_machine)->source_state = source;
  1145. return result;
  1146. }
  1147. static void network_interface_coexistence(state_machine_t* state_machine) {
  1148. // this function is called whenever both wifi and ethernet are
  1149. // found to be active at the same time
  1150. network_t* nm = (network_t *)state_machine;
  1151. if (nm->wifi_connected && state_machine->Event == EN_ETH_GOT_IP) {
  1152. char* eth_reboot = config_alloc_get_default(NVS_TYPE_STR, "eth_boot", "N", 0);
  1153. network_prioritize_wifi(false);
  1154. if (strcasecmp(eth_reboot, "N")) {
  1155. ESP_LOGW(TAG, "Option eth_reboot set to reboot when ethernet is connected. Rebooting");
  1156. simple_restart();
  1157. } else {
  1158. ESP_LOGW(TAG, "Option eth_reboot set to not reboot when ethernet is connected. Using Wifi interface until next reboot");
  1159. }
  1160. FREE_AND_NULL(eth_reboot);
  1161. } else if (get_root(state_machine->State)->Id == NETWORK_ETH_ACTIVE_STATE){
  1162. messaging_post_message(MESSAGING_WARNING, MESSAGING_CLASS_SYSTEM, "Wifi Connected with Ethernet active. System reload needed");
  1163. simple_restart();
  1164. }
  1165. }