luat_network_lwip_tcpip_cb.c 40 KB

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