luat_network_lwip_tcpip_cb.c 41 KB

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