eth_worker.c 15 KB

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