Wiznet W5500 driver and TCP/UDP loopback

Dependencies:   mbed

Committer:
jbkim
Date:
Fri Dec 13 07:35:58 2013 +0000
Revision:
0:2513c6696bdc
Wiznet W5500 library and TCP/UDP loopback program for mbed

Who changed what in which revision?

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