BSD style socket API for W5500.

Dependencies:   W5500

Fork of Ethernet by Raphael Kwon

Embed: (wiki syntax)

« Back to documentation index

1. WIZnet socket APIs

1. WIZnet socket APIs

WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface. More...

Functions

int8_t socket (uint8_t sn, uint8_t protocol, uint16_t port, uint8_t flag)
 Open a socket.
int8_t close (uint8_t sn)
 Close a socket.
int8_t listen (uint8_t sn)
 Listen to a connection request from a client.
int8_t connect (uint8_t sn, uint8_t *addr, uint16_t port)
 Try to connect a server.
int8_t disconnect (uint8_t sn)
 Try to disconnect a connection socket.
int32_t send (uint8_t sn, uint8_t *buf, uint16_t len)
 Send data to the connected peer in TCP socket.
int32_t recv (uint8_t sn, uint8_t *buf, uint16_t len)
 Receive data from the connected peer.
int32_t sendto (uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port)
 Sends datagram to the peer with destination IP address and port number passed as parameter.
int32_t recvfrom (uint8_t sn, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port, uint8_t *packinfo)
 Receive datagram of UDP or MACRAW.
int8_t ctlsocket (uint8_t sn, ctlsock_type cstype, void *arg)
 Control socket.
int8_t setsockopt (uint8_t sn, sockopt_type sotype, void *arg)
 set socket options
int8_t getsockopt (uint8_t sn, sockopt_type sotype, void *arg)
 get socket options

Detailed Description

WIZnet socket APIs are based on Berkeley socket APIs, thus it has much similar name and interface.

But there is a little bit of difference.

Comparison between WIZnet and Berkeley SOCKET APIs

API WIZnet Berkeley
socket() O O
bind() X O
listen() O O
connect() O O
accept() X O
recv() O O
send() O O
recvfrom() O O
sendto() O O
closesocket() O
close() & disconnect()
O

There are bind() and accept() functions in Berkeley SOCKET API but, not in WIZnet SOCKET API. Because socket() of WIZnet is not only creating a SOCKET but also binding a local port number, and listen() of WIZnet is not only listening to connection request from client but also accepting the connection request.
When you program "TCP SERVER" with Berkeley SOCKET API, you can use only one listen port. When the listen SOCKET accepts a connection request from a client, it keeps listening. After accepting the connection request, a new SOCKET is created and the new SOCKET is used in communication with the client.
Following figure shows network flow diagram by Berkeley SOCKET API.

Berkeley_SOCKET.jpg

<Berkeley SOCKET API>

But, When you program "TCP SERVER" with WIZnet SOCKET API, you can use as many as 8 listen SOCKET with same port number.
Because there's no accept() in WIZnet SOCKET APIs, when the listen SOCKET accepts a connection request from a client, it is changed in order to communicate with the client. And the changed SOCKET is not listening any more and is dedicated for communicating with the client.
If there're many listen SOCKET with same listen port number and a client requests a connection, the SOCKET which has the smallest SOCKET number accepts the request and is changed as communication SOCKET.
Following figure shows network flow diagram by WIZnet SOCKET API.

WIZnet_SOCKET.jpg

<WIZnet SOCKET API>


Function Documentation

int8_t close ( uint8_t  sn )

Close a socket.

It closes the socket with 'sn' passed as parameter.

Parameters:
snSocket number. It should be 0 ~ .
Returns:
Success : SOCK_OK
Fail : SOCKERR_SOCKNUM - Invalid socket number

Definition at line 135 of file socket.c.

int8_t connect ( uint8_t  sn,
uint8_t *  addr,
uint16_t  port 
)

Try to connect a server.

It requests connection to the server with destination IP address and port number passed as parameter.

Note:
It is valid only in TCP client mode. In block io mode, it does not return until connection is completed. In Non-block io mode, it return SOCK_BUSY immediatly.
Parameters:
snSocket number. It should be 0 ~ .
addrPointer variable of destination IP address. It should be allocated 4 bytes.
portDestination port number.
Returns:
Success : SOCK_OK
Fail :
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_SOCKMODE - Invalid socket mode
SOCKERR_SOCKINIT - Socket is not initialized
SOCKERR_IPINVALID - Wrong server IP address
SOCKERR_PORTZERO - Server port zero
SOCKERR_TIMEOUT - Timeout occurred during request connection
SOCK_BUSY - In non-block io mode, it returned immediatly

Definition at line 169 of file socket.c.

int8_t ctlsocket ( uint8_t  sn,
ctlsock_type  cstype,
void *  arg 
)

Control socket.

Control IO mode, Interrupt & Mask of socket and get the socket buffer information. Refer to ctlsock_type.

Parameters:
snsocket number
cstypetype of control socket. refer to ctlsock_type.
argData type and value is determined according to ctlsock_type.

