network_manager_handlers.c 49 KB

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