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