cstype data typevalue
CS_SET_IOMODE
CS_GET_IOMODE
uint8_t SOCK_IO_BLOCK SOCK_IO_NONBLOCK
CS_GET_MAXTXBUF
CS_GET_MAXRXBUF
uint16_t 0 ~ 16K
CS_CLR_INTERRUPT
CS_GET_INTERRUPT
sockint_kind SIK_CONNECTED, etc.
CS_CLR_INTERRUPT
CS_GET_INTERRUPT
CS_SET_INTMASK
CS_GET_INTMASK
sockint_kind SIK_CONNECTED, etc.
Returns:
Success SOCK_OK
fail SOCKERR_ARG - Invalid argument

Definition at line 501 of file socket.c.

int8_t disconnect ( uint8_t  sn )

Try to disconnect a connection socket.

It sends request message to disconnect the TCP socket 'sn' passed as parameter to the server or client.

Note:
It is valid only in TCP server or client mode.
In block io mode, it does not return until disconnection is completed.
In Non-block io mode, it return SOCK_BUSY immediatly.
Parameters:
snSocket number. It should be 0 ~ .
Returns:
Success : SOCK_OK
Fail :
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_SOCKMODE - Invalid operation in the socket
SOCKERR_TIMEOUT - Timeout occurred
SOCK_BUSY - Socket is busy.

Definition at line 194 of file socket.c.

int8_t getsockopt ( uint8_t  sn,
sockopt_type  sotype,
void *  arg 
)

get socket options

Get socket option like as FLAG, TTL, MSS, and so on. Refer to sockopt_type

Parameters:
snsocket number
sotypesocket option type. refer to sockopt_type
argData type and value is determined according to sotype.

sotype data typevalue
SO_FLAG uint8_t SF_ETHER_OWN, etc...
SO_TOS uint8_t 0 ~ 255
SO_MSS uint16_t 0 ~ 65535
SO_DESTIP uint8_t[4]
SO_DESTPORT uint16_t
SO_KEEPALIVEAUTO uint8_t 0 ~ 255
SO_SENDBUF uint16_t 0 ~ 65535
SO_RECVBUF uint16_t 0 ~ 65535
SO_STATUS uint8_t SOCK_ESTABLISHED, etc..
SO_REMAINSIZE uint16_t 0~ 65535
Returns:
  • Success : SOCK_OK
  • Fail
    • SOCKERR_SOCKNUM - Invalid Socket number
    • SOCKERR_SOCKOPT - Invalid socket option or its value
    • SOCKERR_SOCKMODE - Invalid socket mode

Definition at line 591 of file socket.c.

int8_t listen ( uint8_t  sn )

Listen to a connection request from a client.

It is listening to a connection request from a client. If connection request is accepted successfully, the connection is established. Socket sn is used in passive(server) mode.

Parameters:
snSocket number. It should be 0 ~ .
Returns:
Success : SOCK_OK
Fail :
SOCKERR_SOCKINIT - Socket is not initialized
SOCKERR_SOCKCLOSED - Socket closed unexpectedly.

Definition at line 150 of file socket.c.

int32_t recv ( uint8_t  sn,
uint8_t *  buf,
uint16_t  len 
)

Receive data from the connected peer.

It is used to read incoming data from the connected socket.
It waits for data as much as the application wants to receive.

Note:
It is valid only in TCP server or client mode. It can't receive data greater than socket buffer size.
In block io mode, it doesn't return until data reception is completed - data is filled as len in socket buffer.
In non-block io mode, it return SOCK_BUSY immediatly when len is greater than data size in socket buffer.
Parameters:
snSocket number. It should be 0 ~ .
bufPointer buffer to read incoming data.
lenThe max data length of data in buf.
Returns:
Success : The real received data size
Fail :
SOCKERR_SOCKSTATUS - Invalid socket status for socket operation
SOCKERR_SOCKMODE - Invalid operation in the socket
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_DATALEN - zero data length
SOCK_BUSY - Socket is busy.

Definition at line 274 of file socket.c.

int32_t recvfrom ( uint8_t  sn,
uint8_t *  buf,
uint16_t  len,
uint8_t *  addr,
uint16_t *  port,
uint8_t *  packinfo 
)

Receive datagram of UDP or MACRAW.

This function is an application I/F function which is used to receive the data in other then TCP mode.
This function is used to receive UDP and MAC_RAW mode, and handle the header as well. This function can divide to recevie the packet data. On the MACRAW SOCKET, the addr and port parameters are ignored.

Note:
In block io mode, it doesn't return until data reception is completed - data is filled as len in socket buffer In non-block io mode, it return SOCK_BUSY immediatly when len is greater than data size in socket buffer.
Parameters:
snSocket number. It should be 0 ~ .
bufPointer buffer to read incoming data.
lenThe max data length of data in buf. When the received packet size <= len, receives data as packet sized. When others, receives data as len.
addrPointer variable of destination IP address. It should be allocated 4 bytes. It is valid only when the first call recvform for receiving the packet. When it is valid, packinfo[7] should be set as '1'.
portPointer variable of destination port number. It is valid only when the first call recvform for receiving the packet. When it is valid, packinfo[7] should be set as '1'.
packinfoNotify whether the packet is received completely or not. When it completed to receive the packet data in socket buffer, packinfo[0] set as '0'. PACK_FIRST When it Remained packet data that receives not yet but remained in socket buffer, packinfo[1] set as '1'. PACK_COMPLETED
Returns:
Success : This function return real received data size for success.
Fail : SOCKERR_DATALEN - zero data length
SOCKERR_SOCKMODE - Invalid operation in the socket
SOCKERR_SOCKNUM - Invalid socket number
SOCKBUSY - Socket is busy.

