net_lwip2.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. #include "platform_def.h"
  2. #include "luat_base.h"
  3. #include "luat_timer.h"
  4. #include "luat_mcu.h"
  5. #include "luat_rtos.h"
  6. #include "dns_def.h"
  7. #include "luat_network_adapter.h"
  8. #include "lwip/tcpip.h"
  9. #include "lwip/udp.h"
  10. #include "lwip/sockets.h"
  11. #include "net_lwip2.h"
  12. #include "luat_crypto.h"
  13. #define LUAT_LOG_TAG "net"
  14. #include "luat_log.h"
  15. void* luat_heap_zalloc(size_t len);
  16. #define MAX_SOCK_NUM 8
  17. #define NET_DBG LLOGD
  18. #define NET_ERR LLOGE
  19. #ifndef SOCKET_BUF_LEN
  20. #define SOCKET_BUF_LEN (3 * TCP_MSS)
  21. #endif
  22. static int network_state = 0;
  23. static int net_lwip2_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data);
  24. enum
  25. {
  26. EV_LWIP_EVENT_START = USER_EVENT_ID_START + 0x2000000,
  27. EV_LWIP_SOCKET_TX,
  28. EV_LWIP_NETIF_INPUT,
  29. EV_LWIP_TCP_TIMER,
  30. EV_LWIP_DNS_TIMER,
  31. EV_LWIP_SOCKET_RX_DONE,
  32. EV_LWIP_SOCKET_CREATE,
  33. EV_LWIP_SOCKET_CONNECT,
  34. EV_LWIP_SOCKET_DNS,
  35. EV_LWIP_SOCKET_DNS_IPV6,
  36. EV_LWIP_SOCKET_LISTEN,
  37. EV_LWIP_SOCKET_ACCPET,
  38. EV_LWIP_SOCKET_CLOSE,
  39. EV_LWIP_NETIF_LINK_STATE,
  40. EV_LWIP_DHCP_TIMER,
  41. EV_LWIP_FAST_TIMER,
  42. EV_LWIP_NETIF_SET_IP,
  43. EV_LWIP_NETIF_IPV6_BY_MAC,
  44. };
  45. #define SOCKET_LOCK(ID) platform_lock_mutex(prvlwip.socket[ID].mutex)
  46. #define SOCKET_UNLOCK(ID) platform_unlock_mutex(prvlwip.socket[ID].mutex)
  47. #undef platform_send_event
  48. // static net_lwip2_ctrl_struct* lwip_ctrls[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  49. static net_lwip2_ctrl_struct prvlwip;
  50. static void net_lwip2_check_network_ready(uint8_t adapter_index);
  51. static void net_lwip2_task(void *param);
  52. static void net_lwip2_create_socket_now(uint8_t adapter_index, uint8_t socket_id);
  53. static void platform_send_event(void *p, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3);
  54. static ip_addr_t *net_lwip2_get_ip6(void);
  55. static err_t net_lwip2_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port);
  56. static int net_lwip2_check_ack(uint8_t adapter_index, int socket_id);
  57. static uint32_t register_statue;
  58. static uint8_t prvlwip_inited = 0;
  59. static LUAT_RT_RET_TYPE net_lwip2_timer_cb(LUAT_RT_CB_PARAM)
  60. {
  61. platform_send_event(NULL, (uint32_t)EV_LWIP_DNS_TIMER, 0, 0, (uint32_t)param);
  62. return LUAT_RT_RET;
  63. }
  64. void net_lwip2_init(uint8_t adapter_index)
  65. {
  66. uint8_t i;
  67. for(i = 0; i < MAX_SOCK_NUM; i++)
  68. {
  69. INIT_LLIST_HEAD(&prvlwip.socket[i].wait_ack_head);
  70. INIT_LLIST_HEAD(&prvlwip.socket[i].tx_head);
  71. INIT_LLIST_HEAD(&prvlwip.socket[i].rx_head);
  72. prvlwip.socket[i].mutex = platform_create_mutex();
  73. }
  74. }
  75. void net_lwip2_set_netif(uint8_t adapter_index, struct netif *netif) {
  76. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
  77. return; // 超范围了
  78. }
  79. if (prvlwip_inited == 0) {
  80. prvlwip_inited = 1;
  81. net_lwip2_init(adapter_index);
  82. dns_init_client(&prvlwip.dns_client);
  83. }
  84. if (prvlwip.dns_udp[adapter_index] == NULL) {
  85. prvlwip.dns_udp[adapter_index] = udp_new();
  86. prvlwip.dns_udp[adapter_index]->recv = net_lwip2_dns_recv_cb;
  87. prvlwip.dns_udp[adapter_index]->recv_arg = adapter_index;
  88. #ifdef udp_bind_netif
  89. udp_bind_netif(prvlwip.dns_udp[adapter_index], netif);
  90. #endif
  91. prvlwip.dns_timer[adapter_index] = platform_create_timer(net_lwip2_timer_cb, (void *)adapter_index, 0);
  92. }
  93. prvlwip.lwip_netif[adapter_index] = netif;
  94. }
  95. static int net_lwip2_del_data_cache(void *p, void *u)
  96. {
  97. socket_data_t *pdata = (socket_data_t *)p;
  98. luat_heap_free(pdata->data);
  99. return LIST_DEL;
  100. }
  101. static int net_lwip2_next_data_cache(void *p, void *u)
  102. {
  103. socket_ctrl_t *socket = (socket_ctrl_t *)u;
  104. socket_data_t *pdata = (socket_data_t *)p;
  105. if (socket->tag != pdata->tag)
  106. {
  107. NET_DBG("tag error");
  108. luat_heap_free(pdata->data);
  109. return LIST_DEL;
  110. }
  111. return LIST_FIND;
  112. }
  113. static socket_data_t * net_lwip2_create_data_node(uint8_t socket_id, uint8_t *data, uint32_t len, luat_ip_addr_t *remote_ip, uint16_t remote_port)
  114. {
  115. socket_data_t *p = (socket_data_t *)luat_heap_malloc(sizeof(socket_data_t));
  116. if (p)
  117. {
  118. memset(p, 0, sizeof(socket_data_t));
  119. p->len = len;
  120. p->port = remote_port;
  121. if (remote_ip)
  122. {
  123. p->ip = *remote_ip;
  124. }
  125. else
  126. {
  127. ip_addr_set_zero(&p->ip);
  128. }
  129. p->tag = prvlwip.socket[socket_id].tag;
  130. if (data && len)
  131. {
  132. p->data = luat_heap_malloc(len);
  133. if (p->data)
  134. {
  135. memcpy(p->data, data, len);
  136. }
  137. else
  138. {
  139. luat_heap_free(p);
  140. return NULL;
  141. }
  142. }
  143. }
  144. return p;
  145. }
  146. static void net_lwip2_callback_to_nw_task(uint8_t adapter_index, uint32_t event_id, uint32_t param1, uint32_t param2, uint32_t param3)
  147. {
  148. luat_network_cb_param_t param = {.tag = 0, .param = prvlwip.user_data};
  149. OS_EVENT event = { .ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3};
  150. if ((event_id > EV_NW_DNS_RESULT))
  151. {
  152. if (event_id != EV_NW_SOCKET_CLOSE_OK)
  153. {
  154. event.Param3 = prvlwip.socket[param1].param;
  155. param.tag = prvlwip.socket[param1].tag;
  156. }
  157. else
  158. {
  159. event.Param3 = ((luat_network_cb_param_t *)param3)->param;
  160. param.tag = ((luat_network_cb_param_t *)param3)->tag;
  161. }
  162. }
  163. switch(event_id)
  164. {
  165. case EV_NW_SOCKET_CLOSE_OK:
  166. case EV_NW_SOCKET_CONNECT_OK:
  167. case EV_NW_SOCKET_ERROR:
  168. prvlwip.socket_busy &= ~(1 << param1);
  169. break;
  170. }
  171. prvlwip.socket_cb(&event, &param);
  172. }
  173. static void net_lwip2_tcp_error(uint8_t adapter_index, int socket_id)
  174. {
  175. prvlwip.socket[socket_id].remote_close = 1;
  176. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  177. }
  178. static err_t net_lwip2_tcp_connected_cb(void *arg, struct tcp_pcb *tpcb, err_t err)
  179. {
  180. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  181. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  182. prvlwip.socket_connect &= ~(1 << socket_id);
  183. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  184. return ERR_OK;
  185. }
  186. static int net_lwip2_rx_data(int socket_id, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  187. {
  188. int is_mem_err = 0;
  189. SOCKET_LOCK(socket_id);
  190. socket_data_t *data_p = net_lwip2_create_data_node(socket_id, NULL, 0, addr, port);
  191. if (data_p)
  192. {
  193. data_p->data = luat_heap_malloc(p->tot_len);
  194. if (data_p->data)
  195. {
  196. data_p->len = pbuf_copy_partial(p, data_p->data, p->tot_len, 0);
  197. // NET_DBG("new data %ubyte", p->tot_len);
  198. llist_add_tail(&data_p->node, &prvlwip.socket[socket_id].rx_head);
  199. prvlwip.socket[socket_id].rx_wait_size += p->tot_len;
  200. }
  201. else
  202. {
  203. luat_heap_free(data_p);
  204. is_mem_err = 1;
  205. }
  206. }
  207. else
  208. {
  209. is_mem_err = 1;
  210. }
  211. SOCKET_UNLOCK(socket_id);
  212. return is_mem_err;
  213. }
  214. static void net_lwip2_tcp_close_done(uint8_t adapter_index, int socket_id, uint8_t notify)
  215. {
  216. luat_network_cb_param_t cb_param;
  217. // LLOGD("net_lwip2_tcp_close_done 1");
  218. // OS_LOCK;
  219. // LLOGD("net_lwip2_tcp_close_done 2");
  220. SOCKET_LOCK(socket_id);
  221. // LLOGD("net_lwip2_tcp_close_done 3");
  222. cb_param.param = prvlwip.socket[socket_id].param;
  223. cb_param.tag = prvlwip.socket[socket_id].tag;
  224. prvlwip.socket[socket_id].pcb.ip = NULL;
  225. prvlwip.socket[socket_id].listen_tcp = NULL;
  226. prvlwip.socket[socket_id].remote_close = 0;
  227. prvlwip.socket[socket_id].state = 0;
  228. prvlwip.socket[socket_id].in_use = 0;
  229. prvlwip.socket[socket_id].param = NULL;
  230. prvlwip.socket[socket_id].rx_wait_size = 0;
  231. prvlwip.socket[socket_id].tx_wait_size = 0;
  232. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip2_del_data_cache, NULL);
  233. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip2_del_data_cache, NULL);
  234. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip2_del_data_cache, NULL);
  235. prvlwip.socket_busy &= ~(1 << socket_id);
  236. prvlwip.socket_connect &= ~(1 << socket_id);
  237. // OS_UNLOCK;
  238. SOCKET_UNLOCK(socket_id);
  239. if (notify)
  240. {
  241. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CLOSE_OK, socket_id, 0, &cb_param);
  242. }
  243. }
  244. static err_t net_lwip2_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb,
  245. struct pbuf *p, err_t err)
  246. {
  247. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  248. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  249. uint16_t len;
  250. if (p)
  251. {
  252. // tcp_recved(tpcb, p->tot_len);
  253. len = p->tot_len;
  254. if (net_lwip2_rx_data(socket_id, p, NULL, 0))
  255. {
  256. NET_DBG("no memory!");
  257. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  258. }
  259. else
  260. {
  261. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  262. }
  263. pbuf_free(p);
  264. }
  265. else if (err == ERR_OK)
  266. {
  267. {
  268. prvlwip.socket[socket_id].remote_close = 1;
  269. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  270. }
  271. }
  272. else
  273. {
  274. net_lwip2_tcp_error(adapter_index, socket_id);
  275. }
  276. return ERR_OK;
  277. }
  278. static err_t net_lwip2_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
  279. u16_t len)
  280. {
  281. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  282. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  283. volatile uint16_t check_len = 0;
  284. volatile uint32_t rest_len;
  285. socket_data_t *p;
  286. SOCKET_LOCK(socket_id);
  287. while(check_len < len)
  288. {
  289. if (llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  290. {
  291. NET_DBG("!");
  292. goto SOCEKT_ERROR;
  293. }
  294. p = (socket_data_t *)prvlwip.socket[socket_id].wait_ack_head.next;
  295. rest_len = p->len - p->read_pos;
  296. if ((len - check_len) >= rest_len)
  297. {
  298. // NET_DBG("adapter %d socket %d, %ubytes ack", adapter_index, socket_id, p->len);
  299. llist_del(&p->node);
  300. luat_heap_free(p->data);
  301. luat_heap_free(p);
  302. check_len += rest_len;
  303. }
  304. else
  305. {
  306. p->read_pos += (len - check_len);
  307. check_len = len;
  308. // NET_DBG("adapter %d socket %d, all %ubytes ack %ubytes ", adapter_index, socket_id, p->len, p->read_pos);
  309. }
  310. }
  311. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  312. {
  313. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip2_next_data_cache, &prvlwip.socket[socket_id]);
  314. if (p)
  315. {
  316. #if ENABLE_PSIF
  317. #if defined(CHIP_EC618)
  318. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, TCP_WRITE_FLAG_COPY, 0, 0, 0))
  319. #else
  320. sockdataflag_t dataflag={0};
  321. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0, dataflag, 0))
  322. #endif
  323. #else
  324. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  325. #endif
  326. {
  327. llist_del(&p->node);
  328. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  329. }
  330. else
  331. {
  332. // NET_DBG("tcp buf is full, wait ack and send again");
  333. break;
  334. }
  335. }
  336. }
  337. SOCKET_UNLOCK(socket_id);
  338. tcp_output(tpcb);
  339. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  340. return ERR_OK;
  341. SOCEKT_ERROR:
  342. SOCKET_UNLOCK(socket_id);
  343. net_lwip2_tcp_error(adapter_index, socket_id);
  344. return ERR_OK;
  345. }
  346. static err_t net_lwip2_tcp_err_cb(void *arg, err_t err)
  347. {
  348. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  349. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  350. if (prvlwip.socket[socket_id].is_tcp)
  351. {
  352. if (prvlwip.socket[socket_id].pcb.tcp)
  353. {
  354. prvlwip.socket[socket_id].pcb.tcp = NULL;
  355. }
  356. }
  357. if (!prvlwip.socket[socket_id].state && !prvlwip.socket[socket_id].remote_close)
  358. {
  359. NET_DBG("adapter %d socket %d not closing, but error %d", adapter_index, socket_id, err);
  360. net_lwip2_tcp_error(adapter_index, socket_id);
  361. }
  362. return 0;
  363. }
  364. static err_t net_lwip2_tcp_fast_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  365. {
  366. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  367. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  368. if (err || !newpcb)
  369. {
  370. net_lwip2_tcp_error(adapter_index, socket_id);
  371. return 0;
  372. }
  373. prvlwip.socket[socket_id].pcb.tcp = newpcb;
  374. // prvlwip.socket[socket_id].pcb.tcp->sockid = socket_id;
  375. prvlwip.socket[socket_id].rx_wait_size = 0;
  376. prvlwip.socket[socket_id].tx_wait_size = 0;
  377. prvlwip.socket[socket_id].pcb.tcp->callback_arg = arg;
  378. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip2_tcp_recv_cb;
  379. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip2_tcp_sent_cb;
  380. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip2_tcp_err_cb;
  381. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  382. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  383. return ERR_OK;
  384. }
  385. static err_t net_lwip2_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  386. {
  387. // int socket_id = ((uint32_t)arg) & 0x0000ffff;
  388. // uint8_t adapter_index = ((uint32_t)arg) >> 16;
  389. return ERR_OK;
  390. }
  391. static err_t net_lwip2_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  392. const ip_addr_t *addr, u16_t port)
  393. {
  394. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  395. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  396. uint16_t len = 0;
  397. // LLOGD("net_lwip2_udp_recv_cb %d %d", socket_id, adapter_index);
  398. if (p)
  399. {
  400. len = p->tot_len;
  401. if (net_lwip2_rx_data(socket_id, p, addr, port))
  402. {
  403. NET_DBG("no memory!");
  404. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  405. }
  406. else
  407. {
  408. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  409. }
  410. pbuf_free(p);
  411. }
  412. return ERR_OK;
  413. }
  414. static int32_t net_lwip2_dns_check_result(void *data, void *param)
  415. {
  416. luat_dns_require_t *require = (luat_dns_require_t *)data;
  417. if (require->result != 0)
  418. {
  419. luat_heap_free(require->uri.Data);
  420. require->uri.Data = NULL;
  421. if (require->result > 0)
  422. {
  423. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  424. int i;
  425. for(i = 0; i < require->result; i++)
  426. {
  427. ip_result[i] = require->ip_result[i];
  428. }
  429. net_lwip2_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  430. }
  431. else
  432. {
  433. net_lwip2_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, 0, 0, require->param);
  434. }
  435. return LIST_DEL;
  436. }
  437. else
  438. {
  439. return LIST_PASS;
  440. }
  441. }
  442. static err_t net_lwip2_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  443. {
  444. Buffer_Struct msg_buf;
  445. Buffer_Struct tx_msg_buf = {0,0,0};
  446. struct pbuf *out_p;
  447. ip_addr_t *t_ip;
  448. int i;
  449. uint8_t adapter_index = (uint32_t)arg;
  450. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY || prvlwip.lwip_netif[adapter_index] == NULL) {
  451. return ERR_OK;
  452. }
  453. // char ip_string[64];
  454. //LLOGD("%s:%d", __FILE__, __LINE__);
  455. if (p)
  456. {
  457. OS_InitBuffer(&msg_buf, p->tot_len);
  458. pbuf_copy_partial(p, msg_buf.Data, p->tot_len, 0);
  459. pbuf_free(p);
  460. dns_run(&prvlwip.dns_client, &msg_buf, NULL, &i);
  461. OS_DeInitBuffer(&msg_buf);
  462. llist_traversal(&prvlwip.dns_client.require_head, net_lwip2_dns_check_result, NULL);
  463. {
  464. dns_run(&prvlwip.dns_client, NULL, &tx_msg_buf, &i);
  465. if (tx_msg_buf.Pos)
  466. {
  467. out_p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_ROM);
  468. if (out_p)
  469. {
  470. out_p->payload = tx_msg_buf.Data;
  471. prvlwip.dns_udp[adapter_index]->local_ip = prvlwip.lwip_netif[adapter_index]->ip_addr;
  472. udp_sendto(prvlwip.dns_udp[adapter_index], out_p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  473. pbuf_free(out_p);
  474. }
  475. OS_DeInitBuffer(&tx_msg_buf);
  476. llist_traversal(&prvlwip.dns_client.require_head, net_lwip2_dns_check_result, NULL);
  477. }
  478. }
  479. }
  480. if (!prvlwip.dns_client.is_run && prvlwip.dns_timer[adapter_index])
  481. {
  482. platform_stop_timer(prvlwip.dns_timer[adapter_index]);
  483. }
  484. return ERR_OK;
  485. }
  486. static void net_lwip2_dns_tx_next(uint8_t adapter_index, Buffer_Struct *tx_msg_buf)
  487. {
  488. int i;
  489. err_t err;
  490. struct pbuf *p;
  491. // LLOGD("CALL net_lwip2_dns_tx_next");
  492. dns_run(&prvlwip.dns_client, NULL, tx_msg_buf, &i);
  493. char ip_string[64];
  494. if (tx_msg_buf->Pos || prvlwip.dns_client.new_result)
  495. {
  496. p = pbuf_alloc(PBUF_RAW, tx_msg_buf->Pos, PBUF_ROM);
  497. if (p)
  498. {
  499. p->payload = tx_msg_buf->Data;
  500. err = udp_connect(prvlwip.dns_udp[adapter_index], &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  501. if (err) {
  502. LLOGI("udp_connect ret %d");
  503. }
  504. else {
  505. err = udp_sendto(prvlwip.dns_udp[adapter_index], p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  506. if (err) {
  507. LLOGI("udp_sendto ret %d");
  508. }
  509. }
  510. // NET_DBG("udp_connect dns_udp %d:%s",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)), err);
  511. // LLOGD("udp_sendto dns_udp");
  512. // err = udp_sendto(prvlwip.dns_udp, p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  513. // NET_DBG("udp_sendto dns_udp %d:%s ret %d",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)), err);
  514. pbuf_free(p);
  515. }
  516. OS_DeInitBuffer(tx_msg_buf);
  517. llist_traversal(&prvlwip.dns_client.require_head, net_lwip2_dns_check_result, NULL);
  518. prvlwip.dns_client.new_result = 0;
  519. }
  520. }
  521. // uint32_t net_lwip2_rand()
  522. // {
  523. // PV_Union uPV;
  524. // luat_crypto_trng(uPV.u8, 4);
  525. // return uPV.u32;
  526. // }
  527. // void net_lwip2_set_local_ip6(ip6_addr_t *ip)
  528. // {
  529. // prvlwip.ec618_ipv6.u_addr.ip6 = *ip;
  530. // prvlwip.ec618_ipv6.type = IPADDR_TYPE_V6;
  531. // }
  532. static void net_lwip2_close_tcp(int socket_id)
  533. {
  534. prvlwip.socket[socket_id].pcb.tcp->sent = NULL;
  535. prvlwip.socket[socket_id].pcb.tcp->errf = NULL;
  536. prvlwip.socket[socket_id].pcb.tcp->recv = tcp_recv_null;
  537. prvlwip.socket[socket_id].pcb.tcp->callback_arg = 0;
  538. prvlwip.socket[socket_id].pcb.tcp->pollinterval = 2;
  539. if (tcp_close(prvlwip.socket[socket_id].pcb.tcp))
  540. {
  541. tcp_abort(prvlwip.socket[socket_id].pcb.tcp);
  542. }
  543. prvlwip.socket[socket_id].pcb.tcp = NULL;
  544. }
  545. static void net_lwip2_task(void *param)
  546. {
  547. luat_network_cb_param_t cb_param;
  548. OS_EVENT event = *((OS_EVENT *)param);
  549. luat_heap_free(param);
  550. Buffer_Struct tx_msg_buf = {0,0,0};
  551. // HANDLE cur_task = luat_get_current_task();
  552. struct netif *netif;
  553. socket_data_t *p;
  554. ip_addr_t *p_ip, *local_ip;
  555. struct pbuf *out_p;
  556. int error, i;
  557. PV_Union uPV;
  558. // uint8_t active_flag;
  559. uint8_t socket_id;
  560. uint8_t adapter_index;
  561. socket_id = event.Param1;
  562. adapter_index = event.Param3;
  563. char ip_string[64] = {0};
  564. // LLOGD("CALL net_lwip2_task event %08X adapter_index %d", event.ID, adapter_index);
  565. switch(event.ID)
  566. {
  567. case EV_LWIP_SOCKET_TX:
  568. SOCKET_LOCK(socket_id);
  569. if (prvlwip.socket[socket_id].in_use && prvlwip.socket[socket_id].pcb.ip)
  570. {
  571. // if (!prvlwip.socket[socket_id].pcb.tcp->unsent && !prvlwip.socket[socket_id].pcb.tcp->unacked)
  572. // {
  573. // active_flag = 0;
  574. // }
  575. // else
  576. // {
  577. // active_flag = 1;
  578. // }
  579. if (prvlwip.socket[socket_id].is_tcp)
  580. {
  581. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  582. {
  583. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip2_next_data_cache, &prvlwip.socket[socket_id]);
  584. if (p->len <= tcp_sndbuf(prvlwip.socket[socket_id].pcb.tcp))
  585. {
  586. #if ENABLE_PSIF
  587. #if defined(CHIP_EC618)
  588. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, TCP_WRITE_FLAG_COPY, 0, 0, 0))
  589. #else
  590. sockdataflag_t dataflag={0};
  591. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0, dataflag, 0))
  592. #endif
  593. #else
  594. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  595. #endif
  596. {
  597. llist_del(&p->node);
  598. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  599. }
  600. else
  601. {
  602. // NET_DBG("tcp buf is full, wait ack and send again");
  603. break;
  604. }
  605. }
  606. else
  607. {
  608. // NET_DBG("tcp buf is full, wait ack and send again");
  609. break;
  610. }
  611. }
  612. SOCKET_UNLOCK(socket_id);
  613. tcp_output(prvlwip.socket[socket_id].pcb.tcp);
  614. prvlwip.socket_busy |= (1 << socket_id);
  615. }
  616. else
  617. {
  618. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip2_next_data_cache, &prvlwip.socket[socket_id]);
  619. if (p)
  620. {
  621. llist_del(&p->node);
  622. }
  623. SOCKET_UNLOCK(socket_id);
  624. if (p)
  625. {
  626. uint32_t len = p->len;
  627. out_p = pbuf_alloc(PBUF_RAW, p->len, PBUF_ROM);
  628. if (out_p)
  629. {
  630. out_p->payload = p->data;
  631. error = udp_sendto(prvlwip.socket[socket_id].pcb.udp, out_p, &p->ip, p->port);
  632. // LLOGD("udp_sendto ret %d", error);
  633. pbuf_free(out_p);
  634. }
  635. else
  636. {
  637. NET_DBG("mem err send fail");
  638. }
  639. luat_heap_free(p->data);
  640. luat_heap_free(p);
  641. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  642. }
  643. }
  644. }
  645. else
  646. {
  647. NET_DBG("adapter %d socket %d no in use! %x", adapter_index, socket_id, prvlwip.socket[socket_id].pcb.ip);
  648. SOCKET_UNLOCK(socket_id);
  649. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  650. }
  651. break;
  652. case EV_LWIP_DNS_TIMER:
  653. #ifdef LUAT_USE_DNS
  654. net_lwip2_dns_tx_next(adapter_index, &tx_msg_buf);
  655. if (!prvlwip.dns_client.is_run && prvlwip.dns_timer[adapter_index])
  656. {
  657. platform_stop_timer(prvlwip.dns_timer[adapter_index]);
  658. }
  659. #endif
  660. break;
  661. case EV_LWIP_SOCKET_RX_DONE:
  662. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip || !prvlwip.socket[socket_id].is_tcp)
  663. {
  664. NET_DBG("error socket %d state %d,%x,%d", socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip, prvlwip.socket[socket_id].is_tcp);
  665. break;
  666. }
  667. // NET_DBG("socket %d rx ack %dbytes", socket_id, event.Param2);
  668. tcp_recved(prvlwip.socket[socket_id].pcb.tcp, event.Param2);
  669. break;
  670. case EV_LWIP_SOCKET_CREATE:
  671. net_lwip2_create_socket_now(adapter_index, socket_id);
  672. break;
  673. case EV_LWIP_SOCKET_CONNECT:
  674. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  675. {
  676. NET_DBG("adapter %d socket %d cannot use! %d,%x", adapter_index, socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip);
  677. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  678. break;
  679. }
  680. p_ip = (ip_addr_t *)event.Param2;
  681. ipaddr_ntoa_r(p_ip, ip_string, 64);
  682. LLOGD("connect %s:%d %s", ip_string, prvlwip.socket[socket_id].remote_port, prvlwip.socket[socket_id].is_tcp ? "TCP" : "UDP");
  683. local_ip = NULL;
  684. // if (p_ip->type == IPADDR_TYPE_V4)
  685. // {
  686. local_ip = &prvlwip.lwip_netif[adapter_index]->ip_addr;
  687. // char ip_string[64];
  688. // ipaddr_ntoa_r(&prvlwip.lwip_netif->ip_addr, ip_string, 64);
  689. // LLOGD("EV_LWIP_SOCKET_CONNECT local_ip %s", ip_string);
  690. // }
  691. // else
  692. // {
  693. // local_ip = net_lwip2_get_ip6();
  694. // }
  695. if (!local_ip && prvlwip.socket[socket_id].is_tcp)
  696. {
  697. NET_DBG("netif no ip !!!!!!");
  698. net_lwip2_tcp_error(adapter_index, socket_id);
  699. break;
  700. }
  701. if (prvlwip.socket[socket_id].is_tcp)
  702. {
  703. error = tcp_bind(prvlwip.socket[socket_id].pcb.tcp, local_ip, 0);
  704. if (error) {
  705. NET_DBG("adapter %d socket %d tcp bind error %d", adapter_index, socket_id, error);
  706. net_lwip2_tcp_error(adapter_index, socket_id);
  707. return;
  708. }
  709. error = tcp_connect(prvlwip.socket[socket_id].pcb.tcp, p_ip, prvlwip.socket[socket_id].remote_port, net_lwip2_tcp_connected_cb);
  710. if (error)
  711. {
  712. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  713. net_lwip2_tcp_error(adapter_index, socket_id);
  714. }
  715. else
  716. {
  717. prvlwip.socket_connect |= (1 << socket_id);
  718. }
  719. }
  720. else
  721. {
  722. error = udp_bind(prvlwip.socket[socket_id].pcb.udp, local_ip, prvlwip.socket[socket_id].local_port);
  723. if (error) {
  724. NET_DBG("udp bind ret %d port %d", error, prvlwip.socket[socket_id].local_port);
  725. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  726. return;
  727. }
  728. error = udp_connect(prvlwip.socket[socket_id].pcb.udp, p_ip, prvlwip.socket[socket_id].remote_port);
  729. if (error)
  730. {
  731. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  732. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  733. }
  734. else
  735. {
  736. if (!prvlwip.socket[socket_id].remote_port)
  737. {
  738. prvlwip.socket[socket_id].pcb.udp->flags &= ~UDP_FLAGS_CONNECTED;
  739. }
  740. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  741. }
  742. }
  743. break;
  744. case EV_LWIP_SOCKET_DNS:
  745. case EV_LWIP_SOCKET_DNS_IPV6:
  746. // LLOGD("event dns query");
  747. if (!prvlwip.dns_client.is_run && prvlwip.dns_timer[adapter_index])
  748. {
  749. platform_start_timer(prvlwip.dns_timer[adapter_index], 1000, 1);
  750. }
  751. dns_require_ipv6(&prvlwip.dns_client, event.Param1, event.Param2, event.Param3, (event.ID - EV_LWIP_SOCKET_DNS));
  752. // LLOGD("event dns query 2");
  753. net_lwip2_dns_tx_next(adapter_index, &tx_msg_buf);
  754. // LLOGD("event dns query 3");
  755. break;
  756. case EV_LWIP_SOCKET_LISTEN:
  757. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  758. {
  759. NET_DBG("adapter %d socket %d cannot use! %d,%x", adapter_index, socket_id, prvlwip.socket[socket_id].in_use, prvlwip.socket[socket_id].pcb.ip);
  760. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  761. break;
  762. }
  763. error = tcp_bind(prvlwip.socket[socket_id].pcb.tcp, NULL, prvlwip.socket[socket_id].local_port);
  764. if (error) {
  765. LLOGE("tcp listen port %d ret %d", prvlwip.socket[socket_id].local_port, error);
  766. net_lwip2_tcp_error(adapter_index, socket_id);
  767. }
  768. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->local_ip, IPADDR_TYPE_ANY);
  769. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
  770. // prvlwip.socket[socket_id].pcb.tcp->sockid = -1;
  771. prvlwip.socket[socket_id].listen_tcp = tcp_listen_with_backlog(prvlwip.socket[socket_id].pcb.tcp, 1);
  772. if (!prvlwip.socket[socket_id].listen_tcp) {
  773. NET_DBG("socket %d listen failed");
  774. net_lwip2_tcp_error(adapter_index, socket_id);
  775. } else {
  776. PV_Union uPV;
  777. uPV.u16[0] = socket_id;
  778. uPV.u16[1] = adapter_index;
  779. prvlwip.socket[socket_id].listen_tcp->callback_arg = uPV.u32;
  780. prvlwip.socket[socket_id].listen_tcp->accept = net_lwip2_tcp_fast_accept_cb;
  781. prvlwip.socket[socket_id].pcb.tcp = NULL;
  782. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_SOCKET_LISTEN, socket_id, 0, 0);
  783. }
  784. break;
  785. // case EV_LWIP_SOCKET_ACCPET:
  786. //
  787. // break;
  788. case EV_LWIP_SOCKET_CLOSE:
  789. // LLOGD("event EV_LWIP_SOCKET_CLOSE 1");
  790. if (!prvlwip.socket[socket_id].in_use)
  791. {
  792. NET_DBG("socket %d no in use!,%x", socket_id);
  793. break;
  794. }
  795. // LLOGD("event EV_LWIP_SOCKET_CLOSE 2");
  796. if (prvlwip.socket[socket_id].listen_tcp)
  797. {
  798. tcp_close(prvlwip.socket[socket_id].listen_tcp);
  799. prvlwip.socket[socket_id].listen_tcp = NULL;
  800. if (prvlwip.socket[socket_id].pcb.tcp)
  801. {
  802. net_lwip2_close_tcp(socket_id);
  803. }
  804. net_lwip2_tcp_close_done(adapter_index, socket_id, event.Param2);
  805. break;
  806. }
  807. // LLOGD("event EV_LWIP_SOCKET_CLOSE 3");
  808. if (prvlwip.socket[socket_id].pcb.ip)
  809. {
  810. if (prvlwip.socket[socket_id].is_tcp)
  811. {
  812. net_lwip2_close_tcp(socket_id);
  813. }
  814. else
  815. {
  816. // LLOGD("event EV_LWIP_SOCKET_CLOSE 31");
  817. udp_remove(prvlwip.socket[socket_id].pcb.udp);
  818. // LLOGD("event EV_LWIP_SOCKET_CLOSE 32");
  819. }
  820. net_lwip2_tcp_close_done(adapter_index, socket_id, event.Param2);
  821. break;
  822. }
  823. // LLOGD("event EV_LWIP_SOCKET_CLOSE 4");
  824. if (prvlwip.socket[socket_id].remote_close)
  825. {
  826. net_lwip2_tcp_close_done(adapter_index, socket_id, event.Param2);
  827. break;
  828. }
  829. // LLOGD("event EV_LWIP_SOCKET_CLOSE DONE");
  830. break;
  831. case EV_LWIP_NETIF_LINK_STATE:
  832. net_lwip2_check_network_ready(event.Param3);
  833. break;
  834. default:
  835. NET_DBG("unknow event %x,%x", event.ID, event.Param1);
  836. break;
  837. }
  838. // LLOGD("End of lwip task");
  839. }
  840. static void platform_send_event(void *p, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3)
  841. {
  842. OS_EVENT *event = luat_heap_malloc(sizeof(OS_EVENT));
  843. event->ID = id;
  844. event->Param1 = param1;
  845. event->Param2 = param2;
  846. event->Param3 = param3;
  847. #if NO_SYS
  848. net_lwip2_task(event);
  849. #else
  850. tcpip_callback_with_block(net_lwip2_task, event, 1);
  851. #endif
  852. }
  853. static void net_lwip2_check_network_ready(uint8_t adapter_index)
  854. {
  855. int i = 0;
  856. luat_ip_addr_t addr = {0};
  857. // char ip_string[64];
  858. if (prvlwip.lwip_netif[adapter_index] == NULL)
  859. return;
  860. uint8_t active_flag = !ip_addr_isany(&prvlwip.lwip_netif[adapter_index]->ip_addr);
  861. if (prvlwip.netif_network_ready[adapter_index] == active_flag) {
  862. LLOGD("网络[%d]状态没有变化, 跳过检查", adapter_index);
  863. return;
  864. }
  865. prvlwip.netif_network_ready[adapter_index] = active_flag;
  866. if (!active_flag)
  867. {
  868. // TODO 如果没有活跃的netif了, 应该关闭dns客户端
  869. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 0, adapter_index);
  870. }
  871. else
  872. {
  873. NET_DBG("network ready");
  874. if (prvlwip.lwip_netif[adapter_index] != NULL && !ip_addr_isany(&prvlwip.lwip_netif[adapter_index]->gw)) {
  875. NET_DBG("使用网关作为默认DNS服务器");
  876. net_lwip2_set_dns_server(0, &prvlwip.lwip_netif[adapter_index]->gw, (void*)adapter_index);
  877. }
  878. else {
  879. NET_DBG("使用223.5.5.5作为默认DNS服务器");
  880. ip4addr_aton("223.5.5.5", &addr);
  881. net_lwip2_set_dns_server(0, &addr, (void*)adapter_index);
  882. }
  883. ip4addr_aton("114.114.114.114", &addr);
  884. net_lwip2_set_dns_server(1, &addr, (void*)adapter_index);
  885. ip4addr_aton("223.5.5.5", &addr);
  886. net_lwip2_set_dns_server(2, &addr, (void*)adapter_index);
  887. ip4addr_aton("119.29.29.29", &addr);
  888. net_lwip2_set_dns_server(3, &addr, (void*)adapter_index);
  889. net_lwip2_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 1, adapter_index);
  890. }
  891. }
  892. static int net_lwip2_check_socket(void *user_data, int socket_id, uint64_t tag)
  893. {
  894. uint8_t adapter_index = (uint32_t)user_data;
  895. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  896. if (socket_id >= MAX_SOCK_NUM) return -1;
  897. if (prvlwip.socket[socket_id].tag != tag) return -1;
  898. if (!prvlwip.socket[socket_id].in_use || prvlwip.socket[socket_id].state) return -1;
  899. return 0;
  900. }
  901. static int net_lwip2_socket_check(int socket_id, uint64_t tag, void *user_data)
  902. {
  903. return net_lwip2_check_socket(user_data, socket_id, tag);
  904. }
  905. static uint8_t net_lwip2_check_ready(void *user_data)
  906. {
  907. uint8_t adapter_index = (uint32_t)user_data;
  908. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  909. // LLOGD("lwip查询网络就绪情况 %d", prvlwip.netif_network_ready[adapter_index]);
  910. if (prvlwip.lwip_netif[adapter_index] == NULL) {
  911. LLOGD("lwip netif is null %d", adapter_index);
  912. return 0;
  913. }
  914. return !ip_addr_isany(&prvlwip.lwip_netif[adapter_index]->ip_addr);
  915. }
  916. static void net_lwip2_create_socket_now(uint8_t adapter_index, uint8_t socket_id)
  917. {
  918. PV_Union uPV;
  919. uPV.u16[0] = socket_id;
  920. uPV.u16[1] = adapter_index;
  921. if (socket_id >= MAX_SOCK_NUM)
  922. return;
  923. if (prvlwip.socket[socket_id].is_tcp)
  924. {
  925. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  926. if (!prvlwip.socket[socket_id].pcb.tcp)
  927. {
  928. NET_DBG("try to abort fin wait 1 tcp");
  929. struct tcp_pcb *pcb, *dpcb;
  930. uint32_t low_time = (uint32_t)(luat_mcu_tick64_ms() / 1000);
  931. dpcb = NULL;
  932. for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  933. {
  934. if (FIN_WAIT_1 == pcb->state)
  935. {
  936. if (((uint32_t)pcb->callback_arg) < low_time)
  937. {
  938. dpcb = pcb;
  939. low_time = (uint32_t)pcb->callback_arg;
  940. }
  941. }
  942. }
  943. if (dpcb)
  944. {
  945. tcp_abort(dpcb);
  946. }
  947. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  948. }
  949. if (prvlwip.socket[socket_id].pcb.tcp)
  950. {
  951. // prvlwip.socket[socket_id].pcb.tcp->sockid = socket_id;
  952. // prvlwip.socket[socket_id].pcb.tcp->local_ip = prvlwip.lwip_netif->ip_addr;
  953. prvlwip.socket[socket_id].rx_wait_size = 0;
  954. prvlwip.socket[socket_id].tx_wait_size = 0;
  955. prvlwip.socket[socket_id].pcb.tcp->callback_arg = uPV.p;
  956. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip2_tcp_recv_cb;
  957. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip2_tcp_sent_cb;
  958. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip2_tcp_err_cb;
  959. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  960. // tcp_set_flags(prvlwip.socket[socket_id].pcb.tcp, TCP_NODELAY);
  961. }
  962. else
  963. {
  964. NET_DBG("tcp pcb full!");
  965. net_lwip2_tcp_error(adapter_index, socket_id);
  966. }
  967. }
  968. else
  969. {
  970. prvlwip.socket[socket_id].pcb.udp = udp_new();
  971. if (prvlwip.socket[socket_id].pcb.udp)
  972. {
  973. prvlwip.socket[socket_id].pcb.udp->recv_arg = uPV.p;
  974. prvlwip.socket[socket_id].pcb.udp->recv = net_lwip2_udp_recv_cb;
  975. prvlwip.socket[socket_id].pcb.udp->so_options |= SOF_BROADCAST|SOF_REUSEADDR;
  976. }
  977. else
  978. {
  979. NET_DBG("udp pcb full!");
  980. net_lwip2_tcp_error(adapter_index, socket_id);
  981. }
  982. }
  983. }
  984. static int net_lwip2_create_socket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  985. {
  986. uint8_t index = (uint32_t)user_data;
  987. uint8_t adapter_index = (uint32_t)user_data;
  988. if ((uint32_t)adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  989. int i, socket_id;
  990. socket_id = -1;
  991. // OS_LOCK;
  992. if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
  993. {
  994. socket_id = prvlwip.next_socket_index;
  995. prvlwip.next_socket_index++;
  996. }
  997. else
  998. {
  999. for (i = 0; i < MAX_SOCK_NUM; i++)
  1000. {
  1001. if (!prvlwip.socket[i].in_use)
  1002. {
  1003. socket_id = i;
  1004. prvlwip.next_socket_index = i + 1;
  1005. break;
  1006. }
  1007. }
  1008. }
  1009. if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
  1010. {
  1011. prvlwip.next_socket_index = 0;
  1012. }
  1013. if (socket_id >= 0)
  1014. {
  1015. LWIP_ASSERT("socket must free before create", !prvlwip.socket[socket_id].pcb.ip);
  1016. prvlwip.socket_busy &= ~(1 << socket_id);
  1017. prvlwip.socket_connect &= ~(1 << socket_id);
  1018. prvlwip.socket_tag++;
  1019. *tag = prvlwip.socket_tag;
  1020. prvlwip.socket[socket_id].in_use = 1;
  1021. prvlwip.socket[socket_id].tag = *tag;
  1022. prvlwip.socket[socket_id].param = param;
  1023. prvlwip.socket[socket_id].is_tcp = is_tcp;
  1024. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip2_del_data_cache, NULL);
  1025. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip2_del_data_cache, NULL);
  1026. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip2_del_data_cache, NULL);
  1027. // OS_UNLOCK;
  1028. // if (platform_get_current_task() == prvlwip.task_handle)
  1029. {
  1030. net_lwip2_create_socket_now(index, socket_id);
  1031. return socket_id;
  1032. }
  1033. // platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CREATE, socket_id, 0, user_data);
  1034. }
  1035. else
  1036. {
  1037. // OS_UNLOCK;
  1038. }
  1039. return socket_id;
  1040. }
  1041. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1042. static int net_lwip2_socket_connect(int socket_id, uint64_t tag, uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1043. {
  1044. uint8_t adapter_index = (uint32_t)user_data;
  1045. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1046. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1047. if (result) return result;
  1048. prvlwip.socket[socket_id].local_port = local_port;
  1049. prvlwip.socket[socket_id].remote_port = remote_port;
  1050. platform_send_event(NULL, EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, user_data);
  1051. return 0;
  1052. }
  1053. //作为server绑定一个port,开始监听
  1054. static int net_lwip2_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1055. {
  1056. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1057. if (result) return result;
  1058. uint8_t adapter_index = (uint32_t)user_data;
  1059. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1060. prvlwip.socket[socket_id].local_port = local_port;
  1061. platform_send_event(NULL, EV_LWIP_SOCKET_LISTEN, socket_id, local_port, user_data);
  1062. return 0;
  1063. }
  1064. //作为server接受一个client
  1065. static int net_lwip2_socket_accept(int socket_id, uint64_t tag, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1066. {
  1067. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1068. if (result) return result;
  1069. uint8_t adapter_index = (uint32_t)user_data;
  1070. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1071. *remote_ip = prvlwip.socket[socket_id].pcb.tcp->remote_ip;
  1072. *remote_port = prvlwip.socket[socket_id].pcb.tcp->remote_port;
  1073. return 0;
  1074. }
  1075. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1076. static int net_lwip2_socket_disconnect(int socket_id, uint64_t tag, void *user_data)
  1077. {
  1078. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1079. if (result) return result;
  1080. uint8_t adapter_index = (uint32_t)user_data;
  1081. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1082. prvlwip.socket[socket_id].state = 1;
  1083. platform_send_event(NULL, EV_LWIP_SOCKET_CLOSE, socket_id, 1, user_data);
  1084. return 0;
  1085. }
  1086. static int net_lwip2_socket_force_close(int socket_id, void *user_data)
  1087. {
  1088. if (socket_id >= MAX_SOCK_NUM) return -1;
  1089. uint8_t adapter_index = (uint32_t)user_data;
  1090. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1091. if (prvlwip.socket[socket_id].in_use && !prvlwip.socket[socket_id].state)
  1092. {
  1093. prvlwip.socket[socket_id].state = 1;
  1094. platform_send_event(NULL, EV_LWIP_SOCKET_CLOSE, socket_id, 0, user_data);
  1095. }
  1096. return 0;
  1097. }
  1098. static int net_lwip2_socket_close(int socket_id, uint64_t tag, void *user_data)
  1099. {
  1100. if (socket_id >= MAX_SOCK_NUM) return -1;
  1101. uint8_t adapter_index = (uint32_t)user_data;
  1102. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1103. if (prvlwip.socket[socket_id].tag != tag)
  1104. {
  1105. NET_DBG("socket %d used by other!", socket_id);
  1106. return -1;
  1107. }
  1108. if (!prvlwip.socket[socket_id].in_use) return 0;
  1109. net_lwip2_socket_force_close(socket_id, user_data);
  1110. return 0;
  1111. }
  1112. static uint32_t net_lwip2_socket_read_data(int socket_id, uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
  1113. {
  1114. uint32_t dummy_len;
  1115. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1116. memcpy(buf, p->data + p->read_pos, dummy_len);
  1117. p->read_pos += dummy_len;
  1118. if (p->read_pos >= p->len)
  1119. {
  1120. if (prvlwip.socket[socket_id].is_tcp)
  1121. {
  1122. platform_send_event(NULL, EV_LWIP_SOCKET_RX_DONE, socket_id, p->len, 0);
  1123. }
  1124. llist_del(&p->node);
  1125. luat_heap_free(p->data);
  1126. luat_heap_free(p);
  1127. }
  1128. *read_len += dummy_len;
  1129. return dummy_len;
  1130. }
  1131. static int net_lwip2_socket_receive(int socket_id, uint64_t tag, uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1132. {
  1133. if (socket_id >= MAX_SOCK_NUM) return -1;
  1134. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1135. if (result) return result;
  1136. uint8_t adapter_index = (uint32_t)user_data;
  1137. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1138. uint32_t read_len = 0;
  1139. if (buf)
  1140. {
  1141. SOCKET_LOCK(socket_id);
  1142. socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip2_next_data_cache, &prvlwip.socket[socket_id]);
  1143. if (prvlwip.socket[socket_id].is_tcp)
  1144. {
  1145. while((read_len < len) && p)
  1146. {
  1147. prvlwip.socket[socket_id].rx_wait_size -= net_lwip2_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1148. p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip2_next_data_cache, &prvlwip.socket[socket_id]);
  1149. }
  1150. }
  1151. else
  1152. {
  1153. if (p)
  1154. {
  1155. if (remote_ip)
  1156. {
  1157. *remote_ip = p->ip;
  1158. }
  1159. if (remote_port)
  1160. {
  1161. *remote_port = p->port;
  1162. }
  1163. prvlwip.socket[socket_id].rx_wait_size -= net_lwip2_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1164. }
  1165. }
  1166. if (llist_empty(&prvlwip.socket[socket_id].rx_head))
  1167. {
  1168. prvlwip.socket[socket_id].rx_wait_size = 0;
  1169. }
  1170. SOCKET_UNLOCK(socket_id);
  1171. }
  1172. else
  1173. {
  1174. read_len = prvlwip.socket[socket_id].rx_wait_size;
  1175. }
  1176. return read_len;
  1177. }
  1178. static int net_lwip2_socket_send(int socket_id, uint64_t tag, const uint8_t *buf, uint32_t len, int flags, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1179. {
  1180. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1181. if (result) return result;
  1182. uint8_t adapter_index = (uint32_t)user_data;
  1183. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1184. SOCKET_LOCK(socket_id);
  1185. uint32_t save_len = 0;
  1186. uint32_t dummy_len = 0;
  1187. socket_data_t *p;
  1188. if (prvlwip.socket[socket_id].is_tcp)
  1189. {
  1190. while(save_len < len)
  1191. {
  1192. dummy_len = ((len - save_len) > SOCKET_BUF_LEN)?SOCKET_BUF_LEN:(len - save_len);
  1193. p = net_lwip2_create_data_node(socket_id, &buf[save_len], dummy_len, remote_ip, remote_port);
  1194. if (p)
  1195. {
  1196. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1197. }
  1198. else
  1199. {
  1200. SOCKET_UNLOCK(socket_id);
  1201. return -1;
  1202. }
  1203. save_len += dummy_len;
  1204. }
  1205. }
  1206. else
  1207. {
  1208. p = net_lwip2_create_data_node(socket_id, buf, len, remote_ip, remote_port);
  1209. if (p)
  1210. {
  1211. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1212. }
  1213. else
  1214. {
  1215. SOCKET_UNLOCK(socket_id);
  1216. return -1;
  1217. }
  1218. }
  1219. SOCKET_UNLOCK(socket_id);
  1220. platform_send_event(NULL, EV_LWIP_SOCKET_TX, socket_id, 0, user_data);
  1221. result = len;
  1222. return result;
  1223. }
  1224. void net_lwip2_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1225. {
  1226. uint8_t adapter_index = (uint32_t)user_data;
  1227. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1228. int socket_list[MAX_SOCK_NUM];
  1229. memset(socket_list, 0, sizeof(socket_list));
  1230. uint32_t i;
  1231. for(i = 0; i < num; i++)
  1232. {
  1233. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1234. {
  1235. socket_list[vaild_socket_list[i]] = 1;
  1236. }
  1237. NET_DBG("%d,%d",i,vaild_socket_list[i]);
  1238. }
  1239. for(i = 0; i < MAX_SOCK_NUM; i++)
  1240. {
  1241. NET_DBG("%d,%d",i,socket_list[i]);
  1242. if ( !socket_list[i] )
  1243. {
  1244. net_lwip2_socket_force_close(i, user_data);
  1245. }
  1246. }
  1247. }
  1248. static int net_lwip2_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
  1249. {
  1250. uint8_t adapter_index = (uint32_t)user_data;
  1251. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1252. if (!prvlwip.lwip_netif[adapter_index]) return -1;
  1253. *ip = prvlwip.lwip_netif[adapter_index]->ip_addr;
  1254. *submask = prvlwip.lwip_netif[adapter_index]->netmask;
  1255. *gateway = prvlwip.lwip_netif[adapter_index]->gw;
  1256. return 0;
  1257. }
  1258. static int net_lwip2_get_full_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1259. {
  1260. uint8_t adapter_index = (uint32_t)user_data;
  1261. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1262. if (!prvlwip.lwip_netif[adapter_index]) return -1;
  1263. *ip = prvlwip.lwip_netif[adapter_index]->ip_addr;
  1264. *submask = prvlwip.lwip_netif[adapter_index]->netmask;
  1265. *gateway = prvlwip.lwip_netif[adapter_index]->gw;
  1266. // luat_ip_addr_t *local_ip = net_lwip2_get_ip6();
  1267. // if (local_ip)
  1268. // {
  1269. // *ipv6 = *local_ip;
  1270. // }
  1271. // else
  1272. // {
  1273. // ipv6->type = 0xff;
  1274. // }
  1275. return 0;
  1276. }
  1277. static int net_lwip2_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1278. {
  1279. return 0;
  1280. }
  1281. static int net_lwip2_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1282. {
  1283. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1284. uint8_t adapter_index = (uint32_t)user_data;
  1285. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1286. char *prv_domain_name = (char *)zalloc(len + 1);
  1287. memcpy(prv_domain_name, domain_name, len);
  1288. platform_send_event(NULL, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1289. return 0;
  1290. }
  1291. static int net_lwip2_dns_ipv6(const char *domain_name, uint32_t len, void *param, void *user_data)
  1292. {
  1293. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1294. uint8_t adapter_index = (uint32_t)user_data;
  1295. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1296. char *prv_domain_name = (char *)zalloc(len + 1);
  1297. memcpy(prv_domain_name, domain_name, len);
  1298. // platform_send_event(prvlwip.task_handle, (prvlwip.ec618_ipv6.type != IPADDR_TYPE_V6)?EV_LWIP_SOCKET_DNS:EV_LWIP_SOCKET_DNS_IPV6, prv_domain_name, param, user_data);
  1299. platform_send_event(NULL, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1300. return 0;
  1301. }
  1302. static int net_lwip2_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1303. {
  1304. uint8_t adapter_index = (uint32_t)user_data;
  1305. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1306. if (server_index >= MAX_DNS_SERVER) return -1;
  1307. prvlwip.dns_client.dns_server[server_index] = *ip;
  1308. prvlwip.dns_client.is_static_dns[server_index] = 1;
  1309. return 0;
  1310. }
  1311. static int net_lwip2_set_mac(uint8_t *mac, void *user_data)
  1312. {
  1313. // uint8_t index = (uint32_t)user_data;
  1314. // if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1315. // if (!prvlwip.lwip_netif) return -1;
  1316. // memcpy(prvlwip.lwip_netif->hwaddr, mac, 6);
  1317. return -1;
  1318. }
  1319. int net_lwip2_set_static_ip(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1320. {
  1321. // uint8_t index = (uint32_t)user_data;
  1322. // if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1323. // if (!prvlwip.lwip_netif) return -1;
  1324. // luat_ip_addr_t *p_ip = zalloc(sizeof(luat_ip_addr_t) * 5);
  1325. // p_ip[0] = ip?(*ip):ip_addr_any_type;
  1326. // p_ip[1] = submask?(*submask):ip_addr_any_type;
  1327. // p_ip[2] = gateway?(*gateway):ip_addr_any_type;
  1328. // p_ip[3] = ipv6?(*ipv6):ip_addr_any_type;
  1329. // platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_SET_IP, p_ip, ipv6, user_data);
  1330. return 0;
  1331. }
  1332. static int32_t net_lwip2_dummy_callback(void *pData, void *pParam)
  1333. {
  1334. return 0;
  1335. }
  1336. static void net_lwip2_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  1337. {
  1338. uint8_t adapter_index = (uint32_t)user_data;
  1339. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1340. prvlwip.socket_cb = cb_fun?cb_fun:net_lwip2_dummy_callback;
  1341. prvlwip.user_data = param;
  1342. }
  1343. int net_lwip2_getsockopt2(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data) {
  1344. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1345. if (result) return result;
  1346. #if LWIP_SOCKET
  1347. return lwip_getsockopt(socket_id, level, optname, optval, optlen);
  1348. #else
  1349. return 0;
  1350. #endif
  1351. }
  1352. int net_lwip2_setsockopt2(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data) {
  1353. int result = net_lwip2_check_socket(user_data, socket_id, tag);
  1354. if (result) return result;
  1355. #if LWIP_SOCKET
  1356. return lwip_setsockopt(socket_id, level, optname, optval, optlen);
  1357. #else
  1358. return 0;
  1359. #endif
  1360. }
  1361. static const network_adapter_info prv_net_lwip2_adapter =
  1362. {
  1363. .check_ready = net_lwip2_check_ready,
  1364. .create_soceket = net_lwip2_create_socket,
  1365. .socket_connect = net_lwip2_socket_connect,
  1366. .socket_listen = net_lwip2_socket_listen,
  1367. .socket_accept = net_lwip2_socket_accept,
  1368. .socket_disconnect = net_lwip2_socket_disconnect,
  1369. .socket_close = net_lwip2_socket_close,
  1370. .socket_force_close = net_lwip2_socket_force_close,
  1371. .socket_receive = net_lwip2_socket_receive,
  1372. .socket_send = net_lwip2_socket_send,
  1373. .socket_check = net_lwip2_socket_check,
  1374. .socket_clean = net_lwip2_socket_clean,
  1375. .getsockopt = net_lwip2_getsockopt2,
  1376. .setsockopt = net_lwip2_setsockopt2,
  1377. .user_cmd = net_lwip2_user_cmd,
  1378. .dns = net_lwip2_dns,
  1379. .set_dns_server = net_lwip2_set_dns_server,
  1380. // .dns = net_lwip2_dns,
  1381. .dns_ipv6 = net_lwip2_dns_ipv6,
  1382. .set_mac = net_lwip2_set_mac,
  1383. .set_static_ip = net_lwip2_set_static_ip,
  1384. .get_local_ip_info = net_lwip2_get_local_ip_info,
  1385. .get_full_ip_info = net_lwip2_get_full_ip_info,
  1386. .socket_set_callback = net_lwip2_socket_set_callback,
  1387. .name = "lwip",
  1388. .max_socket_num = MAX_SOCK_NUM,
  1389. .no_accept = 1,
  1390. .is_posix = 1,
  1391. .check_ack = net_lwip2_check_ack
  1392. };
  1393. void net_lwip2_register_adapter(uint8_t adapter_index)
  1394. {
  1395. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) {
  1396. return; // 超范围了
  1397. }
  1398. if ( (1 << adapter_index) & register_statue) {
  1399. return; // 注册过了
  1400. }
  1401. network_register_adapter(adapter_index, &prv_net_lwip2_adapter, adapter_index);
  1402. register_statue |= (1 << adapter_index);
  1403. }
  1404. static int net_lwip2_check_ack(uint8_t adapter_index, int socket_id) {
  1405. if (prvlwip.socket[socket_id].pcb.tcp == NULL)
  1406. return 0;
  1407. if (!llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  1408. {
  1409. NET_ERR("socekt %d not all ack", socket_id);
  1410. prvlwip.socket_busy |= (1 << socket_id);
  1411. return -1;
  1412. }
  1413. if (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  1414. {
  1415. NET_ERR("socekt %d not all send", socket_id);
  1416. prvlwip.socket_busy |= (1 << socket_id);
  1417. return -1;
  1418. }
  1419. if (prvlwip.socket[socket_id].pcb.tcp->snd_buf != TCP_SND_BUF)
  1420. {
  1421. NET_ERR("socket %d send buf %ubytes, need %u",socket_id, prvlwip.socket[socket_id].pcb.tcp->snd_buf, TCP_SND_BUF);
  1422. prvlwip.socket_busy |= (1 << socket_id);
  1423. }
  1424. else
  1425. {
  1426. prvlwip.socket_busy &= ~(1 << socket_id);
  1427. }
  1428. return 0;
  1429. }
  1430. #if !defined(CHIP_EC718) && !defined(CHIP_EC618) && !defined(CHIP_EC716)
  1431. int net_lwip_check_all_ack(int socket_id)
  1432. {
  1433. return net_lwip2_check_ack(0, socket_id);
  1434. }
  1435. #endif
  1436. void net_lwip2_set_link_state(uint8_t adapter_index, uint8_t updown)
  1437. {
  1438. network_state = updown;
  1439. // LLOGD("net_lwip2_set_link_state %d %d", adapter_index, updown);
  1440. platform_send_event(NULL, EV_LWIP_NETIF_LINK_STATE, updown, 0, adapter_index);
  1441. }
  1442. struct netif * net_lwip2_get_netif(uint8_t adapter_index)
  1443. {
  1444. if (adapter_index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY)
  1445. return NULL;
  1446. return prvlwip.lwip_netif[adapter_index];
  1447. }