eth_worker.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 "stm32wbxx_hal_gpio.h"
  8. #include <wizchip_conf.h>
  9. #define TAG "EthWorker"
  10. static EthWorker* static_worker = NULL;
  11. EthWorker* eth_worker_alloc() {
  12. EthWorker* worker = malloc(sizeof(EthWorker));
  13. worker->config = ehternet_save_process_malloc();
  14. furi_assert(worker->config);
  15. worker->init_process = ethernet_view_process_malloc(EthWorkerProcessInit, worker->config);
  16. worker->dhcp_process = ethernet_view_process_malloc(EthWorkerProcessDHCP, worker->config);
  17. worker->stat_process = ethernet_view_process_malloc(EthWorkerProcessStatic, worker->config);
  18. worker->ping_process = ethernet_view_process_malloc(EthWorkerProcessPing, worker->config);
  19. worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset, worker->config);
  20. worker->active_process = worker->init_process;
  21. static_worker = worker;
  22. worker->state = worker->next_state = EthWorkerStateNotAllocated;
  23. worker->timer = furi_timer_alloc(dhcp_timer_callback, FuriTimerTypePeriodic, NULL);
  24. furi_timer_start(worker->timer, 1000);
  25. eth_log(EthWorkerProcessReset, "Finik Ethernet [START]");
  26. return worker;
  27. }
  28. void eth_worker_free(EthWorker* worker) {
  29. eth_log(EthWorkerProcessReset, "Finik Ethernet [STOP]");
  30. eth_run(worker, EthWorkerProcessExit);
  31. static_worker = NULL;
  32. furi_assert(worker);
  33. ethernet_view_process_free(worker->init_process);
  34. ethernet_view_process_free(worker->dhcp_process);
  35. ethernet_view_process_free(worker->stat_process);
  36. ethernet_view_process_free(worker->ping_process);
  37. ethernet_view_process_free(worker->reset_process);
  38. ehternet_save_process_free(worker->config);
  39. furi_timer_stop(worker->timer);
  40. furi_timer_free(worker->timer);
  41. free(worker);
  42. }
  43. void eth_worker_change_state(EthWorker* worker, EthWorkerState state) {
  44. furi_assert(worker);
  45. worker->state = state;
  46. }
  47. void eth_worker_set_active_process(EthWorker* worker, EthWorkerProcess state) {
  48. furi_assert(worker);
  49. switch(state) {
  50. case EthWorkerProcessInit:
  51. worker->active_process = worker->init_process;
  52. break;
  53. case EthWorkerProcessDHCP:
  54. worker->active_process = worker->dhcp_process;
  55. break;
  56. case EthWorkerProcessStatic:
  57. worker->active_process = worker->stat_process;
  58. break;
  59. case EthWorkerProcessPing:
  60. worker->active_process = worker->ping_process;
  61. break;
  62. case EthWorkerProcessReset:
  63. worker->active_process = worker->reset_process;
  64. break;
  65. }
  66. }
  67. void eth_worker_log(EthWorker* worker, const char* str) {
  68. furi_assert(worker);
  69. ehternet_save_process_print(worker->config, str);
  70. }
  71. static EthViewProcess* get_process(EthWorker* worker, EthWorkerProcess process) {
  72. furi_assert(worker);
  73. switch(process) {
  74. case EthWorkerProcessInit:
  75. return worker->init_process;
  76. case EthWorkerProcessDHCP:
  77. return worker->dhcp_process;
  78. case EthWorkerProcessStatic:
  79. return worker->stat_process;
  80. case EthWorkerProcessPing:
  81. return worker->ping_process;
  82. case EthWorkerProcessReset:
  83. return worker->reset_process;
  84. case EthWorkerProcessActive:
  85. return worker->active_process;
  86. default:
  87. NULL;
  88. }
  89. }
  90. void eth_log(EthWorkerProcess process, const char* format, ...) {
  91. furi_assert(static_worker);
  92. va_list args;
  93. va_start(args, format);
  94. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  95. const char* string = furi_string_get_cstr(fstring);
  96. va_end(args);
  97. FURI_LOG_I(TAG, "%s", string);
  98. ehternet_save_process_print(static_worker->config, string);
  99. ethernet_view_process_print(get_process(static_worker, process), string);
  100. if(process != EthWorkerProcessReset) {
  101. ethernet_view_process_print(get_process(static_worker, EthWorkerProcessReset), string);
  102. }
  103. furi_string_free(fstring);
  104. }
  105. void eth_printf(const char* format, ...) {
  106. furi_assert(static_worker);
  107. va_list args;
  108. va_start(args, format);
  109. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  110. const char* string = furi_string_get_cstr(fstring);
  111. va_end(args);
  112. FURI_LOG_I(TAG, "%s", string);
  113. //ehternet_save_process_print(static_worker->config, string);
  114. furi_string_free(fstring);
  115. }
  116. static void eth_set_force_state(EthWorkerState state) {
  117. EthWorker* worker = static_worker;
  118. furi_assert(worker);
  119. worker->next_state = EthWorkerStateDefaultNext;
  120. if(worker->state == EthWorkerStateNotAllocated) {
  121. return;
  122. }
  123. worker->state = state;
  124. }
  125. void eth_set_state() {
  126. }
  127. void eth_set_next_state(EthWorkerState state) {
  128. EthWorker* worker = static_worker;
  129. furi_assert(worker);
  130. if(state == EthWorkerStateReset || state == EthWorkerStateStop) {
  131. eth_set_force_state(state);
  132. return;
  133. }
  134. if(worker->state == EthWorkerStateNotInited) {
  135. if(state == EthWorkerStateInit) {
  136. worker->next_state = state;
  137. }
  138. }
  139. if(worker->state == EthWorkerStateInited) {
  140. if(state == EthWorkerStateDHCP || state == EthWorkerStateStaticIp) {
  141. worker->next_state = state;
  142. }
  143. }
  144. if(worker->state == EthWorkerStateOnline) {
  145. if(state == EthWorkerStatePing) {
  146. worker->next_state = state;
  147. }
  148. }
  149. }
  150. void eth_run(EthWorker* worker, EthWorkerProcess process) {
  151. furi_assert(worker);
  152. switch(process) {
  153. case EthWorkerProcessInit:
  154. if(worker->state == EthWorkerStateNotAllocated) {
  155. worker->thread = furi_thread_alloc();
  156. furi_thread_set_name(worker->thread, "EthWorker");
  157. furi_thread_set_stack_size(worker->thread, 8192);
  158. furi_thread_set_callback(worker->thread, eth_worker_task);
  159. furi_thread_set_context(worker->thread, worker);
  160. worker->state = EthWorkerStateNotInited;
  161. furi_thread_start(worker->thread);
  162. }
  163. eth_set_next_state(EthWorkerStateInit);
  164. break;
  165. case EthWorkerProcessDHCP:
  166. eth_set_next_state(EthWorkerStateDHCP);
  167. break;
  168. case EthWorkerProcessStatic:
  169. eth_set_next_state(EthWorkerStateStaticIp);
  170. break;
  171. case EthWorkerProcessPing:
  172. eth_set_next_state(EthWorkerStatePing);
  173. break;
  174. case EthWorkerProcessReset:
  175. eth_set_next_state(EthWorkerStateReset);
  176. eth_log(EthWorkerProcessInit, "reset module");
  177. eth_log(EthWorkerProcessDHCP, "reset module");
  178. eth_log(EthWorkerProcessStatic, "reset module");
  179. eth_log(EthWorkerProcessPing, "reset module");
  180. eth_log(EthWorkerProcessReset, "reset module");
  181. break;
  182. case EthWorkerProcessExit:
  183. if(worker->state != EthWorkerStateNotAllocated) {
  184. eth_set_force_state(EthWorkerStateStop);
  185. furi_thread_join(worker->thread);
  186. furi_thread_free(worker->thread);
  187. worker->state = EthWorkerStateNotAllocated;
  188. }
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. /************************** Ethernet Worker Thread *****************************/
  195. static uint8_t ip_assigned = 0;
  196. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  197. static GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  198. static void W5500_Select(void) {
  199. furi_hal_gpio_write(&cspin, false);
  200. }
  201. static void W5500_Unselect(void) {
  202. furi_hal_gpio_write(&cspin, true);
  203. }
  204. static void Callback_IPAssigned(void) {
  205. eth_log(
  206. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  207. ip_assigned = 1;
  208. }
  209. static void Callback_IPConflict(void) {
  210. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  211. }
  212. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  213. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  214. }
  215. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  216. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  217. }
  218. static uint8_t W5500_ReadByte(void) {
  219. uint8_t byte;
  220. W5500_ReadBuff(&byte, sizeof(byte));
  221. return byte;
  222. }
  223. static void W5500_WriteByte(uint8_t byte) {
  224. W5500_WriteBuff(&byte, sizeof(byte));
  225. }
  226. static void wait_ms(int ms) {
  227. furi_delay_ms(ms);
  228. }
  229. static wiz_NetInfo gWIZNETINFO;
  230. void update_WIZNETINFO(uint8_t is_dhcp) {
  231. furi_assert(static_worker);
  232. memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
  233. if(is_dhcp) {
  234. memset(gWIZNETINFO.ip, 0, 4);
  235. memset(gWIZNETINFO.sn, 0, 4);
  236. memset(gWIZNETINFO.gw, 0, 4);
  237. memset(gWIZNETINFO.dns, 0, 4);
  238. gWIZNETINFO.dhcp = NETINFO_DHCP;
  239. } else {
  240. memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
  241. memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
  242. memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
  243. memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
  244. gWIZNETINFO.dhcp = NETINFO_STATIC;
  245. }
  246. }
  247. int check_phylink(EthWorker* worker, EthWorkerState state, EthWorkerProcess proc, int timeout) {
  248. uint32_t start_time = furi_get_tick();
  249. uint32_t last_log_time = start_time;
  250. eth_log(proc, "phy link check 0");
  251. for(;;) {
  252. if(furi_get_tick() > start_time + timeout) {
  253. eth_log(proc, "phy link timeout");
  254. break;
  255. }
  256. if(worker->state != state) {
  257. eth_log(proc, "state changed");
  258. break;
  259. }
  260. uint8_t link = PHY_LINK_OFF;
  261. if(ctlwizchip(CW_GET_PHYLINK, (void*)&link) == -1) {
  262. eth_log(proc, "Unknown PHY link status");
  263. break;
  264. }
  265. if(link != PHY_LINK_OFF) {
  266. eth_log(proc, "phy link on");
  267. return 1;
  268. }
  269. furi_delay_ms(20);
  270. if(furi_get_tick() > last_log_time + 1000) {
  271. eth_log(proc, "phy link check %d", (last_log_time - start_time) / 1000);
  272. last_log_time = furi_get_tick();
  273. }
  274. }
  275. return 0;
  276. }
  277. #define DHCP_SOCKET 0
  278. uint8_t ping_auto(uint8_t s, uint8_t* addr);
  279. static void load_net_parameters(const EthernetSaveConfig* cfg) {
  280. gWIZNETINFO.gw[0] = cfg->gateway[0];
  281. gWIZNETINFO.gw[1] = cfg->gateway[1];
  282. gWIZNETINFO.gw[2] = cfg->gateway[2];
  283. gWIZNETINFO.gw[3] = cfg->gateway[3];
  284. gWIZNETINFO.sn[0] = cfg->mask[0];
  285. gWIZNETINFO.sn[1] = cfg->mask[1];
  286. gWIZNETINFO.sn[2] = cfg->mask[2];
  287. gWIZNETINFO.sn[3] = cfg->mask[3];
  288. gWIZNETINFO.mac[0] = cfg->mac[0];
  289. gWIZNETINFO.mac[1] = cfg->mac[1];
  290. gWIZNETINFO.mac[2] = cfg->mac[2];
  291. gWIZNETINFO.mac[3] = cfg->mac[3];
  292. gWIZNETINFO.mac[4] = cfg->mac[4];
  293. gWIZNETINFO.mac[5] = cfg->mac[5];
  294. gWIZNETINFO.ip[0] = cfg->ip[0];
  295. gWIZNETINFO.ip[1] = cfg->ip[1];
  296. gWIZNETINFO.ip[2] = cfg->ip[2];
  297. gWIZNETINFO.ip[3] = cfg->ip[3];
  298. gWIZNETINFO.dns[0] = cfg->dns[0];
  299. gWIZNETINFO.dns[1] = cfg->dns[1];
  300. gWIZNETINFO.dns[2] = cfg->dns[2];
  301. gWIZNETINFO.dns[3] = cfg->dns[3];
  302. gWIZNETINFO.dhcp = NETINFO_STATIC;
  303. }
  304. int32_t eth_worker_task(void* context) {
  305. furi_assert(context);
  306. EthWorker* worker = (EthWorker*)context;
  307. furi_hal_power_insomnia_enter();
  308. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  309. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  310. uint8_t dhcp_buffer[2048];
  311. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  312. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  313. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  314. furi_hal_gpio_write(&resetpin, true);
  315. furi_hal_gpio_write(&cspin, true);
  316. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  317. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  318. while(worker->state != EthWorkerStateStop) {
  319. if(worker->state == EthWorkerStateNotInited) {
  320. if(worker->next_state == EthWorkerStateInit) {
  321. worker->state = worker->next_state;
  322. }
  323. } else if(worker->state == EthWorkerStateInited) {
  324. if(worker->next_state == EthWorkerStateDHCP ||
  325. worker->next_state == EthWorkerStateStaticIp) {
  326. worker->state = worker->next_state;
  327. }
  328. } else if(worker->state == EthWorkerStateOnline) {
  329. if(worker->next_state == EthWorkerStatePing) {
  330. worker->state = worker->next_state;
  331. }
  332. } else if(worker->state == EthWorkerStateReset) {
  333. worker->state = EthWorkerStateNotInited;
  334. }
  335. if(worker->state == EthWorkerStateInit) {
  336. furi_hal_power_enable_otg();
  337. furi_delay_ms(300);
  338. furi_hal_gpio_write(&resetpin, false);
  339. furi_delay_ms(50);
  340. furi_hal_gpio_write(&resetpin, 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(50);
  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* adress = worker->config->ping_ip;
  495. eth_log(
  496. EthWorkerProcessPing,
  497. "ping %d.%d.%d.%d",
  498. adress[0],
  499. adress[1],
  500. adress[2],
  501. adress[3]);
  502. const uint8_t tryes = 4;
  503. uint8_t try = 0;
  504. while(try < tryes && worker->state == EthWorkerStatePing) {
  505. try++;
  506. uint32_t start_time = furi_get_tick();
  507. uint8_t res = ping_auto_interface(adress);
  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_power_disable_otg();
  526. furi_hal_power_insomnia_exit();
  527. return 0;
  528. }