w5500.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947
  1. #include "platform_def.h"
  2. #include "luat_base.h"
  3. #include "luat_spi.h"
  4. #ifdef LUAT_USE_W5500
  5. #include "luat_rtos.h"
  6. #include "luat_zbuff.h"
  7. #include "luat_gpio.h"
  8. #include "w5500_def.h"
  9. #include "luat_crypto.h"
  10. #include "luat_mcu.h"
  11. #include "luat_timer.h"
  12. #include "luat_malloc.h"
  13. #include "luat_network_adapter.h"
  14. //extern void DBG_Printf(const char* format, ...);
  15. //extern void DBG_HexPrintf(void *Data, unsigned int len);
  16. //#if 0
  17. //#define LLOGD(x,y...)
  18. //#define LLOGE(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  19. //#else
  20. //#define LLOGD(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  21. //#define LLOGE(x,y...) DBG_Printf("%s %d:"x"\r\n", __FUNCTION__,__LINE__,##y)
  22. //#endif
  23. #define LUAT_LOG_TAG "W5500"
  24. #include "luat_log.h"
  25. #define socket_index(n) (n << 5)
  26. #define common_reg (0)
  27. #define socket_reg (1 << 3)
  28. #define socket_tx (2 << 3)
  29. #define socket_rx (3 << 3)
  30. #define is_write (1 << 2)
  31. #define MAX_SOCK_NUM 8
  32. #define SYS_SOCK_ID 0
  33. #define MR_RST 0x80 /**< reset */
  34. #define MR_WOL 0x20 /**< Wake on Lan */
  35. #define MR_PB 0x10 /**< ping block */
  36. #define MR_PPPOE 0x08 /**< enable pppoe */
  37. #define MR_UDP_FARP 0x02 /**< enbale FORCE ARP */
  38. #define IR_CONFLICT 0x80 /**< check ip confict */
  39. #define IR_UNREACH 0x40 /**< get the destination unreachable message in UDP sending */
  40. #define IR_PPPoE 0x20 /**< get the PPPoE close message */
  41. #define IR_MAGIC 0x10 /**< get the magic packet interrupt */
  42. #define Sn_MR_CLOSE 0x00 /**< unused socket */
  43. #define Sn_MR_TCP 0x01 /**< TCP */
  44. #define Sn_MR_UDP 0x02 /**< UDP */
  45. #define Sn_MR_IPRAW 0x03 /**< IP LAYER RAW SOCK */
  46. #define Sn_MR_MACRAW 0x04 /**< MAC LAYER RAW SOCK */
  47. #define Sn_MR_PPPOE 0x05 /**< PPPoE */
  48. #define Sn_MR_UCASTB 0x10 /**< Unicast Block in UDP Multicating*/
  49. #define Sn_MR_ND 0x20 /**< No Delayed Ack(TCP) flag */
  50. #define Sn_MR_MC 0x20 /**< Multicast IGMP (UDP) flag */
  51. #define Sn_MR_BCASTB 0x40 /**< Broadcast blcok in UDP Multicating */
  52. #define Sn_MR_MULTI 0x80 /**< support UDP Multicating */
  53. #define Sn_MR_MIP6N 0x10 /**< IPv6 packet Block */
  54. #define Sn_MR_MMB 0x20 /**< IPv4 Multicasting Block */
  55. //#define Sn_MR_BCASTB 0x40 /**< Broadcast blcok */
  56. #define Sn_MR_MFEN 0x80 /**< support MAC filter enable */
  57. #define Sn_CR_OPEN 0x01 /**< initialize or open socket */
  58. #define Sn_CR_LISTEN 0x02 /**< wait connection request in tcp mode(Server mode) */
  59. #define Sn_CR_CONNECT 0x04 /**< send connection request in tcp mode(Client mode) */
  60. #define Sn_CR_DISCON 0x08 /**< send closing reqeuset in tcp mode */
  61. #define Sn_CR_CLOSE 0x10 /**< close socket */
  62. #define Sn_CR_SEND 0x20 /**< update txbuf pointer, send data */
  63. #define Sn_CR_SEND_MAC 0x21 /**< send data with MAC address, so without ARP process */
  64. #define Sn_CR_SEND_KEEP 0x22 /**< send keep alive message */
  65. #define Sn_CR_RECV 0x40 /**< update rxbuf pointer, recv data */
  66. #define Sn_IR_SEND_OK 0x10 /**< complete sending */
  67. #define Sn_IR_TIMEOUT 0x08 /**< assert timeout */
  68. #define Sn_IR_RECV 0x04 /**< receiving data */
  69. #define Sn_IR_DISCON 0x02 /**< closed socket */
  70. #define Sn_IR_CON 0x01 /**< established connection */
  71. #define SOCK_CLOSED 0x00 /**< closed */
  72. #define SOCK_INIT 0x13 /**< init state */
  73. #define SOCK_LISTEN 0x14 /**< listen state */
  74. #define SOCK_SYNSENT 0x15 /**< connection state */
  75. #define SOCK_SYNRECV 0x16 /**< connection state */
  76. #define SOCK_ESTABLISHED 0x17 /**< success to connect */
  77. #define SOCK_FIN_WAIT 0x18 /**< closing state */
  78. #define SOCK_CLOSING 0x1A /**< closing state */
  79. #define SOCK_TIME_WAIT 0x1B /**< closing state */
  80. #define SOCK_CLOSE_WAIT 0x1C /**< closing state */
  81. #define SOCK_LAST_ACK 0x1D /**< closing state */
  82. #define SOCK_UDP 0x22 /**< udp socket */
  83. #define SOCK_IPRAW 0x32 /**< ip raw mode socket */
  84. #define SOCK_MACRAW 0x42 /**< mac raw mode socket */
  85. #define SOCK_PPPOE 0x5F /**< pppoe socket */
  86. #define SOCK_BUF_LEN (20 * 1024)
  87. #include "dhcp_def.h"
  88. #include "dns_def.h"
  89. #define W5500_LOCK platform_lock_mutex(prv_w5500_ctrl->Sem)
  90. #define W5500_UNLOCK platform_unlock_mutex(prv_w5500_ctrl->Sem)
  91. enum
  92. {
  93. W5500_COMMON_MR,
  94. W5500_COMMON_GAR0,
  95. W5500_COMMON_SUBR0 = 5,
  96. W5500_COMMON_MAC0 = 9,
  97. W5500_COMMON_IP0 = 0x0f,
  98. W5500_COMMON_INTLEVEL0 = 0x13,
  99. W5500_COMMON_IR = 0x15,
  100. W5500_COMMON_IMR,
  101. W5500_COMMON_SOCKET_IR,
  102. W5500_COMMON_SOCKET_IMR,
  103. W5500_COMMON_SOCKET_RTR0,
  104. W5500_COMMON_SOCKET_RCR = 0x1b,
  105. W5500_COMMON_PPP,
  106. W5500_COMMON_UIPR0 = 0x28,
  107. W5500_COMMON_UPORT0 = 0x2c,
  108. W5500_COMMON_PHY = 0x2e,
  109. W5500_COMMON_VERSIONR = 0x39,
  110. W5500_COMMON_QTY,
  111. W5500_SOCKET_MR = 0,
  112. W5500_SOCKET_CR,
  113. W5500_SOCKET_IR,
  114. W5500_SOCKET_SR,
  115. W5500_SOCKET_SOURCE_PORT0,
  116. W5500_SOCKET_DEST_MAC0 = 0x06,
  117. W5500_SOCKET_DEST_IP0 = 0x0C,
  118. W5500_SOCKET_DEST_PORT0 = 0x10,
  119. W5500_SOCKET_SEGMENT0 = 0x12,
  120. W5500_SOCKET_TOS = 0x15,
  121. W5500_SOCKET_TTL,
  122. W5500_SOCKET_RX_MEM_SIZE = 0x1e,
  123. W5500_SOCKET_TX_MEM_SIZE,
  124. W5500_SOCKET_TX_FREE_SIZE0 = 0x20,
  125. W5500_SOCKET_TX_READ_POINT0 = 0x22,
  126. W5500_SOCKET_TX_WRITE_POINT0 = 0x24,
  127. W5500_SOCKET_RX_SIZE0 = 0x26,
  128. W5500_SOCKET_RX_READ_POINT0 = 0x28,
  129. W5500_SOCKET_RX_WRITE_POINT0 = 0x2A,
  130. W5500_SOCKET_IMR = 0x2C,
  131. W5500_SOCKET_IP_HEAD_FRAG_VALUE0,
  132. W5500_SOCKET_KEEP_TIME = 0x2f,
  133. W5500_SOCKET_QTY,
  134. EV_W5500_IRQ = USER_EVENT_ID_START + 1,
  135. EV_W5500_LINK,
  136. //以下event的顺序不能变,新增event请在上方添加
  137. EV_W5500_SOCKET_TX,
  138. EV_W5500_SOCKET_CONNECT,
  139. EV_W5500_SOCKET_CLOSE,
  140. EV_W5500_SOCKET_LISTEN,
  141. EV_W5500_SOCKET_DNS,
  142. EV_W5500_RE_INIT,
  143. EV_W5500_REG_OP,
  144. W5500_SOCKET_OFFLINE = 0,
  145. W5500_SOCKET_CONFIG,
  146. W5500_SOCKET_CONNECT,
  147. W5500_SOCKET_ONLINE,
  148. W5500_SOCKET_CLOSING,
  149. };
  150. typedef struct
  151. {
  152. llist_head node;
  153. uint64_t tag; //考虑到socket复用的问题,必须有tag来做比对
  154. luat_ip_addr_t ip;
  155. uint8_t *data;
  156. uint32_t read_pos;
  157. uint32_t len;
  158. uint16_t port;
  159. uint8_t is_sending;
  160. }socket_data_t;
  161. typedef struct
  162. {
  163. socket_ctrl_t socket[MAX_SOCK_NUM];
  164. dhcp_client_info_t dhcp_client;
  165. dns_client_t dns_client;
  166. uint64_t last_tx_time;
  167. uint64_t tag;
  168. CBFuncEx_t socket_cb;
  169. void *user_data;
  170. void *task_handle;
  171. HANDLE Sem;
  172. uint32_t static_ip; //大端格式存放
  173. uint32_t static_submask; //大端格式存放
  174. uint32_t static_gateway; //大端格式存放
  175. uint16_t RTR;
  176. uint8_t RCR;
  177. uint8_t force_arp;
  178. uint8_t auto_speed;
  179. uint8_t spi_id;
  180. uint8_t cs_pin;
  181. uint8_t rst_pin;
  182. uint8_t irq_pin;
  183. uint8_t link_pin;
  184. uint8_t speed_status;
  185. uint8_t link_ready;
  186. uint8_t ip_ready;
  187. uint8_t network_ready;
  188. uint8_t inter_error;
  189. uint8_t device_on;
  190. uint8_t last_udp_send_ok;
  191. uint8_t rx_buf[2048 + 8];
  192. uint8_t tx_buf[2048 + 8];
  193. uint8_t mac[6];
  194. uint8_t next_socket_index;
  195. }w5500_ctrl_t;
  196. static w5500_ctrl_t *prv_w5500_ctrl;
  197. static void w5500_ip_state(w5500_ctrl_t *w5500, uint8_t check_state);
  198. static void w5500_check_dhcp(w5500_ctrl_t *w5500);
  199. static void w5500_init_reg(w5500_ctrl_t *w5500);
  200. static int w5500_del_data_cache(void *p, void *u)
  201. {
  202. socket_data_t *pdata = (socket_data_t *)p;
  203. free(pdata->data);
  204. return LIST_DEL;
  205. }
  206. static int w5500_next_data_cache(void *p, void *u)
  207. {
  208. socket_ctrl_t *socket = (socket_ctrl_t *)u;
  209. socket_data_t *pdata = (socket_data_t *)p;
  210. if (socket->tag != pdata->tag)
  211. {
  212. LLOGD("tag error");
  213. free(pdata->data);
  214. return LIST_DEL;
  215. }
  216. return LIST_FIND;
  217. }
  218. static int32_t w5500_irq(int pin, void *args)
  219. {
  220. w5500_ctrl_t *w5500 = (w5500_ctrl_t *)args;
  221. if ((pin & 0x00ff) == w5500->irq_pin)
  222. {
  223. platform_send_event(w5500->task_handle, EV_W5500_IRQ, 0, 0, 0);
  224. }
  225. if ((pin & 0x00ff) == w5500->link_pin)
  226. {
  227. platform_send_event(w5500->task_handle, EV_W5500_LINK, 0, 0, 0);
  228. }
  229. return 0;
  230. }
  231. static void w5500_callback_to_nw_task(w5500_ctrl_t *w5500, uint32_t event_id, uint32_t param1, uint32_t param2, uint32_t param3)
  232. {
  233. luat_network_cb_param_t param = {.tag = 0, .param = w5500->user_data};
  234. OS_EVENT event = { .ID = event_id, .Param1 = param1, .Param2 = param2, .Param3 = param3};
  235. if (event_id > EV_NW_DNS_RESULT)
  236. {
  237. event.Param3 = prv_w5500_ctrl->socket[param1].param;
  238. param.tag = prv_w5500_ctrl->socket[param1].tag;
  239. if (EV_NW_SOCKET_CLOSE_OK == event_id)
  240. {
  241. prv_w5500_ctrl->socket[param1].param = NULL;
  242. }
  243. }
  244. w5500->socket_cb(&event, &param);
  245. }
  246. static void w5500_xfer(w5500_ctrl_t *w5500, uint16_t address, uint8_t ctrl, uint8_t *data, uint32_t len)
  247. {
  248. BytesPutBe16(w5500->tx_buf, address);
  249. w5500->tx_buf[2] = ctrl;
  250. if (data && len)
  251. {
  252. memcpy(w5500->tx_buf + 3, data, len);
  253. }
  254. luat_gpio_set(w5500->cs_pin, 0);
  255. luat_spi_transfer(w5500->spi_id, (const char* )w5500->tx_buf, len + 3, (char*)w5500->rx_buf, len + 3);
  256. luat_gpio_set(w5500->cs_pin, 1);
  257. if (data && len)
  258. {
  259. memcpy(data, w5500->rx_buf + 3, len);
  260. }
  261. }
  262. static uint8_t w5500_socket_state(w5500_ctrl_t *w5500, uint8_t socket_id)
  263. {
  264. uint8_t retry = 0;
  265. uint8_t temp[4];
  266. do
  267. {
  268. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg, temp, 4);
  269. if (temp[W5500_SOCKET_MR] == 0xff) //模块读不到了
  270. {
  271. w5500->device_on = 0;
  272. return 0;
  273. }
  274. retry++;
  275. }while(temp[W5500_SOCKET_CR] && (retry < 10));
  276. if (retry >= 10)
  277. {
  278. w5500->inter_error++;
  279. LLOGE("check too much times, error %d", w5500->inter_error);
  280. }
  281. return temp[W5500_SOCKET_SR];
  282. }
  283. static void w5500_socket_disconnect(w5500_ctrl_t *w5500, uint8_t socket_id)
  284. {
  285. uint8_t temp;
  286. temp = w5500_socket_state(w5500, socket_id);
  287. if (!w5500->device_on) return;
  288. if (SOCK_CLOSED == temp)
  289. {
  290. w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  291. // LLOGD("socket %d already closed");
  292. return;
  293. }
  294. // if ((temp >= SOCK_FIN_WAIT) && (temp <= SOCK_LAST_ACK))
  295. // {
  296. // w5500->socket[socket_id].state = W5500_SOCKET_CLOSING;
  297. // LLOGD("socket %d is closing");
  298. // return;
  299. //
  300. // }
  301. temp = Sn_CR_DISCON;
  302. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  303. w5500->socket[socket_id].state = W5500_SOCKET_CLOSING;
  304. }
  305. static void w5500_socket_close(w5500_ctrl_t *w5500, uint8_t socket_id)
  306. {
  307. uint8_t temp;
  308. temp = w5500_socket_state(w5500, socket_id);
  309. if (!w5500->device_on) return;
  310. //
  311. // if (SOCK_CLOSED == temp)
  312. // {
  313. // w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  314. //// LLOGD("socket %d already closed");
  315. // return;
  316. // }
  317. temp = Sn_CR_CLOSE;
  318. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  319. w5500->socket[socket_id].state = W5500_SOCKET_OFFLINE;
  320. }
  321. static int w5500_socket_config(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t is_tcp, uint16_t local_port)
  322. {
  323. uint8_t delay_cnt = 0;
  324. uint8_t temp = 0;
  325. temp = w5500_socket_state(w5500, socket_id);
  326. if (!w5500->device_on) return -1;
  327. if (SOCK_CLOSED != temp)
  328. {
  329. LLOGE("socket %d not closed state %x", socket_id, temp);
  330. return -1;
  331. }
  332. uint8_t cmd[32];
  333. uint16_t wtemp;
  334. for(temp = 0; temp < 3; temp++)
  335. {
  336. cmd[W5500_SOCKET_MR] = is_tcp?Sn_MR_TCP:Sn_MR_UDP;
  337. cmd[W5500_SOCKET_CR] = 0;
  338. cmd[W5500_SOCKET_IR] = 0xff;
  339. BytesPutBe16(&cmd[W5500_SOCKET_SOURCE_PORT0], local_port);
  340. BytesPutLe32(&cmd[W5500_SOCKET_DEST_IP0], 0xffffffff);
  341. BytesPutBe16(&cmd[W5500_SOCKET_DEST_PORT0], 0);
  342. BytesPutBe16(&cmd[W5500_SOCKET_SEGMENT0], is_tcp?1460:1472);
  343. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg|is_write, cmd, W5500_SOCKET_TOS - 1);
  344. w5500_xfer(w5500, W5500_SOCKET_MR, socket_index(socket_id)|socket_reg, cmd, W5500_SOCKET_TOS - 1);
  345. wtemp = BytesGetBe16(&cmd[W5500_SOCKET_SOURCE_PORT0]);
  346. if (wtemp != local_port)
  347. {
  348. LLOGE("error port %u %u", wtemp, local_port);
  349. }
  350. else
  351. {
  352. goto W5500_SOCKET_CONFIG_START;
  353. }
  354. }
  355. return -1;
  356. W5500_SOCKET_CONFIG_START:
  357. cmd[0] = Sn_CR_OPEN;
  358. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, cmd, 1);
  359. do
  360. {
  361. temp = w5500_socket_state(w5500, socket_id);
  362. if (!w5500->device_on) return -1;
  363. delay_cnt++;
  364. }while((temp != SOCK_INIT) && (temp != SOCK_UDP) && (delay_cnt < 100));
  365. if (delay_cnt >= 100)
  366. {
  367. w5500->inter_error++;
  368. LLOGE("socket %d config timeout, error %d", socket_id, w5500->inter_error);
  369. return -1;
  370. }
  371. w5500->socket[socket_id].state = is_tcp?W5500_SOCKET_CONFIG:W5500_SOCKET_ONLINE;
  372. return 0;
  373. }
  374. static int w5500_socket_connect(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t is_listen, uint32_t remote_ip, uint16_t remote_port)
  375. {
  376. uint32_t ip;
  377. uint16_t port;
  378. uint8_t delay_cnt;
  379. uint8_t temp;
  380. uint8_t cmd[16];
  381. temp = w5500_socket_state(w5500, socket_id);
  382. if (!w5500->device_on) return -1;
  383. if ((temp != SOCK_INIT) && (temp != SOCK_UDP))
  384. {
  385. LLOGD("socket %d not config state %x", socket_id, temp);
  386. return -1;
  387. }
  388. // BytesPutLe32(cmd, remote_ip);
  389. // LLOGD("%02d.%02d.%02d,%02d, %u, %d", cmd[0], cmd[1], cmd[2], cmd[3], remote_port, is_listen);
  390. if (!is_listen)
  391. {
  392. // w5500_xfer(w5500, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg, cmd, 6);
  393. // ip = BytesGetLe32(cmd);
  394. // port = BytesGetBe16(cmd + 4);
  395. // if (ip != remote_ip || port != remote_port)
  396. {
  397. BytesPutLe32(cmd, remote_ip);
  398. BytesPutBe16(&cmd[W5500_SOCKET_DEST_PORT0 - W5500_SOCKET_DEST_IP0], remote_port);
  399. w5500_xfer(w5500, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg|is_write, cmd, 6);
  400. }
  401. }
  402. // W5500_SOCKET_CONNECT_START:
  403. if (temp != SOCK_UDP)
  404. {
  405. uint8_t temp = is_listen?Sn_CR_LISTEN:Sn_CR_CONNECT;
  406. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, &temp, 1);
  407. w5500->socket[socket_id].state = W5500_SOCKET_CONNECT;
  408. }
  409. return 0;
  410. }
  411. static int w5500_socket_auto_heart(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t time)
  412. {
  413. uint8_t point[6];
  414. point[0] = time;
  415. w5500_xfer(w5500, W5500_SOCKET_KEEP_TIME, socket_index(socket_id)|socket_reg|is_write, point, 1);
  416. return 0;
  417. }
  418. static int w5500_socket_tx(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint32_t len)
  419. {
  420. uint8_t delay_cnt;
  421. uint8_t temp;
  422. uint8_t point[6];
  423. uint16_t tx_free, tx_point;
  424. temp = w5500_socket_state(w5500, socket_id);
  425. if (!w5500->device_on) return -1;
  426. if ((temp != SOCK_ESTABLISHED) && (temp != SOCK_UDP))
  427. {
  428. LLOGD("socket %d not online state %x", socket_id, temp);
  429. return -1;
  430. }
  431. w5500->socket[socket_id].state = W5500_SOCKET_ONLINE;
  432. if (!data || !len)
  433. {
  434. point[0] = Sn_CR_SEND_KEEP;
  435. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  436. }
  437. w5500_xfer(w5500, W5500_SOCKET_TX_FREE_SIZE0, socket_index(socket_id)|socket_reg, point, 6);
  438. tx_free = BytesGetBe16(point);
  439. tx_point = BytesGetBe16(point + 4);
  440. // if (tx_free != 2048)
  441. // {
  442. // LLOGD("%d,0x%04x,%u,%u", socket_id, tx_point, len,tx_free);
  443. // }
  444. // DBG_HexPrintf(data, len);
  445. if (len > tx_free)
  446. {
  447. len = tx_free;
  448. }
  449. w5500->last_tx_time = luat_mcu_tick64_ms();
  450. w5500_xfer(w5500, tx_point, socket_index(socket_id)|socket_tx|is_write, data, len);
  451. tx_point += len;
  452. BytesPutBe16(point, tx_point);
  453. w5500_xfer(w5500, W5500_SOCKET_TX_WRITE_POINT0, socket_index(socket_id)|socket_reg|is_write, point, 2);
  454. point[0] = Sn_CR_SEND;
  455. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  456. return len;
  457. }
  458. static int w5500_socket_rx(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint16_t len)
  459. {
  460. uint8_t delay_cnt;
  461. uint8_t temp;
  462. uint8_t point[4];
  463. uint16_t rx_size, rx_point;
  464. temp = w5500_socket_state(w5500, socket_id);
  465. if (!w5500->device_on) return -1;
  466. if ((temp < SOCK_ESTABLISHED) || (temp > SOCK_UDP))
  467. {
  468. LLOGD("socket %d not config state %x", socket_id, temp);
  469. return -1;
  470. }
  471. w5500->socket[socket_id].state = W5500_SOCKET_ONLINE;
  472. w5500_xfer(w5500, W5500_SOCKET_RX_SIZE0, socket_index(socket_id)|socket_reg, point, 4);
  473. rx_size = BytesGetBe16(point);
  474. rx_point = BytesGetBe16(point + 2);
  475. // LLOGD("%d,0x%04x,%u", socket_id, rx_point, rx_size);
  476. if (!rx_size) return 0;
  477. if (rx_size < len)
  478. {
  479. len = rx_size;
  480. }
  481. w5500_xfer(w5500, rx_point, socket_index(socket_id)|socket_rx, data, len);
  482. rx_point += len;
  483. BytesPutBe16(point, rx_point);
  484. w5500_xfer(w5500, W5500_SOCKET_RX_READ_POINT0, socket_index(socket_id)|socket_reg|is_write, point, 2);
  485. point[0] = Sn_CR_RECV;
  486. w5500_xfer(w5500, W5500_SOCKET_CR, socket_index(socket_id)|socket_reg|is_write, point, 1);
  487. return len;
  488. }
  489. static void w5500_nw_state(w5500_ctrl_t *w5500)
  490. {
  491. int i;
  492. PV_Union uIP;
  493. if (w5500->link_ready && w5500->ip_ready)
  494. {
  495. if (!w5500->network_ready)
  496. {
  497. w5500->network_ready = 1;
  498. dns_clear(&w5500->dns_client);
  499. w5500->socket[0].tx_wait_size = 0; //dns可以继续发送了
  500. w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 1, 0);
  501. LLOGD("network ready");
  502. for(i = 0; i < MAX_DNS_SERVER; i++)
  503. {
  504. #ifdef LUAT_USE_LWIP
  505. if (w5500->dns_client.dns_server[i].type != 0xff)
  506. {
  507. uIP.u32 = w5500->dns_client.dns_server[i].u_addr.ip4.addr;
  508. LLOGD("DNS%d:%d.%d.%d.%d",i, uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  509. }
  510. #else
  511. if (w5500->dns_client.dns_server[i].is_ipv6 != 0xff)
  512. {
  513. uIP.u32 = w5500->dns_client.dns_server[i].ipv4;
  514. LLOGD("DNS%d:%d.%d.%d.%d",i, uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  515. }
  516. #endif
  517. }
  518. }
  519. }
  520. else
  521. {
  522. if (w5500->network_ready)
  523. {
  524. w5500->network_ready = 0;
  525. dns_clear(&w5500->dns_client);
  526. w5500_callback_to_nw_task(w5500, EV_NW_STATE, 0, 0, 0);
  527. LLOGD("network not ready");
  528. for(i = 0; i < MAX_SOCK_NUM; i++)
  529. {
  530. w5500->socket[i].tx_wait_size = 0;
  531. // llist_traversal(&w5500->socket[i].tx_head, w5500_del_data_cache, NULL);
  532. // llist_traversal(&w5500->socket[i].rx_head, w5500_del_data_cache, NULL);
  533. }
  534. }
  535. }
  536. }
  537. static void w5500_check_dhcp(w5500_ctrl_t *w5500)
  538. {
  539. if (w5500->static_ip) return;
  540. if (DHCP_STATE_CHECK == w5500->dhcp_client.state)
  541. {
  542. w5500->dhcp_client.state = DHCP_STATE_WAIT_LEASE_P1;
  543. uint8_t temp[24];
  544. PV_Union uIP;
  545. memset(temp, 0, sizeof(temp));
  546. temp[0] = w5500->force_arp?MR_UDP_FARP:0;
  547. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->dhcp_client.gateway);
  548. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->dhcp_client.submask);
  549. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->dhcp_client.ip);
  550. memcpy(&temp[W5500_COMMON_MAC0], w5500->dhcp_client.mac, 6);
  551. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, W5500_COMMON_INTLEVEL0);
  552. w5500->dhcp_client.discover_cnt = 0;
  553. uIP.u32 = w5500->dhcp_client.ip;
  554. LLOGD("动态IP:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  555. uIP.u32 = w5500->dhcp_client.submask;
  556. LLOGD("子网掩码:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  557. uIP.u32 = w5500->dhcp_client.gateway;
  558. LLOGD("网关:%d.%d.%d.%d", uIP.u8[0], uIP.u8[1], uIP.u8[2], uIP.u8[3]);
  559. LLOGD("租约时间:%u秒", w5500->dhcp_client.lease_time);
  560. int i;
  561. for(i = 0; i < MAX_DNS_SERVER; i++)
  562. {
  563. if (w5500->dns_client.is_static_dns[i])
  564. {
  565. goto PRINT_DNS;
  566. }
  567. }
  568. #ifdef LUAT_USE_LWIP
  569. if (w5500->dhcp_client.dns_server[0])
  570. {
  571. w5500->dns_client.dns_server[0].u_addr.ip4.addr = w5500->dhcp_client.dns_server[0];
  572. w5500->dns_client.dns_server[0].type = IPADDR_TYPE_V4;
  573. }
  574. if (w5500->dhcp_client.dns_server[1])
  575. {
  576. w5500->dns_client.dns_server[1].u_addr.ip4.addr = w5500->dhcp_client.dns_server[1];
  577. w5500->dns_client.dns_server[1].type = IPADDR_TYPE_V4;
  578. }
  579. #else
  580. if (w5500->dhcp_client.dns_server[0])
  581. {
  582. w5500->dns_client.dns_server[0].ipv4 = w5500->dhcp_client.dns_server[0];
  583. w5500->dns_client.dns_server[0].is_ipv6 = 0;
  584. }
  585. if (w5500->dhcp_client.dns_server[1])
  586. {
  587. w5500->dns_client.dns_server[1].ipv4 = w5500->dhcp_client.dns_server[1];
  588. w5500->dns_client.dns_server[1].is_ipv6 = 0;
  589. }
  590. #endif
  591. PRINT_DNS:
  592. w5500_ip_state(w5500, 1);
  593. }
  594. if ((!w5500->last_udp_send_ok && w5500->dhcp_client.discover_cnt >= 1) || (w5500->last_udp_send_ok && w5500->dhcp_client.discover_cnt >= 3))
  595. {
  596. LLOGD("dhcp long time not get ip, reboot w5500");
  597. memset(&w5500->dhcp_client, 0, sizeof(w5500->dhcp_client));
  598. platform_send_event(w5500->task_handle, EV_W5500_RE_INIT, 0, 0, 0);
  599. }
  600. }
  601. static void w5500_link_state(w5500_ctrl_t *w5500, uint8_t check_state)
  602. {
  603. Buffer_Struct tx_msg_buf = {0,0,0};
  604. uint32_t remote_ip;
  605. int result;
  606. if (w5500->link_ready != check_state)
  607. {
  608. LLOGD("link %d -> %d", w5500->link_ready, check_state);
  609. w5500->link_ready = check_state;
  610. if (w5500->link_ready)
  611. {
  612. if (!w5500->static_ip)
  613. {
  614. w5500_ip_state(w5500, 0);
  615. w5500->dhcp_client.state = w5500->dhcp_client.ip?DHCP_STATE_REQUIRE:DHCP_STATE_DISCOVER;
  616. uint8_t temp[1];
  617. temp[0] = MR_UDP_FARP;
  618. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, 1);
  619. result = ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  620. w5500_check_dhcp(w5500);
  621. if (tx_msg_buf.Pos)
  622. {
  623. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  624. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  625. w5500->last_udp_send_ok = 0;
  626. }
  627. OS_DeInitBuffer(&tx_msg_buf);
  628. }
  629. }
  630. else
  631. {
  632. if (luat_mcu_tick64_ms() < (w5500->last_tx_time + 1500))
  633. {
  634. w5500->inter_error++;
  635. LLOGE("link down too fast, error %u", w5500->inter_error);
  636. }
  637. }
  638. w5500_nw_state(w5500);
  639. }
  640. }
  641. static void w5500_ip_state(w5500_ctrl_t *w5500, uint8_t check_state)
  642. {
  643. if (w5500->ip_ready != check_state)
  644. {
  645. LLOGD("ip %d -> %d", w5500->ip_ready, check_state);
  646. w5500->ip_ready = check_state;
  647. w5500_nw_state(w5500);
  648. }
  649. }
  650. static void w5500_init_reg(w5500_ctrl_t *w5500)
  651. {
  652. uint8_t temp[64];
  653. luat_gpio_set(w5500->rst_pin, 0);
  654. msleep(5);
  655. luat_gpio_set(w5500->rst_pin, 1);
  656. msleep(10);
  657. w5500->ip_ready = 0;
  658. w5500->network_ready = 0;
  659. if (w5500->static_ip)
  660. {
  661. w5500->dhcp_client.state = DHCP_STATE_NOT_WORK;
  662. }
  663. else
  664. {
  665. if (w5500->auto_speed)
  666. {
  667. w5500->dhcp_client.state = DHCP_STATE_DISCOVER;
  668. }
  669. else
  670. {
  671. if ((w5500->dhcp_client.state == DHCP_STATE_NOT_WORK) || !w5500->dhcp_client.ip)
  672. {
  673. w5500->dhcp_client.state = DHCP_STATE_DISCOVER;
  674. }
  675. else
  676. {
  677. w5500->dhcp_client.state = DHCP_STATE_REQUIRE;
  678. }
  679. }
  680. }
  681. w5500_callback_to_nw_task(w5500, EV_NW_RESET, 0, 0, 0);
  682. luat_gpio_close(w5500->link_pin);
  683. luat_gpio_close(w5500->irq_pin);
  684. w5500_xfer(w5500, W5500_COMMON_MR, 0, temp, W5500_COMMON_QTY);
  685. w5500->device_on = (0x04 == temp[W5500_COMMON_VERSIONR])?1:0;
  686. w5500_link_state(w5500, w5500->device_on?(temp[W5500_COMMON_PHY] & 0x01):0);
  687. temp[W5500_COMMON_MR] = w5500->force_arp?MR_UDP_FARP:0;
  688. if (w5500->static_ip)
  689. {
  690. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->static_gateway); //已经是大端格式了不需要转换
  691. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->static_submask); //已经是大端格式了不需要转换
  692. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->static_ip); //已经是大端格式了不需要转换
  693. }
  694. else if (w5500->dhcp_client.ip)
  695. {
  696. BytesPutLe32(&temp[W5500_COMMON_GAR0], w5500->dhcp_client.gateway);
  697. BytesPutLe32(&temp[W5500_COMMON_SUBR0], w5500->dhcp_client.submask);
  698. BytesPutLe32(&temp[W5500_COMMON_IP0], w5500->dhcp_client.ip);
  699. }
  700. else
  701. {
  702. BytesPutLe32(&temp[W5500_COMMON_GAR0], 0);
  703. BytesPutLe32(&temp[W5500_COMMON_SUBR0], 0);
  704. BytesPutLe32(&temp[W5500_COMMON_IP0], 0);
  705. }
  706. memcpy(&temp[W5500_COMMON_MAC0], w5500->mac, 6);
  707. memcpy(w5500->dhcp_client.mac, &temp[W5500_COMMON_MAC0], 6);
  708. sprintf_(w5500->dhcp_client.name, "airm2m-%02x%02x%02x%02x%02x%02x",
  709. w5500->dhcp_client.mac[0],w5500->dhcp_client.mac[1], w5500->dhcp_client.mac[2],
  710. w5500->dhcp_client.mac[3],w5500->dhcp_client.mac[4], w5500->dhcp_client.mac[5]);
  711. // BytesPutBe16(&temp[W5500_COMMON_SOCKET_RTR0], w5500->RTR);
  712. temp[W5500_COMMON_IMR] = IR_CONFLICT|IR_UNREACH|IR_MAGIC;
  713. temp[W5500_COMMON_SOCKET_IMR] = 0xff;
  714. // temp[W5500_COMMON_SOCKET_RCR] = w5500->RCR;
  715. // DBG_HexPrintf(temp + W5500_COMMON_SOCKET_RTR0, 3);
  716. w5500_xfer(w5500, W5500_COMMON_MR, is_write, temp, W5500_COMMON_QTY);
  717. // memset(temp, 0, sizeof(temp));
  718. // w5500_xfer(w5500, W5500_COMMON_MR, 0, NULL, W5500_COMMON_QTY);
  719. // DBG_HexPrintf(temp, W5500_COMMON_QTY);
  720. w5500_ip_state(w5500, w5500->static_ip?1:0);
  721. luat_gpio_t gpio = {0};
  722. gpio.pin = w5500->irq_pin;
  723. gpio.mode = Luat_GPIO_IRQ;
  724. gpio.pull = Luat_GPIO_PULLUP;
  725. gpio.irq = Luat_GPIO_FALLING;
  726. gpio.irq_cb = w5500_irq;
  727. gpio.irq_args = w5500;
  728. luat_gpio_setup(&gpio);
  729. gpio.pin = w5500->link_pin;
  730. gpio.pull = Luat_GPIO_DEFAULT;
  731. gpio.irq = Luat_GPIO_BOTH;
  732. gpio.irq_cb = w5500_irq;
  733. gpio.irq_args = w5500;
  734. luat_gpio_setup(&gpio);
  735. w5500_socket_auto_heart(w5500, SYS_SOCK_ID, 2);
  736. w5500_socket_config(w5500, SYS_SOCK_ID, 0, DHCP_CLIENT_PORT);
  737. if (w5500->auto_speed)
  738. {
  739. temp[0] = 0x78;
  740. w5500_xfer(w5500, W5500_COMMON_PHY, is_write, temp, 1);
  741. temp[0] = 0x78+ 0x80;
  742. w5500_xfer(w5500, W5500_COMMON_PHY, is_write, temp, 1);
  743. }
  744. w5500->inter_error = 0;
  745. w5500->next_socket_index = 1;
  746. int i;
  747. for(i = 0; i < MAX_DNS_SERVER; i++)
  748. {
  749. if (!w5500->dns_client.is_static_dns[i])
  750. {
  751. #ifdef LUAT_USE_LWIP
  752. w5500->dns_client.dns_server[i].type = 0xff;
  753. #else
  754. w5500->dns_client.dns_server[i].is_ipv6 = 0xff;
  755. #endif
  756. }
  757. }
  758. }
  759. static int32_t w5500_dummy_callback(void *pData, void *pParam)
  760. {
  761. return 0;
  762. }
  763. static socket_data_t * w5500_create_data_node(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t *data, uint32_t len, luat_ip_addr_t *remote_ip, uint16_t remote_port)
  764. {
  765. socket_data_t *p = (socket_data_t *)malloc(sizeof(socket_data_t));
  766. if (p)
  767. {
  768. memset(p, 0, sizeof(socket_data_t));
  769. p->len = len;
  770. p->port = remote_port;
  771. if (remote_ip)
  772. {
  773. p->ip = *remote_ip;
  774. }
  775. else
  776. {
  777. #ifdef LUAT_USE_LWIP
  778. p->ip.type = 0xff;
  779. #else
  780. p->ip.is_ipv6 = 0xff;
  781. #endif
  782. }
  783. p->tag = w5500->socket[socket_id].tag;
  784. if (data && len)
  785. {
  786. p->data = malloc(len);
  787. if (p->data)
  788. {
  789. memcpy(p->data, data, len);
  790. }
  791. else
  792. {
  793. free(p);
  794. return NULL;
  795. }
  796. }
  797. }
  798. return p;
  799. }
  800. static void w5500_socket_tx_next_data(w5500_ctrl_t *w5500, uint8_t socket_id)
  801. {
  802. W5500_LOCK;
  803. socket_data_t *p = llist_traversal(&w5500->socket[socket_id].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  804. W5500_UNLOCK;
  805. if (p)
  806. {
  807. if (!w5500->socket[socket_id].is_tcp)
  808. {
  809. #ifdef LUAT_USE_LWIP
  810. w5500_socket_connect(w5500, socket_id, 0, p->ip.u_addr.ip4.addr, p->port);
  811. #else
  812. w5500_socket_connect(w5500, socket_id, 0, p->ip.ipv4, p->port);
  813. #endif
  814. }
  815. if (p->data && p->len)
  816. {
  817. p->read_pos += w5500_socket_tx(w5500, socket_id, p->data + p->read_pos, p->len - p->read_pos);
  818. p->is_sending = 1;
  819. }
  820. else
  821. {
  822. w5500_socket_tx(w5500, socket_id, NULL, 0);
  823. p->len = 0;
  824. p->read_pos = 0;
  825. p->is_sending = 1;
  826. }
  827. }
  828. }
  829. static int32_t w5500_dns_check_result(void *data, void *param)
  830. {
  831. luat_dns_require_t *require = (luat_dns_require_t *)data;
  832. if (require->result != 0)
  833. {
  834. free(require->uri.Data);
  835. require->uri.Data = NULL;
  836. if (require->result > 0)
  837. {
  838. luat_dns_ip_result *ip_result = zalloc(sizeof(luat_dns_ip_result) * require->result);
  839. int i;
  840. for(i = 0; i < require->result; i++)
  841. {
  842. ip_result[i] = require->ip_result[i];
  843. }
  844. w5500_callback_to_nw_task(param, EV_NW_DNS_RESULT, require->result, ip_result, require->param);
  845. }
  846. else
  847. {
  848. w5500_callback_to_nw_task(param, EV_NW_DNS_RESULT, 0, 0, require->param);
  849. }
  850. return LIST_DEL;
  851. }
  852. else
  853. {
  854. return LIST_PASS;
  855. }
  856. }
  857. static void w5500_dns_tx_next(w5500_ctrl_t *w5500, Buffer_Struct *tx_msg_buf)
  858. {
  859. int i;
  860. if (w5500->socket[SYS_SOCK_ID].tx_wait_size) return;
  861. dns_run(&w5500->dns_client, NULL, tx_msg_buf, &i);
  862. if (tx_msg_buf->Pos)
  863. {
  864. #ifdef LUAT_USE_LWIP
  865. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, w5500->dns_client.dns_server[i].u_addr.ip4.addr, DNS_SERVER_PORT);
  866. #else
  867. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, w5500->dns_client.dns_server[i].ipv4, DNS_SERVER_PORT);
  868. #endif
  869. if (!w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf->Data, tx_msg_buf->Pos))
  870. {
  871. w5500->socket[SYS_SOCK_ID].tx_wait_size = 1;
  872. }
  873. OS_DeInitBuffer(tx_msg_buf);
  874. llist_traversal(&w5500->dns_client.require_head, w5500_dns_check_result, w5500);
  875. }
  876. }
  877. static void w5500_sys_socket_callback(w5500_ctrl_t *w5500, uint8_t socket_id, uint8_t event)
  878. {
  879. Buffer_Struct rx_buf;
  880. Buffer_Struct msg_buf;
  881. Buffer_Struct tx_msg_buf = {0,0,0};
  882. int result, i;
  883. uint32_t ip;
  884. uint16_t port;
  885. uint16_t len;
  886. socket_data_t *p;
  887. luat_ip_addr_t ip_addr;
  888. switch(event)
  889. {
  890. case Sn_IR_SEND_OK:
  891. if (!socket_id)
  892. {
  893. w5500->last_udp_send_ok = 1;
  894. if (w5500->network_ready)
  895. {
  896. w5500->socket[SYS_SOCK_ID].tx_wait_size = 0;
  897. w5500_dns_tx_next(w5500, &tx_msg_buf);
  898. }
  899. }
  900. else if (w5500->network_ready)
  901. {
  902. W5500_LOCK;
  903. p = llist_traversal(&w5500->socket[socket_id].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  904. if (p && !p->is_sending)
  905. {
  906. LLOGE("socket %d should sending!", socket_id);
  907. p->read_pos = 0;
  908. }
  909. if (p && (p->read_pos >= p->len))
  910. {
  911. ip = p->len;
  912. w5500->socket[socket_id].tx_wait_size -= p->len;
  913. llist_del(&p->node);
  914. free(p->data);
  915. free(p);
  916. W5500_UNLOCK;
  917. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_TX_OK, socket_id, ip, 0);
  918. }
  919. else
  920. {
  921. W5500_UNLOCK;
  922. }
  923. w5500_socket_tx_next_data(w5500, socket_id);
  924. if (llist_empty(&w5500->socket[socket_id].tx_head))
  925. {
  926. w5500->socket[socket_id].tx_wait_size = 0;
  927. }
  928. }
  929. break;
  930. case Sn_IR_RECV:
  931. if (socket_id && (w5500->socket[socket_id].rx_wait_size >= SOCK_BUF_LEN))
  932. {
  933. LLOGD("socket %d, wait %dbyte", socket_id, w5500->socket[socket_id].rx_wait_size);
  934. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_FULL, socket_id, 0, 0);
  935. w5500->socket[socket_id].rx_waiting = 1;
  936. break;
  937. }
  938. OS_InitBuffer(&rx_buf, 2048);
  939. result = w5500_socket_rx(w5500, socket_id, rx_buf.Data, rx_buf.MaxLen);
  940. if (result > 0)
  941. {
  942. if (socket_id)
  943. {
  944. if (w5500->socket[socket_id].is_tcp)
  945. {
  946. socket_data_t *p = w5500_create_data_node(w5500, socket_id, rx_buf.Data, result, NULL, 0);
  947. W5500_LOCK;
  948. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].rx_head);
  949. prv_w5500_ctrl->socket[socket_id].rx_wait_size += result;
  950. W5500_UNLOCK;
  951. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_NEW, socket_id, result, 0);
  952. }
  953. else
  954. {
  955. W5500_LOCK;
  956. rx_buf.Pos = 0;
  957. while (rx_buf.Pos < result)
  958. {
  959. #ifdef LUAT_USE_LWIP
  960. ip_addr.u_addr.ip4.addr = BytesGetLe32(rx_buf.Data + rx_buf.Pos);
  961. ip_addr.type = IPADDR_TYPE_V4;
  962. #else
  963. ip_addr.ipv4 = BytesGetLe32(rx_buf.Data + rx_buf.Pos);
  964. ip_addr.is_ipv6 = 0;
  965. #endif
  966. port = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 4);
  967. len = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 6);
  968. msg_buf.Data = rx_buf.Data + rx_buf.Pos + 8;
  969. msg_buf.MaxLen = len;
  970. socket_data_t *p = w5500_create_data_node(w5500, socket_id, rx_buf.Data + rx_buf.Pos + 8, len, &ip_addr, port);
  971. rx_buf.Pos += 8 + len;
  972. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].rx_head);
  973. prv_w5500_ctrl->socket[socket_id].rx_wait_size += len;
  974. }
  975. W5500_UNLOCK;
  976. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_RX_NEW, socket_id, result, 0);
  977. }
  978. }
  979. else
  980. {
  981. rx_buf.Pos = 0;
  982. while (rx_buf.Pos < result)
  983. {
  984. ip = BytesGetBe32(rx_buf.Data + rx_buf.Pos);
  985. port = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 4);
  986. len = BytesGetBe16(rx_buf.Data + rx_buf.Pos + 6);
  987. msg_buf.Data = rx_buf.Data + rx_buf.Pos + 8;
  988. msg_buf.MaxLen = len;
  989. msg_buf.Pos = 0;
  990. switch(port)
  991. {
  992. case DHCP_SERVER_PORT:
  993. ip4_dhcp_run(&w5500->dhcp_client, &msg_buf, &tx_msg_buf, &ip);
  994. w5500_check_dhcp(w5500);
  995. if (tx_msg_buf.Pos)
  996. {
  997. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, ip, DHCP_SERVER_PORT);
  998. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  999. w5500->last_udp_send_ok = 0;
  1000. }
  1001. OS_DeInitBuffer(&tx_msg_buf);
  1002. break;
  1003. case DNS_SERVER_PORT:
  1004. dns_run(&w5500->dns_client, &msg_buf, NULL, &i);
  1005. llist_traversal(&w5500->dns_client.require_head, w5500_dns_check_result, w5500);
  1006. if (!w5500->socket[SYS_SOCK_ID].tx_wait_size)
  1007. {
  1008. dns_run(&w5500->dns_client, NULL, &tx_msg_buf, &i);
  1009. if (tx_msg_buf.Pos)
  1010. {
  1011. #ifdef LUAT_USE_LWIP
  1012. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, w5500->dns_client.dns_server[i].u_addr.ip4.addr, DNS_SERVER_PORT);
  1013. #else
  1014. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, w5500->dns_client.dns_server[i].ipv4, DNS_SERVER_PORT);
  1015. #endif
  1016. if (!w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos))
  1017. {
  1018. w5500->socket[SYS_SOCK_ID].tx_wait_size = 1;
  1019. }
  1020. OS_DeInitBuffer(&tx_msg_buf);
  1021. }
  1022. }
  1023. break;
  1024. }
  1025. rx_buf.Pos += 8 + len;
  1026. }
  1027. }
  1028. }
  1029. OS_DeInitBuffer(&rx_buf);
  1030. break;
  1031. case Sn_IR_TIMEOUT:
  1032. LLOGE("socket %d timeout", socket_id);
  1033. if (socket_id)
  1034. {
  1035. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, socket_id, 0, 0);
  1036. }
  1037. break;
  1038. case Sn_IR_CON:
  1039. LLOGD("socket %d connected", socket_id);
  1040. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CONNECT_OK, socket_id, 0, 0);
  1041. break;
  1042. case Sn_IR_DISCON:
  1043. LLOGE("socket %d disconnect", socket_id);
  1044. if (w5500_socket_state(w5500, socket_id) != SOCK_CLOSED)
  1045. {
  1046. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_REMOTE_CLOSE, socket_id, 0, 0);
  1047. }
  1048. else
  1049. {
  1050. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CLOSE_OK, socket_id, 0, 0);
  1051. }
  1052. break;
  1053. default:
  1054. break;
  1055. }
  1056. }
  1057. static void w5500_read_irq(w5500_ctrl_t *w5500)
  1058. {
  1059. OS_EVENT socket_event;
  1060. uint8_t temp[64];
  1061. uint8_t socket_irqs[MAX_SOCK_NUM];
  1062. uint8_t socket_irq, common_irq;
  1063. Buffer_Struct tx_msg_buf = {0,0,0};
  1064. uint32_t remote_ip;
  1065. int i, j;
  1066. RETRY:
  1067. w5500_xfer(w5500, W5500_COMMON_IR, 0, temp, W5500_COMMON_QTY - W5500_COMMON_IR);
  1068. common_irq = temp[0] & 0xf0;
  1069. socket_irq = temp[W5500_COMMON_SOCKET_IR - W5500_COMMON_IR];
  1070. w5500->device_on = (0x04 == temp[W5500_COMMON_VERSIONR - W5500_COMMON_IR])?1:0;
  1071. w5500_link_state(w5500, w5500->device_on?(temp[W5500_COMMON_PHY - W5500_COMMON_IR] & 0x01):0);
  1072. memset(socket_irqs, 0, MAX_SOCK_NUM);
  1073. if (!w5500->device_on)
  1074. {
  1075. luat_gpio_close(w5500->link_pin);
  1076. luat_gpio_close(w5500->irq_pin);
  1077. return;
  1078. }
  1079. if (common_irq)
  1080. {
  1081. if (common_irq & IR_CONFLICT)
  1082. {
  1083. memset(temp, 0, 4);
  1084. w5500_xfer(w5500, W5500_COMMON_IP0, is_write, temp, 4);
  1085. w5500->dhcp_client.state = DHCP_STATE_DECLINE;
  1086. ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  1087. if (tx_msg_buf.Pos)
  1088. {
  1089. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  1090. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  1091. w5500->last_udp_send_ok = 0;
  1092. }
  1093. OS_DeInitBuffer(&tx_msg_buf);
  1094. w5500_ip_state(w5500, 0);
  1095. }
  1096. }
  1097. if (socket_irq)
  1098. {
  1099. for(i = 0; i < MAX_SOCK_NUM; i++)
  1100. {
  1101. if (socket_irq & (1 << i))
  1102. {
  1103. w5500_xfer(w5500, W5500_SOCKET_IR, socket_index(i)|socket_reg, &socket_irqs[i], 1);
  1104. temp[0] = socket_irqs[i];
  1105. // LLOGD("%d,%x",i, socket_irqs[i]);
  1106. w5500_xfer(w5500, W5500_SOCKET_IR, socket_index(i)|socket_reg|is_write, temp, 1);
  1107. }
  1108. }
  1109. }
  1110. temp[0] = 0;
  1111. temp[W5500_COMMON_IMR - W5500_COMMON_IR] = IR_CONFLICT|IR_UNREACH|IR_MAGIC;
  1112. temp[W5500_COMMON_SOCKET_IR - W5500_COMMON_IR] = 0;
  1113. w5500_xfer(w5500, W5500_COMMON_IR, is_write, temp, 1);
  1114. if (socket_irq)
  1115. {
  1116. for(i = 0; i < MAX_SOCK_NUM; i++)
  1117. {
  1118. if (socket_irqs[i])
  1119. {
  1120. for (j = 4; j >= 0; j--)
  1121. {
  1122. if (socket_irqs[i] & (1 << j))
  1123. {
  1124. w5500_sys_socket_callback(w5500, i, (1 << j));
  1125. }
  1126. }
  1127. }
  1128. }
  1129. }
  1130. if (luat_gpio_get(w5500->irq_pin) != 1)
  1131. {
  1132. // LLOGD("irq not clear!");
  1133. goto RETRY;
  1134. }
  1135. }
  1136. static void w5500_task(void *param)
  1137. {
  1138. w5500_ctrl_t *w5500 = (w5500_ctrl_t *)param;
  1139. OS_EVENT event;
  1140. int result;
  1141. Buffer_Struct tx_msg_buf = {0,0,0};
  1142. uint32_t remote_ip, sleep_time;
  1143. PV_Union uPV;
  1144. socket_data_t *p;
  1145. w5500_init_reg(w5500);
  1146. while(1)
  1147. {
  1148. if (w5500->inter_error >= 2)
  1149. {
  1150. LLOGD("error too much, reboot");
  1151. w5500_init_reg(w5500);
  1152. }
  1153. sleep_time = 100;
  1154. if (w5500->network_ready)
  1155. {
  1156. if (!w5500->dns_client.is_run && (w5500->link_pin != 0xff))
  1157. {
  1158. sleep_time = 0;
  1159. }
  1160. else
  1161. {
  1162. sleep_time = 1000;
  1163. }
  1164. }
  1165. else if (w5500->link_ready)
  1166. {
  1167. sleep_time = 500;
  1168. }
  1169. else if (w5500->link_pin != 0xff)
  1170. {
  1171. sleep_time = 0;
  1172. }
  1173. result = platform_wait_event(w5500->task_handle, 0, &event, NULL, sleep_time);
  1174. w5500_xfer(w5500, W5500_COMMON_MR, 0, NULL, W5500_COMMON_QTY);
  1175. w5500->device_on = (0x04 == w5500->rx_buf[3 + W5500_COMMON_VERSIONR])?1:0;
  1176. if (w5500->device_on && w5500->rx_buf[3 + W5500_COMMON_IMR] != (IR_CONFLICT|IR_UNREACH|IR_MAGIC))
  1177. {
  1178. w5500_init_reg(w5500);
  1179. }
  1180. if (!w5500->device_on)
  1181. {
  1182. w5500_link_state(w5500, 0);
  1183. luat_gpio_close(w5500->link_pin);
  1184. luat_gpio_close(w5500->irq_pin);
  1185. }
  1186. else
  1187. {
  1188. w5500_link_state(w5500, w5500->rx_buf[3 + W5500_COMMON_PHY] & 0x01);
  1189. w5500->speed_status = w5500->rx_buf[3 + W5500_COMMON_PHY] & (3 << 1);
  1190. }
  1191. if (result)
  1192. {
  1193. if (w5500->network_ready)
  1194. {
  1195. w5500_dns_tx_next(w5500, &tx_msg_buf);
  1196. }
  1197. else if (w5500->link_ready)
  1198. {
  1199. result = ip4_dhcp_run(&w5500->dhcp_client, NULL, &tx_msg_buf, &remote_ip);
  1200. w5500_check_dhcp(w5500);
  1201. if (tx_msg_buf.Pos)
  1202. {
  1203. w5500_socket_connect(w5500, SYS_SOCK_ID, 0, remote_ip, DHCP_SERVER_PORT);
  1204. w5500_socket_tx(w5500, SYS_SOCK_ID, tx_msg_buf.Data, tx_msg_buf.Pos);
  1205. w5500->last_udp_send_ok = 0;
  1206. }
  1207. OS_DeInitBuffer(&tx_msg_buf);
  1208. }
  1209. continue;
  1210. }
  1211. switch(event.ID)
  1212. {
  1213. case EV_W5500_IRQ:
  1214. if (w5500->device_on)
  1215. {
  1216. w5500_read_irq(w5500);
  1217. }
  1218. break;
  1219. case EV_W5500_SOCKET_TX:
  1220. if (event.Param1) W5500_LOCK;
  1221. p = llist_traversal(&w5500->socket[event.Param1].tx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[event.Param1]);
  1222. if (event.Param1) W5500_UNLOCK;
  1223. if (p && p->is_sending)
  1224. {
  1225. LLOGD("socket %d is sending, need wait!", event.Param1);
  1226. }
  1227. else
  1228. {
  1229. w5500_socket_tx_next_data(w5500, event.Param1);
  1230. if (!p->is_sending)
  1231. {
  1232. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1233. }
  1234. }
  1235. break;
  1236. case EV_W5500_SOCKET_CONNECT:
  1237. uPV.u8[0] = 0;
  1238. while(w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED)
  1239. {
  1240. w5500_socket_close(w5500, event.Param1);
  1241. uPV.u8[0]++;
  1242. if (uPV.u8[0] > 100)
  1243. {
  1244. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1245. break;
  1246. }
  1247. }
  1248. uPV.u32 = event.Param3;
  1249. w5500_socket_config(w5500, event.Param1, w5500->socket[event.Param1].is_tcp, uPV.u16[0]);
  1250. w5500_socket_connect(w5500, event.Param1, 0, event.Param2, uPV.u16[1]);
  1251. if (!w5500->socket[event.Param1].is_tcp)
  1252. {
  1253. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CONNECT_OK, event.Param1, 0, 0);
  1254. }
  1255. break;
  1256. case EV_W5500_SOCKET_CLOSE:
  1257. if ((w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED))
  1258. {
  1259. w5500_socket_disconnect(w5500, event.Param1);
  1260. }
  1261. else
  1262. {
  1263. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_CLOSE_OK, event.Param1, 0, 0);
  1264. }
  1265. break;
  1266. case EV_W5500_SOCKET_LISTEN:
  1267. uPV.u8[0] = 0;
  1268. while(w5500_socket_state(w5500, event.Param1) != SOCK_CLOSED)
  1269. {
  1270. w5500_socket_close(w5500, event.Param1);
  1271. uPV.u8[0]++;
  1272. if (uPV.u8[0] > 100)
  1273. {
  1274. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_ERROR, event.Param1, 0, 0);
  1275. }
  1276. }
  1277. uPV.u32 = event.Param3;
  1278. w5500_socket_config(w5500, event.Param1, w5500->socket[event.Param1].is_tcp, event.Param2);
  1279. w5500_socket_connect(w5500, event.Param1, 1, 0xffffffff, 0xff00);
  1280. w5500_callback_to_nw_task(w5500, EV_NW_SOCKET_LISTEN, event.Param1, 0, 0);
  1281. break;
  1282. case EV_W5500_SOCKET_DNS:
  1283. if (w5500->network_ready)
  1284. {
  1285. dns_require(&w5500->dns_client, event.Param1, event.Param2, event.Param3);
  1286. w5500_dns_tx_next(w5500, &tx_msg_buf);
  1287. }
  1288. break;
  1289. case EV_W5500_RE_INIT:
  1290. w5500_init_reg(w5500);
  1291. break;
  1292. case EV_W5500_REG_OP:
  1293. uPV.u8[0] = event.Param3;
  1294. w5500_xfer(w5500, event.Param1, socket_index(event.Param2)|socket_reg|is_write, uPV.u8, 1);
  1295. break;
  1296. case EV_W5500_LINK:
  1297. w5500_link_state(w5500, !luat_gpio_get(w5500->link_pin));
  1298. break;
  1299. }
  1300. }
  1301. }
  1302. void w5500_set_static_ip(uint32_t ipv4, uint32_t submask, uint32_t gateway)
  1303. {
  1304. if (prv_w5500_ctrl)
  1305. {
  1306. prv_w5500_ctrl->static_ip = ipv4;
  1307. prv_w5500_ctrl->static_submask = submask;
  1308. prv_w5500_ctrl->static_gateway = gateway;
  1309. }
  1310. }
  1311. void w5500_set_mac(uint8_t mac[6])
  1312. {
  1313. if (prv_w5500_ctrl)
  1314. {
  1315. memcpy(prv_w5500_ctrl->mac, mac, 6);
  1316. }
  1317. }
  1318. void w5500_get_mac(uint8_t mac[6])
  1319. {
  1320. if (prv_w5500_ctrl)
  1321. {
  1322. memcpy(mac, prv_w5500_ctrl->mac, 6);
  1323. }
  1324. else
  1325. {
  1326. memset(mac, 0xff, 6);
  1327. }
  1328. }
  1329. void w5500_set_param(uint16_t timeout, uint8_t retry, uint8_t auto_speed, uint8_t force_arp)
  1330. {
  1331. if (prv_w5500_ctrl)
  1332. {
  1333. prv_w5500_ctrl->RTR = timeout;
  1334. prv_w5500_ctrl->RTR = retry;
  1335. prv_w5500_ctrl->auto_speed = auto_speed;
  1336. prv_w5500_ctrl->force_arp = force_arp;
  1337. }
  1338. }
  1339. int w5500_reset(void)
  1340. {
  1341. if (prv_w5500_ctrl && prv_w5500_ctrl->device_on)
  1342. {
  1343. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_RE_INIT, 0, 0, 0);
  1344. return 0;
  1345. }
  1346. else
  1347. {
  1348. return -1;
  1349. }
  1350. }
  1351. void w5500_init(luat_spi_t* spi, uint8_t irq_pin, uint8_t rst_pin, uint8_t link_pin)
  1352. {
  1353. uint8_t *uid;
  1354. size_t t, i;
  1355. if (!prv_w5500_ctrl)
  1356. {
  1357. w5500_ctrl_t *w5500 = malloc(sizeof(w5500_ctrl_t));
  1358. memset(w5500, 0, sizeof(w5500_ctrl_t));
  1359. w5500->socket_cb = w5500_dummy_callback;
  1360. w5500->tag = luat_mcu_tick64_ms();
  1361. w5500->RCR = 8;
  1362. w5500->RTR = 2000;
  1363. w5500->spi_id = spi->id;
  1364. w5500->cs_pin = spi->cs;
  1365. w5500->irq_pin = irq_pin;
  1366. w5500->rst_pin = rst_pin;
  1367. w5500->link_pin = link_pin;
  1368. spi->cs = 0xff;
  1369. luat_spi_setup(spi);
  1370. luat_gpio_t gpio = {0};
  1371. gpio.pin = w5500->cs_pin;
  1372. gpio.mode = Luat_GPIO_OUTPUT;
  1373. gpio.pull = Luat_GPIO_DEFAULT;
  1374. luat_gpio_setup(&gpio);
  1375. luat_gpio_set(w5500->cs_pin, 1);
  1376. gpio.pin = w5500->rst_pin;
  1377. luat_gpio_setup(&gpio);
  1378. luat_gpio_set(w5500->rst_pin, 0);
  1379. char rands[4];
  1380. luat_crypto_trng(rands, 4);
  1381. for(i = 0; i < MAX_SOCK_NUM; i++)
  1382. {
  1383. INIT_LLIST_HEAD(&w5500->socket[i].tx_head);
  1384. INIT_LLIST_HEAD(&w5500->socket[i].rx_head);
  1385. }
  1386. INIT_LLIST_HEAD(&w5500->dns_client.process_head);
  1387. INIT_LLIST_HEAD(&w5500->dns_client.require_head);
  1388. w5500->dhcp_client.xid = BytesGetBe32(rands);
  1389. w5500->dhcp_client.state = DHCP_STATE_NOT_WORK;
  1390. uid = luat_mcu_unique_id(&t);
  1391. memcpy(w5500->mac, &uid[10], 6);
  1392. platform_create_task(&w5500->task_handle, 4 * 1024, 30, "w5500", w5500_task, w5500, 64);
  1393. prv_w5500_ctrl = w5500;
  1394. prv_w5500_ctrl->device_on = 1;
  1395. prv_w5500_ctrl->Sem = luat_mutex_create();
  1396. }
  1397. }
  1398. uint8_t w5500_device_ready(void)
  1399. {
  1400. if (prv_w5500_ctrl)
  1401. {
  1402. return prv_w5500_ctrl->device_on;
  1403. }
  1404. else
  1405. {
  1406. return 0;
  1407. }
  1408. }
  1409. static int w5500_check_socket(w5500_ctrl_t *w5500, int socket_id, uint64_t tag)
  1410. {
  1411. if (w5500 != prv_w5500_ctrl) return -1;
  1412. if (socket_id < 1 || socket_id >= MAX_SOCK_NUM) return -1;
  1413. if (w5500->socket[socket_id].tag != tag) return -1;
  1414. if (!w5500->socket[socket_id].in_use) return -1;
  1415. return 0;
  1416. }
  1417. static int w5500_socket_check(int socket_id, uint64_t tag, void *user_data)
  1418. {
  1419. return w5500_check_socket(user_data, socket_id, tag);
  1420. }
  1421. static uint8_t w5500_check_ready(void *user_data)
  1422. {
  1423. return ((w5500_ctrl_t *)user_data)->network_ready;
  1424. }
  1425. static int w5500_create_soceket(uint8_t is_tcp, uint64_t *tag, void *param, uint8_t is_ipv6, void *user_data)
  1426. {
  1427. if (user_data != prv_w5500_ctrl) return -1;
  1428. int i, socket_id;
  1429. socket_id = -1;
  1430. W5500_LOCK;
  1431. if (!prv_w5500_ctrl->socket[prv_w5500_ctrl->next_socket_index].in_use)
  1432. {
  1433. socket_id = prv_w5500_ctrl->next_socket_index;
  1434. prv_w5500_ctrl->next_socket_index++;
  1435. }
  1436. else
  1437. {
  1438. for (i = 1; i < MAX_SOCK_NUM; i++)
  1439. {
  1440. if (!prv_w5500_ctrl->socket[i].in_use)
  1441. {
  1442. socket_id = i;
  1443. prv_w5500_ctrl->next_socket_index = i + 1;
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. if (prv_w5500_ctrl->next_socket_index >= MAX_SOCK_NUM)
  1449. {
  1450. prv_w5500_ctrl->next_socket_index = 1;
  1451. }
  1452. if (socket_id > 0)
  1453. {
  1454. prv_w5500_ctrl->tag++;
  1455. *tag = prv_w5500_ctrl->tag;
  1456. prv_w5500_ctrl->socket[socket_id].in_use = 1;
  1457. prv_w5500_ctrl->socket[socket_id].tag = *tag;
  1458. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1459. prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
  1460. prv_w5500_ctrl->socket[socket_id].param = param;
  1461. prv_w5500_ctrl->socket[socket_id].is_tcp = is_tcp;
  1462. prv_w5500_ctrl->socket[socket_id].rx_waiting = 0;
  1463. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1464. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1465. }
  1466. W5500_UNLOCK;
  1467. return socket_id;
  1468. }
  1469. //作为client绑定一个port,并连接remote_ip和remote_port对应的server
  1470. static int w5500_socket_connect_ex(int socket_id, uint64_t tag, uint16_t local_port, luat_ip_addr_t *remote_ip, uint16_t remote_port, void *user_data)
  1471. {
  1472. int result = w5500_check_socket(user_data, socket_id, tag);
  1473. if (result) return result;
  1474. PV_Union uPV;
  1475. uPV.u16[0] = local_port;
  1476. uPV.u16[1] = remote_port;
  1477. W5500_LOCK;
  1478. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1479. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1480. W5500_UNLOCK;
  1481. #ifdef LUAT_USE_LWIP
  1482. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CONNECT, socket_id, remote_ip->u_addr.ip4.addr, uPV.u32);
  1483. uPV.u32 = remote_ip->u_addr.ip4.addr;
  1484. #else
  1485. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CONNECT, socket_id, remote_ip->ipv4, uPV.u32);
  1486. uPV.u32 = remote_ip->ipv4;
  1487. #endif
  1488. // LLOGD("%u.%u.%u.%u", uPV.u8[0], uPV.u8[1], uPV.u8[2], uPV.u8[3]);
  1489. return 0;
  1490. }
  1491. //作为server绑定一个port,开始监听
  1492. static int w5500_socket_listen(int socket_id, uint64_t tag, uint16_t local_port, void *user_data)
  1493. {
  1494. int result = w5500_check_socket(user_data, socket_id, tag);
  1495. if (result) return result;
  1496. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_LISTEN, socket_id, local_port, NULL);
  1497. return 0;
  1498. }
  1499. //作为server接受一个client
  1500. static int w5500_socket_accept(int socket_id, uint64_t tag, luat_ip_addr_t *remote_ip, uint16_t *remote_port, void *user_data)
  1501. {
  1502. uint8_t temp[16];
  1503. int result = w5500_check_socket(user_data, socket_id, tag);
  1504. if (result) return result;
  1505. w5500_xfer(user_data, W5500_SOCKET_DEST_IP0, socket_index(socket_id)|socket_reg, temp, 6);
  1506. #ifdef LUAT_USE_LWIP
  1507. remote_ip->type = 0;
  1508. remote_ip->u_addr.ip4.addr = BytesGetLe32(temp);
  1509. #else
  1510. remote_ip->is_ipv6 = 0;
  1511. remote_ip->ipv4 = BytesGetLe32(temp);
  1512. #endif
  1513. *remote_port = BytesGetBe16(temp + 4);
  1514. LLOGD("client %d.%d.%d.%d, %u", temp[0], temp[1], temp[2], temp[3], *remote_port);
  1515. return 0;
  1516. }
  1517. //主动断开一个tcp连接,需要走完整个tcp流程,用户需要接收到close ok回调才能确认彻底断开
  1518. static int w5500_socket_disconnect_ex(int socket_id, uint64_t tag, void *user_data)
  1519. {
  1520. int result = w5500_check_socket(user_data, socket_id, tag);
  1521. if (result) return result;
  1522. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_CLOSE, socket_id, 0, 0);
  1523. return 0;
  1524. }
  1525. static int w5500_socket_force_close(int socket_id, void *user_data)
  1526. {
  1527. W5500_LOCK;
  1528. w5500_socket_close(prv_w5500_ctrl, socket_id);
  1529. if (prv_w5500_ctrl->socket[socket_id].in_use)
  1530. {
  1531. prv_w5500_ctrl->socket[socket_id].in_use = 0;
  1532. prv_w5500_ctrl->socket[socket_id].tag = 0;
  1533. llist_traversal(&prv_w5500_ctrl->socket[socket_id].tx_head, w5500_del_data_cache, NULL);
  1534. llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_del_data_cache, NULL);
  1535. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1536. prv_w5500_ctrl->socket[socket_id].tx_wait_size = 0;
  1537. prv_w5500_ctrl->socket[socket_id].param = NULL;
  1538. }
  1539. W5500_UNLOCK;
  1540. return 0;
  1541. }
  1542. static int w5500_socket_close_ex(int socket_id, uint64_t tag, void *user_data)
  1543. {
  1544. int result = w5500_check_socket(user_data, socket_id, tag);
  1545. if (result) return result;
  1546. return w5500_socket_force_close(socket_id, user_data);
  1547. }
  1548. static uint32_t w5500_socket_read_data(uint8_t *buf, uint32_t *read_len, uint32_t len, socket_data_t *p)
  1549. {
  1550. uint32_t dummy_len;
  1551. dummy_len = ((p->len - p->read_pos) > (len - *read_len))?(len - *read_len):(p->len - p->read_pos);
  1552. memcpy(buf, p->data + p->read_pos, dummy_len);
  1553. p->read_pos += dummy_len;
  1554. if (p->read_pos >= p->len)
  1555. {
  1556. llist_del(&p->node);
  1557. free(p->data);
  1558. free(p);
  1559. }
  1560. *read_len += dummy_len;
  1561. return dummy_len;
  1562. }
  1563. static int w5500_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)
  1564. {
  1565. int result = w5500_check_socket(user_data, socket_id, tag);
  1566. if (result) return result;
  1567. W5500_LOCK;
  1568. uint32_t read_len = 0;
  1569. if (buf)
  1570. {
  1571. socket_data_t *p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  1572. if (prv_w5500_ctrl->socket[socket_id].is_tcp)
  1573. {
  1574. while((read_len < len) && p)
  1575. {
  1576. prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
  1577. p = (socket_data_t *)llist_traversal(&prv_w5500_ctrl->socket[socket_id].rx_head, w5500_next_data_cache, &prv_w5500_ctrl->socket[socket_id]);
  1578. }
  1579. }
  1580. else
  1581. {
  1582. prv_w5500_ctrl->socket[socket_id].rx_wait_size -= w5500_socket_read_data(buf + read_len, &read_len, len, p);
  1583. *remote_ip = p->ip;
  1584. *remote_port = p->port;
  1585. }
  1586. if (llist_empty(&prv_w5500_ctrl->socket[socket_id].rx_head))
  1587. {
  1588. prv_w5500_ctrl->socket[socket_id].rx_wait_size = 0;
  1589. }
  1590. }
  1591. else
  1592. {
  1593. read_len = prv_w5500_ctrl->socket[socket_id].rx_wait_size;
  1594. }
  1595. W5500_UNLOCK;
  1596. if ((prv_w5500_ctrl->socket[socket_id].rx_wait_size < SOCK_BUF_LEN) && prv_w5500_ctrl->socket[socket_id].rx_waiting)
  1597. {
  1598. prv_w5500_ctrl->socket[socket_id].rx_waiting = 0;
  1599. LLOGD("read waiting data");
  1600. w5500_sys_socket_callback(prv_w5500_ctrl, socket_id, Sn_IR_RECV);
  1601. }
  1602. return read_len;
  1603. }
  1604. static int w5500_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)
  1605. {
  1606. int result = w5500_check_socket(user_data, socket_id, tag);
  1607. if (result) return result;
  1608. if (prv_w5500_ctrl->socket[socket_id].tx_wait_size >= SOCK_BUF_LEN) return 0;
  1609. socket_data_t *p = w5500_create_data_node(prv_w5500_ctrl, socket_id, buf, len, remote_ip, remote_port);
  1610. if (p)
  1611. {
  1612. W5500_LOCK;
  1613. llist_add_tail(&p->node, &prv_w5500_ctrl->socket[socket_id].tx_head);
  1614. prv_w5500_ctrl->socket[socket_id].tx_wait_size += len;
  1615. W5500_UNLOCK;
  1616. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_TX, socket_id, 0, 0);
  1617. result = len;
  1618. }
  1619. else
  1620. {
  1621. result = -1;
  1622. }
  1623. return result;
  1624. }
  1625. void w5500_socket_clean(int *vaild_socket_list, uint32_t num, void *user_data)
  1626. {
  1627. if (user_data != prv_w5500_ctrl) return;
  1628. int socket_list[MAX_SOCK_NUM] = {0,0,0,0,0,0,0,0};
  1629. uint32_t i;
  1630. for(i = 1; i < num + 1; i++)
  1631. {
  1632. if ( (vaild_socket_list[i] > 0) && (vaild_socket_list[i] < MAX_SOCK_NUM) )
  1633. {
  1634. socket_list[vaild_socket_list[i]] = 1;
  1635. }
  1636. LLOGD("socket clean check %d,%d",i,vaild_socket_list[i]);
  1637. }
  1638. for(i = 1; i < MAX_SOCK_NUM; i++)
  1639. {
  1640. LLOGD("socket clean %d,%d",i,socket_list[i]);
  1641. if ( !socket_list[i] )
  1642. {
  1643. W5500_LOCK;
  1644. prv_w5500_ctrl->socket[i].in_use = 0;
  1645. prv_w5500_ctrl->socket[i].tag = 0;
  1646. llist_traversal(&prv_w5500_ctrl->socket[i].tx_head, w5500_del_data_cache, NULL);
  1647. llist_traversal(&prv_w5500_ctrl->socket[i].rx_head, w5500_del_data_cache, NULL);
  1648. prv_w5500_ctrl->socket[i].rx_wait_size = 0;
  1649. prv_w5500_ctrl->socket[i].tx_wait_size = 0;
  1650. w5500_socket_close(prv_w5500_ctrl, i);
  1651. W5500_UNLOCK;
  1652. }
  1653. }
  1654. }
  1655. static int w5500_getsockopt(int socket_id, uint64_t tag, int level, int optname, void *optval, uint32_t *optlen, void *user_data)
  1656. {
  1657. return -1;
  1658. }
  1659. static int w5500_setsockopt(int socket_id, uint64_t tag, int level, int optname, const void *optval, uint32_t optlen, void *user_data)
  1660. {
  1661. return -1;
  1662. }
  1663. static int w5500_get_local_ip_info(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, void *user_data)
  1664. {
  1665. if (user_data != prv_w5500_ctrl) return -1;
  1666. if (prv_w5500_ctrl->static_ip)
  1667. {
  1668. #ifdef LUAT_USE_LWIP
  1669. ip->u_addr.ip4.addr = prv_w5500_ctrl->static_ip;
  1670. ip->type = IPADDR_TYPE_V4;
  1671. submask->u_addr.ip4.addr = prv_w5500_ctrl->static_submask;
  1672. submask->type = IPADDR_TYPE_V4;
  1673. gateway->u_addr.ip4.addr = prv_w5500_ctrl->static_gateway;
  1674. gateway->type = IPADDR_TYPE_V4;
  1675. #else
  1676. ip->ipv4 = prv_w5500_ctrl->static_ip;
  1677. ip->is_ipv6 = 0;
  1678. submask->ipv4 = prv_w5500_ctrl->static_submask;
  1679. submask->is_ipv6 = 0;
  1680. gateway->ipv4 = prv_w5500_ctrl->static_gateway;
  1681. gateway->is_ipv6 = 0;
  1682. #endif
  1683. return 0;
  1684. }
  1685. else
  1686. {
  1687. if (!prv_w5500_ctrl->ip_ready)
  1688. {
  1689. return -1;
  1690. }
  1691. #ifdef LUAT_USE_LWIP
  1692. ip->u_addr.ip4.addr = prv_w5500_ctrl->dhcp_client.ip;
  1693. ip->type = IPADDR_TYPE_V4;
  1694. submask->u_addr.ip4.addr = prv_w5500_ctrl->dhcp_client.submask;
  1695. submask->type = IPADDR_TYPE_V4;
  1696. gateway->u_addr.ip4.addr = prv_w5500_ctrl->dhcp_client.gateway;
  1697. gateway->type = IPADDR_TYPE_V4;
  1698. #else
  1699. ip->ipv4 = prv_w5500_ctrl->dhcp_client.ip;
  1700. ip->is_ipv6 = 0;
  1701. submask->ipv4 = prv_w5500_ctrl->dhcp_client.submask;
  1702. submask->is_ipv6 = 0;
  1703. gateway->ipv4 = prv_w5500_ctrl->dhcp_client.gateway;
  1704. gateway->is_ipv6 = 0;
  1705. #endif
  1706. return 0;
  1707. }
  1708. }
  1709. static int w5500_user_cmd(int socket_id, uint64_t tag, uint32_t cmd, uint32_t value, void *user_data)
  1710. {
  1711. int result = w5500_check_socket(user_data, socket_id, tag);
  1712. if (result) return result;
  1713. switch (cmd)
  1714. {
  1715. case NW_CMD_AUTO_HEART_TIME:
  1716. value = (value + 4) / 5;
  1717. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_REG_OP, W5500_SOCKET_KEEP_TIME, socket_id, value);
  1718. break;
  1719. default:
  1720. return -1;
  1721. }
  1722. return 0;
  1723. }
  1724. static int w5500_dns(const char *domain_name, uint32_t len, void *param, void *user_data)
  1725. {
  1726. if (user_data != prv_w5500_ctrl) return -1;
  1727. char *prv_domain_name = (char *)malloc(len);
  1728. memcpy(prv_domain_name, domain_name, len);
  1729. platform_send_event(prv_w5500_ctrl->task_handle, EV_W5500_SOCKET_DNS, prv_domain_name, len, param);
  1730. return 0;
  1731. }
  1732. static int w5500_set_dns_server(uint8_t server_index, luat_ip_addr_t *ip, void *user_data)
  1733. {
  1734. if (user_data != prv_w5500_ctrl) return -1;
  1735. if (server_index >= MAX_DNS_SERVER) return -1;
  1736. prv_w5500_ctrl->dns_client.dns_server[server_index] = *ip;
  1737. prv_w5500_ctrl->dns_client.is_static_dns[server_index] = 1;
  1738. return 0;
  1739. }
  1740. static void w5500_socket_set_callback(CBFuncEx_t cb_fun, void *param, void *user_data)
  1741. {
  1742. if (user_data != prv_w5500_ctrl) return ;
  1743. ((w5500_ctrl_t *)user_data)->socket_cb = cb_fun?cb_fun:w5500_dummy_callback;
  1744. ((w5500_ctrl_t *)user_data)->user_data = param;
  1745. }
  1746. static int w5500_set_mac_lwip(uint8_t *mac, void *user_data)
  1747. {
  1748. if (user_data != prv_w5500_ctrl) return -1;
  1749. w5500_set_mac(mac);
  1750. }
  1751. static int w5500_set_static_ip_lwip(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1752. {
  1753. if (user_data != prv_w5500_ctrl) return -1;
  1754. w5500_set_static_ip(ip, submask, gateway);
  1755. }
  1756. static int w5500_get_full_ip_info_lwip(luat_ip_addr_t *ip, luat_ip_addr_t *submask, luat_ip_addr_t *gateway, luat_ip_addr_t *ipv6, void *user_data)
  1757. {
  1758. if (user_data != prv_w5500_ctrl) return -1;
  1759. #ifdef LUAT_USE_LWIP
  1760. ipv6->type = 0xff;
  1761. #else
  1762. ipv6->is_ipv6 = 0xff;
  1763. #endif
  1764. return w5500_get_local_ip_info(ip, submask, gateway, user_data);
  1765. }
  1766. static network_adapter_info prv_w5500_adapter =
  1767. {
  1768. .check_ready = w5500_check_ready,
  1769. .create_soceket = w5500_create_soceket,
  1770. .socket_connect = w5500_socket_connect_ex,
  1771. .socket_listen = w5500_socket_listen,
  1772. .socket_accept = w5500_socket_accept,
  1773. .socket_disconnect = w5500_socket_disconnect_ex,
  1774. .socket_close = w5500_socket_close_ex,
  1775. .socket_force_close = w5500_socket_force_close,
  1776. .socket_receive = w5500_socket_receive,
  1777. .socket_send = w5500_socket_send,
  1778. .socket_check = w5500_socket_check,
  1779. .socket_clean = w5500_socket_clean,
  1780. .getsockopt = w5500_getsockopt,
  1781. .setsockopt = w5500_setsockopt,
  1782. .user_cmd = w5500_user_cmd,
  1783. .dns = w5500_dns,
  1784. .dns_ipv6 = w5500_dns,
  1785. .set_dns_server = w5500_set_dns_server,
  1786. .set_mac = w5500_set_mac_lwip,
  1787. .set_static_ip = w5500_set_static_ip_lwip,
  1788. .get_full_ip_info = w5500_get_full_ip_info_lwip,
  1789. .get_local_ip_info = w5500_get_local_ip_info,
  1790. .socket_set_callback = w5500_socket_set_callback,
  1791. .name = "w5500",
  1792. .max_socket_num = MAX_SOCK_NUM - 1,
  1793. .no_accept = 1,
  1794. .is_posix = 0,
  1795. };
  1796. void w5500_register_adapter(int index)
  1797. {
  1798. if (prv_w5500_ctrl)
  1799. {
  1800. network_register_adapter(index, &prv_w5500_adapter, prv_w5500_ctrl);
  1801. }
  1802. }
  1803. #endif