net_lwip2.c 58 KB

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