wizchip_socket.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. //*****************************************************************************
  2. //
  3. //! \file wizchip_socket.h
  4. //! \brief SOCKET APIs Header file.
  5. //! \details SOCKET APIs like as berkeley socket api.
  6. //! \version 1.0.2
  7. //! \date 2013/10/21
  8. //! \par Revision history
  9. //! <2015/02/05> Notice
  10. //! The version history is not updated after this point.
  11. //! Download the latest version directly from GitHub. Please visit the our GitHub repository for ioLibrary.
  12. //! >> https://github.com/Wiznet/ioLibrary_Driver
  13. //! <2014/05/01> V1.0.2. Refer to M20140501
  14. //! 1. Modify the comment : SO_REMAINED -> PACK_REMAINED
  15. //! 2. Add the comment as zero byte udp data reception in getsockopt().
  16. //! <2013/10/21> 1st Release
  17. //! \author MidnightCow
  18. //! \copyright
  19. //!
  20. //! Copyright (c) 2013, WIZnet Co., LTD.
  21. //! All rights reserved.
  22. //!
  23. //! Redistribution and use in source and binary forms, with or without
  24. //! modification, are permitted provided that the following conditions
  25. //! are met:
  26. //!
  27. //! * Redistributions of source code must retain the above copyright
  28. //! notice, this list of conditions and the following disclaimer.
  29. //! * Redistributions in binary form must reproduce the above copyright
  30. //! notice, this list of conditions and the following disclaimer in the
  31. //! documentation and/or other materials provided with the distribution.
  32. //! * Neither the name of the <ORGANIZATION> nor the names of its
  33. //! contributors may be used to endorse or promote products derived
  34. //! from this software without specific prior written permission.
  35. //!
  36. //! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  37. //! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. //! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  39. //! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  40. //! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  41. //! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  42. //! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  43. //! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  44. //! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. //! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  46. //! THE POSSIBILITY OF SUCH DAMAGE.
  47. //
  48. //*****************************************************************************
  49. /**
  50. * @defgroup WIZnet_socket_APIs 1. WIZnet socket APIs
  51. * @brief WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface.
  52. * But there is a little bit of difference.
  53. * @details
  54. * <b> Comparison between WIZnet and Berkeley SOCKET APIs </b>
  55. * <table>
  56. * <tr> <td><b>API</b></td> <td><b>WIZnet</b></td> <td><b>Berkeley</b></td> </tr>
  57. * <tr> <td>socket()</td> <td>O</td> <td>O</td> </tr>
  58. * <tr> <td><b>bind()</b></td> <td>X</td> <td>O</td> </tr>
  59. * <tr> <td><b>listen()</b></td> <td>O</td> <td>O</td> </tr>
  60. * <tr> <td><b>connect()</b></td> <td>O</td> <td>O</td> </tr>
  61. * <tr> <td><b>accept()</b></td> <td>X</td> <td>O</td> </tr>
  62. * <tr> <td><b>recv()</b></td> <td>O</td> <td>O</td> </tr>
  63. * <tr> <td><b>send()</b></td> <td>O</td> <td>O</td> </tr>
  64. * <tr> <td><b>recvfrom()</b></td> <td>O</td> <td>O</td> </tr>
  65. * <tr> <td><b>sendto()</b></td> <td>O</td> <td>O</td> </tr>
  66. * <tr> <td><b>closesocket()</b></td> <td>O<br>close() & disconnect()</td> <td>O</td> </tr>
  67. * </table>
  68. * There are @b bind() and @b accept() functions in @b Berkeley SOCKET API but,
  69. * not in @b WIZnet SOCKET API. Because socket() of WIZnet is not only creating a SOCKET but also binding a local port number,
  70. * and listen() of WIZnet is not only listening to connection request from client but also accepting the connection request. \n
  71. * When you program "TCP SERVER" with Berkeley SOCKET API, you can use only one listen port.
  72. * When the listen SOCKET accepts a connection request from a client, it keeps listening.
  73. * After accepting the connection request, a new SOCKET is created and the new SOCKET is used in communication with the client. \n
  74. * Following figure shows network flow diagram by Berkeley SOCKET API.
  75. * @image html Berkeley_SOCKET.jpg "<Berkeley SOCKET API>"
  76. * But, When you program "TCP SERVER" with WIZnet SOCKET API, you can use as many as 8 listen SOCKET with same port number. \n
  77. * Because there's no accept() in WIZnet SOCKET APIs, when the listen SOCKET accepts a connection request from a client,
  78. * it is changed in order to communicate with the client.
  79. * And the changed SOCKET is not listening any more and is dedicated for communicating with the client. \n
  80. * If there're many listen SOCKET with same listen port number and a client requests a connection,
  81. * the SOCKET which has the smallest SOCKET number accepts the request and is changed as communication SOCKET. \n
  82. * Following figure shows network flow diagram by WIZnet SOCKET API.
  83. * @image html WIZnet_SOCKET.jpg "<WIZnet SOCKET API>"
  84. */
  85. #ifndef _WIZCHIP_SOCKET_H_
  86. #define _WIZCHIP_SOCKET_H_
  87. #ifdef __cplusplus
  88. extern "C"
  89. {
  90. #endif
  91. #include "wizchip_conf.h"
  92. #define SOCKET uint8_t // SOCKET type define for legacy driver
  93. #define SOCK_OK 1 // Result is OK about socket process.
  94. #define SOCK_BUSY 0 // Socket is busy on processing the operation. Valid only Non-block IO Mode.
  95. #define SOCK_FATAL -100 // Result is fatal error about socket process.
  96. #define SOCK_ERROR 0
  97. #define SOCKERR_SOCKNUM (SOCK_ERROR - 1) // Invalid socket number
  98. #define SOCKERR_SOCKOPT (SOCK_ERROR - 2) // Invalid socket option
  99. #define SOCKERR_SOCKINIT (SOCK_ERROR - 3) // Socket is not initialized or SIPR is Zero IP address when Sn_MR_TCP
  100. #define SOCKERR_SOCKCLOSED (SOCK_ERROR - 4) // Socket unexpectedly closed.
  101. #define SOCKERR_SOCKMODE (SOCK_ERROR - 5) // Invalid socket mode for socket operation.
  102. #define SOCKERR_SOCKFLAG (SOCK_ERROR - 6) // Invalid socket flag
  103. #define SOCKERR_SOCKSTATUS (SOCK_ERROR - 7) // Invalid socket status for socket operation.
  104. #define SOCKERR_ARG (SOCK_ERROR - 10) // Invalid argument.
  105. #define SOCKERR_PORTZERO (SOCK_ERROR - 11) // Port number is zero
  106. #define SOCKERR_IPINVALID (SOCK_ERROR - 12) // Invalid IP address
  107. #define SOCKERR_TIMEOUT (SOCK_ERROR - 13) // Timeout occurred
  108. #define SOCKERR_DATALEN (SOCK_ERROR - 14) // Data length is zero or greater than buffer max size.
  109. #define SOCKERR_BUFFER (SOCK_ERROR - 15) // Socket buffer is not enough for data communication.
  110. #define SOCKFATAL_PACKLEN (SOCK_FATAL - 1) // Invalid packet length. Fatal Error.
  111. /*
  112. * SOCKET FLAG
  113. */
  114. #define SF_ETHER_OWN (Sn_MR_MFEN) // In @ref Sn_MR_MACRAW, Receive only the packet as broadcast, multicast and own packet
  115. #define SF_IGMP_VER2 (Sn_MR_MC) // In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE, Select IGMP version 2.
  116. #define SF_TCP_NODELAY (Sn_MR_ND) // In @ref Sn_MR_TCP, Use to nodelayed ack.
  117. #define SF_MULTI_ENABLE (Sn_MR_MULTI) // In @ref Sn_MR_UDP, Enable multicast mode.
  118. #if _WIZCHIP_ == 5500
  119. #define SF_BROAD_BLOCK (Sn_MR_BCASTB) // In @ref Sn_MR_UDP or @ref Sn_MR_MACRAW, Block broadcast packet. Valid only in W5500
  120. #define SF_MULTI_BLOCK (Sn_MR_MMB) // In @ref Sn_MR_MACRAW, Block multicast packet. Valid only in W5500
  121. #define SF_IPv6_BLOCK (Sn_MR_MIP6B) // In @ref Sn_MR_MACRAW, Block IPv6 packet. Valid only in W5500
  122. #define SF_UNI_BLOCK (Sn_MR_UCASTB) // In @ref Sn_MR_UDP with \ref SF_MULTI_ENABLE. Valid only in W5500
  123. #endif
  124. // A201505 : For W5300
  125. #if _WIZCHIP_ == 5300
  126. #define SF_TCP_ALIGN 0x02 // Valid only \ref Sn_MR_TCP and W5300, refer to \ref Sn_MR_ALIGN
  127. #endif
  128. #define SF_IO_NONBLOCK 0x01 // Socket nonblock io mode. It used parameter in \ref socket().
  129. /*
  130. * UDP & MACRAW Packet Infomation
  131. */
  132. #define PACK_FIRST 0x80 // In Non-TCP packet, It indicates to start receiving a packet. (When W5300, This flag can be applied)
  133. #define PACK_REMAINED 0x01 // In Non-TCP packet, It indicates to remain a packet to be received. (When W5300, This flag can be applied)
  134. #define PACK_COMPLETED 0x00 // In Non-TCP packet, It indicates to complete to receive a packet. (When W5300, This flag can be applied)
  135. // A20150601 : For Integrating with W5300
  136. #define PACK_FIFOBYTE 0x02 // bbbb Valid only W5300, It indicate to have read already the Sn_RX_FIFOR.
  137. //
  138. /**
  139. * @ingroup WIZnet_socket_APIs
  140. * @brief Open a socket.
  141. * @details Initializes the socket with 'sn' passed as parameter and open.
  142. *
  143. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  144. * @param protocol Protocol type to operate such as TCP, UDP and MACRAW.
  145. * @param port Port number to be bined.
  146. * @param flag Socket flags as \ref SF_ETHER_OWN, \ref SF_IGMP_VER2, \ref SF_TCP_NODELAY, \ref SF_MULTI_ENABLE, \ref SF_IO_NONBLOCK and so on.\n
  147. * Valid flags only in W5500 : @ref SF_BROAD_BLOCK, @ref SF_MULTI_BLOCK, @ref SF_IPv6_BLOCK, and @ref SF_UNI_BLOCK.
  148. * @sa Sn_MR
  149. *
  150. * @return @b Success : The socket number @b 'sn' passed as parameter\n
  151. * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number\n
  152. * @ref SOCKERR_SOCKMODE - Not support socket mode as TCP, UDP, and so on. \n
  153. * @ref SOCKERR_SOCKFLAG - Invaild socket flag.
  154. */
  155. int8_t wizchip_socket(uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag);
  156. /**
  157. * @ingroup WIZnet_socket_APIs
  158. * @brief Close a socket.
  159. * @details It closes the socket with @b'sn' passed as parameter.
  160. *
  161. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  162. *
  163. * @return @b Success : @ref SOCK_OK \n
  164. * @b Fail : @ref SOCKERR_SOCKNUM - Invalid socket number
  165. */
  166. int8_t wizchip_close(uint8_t sn);
  167. /**
  168. * @ingroup WIZnet_socket_APIs
  169. * @brief Listen to a connection request from a client.
  170. * @details It is listening to a connection request from a client.
  171. * If connection request is accepted successfully, the connection is established. Socket sn is used in passive(server) mode.
  172. *
  173. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  174. * @return @b Success : @ref SOCK_OK \n
  175. * @b Fail :\n @ref SOCKERR_SOCKINIT - Socket is not initialized \n
  176. * @ref SOCKERR_SOCKCLOSED - Socket closed unexpectedly.
  177. */
  178. int8_t wizchip_listen(uint8_t sn);
  179. /**
  180. * @ingroup WIZnet_socket_APIs
  181. * @brief Try to connect a server.
  182. * @details It requests connection to the server with destination IP address and port number passed as parameter.\n
  183. * @note It is valid only in TCP client mode.
  184. * In block io mode, it does not return until connection is completed.
  185. * In Non-block io mode, it return @ref SOCK_BUSY immediately.
  186. *
  187. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  188. * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes.
  189. * @param port Destination port number.
  190. *
  191. * @return @b Success : @ref SOCK_OK \n
  192. * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number\n
  193. * @ref SOCKERR_SOCKMODE - Invalid socket mode\n
  194. * @ref SOCKERR_SOCKINIT - Socket is not initialized\n
  195. * @ref SOCKERR_IPINVALID - Wrong server IP address\n
  196. * @ref SOCKERR_PORTZERO - Server port zero\n
  197. * @ref SOCKERR_TIMEOUT - Timeout occurred during request connection\n
  198. * @ref SOCK_BUSY - In non-block io mode, it returned immediately\n
  199. */
  200. int8_t wizchip_connect(uint8_t sn, uint8_t *addr, uint16_t port);
  201. /**
  202. * @ingroup WIZnet_socket_APIs
  203. * @brief Try to disconnect a connection socket.
  204. * @details It sends request message to disconnect the TCP socket 'sn' passed as parameter to the server or client.
  205. * @note It is valid only in TCP server or client mode. \n
  206. * In block io mode, it does not return until disconnection is completed. \n
  207. * In Non-block io mode, it return @ref SOCK_BUSY immediately. \n
  208. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  209. * @return @b Success : @ref SOCK_OK \n
  210. * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number \n
  211. * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
  212. * @ref SOCKERR_TIMEOUT - Timeout occurred \n
  213. * @ref SOCK_BUSY - Socket is busy.
  214. */
  215. int8_t wizchip_disconnect(uint8_t sn);
  216. /**
  217. * @ingroup WIZnet_socket_APIs
  218. * @brief Send data to the connected peer in TCP socket.
  219. * @details It is used to send outgoing data to the connected socket.
  220. * @note It is valid only in TCP server or client mode. It can't send data greater than socket buffer size. \n
  221. * In block io mode, It doesn't return until data send is completed - socket buffer size is greater than data. \n
  222. * In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough. \n
  223. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  224. * @param buf Pointer buffer containing data to be sent.
  225. * @param len The byte length of data in buf.
  226. * @return @b Success : The sent data size \n
  227. * @b Fail : \n @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n
  228. * @ref SOCKERR_TIMEOUT - Timeout occurred \n
  229. * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
  230. * @ref SOCKERR_SOCKNUM - Invalid socket number \n
  231. * @ref SOCKERR_DATALEN - zero data length \n
  232. * @ref SOCK_BUSY - Socket is busy.
  233. */
  234. int32_t wizchip_send(uint8_t sn, uint8_t *buf, uint16_t len);
  235. /**
  236. * @ingroup WIZnet_socket_APIs
  237. * @brief Receive data from the connected peer.
  238. * @details It is used to read incoming data from the connected socket.\n
  239. * It waits for data as much as the application wants to receive.
  240. * @note It is valid only in TCP server or client mode. It can't receive data greater than socket buffer size. \n
  241. * In block io mode, it doesn't return until data reception is completed - data is filled as <I>len</I> in socket buffer. \n
  242. * In non-block io mode, it return @ref SOCK_BUSY immediately when <I>len</I> is greater than data size in socket buffer. \n
  243. *
  244. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  245. * @param buf Pointer buffer to read incoming data.
  246. * @param len The max data length of data in buf.
  247. * @return @b Success : The real received data size \n
  248. * @b Fail :\n
  249. * @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n
  250. * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
  251. * @ref SOCKERR_SOCKNUM - Invalid socket number \n
  252. * @ref SOCKERR_DATALEN - zero data length \n
  253. * @ref SOCK_BUSY - Socket is busy.
  254. */
  255. int32_t wizchip_recv(uint8_t sn, uint8_t *buf, uint16_t len);
  256. /**
  257. * @ingroup WIZnet_socket_APIs
  258. * @brief Sends datagram to the peer with destination IP address and port number passed as parameter.
  259. * @details It sends datagram of UDP or MACRAW to the peer with destination IP address and port number passed as parameter.\n
  260. * Even if the connectionless socket has been previously connected to a specific address,
  261. * the address and port number parameters override the destination address for that particular datagram only.
  262. * @note In block io mode, It doesn't return until data send is completed - socket buffer size is greater than <I>len</I>.
  263. * In non-block io mode, It return @ref SOCK_BUSY immediately when socket buffer is not enough.
  264. *
  265. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  266. * @param buf Pointer buffer to send outgoing data.
  267. * @param len The byte length of data in buf.
  268. * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes.
  269. * @param port Destination port number.
  270. *
  271. * @return @b Success : The sent data size \n
  272. * @b Fail :\n @ref SOCKERR_SOCKNUM - Invalid socket number \n
  273. * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
  274. * @ref SOCKERR_SOCKSTATUS - Invalid socket status for socket operation \n
  275. * @ref SOCKERR_DATALEN - zero data length \n
  276. * @ref SOCKERR_IPINVALID - Wrong server IP address\n
  277. * @ref SOCKERR_PORTZERO - Server port zero\n
  278. * @ref SOCKERR_SOCKCLOSED - Socket unexpectedly closed \n
  279. * @ref SOCKERR_TIMEOUT - Timeout occurred \n
  280. * @ref SOCK_BUSY - Socket is busy.
  281. */
  282. int32_t wizchip_sendto(uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port);
  283. /**
  284. * @ingroup WIZnet_socket_APIs
  285. * @brief Receive datagram of UDP or MACRAW
  286. * @details This function is an application I/F function which is used to receive the data in other then TCP mode. \n
  287. * This function is used to receive UDP and MAC_RAW mode, and handle the header as well.
  288. * This function can divide to received the packet data.
  289. * On the MACRAW SOCKET, the addr and port parameters are ignored.
  290. * @note In block io mode, it doesn't return until data reception is completed - data is filled as <I>len</I> in socket buffer
  291. * In non-block io mode, it return @ref SOCK_BUSY immediately when <I>len</I> is greater than data size in socket buffer.
  292. *
  293. * @param sn Socket number. It should be <b>0 ~ @ref \_WIZCHIP_SOCK_NUM_</b>.
  294. * @param buf Pointer buffer to read incoming data.
  295. * @param len The max data length of data in buf.
  296. * When the received packet size <= len, receives data as packet sized.
  297. * When others, receives data as len.
  298. * @param addr Pointer variable of destination IP address. It should be allocated 4 bytes.
  299. * It is valid only when the first call recvfrom for receiving the packet.
  300. * When it is valid, @ref packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo).
  301. * @param port Pointer variable of destination port number.
  302. * It is valid only when the first call recvform for receiving the packet.
  303. * When it is valid, @ref packinfo[7] should be set as '1' after call @ref getsockopt(sn, SO_PACKINFO, &packinfo).
  304. *
  305. * @return @b Success : This function return real received data size for success.\n
  306. * @b Fail : @ref SOCKERR_DATALEN - zero data length \n
  307. * @ref SOCKERR_SOCKMODE - Invalid operation in the socket \n
  308. * @ref SOCKERR_SOCKNUM - Invalid socket number \n
  309. * @ref SOCKBUSY - Socket is busy.
  310. */
  311. int32_t wizchip_recvfrom(uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port);
  312. /////////////////////////////
  313. // SOCKET CONTROL & OPTION //
  314. /////////////////////////////
  315. #define SOCK_IO_BLOCK 0 // Socket Block IO Mode in @ref setsockopt().
  316. #define SOCK_IO_NONBLOCK 1 // Socket Non-block IO Mode in @ref setsockopt().
  317. /**
  318. * @defgroup DATA_TYPE DATA TYPE
  319. */
  320. /**
  321. * @ingroup DATA_TYPE
  322. * @brief The kind of Socket Interrupt.
  323. * @sa Sn_IR, Sn_IMR, setSn_IR(), getSn_IR(), setSn_IMR(), getSn_IMR()
  324. */
  325. typedef enum
  326. {
  327. SIK_CONNECTED = (1 << 0), // connected
  328. SIK_DISCONNECTED = (1 << 1), // disconnected
  329. SIK_RECEIVED = (1 << 2), // data received
  330. SIK_TIMEOUT = (1 << 3), // timeout occurred
  331. SIK_SENT = (1 << 4), // send ok
  332. // M20150410 : Remove the comma of last member
  333. // SIK_ALL = 0x1F, // all interrupt
  334. SIK_ALL = 0x1F // all interrupt
  335. } sockint_kind;
  336. /**
  337. * @ingroup DATA_TYPE
  338. * @brief The type of @ref ctlsocket().
  339. */
  340. typedef enum
  341. {
  342. CS_SET_IOMODE, // set socket IO mode with @ref SOCK_IO_BLOCK or @ref SOCK_IO_NONBLOCK
  343. CS_GET_IOMODE, // get socket IO mode
  344. CS_GET_MAXTXBUF, // get the size of socket buffer allocated in TX memory
  345. CS_GET_MAXRXBUF, // get the size of socket buffer allocated in RX memory
  346. CS_CLR_INTERRUPT, // clear the interrupt of socket with @ref sockint_kind
  347. CS_GET_INTERRUPT, // get the socket interrupt. refer to @ref sockint_kind
  348. #if _WIZCHIP_ > 5100
  349. CS_SET_INTMASK, // set the interrupt mask of socket with @ref sockint_kind, Not supported in W5100
  350. CS_GET_INTMASK // get the masked interrupt of socket. refer to @ref sockint_kind, Not supported in W5100
  351. #endif
  352. } ctlsock_type;
  353. /**
  354. * @ingroup DATA_TYPE
  355. * @brief The type of socket option in @ref setsockopt() or @ref getsockopt()
  356. */
  357. typedef enum
  358. {
  359. SO_FLAG, // Valid only in getsockopt(), For set flag of socket refer to <I>flag</I> in @ref socket().
  360. SO_TTL, // Set TTL. @ref Sn_TTL ( @ref setSn_TTL(), @ref getSn_TTL() )
  361. SO_TOS, // Set TOS. @ref Sn_TOS ( @ref setSn_TOS(), @ref getSn_TOS() )
  362. SO_MSS, // Set MSS. @ref Sn_MSSR ( @ref setSn_MSSR(), @ref getSn_MSSR() )
  363. SO_DESTIP, // Set the destination IP address. @ref Sn_DIPR ( @ref setSn_DIPR(), @ref getSn_DIPR() )
  364. SO_DESTPORT, // Set the destination Port number. @ref Sn_DPORT ( @ref setSn_DPORT(), @ref getSn_DPORT() )
  365. #if _WIZCHIP_ != 5100
  366. SO_KEEPALIVESEND, // Valid only in setsockopt. Manually send keep-alive packet in TCP mode, Not supported in W5100
  367. #if !((_WIZCHIP_ == 5100) || (_WIZCHIP_ == 5200))
  368. SO_KEEPALIVEAUTO, // Set/Get keep-alive auto transmission timer in TCP mode, Not supported in W5100, W5200
  369. #endif
  370. #endif
  371. SO_SENDBUF, // Valid only in getsockopt. Get the free data size of Socekt TX buffer. @ref Sn_TX_FSR, @ref getSn_TX_FSR()
  372. SO_RECVBUF, // Valid only in getsockopt. Get the received data size in socket RX buffer. @ref Sn_RX_RSR, @ref getSn_RX_RSR()
  373. SO_STATUS, // Valid only in getsockopt. Get the socket status. @ref Sn_SR, @ref getSn_SR()
  374. SO_REMAINSIZE, // Valid only in getsockopt. Get the remained packet size in other then TCP mode.
  375. SO_PACKINFO // Valid only in getsockopt. Get the packet information as @ref PACK_FIRST, @ref PACK_REMAINED, and @ref PACK_COMPLETED in other then TCP mode.
  376. } sockopt_type;
  377. /**
  378. * @ingroup WIZnet_socket_APIs
  379. * @brief Control socket.
  380. * @details Control IO mode, Interrupt & Mask of socket and get the socket buffer information.
  381. * Refer to @ref ctlsock_type.
  382. * @param sn socket number
  383. * @param cstype type of control socket. refer to @ref ctlsock_type.
  384. * @param arg Data type and value is determined according to @ref ctlsock_type. \n
  385. * <table>
  386. * <tr> <td> @b cstype </td> <td> @b data type</td><td>@b value</td></tr>
  387. * <tr> <td> @ref CS_SET_IOMODE \n @ref CS_GET_IOMODE </td> <td> uint8_t </td><td>@ref SOCK_IO_BLOCK @ref SOCK_IO_NONBLOCK</td></tr>
  388. * <tr> <td> @ref CS_GET_MAXTXBUF \n @ref CS_GET_MAXRXBUF </td> <td> uint16_t </td><td> 0 ~ 16K </td></tr>
  389. * <tr> <td> @ref CS_CLR_INTERRUPT \n @ref CS_GET_INTERRUPT \n @ref CS_SET_INTMASK \n @ref CS_GET_INTMASK </td> <td> @ref sockint_kind </td><td> @ref SIK_CONNECTED, etc. </td></tr>
  390. * </table>
  391. * @return @b Success @ref SOCK_OK \n
  392. * @b fail @ref SOCKERR_ARG - Invalid argument\n
  393. */
  394. int8_t wizchip_ctlsocket(uint8_t sn, ctlsock_type cstype, void *arg);
  395. /**
  396. * @ingroup WIZnet_socket_APIs
  397. * @brief set socket options
  398. * @details Set socket option like as TTL, MSS, TOS, and so on. Refer to @ref sockopt_type.
  399. *
  400. * @param sn socket number
  401. * @param sotype socket option type. refer to @ref sockopt_type
  402. * @param arg Data type and value is determined according to <I>sotype</I>. \n
  403. * <table>
  404. * <tr> <td> @b sotype </td> <td> @b data type</td><td>@b value</td></tr>
  405. * <tr> <td> @ref SO_TTL </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
  406. * <tr> <td> @ref SO_TOS </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
  407. * <tr> <td> @ref SO_MSS </td> <td> uint16_t </td><td> 0 ~ 65535 </td> </tr>
  408. * <tr> <td> @ref SO_DESTIP </td> <td> uint8_t[4] </td><td> </td></tr>
  409. * <tr> <td> @ref SO_DESTPORT </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr>
  410. * <tr> <td> @ref SO_KEEPALIVESEND </td> <td> null </td><td> null </td></tr>
  411. * <tr> <td> @ref SO_KEEPALIVEAUTO </td> <td> uint8_t </td><td> 0 ~ 255 </td></tr>
  412. * </table>
  413. * @return
  414. * - @b Success : @ref SOCK_OK \n
  415. * - @b Fail
  416. * - @ref SOCKERR_SOCKNUM - Invalid Socket number \n
  417. * - @ref SOCKERR_SOCKMODE - Invalid socket mode \n
  418. * - @ref SOCKERR_SOCKOPT - Invalid socket option or its value \n
  419. * - @ref SOCKERR_TIMEOUT - Timeout occurred when sending keep-alive packet \n
  420. */
  421. int8_t wizchip_setsockopt(uint8_t sn, sockopt_type sotype, void *arg);
  422. /**
  423. * @ingroup WIZnet_socket_APIs
  424. * @brief get socket options
  425. * @details Get socket option like as FLAG, TTL, MSS, and so on. Refer to @ref sockopt_type
  426. * @param sn socket number
  427. * @param sotype socket option type. refer to @ref sockopt_type
  428. * @param arg Data type and value is determined according to <I>sotype</I>. \n
  429. * <table>
  430. * <tr> <td> @b sotype </td> <td>@b data type</td><td>@b value</td></tr>
  431. * <tr> <td> @ref SO_FLAG </td> <td> uint8_t </td><td> @ref SF_ETHER_OWN, etc... </td> </tr>
  432. * <tr> <td> @ref SO_TOS </td> <td> uint8_t </td><td> 0 ~ 255 </td> </tr>
  433. * <tr> <td> @ref SO_MSS </td> <td> uint16_t </td><td> 0 ~ 65535 </td> </tr>
  434. * <tr> <td> @ref SO_DESTIP </td> <td> uint8_t[4] </td><td> </td></tr>
  435. * <tr> <td> @ref SO_DESTPORT </td> <td> uint16_t </td><td> </td></tr>
  436. * <tr> <td> @ref SO_KEEPALIVEAUTO </td> <td> uint8_t </td><td> 0 ~ 255 </td></tr>
  437. * <tr> <td> @ref SO_SENDBUF </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr>
  438. * <tr> <td> @ref SO_RECVBUF </td> <td> uint16_t </td><td> 0 ~ 65535 </td></tr>
  439. * <tr> <td> @ref SO_STATUS </td> <td> uint8_t </td><td> @ref SOCK_ESTABLISHED, etc.. </td></tr>
  440. * <tr> <td> @ref SO_REMAINSIZE </td> <td> uint16_t </td><td> 0~ 65535 </td></tr>
  441. * <tr> <td> @ref SO_PACKINFO </td> <td> uint8_t </td><td> @ref PACK_FIRST, etc... </td></tr>
  442. * </table>
  443. * @return
  444. * - @b Success : @ref SOCK_OK \n
  445. * - @b Fail
  446. * - @ref SOCKERR_SOCKNUM - Invalid Socket number \n
  447. * - @ref SOCKERR_SOCKOPT - Invalid socket option or its value \n
  448. * - @ref SOCKERR_SOCKMODE - Invalid socket mode \n
  449. * @note
  450. * The option as PACK_REMAINED and SO_PACKINFO is valid only in NON-TCP mode and after call @ref recvfrom(). \n
  451. * When SO_PACKINFO value is PACK_FIRST and the return value of recvfrom() is zero,
  452. * This means the zero byte UDP data(UDP Header only) received.
  453. */
  454. int8_t wizchip_getsockopt(uint8_t sn, sockopt_type sotype, void *arg);
  455. #ifdef __cplusplus
  456. }
  457. #endif
  458. #endif // _WIZCHIP_SOCKET_H_