BSD style socket API for W5500.

Dependencies:   W5500

Dependents:   W5500_Porting_WIZnetLib W5500_Porting_WIZnetLib W5500_Porting_WIZnetLib_LPC800

Fork of Ethernet by Raphael Kwon

Committer:
xeon011
Date:
Tue Feb 18 05:41:48 2014 +0000
Revision:
2:ee0f7508fb5c
Parent:
0:9158fe13dc14
First release

Who changed what in which revision?

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