network_manager_handlers.c 49 KB

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