eth_worker.c 20 KB

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