PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/network/wiznet/Ethernet/socket.h

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