eth_worker.c 15 KB

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