net_lwip2.c 49 KB

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