IOT Sockets#
This section provides a reference to IoT Socket API functions.
The IOT socket implementation aims to emulate the standards library to the extent possible for embedded offerings from Silabs. There are substantial limitation/exceptions however, and those are mentioned in the subsequent documentation of IOT sockets.
Modules#
Functions#
Create a communication socket.
Assign a local address to a socket.
Listen for socket connections.
Accept a new connection on a socket.
Connect a socket to a remote host.
Receive data from a socket.
Receive data from a socket.
Send data or check if data can be sent on a connected socket.
Send data or check if data can be sent on a socket.
Retrieve local IP address and port of a socket.
Retrieve remote IP address and port of a socket.
Retrieve socket option.
Set socket option.
Close and release a socket.
Retrieve host IP address from host name.
Function Documentation#
iotSocketCreate#
int32_t iotSocketCreate (int32_t af, int32_t type, int32_t protocol)
Create a communication socket.
[in] | af | Address family. One of the values from Address Family. |
[in] | type | Socket type. One of the values from Socket Type. |
[in] | protocol | Socket protocol. One of the values from Socket Protocol. |
The iotSocketCreate() function creates a new socket that is used for communication. The socket is created with the specified address family, socket type, and protocol.
Returns
Returns the socket identification number (>=0) on success. On failure, returns one of the following error codes:
IOT_SOCKET_EINVAL : Invalid argument.
IOT_SOCKET_ENOTSUP : Operation not supported.
IOT_SOCKET_ENOMEM : Not enough memory.
IOT_SOCKET_ERROR : Unspecified error.
146
of file third_party/iot_socket/include/iot_socket.h
iotSocketBind#
int32_t iotSocketBind (int32_t socket, const uint8_t * ip, uint32_t ip_len, uint16_t port)
Assign a local address to a socket.
[in] | socket | Socket identification number. |
[in] | ip | Pointer to the local IP address. The address should be in network byte order. |
[in] | ip_len | Length of the |
[in] | port | Local port number. The port number should be in host byte order. |
The iotSocketBind() function assigns a local IP address and port number to a socket identified by socket
. This function is typically used on the server side to bind a socket to a specific IP address and port number, so that the socket can listen for incoming connections or datagrams.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (address or socket already bound).
IOT_SOCKET_EADDRINUSE : Address already in use.
IOT_SOCKET_ERROR : Unspecified error.
177
of file third_party/iot_socket/include/iot_socket.h
iotSocketListen#
int32_t iotSocketListen (int32_t socket, int32_t backlog)
Listen for socket connections.
[in] | socket | Socket identification number. |
[in] | backlog | Maximum number of pending connections that can be queued. |
The iotSocketListen() function marks the socket referred to by socket
as a passive socket, that is, as a socket that will be used to accept incoming connection requests using iotSocketAccept(). The backlog
parameter defines the maximum length to which the queue of pending connections for socket
may grow.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (socket not bound).
IOT_SOCKET_ENOTSUP : Operation not supported.
IOT_SOCKET_EISCONN : Socket is already connected.
IOT_SOCKET_ERROR : Unspecified error.
203
of file third_party/iot_socket/include/iot_socket.h
iotSocketAccept#
int32_t iotSocketAccept (int32_t socket, uint8_t * ip, uint32_t * ip_len, uint16_t * port)
Accept a new connection on a socket.
[in] | socket | Socket identification number. |
[out] | ip | Pointer to a buffer where the address of the connecting socket will be returned. If NULL, no address is returned. |
[inout] | ip_len | Pointer to the length of the |
[out] | port | Pointer to a buffer where the port of the connecting socket will be returned. If NULL, no port is returned. |
The iotSocketAccept() function is used to accept a new incoming connection on a socket that is in listening mode. It extracts the first connection request on the queue of pending connections for the listening socket, socket
, creates a new connected socket, and returns a new file descriptor referring to that socket. The newly created socket is not in the listening state. The original socket remains open and can continue to accept new connections.
Returns
Returns the socket identification number of the accepted socket (>=0) on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (socket not in listen mode).
IOT_SOCKET_ENOTSUP : Operation not supported (socket type does not support accepting connections).
IOT_SOCKET_ECONNRESET : Connection reset by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EAGAIN : Operation would block or timed out (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
240
of file third_party/iot_socket/include/iot_socket.h
iotSocketConnect#
int32_t iotSocketConnect (int32_t socket, const uint8_t * ip, uint32_t ip_len, uint16_t port)
Connect a socket to a remote host.
[in] | socket | Socket identification number. |
[in] | ip | Pointer to the remote IP address. |
[in] | ip_len | Length of the |
[in] | port | Remote port number. |
The iotSocketConnect() function establishes a connection to a remote host specified by the IP address and port number. This function is used for connection-oriented protocols such as TCP. The socket identified by socket
must be created and optionally bound to a local address before calling this function.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument.
IOT_SOCKET_EALREADY : Connection already in progress.
IOT_SOCKET_EINPROGRESS : Operation in progress.
IOT_SOCKET_EISCONN : Socket is already connected.
IOT_SOCKET_ECONNREFUSED : Connection rejected by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EADDRINUSE : Address already in use.
IOT_SOCKET_ETIMEDOUT : Operation timed out.
IOT_SOCKET_ERROR : Unspecified error.
277
of file third_party/iot_socket/include/iot_socket.h
iotSocketRecv#
int32_t iotSocketRecv (int32_t socket, void * buf, uint32_t len)
Receive data from a socket.
[in] | socket | Socket identification number. |
[out] | buf | Pointer to the buffer where the received data should be stored. |
[in] | len | Length of the buffer in bytes. |
The iotSocketRecv() function receives data from a connected socket identified by socket
. The received data is stored in the buffer pointed to by buf
, which has a length of len
bytes.
Returns
Returns the number of bytes received (>=0) on success. On failure, returns one of the following error codes:
0: Data is available (when
len
is 0).IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ENOTCONN : Socket is not connected.
IOT_SOCKET_ECONNRESET : Connection reset by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EAGAIN : Operation would block or timed out (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
308
of file third_party/iot_socket/include/iot_socket.h
iotSocketRecvFrom#
int32_t iotSocketRecvFrom (int32_t socket, void * buf, uint32_t len, uint8_t * ip, uint32_t * ip_len, uint16_t * port)
Receive data from a socket.
[in] | socket | Socket identification number. |
[out] | buf | Pointer to the buffer where the received data should be stored. |
[in] | len | Length of the buffer in bytes. |
[out] | ip | Pointer to a buffer where the remote source address will be returned. If NULL, no address is returned. |
[inout] | ip_len | Pointer to the length of the |
[out] | port | Pointer to a buffer where the remote source port will be returned. If NULL, no port is returned. |
The iotSocketRecvFrom() function receives data from a socket identified by socket
. The received data is stored in the buffer pointed to by buf
, which has a length of len
bytes. If the ip
and port
parameters are provided, the function also returns the source address and port of the received data.
Returns
Returns the number of bytes received (>=0) on success. On failure, returns one of the following error codes:
0: Data is available (when
len
is 0).IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ENOTCONN : Socket is not connected.
IOT_SOCKET_ECONNRESET : Connection reset by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EAGAIN : Operation would block or timed out (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
351
of file third_party/iot_socket/include/iot_socket.h
iotSocketSend#
int32_t iotSocketSend (int32_t socket, const void * buf, uint32_t len)
Send data or check if data can be sent on a connected socket.
[in] | socket | Socket identification number. |
[in] | buf | Pointer to the buffer containing the data to send. |
[in] | len | Length of the data in bytes. |
The iotSocketSend() function sends data from the buffer pointed to by buf
through the socket identified by socket
. If len
is 0, the function checks if data can be sent on the socket without blocking.
Returns
Returns the number of bytes sent (>=0) on success if
len
is not 0. Iflen
is 0, returns 0 if data can be sent. On failure, returns one of the following error codes:IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ENOTCONN : Socket is not connected.
IOT_SOCKET_ECONNRESET : Connection reset by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EAGAIN : Operation would block or timed out (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
Note
The function does not guarantee that the packets are transmitted to the remote node; they are enqueued in the queue.
The function can only send a maximum of 1460 bytes in the case of plain TCP and UDP. For TLS, the maximum buffer length is 1370 bytes.
386
of file third_party/iot_socket/include/iot_socket.h
iotSocketSendTo#
int32_t iotSocketSendTo (int32_t socket, const void * buf, uint32_t len, const uint8_t * ip, uint32_t ip_len, uint16_t port)
Send data or check if data can be sent on a socket.
[in] | socket | Socket identification number. |
[in] | buf | Pointer to the buffer containing the data to send. |
[in] | len | Length of the data in bytes. |
[in] | ip | Pointer to the remote destination IP address. |
[in] | ip_len | Length of the |
[in] | port | Remote destination port number. |
The iotSocketSendTo() function sends data from the buffer pointed to by buf
through the socket identified by socket
to the specified remote destination IP address and port. If len
is 0, the function checks if data can be sent on the socket without blocking.
Returns
Returns the number of bytes sent (>=0) on success if
len
is not 0. Iflen
is 0, returns 0 if data can be sent. On failure, returns one of the following error codes:IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ENOTCONN : Socket is not connected.
IOT_SOCKET_ECONNRESET : Connection reset by the peer.
IOT_SOCKET_ECONNABORTED : Connection aborted locally.
IOT_SOCKET_EAGAIN : Operation would block or timed out (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
Note
If the number of bytes to be sent exceeds the Maximum Segment Size (MSS) specified by the remote node, the API will return IOT_SOCKET_ERROR. To know the MSS size for the socket, use the
sl_si91x_get_socket_mss()
utility. In the case of TCP, is should be called after iotSocketConnect().The function does not guarantee that the packets are transmitted to the remote node; they are enqueued in the queue.
The function can only send a maximum of 1460 bytes in the case of plain TCP or UDP. For TLS, the maximum buffer length is 1370 bytes.
433
of file third_party/iot_socket/include/iot_socket.h
iotSocketGetSockName#
int32_t iotSocketGetSockName (int32_t socket, uint8_t * ip, uint32_t * ip_len, uint16_t * port)
Retrieve local IP address and port of a socket.
[in] | socket | Socket identification number. |
[out] | ip | Pointer to a buffer where the local IP address will be returned. If NULL, no address is returned. |
[inout] | ip_len | Pointer to the length of the |
[out] | port | Pointer to a buffer where the local port number would be returned. If NULL, no port is returned. |
The iotSocketGetSockName() function retrieves the local IP address and port number associated with the socket identified by socket
. This function is typically used to obtain the local address and port assigned to a socket after it has been bound or connected.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ERROR : Unspecified error.
464
of file third_party/iot_socket/include/iot_socket.h
iotSocketGetPeerName#
int32_t iotSocketGetPeerName (int32_t socket, uint8_t * ip, uint32_t * ip_len, uint16_t * port)
Retrieve remote IP address and port of a socket.
[in] | socket | Socket identification number. |
[out] | ip | Pointer to a buffer where the remote IP address would be returned. If NULL, no address is returned. |
[inout] | ip_len | Pointer to the length of the |
[out] | port | Pointer to a buffer where the remote port number would be returned. If NULL, no port is returned. |
The iotSocketGetPeerName() function retrieves the remote IP address and port number associated with the socket identified by socket
. This function is typically used to obtain the address and port of the peer to which the socket is connected.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument (pointer to buffer or length).
IOT_SOCKET_ENOTCONN : Socket is not connected.
IOT_SOCKET_ERROR : Unspecified error.
496
of file third_party/iot_socket/include/iot_socket.h
iotSocketGetOpt#
int32_t iotSocketGetOpt (int32_t socket, int32_t opt_id, void * opt_val, uint32_t * opt_len)
Retrieve socket option.
[in] | socket | Socket identification number. |
[in] | opt_id | Option identifier. One of the values from Socket Option Id. |
[out] | opt_val | Pointer to the buffer that would receive the option value. |
[inout] | opt_len | Pointer to the length of the option value:
|
The iotSocketGetOpt() function retrieves the value of a socket option specified by opt_id
for the socket identified by socket
. The option value is stored in the buffer pointed to by opt_val
, and the length of the option value is specified by opt_len
.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument.
IOT_SOCKET_ENOTSUP : Operation not supported.
IOT_SOCKET_ERROR : Unspecified error.
Note
The following options are currently supported:
537
of file third_party/iot_socket/include/iot_socket.h
iotSocketSetOpt#
int32_t iotSocketSetOpt (int32_t socket, int32_t opt_id, const void * opt_val, uint32_t opt_len)
Set socket option.
[in] | socket | Socket identification number. |
[in] | opt_id | Option identifier. One of the values from Socket Option Id. |
[in] | opt_val | Pointer to the buffer containing the option value. |
[in] | opt_len | Length of the option value in bytes. |
The iotSocketSetOpt() function sets the value of a socket option specified by opt_id
for the socket identified by socket
. The option value is provided in the buffer pointed to by opt_val
, and the length of the option value is specified by opt_len
.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EINVAL : Invalid argument.
IOT_SOCKET_ENOTSUP : Operation not supported.
IOT_SOCKET_ERROR : Unspecified error.
Note
The following options are currently supported:
576
of file third_party/iot_socket/include/iot_socket.h
iotSocketClose#
int32_t iotSocketClose (int32_t socket)
Close and release a socket.
[in] | socket | Socket identification number. |
The iotSocketClose() function closes the socket identified by socket
and releases any resources associated with it. If the socket is a server socket or the first client socket, closing it may also close other related sockets.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_ESOCK : Invalid socket.
IOT_SOCKET_EAGAIN : Operation would block (may be called again).
IOT_SOCKET_ERROR : Unspecified error.
Note
Calling close on a server or the first client socket may result in closing other related sockets as well.
599
of file third_party/iot_socket/include/iot_socket.h
iotSocketGetHostByName#
int32_t iotSocketGetHostByName (const char * name, int32_t af, uint8_t * ip, uint32_t * ip_len)
Retrieve host IP address from host name.
[in] | name | Host name to be resolved. |
[in] | af | Address family. One of the values from Address Family. |
[out] | ip | Pointer to the buffer where the resolved IP address will be returned. |
[inout] | ip_len | Pointer to the length of the
|
The iotSocketGetHostByName() function resolves a host name to an IP address. The resolved IP address is stored in the buffer pointed to by ip
, and the length of the IP address is specified by ip_len
. The address family is specified by af
.
Returns
Returns 0 on success. On failure, returns one of the following error codes:
IOT_SOCKET_EINVAL : Invalid argument.
IOT_SOCKET_ENOTSUP : Operation not supported.
IOT_SOCKET_ETIMEDOUT : Operation timed out.
IOT_SOCKET_EHOSTNOTFOUND : Host not found.
IOT_SOCKET_ERROR : Unspecified error.
633
of file third_party/iot_socket/include/iot_socket.h