network_manager_handlers.c 49 KB

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