net_lwip2.c 48 KB

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