net_lwip2.c 48 KB

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