Definition at line 386 of file socket.c.

int32_t send ( uint8_t  sn,
uint8_t *  buf,
uint16_t  len 
)

Send data to the connected peer in TCP socket.

It is used to send outgoing data to the connected socket.

Note:
It is valid only in TCP server or client mode. It can't send data greater than socket buffer size.
In block io mode, It doesn't return until data send is completed - socket buffer size is greater than data.
In non-block io mode, It return SOCK_BUSY immediatly when socket buffer is not enough.
Parameters:
snSocket number. It should be 0 ~ .
bufPointer buffer containing data to be sent.
lenThe byte length of data in buf.
Returns:
Success : The sent data size
Fail :
SOCKERR_SOCKSTATUS - Invalid socket status for socket operation
SOCKERR_TIMEOUT - Timeout occurred
SOCKERR_SOCKMODE - Invalid operation in the socket
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_DATALEN - zero data length
SOCK_BUSY - Socket is busy.

Definition at line 214 of file socket.c.

int32_t sendto ( uint8_t  sn,
uint8_t *  buf,
uint16_t  len,
uint8_t *  addr,
uint16_t  port 
)

Sends datagram to the peer with destination IP address and port number passed as parameter.

It sends datagram of UDP or MACRAW to the peer with destination IP address and port number passed as parameter.
Even if the connectionless socket has been previously connected to a specific address, the address and port number parameters override the destination address for that particular datagram only.

Note:
In block io mode, It doesn't return until data send is completed - socket buffer size is greater than len. In non-block io mode, It return SOCK_BUSY immediatly when socket buffer is not enough.
Parameters:
snSocket number. It should be 0 ~ .
bufPointer buffer to send outgoing data.
lenThe byte length of data in buf.
addrPointer variable of destination IP address. It should be allocated 4 bytes.
portDestination port number.
Returns:
Success : The sent data size
Fail :
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_SOCKMODE - Invalid operation in the socket
SOCKERR_SOCKSTATUS - Invalid socket status for socket operation
SOCKERR_DATALEN - zero data length
SOCKERR_IPINVALID - Wrong server IP address
SOCKERR_PORTZERO - Server port zero
SOCKERR_SOCKCLOSED - Socket unexpectedly closed
SOCKERR_TIMEOUT - Timeout occurred
SOCK_BUSY - Socket is busy.

Definition at line 315 of file socket.c.

int8_t setsockopt ( uint8_t  sn,
sockopt_type  sotype,
void *  arg 
)

set socket options

Set socket option like as TTL, MSS, TOS, and so on. Refer to sockopt_type.

Parameters:
snsocket number
sotypesocket option type. refer to sockopt_type
argData type and value is determined according to sotype.

sotype data typevalue
SO_TTL uint8_t 0 ~ 255
SO_TOS uint8_t 0 ~ 255
SO_MSS uint16_t 0 ~ 65535
SO_DESTIP uint8_t[4]
SO_DESTPORT uint16_t 0 ~ 65535
SO_KEEPALIVESEND null null
SO_KEEPALIVEAUTO uint8_t 0 ~ 255
Returns:
  • Success : SOCK_OK
  • Fail
    • SOCKERR_SOCKNUM - Invalid Socket number
    • SOCKERR_SOCKMODE - Invalid socket mode
    • SOCKERR_SOCKOPT - Invalid socket option or its value
    • SOCKERR_TIMEOUT - Timeout occured when sending keep-alive packet

Definition at line 541 of file socket.c.

int8_t socket ( uint8_t  sn,
uint8_t  protocol,
uint16_t  port,
uint8_t  flag 
)

Open a socket.

Initializes the socket with 'sn' passed as parameter and open.

Parameters:
snSocket number. It should be 0 ~ .
protocolProtocol type to operate such as TCP, UDP and MACRAW.
portPort number to be bined.
flagSocket flags as SF_ETHER_OWN, SF_IGMP_VER2, SF_TCP_NODELAY, SF_MULTI_ENABLE, SF_IO_NONBLOCK and so on.
Valid flags only in W5500 : SF_BROAD_BLOCK, SF_MULTI_BLOCK, SF_IPv6_BLOCK, and SF_UNI_BLOCK.
See also:
Sn_MR
Returns:
Success : The socket number 'sn' passed as parameter
Fail :
SOCKERR_SOCKNUM - Invalid socket number
SOCKERR_SOCKMODE - Not support socket mode as TCP, UDP, and so on.
SOCKERR_SOCKFLAG - Invaild socket flag.

Definition at line 73 of file socket.c.