net_lwip2.c 48 KB

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