luat_lwip_air101.c 69 KB

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