w5500.c 58 KB

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