luat_network_lwip_tcpip_cb.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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].state && !prvlwip.socket[socket_id].remote_close)
  332. {
  333. NET_DBG("adapter %d socket %d not closing, but error %d", adapter_index, socket_id, err);
  334. prvlwip.socket[socket_id].pcb.ip = NULL;
  335. net_lwip_tcp_error(adapter_index, socket_id);
  336. }
  337. return 0;
  338. }
  339. static err_t net_lwip_tcp_fast_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  340. {
  341. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  342. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  343. if (err || !newpcb)
  344. {
  345. net_lwip_tcp_error(adapter_index, socket_id);
  346. return 0;
  347. }
  348. prvlwip.socket[socket_id].pcb.tcp = newpcb;
  349. // prvlwip.socket[socket_id].pcb.tcp->sockid = socket_id;
  350. prvlwip.socket[socket_id].rx_wait_size = 0;
  351. prvlwip.socket[socket_id].tx_wait_size = 0;
  352. prvlwip.socket[socket_id].pcb.tcp->callback_arg = arg;
  353. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  354. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  355. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  356. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  357. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  358. return ERR_OK;
  359. }
  360. static err_t net_lwip_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  361. {
  362. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  363. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  364. return ERR_OK;
  365. }
  366. static err_t net_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  367. const ip_addr_t *addr, u16_t port)
  368. {
  369. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  370. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  371. uint16_t len;
  372. if (p)
  373. {
  374. len = p->tot_len;
  375. if (net_lwip_rx_data(socket_id, p, addr, port))
  376. {
  377. NET_DBG("no memory!");
  378. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  379. }
  380. else
  381. {
  382. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  383. }
  384. pbuf_free(p);
  385. }
  386. return ERR_OK;
  387. }
  388. static int32_t net_lwip_dns_check_result(void *data, void *param)
  389. {
  390. luat_dns_require_t *require = (luat_dns_require_t *)data;
  391. if (require->result != 0)
  392. {
  393. free(require->uri.Data);
  394. require->uri.Data = NULL;
  395. if (require->result > 0)
  396. {
  397. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  398. int i;
  399. for(i = 0; i < require->result; i++)
  400. {
  401. ip_result[i] = require->ip_result[i];
  402. }
  403. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  404. }
  405. else
  406. {
  407. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, 0, 0, require->param);
  408. }
  409. return LIST_DEL;
  410. }
  411. else
  412. {
  413. return LIST_PASS;
  414. }
  415. }
  416. static err_t net_lwip_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  417. const ip_addr_t *addr, u16_t port)
  418. {
  419. Buffer_Struct msg_buf;
  420. Buffer_Struct tx_msg_buf = {0,0,0};
  421. struct pbuf *out_p;
  422. ip_addr_t *t_ip;
  423. int i;
  424. if (p)
  425. {
  426. OS_InitBuffer(&msg_buf, p->tot_len);
  427. pbuf_copy_partial(p, msg_buf.Data, p->tot_len, 0);
  428. pbuf_free(p);
  429. dns_run(&prvlwip.dns_client, &msg_buf, NULL, &i);
  430. OS_DeInitBuffer(&msg_buf);
  431. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  432. {
  433. dns_run(&prvlwip.dns_client, NULL, &tx_msg_buf, &i);
  434. if (tx_msg_buf.Pos)
  435. {
  436. out_p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_ROM);
  437. if (out_p)
  438. {
  439. out_p->payload = tx_msg_buf.Data;
  440. err_t err = udp_sendto(prvlwip.dns_udp, out_p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  441. pbuf_free(out_p);
  442. }
  443. OS_DeInitBuffer(&tx_msg_buf);
  444. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  445. }
  446. }
  447. }
  448. if (!prvlwip.dns_client.is_run)
  449. {
  450. platform_stop_timer(prvlwip.dns_timer);
  451. }
  452. return ERR_OK;
  453. }
  454. static void net_lwip_dns_tx_next(Buffer_Struct *tx_msg_buf)
  455. {
  456. int i;
  457. err_t err;
  458. struct pbuf *p;
  459. dns_run(&prvlwip.dns_client, NULL, tx_msg_buf, &i);
  460. if (tx_msg_buf->Pos || prvlwip.dns_client.new_result)
  461. {
  462. p = pbuf_alloc(PBUF_RAW, tx_msg_buf->Pos, PBUF_ROM);
  463. if (p)
  464. {
  465. p->payload = tx_msg_buf->Data;
  466. err = udp_sendto(prvlwip.dns_udp, p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  467. pbuf_free(p);
  468. }
  469. OS_DeInitBuffer(tx_msg_buf);
  470. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  471. prvlwip.dns_client.new_result = 0;
  472. }
  473. }
  474. void net_lwip_init(void)
  475. {
  476. uint8_t i;
  477. for(i = 0; i < MAX_SOCK_NUM; i++)
  478. {
  479. INIT_LLIST_HEAD(&prvlwip.socket[i].wait_ack_head);
  480. INIT_LLIST_HEAD(&prvlwip.socket[i].tx_head);
  481. INIT_LLIST_HEAD(&prvlwip.socket[i].rx_head);
  482. prvlwip.socket[i].mutex = platform_create_mutex();
  483. }
  484. prvlwip.dns_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
  485. }
  486. static void net_lwip_close_tcp(int socket_id)
  487. {
  488. prvlwip.socket[socket_id].pcb.tcp->sent = NULL;
  489. prvlwip.socket[socket_id].pcb.tcp->errf = NULL;
  490. prvlwip.socket[socket_id].pcb.tcp->recv = tcp_recv_null;
  491. prvlwip.socket[socket_id].pcb.tcp->callback_arg = 0;
  492. prvlwip.socket[socket_id].pcb.tcp->pollinterval = 2;
  493. if (tcp_close(prvlwip.socket[socket_id].pcb.tcp))
  494. {
  495. tcp_abort(prvlwip.socket[socket_id].pcb.tcp);
  496. }
  497. }
  498. static void net_lwip_task(void *param)
  499. {
  500. luat_network_cb_param_t cb_param;
  501. OS_EVENT event = *((OS_EVENT *)param);
  502. free(param);
  503. Buffer_Struct tx_msg_buf = {0,0,0};
  504. HANDLE cur_task = luat_get_current_task();
  505. struct netif *netif;
  506. socket_data_t *p;
  507. ip_addr_t *p_ip, *local_ip;
  508. struct pbuf *out_p;
  509. int error, i;
  510. PV_Union uPV;
  511. // uint8_t active_flag;
  512. uint8_t socket_id;
  513. uint8_t adapter_index;
  514. socket_id = event.Param1;
  515. adapter_index = event.Param3;
  516. switch(event.ID)
  517. {
  518. case EV_LWIP_SOCKET_TX:
  519. SOCKET_LOCK(socket_id);
  520. if (prvlwip.socket[socket_id].in_use && prvlwip.socket[socket_id].pcb.ip)
  521. {
  522. // if (!prvlwip.socket[socket_id].pcb.tcp->unsent && !prvlwip.socket[socket_id].pcb.tcp->unacked)
  523. // {
  524. // active_flag = 0;
  525. // }
  526. // else
  527. // {
  528. // active_flag = 1;
  529. // }
  530. if (prvlwip.socket[socket_id].is_tcp)
  531. {
  532. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  533. {
  534. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  535. if (p->len <= tcp_sndbuf(prvlwip.socket[socket_id].pcb.tcp))
  536. {
  537. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  538. {
  539. llist_del(&p->node);
  540. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  541. }
  542. else
  543. {
  544. // NET_DBG("tcp buf is full, wait ack and send again");
  545. break;
  546. }
  547. }
  548. else
  549. {
  550. // NET_DBG("tcp buf is full, wait ack and send again");
  551. break;
  552. }
  553. }
  554. SOCKET_UNLOCK(socket_id);
  555. tcp_output(prvlwip.socket[socket_id].pcb.tcp);
  556. prvlwip.socket_busy |= (1 << socket_id);
  557. }
  558. else
  559. {
  560. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  561. if (p)
  562. {
  563. llist_del(&p->node);
  564. }
  565. SOCKET_UNLOCK(socket_id);
  566. if (p)
  567. {
  568. uint32_t len = p->len;
  569. out_p = pbuf_alloc(PBUF_RAW, p->len, PBUF_ROM);
  570. if (out_p)
  571. {
  572. out_p->payload = p->data;
  573. error = udp_sendto(prvlwip.socket[socket_id].pcb.udp, out_p, &p->ip, p->port);
  574. pbuf_free(out_p);
  575. }
  576. else
  577. {
  578. NET_DBG("mem err send fail");
  579. }
  580. free(p->data);
  581. free(p);
  582. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  583. }
  584. }
  585. }
  586. else
  587. {
  588. NET_DBG("adapter %d socket %d no in use! %x", adapter_index, socket_id, prvlwip.socket[socket_id].pcb.ip);
  589. SOCKET_UNLOCK(socket_id);
  590. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  591. }
  592. break;
  593. case EV_LWIP_COMMON_TIMER:
  594. #ifdef LUAT_USE_DNS
  595. net_lwip_dns_tx_next(&tx_msg_buf);
  596. #endif
  597. if (!prvlwip.dns_client.is_run)
  598. {
  599. platform_stop_timer(prvlwip.dns_timer);
  600. }
  601. break;
  602. case EV_LWIP_SOCKET_RX_DONE:
  603. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip || !prvlwip.socket[socket_id].is_tcp)
  604. {
  605. 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);
  606. break;
  607. }
  608. // NET_DBG("socket %d rx ack %dbytes", socket_id, event.Param2);
  609. tcp_recved(prvlwip.socket[socket_id].pcb.tcp, event.Param2);
  610. break;
  611. case EV_LWIP_SOCKET_CREATE:
  612. net_lwip_create_socket_now(adapter_index, socket_id);
  613. break;
  614. case EV_LWIP_SOCKET_CONNECT:
  615. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  616. {
  617. 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);
  618. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  619. break;
  620. }
  621. p_ip = (ip_addr_t *)event.Param2;
  622. //local_ip = NULL;
  623. // if (p_ip->type == IPADDR_TYPE_V4)
  624. // {
  625. // local_ip = &prvlwip.lwip_netif->ip_addr;
  626. // }
  627. // else
  628. // {
  629. // local_ip = net_lwip_get_ip6();
  630. // }
  631. // if (!local_ip)
  632. // {
  633. // NET_DBG("netif no ip !!!!!!");
  634. // net_lwip_tcp_error(adapter_index, socket_id);
  635. // break;
  636. // }
  637. if (prvlwip.socket[socket_id].is_tcp)
  638. {
  639. //tcp_bind(prvlwip.socket[socket_id].pcb.tcp, local_ip, prvlwip.socket[socket_id].local_port);
  640. error = tcp_connect(prvlwip.socket[socket_id].pcb.tcp, p_ip, prvlwip.socket[socket_id].remote_port, net_lwip_tcp_connected_cb);
  641. if (error)
  642. {
  643. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  644. net_lwip_tcp_error(adapter_index, socket_id);
  645. }
  646. else
  647. {
  648. prvlwip.socket_connect |= (1 << socket_id);
  649. }
  650. }
  651. else
  652. {
  653. udp_bind(prvlwip.socket[socket_id].pcb.udp, NULL, prvlwip.socket[socket_id].local_port);
  654. error = udp_connect(prvlwip.socket[socket_id].pcb.udp, p_ip, prvlwip.socket[socket_id].remote_port);
  655. if (error)
  656. {
  657. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  658. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  659. }
  660. else
  661. {
  662. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  663. }
  664. }
  665. break;
  666. case EV_LWIP_SOCKET_DNS:
  667. case EV_LWIP_SOCKET_DNS_IPV6:
  668. if (!prvlwip.dns_client.is_run)
  669. {
  670. platform_start_timer(prvlwip.dns_timer, 1000, 1);
  671. }
  672. dns_require_ipv6(&prvlwip.dns_client, event.Param1, event.Param2, event.Param3, (event.ID - EV_LWIP_SOCKET_DNS));
  673. net_lwip_dns_tx_next(&tx_msg_buf);
  674. break;
  675. case EV_LWIP_SOCKET_LISTEN:
  676. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  677. {
  678. 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);
  679. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  680. break;
  681. }
  682. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, NULL, prvlwip.socket[socket_id].local_port);
  683. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->local_ip, IPADDR_TYPE_ANY);
  684. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
  685. // prvlwip.socket[socket_id].pcb.tcp->sockid = -1;
  686. prvlwip.socket[socket_id].listen_tcp = tcp_listen_with_backlog(prvlwip.socket[socket_id].pcb.tcp, 1);
  687. if (!prvlwip.socket[socket_id].listen_tcp) {
  688. NET_DBG("socket %d listen failed");
  689. net_lwip_tcp_error(adapter_index, socket_id);
  690. } else {
  691. PV_Union uPV;
  692. uPV.u16[0] = socket_id;
  693. uPV.u16[1] = adapter_index;
  694. prvlwip.socket[socket_id].listen_tcp->callback_arg = uPV.u32;
  695. prvlwip.socket[socket_id].listen_tcp->accept = net_lwip_tcp_fast_accept_cb;
  696. prvlwip.socket[socket_id].pcb.tcp = NULL;
  697. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_LISTEN, socket_id, 0, 0);
  698. }
  699. break;
  700. // case EV_LWIP_SOCKET_ACCPET:
  701. //
  702. // break;
  703. case EV_LWIP_SOCKET_CLOSE:
  704. if (!prvlwip.socket[socket_id].in_use)
  705. {
  706. NET_DBG("socket %d no in use!,%x", socket_id);
  707. break;
  708. }
  709. if (prvlwip.socket[socket_id].listen_tcp)
  710. {
  711. tcp_close(prvlwip.socket[socket_id].listen_tcp);
  712. prvlwip.socket[socket_id].listen_tcp = NULL;
  713. if (prvlwip.socket[socket_id].pcb.tcp)
  714. {
  715. net_lwip_close_tcp(socket_id);
  716. }
  717. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  718. break;
  719. }
  720. if (prvlwip.socket[socket_id].pcb.ip)
  721. {
  722. if (prvlwip.socket[socket_id].is_tcp)
  723. {
  724. net_lwip_close_tcp(socket_id);
  725. }
  726. else
  727. {
  728. udp_remove(prvlwip.socket[socket_id].pcb.udp);
  729. }
  730. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  731. break;
  732. }
  733. if (prvlwip.socket[socket_id].remote_close)
  734. {
  735. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  736. break;
  737. }
  738. break;
  739. case EV_LWIP_NETIF_LINK_STATE:
  740. net_lwip_check_network_ready(event.Param3);
  741. break;
  742. default:
  743. NET_DBG("unknow event %x,%x", event.ID, event.Param1);
  744. break;
  745. }
  746. }
  747. static void platform_send_event(void *p, uint32_t id, uint32_t param1, uint32_t param2, uint32_t param3)
  748. {
  749. OS_EVENT *event = malloc(sizeof(OS_EVENT));
  750. event->ID = id;
  751. event->Param1 = param1;
  752. event->Param2 = param2;
  753. event->Param3 = param3;
  754. tcpip_callback_with_block(net_lwip_task, event, 1);
  755. }
  756. static void net_lwip_check_network_ready(uint8_t adapter_index)
  757. {
  758. int i;
  759. uint8_t ip_string[64];
  760. uint8_t active_flag = network_state;
  761. if (prvlwip.netif_network_ready != active_flag)
  762. {
  763. prvlwip.netif_network_ready = active_flag;
  764. if (!active_flag)
  765. {
  766. dns_clear(&prvlwip.dns_client);
  767. prvlwip.dns_client.is_run = 0;
  768. // NET_DBG("network not ready");
  769. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 0, adapter_index);
  770. }
  771. else
  772. {
  773. NET_DBG("network ready");
  774. // TODO 设置为本地的DNS配置
  775. PV_Union puTmp = {.u8 = {223,5,5,5}};
  776. #if LWIP_IPV6
  777. prvlwip.dns_client.dns_server[0].u_addr.ip4.addr = puTmp.u32;
  778. prvlwip.dns_client.dns_server[0].type = IPADDR_TYPE_V4;
  779. #else
  780. prvlwip.dns_client.dns_server[0].addr = puTmp.u32;
  781. #endif
  782. prvlwip.dns_client.is_static_dns[0] = 1;
  783. PV_Union puTmp2 = {.u8 = {114,114,114,114}};
  784. #if LWIP_IPV6
  785. prvlwip.dns_client.dns_server[1].u_addr.ip4.addr = puTmp2.u32;
  786. prvlwip.dns_client.dns_server[1].type = IPADDR_TYPE_V4;
  787. #else
  788. prvlwip.dns_client.dns_server[1].addr = puTmp2.u32;
  789. #endif
  790. prvlwip.dns_client.is_static_dns[1] = 1;
  791. for(i = 0; i < MAX_DNS_SERVER; i++)
  792. {
  793. #if LWIP_IPV6
  794. if (prvlwip.dns_client.dns_server[i].type != 0xff)
  795. #else
  796. if (prvlwip.dns_client.dns_server[i].addr != 0)
  797. #endif
  798. {
  799. NET_DBG("DNS%d:%s",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)));
  800. }
  801. }
  802. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 1, adapter_index);
  803. }
  804. }
  805. }
  806. static int net_lwip_check_socket(void *user_data, int socket_id, uint64_t tag)
  807. {
  808. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  809. if (socket_id >= MAX_SOCK_NUM) return -1;
  810. if (prvlwip.socket[socket_id].tag != tag) return -1;
  811. if (!prvlwip.socket[socket_id].in_use || prvlwip.socket[socket_id].state) return -1;
  812. return 0;
  813. }
  814. static int net_lwip_socket_check(int socket_id, uint64_t tag, void *user_data)
  815. {
  816. return net_lwip_check_socket(user_data, socket_id, tag);
  817. }
  818. static uint8_t net_lwip_check_ready(void *user_data)
  819. {
  820. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  821. return (prvlwip.netif_network_ready);
  822. }
  823. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id)
  824. {
  825. PV_Union uPV;
  826. uPV.u16[0] = socket_id;
  827. uPV.u16[1] = adapter_index;
  828. if (prvlwip.socket[socket_id].is_tcp)
  829. {
  830. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  831. if (!prvlwip.socket[socket_id].pcb.tcp)
  832. {
  833. NET_DBG("try to abort fin wait 1 tcp");
  834. struct tcp_pcb *pcb, *dpcb;
  835. uint32_t low_time = (uint32_t)(luat_mcu_tick64_ms() / 1000);
  836. dpcb = NULL;
  837. for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  838. {
  839. if (FIN_WAIT_1 == pcb->state)
  840. {
  841. if (((uint32_t)pcb->callback_arg) < low_time)
  842. {
  843. dpcb = pcb;
  844. low_time = (uint32_t)pcb->callback_arg;
  845. }
  846. }
  847. }
  848. if (dpcb)
  849. {
  850. tcp_abort(dpcb);
  851. }
  852. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  853. }
  854. if (prvlwip.socket[socket_id].pcb.tcp)
  855. {
  856. // prvlwip.socket[socket_id].pcb.tcp->sockid = socket_id;
  857. prvlwip.socket[socket_id].pcb.tcp->local_ip = prvlwip.lwip_netif->ip_addr;
  858. prvlwip.socket[socket_id].rx_wait_size = 0;
  859. prvlwip.socket[socket_id].tx_wait_size = 0;
  860. prvlwip.socket[socket_id].pcb.tcp->callback_arg = uPV.p;
  861. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  862. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  863. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  864. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  865. // tcp_set_flags(prvlwip.socket[socket_id].pcb.tcp, TCP_NODELAY);
  866. }
  867. else
  868. {
  869. NET_DBG("tcp pcb full!");
  870. net_lwip_tcp_error(adapter_index, socket_id);
  871. }
  872. }
  873. else
  874. {
  875. prvlwip.socket[socket_id].pcb.udp = udp_new();
  876. if (prvlwip.socket[socket_id].pcb.udp)
  877. {
  878. prvlwip.socket[socket_id].pcb.udp->recv_arg = uPV.p;
  879. prvlwip.socket[socket_id].pcb.udp->recv = net_lwip_udp_recv_cb;
  880. prvlwip.socket[socket_id].pcb.udp->so_options |= SOF_BROADCAST|SOF_REUSEADDR;
  881. }
  882. else
  883. {
  884. NET_DBG("udp pcb full!");
  885. net_lwip_tcp_error(adapter_index, socket_id);
  886. }
  887. }
  888. }
  889. static int net_lwip_create_socket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  890. {
  891. uint8_t index = (uint32_t)user_data;
  892. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  893. int i, socket_id;
  894. socket_id = -1;
  895. OS_LOCK;
  896. if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
  897. {
  898. socket_id = prvlwip.next_socket_index;
  899. prvlwip.next_socket_index++;
  900. }
  901. else
  902. {
  903. for (i = 0; i < MAX_SOCK_NUM; i++)
  904. {
  905. if (!prvlwip.socket[i].in_use)
  906. {
  907. socket_id = i;
  908. prvlwip.next_socket_index = i + 1;
  909. break;
  910. }
  911. }
  912. }
  913. if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
  914. {
  915. prvlwip.next_socket_index = 0;
  916. }
  917. if (socket_id >= 0)
  918. {
  919. LWIP_ASSERT("socket must free before create", !prvlwip.socket[socket_id].pcb.ip);
  920. prvlwip.socket_busy &= ~(1 << socket_id);
  921. prvlwip.socket_connect &= ~(1 << socket_id);
  922. prvlwip.socket_tag++;
  923. *tag = prvlwip.socket_tag;
  924. prvlwip.socket[socket_id].in_use = 1;
  925. prvlwip.socket[socket_id].tag = *tag;
  926. prvlwip.socket[socket_id].param = param;
  927. prvlwip.socket[socket_id].is_tcp = is_tcp;
  928. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  929. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  930. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  931. OS_UNLOCK;
  932. // if (platform_get_current_task() == prvlwip.task_handle)
  933. {
  934. net_lwip_create_socket_now(index, socket_id);
  935. return socket_id;
  936. }
  937. // platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CREATE, socket_id, 0, user_data);
  938. }
  939. else
  940. {
  941. OS_UNLOCK;
  942. }
  943. return socket_id;
  944. }
  945. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  946. 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)
  947. {
  948. int result = net_lwip_check_socket(user_data, socket_id, tag);
  949. if (result) return result;
  950. prvlwip.socket[socket_id].local_port = local_port;
  951. prvlwip.socket[socket_id].remote_port = remote_port;
  952. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, user_data);
  953. return 0;
  954. }
  955. //作为server绑定一个port,开始监听
  956. static int net_lwip_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  957. {
  958. int result = net_lwip_check_socket(user_data, socket_id, tag);
  959. if (result) return result;
  960. prvlwip.socket[socket_id].local_port = local_port;
  961. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_LISTEN, socket_id, local_port, user_data);
  962. return 0;
  963. }
  964. //作为server接受一个client
  965. 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)
  966. {
  967. int result = net_lwip_check_socket(user_data, socket_id, tag);
  968. if (result) return result;
  969. *remote_ip = prvlwip.socket[socket_id].pcb.tcp->remote_ip;
  970. *remote_port = prvlwip.socket[socket_id].pcb.tcp->remote_port;
  971. return 0;
  972. }
  973. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  974. static int net_lwip_socket_disconnect(int socket_id, uint64_t tag, void *user_data)
  975. {
  976. int result = net_lwip_check_socket(user_data, socket_id, tag);
  977. if (result) return result;
  978. prvlwip.socket[socket_id].state = 1;
  979. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 1, user_data);
  980. return 0;
  981. }
  982. static int net_lwip_socket_force_close(int socket_id, void *user_data)
  983. {
  984. if (prvlwip.socket[socket_id].in_use && !prvlwip.socket[socket_id].state)
  985. {
  986. prvlwip.socket[socket_id].state = 1;
  987. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 0, user_data);
  988. }
  989. return 0;
  990. }
  991. static int net_lwip_socket_close(int socket_id, uint64_t tag, void *user_data)
  992. {
  993. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  994. if (socket_id >= MAX_SOCK_NUM) return -1;
  995. if (prvlwip.socket[socket_id].tag != tag)
  996. {
  997. NET_DBG("socket %d used by other!", socket_id);
  998. return -1;
  999. }
  1000. if (!prvlwip.socket[socket_id].in_use) return 0;
  1001. net_lwip_socket_force_close(socket_id, user_data);
  1002. return 0;
  1003. }
  1004. 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)
  1005. {
  1006. uint32_t dummy_len;
  1007. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1008. memcpy(buf, p->data + p->read_pos, dummy_len);
  1009. p->read_pos += dummy_len;
  1010. if (p->read_pos >= p->len)
  1011. {
  1012. if (prvlwip.socket[socket_id].is_tcp)
  1013. {
  1014. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_RX_DONE, socket_id, p->len, 0);
  1015. }
  1016. llist_del(&p->node);
  1017. free(p->data);
  1018. free(p);
  1019. }
  1020. *read_len += dummy_len;
  1021. return dummy_len;
  1022. }
  1023. 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)
  1024. {
  1025. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1026. if (result) return result;
  1027. uint32_t read_len = 0;
  1028. if (buf)
  1029. {
  1030. SOCKET_LOCK(socket_id);
  1031. socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1032. if (prvlwip.socket[socket_id].is_tcp)
  1033. {
  1034. while((read_len < len) && p)
  1035. {
  1036. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1037. p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1038. }
  1039. }
  1040. else
  1041. {
  1042. if (p)
  1043. {
  1044. if (remote_ip)
  1045. {
  1046. *remote_ip = p->ip;
  1047. }
  1048. if (remote_port)
  1049. {
  1050. *remote_port = p->port;
  1051. }
  1052. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1053. }
  1054. }
  1055. if (llist_empty(&prvlwip.socket[socket_id].rx_head))
  1056. {
  1057. prvlwip.socket[socket_id].rx_wait_size = 0;
  1058. }
  1059. SOCKET_UNLOCK(socket_id);
  1060. }
  1061. else
  1062. {
  1063. read_len = prvlwip.socket[socket_id].rx_wait_size;
  1064. }
  1065. return read_len;
  1066. }
  1067. 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)
  1068. {
  1069. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1070. if (result) return result;
  1071. SOCKET_LOCK(socket_id);
  1072. uint32_t save_len = 0;
  1073. uint32_t dummy_len = 0;
  1074. socket_data_t *p;
  1075. if (prvlwip.socket[socket_id].is_tcp)
  1076. {
  1077. while(save_len < len)
  1078. {
  1079. dummy_len = ((len - save_len) > SOCKET_BUF_LEN)?SOCKET_BUF_LEN:(len - save_len);
  1080. p = net_lwip_create_data_node(socket_id, &buf[save_len], dummy_len, remote_ip, remote_port);
  1081. if (p)
  1082. {
  1083. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1084. }
  1085. else
  1086. {
  1087. SOCKET_UNLOCK(socket_id);
  1088. return -1;
  1089. }
  1090. save_len += dummy_len;
  1091. }
  1092. }
  1093. else
  1094. {
  1095. p = net_lwip_create_data_node(socket_id, buf, len, remote_ip, remote_port);
  1096. if (p)
  1097. {
  1098. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1099. }
  1100. else
  1101. {
  1102. SOCKET_UNLOCK(socket_id);
  1103. return -1;
  1104. }
  1105. }
  1106. SOCKET_UNLOCK(socket_id);
  1107. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_TX, socket_id, 0, user_data);
  1108. result = len;
  1109. return result;
  1110. }
  1111. void net_lwip_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1112. {
  1113. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1114. int socket_list[MAX_SOCK_NUM];
  1115. memset(socket_list, 0, sizeof(socket_list));
  1116. uint32_t i;
  1117. for(i = 0; i < num; i++)
  1118. {
  1119. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1120. {
  1121. socket_list[vaild_socket_list[i]] = 1;
  1122. }
  1123. NET_DBG("%d,%d",i,vaild_socket_list[i]);
  1124. }
  1125. for(i = 0; i < MAX_SOCK_NUM; i++)
  1126. {
  1127. NET_DBG("%d,%d",i,socket_list[i]);
  1128. if ( !socket_list[i] )
  1129. {
  1130. net_lwip_socket_force_close(i, user_data);
  1131. }
  1132. }
  1133. }
  1134. 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)
  1135. {
  1136. uint8_t index = (uint32_t)user_data;
  1137. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1138. if (!prvlwip.lwip_netif) return -1;
  1139. *ip = prvlwip.lwip_netif->ip_addr;
  1140. *submask = prvlwip.lwip_netif->netmask;
  1141. *gateway = prvlwip.lwip_netif->gw;
  1142. return 0;
  1143. }
  1144. static int net_lwip_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1145. {
  1146. return 0;
  1147. }
  1148. static int net_lwip_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1149. {
  1150. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1151. char *prv_domain_name = (char *)zalloc(len + 1);
  1152. memcpy(prv_domain_name, domain_name, len);
  1153. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1154. return 0;
  1155. }
  1156. static int net_lwip_dns_ipv6(const char *domain_name, uint32_t len, void *param, void *user_data)
  1157. {
  1158. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1159. char *prv_domain_name = (char *)zalloc(len + 1);
  1160. memcpy(prv_domain_name, domain_name, len);
  1161. // 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);
  1162. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1163. return 0;
  1164. }
  1165. static int net_lwip_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1166. {
  1167. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1168. if (server_index >= MAX_DNS_SERVER) return -1;
  1169. prvlwip.dns_client.dns_server[server_index] = *ip;
  1170. prvlwip.dns_client.is_static_dns[server_index] = 1;
  1171. return 0;
  1172. }
  1173. static int net_lwip_set_mac(uint8_t *mac, void *user_data)
  1174. {
  1175. // uint8_t index = (uint32_t)user_data;
  1176. // if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1177. // if (!prvlwip.lwip_netif) return -1;
  1178. // memcpy(prvlwip.lwip_netif->hwaddr, mac, 6);
  1179. return -1;
  1180. }
  1181. 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)
  1182. {
  1183. // uint8_t index = (uint32_t)user_data;
  1184. // if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1185. // if (!prvlwip.lwip_netif) return -1;
  1186. // luat_ip_addr_t *p_ip = zalloc(sizeof(luat_ip_addr_t) * 5);
  1187. // p_ip[0] = ip?(*ip):ip_addr_any_type;
  1188. // p_ip[1] = submask?(*submask):ip_addr_any_type;
  1189. // p_ip[2] = gateway?(*gateway):ip_addr_any_type;
  1190. // p_ip[3] = ipv6?(*ipv6):ip_addr_any_type;
  1191. // platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_SET_IP, p_ip, ipv6, user_data);
  1192. return 0;
  1193. }
  1194. static int32_t net_lwip_dummy_callback(void *pData, void *pParam)
  1195. {
  1196. return 0;
  1197. }
  1198. static void net_lwip_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  1199. {
  1200. uint8_t index = (uint32_t)user_data;
  1201. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1202. prvlwip.socket_cb = cb_fun?cb_fun:net_lwip_dummy_callback;
  1203. prvlwip.user_data = param;
  1204. }
  1205. int net_lwip_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data) {
  1206. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1207. if (result) return result;
  1208. return lwip_getsockopt(socket_id, level, optname, optval, optlen);
  1209. }
  1210. int net_lwip_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data) {
  1211. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1212. if (result) return result;
  1213. return lwip_setsockopt(socket_id, level, optname, optval, optlen);
  1214. }
  1215. static network_adapter_info prv_net_lwip_adapter =
  1216. {
  1217. .check_ready = net_lwip_check_ready,
  1218. .create_soceket = net_lwip_create_socket,
  1219. .socket_connect = net_lwip_socket_connect,
  1220. .socket_listen = net_lwip_socket_listen,
  1221. .socket_accept = net_lwip_socket_accept,
  1222. .socket_disconnect = net_lwip_socket_disconnect,
  1223. .socket_close = net_lwip_socket_close,
  1224. .socket_force_close = net_lwip_socket_force_close,
  1225. .socket_receive = net_lwip_socket_receive,
  1226. .socket_send = net_lwip_socket_send,
  1227. .socket_check = net_lwip_socket_check,
  1228. .socket_clean = net_lwip_socket_clean,
  1229. .getsockopt = net_lwip_getsockopt,
  1230. .setsockopt = net_lwip_setsockopt,
  1231. .user_cmd = net_lwip_user_cmd,
  1232. .dns = net_lwip_dns,
  1233. .set_dns_server = net_lwip_set_dns_server,
  1234. .dns = net_lwip_dns,
  1235. .dns_ipv6 = net_lwip_dns_ipv6,
  1236. .set_mac = net_lwip_set_mac,
  1237. .set_static_ip = net_lwip_set_static_ip,
  1238. .get_local_ip_info = net_lwip_get_local_ip_info,
  1239. .socket_set_callback = net_lwip_socket_set_callback,
  1240. .name = "lwip",
  1241. .max_socket_num = MAX_SOCK_NUM,
  1242. .no_accept = 1,
  1243. .is_posix = 1,
  1244. };
  1245. void net_lwip_register_adapter(uint8_t adapter_index)
  1246. {
  1247. network_register_adapter(adapter_index, &prv_net_lwip_adapter, adapter_index);
  1248. }
  1249. int net_lwip_check_all_ack(int socket_id)
  1250. {
  1251. if (!llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  1252. {
  1253. NET_ERR("socekt %d not all ack", socket_id);
  1254. prvlwip.socket_busy |= (1 << socket_id);
  1255. return -1;
  1256. }
  1257. if (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  1258. {
  1259. NET_ERR("socekt %d not all send", socket_id);
  1260. prvlwip.socket_busy |= (1 << socket_id);
  1261. return -1;
  1262. }
  1263. if (prvlwip.socket[socket_id].pcb.tcp->snd_buf != TCP_SND_BUF)
  1264. {
  1265. NET_ERR("socket %d send buf %ubytes, need %u",socket_id, prvlwip.socket[socket_id].pcb.tcp->snd_buf, TCP_SND_BUF);
  1266. prvlwip.socket_busy |= (1 << socket_id);
  1267. }
  1268. else
  1269. {
  1270. prvlwip.socket_busy &= ~(1 << socket_id);
  1271. }
  1272. return 0;
  1273. }
  1274. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t updown)
  1275. {
  1276. network_state = updown;
  1277. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_LINK_STATE, updown, 0, adapter_index);
  1278. }
  1279. struct netif * net_lwip_get_netif(uint8_t adapter_index)
  1280. {
  1281. return prvlwip.lwip_netif;
  1282. }
  1283. void soc_lwip_init_hook(void)
  1284. {
  1285. // prvlwip.task_handle = luat_get_current_task();
  1286. prvlwip.task_handle = NULL;
  1287. prvlwip.dns_udp = udp_new();
  1288. prvlwip.dns_udp->recv = net_lwip_dns_recv_cb;
  1289. // udp_bind(prvlwip.dns_udp, NULL, 55);
  1290. dns_init_client(&prvlwip.dns_client);
  1291. }
  1292. void net_lwip_set_netif(struct netif *netif)
  1293. {
  1294. prvlwip.lwip_netif = netif;
  1295. if (!prvlwip.lwip_netif)
  1296. {
  1297. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_LINK_STATE, 0, 0, 1);
  1298. }
  1299. else
  1300. {
  1301. //prvlwip.dns_udp->netif_idx = netif_get_index(netif);
  1302. prvlwip.dns_adapter_index = NW_ADAPTER_INDEX_LWIP_WIFI_STA;
  1303. }
  1304. return;
  1305. }
  1306. #endif