socket.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. //*****************************************************************************
  2. //
  3. //! \file socket.c
  4. //! \brief SOCKET APIs Implements file.
  5. //! \details SOCKET APIs like as Berkeley Socket APIs.
  6. //! \version 1.0.3
  7. //! \date 2013/10/21
  8. //! \par Revision history
  9. //! <2014/05/01> V1.0.3. Refer to M20140501
  10. //! 1. Implicit type casting -> Explicit type casting.
  11. //! 2. replace 0x01 with PACK_REMAINED in recvfrom()
  12. //! 3. Validation a destination ip in connect() & sendto():
  13. //! It occurs a fatal error on converting unint32 address if uint8* addr parameter is not aligned by 4byte address.
  14. //! Copy 4 byte addr value into temporary uint32 variable and then compares it.
  15. //! <2013/12/20> V1.0.2 Refer to M20131220
  16. //! Remove Warning.
  17. //! <2013/11/04> V1.0.1 2nd Release. Refer to "20131104".
  18. //! In sendto(), Add to clear timeout interrupt status (Sn_IR_TIMEOUT)
  19. //! <2013/10/21> 1st Release
  20. //! \author MidnightCow
  21. //! \copyright
  22. //!
  23. //! Copyright (c) 2013, WIZnet Co., LTD.
  24. //! All rights reserved.
  25. //!
  26. //! Redistribution and use in source and binary forms, with or without
  27. //! modification, are permitted provided that the following conditions
  28. //! are met:
  29. //!
  30. //! * Redistributions of source code must retain the above copyright
  31. //! notice, this list of conditions and the following disclaimer.
  32. //! * Redistributions in binary form must reproduce the above copyright
  33. //! notice, this list of conditions and the following disclaimer in the
  34. //! documentation and/or other materials provided with the distribution.
  35. //! * Neither the name of the <ORGANIZATION> nor the names of its
  36. //! contributors may be used to endorse or promote products derived
  37. //! from this software without specific prior written permission.
  38. //!
  39. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  42. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  43. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  44. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  45. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  46. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  47. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  49. //! THE POSSIBILITY OF SUCH DAMAGE.
  50. //
  51. //*****************************************************************************
  52. #include "socket.h"
  53. #include "rtl.h" //add slw
  54. #define SOCK_ANY_PORT_NUM 0xC000;
  55. static uint16_t sock_any_port = SOCK_ANY_PORT_NUM;
  56. static uint16_t sock_io_mode = 0;
  57. static uint16_t sock_is_sending = 0;
  58. static uint16_t sock_remained_size[_WIZCHIP_SOCK_NUM_] = {0,0,};
  59. static uint8_t sock_pack_info[_WIZCHIP_SOCK_NUM_] = {0,};
  60. #if _WIZCHIP_ == 5200
  61. static uint16_t sock_next_rd[_WIZCHIP_SOCK_NUM_] ={0,};
  62. #endif
  63. #define CHECK_SOCKNUM() \
  64. do{ \
  65. if(sn > _WIZCHIP_SOCK_NUM_) return SOCKERR_SOCKNUM; \
  66. }while(0); \
  67. #define CHECK_SOCKMODE(mode) \
  68. do{ \
  69. if((getSn_MR(sn) & 0x0F) != mode) return SOCKERR_SOCKMODE; \
  70. }while(0); \
  71. #define CHECK_SOCKINIT() \
  72. do{ \
  73. if((getSn_SR(sn) != SOCK_INIT)) return SOCKERR_SOCKINIT; \
  74. }while(0); \
  75. #define CHECK_SOCKDATA() \
  76. do{ \
  77. if(len == 0) return SOCKERR_DATALEN; \
  78. }while(0); \
  79. int8_t socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
  80. {
  81. CHECK_SOCKNUM();
  82. switch(protocol)
  83. {
  84. case Sn_MR_TCP :
  85. case Sn_MR_UDP :
  86. case Sn_MR_MACRAW :
  87. break;
  88. #if ( _WIZCHIP_ < 5200 )
  89. case Sn_MR_IPRAW :
  90. case Sn_MR_PPPoE :
  91. break;
  92. #endif
  93. default :
  94. return SOCKERR_SOCKMODE;
  95. }
  96. if((flag & 0x06) != 0) return SOCKERR_SOCKFLAG;
  97. #if _WIZCHIP_ == 5200
  98. if(flag & 0x10) return SOCKERR_SOCKFLAG;
  99. #endif
  100. if(flag != 0)
  101. {
  102. switch(protocol)
  103. {
  104. case Sn_MR_TCP:
  105. if((flag & (SF_TCP_NODELAY|SF_IO_NONBLOCK))==0) return SOCKERR_SOCKFLAG;
  106. break;
  107. case Sn_MR_UDP:
  108. if(flag & SF_IGMP_VER2)
  109. {
  110. if((flag & SF_MULTI_ENABLE)==0) return SOCKERR_SOCKFLAG;
  111. }
  112. #if _WIZCHIP_ == 5500
  113. if(flag & SF_UNI_BLOCK)
  114. {
  115. if((flag & SF_MULTI_ENABLE) == 0) return SOCKERR_SOCKFLAG;
  116. }
  117. #endif
  118. break;
  119. default:
  120. break;
  121. }
  122. }
  123. close(sn);
  124. setSn_MR(sn, (protocol | (flag & 0xF0)));
  125. if(!port)
  126. {
  127. port = sock_any_port++;
  128. if(sock_any_port == 0xFFF0) sock_any_port = SOCK_ANY_PORT_NUM;
  129. }
  130. setSn_PORT(sn,port);
  131. setSn_CR(sn,Sn_CR_OPEN);
  132. while(getSn_CR(sn));
  133. sock_io_mode |= ((flag & SF_IO_NONBLOCK) << sn);
  134. sock_is_sending &= ~(1<<sn);
  135. sock_remained_size[sn] = 0;
  136. sock_pack_info[sn] = 0;
  137. while(getSn_SR(sn) == SOCK_CLOSED);
  138. return (int8_t)sn;
  139. }
  140. int8_t close(uint8_t sn)
  141. {
  142. CHECK_SOCKNUM();
  143. setSn_CR(sn,Sn_CR_CLOSE);
  144. /* wait to process the command... */
  145. while( getSn_CR(sn) );
  146. /* clear all interrupt of the socket. */
  147. setSn_IR(sn, 0xFF);
  148. sock_is_sending &= ~(1<<sn);
  149. sock_remained_size[sn] = 0;
  150. sock_pack_info[sn] = 0;
  151. while(getSn_SR(sn) != SOCK_CLOSED);
  152. return SOCK_OK;
  153. }
  154. int8_t listen(uint8_t sn)
  155. {
  156. CHECK_SOCKNUM();
  157. CHECK_SOCKMODE(Sn_MR_TCP);
  158. CHECK_SOCKINIT();
  159. setSn_CR(sn,Sn_CR_LISTEN);
  160. while(getSn_CR(sn));
  161. while(getSn_SR(sn) != SOCK_LISTEN)
  162. {
  163. if(getSn_CR(sn) == SOCK_CLOSED)
  164. {
  165. close(sn);
  166. return SOCKERR_SOCKCLOSED;
  167. }
  168. }
  169. return SOCK_OK;
  170. }
  171. int8_t connect(uint8_t sn, uint8_t * addr, uint16_t port)
  172. {
  173. int timeout=0;
  174. CHECK_SOCKNUM();
  175. CHECK_SOCKMODE(Sn_MR_TCP);
  176. CHECK_SOCKINIT();
  177. //M20140501 : For avoiding fatal error on memory align mismatched
  178. //if( *((uint32_t*)addr) == 0xFFFFFFFF || *((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
  179. {
  180. uint32_t taddr;
  181. taddr = ((uint32_t)addr[0] & 0x000000FF);
  182. taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
  183. taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
  184. taddr = (taddr << 8) + ((uint32_t)addr[0] & 0x000000FF);
  185. if( taddr == 0xFFFFFFFF || taddr == 0) return SOCKERR_IPINVALID;
  186. }
  187. //
  188. if(port == 0) return SOCKERR_PORTZERO;
  189. setSn_DIPR(sn,addr);
  190. setSn_DPORT(sn,port);
  191. #if _WIZCHIP_ == 5200 // for W5200 ARP errata
  192. setSUBR(0);
  193. #endif
  194. setSn_CR(sn,Sn_CR_CONNECT);
  195. while(getSn_CR(sn)){
  196. os_dly_wait(1);//add by slw
  197. if(++timeout>1000)return -1;
  198. }
  199. timeout=0;
  200. //if(sock_io_mode & (1<<sn)) return SOCK_BUSY;//modify by slw
  201. while(getSn_SR(sn) != SOCK_ESTABLISHED)
  202. {
  203. if (getSn_IR(sn) & Sn_IR_TIMEOUT)
  204. {
  205. setSn_IR(sn, Sn_IR_TIMEOUT);
  206. #if _WIZCHIP_ == 5200 // for W5200 ARP errata
  207. setSUBR((uint8_t*)"\x00\x00\x00\x00");
  208. #endif
  209. return SOCKERR_TIMEOUT;
  210. }
  211. os_dly_wait(1);//add by slw
  212. if(++timeout>2000)return -2;
  213. }
  214. #if _WIZCHIP_ == 5200 // for W5200 ARP errata
  215. setSUBR((uint8_t*)"\x00\x00\x00\x00");
  216. #endif
  217. return SOCK_OK;
  218. }
  219. int8_t disconnect(uint8_t sn)
  220. {
  221. CHECK_SOCKNUM();
  222. CHECK_SOCKMODE(Sn_MR_TCP);
  223. setSn_CR(sn,Sn_CR_DISCON);
  224. /* wait to process the command... */
  225. while(getSn_CR(sn));
  226. sock_is_sending &= ~(1<<sn);
  227. if(sock_io_mode & (1<<sn)) return SOCK_BUSY;
  228. while(getSn_SR(sn) != SOCK_CLOSED)
  229. {
  230. if(getSn_IR(sn) & Sn_IR_TIMEOUT)
  231. {
  232. close(sn);
  233. return SOCKERR_TIMEOUT;
  234. }
  235. }
  236. return SOCK_OK;
  237. }
  238. int32_t send(uint8_t sn, uint8_t * buf, uint16_t len)
  239. {
  240. uint8_t tmp=0;
  241. uint16_t freesize=0;
  242. CHECK_SOCKNUM();
  243. CHECK_SOCKMODE(Sn_MR_TCP);
  244. CHECK_SOCKDATA();
  245. tmp = getSn_SR(sn);
  246. if(tmp != SOCK_ESTABLISHED && tmp != SOCK_CLOSE_WAIT) return SOCKERR_SOCKSTATUS;
  247. if( sock_is_sending & (1<<sn) )
  248. {
  249. tmp = getSn_IR(sn);
  250. if(tmp & Sn_IR_SENDOK)
  251. {
  252. setSn_IR(sn, Sn_IR_SENDOK);
  253. #if _WZICHIP_ == 5200
  254. if(getSn_TX_RD(sn) != sock_next_rd[sn])
  255. {
  256. setSn_CR(sn,Sn_CR_SEND);
  257. while(getSn_CR(sn));
  258. return SOCKERR_BUSY;
  259. }
  260. #endif
  261. sock_is_sending &= ~(1<<sn);
  262. }
  263. else if(tmp & Sn_IR_TIMEOUT)
  264. {
  265. close(sn);
  266. return SOCKERR_TIMEOUT;
  267. }
  268. else return SOCK_BUSY;
  269. }
  270. freesize = getSn_TxMAX(sn);
  271. if (len > freesize) len = freesize; // check size not to exceed MAX size.
  272. while(1)
  273. {
  274. freesize = getSn_TX_FSR(sn);
  275. tmp = getSn_SR(sn);
  276. if ((tmp != SOCK_ESTABLISHED) && (tmp != SOCK_CLOSE_WAIT))
  277. {
  278. close(sn);
  279. return SOCKERR_SOCKSTATUS;
  280. }
  281. if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY;
  282. if(len <= freesize) break;
  283. }
  284. wiz_send_data(sn, buf, len);
  285. #if _WIZCHIP_ == 5200
  286. sock_next_rd[sn] = getSn_TX_RD(sn) + len;
  287. #endif
  288. setSn_CR(sn,Sn_CR_SEND);
  289. /* wait to process the command... */
  290. while(getSn_CR(sn));
  291. sock_is_sending |= (1 << sn);
  292. return len;
  293. }
  294. int32_t recv(uint8_t sn, uint8_t * buf, uint16_t len)
  295. {
  296. uint8_t tmp = 0;
  297. uint16_t recvsize = 0;
  298. CHECK_SOCKNUM();
  299. CHECK_SOCKMODE(Sn_MR_TCP);
  300. CHECK_SOCKDATA();
  301. recvsize = getSn_RxMAX(sn);
  302. if(recvsize < len) len = recvsize;
  303. while(1)
  304. {
  305. recvsize = getSn_RX_RSR(sn);
  306. tmp = getSn_SR(sn);
  307. if (tmp != SOCK_ESTABLISHED)
  308. {
  309. if(tmp == SOCK_CLOSE_WAIT)
  310. {
  311. if(recvsize != 0) break;
  312. else if(getSn_TX_FSR(sn) == getSn_TxMAX(sn))
  313. {
  314. close(sn);
  315. return SOCKERR_SOCKSTATUS;
  316. }
  317. }
  318. else
  319. {
  320. close(sn);
  321. return SOCKERR_SOCKSTATUS;
  322. }
  323. }
  324. if((sock_io_mode & (1<<sn)) && (recvsize == 0)) return SOCK_BUSY;
  325. if(recvsize != 0) break;
  326. };
  327. if(recvsize < len) len = recvsize;
  328. wiz_recv_data(sn, buf, len);
  329. setSn_CR(sn,Sn_CR_RECV);
  330. while(getSn_CR(sn));
  331. return len;
  332. }
  333. int32_t sendto(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port)
  334. {
  335. uint8_t tmp = 0;
  336. uint16_t freesize = 0;
  337. CHECK_SOCKNUM();
  338. switch(getSn_MR(sn) & 0x0F)
  339. {
  340. case Sn_MR_UDP:
  341. case Sn_MR_MACRAW:
  342. break;
  343. default:
  344. return SOCKERR_SOCKMODE;
  345. }
  346. CHECK_SOCKDATA();
  347. //M20140501 : For avoiding fatal error on memory align mismatched
  348. //if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
  349. {
  350. uint32_t taddr;
  351. taddr = ((uint32_t)addr[0]) & 0x000000FF;
  352. taddr = (taddr << 8) + ((uint32_t)addr[1] & 0x000000FF);
  353. taddr = (taddr << 8) + ((uint32_t)addr[2] & 0x000000FF);
  354. taddr = (taddr << 8) + ((uint32_t)addr[3] & 0x000000FF);
  355. }
  356. //
  357. if(*((uint32_t*)addr) == 0) return SOCKERR_IPINVALID;
  358. if(port == 0) return SOCKERR_PORTZERO;
  359. tmp = getSn_SR(sn);
  360. if(tmp != SOCK_MACRAW && tmp != SOCK_UDP) return SOCKERR_SOCKSTATUS;
  361. setSn_DIPR(sn,addr);
  362. setSn_DPORT(sn,port);
  363. freesize = getSn_TxMAX(sn);
  364. if (len > freesize) len = freesize; // check size not to exceed MAX size.
  365. while(1)
  366. {
  367. freesize = getSn_TX_FSR(sn);
  368. if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED;
  369. if( (sock_io_mode & (1<<sn)) && (len > freesize) ) return SOCK_BUSY;
  370. if(len <= freesize) break;
  371. };
  372. wiz_send_data(sn, buf, len);
  373. #if _WIZCHIP_ == 5200 // for W5200 ARP errata
  374. setSUBR(0);
  375. #endif
  376. setSn_CR(sn,Sn_CR_SEND);
  377. /* wait to process the command... */
  378. while(getSn_CR(sn));
  379. #if _WIZCHIP_ == 5200 // for W5200 ARP errata
  380. setSUBR((uint8_t*)"\x00\x00\x00\x00");
  381. #endif
  382. while(1)
  383. {
  384. tmp = getSn_IR(sn);
  385. if(tmp & Sn_IR_SENDOK)
  386. {
  387. setSn_IR(sn, Sn_IR_SENDOK);
  388. break;
  389. }
  390. //M:20131104
  391. //else if(tmp & Sn_IR_TIMEOUT) return SOCKERR_TIMEOUT;
  392. else if(tmp & Sn_IR_TIMEOUT)
  393. {
  394. setSn_IR(sn, Sn_IR_TIMEOUT);
  395. return SOCKERR_TIMEOUT;
  396. }
  397. ////////////
  398. }
  399. return len;
  400. }
  401. int32_t recvfrom(uint8_t sn, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port)
  402. {
  403. uint8_t mr;
  404. uint8_t head[8];
  405. uint16_t pack_len=0;
  406. CHECK_SOCKNUM();
  407. //CHECK_SOCKMODE(Sn_MR_UDP);
  408. switch((mr=getSn_MR(sn)) & 0x0F)
  409. {
  410. case Sn_MR_UDP:
  411. case Sn_MR_MACRAW:
  412. break;
  413. #if ( _WIZCHIP_ < 5200 )
  414. case Sn_MR_IPRAW:
  415. case Sn_MR_PPPoE:
  416. break;
  417. #endif
  418. default:
  419. return SOCKERR_SOCKMODE;
  420. }
  421. CHECK_SOCKDATA();
  422. if(sock_remained_size[sn] == 0)
  423. {
  424. while(1)
  425. {
  426. pack_len = getSn_RX_RSR(sn);
  427. if(getSn_SR(sn) == SOCK_CLOSED) return SOCKERR_SOCKCLOSED;
  428. if( (sock_io_mode & (1<<sn)) && (pack_len == 0) ) return SOCK_BUSY;
  429. if(pack_len != 0) break;
  430. };
  431. }
  432. sock_pack_info[sn] = PACK_COMPLETED;
  433. switch (mr & 0x07)
  434. {
  435. case Sn_MR_UDP :
  436. if(sock_remained_size[sn] == 0)
  437. {
  438. wiz_recv_data(sn, head, 8);
  439. setSn_CR(sn,Sn_CR_RECV);
  440. while(getSn_CR(sn));
  441. // read peer's IP address, port number & packet length
  442. addr[0] = head[0];
  443. addr[1] = head[1];
  444. addr[2] = head[2];
  445. addr[3] = head[3];
  446. *port = head[4];
  447. *port = (*port << 8) + head[5];
  448. sock_remained_size[sn] = head[6];
  449. sock_remained_size[sn] = (sock_remained_size[sn] << 8) + head[7];
  450. sock_pack_info[sn] = PACK_FIRST;
  451. }
  452. if(len < sock_remained_size[sn]) pack_len = len;
  453. else pack_len = sock_remained_size[sn];
  454. //
  455. // Need to packet length check (default 1472)
  456. //
  457. wiz_recv_data(sn, buf, pack_len); // data copy.
  458. break;
  459. case Sn_MR_MACRAW :
  460. if(sock_remained_size[sn] == 0)
  461. {
  462. wiz_recv_data(sn, head, 2);
  463. setSn_CR(sn,Sn_CR_RECV);
  464. while(getSn_CR(sn));
  465. // read peer's IP address, port number & packet length
  466. sock_remained_size[sn] = head[0];
  467. sock_remained_size[sn] = (sock_remained_size[sn] <<8) + head[1];
  468. if(sock_remained_size[sn] > 1514)
  469. {
  470. close(sn);
  471. return SOCKFATAL_PACKLEN;
  472. }
  473. sock_pack_info[sn] = PACK_FIRST;
  474. }
  475. if(len < sock_remained_size[sn]) pack_len = len;
  476. else pack_len = sock_remained_size[sn];
  477. wiz_recv_data(sn,buf,pack_len);
  478. break;
  479. #if ( _WIZCHIP_ < 5200 )
  480. case Sn_MR_IPRAW:
  481. if(sock_remained_size[sn] == 0)
  482. {
  483. wiz_recv_data(sn, head, 6);
  484. setSn_CR(sn,Sn_CR_RECV);
  485. while(getSn_CR(sn));
  486. addr[0] = head[0];
  487. addr[1] = head[1];
  488. addr[2] = head[2];
  489. addr[3] = head[3];
  490. sock_remained_size[sn] = head[4];
  491. sock_remaiend_size[sn] = (sock_remained_size[sn] << 8) + head[5];
  492. sock_pack_info[sn] = PACK_FIRST;
  493. }
  494. //
  495. // Need to packet length check
  496. //
  497. if(len < sock_remained_size[sn]) pack_len = len;
  498. else pack_len = sock_remained_size[sn];
  499. wiz_recv_data(sn, buf, pack_len); // data copy.
  500. break;
  501. #endif
  502. default:
  503. wiz_recv_ignore(sn, pack_len); // data copy.
  504. sock_remained_size[sn] = pack_len;
  505. break;
  506. }
  507. setSn_CR(sn,Sn_CR_RECV);
  508. /* wait to process the command... */
  509. while(getSn_CR(sn)) ;
  510. sock_remained_size[sn] -= pack_len;
  511. //M20140501 : replace 0x01 with PACK_REMAINED
  512. //if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= 0x01;
  513. if(sock_remained_size[sn] != 0) sock_pack_info[sn] |= PACK_REMAINED;
  514. //
  515. return pack_len;
  516. }
  517. int8_t ctlsocket(uint8_t sn, ctlsock_type cstype, void* arg)
  518. {
  519. uint8_t tmp = 0;
  520. CHECK_SOCKNUM();
  521. switch(cstype)
  522. {
  523. case CS_SET_IOMODE:
  524. tmp = *((uint8_t*)arg);
  525. if(tmp == SOCK_IO_NONBLOCK) sock_io_mode |= (1<<sn);
  526. else if(tmp == SOCK_IO_BLOCK) sock_io_mode &= ~(1<<sn);
  527. else return SOCKERR_ARG;
  528. break;
  529. case CS_GET_IOMODE:
  530. //M20140501 : implict type casting -> explict type casting
  531. //*((uint8_t*)arg) = (sock_io_mode >> sn) & 0x0001;
  532. *((uint8_t*)arg) = (uint8_t)((sock_io_mode >> sn) & 0x0001);
  533. //
  534. break;
  535. case CS_GET_MAXTXBUF:
  536. *((uint16_t*)arg) = getSn_TxMAX(sn);
  537. break;
  538. case CS_GET_MAXRXBUF:
  539. *((uint16_t*)arg) = getSn_RxMAX(sn);
  540. break;
  541. case CS_CLR_INTERRUPT:
  542. if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG;
  543. setSn_IR(sn,*(uint8_t*)arg);
  544. break;
  545. case CS_GET_INTERRUPT:
  546. *((uint8_t*)arg) = getSn_IR(sn);
  547. break;
  548. case CS_SET_INTMASK:
  549. if( (*(uint8_t*)arg) > SIK_ALL) return SOCKERR_ARG;
  550. setSn_IMR(sn,*(uint8_t*)arg);
  551. break;
  552. case CS_GET_INTMASK:
  553. *((uint8_t*)arg) = getSn_IMR(sn);
  554. default:
  555. return SOCKERR_ARG;
  556. }
  557. return SOCK_OK;
  558. }
  559. int8_t setsockopt(uint8_t sn, sockopt_type sotype, void* arg)
  560. {
  561. // M20131220 : Remove warning
  562. //uint8_t tmp;
  563. CHECK_SOCKNUM();
  564. switch(sotype)
  565. {
  566. case SO_TTL:
  567. setSn_TTL(sn,*(uint8_t*)arg);
  568. break;
  569. case SO_TOS:
  570. setSn_TOS(sn,*(uint8_t*)arg);
  571. break;
  572. case SO_MSS:
  573. setSn_MSSR(sn,*(uint16_t*)arg);
  574. break;
  575. case SO_DESTIP:
  576. setSn_DIPR(sn, (uint8_t*)arg);
  577. break;
  578. case SO_DESTPORT:
  579. setSn_DPORT(sn, *(uint16_t*)arg);
  580. break;
  581. #if _WIZCHIP_ != 5100
  582. case SO_KEEPALIVESEND:
  583. CHECK_SOCKMODE(Sn_MR_TCP);
  584. #if _WIZCHIP_ > 5200
  585. if(getSn_KPALVTR(sn) != 0) return SOCKERR_SOCKOPT;
  586. #endif
  587. setSn_CR(sn,Sn_CR_SEND_KEEP);
  588. while(getSn_CR(sn) != 0)
  589. {
  590. // M20131220
  591. //if ((tmp = getSn_IR(sn)) & Sn_IR_TIMEOUT)
  592. if (getSn_IR(sn) & Sn_IR_TIMEOUT)
  593. {
  594. setSn_IR(sn, Sn_IR_TIMEOUT);
  595. return SOCKERR_TIMEOUT;
  596. }
  597. }
  598. break;
  599. #if _WIZCHIP_ > 5200
  600. case SO_KEEPALIVEAUTO:
  601. CHECK_SOCKMODE(Sn_MR_TCP);
  602. setSn_KPALVTR(sn,*(uint8_t*)arg);
  603. break;
  604. #endif
  605. #endif
  606. default:
  607. return SOCKERR_ARG;
  608. }
  609. return SOCK_OK;
  610. }
  611. int8_t getsockopt(uint8_t sn, sockopt_type sotype, void* arg)
  612. {
  613. CHECK_SOCKNUM();
  614. switch(sotype)
  615. {
  616. case SO_FLAG:
  617. *(uint8_t*)arg = getSn_MR(sn) & 0xF0;
  618. break;
  619. case SO_TTL:
  620. *(uint8_t*) arg = getSn_TTL(sn);
  621. break;
  622. case SO_TOS:
  623. *(uint8_t*) arg = getSn_TOS(sn);
  624. break;
  625. case SO_MSS:
  626. *(uint8_t*) arg = getSn_MSSR(sn);
  627. case SO_DESTIP:
  628. getSn_DIPR(sn, (uint8_t*)arg);
  629. break;
  630. case SO_DESTPORT:
  631. *(uint16_t*) arg = getSn_DPORT(sn);
  632. break;
  633. #if _WIZCHIP_ > 5200
  634. case SO_KEEPALIVEAUTO:
  635. CHECK_SOCKMODE(Sn_MR_TCP);
  636. *(uint16_t*) arg = getSn_KPALVTR(sn);
  637. break;
  638. #endif
  639. case SO_SENDBUF:
  640. *(uint16_t*) arg = getSn_TX_FSR(sn);
  641. case SO_RECVBUF:
  642. *(uint16_t*) arg = getSn_RX_RSR(sn);
  643. case SO_STATUS:
  644. *(uint8_t*) arg = getSn_SR(sn);
  645. break;
  646. case SO_REMAINSIZE:
  647. if(getSn_MR(sn) == Sn_MR_TCP)
  648. *(uint16_t*)arg = getSn_RX_RSR(sn);
  649. else
  650. *(uint16_t*)arg = sock_remained_size[sn];
  651. break;
  652. case SO_PACKINFO:
  653. CHECK_SOCKMODE(Sn_MR_TCP);
  654. *(uint8_t*)arg = sock_pack_info[sn];
  655. break;
  656. default:
  657. return SOCKERR_SOCKOPT;
  658. }
  659. return SOCK_OK;
  660. }