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 under Note section in the subsequent API documentation of IOT sockets.

  • Note

    • IOT Sockets are used for cloud connectivity.

Modules#

Address Family

Socket Type

Socket Protocol

Socket Option Id

Socket Return Codes

Functions#

int32_t
iotSocketCreate(int32_t af, int32_t type, int32_t protocol)

Create a communication socket.

int32_t
iotSocketBind(int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)

Assign a local address to a socket.

int32_t
iotSocketListen(int32_t socket, int32_t backlog)

Listen for socket connections.

int32_t
iotSocketAccept(int32_t socket, uint8_t *ip, uint32_t *ip_len, uint16_t *port)

Accept a new connection on a socket.

int32_t
iotSocketConnect(int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)

Connect a socket to a remote host.

int32_t
iotSocketRecv(int32_t socket, void *buf, uint32_t len)

Receive data from a socket.

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.

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.

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.

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.

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.

int32_t
iotSocketGetOpt(int32_t socket, int32_t opt_id, void *opt_val, uint32_t *opt_len)

Retrieve socket option.

int32_t
iotSocketSetOpt(int32_t socket, int32_t opt_id, const void *opt_val, uint32_t opt_len)

Set socket option.

int32_t
iotSocketClose(int32_t socket)

Close and release a socket.

int32_t
iotSocketGetHostByName(const char *name, int32_t af, uint8_t *ip, uint32_t *ip_len)

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.

Parameters
[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


Definition at line 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.

Parameters
[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 ip address in bytes.

[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


Definition at line 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.

Parameters
[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


Definition at line 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.

Parameters
[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 ip buffer. On input, it should contain the size of the buffer. On output, it will be modified to indicate the actual length of the address returned. If ip is NULL, ip_len should also be NULL.

[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


Definition at line 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.

Parameters
[in]socket

Socket identification number.

[in]ip

Pointer to the remote IP address.

[in]ip_len

Length of the ip address in bytes.

[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


Definition at line 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.

Parameters
[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


Definition at line 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.

Parameters
[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 ip buffer. On input, it should contain the size of the buffer. On output, it will be modified to indicate the actual length of the address returned. If ip is NULL, ip_len should also be NULL.

[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


Definition at line 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.

Parameters
[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

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.


Definition at line 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.

Parameters
[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 ip address in bytes.

[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

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.


Definition at line 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.

Parameters
[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 ip buffer. On input, it should contain the size of the buffer. On output, it will be modified to indicate the actual length of the address returned. If ip is NULL, ip_len should also be NULL.

[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


Definition at line 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.

Parameters
[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 ip buffer. On input, it should contain the size of the buffer. On output, it will be modified to indicate the actual length of the address returned. If ip is NULL, ip_len should also be NULL.

[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


Definition at line 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.

Parameters
[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:

  • On input, it specifies the length of the buffer.

  • On output, it specifies the length of the data returned.

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

Note


Definition at line 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.

Parameters
[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

Note


Definition at line 576 of file third_party/iot_socket/include/iot_socket.h

iotSocketClose#

int32_t iotSocketClose (int32_t socket)

Close and release a socket.

Parameters
[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

Note

  • Calling close on a server or the first client socket may result in closing other related sockets as well.


Definition at line 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.

Parameters
[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 ip buffer:

  • On input, it specifies the length of the supplied ip buffer.

  • On output, it specifies the length of the stored ip address.

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


Definition at line 633 of file third_party/iot_socket/include/iot_socket.h