eth_worker.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. #include "eth_worker_i.h"
  2. #include "eth_worker.h"
  3. #include "eth_save_process.h"
  4. #include <furi_hal.h>
  5. #include "dhcp.h"
  6. #include "socket.h"
  7. #include <wizchip_conf.h>
  8. #define ETH_RESET_PIN (&gpio_ext_pc3)
  9. #define ETH_CS_PIN (&gpio_ext_pa4)
  10. #define TAG "EthWorker"
  11. static EthWorker* static_worker = NULL;
  12. EthWorker* eth_worker_alloc() {
  13. EthWorker* worker = malloc(sizeof(EthWorker));
  14. worker->config = ethernet_save_process_malloc();
  15. furi_assert(worker->config);
  16. worker->init_process = ethernet_view_process_malloc(EthWorkerProcessInit, worker->config);
  17. worker->dhcp_process = ethernet_view_process_malloc(EthWorkerProcessDHCP, worker->config);
  18. worker->stat_process = ethernet_view_process_malloc(EthWorkerProcessStatic, worker->config);
  19. worker->ping_process = ethernet_view_process_malloc(EthWorkerProcessPing, worker->config);
  20. worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset, worker->config);
  21. worker->active_process = worker->init_process;
  22. static_worker = worker;
  23. worker->state = worker->next_state = EthWorkerStateNotAllocated;
  24. worker->timer = furi_timer_alloc(dhcp_timer_callback, FuriTimerTypePeriodic, NULL);
  25. furi_timer_start(worker->timer, 1000);
  26. eth_log(EthWorkerProcessReset, "Finik Ethernet [START]");
  27. return worker;
  28. }
  29. void eth_worker_free(EthWorker* worker) {
  30. eth_log(EthWorkerProcessReset, "Finik Ethernet [STOP]");
  31. eth_run(worker, EthWorkerProcessExit);
  32. static_worker = NULL;
  33. furi_assert(worker);
  34. ethernet_view_process_free(worker->init_process);
  35. ethernet_view_process_free(worker->dhcp_process);
  36. ethernet_view_process_free(worker->stat_process);
  37. ethernet_view_process_free(worker->ping_process);
  38. ethernet_view_process_free(worker->reset_process);
  39. ethernet_save_process_free(worker->config);
  40. furi_timer_stop(worker->timer);
  41. furi_timer_free(worker->timer);
  42. free(worker);
  43. }
  44. void eth_worker_change_state(EthWorker* worker, EthWorkerState state) {
  45. furi_assert(worker);
  46. worker->state = state;
  47. }
  48. void eth_worker_set_active_process(EthWorker* worker, EthWorkerProcess state) {
  49. furi_assert(worker);
  50. switch(state) {
  51. case EthWorkerProcessInit:
  52. worker->active_process = worker->init_process;
  53. break;
  54. case EthWorkerProcessDHCP:
  55. worker->active_process = worker->dhcp_process;
  56. break;
  57. case EthWorkerProcessStatic:
  58. worker->active_process = worker->stat_process;
  59. break;
  60. case EthWorkerProcessPing:
  61. worker->active_process = worker->ping_process;
  62. break;
  63. case EthWorkerProcessReset:
  64. worker->active_process = worker->reset_process;
  65. break;
  66. default:
  67. break;
  68. }
  69. }
  70. void eth_worker_log(EthWorker* worker, const char* str) {
  71. furi_assert(worker);
  72. ethernet_save_process_print(worker->config, str);
  73. }
  74. static EthViewProcess* get_process(EthWorker* worker, EthWorkerProcess process) {
  75. furi_assert(worker);
  76. switch(process) {
  77. case EthWorkerProcessInit:
  78. return worker->init_process;
  79. case EthWorkerProcessDHCP:
  80. return worker->dhcp_process;
  81. case EthWorkerProcessStatic:
  82. return worker->stat_process;
  83. case EthWorkerProcessPing:
  84. return worker->ping_process;
  85. case EthWorkerProcessReset:
  86. return worker->reset_process;
  87. case EthWorkerProcessActive:
  88. return worker->active_process;
  89. default:
  90. return NULL;
  91. }
  92. }
  93. void eth_log(EthWorkerProcess process, const char* format, ...) {
  94. furi_assert(static_worker);
  95. va_list args;
  96. va_start(args, format);
  97. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  98. const char* string = furi_string_get_cstr(fstring);
  99. va_end(args);
  100. FURI_LOG_I(TAG, "%s", string);
  101. ethernet_save_process_print(static_worker->config, string);
  102. ethernet_view_process_print(get_process(static_worker, process), string);
  103. if(process != EthWorkerProcessReset) {
  104. ethernet_view_process_print(get_process(static_worker, EthWorkerProcessReset), string);
  105. }
  106. furi_string_free(fstring);
  107. }
  108. void eth_printf(const char* format, ...) {
  109. furi_assert(static_worker);
  110. va_list args;
  111. va_start(args, format);
  112. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  113. const char* string = furi_string_get_cstr(fstring);
  114. va_end(args);
  115. FURI_LOG_I(TAG, "%s", string);
  116. //ehternet_save_process_print(static_worker->config, string);
  117. furi_string_free(fstring);
  118. }
  119. static void eth_set_force_state(EthWorkerState state) {
  120. EthWorker* worker = static_worker;
  121. furi_assert(worker);
  122. worker->next_state = EthWorkerStateDefaultNext;
  123. if(worker->state == EthWorkerStateNotAllocated) {
  124. return;
  125. }
  126. worker->state = state;
  127. }
  128. void eth_set_state() {
  129. }
  130. void eth_set_next_state(EthWorkerState state) {
  131. EthWorker* worker = static_worker;
  132. furi_assert(worker);
  133. if(state == EthWorkerStateReset || state == EthWorkerStateStop) {
  134. eth_set_force_state(state);
  135. return;
  136. }
  137. if(worker->state == EthWorkerStateNotInited) {
  138. if(state == EthWorkerStateInit) {
  139. worker->next_state = state;
  140. }
  141. }
  142. if(worker->state == EthWorkerStateInited) {
  143. if(state == EthWorkerStateDHCP || state == EthWorkerStateStaticIp) {
  144. worker->next_state = state;
  145. }
  146. }
  147. if(worker->state == EthWorkerStateOnline) {
  148. if(state == EthWorkerStatePing) {
  149. worker->next_state = state;
  150. }
  151. }
  152. }
  153. void eth_run(EthWorker* worker, EthWorkerProcess process) {
  154. furi_assert(worker);
  155. switch(process) {
  156. case EthWorkerProcessInit:
  157. if(worker->state == EthWorkerStateNotAllocated) {
  158. worker->thread = furi_thread_alloc();
  159. furi_thread_set_name(worker->thread, "EthWorker");
  160. furi_thread_set_stack_size(worker->thread, 8192);
  161. furi_thread_set_callback(worker->thread, eth_worker_task);
  162. furi_thread_set_context(worker->thread, worker);
  163. worker->state = EthWorkerStateNotInited;
  164. furi_thread_start(worker->thread);
  165. }
  166. eth_set_next_state(EthWorkerStateInit);
  167. break;
  168. case EthWorkerProcessDHCP:
  169. eth_set_next_state(EthWorkerStateDHCP);
  170. break;
  171. case EthWorkerProcessStatic:
  172. eth_set_next_state(EthWorkerStateStaticIp);
  173. break;
  174. case EthWorkerProcessPing:
  175. eth_set_next_state(EthWorkerStatePing);
  176. break;
  177. case EthWorkerProcessReset:
  178. eth_set_next_state(EthWorkerStateReset);
  179. eth_log(EthWorkerProcessInit, "reset module");
  180. eth_log(EthWorkerProcessDHCP, "reset module");
  181. eth_log(EthWorkerProcessStatic, "reset module");
  182. eth_log(EthWorkerProcessPing, "reset module");
  183. eth_log(EthWorkerProcessReset, "reset module");
  184. break;
  185. case EthWorkerProcessExit:
  186. if(worker->state != EthWorkerStateNotAllocated) {
  187. eth_set_force_state(EthWorkerStateStop);
  188. furi_thread_join(worker->thread);
  189. furi_thread_free(worker->thread);
  190. worker->state = EthWorkerStateNotAllocated;
  191. }
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. /************************** Ethernet Worker Thread *****************************/
  198. static uint8_t ip_assigned = 0;
  199. static void W5500_Select(void) {
  200. furi_hal_gpio_write(ETH_CS_PIN, false);
  201. }
  202. static void W5500_Unselect(void) {
  203. furi_hal_gpio_write(ETH_CS_PIN, true);
  204. }
  205. static void Callback_IPAssigned(void) {
  206. eth_log(
  207. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  208. ip_assigned = 1;
  209. }
  210. static void Callback_IPConflict(void) {
  211. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  212. }
  213. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  214. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  215. }
  216. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  217. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  218. }
  219. static uint8_t W5500_ReadByte(void) {
  220. uint8_t byte;
  221. W5500_ReadBuff(&byte, sizeof(byte));
  222. return byte;
  223. }
  224. static void W5500_WriteByte(uint8_t byte) {
  225. W5500_WriteBuff(&byte, sizeof(byte));
  226. }
  227. static wiz_NetInfo gWIZNETINFO;
  228. void update_WIZNETINFO(uint8_t is_dhcp) {
  229. furi_assert(static_worker);
  230. memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
  231. if(is_dhcp) {
  232. memset(gWIZNETINFO.ip, 0, 4);
  233. memset(gWIZNETINFO.sn, 0, 4);
  234. memset(gWIZNETINFO.gw, 0, 4);
  235. memset(gWIZNETINFO.dns, 0, 4);
  236. gWIZNETINFO.dhcp = NETINFO_DHCP;
  237. } else {
  238. memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
  239. memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
  240. memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
  241. memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
  242. gWIZNETINFO.dhcp = NETINFO_STATIC;
  243. }
  244. }
  245. int check_phylink(EthWorker* worker, EthWorkerState state, EthWorkerProcess proc, int timeout) {
  246. uint32_t start_time = furi_get_tick();
  247. uint32_t last_log_time = start_time;
  248. eth_log(proc, "phy link check 0");
  249. for(;;) {
  250. if(furi_get_tick() > start_time + timeout) {
  251. eth_log(proc, "phy link timeout");
  252. break;
  253. }
  254. if(worker->state != state) {
  255. eth_log(proc, "state changed");
  256. break;
  257. }
  258. uint8_t link = PHY_LINK_OFF;
  259. if(ctlwizchip(CW_GET_PHYLINK, (void*)&link) == -1) {
  260. eth_log(proc, "Unknown PHY link status");
  261. break;
  262. }
  263. if(link != PHY_LINK_OFF) {
  264. eth_log(proc, "phy link on");
  265. return 1;
  266. }
  267. furi_delay_ms(20);
  268. if(furi_get_tick() > last_log_time + 1000) {
  269. eth_log(proc, "phy link check %d", (last_log_time - start_time) / 1000);
  270. last_log_time = furi_get_tick();
  271. }
  272. }
  273. return 0;
  274. }
  275. #define DHCP_SOCKET 0
  276. uint8_t ping_auto(uint8_t s, uint8_t* addr);
  277. static void load_net_parameters(const EthernetSaveConfig* cfg) {
  278. gWIZNETINFO.gw[0] = cfg->gateway[0];
  279. gWIZNETINFO.gw[1] = cfg->gateway[1];
  280. gWIZNETINFO.gw[2] = cfg->gateway[2];
  281. gWIZNETINFO.gw[3] = cfg->gateway[3];
  282. gWIZNETINFO.sn[0] = cfg->mask[0];
  283. gWIZNETINFO.sn[1] = cfg->mask[1];
  284. gWIZNETINFO.sn[2] = cfg->mask[2];
  285. gWIZNETINFO.sn[3] = cfg->mask[3];
  286. gWIZNETINFO.mac[0] = cfg->mac[0];
  287. gWIZNETINFO.mac[1] = cfg->mac[1];
  288. gWIZNETINFO.mac[2] = cfg->mac[2];
  289. gWIZNETINFO.mac[3] = cfg->mac[3];
  290. gWIZNETINFO.mac[4] = cfg->mac[4];
  291. gWIZNETINFO.mac[5] = cfg->mac[5];
  292. gWIZNETINFO.ip[0] = cfg->ip[0];
  293. gWIZNETINFO.ip[1] = cfg->ip[1];
  294. gWIZNETINFO.ip[2] = cfg->ip[2];
  295. gWIZNETINFO.ip[3] = cfg->ip[3];
  296. gWIZNETINFO.dns[0] = cfg->dns[0];
  297. gWIZNETINFO.dns[1] = cfg->dns[1];
  298. gWIZNETINFO.dns[2] = cfg->dns[2];
  299. gWIZNETINFO.dns[3] = cfg->dns[3];
  300. gWIZNETINFO.dhcp = NETINFO_STATIC;
  301. }
  302. int32_t eth_worker_task(void* context) {
  303. furi_assert(context);
  304. EthWorker* worker = (EthWorker*)context;
  305. furi_hal_spi_bus_handle_init(&furi_hal_spi_bus_handle_external);
  306. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  307. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  308. uint8_t dhcp_buffer[2048];
  309. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  310. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  311. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  312. //furi_hal_gpio_write(ETH_CS_PIN, true);
  313. furi_hal_gpio_write(ETH_RESET_PIN, true);
  314. furi_hal_gpio_init(ETH_RESET_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  315. //furi_hal_gpio_init(ETH_CS_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  316. while(worker->state != EthWorkerStateStop) {
  317. if(worker->state == EthWorkerStateNotInited) {
  318. if(worker->next_state == EthWorkerStateInit) {
  319. worker->state = worker->next_state;
  320. }
  321. } else if(worker->state == EthWorkerStateInited) {
  322. if(worker->next_state == EthWorkerStateDHCP ||
  323. worker->next_state == EthWorkerStateStaticIp) {
  324. worker->state = worker->next_state;
  325. }
  326. } else if(worker->state == EthWorkerStateOnline) {
  327. if(worker->next_state == EthWorkerStatePing) {
  328. worker->state = worker->next_state;
  329. }
  330. } else if(worker->state == EthWorkerStateReset) {
  331. worker->state = EthWorkerStateNotInited;
  332. }
  333. if(worker->state == EthWorkerStateInit) {
  334. if(!furi_hal_power_is_otg_enabled()) {
  335. furi_hal_power_enable_otg();
  336. }
  337. furi_delay_ms(300);
  338. furi_hal_gpio_write(ETH_RESET_PIN, false);
  339. furi_delay_ms(50);
  340. furi_hal_gpio_write(ETH_RESET_PIN, true);
  341. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  342. eth_log(EthWorkerProcessInit, "[error] W5500 init fail");
  343. eth_set_force_state(EthWorkerStateNotInited);
  344. continue;
  345. }
  346. eth_log(EthWorkerProcessInit, "W5500 inited");
  347. furi_delay_ms(90);
  348. update_WIZNETINFO(false);
  349. wizchip_setnetinfo(&gWIZNETINFO);
  350. wiz_NetInfo readed_net_info;
  351. wizchip_getnetinfo(&readed_net_info);
  352. if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) {
  353. eth_log(EthWorkerProcessInit, "[error] module not detected");
  354. eth_set_force_state(EthWorkerStateNotInited);
  355. continue;
  356. }
  357. setSHAR(gWIZNETINFO.mac);
  358. wiz_PhyConf conf;
  359. wizphy_getphyconf(&conf);
  360. eth_log(
  361. EthWorkerProcessInit,
  362. "conf %d %d %d %d",
  363. conf.by,
  364. conf.mode,
  365. conf.speed,
  366. conf.duplex);
  367. eth_log(EthWorkerProcessInit, "net info setted");
  368. eth_log(
  369. EthWorkerProcessInit,
  370. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  371. gWIZNETINFO.mac[0],
  372. gWIZNETINFO.mac[1],
  373. gWIZNETINFO.mac[2],
  374. gWIZNETINFO.mac[3],
  375. gWIZNETINFO.mac[4],
  376. gWIZNETINFO.mac[5]);
  377. eth_set_force_state(EthWorkerStateInited);
  378. } else if(worker->state == EthWorkerStateDHCP) {
  379. if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) {
  380. worker->state = EthWorkerStateInited;
  381. continue;
  382. }
  383. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  384. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  385. {
  386. // DHCP_run();
  387. // eth_log(EthWorkerProcessDHCP, "DHCP Send Discover");
  388. // furi_delay_ms(1000);
  389. // DHCP_stop();
  390. }
  391. uint8_t next_cycle = 1;
  392. uint16_t divider = 0;
  393. while(next_cycle && worker->state == EthWorkerStateDHCP) {
  394. uint8_t dhcp_ret = DHCP_run();
  395. switch(dhcp_ret) {
  396. case DHCP_IP_ASSIGN:
  397. case DHCP_IP_CHANGED:
  398. case DHCP_IP_LEASED:
  399. getIPfromDHCP(gWIZNETINFO.ip);
  400. getGWfromDHCP(gWIZNETINFO.gw);
  401. getSNfromDHCP(gWIZNETINFO.sn);
  402. getDNSfromDHCP(gWIZNETINFO.dns);
  403. gWIZNETINFO.dhcp = NETINFO_DHCP;
  404. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  405. eth_log(
  406. EthWorkerProcessDHCP, "DHCP IP Leased Time : %ld Sec", getDHCPLeasetime());
  407. break;
  408. case DHCP_FAILED:
  409. eth_log(EthWorkerProcessDHCP, "DHCP Failed");
  410. break;
  411. }
  412. furi_delay_ms(10);
  413. if(divider++ % 100 == 0) {
  414. eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 100);
  415. if(divider > 2000) {
  416. DHCP_stop();
  417. eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer");
  418. eth_set_force_state(EthWorkerStateInited);
  419. break;
  420. }
  421. }
  422. next_cycle = (dhcp_ret == DHCP_RUNNING);
  423. }
  424. if(worker->state != EthWorkerStateDHCP) {
  425. continue;
  426. }
  427. eth_log(
  428. EthWorkerProcessDHCP,
  429. "IP address:\n %d.%d.%d.%d",
  430. gWIZNETINFO.ip[0],
  431. gWIZNETINFO.ip[1],
  432. gWIZNETINFO.ip[2],
  433. gWIZNETINFO.ip[3]);
  434. eth_log(
  435. EthWorkerProcessDHCP,
  436. "SM Mask:\n %d.%d.%d.%d",
  437. gWIZNETINFO.sn[0],
  438. gWIZNETINFO.sn[1],
  439. gWIZNETINFO.sn[2],
  440. gWIZNETINFO.sn[3]);
  441. eth_log(
  442. EthWorkerProcessDHCP,
  443. "Gate way:\n %d.%d.%d.%d",
  444. gWIZNETINFO.gw[0],
  445. gWIZNETINFO.gw[1],
  446. gWIZNETINFO.gw[2],
  447. gWIZNETINFO.gw[3]);
  448. eth_log(
  449. EthWorkerProcessDHCP,
  450. "DNS Server:\n %d.%d.%d.%d",
  451. gWIZNETINFO.dns[0],
  452. gWIZNETINFO.dns[1],
  453. gWIZNETINFO.dns[2],
  454. gWIZNETINFO.dns[3]);
  455. eth_set_force_state(EthWorkerStateOnline);
  456. } else if(worker->state == EthWorkerStateStaticIp) {
  457. if(!check_phylink(worker, EthWorkerStateStaticIp, EthWorkerProcessStatic, 5000)) {
  458. worker->state = EthWorkerStateInited;
  459. continue;
  460. }
  461. eth_log(EthWorkerProcessStatic, "set static ip");
  462. load_net_parameters(worker->config);
  463. eth_log(
  464. EthWorkerProcessStatic,
  465. "IP address:\n %d.%d.%d.%d",
  466. gWIZNETINFO.ip[0],
  467. gWIZNETINFO.ip[1],
  468. gWIZNETINFO.ip[2],
  469. gWIZNETINFO.ip[3]);
  470. eth_log(
  471. EthWorkerProcessStatic,
  472. "SM Mask:\n %d.%d.%d.%d",
  473. gWIZNETINFO.sn[0],
  474. gWIZNETINFO.sn[1],
  475. gWIZNETINFO.sn[2],
  476. gWIZNETINFO.sn[3]);
  477. eth_log(
  478. EthWorkerProcessStatic,
  479. "Gate way:\n %d.%d.%d.%d",
  480. gWIZNETINFO.gw[0],
  481. gWIZNETINFO.gw[1],
  482. gWIZNETINFO.gw[2],
  483. gWIZNETINFO.gw[3]);
  484. eth_log(
  485. EthWorkerProcessStatic,
  486. "DNS Server:\n %d.%d.%d.%d",
  487. gWIZNETINFO.dns[0],
  488. gWIZNETINFO.dns[1],
  489. gWIZNETINFO.dns[2],
  490. gWIZNETINFO.dns[3]);
  491. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  492. eth_set_force_state(EthWorkerStateOnline);
  493. } else if(worker->state == EthWorkerStatePing) {
  494. uint8_t* address = worker->config->ping_ip;
  495. eth_log(
  496. EthWorkerProcessPing,
  497. "ping %d.%d.%d.%d",
  498. address[0],
  499. address[1],
  500. address[2],
  501. address[3]);
  502. const uint8_t tries = 4;
  503. uint8_t try = 0;
  504. while(try < tries && worker->state == EthWorkerStatePing) {
  505. try++;
  506. uint32_t start_time = furi_get_tick();
  507. uint8_t res = ping_auto_interface(address);
  508. uint32_t res_time = furi_get_tick();
  509. if(res == 3) {
  510. eth_log(EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time);
  511. } else {
  512. eth_log(
  513. EthWorkerProcessPing, "%d error %d, %d", try, res, res_time - start_time);
  514. break;
  515. }
  516. }
  517. if(worker->state != EthWorkerStatePing) {
  518. break;
  519. }
  520. eth_set_force_state(EthWorkerStateOnline);
  521. }
  522. furi_delay_ms(5);
  523. }
  524. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  525. furi_hal_spi_bus_handle_deinit(&furi_hal_spi_bus_handle_external);
  526. furi_hal_gpio_init_simple(ETH_RESET_PIN, GpioModeAnalog);
  527. //furi_hal_gpio_init_simple(ETH_CS_PIN, GpioModeAnalog);
  528. if(furi_hal_power_is_otg_enabled()) {
  529. furi_hal_power_disable_otg();
  530. }
  531. return 0;
  532. }