eth_worker.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. static void eth_set_force_state(EthWorkerState state) {
  102. EthWorker* worker = static_worker;
  103. furi_assert(worker);
  104. worker->next_state = EthWorkerStateDefaultNext;
  105. if(worker->state == EthWorkerStateNotAllocated) {
  106. return;
  107. }
  108. worker->state = state;
  109. }
  110. void eth_set_state() {
  111. }
  112. void eth_set_next_state(EthWorkerState state) {
  113. EthWorker* worker = static_worker;
  114. furi_assert(worker);
  115. if(state == EthWorkerStateReset || state == EthWorkerStateStop) {
  116. eth_set_force_state(state);
  117. return;
  118. }
  119. if(worker->state == EthWorkerStateNotInited) {
  120. if(state == EthWorkerStateInit) {
  121. worker->next_state = state;
  122. }
  123. }
  124. if(worker->state == EthWorkerStateInited) {
  125. if(state == EthWorkerStateDHCP || state == EthWorkerStateStaticIp) {
  126. worker->next_state = state;
  127. }
  128. }
  129. if(worker->state == EthWorkerStateOnline) {
  130. if(state == EthWorkerStatePing) {
  131. worker->next_state = state;
  132. }
  133. }
  134. }
  135. void eth_run(EthWorker* worker, EthWorkerProcess process) {
  136. furi_assert(worker);
  137. switch(process) {
  138. case EthWorkerProcessInit:
  139. if(worker->state == EthWorkerStateNotAllocated) {
  140. worker->thread = furi_thread_alloc();
  141. furi_thread_set_name(worker->thread, "EthWorker");
  142. furi_thread_set_stack_size(worker->thread, 8192);
  143. furi_thread_set_callback(worker->thread, eth_worker_task);
  144. furi_thread_set_context(worker->thread, worker);
  145. worker->state = EthWorkerStateNotInited;
  146. furi_thread_start(worker->thread);
  147. }
  148. eth_set_next_state(EthWorkerStateInit);
  149. break;
  150. case EthWorkerProcessDHCP:
  151. eth_set_next_state(EthWorkerStateDHCP);
  152. break;
  153. case EthWorkerProcessStatic:
  154. eth_set_next_state(EthWorkerStateStaticIp);
  155. break;
  156. case EthWorkerProcessPing:
  157. eth_set_next_state(EthWorkerStatePing);
  158. break;
  159. case EthWorkerProcessReset:
  160. eth_set_next_state(EthWorkerStateReset);
  161. eth_log(EthWorkerProcessInit, "reset module");
  162. eth_log(EthWorkerProcessDHCP, "reset module");
  163. eth_log(EthWorkerProcessStatic, "reset module");
  164. eth_log(EthWorkerProcessPing, "reset module");
  165. eth_log(EthWorkerProcessReset, "reset module");
  166. break;
  167. case EthWorkerProcessExit:
  168. if(worker->state != EthWorkerStateNotAllocated) {
  169. eth_set_force_state(EthWorkerStateStop);
  170. furi_thread_join(worker->thread);
  171. furi_thread_free(worker->thread);
  172. worker->state = EthWorkerStateNotAllocated;
  173. }
  174. break;
  175. default:
  176. break;
  177. }
  178. }
  179. /************************** Ethernet Worker Thread *****************************/
  180. static uint8_t ip_assigned = 0;
  181. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  182. static GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  183. static void W5500_Select(void) {
  184. furi_hal_gpio_write(&cspin, false);
  185. }
  186. static void W5500_Unselect(void) {
  187. furi_hal_gpio_write(&cspin, true);
  188. }
  189. static void Callback_IPAssigned(void) {
  190. eth_log(
  191. EthWorkerProcessDHCP, "Callback: IP assigned! Leased time: %d sec", getDHCPLeasetime());
  192. ip_assigned = 1;
  193. }
  194. static void Callback_IPConflict(void) {
  195. eth_log(EthWorkerProcessDHCP, "Callback: IP conflict!");
  196. }
  197. static void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  198. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  199. }
  200. static void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  201. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  202. }
  203. static uint8_t W5500_ReadByte(void) {
  204. uint8_t byte;
  205. W5500_ReadBuff(&byte, sizeof(byte));
  206. return byte;
  207. }
  208. static void W5500_WriteByte(uint8_t byte) {
  209. W5500_WriteBuff(&byte, sizeof(byte));
  210. }
  211. static void wait_ms(int ms) {
  212. furi_delay_ms(ms);
  213. }
  214. static wiz_NetInfo gWIZNETINFO;
  215. void update_WIZNETINFO(uint8_t is_dhcp) {
  216. furi_assert(static_worker);
  217. memcpy(gWIZNETINFO.mac, static_worker->config->mac, 6);
  218. if(is_dhcp) {
  219. memset(gWIZNETINFO.ip, 0, 4);
  220. memset(gWIZNETINFO.sn, 0, 4);
  221. memset(gWIZNETINFO.gw, 0, 4);
  222. memset(gWIZNETINFO.dns, 0, 4);
  223. gWIZNETINFO.dhcp = NETINFO_DHCP;
  224. } else {
  225. memcpy(gWIZNETINFO.ip, static_worker->config->ip, 4);
  226. memcpy(gWIZNETINFO.sn, static_worker->config->mask, 4);
  227. memcpy(gWIZNETINFO.gw, static_worker->config->gateway, 4);
  228. memcpy(gWIZNETINFO.dns, static_worker->config->dns, 4);
  229. gWIZNETINFO.dhcp = NETINFO_STATIC;
  230. }
  231. }
  232. int check_phylink(EthWorker* worker, EthWorkerState state, EthWorkerProcess proc, int timeout) {
  233. uint32_t start_time = furi_get_tick();
  234. uint32_t last_log_time = start_time;
  235. eth_log(proc, "phy link check 0");
  236. for(;;) {
  237. if(furi_get_tick() > start_time + timeout) {
  238. eth_log(proc, "phy link timeout");
  239. break;
  240. }
  241. if(worker->state != state) {
  242. eth_log(proc, "state changed");
  243. break;
  244. }
  245. uint8_t link = PHY_LINK_OFF;
  246. if(ctlwizchip(CW_GET_PHYLINK, (void*)&link) == -1) {
  247. eth_log(proc, "Unknown PHY link status");
  248. break;
  249. }
  250. if(link != PHY_LINK_OFF) {
  251. eth_log(proc, "phy link on");
  252. return 1;
  253. }
  254. furi_delay_ms(20);
  255. if(furi_get_tick() > last_log_time + 1000) {
  256. eth_log(proc, "phy link check %d", (last_log_time - start_time) / 1000);
  257. last_log_time = furi_get_tick();
  258. }
  259. }
  260. return 0;
  261. }
  262. #define DHCP_SOCKET 0
  263. uint8_t ping_auto(uint8_t s, uint8_t* addr);
  264. int32_t eth_worker_task(void* context) {
  265. furi_assert(context);
  266. EthWorker* worker = (EthWorker*)context;
  267. furi_hal_power_insomnia_enter();
  268. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  269. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  270. uint8_t dhcp_buffer[2000];
  271. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  272. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  273. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  274. furi_hal_gpio_write(&resetpin, true);
  275. furi_hal_gpio_write(&cspin, true);
  276. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  277. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  278. while(worker->state != EthWorkerStateStop) {
  279. if(worker->state == EthWorkerStateNotInited) {
  280. if(worker->next_state == EthWorkerStateInit) {
  281. worker->state = worker->next_state;
  282. }
  283. } else if(worker->state == EthWorkerStateInited) {
  284. if(worker->next_state == EthWorkerStateDHCP ||
  285. worker->next_state == EthWorkerStateStaticIp) {
  286. worker->state = worker->next_state;
  287. }
  288. } else if(worker->state == EthWorkerStateOnline) {
  289. if(worker->next_state == EthWorkerStatePing) {
  290. worker->state = worker->next_state;
  291. }
  292. } else if(worker->state == EthWorkerStateReset) {
  293. worker->state = EthWorkerStateNotInited;
  294. }
  295. if(worker->state == EthWorkerStateInit) {
  296. furi_hal_power_enable_otg();
  297. furi_delay_ms(300);
  298. furi_hal_gpio_write(&resetpin, false);
  299. furi_delay_ms(50);
  300. furi_hal_gpio_write(&resetpin, true);
  301. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  302. eth_log(EthWorkerProcessInit, "[error] W5500 init fail");
  303. eth_set_force_state(EthWorkerStateNotInited);
  304. continue;
  305. }
  306. eth_log(EthWorkerProcessInit, "W5500 inited");
  307. furi_delay_ms(50);
  308. update_WIZNETINFO(false);
  309. wizchip_setnetinfo(&gWIZNETINFO);
  310. wiz_NetInfo readed_net_info;
  311. wizchip_getnetinfo(&readed_net_info);
  312. if(memcmp(&readed_net_info, &gWIZNETINFO, sizeof(wiz_NetInfo))) {
  313. eth_log(EthWorkerProcessInit, "[error] module not detected");
  314. eth_set_force_state(EthWorkerStateNotInited);
  315. continue;
  316. }
  317. setSHAR(gWIZNETINFO.mac);
  318. wiz_PhyConf conf;
  319. wizphy_getphyconf(&conf);
  320. eth_log(
  321. EthWorkerProcessInit,
  322. "conf %d %d %d %d",
  323. conf.by,
  324. conf.mode,
  325. conf.speed,
  326. conf.duplex);
  327. eth_log(EthWorkerProcessInit, "net info setted");
  328. eth_log(
  329. EthWorkerProcessInit,
  330. "mac: %02X-%02X-%02X-%02X-%02X-%02X",
  331. gWIZNETINFO.mac[0],
  332. gWIZNETINFO.mac[1],
  333. gWIZNETINFO.mac[2],
  334. gWIZNETINFO.mac[3],
  335. gWIZNETINFO.mac[4],
  336. gWIZNETINFO.mac[5]);
  337. eth_set_force_state(EthWorkerStateInited);
  338. } else if(worker->state == EthWorkerStateDHCP) {
  339. if(!check_phylink(worker, EthWorkerStateDHCP, EthWorkerProcessDHCP, 5000)) {
  340. worker->state = EthWorkerStateInited;
  341. continue;
  342. }
  343. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  344. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  345. uint8_t next_cycle = 1;
  346. uint8_t divider = 0;
  347. while(next_cycle && worker->state == EthWorkerStateDHCP) {
  348. uint8_t dhcp_ret = DHCP_run();
  349. switch(dhcp_ret) {
  350. case DHCP_IP_ASSIGN:
  351. case DHCP_IP_CHANGED:
  352. case DHCP_IP_LEASED:
  353. getIPfromDHCP(gWIZNETINFO.ip);
  354. getGWfromDHCP(gWIZNETINFO.gw);
  355. getSNfromDHCP(gWIZNETINFO.sn);
  356. getDNSfromDHCP(gWIZNETINFO.dns);
  357. gWIZNETINFO.dhcp = NETINFO_DHCP;
  358. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  359. eth_log(
  360. EthWorkerProcessDHCP, "DHCP IP Leased Time : %ld Sec", getDHCPLeasetime());
  361. break;
  362. case DHCP_FAILED:
  363. eth_log(EthWorkerProcessDHCP, "DHCP Failed");
  364. break;
  365. }
  366. furi_delay_ms(100);
  367. if(divider++ % 10 == 0) {
  368. eth_log(EthWorkerProcessDHCP, "DHCP process %d", divider / 10);
  369. if(divider > 250) {
  370. DHCP_stop();
  371. eth_log(EthWorkerProcessDHCP, "DHCP Stop by timer");
  372. eth_set_force_state(EthWorkerStateInited);
  373. break;
  374. }
  375. }
  376. next_cycle = (dhcp_ret == DHCP_RUNNING);
  377. }
  378. if(worker->state != EthWorkerStateDHCP) {
  379. continue;
  380. }
  381. eth_log(
  382. EthWorkerProcessDHCP,
  383. "IP address:\n %d.%d.%d.%d",
  384. gWIZNETINFO.ip[0],
  385. gWIZNETINFO.ip[1],
  386. gWIZNETINFO.ip[2],
  387. gWIZNETINFO.ip[3]);
  388. eth_log(
  389. EthWorkerProcessDHCP,
  390. "SM Mask:\n %d.%d.%d.%d",
  391. gWIZNETINFO.sn[0],
  392. gWIZNETINFO.sn[1],
  393. gWIZNETINFO.sn[2],
  394. gWIZNETINFO.sn[3]);
  395. eth_log(
  396. EthWorkerProcessDHCP,
  397. "Gate way:\n %d.%d.%d.%d",
  398. gWIZNETINFO.gw[0],
  399. gWIZNETINFO.gw[1],
  400. gWIZNETINFO.gw[2],
  401. gWIZNETINFO.gw[3]);
  402. eth_log(
  403. EthWorkerProcessDHCP,
  404. "DNS Server:\n %d.%d.%d.%d",
  405. gWIZNETINFO.dns[0],
  406. gWIZNETINFO.dns[1],
  407. gWIZNETINFO.dns[2],
  408. gWIZNETINFO.dns[3]);
  409. eth_set_force_state(EthWorkerStateOnline);
  410. } else if(worker->state == EthWorkerStatePing) {
  411. uint8_t* adress = worker->config->ping_ip;
  412. eth_log(
  413. EthWorkerProcessPing,
  414. "ping %d.%d.%d.%d",
  415. adress[0],
  416. adress[1],
  417. adress[2],
  418. adress[3]);
  419. const uint8_t tryes = 4;
  420. uint8_t try = 0;
  421. while(try < tryes && worker->state == EthWorkerStatePing) {
  422. try++;
  423. uint32_t start_time = furi_get_tick();
  424. uint8_t res = ping_auto_interface(adress);
  425. uint32_t res_time = furi_get_tick();
  426. if(res == 3) {
  427. eth_log(EthWorkerProcessPing, "%d success %d ms", try, res_time - start_time);
  428. } else {
  429. eth_log(
  430. EthWorkerProcessPing, "%d error %d, %d", try, res, res_time - start_time);
  431. break;
  432. }
  433. }
  434. if(worker->state != EthWorkerStatePing) {
  435. break;
  436. }
  437. eth_set_force_state(EthWorkerStateOnline);
  438. }
  439. furi_delay_ms(5);
  440. }
  441. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  442. furi_hal_power_disable_otg();
  443. furi_hal_power_insomnia_exit();
  444. return 0;
  445. }