luat_network_lwip_tcpip_cb.c 40 KB

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