eth_worker.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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->traceroute_process = ethernet_view_process_malloc(EthWorkerProcessTraceroute, worker->config);
  21. worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset, worker->config);
  22. worker->active_process = worker->init_process;
  23. static_worker = worker;
  24. worker->state = worker->next_state = EthWorkerStateNotAllocated;
  25. worker->timer = furi_timer_alloc(dhcp_timer_callback, FuriTimerTypePeriodic, NULL);
  26. furi_timer_start(worker->timer, 1000);
  27. eth_log(EthWorkerProcessReset, "Finik Ethernet [START]");
  28. return worker;
  29. }
  30. void eth_worker_free(EthWorker* worker) {
  31. eth_log(EthWorkerProcessReset, "Finik Ethernet [STOP]");
  32. eth_run(worker, EthWorkerProcessExit);
  33. static_worker = NULL;
  34. furi_assert(worker);
  35. ethernet_view_process_free(worker->init_process);
  36. ethernet_view_process_free(worker->dhcp_process);
  37. ethernet_view_process_free(worker->stat_process);
  38. ethernet_view_process_free(worker->ping_process);
  39. ethernet_view_process_free(worker->traceroute_process);
  40. ethernet_view_process_free(worker->reset_process);
  41. ethernet_save_process_free(worker->config);
  42. furi_timer_stop(worker->timer);
  43. furi_timer_free(worker->timer);
  44. free(worker);
  45. }
  46. void eth_worker_change_state(EthWorker* worker, EthWorkerState state) {
  47. furi_assert(worker);
  48. worker->state = state;
  49. }
  50. void eth_worker_set_active_process(EthWorker* worker, EthWorkerProcess state) {
  51. furi_assert(worker);
  52. switch(state) {
  53. case EthWorkerProcessInit:
  54. worker->active_process = worker->init_process;
  55. break;
  56. case EthWorkerProcessDHCP:
  57. worker->active_process = worker->dhcp_process;
  58. break;
  59. case EthWorkerProcessStatic:
  60. worker->active_process = worker->stat_process;
  61. break;
  62. case EthWorkerProcessPing:
  63. worker->active_process = worker->ping_process;
  64. break;
  65. case EthWorkerProcessReset:
  66. worker->active_process = worker->reset_process;
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. void eth_worker_log(EthWorker* worker, const char* str) {
  73. furi_assert(worker);
  74. ethernet_save_process_print(worker->config, str);
  75. }
  76. static EthViewProcess* get_process(EthWorker* worker, EthWorkerProcess process) {
  77. furi_assert(worker);
  78. switch(process) {
  79. case EthWorkerProcessInit:
  80. return worker->init_process;
  81. case EthWorkerProcessDHCP:
  82. return worker->dhcp_process;
  83. case EthWorkerProcessStatic:
  84. return worker->stat_process;
  85. case EthWorkerProcessPing:
  86. return worker->ping_process;
  87. case EthWorkerProcessReset:
  88. return worker->reset_process;
  89. case EthWorkerProcessActive:
  90. return worker->active_process;
  91. default:
  92. return NULL;
  93. }
  94. }
  95. void eth_log(EthWorkerProcess process, const char* format, ...) {
  96. furi_assert(static_worker);
  97. va_list args;
  98. va_start(args, format);
  99. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  100. const char* string = furi_string_get_cstr(fstring);
  101. va_end(args);
  102. FURI_LOG_I(TAG, "%s", string);
  103. ethernet_save_process_print(static_worker->config, string);
  104. ethernet_view_process_print(get_process(static_worker, process), string);
  105. if(process != EthWorkerProcessReset) {
  106. ethernet_view_process_print(get_process(static_worker, EthWorkerProcessReset), string);
  107. }
  108. furi_string_free(fstring);
  109. }
  110. void eth_printf(const char* format, ...) {
  111. furi_assert(static_worker);
  112. va_list args;
  113. va_start(args, format);
  114. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  115. const char* string = furi_string_get_cstr(fstring);
  116. va_end(args);
  117. FURI_LOG_I(TAG, "%s", string);
  118. //ehternet_save_process_print(static_worker->config, string);
  119. furi_string_free(fstring);
  120. }
  121. static void eth_set_force_state(EthWorkerState state) {
  122. EthWorker* worker = static_worker;
  123. furi_assert(worker);
  124. worker->next_state = EthWorkerStateDefaultNext;
  125. if(worker->state == EthWorkerStateNotAllocated) {
  126. return;
  127. }
  128. worker->state = state;
  129. }
  130. void eth_set_state() {
  131. }
  132. void eth_set_next_state(EthWorkerState state) {
  133. EthWorker* worker = static_worker;
  134. furi_assert(worker);
  135. if(state == EthWorkerStateReset || state == EthWorkerStateStop) {
  136. eth_set_force_state(state);
  137. return;
  138. }
  139. if(worker->state == EthWorkerStateNotInited) {
  140. if(state == EthWorkerStateInit) {
  141. worker->next_state = state;
  142. }
  143. }
  144. if(worker->state == EthWorkerStateInited) {
  145. if(state == EthWorkerStateDHCP || state == EthWorkerStateStaticIp) {
  146. worker->next_state = state;
  147. }
  148. }
  149. if(worker->state == EthWorkerStateOnline) {
  150. if(state == EthWorkerStatePing) {
  151. worker->next_state = state;
  152. }
  153. }
  154. }
  155. void eth_run(EthWorker* worker, EthWorkerProcess process) {
  156. furi_assert(worker);
  157. switch(process) {
  158. case EthWorkerProcessInit:
  159. if(worker->state == EthWorkerStateNotAllocated) {
  160. worker->thread = furi_thread_alloc();
  161. furi_thread_set_name(worker->thread, "EthWorker");
  162. furi_thread_set_stack_size(worker->thread, 8192);
  163. furi_thread_set_callback(worker->thread, eth_worker_task);
  164. furi_thread_set_context(worker->thread, worker);
  165. worker->state = EthWorkerStateNotInited;
  166. furi_thread_start(worker->thread);
  167. }
  168. eth_set_next_state(EthWorkerStateInit);
  169. break;
  170. case EthWorkerProcessDHCP:
  171. eth_set_next_state(EthWorkerStateDHCP);
  172. break;
  173. case EthWorkerProcessStatic:
  174. eth_set_next_state(EthWorkerStateStaticIp);
  175. break;
  176. case EthWorkerProcessPing:
  177. eth_set_next_state(EthWorkerStatePing);
  178. break;
  179. case EthWorkerProcessReset:
  180. eth_set_next_state(EthWorkerStateReset);
  181. eth_log(EthWorkerProcessInit, "reset module");
  182. eth_log(EthWorkerProcessDHCP, "reset module");
  183. eth_log(EthWorkerProcessStatic, "reset module");
  184. eth_log(EthWorkerProcessPing, "reset module");
  185. eth_log(EthWorkerProcessReset, "reset module");
  186. break;
  187. case EthWorkerProcessExit:
  188. if(worker->state != EthWorkerStateNotAllocated) {
  189. eth_set_force_state(EthWorkerStateStop);
  190. furi_thread_join(worker->thread);
  191. furi_thread_free(worker->thread);
  192. worker->state = EthWorkerStateNotAllocated;
  193. }
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. /************************** Ethernet Worker Thread *****************************/
  200. static uint8_t ip_assigned = 0;
  201. static void W5500_Select(void) {
  202. furi_hal_gpio_write(ETH_CS_PIN, false);
  203. }
  204. static void W5500_Unselect(void) {
  205. furi_hal_gpio_write(ETH_CS_PIN, true);
  206. }
  207. static void Callback_IPAssigned(void) {
  208. eth_log(
  209. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  210. ip_assigned = 1;
  211. }
  212. static void Callback_IPConflict(void) {
  213. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  214. }
  215. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  216. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  217. }
  218. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  219. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  220. }
  221. static uint8_t W5500_ReadByte(void) {
  222. uint8_t byte;
  223. W5500_ReadBuff(&byte, sizeof(byte));
  224. return byte;
  225. }
  226. static void W5500_WriteByte(uint8_t byte) {
  227. W5500_WriteBuff(&byte, sizeof(byte));
  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_spi_bus_handle_init(&furi_hal_spi_bus_handle_external);
  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(ETH_CS_PIN, true);
  315. furi_hal_gpio_write(ETH_RESET_PIN, true);
  316. furi_hal_gpio_init(ETH_RESET_PIN, GpioModeOutputPushPull, GpioPullUp, GpioSpeedVeryHigh);
  317. //furi_hal_gpio_init(ETH_CS_PIN, GpioModeOutputPushPull, GpioPullUp, 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. if(!furi_hal_power_is_otg_enabled()) {
  337. furi_hal_power_enable_otg();
  338. }
  339. furi_delay_ms(300);
  340. furi_hal_gpio_write(ETH_RESET_PIN, false);
  341. furi_delay_ms(50);
  342. furi_hal_gpio_write(ETH_RESET_PIN, true);
  343. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  344. eth_log(EthWorkerProcessInit, "[error] W5500 init fail");
  345. eth_set_force_state(EthWorkerStateNotInited);
  346. continue;
  347. }
  348. eth_log(EthWorkerProcessInit, "W5500 inited");
  349. furi_delay_ms(90);
  350. update_WIZNETINFO(false);
  351. wizchip_setnetinfo(&gWIZNETINFO);
  352. wiz_NetInfo readed_net_info;
  353. wizchip_getnetinfo(&readed_net_info);
  354. if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) {
  355. eth_log(EthWorkerProcessInit, "[error] module not detected");
  356. eth_set_force_state(EthWorkerStateNotInited);
  357. continue;
  358. }
  359. setSHAR(gWIZNETINFO.mac);
  360. wiz_PhyConf conf;
  361. wizphy_getphyconf(&conf);
  362. eth_log(
  363. EthWorkerProcessInit,
  364. "conf %d %d %d %d",
  365. conf.by,
  366. conf.mode,
  367. conf.speed,
  368. conf.duplex);
  369. eth_log(EthWorkerProcessInit, "net info setted");
  370. eth_log(
  371. EthWorkerProcessInit,
  372. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  373. gWIZNETINFO.mac[0],
  374. gWIZNETINFO.mac[1],
  375. gWIZNETINFO.mac[2],
  376. gWIZNETINFO.mac[3],
  377. gWIZNETINFO.mac[4],
  378. gWIZNETINFO.mac[5]);
  379. eth_set_force_state(EthWorkerStateInited);
  380. } else if(worker->state == EthWorkerStateDHCP) {
  381. if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) {
  382. worker->state = EthWorkerStateInited;
  383. continue;
  384. }
  385. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  386. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  387. {
  388. // DHCP_run();
  389. // eth_log(EthWorkerProcessDHCP, "DHCP Send Discover");
  390. // furi_delay_ms(1000);
  391. // DHCP_stop();
  392. }
  393. uint8_t next_cycle = 1;
  394. uint16_t divider = 0;
  395. while(next_cycle && worker->state == EthWorkerStateDHCP) {
  396. uint8_t dhcp_ret = DHCP_run();
  397. switch(dhcp_ret) {
  398. case DHCP_IP_ASSIGN:
  399. case DHCP_IP_CHANGED:
  400. case DHCP_IP_LEASED:
  401. getIPfromDHCP(gWIZNETINFO.ip);
  402. getGWfromDHCP(gWIZNETINFO.gw);
  403. getSNfromDHCP(gWIZNETINFO.sn);
  404. getDNSfromDHCP(gWIZNETINFO.dns);
  405. gWIZNETINFO.dhcp = NETINFO_DHCP;
  406. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  407. eth_log(
  408. EthWorkerProcessDHCP, "DHCP IP Leased Time : %ld Sec", getDHCPLeasetime());
  409. break;
  410. case DHCP_FAILED:
  411. eth_log(EthWorkerProcessDHCP, "DHCP Failed");
  412. break;
  413. }
  414. furi_delay_ms(10);
  415. if(divider++ % 100 == 0) {
  416. eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 100);
  417. if(divider > 2000) {
  418. DHCP_stop();
  419. eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer");
  420. eth_set_force_state(EthWorkerStateInited);
  421. break;
  422. }
  423. }
  424. next_cycle = (dhcp_ret == DHCP_RUNNING);
  425. }
  426. if(worker->state != EthWorkerStateDHCP) {
  427. continue;
  428. }
  429. eth_log(
  430. EthWorkerProcessDHCP,
  431. "IP address:\n %d.%d.%d.%d",
  432. gWIZNETINFO.ip[0],
  433. gWIZNETINFO.ip[1],
  434. gWIZNETINFO.ip[2],
  435. gWIZNETINFO.ip[3]);
  436. eth_log(
  437. EthWorkerProcessDHCP,
  438. "SM Mask:\n %d.%d.%d.%d",
  439. gWIZNETINFO.sn[0],
  440. gWIZNETINFO.sn[1],
  441. gWIZNETINFO.sn[2],
  442. gWIZNETINFO.sn[3]);
  443. eth_log(
  444. EthWorkerProcessDHCP,
  445. "Gate way:\n %d.%d.%d.%d",
  446. gWIZNETINFO.gw[0],
  447. gWIZNETINFO.gw[1],
  448. gWIZNETINFO.gw[2],
  449. gWIZNETINFO.gw[3]);
  450. eth_log(
  451. EthWorkerProcessDHCP,
  452. "DNS Server:\n %d.%d.%d.%d",
  453. gWIZNETINFO.dns[0],
  454. gWIZNETINFO.dns[1],
  455. gWIZNETINFO.dns[2],
  456. gWIZNETINFO.dns[3]);
  457. eth_set_force_state(EthWorkerStateOnline);
  458. } else if(worker->state == EthWorkerStateStaticIp) {
  459. if(!check_phylink(worker, EthWorkerStateStaticIp, EthWorkerProcessStatic, 5000)) {
  460. worker->state = EthWorkerStateInited;
  461. continue;
  462. }
  463. eth_log(EthWorkerProcessStatic, "set static ip");
  464. load_net_parameters(worker->config);
  465. eth_log(
  466. EthWorkerProcessStatic,
  467. "IP address:\n %d.%d.%d.%d",
  468. gWIZNETINFO.ip[0],
  469. gWIZNETINFO.ip[1],
  470. gWIZNETINFO.ip[2],
  471. gWIZNETINFO.ip[3]);
  472. eth_log(
  473. EthWorkerProcessStatic,
  474. "SM Mask:\n %d.%d.%d.%d",
  475. gWIZNETINFO.sn[0],
  476. gWIZNETINFO.sn[1],
  477. gWIZNETINFO.sn[2],
  478. gWIZNETINFO.sn[3]);
  479. eth_log(
  480. EthWorkerProcessStatic,
  481. "Gate way:\n %d.%d.%d.%d",
  482. gWIZNETINFO.gw[0],
  483. gWIZNETINFO.gw[1],
  484. gWIZNETINFO.gw[2],
  485. gWIZNETINFO.gw[3]);
  486. eth_log(
  487. EthWorkerProcessStatic,
  488. "DNS Server:\n %d.%d.%d.%d",
  489. gWIZNETINFO.dns[0],
  490. gWIZNETINFO.dns[1],
  491. gWIZNETINFO.dns[2],
  492. gWIZNETINFO.dns[3]);
  493. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  494. eth_set_force_state(EthWorkerStateOnline);
  495. } else if(worker->state == EthWorkerStatePing) {
  496. uint8_t* address = worker->config->ping_ip;
  497. eth_log(
  498. EthWorkerProcessPing,
  499. "ping %d.%d.%d.%d",
  500. address[0],
  501. address[1],
  502. address[2],
  503. address[3]);
  504. const uint8_t tries = 4;
  505. uint8_t try = 0;
  506. while(try < tries && worker->state == EthWorkerStatePing) {
  507. try++;
  508. uint32_t start_time = furi_get_tick();
  509. uint8_t res = ping_auto_interface(address);
  510. uint32_t res_time = furi_get_tick();
  511. if(res == 3) {
  512. eth_log(EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time);
  513. } else {
  514. eth_log(
  515. EthWorkerProcessPing, "%d error %d, %d", try, res, res_time - start_time);
  516. break;
  517. }
  518. }
  519. if(worker->state != EthWorkerStatePing) {
  520. break;
  521. }
  522. eth_set_force_state(EthWorkerStateOnline);
  523. }
  524. furi_delay_ms(5);
  525. }
  526. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  527. furi_hal_spi_bus_handle_deinit(&furi_hal_spi_bus_handle_external);
  528. furi_hal_gpio_init_simple(ETH_RESET_PIN, GpioModeAnalog);
  529. //furi_hal_gpio_init_simple(ETH_CS_PIN, GpioModeAnalog);
  530. if(furi_hal_power_is_otg_enabled()) {
  531. furi_hal_power_disable_otg();
  532. }
  533. return 0;
  534. }