w5500.c 58 KB

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