luat_lwip_idf5.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310
  1. #include "platform_def.h"
  2. #include "luat_base.h"
  3. #include "luat_malloc.h"
  4. #include "luat_mcu.h"
  5. #include "luat_rtos.h"
  6. #include "dns_def.h"
  7. #include "luat_network_adapter.h"
  8. #include "net_lwip.h"
  9. #include "luat_rtos.h"
  10. #include "luat_rtos_legacy.h"
  11. #include "platform_def.h"
  12. // #include "luat_mobile.h"
  13. #include "luat_mcu.h"
  14. #include "luat_rtos.h"
  15. #include "dns_def.h"
  16. // #include "common_api.h"
  17. #include "luat_network_adapter.h"
  18. #include "lwip/tcpip.h"
  19. #include "lwip/udp.h"
  20. #include "luat_timer.h"
  21. #include "luat_malloc.h"
  22. #include "luat_crypto.h"
  23. #define LUAT_LOG_TAG "lwip"
  24. #include "luat_log.h"
  25. #define LWIP_NUM_SOCKETS 8
  26. #define SOCKET_BUF_LEN 1500
  27. /* Socket protocol types (TCP/UDP/RAW) */
  28. #define SOCK_STREAM 1
  29. #define SOCK_DGRAM 2
  30. #define SOCK_RAW 3
  31. /*
  32. * Option flags per-socket. These must match the SOF_ flags in ip.h (checked in init.c)
  33. */
  34. #define SO_REUSEADDR 0x0004 /* Allow local address reuse */
  35. #define SO_KEEPALIVE 0x0008 /* keep connections alive */
  36. #define SO_BROADCAST 0x0020 /* permit to send and to receive broadcast messages (see IP_SOF_BROADCAST option) */
  37. /*
  38. * Additional options, not kept in so_options.
  39. */
  40. #define SO_DEBUG 0x0001 /* Unimplemented: turn on debugging info recording */
  41. #define SO_ACCEPTCONN 0x0002 /* socket has had listen() */
  42. #define SO_DONTROUTE 0x0010 /* Unimplemented: just use interface addresses */
  43. #define SO_USELOOPBACK 0x0040 /* Unimplemented: bypass hardware when possible */
  44. #define SO_LINGER 0x0080 /* linger on close if data present */
  45. #define SO_DONTLINGER ((int)(~SO_LINGER))
  46. #define SO_OOBINLINE 0x0100 /* Unimplemented: leave received OOB data in line */
  47. #define SO_REUSEPORT 0x0200 /* Unimplemented: allow local address & port reuse */
  48. #define SO_SNDBUF 0x1001 /* Unimplemented: send buffer size */
  49. #define SO_RCVBUF 0x1002 /* receive buffer size */
  50. #define SO_SNDLOWAT 0x1003 /* Unimplemented: send low-water mark */
  51. #define SO_RCVLOWAT 0x1004 /* Unimplemented: receive low-water mark */
  52. #define SO_SNDTIMEO 0x1005 /* send timeout */
  53. #define SO_RCVTIMEO 0x1006 /* receive timeout */
  54. #define SO_ERROR 0x1007 /* get error status and clear */
  55. #define SO_TYPE 0x1008 /* get socket type */
  56. #define SO_CONTIMEO 0x1009 /* Unimplemented: connect timeout */
  57. #define SO_NO_CHECK 0x100a /* don't create UDP checksum */
  58. #define SO_BINDTODEVICE 0x100b /* bind to device */
  59. /*
  60. * Structure used for manipulating linger option.
  61. */
  62. /*
  63. * Level number for (get/set)sockopt() to apply to socket itself.
  64. */
  65. #define SOL_SOCKET 0xfff /* options for socket level */
  66. #define AF_UNSPEC 0
  67. #define AF_INET 2
  68. #if LWIP_IPV6
  69. #define AF_INET6 10
  70. #else /* LWIP_IPV6 */
  71. #define AF_INET6 AF_UNSPEC
  72. #endif /* LWIP_IPV6 */
  73. #define PF_INET AF_INET
  74. #define PF_INET6 AF_INET6
  75. #define PF_UNSPEC AF_UNSPEC
  76. #define IPPROTO_IP 0
  77. #define IPPROTO_ICMP 1
  78. #define IPPROTO_TCP 6
  79. #define IPPROTO_UDP 17
  80. #if LWIP_IPV6
  81. #define IPPROTO_IPV6 41
  82. #define IPPROTO_ICMPV6 58
  83. #endif /* LWIP_IPV6 */
  84. #define IPPROTO_UDPLITE 136
  85. #define IPPROTO_RAW 255
  86. /* Flags we can use with send and recv. */
  87. #define MSG_PEEK 0x01 /* Peeks at an incoming message */
  88. #define MSG_WAITALL 0x02 /* Unimplemented: Requests that the function block until the full amount of data requested can be returned */
  89. #define MSG_OOB 0x04 /* Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific */
  90. #define MSG_DONTWAIT 0x08 /* Nonblocking i/o for this operation only */
  91. #define MSG_MORE 0x10 /* Sender will send more */
  92. #define MSG_NOSIGNAL 0x20 /* Uninmplemented: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. */
  93. /*
  94. * Options for level IPPROTO_IP
  95. */
  96. #define IP_TOS 1
  97. #define IP_TTL 2
  98. #define IP_PKTINFO 8
  99. #if LWIP_TCP
  100. /*
  101. * Options for level IPPROTO_TCP
  102. */
  103. #define TCP_NODELAY 0x01 /* don't delay send to coalesce packets */
  104. #define TCP_KEEPALIVE 0x02 /* send KEEPALIVE probes when idle for pcb->keep_idle milliseconds */
  105. #define TCP_KEEPIDLE 0x03 /* set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt */
  106. #define TCP_KEEPINTVL 0x04 /* set pcb->keep_intvl - Use seconds for get/setsockopt */
  107. #define TCP_KEEPCNT 0x05 /* set pcb->keep_cnt - Use number of probes sent for get/setsockopt */
  108. #endif /* LWIP_TCP */
  109. #if LWIP_IPV6
  110. /*
  111. * Options for level IPPROTO_IPV6
  112. */
  113. #define IPV6_CHECKSUM 7 /* RFC3542: calculate and insert the ICMPv6 checksum for raw sockets. */
  114. #define IPV6_V6ONLY 27 /* RFC3493: boolean control to restrict AF_INET6 sockets to IPv6 communications only. */
  115. #endif /* LWIP_IPV6 */
  116. #if LWIP_UDP && LWIP_UDPLITE
  117. /*
  118. * Options for level IPPROTO_UDPLITE
  119. */
  120. #define UDPLITE_SEND_CSCOV 0x01 /* sender checksum coverage */
  121. #define UDPLITE_RECV_CSCOV 0x02 /* minimal receiver checksum coverage */
  122. #endif /* LWIP_UDP && LWIP_UDPLITE*/
  123. #if LWIP_MULTICAST_TX_OPTIONS
  124. /*
  125. * Options and types for UDP multicast traffic handling
  126. */
  127. #define IP_MULTICAST_TTL 5
  128. #define IP_MULTICAST_IF 6
  129. #define IP_MULTICAST_LOOP 7
  130. #endif /* LWIP_MULTICAST_TX_OPTIONS */
  131. #if LWIP_IGMP
  132. /*
  133. * Options and types related to multicast membership
  134. */
  135. #define IP_ADD_MEMBERSHIP 3
  136. #define IP_DROP_MEMBERSHIP 4
  137. typedef struct ip_mreq {
  138. struct in_addr imr_multiaddr; /* IP multicast address of group */
  139. struct in_addr imr_interface; /* local IP address of interface */
  140. } ip_mreq;
  141. #endif /* LWIP_IGMP */
  142. #if LWIP_IPV6_MLD
  143. /*
  144. * Options and types related to IPv6 multicast membership
  145. */
  146. #define IPV6_JOIN_GROUP 12
  147. #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
  148. #define IPV6_LEAVE_GROUP 13
  149. #define IPV6_DROP_MEMBERSHIP IPV6_LEAVE_GROUP
  150. #endif /* LWIP_IPV6_MLD */
  151. /*
  152. * The Type of Service provides an indication of the abstract
  153. * parameters of the quality of service desired. These parameters are
  154. * to be used to guide the selection of the actual service parameters
  155. * when transmitting a datagram through a particular network. Several
  156. * networks offer service precedence, which somehow treats high
  157. * precedence traffic as more important than other traffic (generally
  158. * by accepting only traffic above a certain precedence at time of high
  159. * load). The major choice is a three way tradeoff between low-delay,
  160. * high-reliability, and high-throughput.
  161. * The use of the Delay, Throughput, and Reliability indications may
  162. * increase the cost (in some sense) of the service. In many networks
  163. * better performance for one of these parameters is coupled with worse
  164. * performance on another. Except for very unusual cases at most two
  165. * of these three indications should be set.
  166. */
  167. #define IPTOS_TOS_MASK 0x1E
  168. #define IPTOS_TOS(tos) ((tos) & IPTOS_TOS_MASK)
  169. #define IPTOS_LOWDELAY 0x10
  170. #define IPTOS_THROUGHPUT 0x08
  171. #define IPTOS_RELIABILITY 0x04
  172. #define IPTOS_LOWCOST 0x02
  173. #define IPTOS_MINCOST IPTOS_LOWCOST
  174. /*
  175. * The Network Control precedence designation is intended to be used
  176. * within a network only. The actual use and control of that
  177. * designation is up to each network. The Internetwork Control
  178. * designation is intended for use by gateway control originators only.
  179. * If the actual use of these precedence designations is of concern to
  180. * a particular network, it is the responsibility of that network to
  181. * control the access to, and use of, those precedence designations.
  182. */
  183. #define IPTOS_PREC_MASK 0xe0
  184. #define IPTOS_PREC(tos) ((tos) & IPTOS_PREC_MASK)
  185. #define IPTOS_PREC_NETCONTROL 0xe0
  186. #define IPTOS_PREC_INTERNETCONTROL 0xc0
  187. #define IPTOS_PREC_CRITIC_ECP 0xa0
  188. #define IPTOS_PREC_FLASHOVERRIDE 0x80
  189. #define IPTOS_PREC_FLASH 0x60
  190. #define IPTOS_PREC_IMMEDIATE 0x40
  191. #define IPTOS_PREC_PRIORITY 0x20
  192. #define IPTOS_PREC_ROUTINE 0x00
  193. /*
  194. * Commands for ioctlsocket(), taken from the BSD file fcntl.h.
  195. * lwip_ioctl only supports FIONREAD and FIONBIO, for now
  196. *
  197. * Ioctl's have the command encoded in the lower word,
  198. * and the size of any in or out parameters in the upper
  199. * word. The high 2 bits of the upper word are used
  200. * to encode the in/out status of the parameter; for now
  201. * we restrict parameters to at most 128 bytes.
  202. */
  203. #if !defined(FIONREAD) || !defined(FIONBIO)
  204. #define IOCPARM_MASK 0x7fU /* parameters must be < 128 bytes */
  205. #define IOC_VOID 0x20000000UL /* no parameters */
  206. #define IOC_OUT 0x40000000UL /* copy out parameters */
  207. #define IOC_IN 0x80000000UL /* copy in parameters */
  208. #define IOC_INOUT (IOC_IN|IOC_OUT)
  209. /* 0x20000000 distinguishes new &
  210. old ioctl's */
  211. #define _IO(x,y) ((long)(IOC_VOID|((x)<<8)|(y)))
  212. #define _IOR(x,y,t) ((long)(IOC_OUT|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
  213. #define _IOW(x,y,t) ((long)(IOC_IN|((sizeof(t)&IOCPARM_MASK)<<16)|((x)<<8)|(y)))
  214. #endif /* !defined(FIONREAD) || !defined(FIONBIO) */
  215. #ifndef FIONREAD
  216. #define FIONREAD _IOR('f', 127, unsigned long) /* get # bytes to read */
  217. #endif
  218. #ifndef FIONBIO
  219. #define FIONBIO _IOW('f', 126, unsigned long) /* set/clear non-blocking i/o */
  220. #endif
  221. /* Socket I/O Controls: unimplemented */
  222. #ifndef SIOCSHIWAT
  223. #define SIOCSHIWAT _IOW('s', 0, unsigned long) /* set high watermark */
  224. #define SIOCGHIWAT _IOR('s', 1, unsigned long) /* get high watermark */
  225. #define SIOCSLOWAT _IOW('s', 2, unsigned long) /* set low watermark */
  226. #define SIOCGLOWAT _IOR('s', 3, unsigned long) /* get low watermark */
  227. #define SIOCATMARK _IOR('s', 7, unsigned long) /* at oob mark? */
  228. #endif
  229. /* commands for fnctl */
  230. #ifndef F_GETFL
  231. #define F_GETFL 3
  232. #endif
  233. #ifndef F_SETFL
  234. #define F_SETFL 4
  235. #endif
  236. /* File status flags and file access modes for fnctl,
  237. these are bits in an int. */
  238. #ifndef O_NONBLOCK
  239. #define O_NONBLOCK 1 /* nonblocking I/O */
  240. #endif
  241. #ifndef O_NDELAY
  242. #define O_NDELAY O_NONBLOCK /* same as O_NONBLOCK, for compatibility */
  243. #endif
  244. #ifndef O_RDONLY
  245. #define O_RDONLY 2
  246. #endif
  247. #ifndef O_WRONLY
  248. #define O_WRONLY 4
  249. #endif
  250. #ifndef O_RDWR
  251. #define O_RDWR (O_RDONLY|O_WRONLY)
  252. #endif
  253. static int
  254. lwip_sockopt_to_ipopt(int optname)
  255. {
  256. /* Map SO_* values to our internal SOF_* values
  257. * We should not rely on #defines in socket.h
  258. * being in sync with ip.h.
  259. */
  260. switch (optname) {
  261. case SO_BROADCAST:
  262. return SOF_BROADCAST;
  263. case SO_KEEPALIVE:
  264. return SOF_KEEPALIVE;
  265. case SO_REUSEADDR:
  266. return SOF_REUSEADDR;
  267. default:
  268. LWIP_ASSERT("Unknown socket option", 0);
  269. return 0;
  270. }
  271. }
  272. #define MAX_SOCK_NUM LWIP_NUM_SOCKETS
  273. // extern LUAT_WEAK void DBG_Printf(const char* format, ...);
  274. // #ifdef LUAT_LOG_NO_NEWLINE
  275. #define NET_DBG(x,y...) LLOGD("%s %d:"x, __FUNCTION__,__LINE__,##y)
  276. // #else
  277. // #define NET_DBG(x,y...) LLOGD("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  278. // #endif
  279. enum
  280. {
  281. EV_LWIP_EVENT_START = USER_EVENT_ID_START + 0x2000000,
  282. EV_LWIP_SOCKET_TX,
  283. EV_LWIP_NETIF_INPUT,
  284. EV_LWIP_TCP_TIMER,
  285. EV_LWIP_COMMON_TIMER,
  286. EV_LWIP_SOCKET_RX_DONE,
  287. EV_LWIP_SOCKET_CREATE,
  288. EV_LWIP_SOCKET_CONNECT,
  289. EV_LWIP_SOCKET_DNS,
  290. EV_LWIP_SOCKET_DNS_IPV6,
  291. EV_LWIP_SOCKET_LISTEN,
  292. EV_LWIP_SOCKET_ACCPET,
  293. EV_LWIP_SOCKET_CLOSE,
  294. EV_LWIP_NETIF_LINK_STATE,
  295. EV_LWIP_DHCP_TIMER,
  296. EV_LWIP_FAST_TIMER,
  297. EV_LWIP_NETIF_SET_IP,
  298. EV_LWIP_NETIF_IPV6_BY_MAC,
  299. };
  300. extern u32_t tcp_ticks;
  301. extern struct tcp_pcb *tcp_active_pcbs;
  302. extern struct tcp_pcb *tcp_tw_pcbs;
  303. #define SOCKET_LOCK(ID) platform_lock_mutex(prvlwip.socket[ID].mutex)
  304. #define SOCKET_UNLOCK(ID) platform_unlock_mutex(prvlwip.socket[ID].mutex)
  305. typedef struct
  306. {
  307. llist_head node;
  308. uint64_t tag; //考虑到socket复用的问题,必须有tag来做比对
  309. luat_ip_addr_t ip;
  310. uint8_t *data;
  311. uint32_t read_pos;
  312. uint16_t len;
  313. uint16_t port;
  314. uint8_t is_sending;
  315. uint8_t is_need_ack;
  316. }socket_data_t;
  317. typedef struct
  318. {
  319. dns_client_t dns_client;
  320. socket_ctrl_t socket[MAX_SOCK_NUM];
  321. struct netif *lwip_netif[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  322. uint64_t last_sleep_ms;
  323. uint64_t socket_tag;
  324. CBFuncEx_t socket_cb[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  325. void *user_data[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  326. void *task_handle;
  327. struct udp_pcb *dns_udp;
  328. //HANDLE socket_mutex;
  329. HANDLE tcp_timer;//tcp_tmr
  330. HANDLE common_timer;//ip_reass_tmr,etharp_tmr,dns_tmr,nd6_tmr,ip6_reass_tmr
  331. // HANDLE fast_timer;//igmp_tmr,mld6_tmr,autoip_tmr
  332. // HANDLE dhcp_timer;//dhcp_fine_tmr,dhcp6_tmr
  333. uint8_t dns_adapter_index;
  334. uint8_t netif_network_ready[NW_ADAPTER_INDEX_LWIP_NETIF_QTY];
  335. uint8_t tcpip_tcp_timer_active;
  336. uint8_t common_timer_active;
  337. // uint8_t dhcp_timer_active;
  338. // uint8_t fast_timer_active;
  339. // uint8_t dhcp_check_cnt;
  340. uint8_t next_socket_index;
  341. uint8_t socket_busy;
  342. }net_lwip_ctrl_struct;
  343. static net_lwip_ctrl_struct prvlwip;
  344. static void net_lwip_check_network_ready(uint8_t adapter_index);
  345. static void net_lwip_task(void *param);
  346. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id);
  347. // static void net_lwip_dhcp_done_cb(struct netif *netif)
  348. // {
  349. // net_lwip_check_network_ready((uint8_t)netif->dhcp_done_arg);
  350. // }
  351. static int net_lwip_del_data_cache(void *p, void *u)
  352. {
  353. socket_data_t *pdata = (socket_data_t *)p;
  354. free(pdata->data);
  355. return LIST_DEL;
  356. }
  357. static int net_lwip_next_data_cache(void *p, void *u)
  358. {
  359. socket_ctrl_t *socket = (socket_ctrl_t *)u;
  360. socket_data_t *pdata = (socket_data_t *)p;
  361. if (socket->tag != pdata->tag)
  362. {
  363. NET_DBG("tag error");
  364. free(pdata->data);
  365. return LIST_DEL;
  366. }
  367. return LIST_FIND;
  368. }
  369. 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)
  370. {
  371. socket_data_t *p = (socket_data_t *)malloc(sizeof(socket_data_t));
  372. if (p)
  373. {
  374. memset(p, 0, sizeof(socket_data_t));
  375. p->len = len;
  376. p->port = remote_port;
  377. if (remote_ip)
  378. {
  379. p->ip = *remote_ip;
  380. }
  381. else
  382. {
  383. p->ip.type = 0xff;
  384. }
  385. p->tag = prvlwip.socket[socket_id].tag;
  386. if (data && len)
  387. {
  388. p->data = malloc(len);
  389. if (p->data)
  390. {
  391. memcpy(p->data, data, len);
  392. }
  393. else
  394. {
  395. free(p);
  396. return NULL;
  397. }
  398. }
  399. }
  400. return p;
  401. }
  402. static LUAT_RT_RET_TYPE net_lwip_timer_cb(LUAT_RT_CB_PARAM)
  403. {
  404. luat_send_event_to_task(prvlwip.task_handle, (uint32_t)param, 0, 0, 0);
  405. return LUAT_RT_RET;
  406. }
  407. 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)
  408. {
  409. luat_network_cb_param_t param = {.tag = 0, .param = prvlwip.user_data[adapter_index]};
  410. OS_EVENT event = { .ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3};
  411. if ((event_id > EV_NW_DNS_RESULT))
  412. {
  413. if (event_id != EV_NW_SOCKET_CLOSE_OK)
  414. {
  415. event.Param3 = prvlwip.socket[param1].param;
  416. param.tag = prvlwip.socket[param1].tag;
  417. }
  418. else
  419. {
  420. event.Param3 = ((luat_network_cb_param_t *)param3)->param;
  421. param.tag = ((luat_network_cb_param_t *)param3)->tag;
  422. }
  423. }
  424. prvlwip.socket_cb[adapter_index](&event, &param);
  425. }
  426. static err_t net_lwip_tcp_connected_cb(void *arg, struct tcp_pcb *tpcb, err_t err)
  427. {
  428. // LLOGD("net_lwip_tcp_connected_cb ?? %d", err);
  429. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  430. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  431. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  432. return ERR_OK;
  433. }
  434. static void net_lwip_tcp_error(uint8_t adapter_index, int socket_id)
  435. {
  436. // LLOGD("net_lwip_tcp_error ?? %d", socket_id);
  437. prvlwip.socket[socket_id].remote_close = 1;
  438. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  439. }
  440. static int net_lwip_rx_data(int socket_id, struct pbuf *p, const ip_addr_t *addr, u16_t port)
  441. {
  442. // LLOGD("net_lwip_rx_data ?? %d", socket_id);
  443. int is_mem_err = 0;
  444. SOCKET_LOCK(socket_id);
  445. socket_data_t *data_p = net_lwip_create_data_node(socket_id, NULL, 0, addr, port);
  446. if (data_p)
  447. {
  448. data_p->data = malloc(p->tot_len);
  449. if (data_p->data)
  450. {
  451. data_p->len = pbuf_copy_partial(p, data_p->data, p->tot_len, 0);
  452. // NET_DBG("new data %ubyte", p->tot_len);
  453. llist_add_tail(&data_p->node, &prvlwip.socket[socket_id].rx_head);
  454. prvlwip.socket[socket_id].rx_wait_size += p->tot_len;
  455. }
  456. else
  457. {
  458. free(data_p);
  459. is_mem_err = 1;
  460. }
  461. }
  462. else
  463. {
  464. is_mem_err = 1;
  465. }
  466. SOCKET_UNLOCK(socket_id);
  467. return is_mem_err;
  468. }
  469. static void net_lwip_tcp_close_done(uint8_t adapter_index, int socket_id, uint8_t notify)
  470. {
  471. // LLOGD("net_lwip_tcp_close_done %d", socket_id);
  472. luat_network_cb_param_t cb_param;
  473. SOCKET_LOCK(socket_id);
  474. OS_LOCK;
  475. cb_param.param = prvlwip.socket[socket_id].param;
  476. cb_param.tag = prvlwip.socket[socket_id].tag;
  477. prvlwip.socket[socket_id].pcb.ip = NULL;
  478. prvlwip.socket[socket_id].listen_tcp = NULL;
  479. prvlwip.socket[socket_id].remote_close = 0;
  480. prvlwip.socket[socket_id].state = 0;
  481. prvlwip.socket[socket_id].in_use = 0;
  482. prvlwip.socket[socket_id].param = NULL;
  483. prvlwip.socket[socket_id].rx_wait_size = 0;
  484. prvlwip.socket[socket_id].tx_wait_size = 0;
  485. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  486. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  487. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  488. prvlwip.socket_busy &= ~(1 << socket_id);
  489. OS_UNLOCK;
  490. SOCKET_UNLOCK(socket_id);
  491. if (notify)
  492. {
  493. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CLOSE_OK, socket_id, 0, &cb_param);
  494. }
  495. }
  496. static err_t net_lwip_tcp_recv_cb(void *arg, struct tcp_pcb *tpcb,
  497. struct pbuf *p, err_t err)
  498. {
  499. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  500. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  501. uint16_t len;
  502. if (p)
  503. {
  504. // tcp_recved(tpcb, p->tot_len);
  505. len = p->tot_len;
  506. if (net_lwip_rx_data(socket_id, p, NULL, 0))
  507. {
  508. NET_DBG("no memory!");
  509. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  510. }
  511. else
  512. {
  513. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  514. }
  515. pbuf_free(p);
  516. }
  517. else if (err == ERR_OK)
  518. {
  519. {
  520. prvlwip.socket[socket_id].remote_close = 1;
  521. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  522. }
  523. }
  524. else
  525. {
  526. net_lwip_tcp_error(adapter_index, socket_id);
  527. }
  528. return ERR_OK;
  529. }
  530. static err_t net_lwip_tcp_sent_cb(void *arg, struct tcp_pcb *tpcb,
  531. u16_t len)
  532. {
  533. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  534. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  535. volatile uint16_t check_len = 0;
  536. volatile uint32_t rest_len;
  537. socket_data_t *p;
  538. SOCKET_LOCK(socket_id);
  539. while(check_len < len)
  540. {
  541. if (llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  542. {
  543. NET_DBG("!");
  544. goto SOCEKT_ERROR;
  545. }
  546. p = (socket_data_t *)prvlwip.socket[socket_id].wait_ack_head.next;
  547. rest_len = p->len - p->read_pos;
  548. if ((len - check_len) >= rest_len)
  549. {
  550. // NET_DBG("adapter %d socket %d, %ubytes ack", adapter_index, socket_id, p->len);
  551. llist_del(&p->node);
  552. free(p->data);
  553. free(p);
  554. check_len += rest_len;
  555. }
  556. else
  557. {
  558. p->read_pos += (len - check_len);
  559. check_len = len;
  560. // NET_DBG("adapter %d socket %d, all %ubytes ack %ubytes ", adapter_index, socket_id, p->len, p->read_pos);
  561. }
  562. }
  563. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  564. {
  565. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  566. if (p)
  567. {
  568. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  569. {
  570. llist_del(&p->node);
  571. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  572. }
  573. else
  574. {
  575. NET_DBG("tcp buf is full, wait ack and send again");
  576. break;
  577. }
  578. }
  579. }
  580. SOCKET_UNLOCK(socket_id);
  581. tcp_output(tpcb);
  582. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  583. return ERR_OK;
  584. SOCEKT_ERROR:
  585. SOCKET_UNLOCK(socket_id);
  586. net_lwip_tcp_error(adapter_index, socket_id);
  587. return ERR_OK;
  588. }
  589. static err_t net_lwip_tcp_err_cb(void *arg, err_t err)
  590. {
  591. // LLOGD("net_lwip_tcp_err_cb ?? %d", err);
  592. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  593. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  594. if (!prvlwip.socket[socket_id].state && !prvlwip.socket[socket_id].remote_close)
  595. {
  596. NET_DBG("adapter %d socket %d not closing, but error %d", adapter_index, socket_id, err);
  597. prvlwip.socket[socket_id].pcb.ip = NULL;
  598. net_lwip_tcp_error(adapter_index, socket_id);
  599. }
  600. return 0;
  601. }
  602. static err_t net_lwip_tcp_fast_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  603. {
  604. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  605. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  606. if (err || !newpcb)
  607. {
  608. net_lwip_tcp_error(adapter_index, socket_id);
  609. return 0;
  610. }
  611. prvlwip.socket[socket_id].pcb.tcp = newpcb;
  612. prvlwip.socket[socket_id].rx_wait_size = 0;
  613. prvlwip.socket[socket_id].tx_wait_size = 0;
  614. prvlwip.socket[socket_id].pcb.tcp->callback_arg = arg;
  615. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  616. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  617. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  618. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  619. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  620. return ERR_OK;
  621. }
  622. static err_t net_lwip_tcp_accept_cb(void *arg, struct tcp_pcb *newpcb, err_t err)
  623. {
  624. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  625. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  626. return ERR_OK;
  627. }
  628. static err_t net_lwip_udp_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  629. const ip_addr_t *addr, u16_t port)
  630. {
  631. int socket_id = ((uint32_t)arg) & 0x0000ffff;
  632. uint8_t adapter_index = ((uint32_t)arg) >> 16;
  633. uint16_t len;
  634. if (p)
  635. {
  636. len = p->tot_len;
  637. if (net_lwip_rx_data(socket_id, p, addr, port))
  638. {
  639. NET_DBG("no memory!");
  640. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  641. }
  642. else
  643. {
  644. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_RX_NEW, socket_id, len, 0);
  645. }
  646. pbuf_free(p);
  647. }
  648. return ERR_OK;
  649. }
  650. static int32_t net_lwip_dns_check_result(void *data, void *param)
  651. {
  652. luat_dns_require_t *require = (luat_dns_require_t *)data;
  653. if (require->result != 0)
  654. {
  655. free(require->uri.Data);
  656. require->uri.Data = NULL;
  657. if (require->result > 0)
  658. {
  659. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  660. int i;
  661. for(i = 0; i < require->result; i++)
  662. {
  663. ip_result[i] = require->ip_result[i];
  664. }
  665. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  666. }
  667. else
  668. {
  669. net_lwip_callback_to_nw_task(require->adapter_index, EV_NW_DNS_RESULT, 0, 0, require->param);
  670. }
  671. return LIST_DEL;
  672. }
  673. else
  674. {
  675. return LIST_PASS;
  676. }
  677. }
  678. static err_t net_lwip_dns_recv_cb(void *arg, struct udp_pcb *pcb, struct pbuf *p,
  679. const ip_addr_t *addr, u16_t port)
  680. {
  681. Buffer_Struct msg_buf;
  682. Buffer_Struct tx_msg_buf = {0,0,0};
  683. struct pbuf *out_p;
  684. int i;
  685. if (p)
  686. {
  687. OS_InitBuffer(&msg_buf, p->tot_len);
  688. pbuf_copy_partial(p, msg_buf.Data, p->tot_len, 0);
  689. pbuf_free(p);
  690. dns_run(&prvlwip.dns_client, &msg_buf, NULL, &i);
  691. OS_DeInitBuffer(&msg_buf);
  692. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  693. {
  694. dns_run(&prvlwip.dns_client, NULL, &tx_msg_buf, &i);
  695. if (tx_msg_buf.Pos)
  696. {
  697. out_p = pbuf_alloc(PBUF_RAW, tx_msg_buf.Pos, PBUF_ROM);
  698. if (out_p)
  699. {
  700. out_p->payload = tx_msg_buf.Data;
  701. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  702. {
  703. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  704. }
  705. else
  706. {
  707. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  708. {
  709. if (prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr_state[i] & IP6_ADDR_VALID)
  710. {
  711. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr[i];
  712. break;
  713. }
  714. }
  715. }
  716. err_t err = udp_sendto(prvlwip.dns_udp, out_p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  717. pbuf_free(out_p);
  718. }
  719. OS_DeInitBuffer(&tx_msg_buf);
  720. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  721. }
  722. }
  723. }
  724. return ERR_OK;
  725. }
  726. static void net_lwip_dns_tx_next(Buffer_Struct *tx_msg_buf)
  727. {
  728. int i;
  729. err_t err;
  730. struct pbuf *p;
  731. dns_run(&prvlwip.dns_client, NULL, tx_msg_buf, &i);
  732. if (tx_msg_buf->Pos)
  733. {
  734. p = pbuf_alloc(PBUF_RAW, tx_msg_buf->Pos, PBUF_ROM);
  735. if (p)
  736. {
  737. p->payload = tx_msg_buf->Data;
  738. if (prvlwip.dns_client.dns_server[i].type == IPADDR_TYPE_V4)
  739. {
  740. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip_addr;
  741. }
  742. else
  743. {
  744. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  745. {
  746. if (prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr_state[i] & IP6_ADDR_VALID)
  747. {
  748. prvlwip.dns_udp->local_ip = prvlwip.lwip_netif[prvlwip.dns_adapter_index]->ip6_addr[i];
  749. break;
  750. }
  751. }
  752. }
  753. err = udp_sendto(prvlwip.dns_udp, p, &prvlwip.dns_client.dns_server[i], DNS_SERVER_PORT);
  754. pbuf_free(p);
  755. }
  756. OS_DeInitBuffer(tx_msg_buf);
  757. llist_traversal(&prvlwip.dns_client.require_head, net_lwip_dns_check_result, NULL);
  758. }
  759. }
  760. // uint32_t net_lwip_rand()
  761. // {
  762. // PV_Union uPV;
  763. // luat_crypto_trng((char*)uPV.u8, 4);
  764. // return uPV.u32;
  765. // }
  766. void net_lwip_init(void)
  767. {
  768. // LLOGD("%s:%d", __FILE__, __LINE__);
  769. uint8_t i;
  770. for(i = 0; i < MAX_SOCK_NUM; i++)
  771. {
  772. INIT_LLIST_HEAD(&prvlwip.socket[i].wait_ack_head);
  773. INIT_LLIST_HEAD(&prvlwip.socket[i].tx_head);
  774. INIT_LLIST_HEAD(&prvlwip.socket[i].rx_head);
  775. prvlwip.socket[i].mutex = platform_create_mutex();
  776. }
  777. prvlwip.tcp_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_TCP_TIMER, 0);
  778. prvlwip.common_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_COMMON_TIMER, 0);
  779. // prvlwip.fast_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_FAST_TIMER, 0);
  780. // prvlwip.dhcp_timer = platform_create_timer(net_lwip_timer_cb, (void *)EV_LWIP_DHCP_TIMER, 0);
  781. tcp_ticks = luat_mcu_tick64_ms() / TCP_SLOW_INTERVAL;
  782. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  783. platform_create_task(&prvlwip.task_handle, 8 * 1024, 40, "lwip", net_lwip_task, NULL, 64);
  784. platform_start_timer(prvlwip.common_timer, 1000, 1);
  785. prvlwip.dns_udp = udp_new();
  786. prvlwip.dns_udp->recv = net_lwip_dns_recv_cb;
  787. udp_bind(prvlwip.dns_udp, NULL, 55);
  788. dns_init_client(&prvlwip.dns_client);
  789. }
  790. // void tcp_timer_needed(void)
  791. // {
  792. // if (!prvlwip.tcpip_tcp_timer_active && (tcp_active_pcbs || tcp_tw_pcbs)) {
  793. // prvlwip.tcpip_tcp_timer_active = 1;
  794. // platform_start_timer(prvlwip.tcp_timer, TCP_TMR_INTERVAL, 1);
  795. // NET_DBG("tcp timer start");
  796. // }
  797. // }
  798. // u32_t sys_now(void)
  799. // {
  800. // return (u32_t)luat_mcu_tick64_ms();
  801. // }
  802. static ip_addr_t *net_lwip_get_ip6(void)
  803. {
  804. // int i;
  805. // for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  806. // {
  807. // if (prvlwip.lwip_netif->ip6_addr_state[i] & IP6_ADDR_PREFERRED)
  808. // {
  809. // return &prvlwip.lwip_netif->ip6_addr[i];
  810. // }
  811. // }
  812. // for(i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  813. // {
  814. // if (prvlwip.lwip_netif->ip6_addr_state[i] & IP6_ADDR_VALID)
  815. // {
  816. // return &prvlwip.lwip_netif->ip6_addr[i];
  817. // }
  818. // }
  819. return NULL;
  820. }
  821. static void net_lwip_close_tcp(int socket_id)
  822. {
  823. prvlwip.socket[socket_id].pcb.tcp->sent = NULL;
  824. prvlwip.socket[socket_id].pcb.tcp->errf = NULL;
  825. prvlwip.socket[socket_id].pcb.tcp->poll = NULL;
  826. prvlwip.socket[socket_id].pcb.tcp->recv = tcp_recv_null;
  827. prvlwip.socket[socket_id].pcb.tcp->callback_arg = 0;
  828. if (tcp_close(prvlwip.socket[socket_id].pcb.tcp))
  829. {
  830. tcp_abort(prvlwip.socket[socket_id].pcb.tcp);
  831. }
  832. }
  833. static void net_lwip_task(void *param)
  834. {
  835. luat_rtos_task_sleep(10);
  836. luat_network_cb_param_t cb_param;
  837. OS_EVENT event;
  838. Buffer_Struct tx_msg_buf = {0,0,0};
  839. // HANDLE cur_task = prvlwip.task_handle;
  840. // LLOGD("net_lwip_task cur_task? %p", cur_task);
  841. struct tcp_pcb *pcb;
  842. struct tcp_pcb *dpcb;
  843. struct netif *netif;
  844. // struct dhcp *dhcp;
  845. socket_data_t *p;
  846. ip_addr_t *p_ip, *local_ip;
  847. struct pbuf *out_p;
  848. int error, i;
  849. PV_Union uPV;
  850. uint8_t active_flag;
  851. uint8_t socket_id;
  852. uint8_t adapter_index;
  853. while(1)
  854. {
  855. if (luat_wait_event_from_task(prvlwip.task_handle, 0, &event, NULL, 0) != ERROR_NONE)
  856. {
  857. continue;
  858. }
  859. if (!prvlwip.tcpip_tcp_timer_active)
  860. {
  861. if ((luat_mcu_tick64_ms() - prvlwip.last_sleep_ms) >= TCP_SLOW_INTERVAL)
  862. {
  863. tcp_ticks += (luat_mcu_tick64_ms() - prvlwip.last_sleep_ms) / TCP_SLOW_INTERVAL;
  864. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  865. // NET_DBG("tcp ticks add to %u", tcp_ticks);
  866. }
  867. }
  868. else
  869. {
  870. prvlwip.last_sleep_ms = luat_mcu_tick64_ms();
  871. }
  872. socket_id = event.Param1;
  873. adapter_index = event.Param3;
  874. // LLOGD("task event %08X", event.ID & 0xFFFFFFF);
  875. switch(event.ID)
  876. {
  877. case EV_LWIP_SOCKET_TX:
  878. SOCKET_LOCK(socket_id);
  879. if (prvlwip.socket[socket_id].in_use && prvlwip.socket[socket_id].pcb.ip)
  880. {
  881. if (!prvlwip.socket[socket_id].pcb.tcp->unsent && !prvlwip.socket[socket_id].pcb.tcp->unacked)
  882. {
  883. active_flag = 0;
  884. }
  885. else
  886. {
  887. active_flag = 1;
  888. }
  889. if (prvlwip.socket[socket_id].is_tcp)
  890. {
  891. while (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  892. {
  893. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  894. if (p->len <= tcp_sndbuf(prvlwip.socket[socket_id].pcb.tcp))
  895. {
  896. if (ERR_OK == tcp_write(prvlwip.socket[socket_id].pcb.tcp, p->data, p->len, 0))
  897. {
  898. llist_del(&p->node);
  899. llist_add_tail(&p->node, &prvlwip.socket[socket_id].wait_ack_head);
  900. }
  901. else
  902. {
  903. // NET_DBG("tcp buf is full, wait ack and send again");
  904. break;
  905. }
  906. }
  907. }
  908. SOCKET_UNLOCK(socket_id);
  909. tcp_output(prvlwip.socket[socket_id].pcb.tcp);
  910. }
  911. else
  912. {
  913. p = llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  914. if (p)
  915. {
  916. llist_del(&p->node);
  917. }
  918. SOCKET_UNLOCK(socket_id);
  919. if (p)
  920. {
  921. uint32_t len = p->len;
  922. out_p = pbuf_alloc(PBUF_RAW, p->len, PBUF_ROM);
  923. if (out_p)
  924. {
  925. out_p->payload = p->data;
  926. error = udp_sendto(prvlwip.socket[socket_id].pcb.udp, out_p, &p->ip, p->port);
  927. pbuf_free(out_p);
  928. }
  929. else
  930. {
  931. NET_DBG("mem err send fail");
  932. }
  933. free(p->data);
  934. free(p);
  935. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_TX_OK, socket_id, len, 0);
  936. }
  937. }
  938. }
  939. else
  940. {
  941. NET_DBG("adapter %d socket %d no in use! %x", adapter_index, socket_id, prvlwip.socket[socket_id].pcb.ip);
  942. SOCKET_UNLOCK(socket_id);
  943. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  944. }
  945. break;
  946. case EV_LWIP_NETIF_INPUT:
  947. netif = (struct netif *)event.Param3;
  948. error = netif->input((struct pbuf *)event.Param1, netif);
  949. if(error != ERR_OK)
  950. {
  951. NET_DBG("%d", error);
  952. pbuf_free((struct pbuf *)event.Param1);
  953. }
  954. break;
  955. case EV_LWIP_TCP_TIMER:
  956. tcp_tmr();
  957. if (tcp_active_pcbs || tcp_tw_pcbs)
  958. {
  959. dpcb = NULL;
  960. for (pcb = tcp_active_pcbs; pcb != NULL; pcb = pcb->next)
  961. {
  962. if (FIN_WAIT_1 == pcb->state)
  963. {
  964. uPV.p = pcb->callback_arg;
  965. uPV.u32++;
  966. pcb->callback_arg = uPV.p;
  967. if (uPV.u32 >= 20)
  968. {
  969. NET_DBG("tcp %x fin send too much time!", pcb);
  970. dpcb = pcb;
  971. }
  972. }
  973. }
  974. if (dpcb)
  975. {
  976. tcp_abort(dpcb);
  977. }
  978. }
  979. else
  980. {
  981. prvlwip.tcpip_tcp_timer_active = 0;
  982. platform_stop_timer(prvlwip.tcp_timer);
  983. NET_DBG("tcp timer stop");
  984. }
  985. break;
  986. case EV_LWIP_COMMON_TIMER:
  987. #ifdef LUAT_USE_DNS
  988. net_lwip_dns_tx_next(&tx_msg_buf);
  989. #endif
  990. // #if IP_REASSEMBLY
  991. // ip_reass_tmr();
  992. // #endif
  993. // #if LWIP_ARP
  994. // etharp_tmr();
  995. // #endif
  996. // #if LWIP_DNS
  997. // dns_tmr();
  998. // #endif
  999. // #if LWIP_IPV6
  1000. // nd6_tmr();
  1001. // #endif
  1002. // #if LWIP_IPV6_REASS
  1003. // ip6_reass_tmr();
  1004. // #endif
  1005. // #if LWIP_DHCP
  1006. // // prvlwip.dhcp_check_cnt++;
  1007. // // if (prvlwip.dhcp_check_cnt >= DHCP_COARSE_TIMER_SECS)
  1008. // // {
  1009. // // prvlwip.dhcp_check_cnt = 0;
  1010. // // dhcp_coarse_tmr();
  1011. // // if (!prvlwip.dhcp_timer_active)
  1012. // // {
  1013. // // prvlwip.dhcp_timer_active = 1;
  1014. // // platform_start_timer(prvlwip.dhcp_timer, DHCP_FINE_TIMER_MSECS, 1);
  1015. // // NET_DBG("dhcp timer start");
  1016. // // }
  1017. // // }
  1018. // #endif
  1019. break;
  1020. case EV_LWIP_SOCKET_RX_DONE:
  1021. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip || !prvlwip.socket[socket_id].is_tcp)
  1022. {
  1023. 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);
  1024. break;
  1025. }
  1026. // NET_DBG("socket %d rx ack %dbytes", socket_id, event.Param2);
  1027. tcp_recved(prvlwip.socket[socket_id].pcb.tcp, event.Param2);
  1028. break;
  1029. case EV_LWIP_SOCKET_CREATE:
  1030. net_lwip_create_socket_now(adapter_index, socket_id);
  1031. break;
  1032. case EV_LWIP_SOCKET_CONNECT:
  1033. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  1034. {
  1035. 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);
  1036. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1037. break;
  1038. }
  1039. p_ip = (ip_addr_t *)event.Param2;
  1040. local_ip = NULL;
  1041. if (p_ip->type == IPADDR_TYPE_V4)
  1042. {
  1043. local_ip = &prvlwip.lwip_netif[adapter_index]->ip_addr;
  1044. }
  1045. else
  1046. {
  1047. local_ip = net_lwip_get_ip6();
  1048. }
  1049. if (!local_ip)
  1050. {
  1051. NET_DBG("netif no ip !!!!!!");
  1052. net_lwip_tcp_error(adapter_index, socket_id);
  1053. break;
  1054. }
  1055. if (prvlwip.socket[socket_id].is_tcp)
  1056. {
  1057. // LLOGD(">>>>>>>>> tcp_bind %d %d", socket_id, prvlwip.socket[socket_id].local_port);
  1058. // tcp_bind 有问题, 不知道为啥, 可能是本地ip为NULL?
  1059. // error = tcp_bind(prvlwip.socket[socket_id].pcb.tcp, local_ip, prvlwip.socket[socket_id].local_port);
  1060. // LLOGD(">>> tcp_bind %d", error);
  1061. error = tcp_connect(prvlwip.socket[socket_id].pcb.tcp, p_ip, prvlwip.socket[socket_id].remote_port, net_lwip_tcp_connected_cb);
  1062. // LLOGD(">>> tcp_connect %d", error);
  1063. if (error)
  1064. {
  1065. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  1066. net_lwip_tcp_error(adapter_index, socket_id);
  1067. }
  1068. }
  1069. else
  1070. {
  1071. udp_bind(prvlwip.socket[socket_id].pcb.udp, local_ip, prvlwip.socket[socket_id].local_port);
  1072. error = udp_connect(prvlwip.socket[socket_id].pcb.udp, p_ip, prvlwip.socket[socket_id].remote_port);
  1073. if (error)
  1074. {
  1075. NET_DBG("adapter %d socket %d connect error %d", adapter_index, socket_id, error);
  1076. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1077. }
  1078. else
  1079. {
  1080. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  1081. }
  1082. }
  1083. break;
  1084. case EV_LWIP_SOCKET_DNS:
  1085. case EV_LWIP_SOCKET_DNS_IPV6:
  1086. dns_require_ipv6(&prvlwip.dns_client, event.Param1, event.Param2, event.Param3, event.ID - EV_LWIP_SOCKET_DNS);
  1087. net_lwip_dns_tx_next(&tx_msg_buf);
  1088. break;
  1089. case EV_LWIP_SOCKET_LISTEN:
  1090. if (!prvlwip.socket[socket_id].in_use || !prvlwip.socket[socket_id].pcb.ip)
  1091. {
  1092. 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);
  1093. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1094. break;
  1095. }
  1096. tcp_bind(prvlwip.socket[socket_id].pcb.tcp, NULL, prvlwip.socket[socket_id].local_port);
  1097. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->local_ip, IPADDR_TYPE_ANY);
  1098. IP_SET_TYPE_VAL(prvlwip.socket[socket_id].pcb.tcp->remote_ip, IPADDR_TYPE_ANY);
  1099. prvlwip.socket[socket_id].listen_tcp = tcp_listen_with_backlog(prvlwip.socket[socket_id].pcb.tcp, 1);
  1100. if (!prvlwip.socket[socket_id].listen_tcp) {
  1101. NET_DBG("socket %d listen failed");
  1102. net_lwip_tcp_error(adapter_index, socket_id);
  1103. } else {
  1104. PV_Union uPV;
  1105. uPV.u16[0] = socket_id;
  1106. uPV.u16[1] = adapter_index;
  1107. prvlwip.socket[socket_id].listen_tcp->callback_arg = uPV.u32;
  1108. prvlwip.socket[socket_id].listen_tcp->accept = net_lwip_tcp_fast_accept_cb;
  1109. prvlwip.socket[socket_id].pcb.tcp = NULL;
  1110. net_lwip_callback_to_nw_task(adapter_index, EV_NW_SOCKET_LISTEN, socket_id, 0, 0);
  1111. }
  1112. break;
  1113. // case EV_LWIP_SOCKET_ACCPET:
  1114. //
  1115. // break;
  1116. case EV_LWIP_SOCKET_CLOSE:
  1117. if (!prvlwip.socket[socket_id].in_use)
  1118. {
  1119. NET_DBG("socket %d no in use!,%x", socket_id);
  1120. break;
  1121. }
  1122. if (prvlwip.socket[socket_id].listen_tcp)
  1123. {
  1124. tcp_close(prvlwip.socket[socket_id].listen_tcp);
  1125. prvlwip.socket[socket_id].listen_tcp = NULL;
  1126. if (prvlwip.socket[socket_id].pcb.tcp)
  1127. {
  1128. net_lwip_close_tcp(socket_id);
  1129. }
  1130. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1131. break;
  1132. }
  1133. if (prvlwip.socket[socket_id].pcb.ip)
  1134. {
  1135. if (prvlwip.socket[socket_id].is_tcp)
  1136. {
  1137. net_lwip_close_tcp(socket_id);
  1138. }
  1139. else
  1140. {
  1141. udp_remove(prvlwip.socket[socket_id].pcb.udp);
  1142. }
  1143. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1144. break;
  1145. }
  1146. if (prvlwip.socket[socket_id].remote_close)
  1147. {
  1148. net_lwip_tcp_close_done(adapter_index, socket_id, event.Param2);
  1149. break;
  1150. }
  1151. break;
  1152. // case EV_LWIP_DHCP_TIMER:
  1153. // #if LWIP_DHCP
  1154. // // dhcp_fine_tmr();
  1155. // // active_flag = 0;
  1156. // // NETIF_FOREACH(netif)
  1157. // // {
  1158. // // dhcp = netif_dhcp_data(netif);
  1159. // // /* only act on DHCP configured interfaces */
  1160. // // if (dhcp && dhcp->request_timeout && (dhcp->state != DHCP_STATE_BOUND))
  1161. // // {
  1162. // // active_flag = 1;
  1163. // // break;
  1164. // // }
  1165. // // }
  1166. // // if (!active_flag)
  1167. // // {
  1168. // // NET_DBG("stop dhcp timer!");
  1169. // // prvlwip.dhcp_timer_active = 0;
  1170. // // platform_stop_timer(prvlwip.dhcp_timer);
  1171. // // }
  1172. // #endif
  1173. // break;
  1174. // case EV_LWIP_FAST_TIMER:
  1175. // #if LWIP_AUTOIP
  1176. // autoip_tmr();
  1177. // #endif
  1178. // #if LWIP_IGMP
  1179. // igmp_tmr();
  1180. // #endif
  1181. // #if LWIP_IPV6_MLD
  1182. // mld6_tmr();
  1183. // #endif
  1184. // active_flag = 0;
  1185. // NETIF_FOREACH(netif)
  1186. // {
  1187. // if (
  1188. // #if LWIP_IPV6_MLD
  1189. // netif_mld6_data(netif)
  1190. // #endif
  1191. // #if LWIP_IGMP
  1192. // || netif_igmp_data(netif)
  1193. // #endif
  1194. // #if LWIP_AUTOIP
  1195. // || netif_autoip_data(netif)
  1196. // #endif
  1197. // )
  1198. // {
  1199. // active_flag = 1;
  1200. // break;
  1201. // }
  1202. // }
  1203. // if (!active_flag)
  1204. // {
  1205. // NET_DBG("stop fast timer!");
  1206. // prvlwip.fast_timer_active = 0;
  1207. // platform_stop_timer(prvlwip.fast_timer);
  1208. // }
  1209. // break;
  1210. case EV_LWIP_NETIF_SET_IP:
  1211. netif = prvlwip.lwip_netif[event.Param3];
  1212. p_ip = (ip_addr_t *)event.Param1;
  1213. // if ((p_ip[0].type != IPADDR_TYPE_ANY) && p_ip[0].u_addr.ip4.addr)
  1214. // {
  1215. // dhcp_release_and_stop(netif);
  1216. // }
  1217. // netif_set_addr(netif, &p_ip[0].u_addr.ip4, &p_ip[1].u_addr.ip4, &p_ip[2].u_addr.ip4);
  1218. // netif_ip6_addr_set(netif, 1, &p_ip[3]);
  1219. // if (event.Param2)
  1220. // {
  1221. // netif_ip6_addr_set_state(netif, 1, IP6_ADDR_VALID);
  1222. // }
  1223. // else
  1224. // {
  1225. // netif_ip6_addr_set_state(netif, 1, IP6_ADDR_INVALID);
  1226. // }
  1227. // if (!prvlwip.fast_timer_active)
  1228. // {
  1229. // NET_DBG("start fast timer!");
  1230. // prvlwip.fast_timer_active = 1;
  1231. // platform_start_timer(prvlwip.fast_timer, 100, 1);
  1232. // }
  1233. free(p_ip);
  1234. break;
  1235. case EV_LWIP_NETIF_LINK_STATE:
  1236. NET_DBG("%d,%d,%d", event.Param3, prvlwip.netif_network_ready[event.Param3], event.Param1);
  1237. // if (prvlwip.netif_network_ready[event.Param3] != event.Param1)
  1238. // {
  1239. // if (event.Param1)
  1240. // {
  1241. // netif_set_link_up(prvlwip.lwip_netif[event.Param3]);
  1242. // }
  1243. // else
  1244. // {
  1245. // netif_set_link_down(prvlwip.lwip_netif[event.Param3]);
  1246. // }
  1247. // }
  1248. net_lwip_check_network_ready(event.Param3);
  1249. break;
  1250. case EV_LWIP_NETIF_IPV6_BY_MAC:
  1251. // netif_create_ip6_linklocal_address(prvlwip.lwip_netif[event.Param3], 1);
  1252. break;
  1253. default:
  1254. NET_DBG("unknow event %x,%x", event.ID, event.Param1);
  1255. break;
  1256. }
  1257. }
  1258. }
  1259. static void net_lwip_check_network_ready(uint8_t adapter_index)
  1260. {
  1261. int i;
  1262. char ip_string[64];
  1263. struct netif *netif = prvlwip.lwip_netif[adapter_index];
  1264. uint8_t active_flag = netif_is_flag_set(netif, NETIF_FLAG_LINK_UP);
  1265. uint8_t ip_ready = 0;
  1266. LLOGD("CALL net_lwip_check_network_ready index %d", adapter_index);
  1267. // if (!active_flag)
  1268. // {
  1269. // dhcp_release_and_stop(netif);
  1270. // }
  1271. #if LWIP_IPV4
  1272. if (netif->ip_addr.u_addr.ip4.addr)
  1273. {
  1274. ip_ready = 1;
  1275. }
  1276. #endif
  1277. #if LWIP_IPV6
  1278. for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++)
  1279. {
  1280. if (ip6_addr_isinvalid(netif_ip6_addr_state(netif, i)))
  1281. {
  1282. ip_ready = 1;
  1283. break;
  1284. }
  1285. }
  1286. #endif
  1287. if (!ip_ready)
  1288. {
  1289. #if LWIP_DHCP
  1290. if (netif->flags & NETIF_FLAG_ETHARP)
  1291. {
  1292. // dhcp_start(netif);
  1293. // if (!prvlwip.dhcp_timer_active)
  1294. // {
  1295. // prvlwip.dhcp_timer_active = 1;
  1296. // platform_start_timer(prvlwip.dhcp_timer, 500, 1);
  1297. // }
  1298. }
  1299. #endif
  1300. active_flag = 0;
  1301. }
  1302. if (prvlwip.netif_network_ready[adapter_index] != active_flag)
  1303. {
  1304. prvlwip.netif_network_ready[adapter_index] = active_flag;
  1305. if (!active_flag)
  1306. {
  1307. dns_clear(&prvlwip.dns_client);
  1308. prvlwip.dns_client.is_run = 0;
  1309. NET_DBG("network not ready");
  1310. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 0, adapter_index);
  1311. }
  1312. else
  1313. {
  1314. NET_DBG("network ready");
  1315. for(i = 0; i < MAX_DNS_SERVER; i++)
  1316. {
  1317. if (prvlwip.dns_client.dns_server[i].type != 0xff)
  1318. {
  1319. NET_DBG("DNS%d:%s",i, ipaddr_ntoa_r(&prvlwip.dns_client.dns_server[i], ip_string, sizeof(ip_string)));
  1320. }
  1321. }
  1322. net_lwip_callback_to_nw_task(adapter_index, EV_NW_STATE, 0, 1, adapter_index);
  1323. }
  1324. }
  1325. }
  1326. static int net_lwip_check_socket(void *user_data, int socket_id, uint64_t tag)
  1327. {
  1328. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1329. if (socket_id >= MAX_SOCK_NUM) return -1;
  1330. if (prvlwip.socket[socket_id].tag != tag) return -1;
  1331. if (!prvlwip.socket[socket_id].in_use || prvlwip.socket[socket_id].state) return -1;
  1332. return 0;
  1333. }
  1334. static int net_lwip_socket_check(int socket_id, uint64_t tag, void *user_data)
  1335. {
  1336. return net_lwip_check_socket(user_data, socket_id, tag);
  1337. }
  1338. static uint8_t net_lwip_check_ready(void *user_data)
  1339. {
  1340. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return 0;
  1341. return (prvlwip.netif_network_ready[(uint32_t)user_data]);
  1342. }
  1343. static void net_lwip_create_socket_now(uint8_t adapter_index, uint8_t socket_id)
  1344. {
  1345. PV_Union uPV;
  1346. // LLOGD("CALL net_lwip_create_socket_now");
  1347. uPV.u16[0] = socket_id;
  1348. uPV.u16[1] = adapter_index;
  1349. if (prvlwip.socket[socket_id].is_tcp)
  1350. {
  1351. prvlwip.socket[socket_id].pcb.tcp = tcp_new();
  1352. if (prvlwip.socket[socket_id].pcb.tcp)
  1353. {
  1354. // LLOGD("socket pcb %p", prvlwip.socket[socket_id].pcb.tcp);
  1355. // prvlwip.socket[socket_id].pcb.tcp->netif_idx = netif_get_index(prvlwip.lwip_netif[adapter_index]);
  1356. prvlwip.socket[socket_id].rx_wait_size = 0;
  1357. prvlwip.socket[socket_id].tx_wait_size = 0;
  1358. prvlwip.socket[socket_id].pcb.tcp->callback_arg = uPV.p;
  1359. prvlwip.socket[socket_id].pcb.tcp->recv = net_lwip_tcp_recv_cb;
  1360. prvlwip.socket[socket_id].pcb.tcp->sent = net_lwip_tcp_sent_cb;
  1361. prvlwip.socket[socket_id].pcb.tcp->errf = net_lwip_tcp_err_cb;
  1362. prvlwip.socket[socket_id].pcb.tcp->so_options |= SOF_KEEPALIVE|SOF_REUSEADDR;
  1363. // tcp_set_flags(prvlwip.socket[socket_id].pcb.tcp, TCP_NODELAY);
  1364. // if (adapter_index == NW_ADAPTER_INDEX_LWIP_GPRS)
  1365. // {
  1366. // prvlwip.socket[socket_id].pcb.tcp->more_delay = 6;
  1367. // }
  1368. }
  1369. else
  1370. {
  1371. NET_DBG("tcp pcb full!");
  1372. net_lwip_tcp_error(adapter_index, socket_id);
  1373. }
  1374. }
  1375. else
  1376. {
  1377. prvlwip.socket[socket_id].pcb.udp = udp_new();
  1378. if (prvlwip.socket[socket_id].pcb.udp)
  1379. {
  1380. // prvlwip.socket[socket_id].pcb.udp->netif_idx = netif_get_index(prvlwip.lwip_netif[adapter_index]);
  1381. prvlwip.socket[socket_id].pcb.udp->recv_arg = uPV.p;
  1382. prvlwip.socket[socket_id].pcb.udp->recv = net_lwip_udp_recv_cb;
  1383. prvlwip.socket[socket_id].pcb.udp->so_options |= SOF_BROADCAST|SOF_REUSEADDR;
  1384. }
  1385. else
  1386. {
  1387. NET_DBG("udp pcb full!");
  1388. net_lwip_tcp_error(adapter_index, socket_id);
  1389. }
  1390. }
  1391. }
  1392. static int net_lwip_create_socket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  1393. {
  1394. uint8_t index = (uint32_t)user_data;
  1395. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1396. int i, socket_id;
  1397. socket_id = -1;
  1398. OS_LOCK;
  1399. if (!prvlwip.socket[prvlwip.next_socket_index].in_use)
  1400. {
  1401. socket_id = prvlwip.next_socket_index;
  1402. prvlwip.next_socket_index++;
  1403. }
  1404. else
  1405. {
  1406. for (i = 0; i < MAX_SOCK_NUM; i++)
  1407. {
  1408. if (!prvlwip.socket[i].in_use)
  1409. {
  1410. socket_id = i;
  1411. prvlwip.next_socket_index = i + 1;
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. if (prvlwip.next_socket_index >= MAX_SOCK_NUM)
  1417. {
  1418. prvlwip.next_socket_index = 0;
  1419. }
  1420. if (socket_id >= 0)
  1421. {
  1422. LWIP_ASSERT("socket must free before create", !prvlwip.socket[socket_id].pcb.ip);
  1423. prvlwip.socket_tag++;
  1424. *tag = prvlwip.socket_tag;
  1425. prvlwip.socket[socket_id].in_use = 1;
  1426. prvlwip.socket[socket_id].tag = *tag;
  1427. prvlwip.socket[socket_id].param = param;
  1428. prvlwip.socket[socket_id].is_tcp = is_tcp;
  1429. llist_traversal(&prvlwip.socket[socket_id].wait_ack_head, net_lwip_del_data_cache, NULL);
  1430. llist_traversal(&prvlwip.socket[socket_id].tx_head, net_lwip_del_data_cache, NULL);
  1431. llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_del_data_cache, NULL);
  1432. OS_UNLOCK;
  1433. if (platform_get_current_task() == prvlwip.task_handle)
  1434. {
  1435. net_lwip_create_socket_now(index, socket_id);
  1436. return socket_id;
  1437. }
  1438. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CREATE, socket_id, 0, user_data);
  1439. }
  1440. else
  1441. {
  1442. OS_UNLOCK;
  1443. }
  1444. return socket_id;
  1445. }
  1446. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1447. 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)
  1448. {
  1449. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1450. if (result) return result;
  1451. prvlwip.socket[socket_id].local_port = local_port;
  1452. prvlwip.socket[socket_id].remote_port = remote_port;
  1453. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CONNECT, socket_id, remote_ip, user_data);
  1454. return 0;
  1455. }
  1456. //作为server绑定一个port,开始监听
  1457. static int net_lwip_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1458. {
  1459. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1460. if (result) return result;
  1461. prvlwip.socket[socket_id].local_port = local_port;
  1462. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_LISTEN, socket_id, local_port, user_data);
  1463. return 0;
  1464. }
  1465. //作为server接受一个client
  1466. 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)
  1467. {
  1468. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1469. if (result) return result;
  1470. *remote_ip = prvlwip.socket[socket_id].pcb.tcp->remote_ip;
  1471. *remote_port = prvlwip.socket[socket_id].pcb.tcp->remote_port;
  1472. return 0;
  1473. }
  1474. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1475. static int net_lwip_socket_disconnect(int socket_id, uint64_t tag, void *user_data)
  1476. {
  1477. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1478. if (result) return result;
  1479. prvlwip.socket[socket_id].state = 1;
  1480. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 1, user_data);
  1481. return 0;
  1482. }
  1483. static int net_lwip_socket_force_close(int socket_id, void *user_data)
  1484. {
  1485. if (prvlwip.socket[socket_id].in_use && !prvlwip.socket[socket_id].state)
  1486. {
  1487. prvlwip.socket[socket_id].state = 1;
  1488. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_CLOSE, socket_id, 0, user_data);
  1489. }
  1490. return 0;
  1491. }
  1492. static int net_lwip_socket_close(int socket_id, uint64_t tag, void *user_data)
  1493. {
  1494. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1495. if (socket_id >= MAX_SOCK_NUM) return -1;
  1496. if (prvlwip.socket[socket_id].tag != tag)
  1497. {
  1498. NET_DBG("socket %d used by other!", socket_id);
  1499. return -1;
  1500. }
  1501. if (!prvlwip.socket[socket_id].in_use) return 0;
  1502. net_lwip_socket_force_close(socket_id, user_data);
  1503. return 0;
  1504. }
  1505. 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)
  1506. {
  1507. uint32_t dummy_len;
  1508. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1509. memcpy(buf, p->data + p->read_pos, dummy_len);
  1510. p->read_pos += dummy_len;
  1511. if (p->read_pos >= p->len)
  1512. {
  1513. if (prvlwip.socket[socket_id].is_tcp)
  1514. {
  1515. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_RX_DONE, socket_id, p->len, 0);
  1516. }
  1517. llist_del(&p->node);
  1518. free(p->data);
  1519. free(p);
  1520. }
  1521. *read_len += dummy_len;
  1522. return dummy_len;
  1523. }
  1524. 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)
  1525. {
  1526. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1527. if (result) return result;
  1528. uint32_t read_len = 0;
  1529. if (buf)
  1530. {
  1531. SOCKET_LOCK(socket_id);
  1532. socket_data_t *p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1533. if (prvlwip.socket[socket_id].is_tcp)
  1534. {
  1535. while((read_len < len) && p)
  1536. {
  1537. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1538. p = (socket_data_t *)llist_traversal(&prvlwip.socket[socket_id].rx_head, net_lwip_next_data_cache, &prvlwip.socket[socket_id]);
  1539. }
  1540. }
  1541. else
  1542. {
  1543. prvlwip.socket[socket_id].rx_wait_size -= net_lwip_socket_read_data(socket_id, buf + read_len, &read_len, len, p);
  1544. }
  1545. if (llist_empty(&prvlwip.socket[socket_id].rx_head))
  1546. {
  1547. prvlwip.socket[socket_id].rx_wait_size = 0;
  1548. }
  1549. SOCKET_UNLOCK(socket_id);
  1550. }
  1551. else
  1552. {
  1553. read_len = prvlwip.socket[socket_id].rx_wait_size;
  1554. }
  1555. return read_len;
  1556. }
  1557. 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)
  1558. {
  1559. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1560. if (result) return result;
  1561. SOCKET_LOCK(socket_id);
  1562. uint32_t save_len = 0;
  1563. uint32_t dummy_len = 0;
  1564. socket_data_t *p;
  1565. if (prvlwip.socket[socket_id].is_tcp)
  1566. {
  1567. while(save_len < len)
  1568. {
  1569. dummy_len = ((len - save_len) > SOCKET_BUF_LEN)?SOCKET_BUF_LEN:(len - save_len);
  1570. p = net_lwip_create_data_node(socket_id, &buf[save_len], dummy_len, remote_ip, remote_port);
  1571. if (p)
  1572. {
  1573. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1574. }
  1575. else
  1576. {
  1577. SOCKET_UNLOCK(socket_id);
  1578. return -1;
  1579. }
  1580. save_len += dummy_len;
  1581. }
  1582. }
  1583. else
  1584. {
  1585. p = net_lwip_create_data_node(socket_id, buf, len, remote_ip, remote_port);
  1586. if (p)
  1587. {
  1588. llist_add_tail(&p->node, &prvlwip.socket[socket_id].tx_head);
  1589. }
  1590. else
  1591. {
  1592. SOCKET_UNLOCK(socket_id);
  1593. return -1;
  1594. }
  1595. }
  1596. SOCKET_UNLOCK(socket_id);
  1597. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_TX, socket_id, 0, user_data);
  1598. result = len;
  1599. return result;
  1600. }
  1601. void net_lwip_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1602. {
  1603. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  1604. int socket_list[MAX_SOCK_NUM];
  1605. memset(socket_list, 0, sizeof(socket_list));
  1606. uint32_t i;
  1607. for(i = 0; i < num; i++)
  1608. {
  1609. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1610. {
  1611. socket_list[vaild_socket_list[i]] = 1;
  1612. }
  1613. NET_DBG("%d,%d",i,vaild_socket_list[i]);
  1614. }
  1615. for(i = 0; i < MAX_SOCK_NUM; i++)
  1616. {
  1617. NET_DBG("%d,%d",i,socket_list[i]);
  1618. if ( !socket_list[i] )
  1619. {
  1620. net_lwip_socket_force_close(i, user_data);
  1621. }
  1622. }
  1623. }
  1624. static int net_lwip_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data)
  1625. {
  1626. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1627. if (result) return result;
  1628. for(int i = 0; i < 10; i++)
  1629. {
  1630. if (!prvlwip.socket[socket_id].pcb.ip)
  1631. {
  1632. platform_task_sleep(1);
  1633. }
  1634. else
  1635. {
  1636. break;
  1637. }
  1638. }
  1639. if (!prvlwip.socket[socket_id].pcb.ip)
  1640. {
  1641. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1642. return -1;
  1643. }
  1644. switch (level)
  1645. {
  1646. /* Level: SOL_SOCKET */
  1647. case SOL_SOCKET:
  1648. switch (optname)
  1649. {
  1650. #if LWIP_TCP
  1651. case SO_ACCEPTCONN:
  1652. if (!prvlwip.socket[socket_id].is_tcp) {
  1653. return -1;
  1654. }
  1655. if ((prvlwip.socket[socket_id].pcb.tcp != NULL) && (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN)) {
  1656. *(int *)optval = 1;
  1657. } else {
  1658. *(int *)optval = 0;
  1659. }
  1660. break;
  1661. #endif /* LWIP_TCP */
  1662. /* The option flags */
  1663. case SO_BROADCAST:
  1664. case SO_KEEPALIVE:
  1665. #if SO_REUSE
  1666. case SO_REUSEADDR:
  1667. #endif /* SO_REUSE */
  1668. if ((optname == SO_BROADCAST) && prvlwip.socket[socket_id].is_tcp) {
  1669. return -1;
  1670. }
  1671. optname = lwip_sockopt_to_ipopt(optname);
  1672. *(int *)optval = ip_get_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1673. break;
  1674. case SO_TYPE:
  1675. if (prvlwip.socket[socket_id].is_tcp)
  1676. {
  1677. *(int *)optval = SOCK_STREAM;
  1678. }
  1679. else
  1680. {
  1681. *(int *)optval = SOCK_DGRAM;
  1682. }
  1683. break;
  1684. case SO_ERROR:
  1685. return -1;
  1686. break;
  1687. #if LWIP_UDP
  1688. case SO_NO_CHECK:
  1689. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1690. #if LWIP_UDPLITE
  1691. if (udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_UDPLITE)) {
  1692. /* this flag is only available for UDP, not for UDP lite */
  1693. done_socket(sock);
  1694. return -1;
  1695. }
  1696. #endif /* LWIP_UDPLITE */
  1697. *(int *)optval = udp_is_flag_set(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM) ? 1 : 0;
  1698. break;
  1699. #endif /* LWIP_UDP*/
  1700. default:
  1701. return -1;
  1702. break;
  1703. } /* switch (optname) */
  1704. break;
  1705. /* Level: IPPROTO_IP */
  1706. case IPPROTO_IP:
  1707. switch (optname) {
  1708. case IP_TTL:
  1709. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->ttl;
  1710. break;
  1711. case IP_TOS:
  1712. *(int *)optval = prvlwip.socket[socket_id].pcb.ip->tos;
  1713. break;
  1714. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1715. case IP_MULTICAST_TTL:
  1716. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1717. *(u8_t *)optval = udp_get_multicast_ttl(prvlwip.socket[socket_id].pcb.udp);
  1718. break;
  1719. case IP_MULTICAST_IF:
  1720. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1721. ((struct in_addr *)optval)->s_addr = prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr;
  1722. break;
  1723. case IP_MULTICAST_LOOP:
  1724. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1725. if ((prvlwip.socket[socket_id].pcb.udp->flags & UDP_FLAGS_MULTICAST_LOOP) != 0) {
  1726. *(u8_t *)optval = 1;
  1727. } else {
  1728. *(u8_t *)optval = 0;
  1729. }
  1730. break;
  1731. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1732. default:
  1733. return -1;
  1734. } /* switch (optname) */
  1735. break;
  1736. #if LWIP_TCP
  1737. /* Level: IPPROTO_TCP */
  1738. case IPPROTO_TCP:
  1739. /* Special case: all IPPROTO_TCP option take an int */
  1740. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1741. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1742. return -1;
  1743. }
  1744. switch (optname) {
  1745. case TCP_NODELAY:
  1746. *(int *)optval = tcp_nagle_disabled(prvlwip.socket[socket_id].pcb.tcp);
  1747. break;
  1748. case TCP_KEEPALIVE:
  1749. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_idle;
  1750. break;
  1751. #if LWIP_TCP_KEEPALIVE
  1752. case TCP_KEEPIDLE:
  1753. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_idle / 1000);
  1754. break;
  1755. case TCP_KEEPINTVL:
  1756. *(int *)optval = (int)(prvlwip.socket[socket_id].pcb.tcp->keep_intvl / 1000);
  1757. break;
  1758. case TCP_KEEPCNT:
  1759. *(int *)optval = (int)prvlwip.socket[socket_id].pcb.tcp->keep_cnt;
  1760. break;
  1761. #endif /* LWIP_TCP_KEEPALIVE */
  1762. default:
  1763. return -1;
  1764. break;
  1765. } /* switch (optname) */
  1766. break;
  1767. #endif /* LWIP_TCP */
  1768. #if LWIP_UDP && LWIP_UDPLITE
  1769. /* Level: IPPROTO_UDPLITE */
  1770. case IPPROTO_UDPLITE:
  1771. /* Special case: all IPPROTO_UDPLITE option take an int */
  1772. LWIP_SOCKOPT_CHECK_OPTLEN_CONN_PCB(sock, *optlen, int);
  1773. /* If this is no UDP lite socket, ignore any options. */
  1774. if (!NETCONNTYPE_ISUDPLITE(netconn_type(sock->conn))) {
  1775. done_socket(sock);
  1776. return ENOPROTOOPT;
  1777. }
  1778. switch (optname) {
  1779. case UDPLITE_SEND_CSCOV:
  1780. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_tx;
  1781. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_SEND_CSCOV) = %d\n",
  1782. s, (*(int *)optval)) );
  1783. break;
  1784. case UDPLITE_RECV_CSCOV:
  1785. *(int *)optval = prvlwip.socket[socket_id].pcb.udp->chksum_len_rx;
  1786. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UDPLITE_RECV_CSCOV) = %d\n",
  1787. s, (*(int *)optval)) );
  1788. break;
  1789. default:
  1790. LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n",
  1791. s, optname));
  1792. err = ENOPROTOOPT;
  1793. break;
  1794. } /* switch (optname) */
  1795. break;
  1796. #endif /* LWIP_UDP */
  1797. /* Level: IPPROTO_RAW */
  1798. case IPPROTO_RAW:
  1799. return -1;
  1800. default:
  1801. return -1;
  1802. break;
  1803. } /* switch (level) */
  1804. return 0;
  1805. }
  1806. static int net_lwip_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data)
  1807. {
  1808. int result = net_lwip_check_socket(user_data, socket_id, tag);
  1809. if (result) return result;
  1810. for(int i = 0; i < 10; i++)
  1811. {
  1812. if (!prvlwip.socket[socket_id].pcb.ip)
  1813. {
  1814. platform_task_sleep(1);
  1815. }
  1816. else
  1817. {
  1818. break;
  1819. }
  1820. }
  1821. if (!prvlwip.socket[socket_id].pcb.ip)
  1822. {
  1823. NET_DBG("socket %d not create try later! %d, %d, %x, %d", socket_id, level, optname, optval, optlen);
  1824. return -1;
  1825. }
  1826. switch (level)
  1827. {
  1828. /* Level: SOL_SOCKET */
  1829. case SOL_SOCKET:
  1830. switch (optname)
  1831. {
  1832. /* SO_ACCEPTCONN is get-only */
  1833. /* The option flags */
  1834. case SO_BROADCAST:
  1835. case SO_KEEPALIVE:
  1836. #if SO_REUSE
  1837. case SO_REUSEADDR:
  1838. #endif /* SO_REUSE */
  1839. if ((optname == SO_BROADCAST) &&
  1840. (prvlwip.socket[socket_id].is_tcp)) {
  1841. return -1;
  1842. }
  1843. optname = lwip_sockopt_to_ipopt(optname);
  1844. if (*(const int *)optval) {
  1845. ip_set_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1846. } else {
  1847. ip_reset_option(prvlwip.socket[socket_id].pcb.ip, optname);
  1848. }
  1849. break;
  1850. #if LWIP_UDP
  1851. case SO_NO_CHECK:
  1852. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1853. if (*(const int *)optval) {
  1854. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1855. } else {
  1856. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_NOCHKSUM);
  1857. }
  1858. break;
  1859. #endif /* LWIP_UDP */
  1860. default:
  1861. return -1;
  1862. } /* switch (optname) */
  1863. break;
  1864. /* Level: IPPROTO_IP */
  1865. case IPPROTO_IP:
  1866. switch (optname)
  1867. {
  1868. case IP_TTL:
  1869. prvlwip.socket[socket_id].pcb.ip->ttl = (u8_t)(*(const int *)optval);
  1870. break;
  1871. case IP_TOS:
  1872. prvlwip.socket[socket_id].pcb.ip->tos = (u8_t)(*(const int *)optval);
  1873. break;
  1874. #if LWIP_NETBUF_RECVINFO
  1875. case IP_PKTINFO:
  1876. if (*(const int *)optval) {
  1877. prvlwip.socket[socket_id].flags |= NETCONN_FLAG_PKTINFO;
  1878. } else {
  1879. prvlwip.socket[socket_id].flags &= ~NETCONN_FLAG_PKTINFO;
  1880. }
  1881. break;
  1882. #endif /* LWIP_NETBUF_RECVINFO */
  1883. #if LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP
  1884. case IP_MULTICAST_TTL:
  1885. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1886. udp_set_multicast_ttl(prvlwip.socket[socket_id].pcb.udp, (u8_t)(*(const u8_t *)optval));
  1887. break;
  1888. case IP_MULTICAST_IF:
  1889. if (prvlwip.socket[socket_id].is_tcp) return -1;
  1890. prvlwip.socket[socket_id].pcb.udp->mcast_ip4.addr = ((struct in_addr *)optval)->s_addr;
  1891. break;
  1892. case IP_MULTICAST_LOOP:
  1893. if (*(const u8_t *)optval) {
  1894. udp_set_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1895. } else {
  1896. udp_clear_flags(prvlwip.socket[socket_id].pcb.udp, UDP_FLAGS_MULTICAST_LOOP);
  1897. }
  1898. break;
  1899. #endif /* LWIP_IPV4 && LWIP_MULTICAST_TX_OPTIONS && LWIP_UDP */
  1900. default:
  1901. return -1;
  1902. break;
  1903. } /* switch (optname) */
  1904. break;
  1905. #if LWIP_TCP
  1906. /* Level: IPPROTO_TCP */
  1907. case IPPROTO_TCP:
  1908. /* Special case: all IPPROTO_TCP option take an int */
  1909. if (!prvlwip.socket[socket_id].is_tcp) return -1;
  1910. if (prvlwip.socket[socket_id].pcb.tcp->state == LISTEN) {
  1911. return -1;
  1912. }
  1913. switch (optname)
  1914. {
  1915. case TCP_NODELAY:
  1916. if (*(const int *)optval) {
  1917. tcp_nagle_disable(prvlwip.socket[socket_id].pcb.tcp);
  1918. } else {
  1919. tcp_nagle_enable(prvlwip.socket[socket_id].pcb.tcp);
  1920. }
  1921. break;
  1922. case TCP_KEEPALIVE:
  1923. prvlwip.socket[socket_id].pcb.tcp->keep_idle = (u32_t)(*(const int *)optval);
  1924. break;
  1925. #if LWIP_TCP_KEEPALIVE
  1926. case TCP_KEEPIDLE:
  1927. prvlwip.socket[socket_id].pcb.tcp->keep_idle = 1000 * (u32_t)(*(const int *)optval);
  1928. break;
  1929. case TCP_KEEPINTVL:
  1930. prvlwip.socket[socket_id].pcb.tcp->keep_intvl = 1000 * (u32_t)(*(const int *)optval);
  1931. break;
  1932. case TCP_KEEPCNT:
  1933. prvlwip.socket[socket_id].pcb.tcp->keep_cnt = (u32_t)(*(const int *)optval);
  1934. break;
  1935. #endif /* LWIP_TCP_KEEPALIVE */
  1936. default:
  1937. return -1;
  1938. break;
  1939. } /* switch (optname) */
  1940. break;
  1941. #endif /* LWIP_TCP*/
  1942. #if LWIP_IPV6
  1943. /* Level: IPPROTO_IPV6 */
  1944. case IPPROTO_IPV6:
  1945. return -1;
  1946. break;
  1947. #endif /* LWIP_IPV6 */
  1948. /* Level: IPPROTO_RAW */
  1949. case IPPROTO_RAW:
  1950. return -1;
  1951. break;
  1952. default:
  1953. return -1;
  1954. } /* switch (level) */
  1955. return 0;
  1956. }
  1957. 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)
  1958. {
  1959. uint8_t index = (uint32_t)user_data;
  1960. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1961. if (!prvlwip.lwip_netif[index]) return -1;
  1962. *ip = prvlwip.lwip_netif[index]->ip_addr;
  1963. *submask = prvlwip.lwip_netif[index]->netmask;
  1964. *gateway = prvlwip.lwip_netif[index]->gw;
  1965. return 0;
  1966. }
  1967. static int net_lwip_get_full_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1968. {
  1969. uint8_t index = (uint32_t)user_data;
  1970. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1971. if (!prvlwip.lwip_netif) return -1;
  1972. // *ip = prvlwip.lwip_netif->ip_addr;
  1973. // *submask = prvlwip.lwip_netif->netmask;
  1974. // *gateway = prvlwip.lwip_netif->gw;
  1975. luat_ip_addr_t *local_ip = net_lwip_get_ip6();
  1976. if (local_ip)
  1977. {
  1978. *ipv6 = *local_ip;
  1979. }
  1980. else
  1981. {
  1982. ipv6->type = 0xff;
  1983. }
  1984. return 0;
  1985. }
  1986. static int net_lwip_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1987. {
  1988. return 0;
  1989. }
  1990. static int net_lwip_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1991. {
  1992. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  1993. char *prv_domain_name = (char *)zalloc(len + 1);
  1994. memcpy(prv_domain_name, domain_name, len);
  1995. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS, prv_domain_name, param, user_data);
  1996. return 0;
  1997. }
  1998. static int net_lwip_dns_ipv6(const char *domain_name, uint32_t len, void *param, void *user_data)
  1999. {
  2000. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  2001. char *prv_domain_name = (char *)zalloc(len + 1);
  2002. memcpy(prv_domain_name, domain_name, len);
  2003. platform_send_event(prvlwip.task_handle, EV_LWIP_SOCKET_DNS_IPV6, prv_domain_name, param, user_data);
  2004. return 0;
  2005. }
  2006. static int net_lwip_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  2007. {
  2008. if ((uint32_t)user_data >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  2009. if (server_index >= MAX_DNS_SERVER) return -1;
  2010. prvlwip.dns_client.dns_server[server_index] = *ip;
  2011. prvlwip.dns_client.is_static_dns[server_index] = 1;
  2012. return 0;
  2013. }
  2014. static int net_lwip_set_mac(uint8_t *mac, void *user_data)
  2015. {
  2016. uint8_t index = (uint32_t)user_data;
  2017. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  2018. if (!prvlwip.lwip_netif[index]) return -1;
  2019. memcpy(prvlwip.lwip_netif[index]->hwaddr, mac, 6);
  2020. return -1;
  2021. }
  2022. 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)
  2023. {
  2024. uint8_t index = (uint32_t)user_data;
  2025. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return -1;
  2026. if (!prvlwip.lwip_netif[index]) return -1;
  2027. luat_ip_addr_t *p_ip = zalloc(sizeof(luat_ip_addr_t) * 5);
  2028. p_ip[0] = ip?(*ip):ip_addr_any_type;
  2029. p_ip[1] = submask?(*submask):ip_addr_any_type;
  2030. p_ip[2] = gateway?(*gateway):ip_addr_any_type;
  2031. p_ip[3] = ipv6?(*ipv6):ip_addr_any_type;
  2032. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_SET_IP, p_ip, ipv6, user_data);
  2033. return 0;
  2034. }
  2035. static int32_t net_lwip_dummy_callback(void *pData, void *pParam)
  2036. {
  2037. return 0;
  2038. }
  2039. static void net_lwip_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  2040. {
  2041. uint8_t index = (uint32_t)user_data;
  2042. if (index >= NW_ADAPTER_INDEX_LWIP_NETIF_QTY) return;
  2043. prvlwip.socket_cb[index] = cb_fun?cb_fun:net_lwip_dummy_callback;
  2044. prvlwip.user_data[index] = param;
  2045. }
  2046. static network_adapter_info prv_net_lwip_adapter =
  2047. {
  2048. .check_ready = net_lwip_check_ready,
  2049. .create_soceket = net_lwip_create_socket,
  2050. .socket_connect = net_lwip_socket_connect,
  2051. .socket_listen = net_lwip_socket_listen,
  2052. .socket_accept = net_lwip_socket_accept,
  2053. .socket_disconnect = net_lwip_socket_disconnect,
  2054. .socket_close = net_lwip_socket_close,
  2055. .socket_force_close = net_lwip_socket_force_close,
  2056. .socket_receive = net_lwip_socket_receive,
  2057. .socket_send = net_lwip_socket_send,
  2058. .socket_check = net_lwip_socket_check,
  2059. .socket_clean = net_lwip_socket_clean,
  2060. .getsockopt = net_lwip_getsockopt,
  2061. .setsockopt = net_lwip_setsockopt,
  2062. .user_cmd = net_lwip_user_cmd,
  2063. .dns = net_lwip_dns,
  2064. .dns_ipv6 = net_lwip_dns_ipv6,
  2065. .set_dns_server = net_lwip_set_dns_server,
  2066. .set_mac = net_lwip_set_mac,
  2067. .set_static_ip = net_lwip_set_static_ip,
  2068. .get_local_ip_info = net_lwip_get_local_ip_info,
  2069. .get_full_ip_info = net_lwip_get_full_ip_info,
  2070. .socket_set_callback = net_lwip_socket_set_callback,
  2071. .name = "lwip",
  2072. .max_socket_num = MAX_SOCK_NUM,
  2073. .no_accept = 1,
  2074. .is_posix = 1,
  2075. };
  2076. void net_lwip_register_adapter(uint8_t adapter_index)
  2077. {
  2078. network_register_adapter(adapter_index, &prv_net_lwip_adapter, adapter_index);
  2079. }
  2080. int net_lwip_check_all_ack(int socket_id)
  2081. {
  2082. if (!llist_empty(&prvlwip.socket[socket_id].wait_ack_head))
  2083. {
  2084. NET_DBG("socekt %d not all ack", socket_id);
  2085. return -1;
  2086. }
  2087. if (!llist_empty(&prvlwip.socket[socket_id].tx_head))
  2088. {
  2089. NET_DBG("socekt %d not all send", socket_id);
  2090. return -1;
  2091. }
  2092. if (prvlwip.socket[socket_id].pcb.tcp->snd_buf != TCP_SND_BUF)
  2093. {
  2094. NET_DBG("socket %d send buf %ubytes, need %u",socket_id, prvlwip.socket[socket_id].pcb.tcp->snd_buf, TCP_SND_BUF);
  2095. }
  2096. return 0;
  2097. }
  2098. void net_lwip_set_netif(uint8_t adapter_index, struct netif *netif, void *init, uint8_t is_default)
  2099. {
  2100. // if (!netif)
  2101. // {
  2102. // netif = zalloc(sizeof(struct netif));
  2103. // }
  2104. prvlwip.lwip_netif[adapter_index] = netif;
  2105. // netif_add(netif, NULL, NULL, NULL, NULL, init, netif_input);
  2106. // netif->dhcp_done_callback = net_lwip_dhcp_done_cb;
  2107. // netif->dhcp_done_arg = (void *)adapter_index;
  2108. // switch(adapter_index)
  2109. // {
  2110. // case NW_ADAPTER_INDEX_LWIP_WIFI_STA:
  2111. // case NW_ADAPTER_INDEX_LWIP_WIFI_AP:
  2112. // case NW_ADAPTER_INDEX_LWIP_ETH:
  2113. // prvlwip.lwip_netif[adapter_index]->flags |= (NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET|NETIF_FLAG_BROADCAST);
  2114. // break;
  2115. // }
  2116. // if (is_default)
  2117. // {
  2118. prvlwip.dns_udp->netif_idx = netif_get_index(netif);
  2119. // netif_set_default(netif);
  2120. prvlwip.dns_adapter_index = adapter_index;
  2121. // }
  2122. }
  2123. struct netif * net_lwip_get_netif(uint8_t adapter_index)
  2124. {
  2125. return prvlwip.lwip_netif[adapter_index];
  2126. }
  2127. // void net_lwip_input_packets(struct netif *netif, struct pbuf *p)
  2128. // {
  2129. // platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_INPUT, p, 0, netif);
  2130. // }
  2131. void net_lwip_set_link_state(uint8_t adapter_index, uint8_t onoff)
  2132. {
  2133. platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_LINK_STATE, onoff, 0, adapter_index);
  2134. }
  2135. // void net_lwip_set_ipv6_by_mac(uint8_t adapter_index)
  2136. // {
  2137. // platform_send_event(prvlwip.task_handle, EV_LWIP_NETIF_IPV6_BY_MAC, 0, 0, adapter_index);
  2138. // }