eth_worker.c 20 KB

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