usbh_lwip.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "netif/etharp.h"
  7. #include "lwip/netif.h"
  8. #include "lwip/pbuf.h"
  9. #include "lwip/tcpip.h"
  10. #if LWIP_DHCP
  11. #include "lwip/dhcp.h"
  12. #include "lwip/prot/dhcp.h"
  13. #endif
  14. #include "usbh_core.h"
  15. #if LWIP_TCPIP_CORE_LOCKING_INPUT != 1
  16. #warning suggest you to set LWIP_TCPIP_CORE_LOCKING_INPUT to 1, usb handles eth input with own thread
  17. #endif
  18. #if LWIP_TCPIP_CORE_LOCKING != 1
  19. #error must set LWIP_TCPIP_CORE_LOCKING to 1
  20. #endif
  21. #if PBUF_POOL_BUFSIZE < 1600
  22. #error PBUF_POOL_BUFSIZE must be larger than 1600
  23. #endif
  24. #if TCPIP_THREAD_STACKSIZE < 1024
  25. #error TCPIP_THREAD_STACKSIZE must be >= 1024
  26. #endif
  27. // #define CONFIG_USBHOST_PLATFORM_CDC_ECM
  28. // #define CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  29. // #define CONFIG_USBHOST_PLATFORM_CDC_NCM
  30. // #define CONFIG_USBHOST_PLATFORM_ASIX
  31. // #define CONFIG_USBHOST_PLATFORM_RTL8152
  32. // #define CONFIG_USBHOST_PLATFORM_BL616
  33. ip_addr_t g_ipaddr;
  34. ip_addr_t g_netmask;
  35. ip_addr_t g_gateway;
  36. void usbh_lwip_eth_output_common(struct pbuf *p, uint8_t *buf)
  37. {
  38. struct pbuf *q;
  39. uint8_t *buffer;
  40. buffer = buf;
  41. for (q = p; q != NULL; q = q->next) {
  42. usb_memcpy(buffer, q->payload, q->len);
  43. buffer += q->len;
  44. }
  45. }
  46. void usbh_lwip_eth_input_common(struct netif *netif, uint8_t *buf, uint32_t len)
  47. {
  48. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  49. pbuf_type type = PBUF_REF;
  50. #else
  51. pbuf_type type = PBUF_POOL;
  52. #endif
  53. err_t err;
  54. struct pbuf *p;
  55. p = pbuf_alloc(PBUF_RAW, len, type);
  56. if (p != NULL) {
  57. #if LWIP_TCPIP_CORE_LOCKING_INPUT
  58. p->payload = buf;
  59. #else
  60. usb_memcpy(p->payload, buf, len);
  61. #endif
  62. err = netif->input(p, netif);
  63. if (err != ERR_OK) {
  64. pbuf_free(p);
  65. }
  66. } else {
  67. USB_LOG_ERR("No memory to alloc pbuf\r\n");
  68. }
  69. }
  70. struct usb_osal_timer *dhcp_handle;
  71. static void dhcp_timeout(void *arg)
  72. {
  73. struct netif *netif = (struct netif *)arg;
  74. #if LWIP_DHCP
  75. struct dhcp *dhcp;
  76. #endif
  77. if (netif_is_up(netif)) {
  78. #if LWIP_DHCP
  79. dhcp = netif_dhcp_data(netif);
  80. if (dhcp && (dhcp->state == DHCP_STATE_BOUND)) {
  81. #endif
  82. USB_LOG_INFO("IPv4 Address : %s\r\n", ipaddr_ntoa(&netif->ip_addr));
  83. USB_LOG_INFO("IPv4 Subnet mask : %s\r\n", ipaddr_ntoa(&netif->netmask));
  84. USB_LOG_INFO("IPv4 Gateway : %s\r\n\r\n", ipaddr_ntoa(&netif->gw));
  85. usb_osal_timer_stop(dhcp_handle);
  86. #if LWIP_DHCP
  87. }
  88. #endif
  89. } else {
  90. }
  91. }
  92. #ifdef CONFIG_USBHOST_PLATFORM_CDC_ECM
  93. #include "usbh_cdc_ecm.h"
  94. struct netif g_cdc_ecm_netif;
  95. static err_t usbh_cdc_ecm_linkoutput(struct netif *netif, struct pbuf *p)
  96. {
  97. int ret;
  98. (void)netif;
  99. usbh_lwip_eth_output_common(p, usbh_cdc_ecm_get_eth_txbuf());
  100. ret = usbh_cdc_ecm_eth_output(p->tot_len);
  101. if (ret < 0) {
  102. return ERR_BUF;
  103. } else {
  104. return ERR_OK;
  105. }
  106. }
  107. void usbh_cdc_ecm_eth_input(uint8_t *buf, uint32_t buflen)
  108. {
  109. usbh_lwip_eth_input_common(&g_cdc_ecm_netif, buf, buflen);
  110. }
  111. static err_t usbh_cdc_ecm_if_init(struct netif *netif)
  112. {
  113. LWIP_ASSERT("netif != NULL", (netif != NULL));
  114. netif->mtu = 1500;
  115. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  116. netif->state = NULL;
  117. netif->name[0] = 'E';
  118. netif->name[1] = 'X';
  119. netif->output = etharp_output;
  120. netif->linkoutput = usbh_cdc_ecm_linkoutput;
  121. return ERR_OK;
  122. }
  123. void usbh_cdc_ecm_run(struct usbh_cdc_ecm *cdc_ecm_class)
  124. {
  125. struct netif *netif = &g_cdc_ecm_netif;
  126. netif->hwaddr_len = 6;
  127. memcpy(netif->hwaddr, cdc_ecm_class->mac, 6);
  128. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  129. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  130. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  131. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_cdc_ecm_if_init, tcpip_input);
  132. netif_set_default(netif);
  133. while (!netif_is_up(netif)) {
  134. }
  135. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  136. if (dhcp_handle == NULL) {
  137. USB_LOG_ERR("timer creation failed! \r\n");
  138. while (1) {
  139. }
  140. }
  141. usb_osal_thread_create("usbh_cdc_ecm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ecm_rx_thread, NULL);
  142. #if LWIP_DHCP
  143. dhcp_start(netif);
  144. usb_osal_timer_start(dhcp_handle);
  145. #endif
  146. }
  147. void usbh_cdc_ecm_stop(struct usbh_cdc_ecm *cdc_ecm_class)
  148. {
  149. struct netif *netif = &g_cdc_ecm_netif;
  150. (void)cdc_ecm_class;
  151. #if LWIP_DHCP
  152. dhcp_stop(netif);
  153. dhcp_cleanup(netif);
  154. usb_osal_timer_delete(dhcp_handle);
  155. #endif
  156. netif_set_down(netif);
  157. netif_remove(netif);
  158. }
  159. #endif
  160. #ifdef CONFIG_USBHOST_PLATFORM_CDC_RNDIS
  161. #include "usbh_rndis.h"
  162. struct usb_osal_timer *timer_handle;
  163. static void rndis_dev_keepalive_timeout(void *arg)
  164. {
  165. struct usbh_rndis *rndis_class = (struct usbh_rndis *)arg;
  166. usbh_rndis_keepalive(rndis_class);
  167. }
  168. void timer_init(struct usbh_rndis *rndis_class)
  169. {
  170. timer_handle = usb_osal_timer_create("rndis_keepalive", 5000, rndis_dev_keepalive_timeout, rndis_class, true);
  171. if (NULL != timer_handle) {
  172. usb_osal_timer_start(timer_handle);
  173. } else {
  174. USB_LOG_ERR("timer creation failed! \r\n");
  175. for (;;) {
  176. ;
  177. }
  178. }
  179. }
  180. struct netif g_rndis_netif;
  181. static err_t usbh_rndis_linkoutput(struct netif *netif, struct pbuf *p)
  182. {
  183. int ret;
  184. (void)netif;
  185. usbh_lwip_eth_output_common(p, usbh_rndis_get_eth_txbuf());
  186. ret = usbh_rndis_eth_output(p->tot_len);
  187. if (ret < 0) {
  188. return ERR_BUF;
  189. } else {
  190. return ERR_OK;
  191. }
  192. }
  193. void usbh_rndis_eth_input(uint8_t *buf, uint32_t buflen)
  194. {
  195. usbh_lwip_eth_input_common(&g_rndis_netif, buf, buflen);
  196. }
  197. static err_t usbh_rndis_if_init(struct netif *netif)
  198. {
  199. LWIP_ASSERT("netif != NULL", (netif != NULL));
  200. netif->mtu = 1500;
  201. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  202. netif->state = NULL;
  203. netif->name[0] = 'E';
  204. netif->name[1] = 'X';
  205. netif->output = etharp_output;
  206. netif->linkoutput = usbh_rndis_linkoutput;
  207. return ERR_OK;
  208. }
  209. void usbh_rndis_run(struct usbh_rndis *rndis_class)
  210. {
  211. struct netif *netif = &g_rndis_netif;
  212. netif->hwaddr_len = 6;
  213. memcpy(netif->hwaddr, rndis_class->mac, 6);
  214. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  215. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  216. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  217. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_rndis_if_init, tcpip_input);
  218. netif_set_default(netif);
  219. while (!netif_is_up(netif)) {
  220. }
  221. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  222. if (dhcp_handle == NULL) {
  223. USB_LOG_ERR("timer creation failed! \r\n");
  224. while (1) {
  225. }
  226. }
  227. usb_osal_thread_create("usbh_rndis_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rndis_rx_thread, NULL);
  228. //timer_init(rndis_class);
  229. #if LWIP_DHCP
  230. dhcp_start(netif);
  231. usb_osal_timer_start(dhcp_handle);
  232. #endif
  233. }
  234. void usbh_rndis_stop(struct usbh_rndis *rndis_class)
  235. {
  236. struct netif *netif = &g_rndis_netif;
  237. (void)rndis_class;
  238. #if LWIP_DHCP
  239. dhcp_stop(netif);
  240. dhcp_cleanup(netif);
  241. usb_osal_timer_delete(dhcp_handle);
  242. #endif
  243. netif_set_down(netif);
  244. netif_remove(netif);
  245. // xTimerStop(timer_handle, 0);
  246. // xTimerDelete(timer_handle, 0);
  247. }
  248. #endif
  249. #ifdef CONFIG_USBHOST_PLATFORM_CDC_NCM
  250. #include "usbh_cdc_ncm.h"
  251. struct netif g_cdc_ncm_netif;
  252. static err_t usbh_cdc_ncm_linkoutput(struct netif *netif, struct pbuf *p)
  253. {
  254. int ret;
  255. (void)netif;
  256. usbh_lwip_eth_output_common(p, usbh_cdc_ncm_get_eth_txbuf());
  257. ret = usbh_cdc_ncm_eth_output(p->tot_len);
  258. if (ret < 0) {
  259. return ERR_BUF;
  260. } else {
  261. return ERR_OK;
  262. }
  263. }
  264. void usbh_cdc_ncm_eth_input(uint8_t *buf, uint32_t buflen)
  265. {
  266. usbh_lwip_eth_input_common(&g_cdc_ncm_netif, buf, buflen);
  267. }
  268. static err_t usbh_cdc_ncm_if_init(struct netif *netif)
  269. {
  270. LWIP_ASSERT("netif != NULL", (netif != NULL));
  271. netif->mtu = 1500;
  272. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  273. netif->state = NULL;
  274. netif->name[0] = 'E';
  275. netif->name[1] = 'X';
  276. netif->output = etharp_output;
  277. netif->linkoutput = usbh_cdc_ncm_linkoutput;
  278. return ERR_OK;
  279. }
  280. void usbh_cdc_ncm_run(struct usbh_cdc_ncm *cdc_ncm_class)
  281. {
  282. struct netif *netif = &g_cdc_ncm_netif;
  283. netif->hwaddr_len = 6;
  284. memcpy(netif->hwaddr, cdc_ncm_class->mac, 6);
  285. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  286. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  287. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  288. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_cdc_ncm_if_init, tcpip_input);
  289. netif_set_default(netif);
  290. while (!netif_is_up(netif)) {
  291. }
  292. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  293. if (dhcp_handle == NULL) {
  294. USB_LOG_ERR("timer creation failed! \r\n");
  295. while (1) {
  296. }
  297. }
  298. usb_osal_thread_create("usbh_cdc_ncm_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_ncm_rx_thread, NULL);
  299. #if LWIP_DHCP
  300. dhcp_start(netif);
  301. usb_osal_timer_start(dhcp_handle);
  302. #endif
  303. }
  304. void usbh_cdc_ncm_stop(struct usbh_cdc_ncm *cdc_ncm_class)
  305. {
  306. struct netif *netif = &g_cdc_ncm_netif;
  307. (void)cdc_ncm_class;
  308. #if LWIP_DHCP
  309. dhcp_stop(netif);
  310. dhcp_cleanup(netif);
  311. usb_osal_timer_delete(dhcp_handle);
  312. #endif
  313. netif_set_down(netif);
  314. netif_remove(netif);
  315. }
  316. #endif
  317. #ifdef CONFIG_USBHOST_PLATFORM_ASIX
  318. #include "usbh_asix.h"
  319. struct netif g_asix_netif;
  320. static err_t usbh_asix_linkoutput(struct netif *netif, struct pbuf *p)
  321. {
  322. int ret;
  323. (void)netif;
  324. usbh_lwip_eth_output_common(p, usbh_asix_get_eth_txbuf());
  325. ret = usbh_asix_eth_output(p->tot_len);
  326. if (ret < 0) {
  327. return ERR_BUF;
  328. } else {
  329. return ERR_OK;
  330. }
  331. }
  332. void usbh_asix_eth_input(uint8_t *buf, uint32_t buflen)
  333. {
  334. usbh_lwip_eth_input_common(&g_asix_netif, buf, buflen);
  335. }
  336. static err_t usbh_asix_if_init(struct netif *netif)
  337. {
  338. LWIP_ASSERT("netif != NULL", (netif != NULL));
  339. netif->mtu = 1500;
  340. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  341. netif->state = NULL;
  342. netif->name[0] = 'E';
  343. netif->name[1] = 'X';
  344. netif->output = etharp_output;
  345. netif->linkoutput = usbh_asix_linkoutput;
  346. return ERR_OK;
  347. }
  348. void usbh_asix_run(struct usbh_asix *asix_class)
  349. {
  350. struct netif *netif = &g_asix_netif;
  351. netif->hwaddr_len = 6;
  352. memcpy(netif->hwaddr, asix_class->mac, 6);
  353. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  354. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  355. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  356. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_asix_if_init, tcpip_input);
  357. netif_set_default(netif);
  358. while (!netif_is_up(netif)) {
  359. }
  360. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  361. if (dhcp_handle == NULL) {
  362. USB_LOG_ERR("timer creation failed! \r\n");
  363. while (1) {
  364. }
  365. }
  366. usb_osal_thread_create("usbh_asix_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_asix_rx_thread, NULL);
  367. #if LWIP_DHCP
  368. dhcp_start(netif);
  369. usb_osal_timer_start(dhcp_handle);
  370. #endif
  371. }
  372. void usbh_asix_stop(struct usbh_asix *asix_class)
  373. {
  374. struct netif *netif = &g_asix_netif;
  375. (void)asix_class;
  376. #if LWIP_DHCP
  377. dhcp_stop(netif);
  378. dhcp_cleanup(netif);
  379. usb_osal_timer_delete(dhcp_handle);
  380. #endif
  381. netif_set_down(netif);
  382. netif_remove(netif);
  383. }
  384. #endif
  385. #ifdef CONFIG_USBHOST_PLATFORM_RTL8152
  386. #include "usbh_rtl8152.h"
  387. struct netif g_rtl8152_netif;
  388. static err_t usbh_rtl8152_linkoutput(struct netif *netif, struct pbuf *p)
  389. {
  390. int ret;
  391. (void)netif;
  392. usbh_lwip_eth_output_common(p, usbh_rtl8152_get_eth_txbuf());
  393. ret = usbh_rtl8152_eth_output(p->tot_len);
  394. if (ret < 0) {
  395. return ERR_BUF;
  396. } else {
  397. return ERR_OK;
  398. }
  399. }
  400. void usbh_rtl8152_eth_input(uint8_t *buf, uint32_t buflen)
  401. {
  402. usbh_lwip_eth_input_common(&g_rtl8152_netif, buf, buflen);
  403. }
  404. static err_t usbh_rtl8152_if_init(struct netif *netif)
  405. {
  406. LWIP_ASSERT("netif != NULL", (netif != NULL));
  407. netif->mtu = 1500;
  408. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_UP;
  409. netif->state = NULL;
  410. netif->name[0] = 'E';
  411. netif->name[1] = 'X';
  412. netif->output = etharp_output;
  413. netif->linkoutput = usbh_rtl8152_linkoutput;
  414. return ERR_OK;
  415. }
  416. void usbh_rtl8152_run(struct usbh_rtl8152 *rtl8152_class)
  417. {
  418. struct netif *netif = &g_rtl8152_netif;
  419. netif->hwaddr_len = 6;
  420. memcpy(netif->hwaddr, rtl8152_class->mac, 6);
  421. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  422. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  423. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  424. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_rtl8152_if_init, tcpip_input);
  425. netif_set_default(netif);
  426. while (!netif_is_up(netif)) {
  427. }
  428. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  429. if (dhcp_handle == NULL) {
  430. USB_LOG_ERR("timer creation failed! \r\n");
  431. while (1) {
  432. }
  433. }
  434. usb_osal_thread_create("usbh_rtl8152_rx", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_rtl8152_rx_thread, NULL);
  435. #if LWIP_DHCP
  436. dhcp_start(netif);
  437. usb_osal_timer_start(dhcp_handle);
  438. #endif
  439. }
  440. void usbh_rtl8152_stop(struct usbh_rtl8152 *rtl8152_class)
  441. {
  442. struct netif *netif = &g_rtl8152_netif;
  443. (void)rtl8152_class;
  444. #if LWIP_DHCP
  445. dhcp_stop(netif);
  446. dhcp_cleanup(netif);
  447. usb_osal_timer_delete(dhcp_handle);
  448. #endif
  449. netif_set_down(netif);
  450. netif_remove(netif);
  451. }
  452. #endif
  453. #ifdef CONFIG_USBHOST_PLATFORM_BL616
  454. #include "usbh_bl616.h"
  455. struct netif g_bl616_netif;
  456. static err_t usbh_bl616_linkoutput(struct netif *netif, struct pbuf *p)
  457. {
  458. int ret;
  459. (void)netif;
  460. usbh_lwip_eth_output_common(p, usbh_bl616_get_eth_txbuf());
  461. ret = usbh_bl616_eth_output(p->tot_len);
  462. if (ret < 0) {
  463. return ERR_BUF;
  464. } else {
  465. return ERR_OK;
  466. }
  467. }
  468. void usbh_bl616_eth_input(uint8_t *buf, uint32_t buflen)
  469. {
  470. usbh_lwip_eth_input_common(&g_bl616_netif, buf, buflen);
  471. }
  472. static err_t usbh_bl616_if_init(struct netif *netif)
  473. {
  474. LWIP_ASSERT("netif != NULL", (netif != NULL));
  475. netif->mtu = 1500;
  476. netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
  477. netif->state = NULL;
  478. netif->name[0] = 'E';
  479. netif->name[1] = 'X';
  480. netif->output = etharp_output;
  481. netif->linkoutput = usbh_bl616_linkoutput;
  482. return ERR_OK;
  483. }
  484. void usbh_bl616_sta_connect_callback(void)
  485. {
  486. }
  487. void usbh_bl616_sta_disconnect_callback(void)
  488. {
  489. struct netif *netif = &g_bl616_netif;
  490. netif_set_down(netif);
  491. }
  492. void usbh_bl616_sta_update_ip(uint8_t ip4_addr[4], uint8_t ip4_mask[4], uint8_t ip4_gw[4])
  493. {
  494. struct netif *netif = &g_bl616_netif;
  495. IP4_ADDR(&netif->ip_addr, ip4_addr[0], ip4_addr[1], ip4_addr[2], ip4_addr[3]);
  496. IP4_ADDR(&netif->netmask, ip4_mask[0], ip4_mask[1], ip4_mask[2], ip4_mask[3]);
  497. IP4_ADDR(&netif->gw, ip4_gw[0], ip4_gw[1], ip4_gw[2], ip4_gw[3]);
  498. netif_set_up(netif);
  499. }
  500. void usbh_bl616_run(struct usbh_bl616 *bl616_class)
  501. {
  502. struct netif *netif = &g_bl616_netif;
  503. netif->hwaddr_len = 6;
  504. memcpy(netif->hwaddr, bl616_class->sta_mac, 6);
  505. IP4_ADDR(&g_ipaddr, 0, 0, 0, 0);
  506. IP4_ADDR(&g_netmask, 0, 0, 0, 0);
  507. IP4_ADDR(&g_gateway, 0, 0, 0, 0);
  508. netif = netif_add(netif, &g_ipaddr, &g_netmask, &g_gateway, NULL, usbh_bl616_if_init, tcpip_input);
  509. netif_set_down(netif);
  510. netif_set_default(netif);
  511. dhcp_handle = usb_osal_timer_create("dhcp", 200, dhcp_timeout, netif, true);
  512. if (dhcp_handle == NULL) {
  513. USB_LOG_ERR("timer creation failed! \r\n");
  514. while (1) {
  515. }
  516. }
  517. usb_osal_timer_start(dhcp_handle);
  518. usb_osal_thread_create("usbh_bl616", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_bl616_rx_thread, NULL);
  519. }
  520. void usbh_bl616_stop(struct usbh_bl616 *bl616_class)
  521. {
  522. struct netif *netif = &g_bl616_netif;
  523. (void)bl616_class;
  524. netif_set_down(netif);
  525. netif_remove(netif);
  526. }
  527. // #include "shell.h"
  528. // CSH_CMD_EXPORT(wifi_sta_connect, );
  529. // CSH_CMD_EXPORT(wifi_scan, );
  530. #endif