socket.h 27 KB

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