eth_worker.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #include "eth_worker_i.h"
  2. #include "eth_worker.h"
  3. #include "eth_save_process.h"
  4. #include <furi_hal.h>
  5. #include "socket.h"
  6. #include "dhcp.h"
  7. #include "ping.h"
  8. #include "stm32wbxx_hal_gpio.h"
  9. #include "wizchip_conf.h"
  10. #define TAG "EthWorker"
  11. static EthWorker* static_worker = NULL;
  12. EthWorker* eth_worker_alloc() {
  13. EthWorker* eth_worker = malloc(sizeof(EthWorker));
  14. eth_worker->thread = furi_thread_alloc();
  15. furi_thread_set_name(eth_worker->thread, "EthWorker");
  16. furi_thread_set_stack_size(eth_worker->thread, 8192);
  17. furi_thread_set_callback(eth_worker->thread, eth_worker_task);
  18. furi_thread_set_context(eth_worker->thread, eth_worker);
  19. eth_worker_change_state(eth_worker, EthWorkerStateModuleInit);
  20. eth_worker->config = ehternet_save_process_malloc();
  21. furi_assert(eth_worker->config);
  22. eth_worker->init_process =
  23. ethernet_view_process_malloc(EthWorkerProcessInit, eth_worker->config);
  24. eth_worker->dhcp_process =
  25. ethernet_view_process_malloc(EthWorkerProcessDHCP, eth_worker->config);
  26. eth_worker->stat_process =
  27. ethernet_view_process_malloc(EthWorkerProcessStatic, eth_worker->config);
  28. eth_worker->ping_process =
  29. ethernet_view_process_malloc(EthWorkerProcessPing, eth_worker->config);
  30. eth_worker->reset_process =
  31. ethernet_view_process_malloc(EthWorkerProcessReset, eth_worker->config);
  32. eth_worker->active_process = eth_worker->init_process;
  33. static_worker = eth_worker;
  34. eth_log(EthWorkerProcessReset, "Finik Ethernet [START]");
  35. return eth_worker;
  36. }
  37. void eth_worker_free(EthWorker* eth_worker) {
  38. eth_log(EthWorkerProcessReset, "Finik Ethernet [STOP]");
  39. static_worker = NULL;
  40. furi_assert(eth_worker);
  41. furi_thread_free(eth_worker->thread);
  42. ethernet_view_process_free(eth_worker->init_process);
  43. ethernet_view_process_free(eth_worker->dhcp_process);
  44. ethernet_view_process_free(eth_worker->stat_process);
  45. ethernet_view_process_free(eth_worker->ping_process);
  46. ethernet_view_process_free(eth_worker->reset_process);
  47. ehternet_save_process_free(eth_worker->config);
  48. free(eth_worker);
  49. }
  50. void eth_worker_change_state(EthWorker* eth_worker, EthWorkerState state) {
  51. furi_assert(eth_worker);
  52. eth_worker->state = state;
  53. }
  54. void eth_worker_set_active_process(EthWorker* eth_worker, EthWorkerProcess state) {
  55. furi_assert(eth_worker);
  56. switch(state) {
  57. case EthWorkerProcessInit:
  58. eth_worker->active_process = eth_worker->init_process;
  59. break;
  60. case EthWorkerProcessDHCP:
  61. eth_worker->active_process = eth_worker->dhcp_process;
  62. break;
  63. case EthWorkerProcessStatic:
  64. eth_worker->active_process = eth_worker->stat_process;
  65. break;
  66. case EthWorkerProcessPing:
  67. eth_worker->active_process = eth_worker->ping_process;
  68. break;
  69. case EthWorkerProcessReset:
  70. eth_worker->active_process = eth_worker->reset_process;
  71. break;
  72. }
  73. }
  74. void eth_worker_log(EthWorker* eth_worker, const char* str) {
  75. furi_assert(eth_worker);
  76. ehternet_save_process_print(eth_worker->config, str);
  77. }
  78. static EthViewProcess* get_process(EthWorker* worker, EthWorkerProcess process) {
  79. furi_assert(worker);
  80. switch(process) {
  81. case EthWorkerProcessInit:
  82. return worker->init_process;
  83. case EthWorkerProcessDHCP:
  84. return worker->dhcp_process;
  85. case EthWorkerProcessStatic:
  86. return worker->stat_process;
  87. case EthWorkerProcessPing:
  88. return worker->ping_process;
  89. case EthWorkerProcessReset:
  90. return worker->reset_process;
  91. case EthWorkerProcessActive:
  92. return worker->active_process;
  93. default:
  94. NULL;
  95. }
  96. }
  97. void eth_log(EthWorkerProcess process, const char* format, ...) {
  98. furi_assert(static_worker);
  99. va_list args;
  100. va_start(args, format);
  101. FuriString* fstring = furi_string_alloc_vprintf(format, args);
  102. const char* string = furi_string_get_cstr(fstring);
  103. va_end(args);
  104. FURI_LOG_I(TAG, "%s", string);
  105. ehternet_save_process_print(static_worker->config, string);
  106. ethernet_view_process_print(get_process(static_worker, process), string);
  107. furi_string_free(fstring);
  108. }
  109. /************************** Ethernet Worker Thread *****************************/
  110. int32_t eth_worker_task(void* context) {
  111. furi_assert(context);
  112. EthWorker* eth_worker = (EthWorker*)context;
  113. furi_hal_power_insomnia_enter();
  114. while(eth_worker->state != EthWorkerStateStop) {
  115. if(eth_worker->state == EthWorkerStateModuleInit) {
  116. eth_worker_w5500(eth_worker);
  117. eth_worker_dhcp(eth_worker);
  118. }
  119. if(eth_worker->state == EthWorkerStateDHCP) {
  120. eth_worker_dhcp(eth_worker);
  121. }
  122. }
  123. FURI_LOG_I(TAG, "eth_worker_task exit \r\n");
  124. eth_worker_change_state(eth_worker, EthWorkerStateStop);
  125. furi_hal_power_insomnia_exit();
  126. return 0;
  127. }
  128. #define DHCP_SOCKET 0
  129. #define PING_SOCKET 1
  130. // MAC-àäðåñ
  131. #define ETHADDR0 0x00 // The first octet of the Ethernet address
  132. #define ETHADDR1 0x08 // The second octet of the Ethernet address
  133. #define ETHADDR2 0xDC // The third octet of the Ethernet address
  134. #define ETHADDR3 0x47 // The fourth octet of the Ethernet address
  135. #define ETHADDR4 0x47 // The fifth octet of the Ethernet address
  136. #define ETHADDR5 0x54 // The sixth octet of the Ethernet address
  137. // Ïàðàìåòðû IP (åñëè DHCP îòêëþ÷åí)
  138. #define IPADDR0 192 // The first octet of the IP address of this uIP node
  139. #define IPADDR1 168 // The second octet of the IP address of this uIP node
  140. #define IPADDR2 1 // The third octet of the IP address of this uIP node
  141. #define IPADDR3 137 // The fourth octet of the IP address of this uIP node
  142. #define NETMASK0 255 // The first octet of the netmask of this uIP node
  143. #define NETMASK1 255 // The second octet of the netmask of this uIP node
  144. #define NETMASK2 255 // The third octet of the netmask of this uIP node
  145. #define NETMASK3 0 // The fourth octet of the netmask of this uIP node
  146. #define DRIPADDR0 192 // The first octet of the IP address of the default router
  147. #define DRIPADDR1 168 // The second octet of the IP address of the default router
  148. #define DRIPADDR2 1 // The third octet of the IP address of the default router
  149. #define DRIPADDR3 1 // The fourth octet of the IP address of the default router
  150. static uint8_t ip_assigned = 0;
  151. static GpioPin cspin = {.port = GPIOA, .pin = GPIO_PIN_4};
  152. void W5500_Select(void) {
  153. furi_hal_gpio_write(&cspin, false);
  154. }
  155. void W5500_Unselect(void) {
  156. furi_hal_gpio_write(&cspin, true);
  157. }
  158. void Callback_IPAssigned(void) {
  159. FURI_LOG_I(TAG, "Callback: IP assigned! Leased time: %d sec\r\n", getDHCPLeasetime());
  160. ip_assigned = 1;
  161. }
  162. void Callback_IPConflict(void) {
  163. FURI_LOG_I(TAG, "Callback: IP conflict!\r\n");
  164. }
  165. void W5500_ReadBuff(uint8_t* buff, uint16_t len) {
  166. furi_hal_spi_bus_rx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  167. //if(buff[0] != 0)
  168. // FURI_LOG_I(TAG, "spirx[%d]", buff[0]);
  169. }
  170. void W5500_WriteBuff(uint8_t* buff, uint16_t len) {
  171. furi_hal_spi_bus_tx(&furi_hal_spi_bus_handle_external, buff, len, 1000);
  172. }
  173. uint8_t W5500_ReadByte(void) {
  174. uint8_t byte;
  175. W5500_ReadBuff(&byte, sizeof(byte));
  176. return byte;
  177. }
  178. void W5500_WriteByte(uint8_t byte) {
  179. W5500_WriteBuff(&byte, sizeof(byte));
  180. }
  181. void wait_ms(int ms) {
  182. furi_delay_ms(ms);
  183. }
  184. wiz_NetInfo gWIZNETINFO = {
  185. .mac = {ETHADDR0, ETHADDR1, ETHADDR2, ETHADDR3, ETHADDR4, ETHADDR5},
  186. .ip = {IPADDR0, IPADDR1, IPADDR2, IPADDR3},
  187. .sn = {NETMASK0, NETMASK1, NETMASK2, NETMASK3},
  188. .gw = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  189. .dns = {DRIPADDR0, DRIPADDR1, DRIPADDR2, DRIPADDR3},
  190. .dhcp = NETINFO_DHCP // NETINFO_STATIC
  191. };
  192. void eth_worker_dhcp(EthWorker* eth_worker) {
  193. furi_assert(eth_worker);
  194. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  195. uint8_t temp;
  196. uint8_t W5500FifoSize[2][8] = {
  197. {
  198. 2,
  199. 2,
  200. 2,
  201. 2,
  202. 2,
  203. 2,
  204. 2,
  205. 2,
  206. },
  207. {2, 2, 2, 2, 2, 2, 2, 2}};
  208. uint8_t dhcp_buffer[2000];
  209. FURI_LOG_I(TAG, "registering W5500 callbacks\r\n");
  210. FURI_LOG_I(TAG, "sizeof %d", sizeof(gWIZNETINFO));
  211. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  212. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  213. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  214. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  215. furi_hal_gpio_write(&resetpin, true);
  216. furi_hal_gpio_write(&cspin, true);
  217. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  218. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  219. furi_hal_power_enable_otg();
  220. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  221. furi_delay_ms(1000);
  222. furi_hal_gpio_write(&resetpin, false);
  223. furi_delay_ms(10);
  224. furi_hal_gpio_write(&resetpin, true);
  225. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  226. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  227. FURI_LOG_I(TAG, "W5500 initialized fail");
  228. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  229. while(1)
  230. ;
  231. //break;
  232. }
  233. FURI_LOG_I(TAG, "W5500 initialized success");
  234. furi_delay_ms(200);
  235. wizchip_setnetinfo(&gWIZNETINFO);
  236. FURI_LOG_I(TAG, "W5500 info setted 1");
  237. setSHAR(gWIZNETINFO.mac);
  238. FURI_LOG_I(TAG, "W5500 info setted 2");
  239. //check phy status
  240. do {
  241. if(ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1) {
  242. FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  243. }
  244. furi_delay_ms(1);
  245. } while(temp == PHY_LINK_OFF);
  246. FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  247. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  248. furi_delay_ms(1000);
  249. FURI_LOG_I(TAG, "Registering DHCP callbacks.\r\n");
  250. reg_dhcp_cbfunc(Callback_IPAssigned, Callback_IPAssigned, Callback_IPConflict);
  251. ////eth_worker->callback(EthCustomEventDHCPConnect, eth_worker->context);
  252. if(gWIZNETINFO.dhcp == NETINFO_DHCP) {
  253. DHCP_init(DHCP_SOCKET, dhcp_buffer);
  254. uint8_t dhcp_ret = DHCP_STOPPED;
  255. while(
  256. !((dhcp_ret == DHCP_IP_ASSIGN) || (dhcp_ret == DHCP_IP_CHANGED) ||
  257. (dhcp_ret == DHCP_FAILED) || (dhcp_ret == DHCP_IP_LEASED))) {
  258. dhcp_ret = DHCP_run();
  259. switch(dhcp_ret) {
  260. case DHCP_IP_ASSIGN:
  261. case DHCP_IP_CHANGED:
  262. case DHCP_IP_LEASED:
  263. getIPfromDHCP(gWIZNETINFO.ip);
  264. getGWfromDHCP(gWIZNETINFO.gw);
  265. getSNfromDHCP(gWIZNETINFO.sn);
  266. getDNSfromDHCP(gWIZNETINFO.dns);
  267. gWIZNETINFO.dhcp = NETINFO_DHCP;
  268. ctlnetwork(CN_SET_NETINFO, (void*)&gWIZNETINFO);
  269. FURI_LOG_I(TAG, "\r\n>> DHCP IP Leased Time : %ld Sec\r\n", getDHCPLeasetime());
  270. break;
  271. case DHCP_FAILED:
  272. FURI_LOG_I(TAG, ">> DHCP Failed\r\n");
  273. gWIZNETINFO.dhcp = NETINFO_STATIC;
  274. break;
  275. }
  276. furi_delay_ms(1);
  277. }
  278. wizchip_getnetinfo(&gWIZNETINFO);
  279. FURI_LOG_I(
  280. TAG,
  281. "Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",
  282. gWIZNETINFO.mac[0],
  283. gWIZNETINFO.mac[1],
  284. gWIZNETINFO.mac[2],
  285. gWIZNETINFO.mac[3],
  286. gWIZNETINFO.mac[4],
  287. gWIZNETINFO.mac[5]);
  288. if(gWIZNETINFO.dhcp == NETINFO_DHCP)
  289. FURI_LOG_I(TAG, "DHCP\n\r");
  290. else
  291. FURI_LOG_I(TAG, "Static IP\n\r");
  292. FURI_LOG_I(
  293. TAG,
  294. "IP address : %d.%d.%d.%d\n\r",
  295. gWIZNETINFO.ip[0],
  296. gWIZNETINFO.ip[1],
  297. gWIZNETINFO.ip[2],
  298. gWIZNETINFO.ip[3]);
  299. FURI_LOG_I(
  300. TAG,
  301. "SM Mask : %d.%d.%d.%d\n\r",
  302. gWIZNETINFO.sn[0],
  303. gWIZNETINFO.sn[1],
  304. gWIZNETINFO.sn[2],
  305. gWIZNETINFO.sn[3]);
  306. FURI_LOG_I(
  307. TAG,
  308. "Gate way : %d.%d.%d.%d\n\r",
  309. gWIZNETINFO.gw[0],
  310. gWIZNETINFO.gw[1],
  311. gWIZNETINFO.gw[2],
  312. gWIZNETINFO.gw[3]);
  313. FURI_LOG_I(
  314. TAG,
  315. "DNS Server : %d.%d.%d.%d\n\r",
  316. gWIZNETINFO.dns[0],
  317. gWIZNETINFO.dns[1],
  318. gWIZNETINFO.dns[2],
  319. gWIZNETINFO.dns[3]);
  320. ////eth_worker->callback(EthCustomEventDHCPConnectSuccess, eth_worker->context);
  321. furi_delay_ms(20000);
  322. uint8_t pDestaddr[4] = {8, 8, 8, 8};
  323. uint8_t tmp = ping_auto(1, pDestaddr);
  324. //tmp = ping_count(0,3,pDestaddr);
  325. if(tmp == SUCCESS) {
  326. ////eth_worker->callback(EthCustomEventPingConnect, eth_worker->context);
  327. FURI_LOG_I(TAG, "-----------PING TEST OK----------\r\n");
  328. } else {
  329. ////eth_worker->callback(EthCustomEventPingError, eth_worker->context);
  330. FURI_LOG_I(TAG, "----------ERROR = %d----------\r\n", tmp);
  331. }
  332. furi_delay_ms(3000);
  333. furi_delay_ms(2000);
  334. ////eth_worker->callback(EthCustomEventWellDone, eth_worker->context);
  335. }
  336. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  337. }
  338. static void w5500_init() {
  339. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  340. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  341. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  342. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  343. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  344. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  345. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  346. furi_hal_gpio_write(&resetpin, true);
  347. furi_hal_gpio_write(&cspin, true);
  348. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  349. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  350. }
  351. static void w5500_deinit() {
  352. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  353. }
  354. void eth_worker_w5500(EthWorker* eth_worker) {
  355. furi_assert(eth_worker);
  356. //uint8_t temp;
  357. FURI_LOG_I(TAG, "Ehtping_Init");
  358. furi_hal_spi_acquire(&furi_hal_spi_bus_handle_external);
  359. uint8_t W5500FifoSize[2][8] = {{2, 2, 2, 2, 2, 2, 2, 2}, {2, 2, 2, 2, 2, 2, 2, 2}};
  360. FURI_LOG_I(TAG, "Registering W5500 callbacks");
  361. reg_wizchip_spi_cbfunc(W5500_ReadByte, W5500_WriteByte);
  362. reg_wizchip_spiburst_cbfunc(W5500_ReadBuff, W5500_WriteBuff);
  363. reg_wizchip_cs_cbfunc(W5500_Select, W5500_Unselect);
  364. FURI_LOG_I(TAG, "Registered W5500 callbacks");
  365. GpioPin resetpin = {.port = GPIOC, .pin = GPIO_PIN_3};
  366. furi_hal_gpio_write(&resetpin, true);
  367. furi_hal_gpio_write(&cspin, true);
  368. furi_hal_gpio_init(&resetpin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  369. furi_hal_gpio_init(&cspin, GpioModeOutputOpenDrain, GpioPullNo, GpioSpeedVeryHigh);
  370. FURI_LOG_I(TAG, "GPIO inited");
  371. furi_hal_power_enable_otg();
  372. furi_delay_ms(1000);
  373. //eth_worker->callback(EthCustomEventModulePowerOn, eth_worker->context);
  374. furi_delay_ms(2000);
  375. furi_hal_gpio_write(&resetpin, false);
  376. furi_delay_ms(10);
  377. furi_hal_gpio_write(&resetpin, true);
  378. FURI_LOG_I(TAG, "GPIO used");
  379. //eth_worker->callback(EthCustomEventModuleConnect, eth_worker->context);
  380. if(ctlwizchip(CW_INIT_WIZCHIP, (void*)W5500FifoSize) == -1) {
  381. FURI_LOG_I(TAG, "W5500 initialized fail.\r\n");
  382. //eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  383. }
  384. FURI_LOG_I(TAG, "W5500 initialized success.\r\n");
  385. furi_delay_ms(2000);
  386. wizchip_setnetinfo(&gWIZNETINFO);
  387. FURI_LOG_I(TAG, "W5500 info setted 1.\r\n");
  388. setSHAR(gWIZNETINFO.mac);
  389. FURI_LOG_I(TAG, "W5500 info setted 2.\r\n");
  390. //check phy status
  391. //do
  392. //{
  393. // if (ctlwizchip(CW_GET_PHYLINK, (void*)&temp) == -1)
  394. // {
  395. // FURI_LOG_I(TAG, "Unknown PHY link status.\r\n");
  396. // }
  397. // furi_delay_ms(1);
  398. //} while (temp == PHY_LINK_OFF);
  399. //FURI_LOG_I(TAG, "W5500 gWIZNETINFO success.\r\n");
  400. ////eth_worker->callback(EthCustomEventPHYConnect, eth_worker->context);
  401. FURI_LOG_I(TAG, "W5500 before delay\r\n");
  402. furi_delay_ms(2000);
  403. FURI_LOG_I(TAG, "W5500 after delay\r\n");
  404. //furi_hal_power_disable_otg();
  405. //FURI_LOG_I(TAG, "W5500 power off\r\n");
  406. ////eth_worker->callback(EthCustomEventModuleError, eth_worker->context);
  407. furi_delay_ms(2000);
  408. ////eth_worker->callback(EthCustomEventModuleConnected, eth_worker->context);
  409. furi_hal_spi_release(&furi_hal_spi_bus_handle_external);
  410. }