Modules#

Socket API type definitions

POSIX-compliant Socket#

The Socket component offers a POSIX-compliant alternative to the Wi-SUN stack native socket API. The Portable Operating System Interface (POSIX) is a family of standards specified by the IEEE Computer Society. It promotes compatibility and portability of software solutions. It becomes extremly valuable in the IP world to leverage the various application protocols available. In this sense, the socket component can accelerate the integration of existing software libraries on top of the Wi-SUN stack.

The component allows the application to create/destroy sockets, set socket options, and send or receive IP packets using the APIs defined in the POSIX standard. The APIs exposed by the component are thread-safe. The graphical configuration interface gives access to a number of configurations like the maximum count of available sockets and the FIFO buffer size. The component includes the socket-related event handler implementations.

To use the component in your application, add it to your project.

Variables#

Definition of any address for IPv6.

Functions#

int32_t
socket(sock_domain_t domain, sock_type_t type, proto_type_t protocol)

Create an endpoint for communication and return a socket ID.

int32_t
close(int32_t sockid)

Close socket.

int32_t
bind(int32_t sockid, const struct sockaddr *addr, socklen_t addrlen)

Bind a name to a socket.

int32_t
listen(int32_t sockid, int32_t backlog)

Listen for connections on a socket.

int32_t
accept(int32_t sockid, struct sockaddr *addr, socklen_t *addrlen)

Accept a connection on a socket.

int32_t
connect(int32_t sockid, const struct sockaddr *addr, socklen_t addrlen)

Initiate a connection on a socket.

int32_t
send(int32_t sockid, const void *buff, uint32_t len, int32_t flags)

Send a message on a socket,.

int32_t
sendto(int32_t sockid, const void *buff, uint32_t len, int32_t flags, const struct sockaddr *dest_addr, socklen_t addr_len)

Send a message on a socket.

int32_t
recv(int32_t sockid, void *buf, uint32_t len, int32_t flags)

Receive a message from a socket.

int32_t
recvfrom(int32_t sockid, void *buf, uint32_t len, int32_t flags, struct sockaddr *src_addr, socklen_t *addrlen)

Receive messages from a socket.

int32_t
setsockopt(int32_t sockid, int32_t level, int32_t optname, const void *optval, socklen_t optlen)

Set socket option.

int32_t
getsockopt(int32_t sockid, int32_t level, int32_t optname, void *optval, socklen_t *optlen)

Get socket option.

uint32_t
htonl(uint32_t hostlong)

Long host byte order to network order.

uint16_t
htons(uint16_t hostshort)

Short host byte order to network order.

uint32_t
ntohl(uint32_t netlong)

Long network byte order to host byte order.

uint16_t
ntohs(uint16_t netshort)

Short network byte order to host byte order.

int32_t
inet_pton(sock_domain_t af, const char *src, void *dst)

Convert IPv4 and IPv6 addresses from text to binary form.

const char *
inet_ntop(sock_domain_t af, const void *src, char *dst, socklen_t size)

Convert IPv4 and IPv6 addresses from binary to text form.

Macros#

#define
SOCKADDR_WISUN_FAMILY_OFFSET (offsetof(wisun_addr_t, sin6_family))

Wi-SUN address structure offsets.

#define
SOCKADDR_WISUN_PORT_OFFSET (offsetof(wisun_addr_t, sin6_port))
#define
SOCKADDR_WISUN_FLOWINFO_OFFSET (offsetof(wisun_addr_t, sin6_flowinfo))
#define
SOCKADDR_WISUN_ADDRESS_OFFSET (offsetof(wisun_addr_t, sin6_addr))
#define
SOCKADDR_WISUN_SCOPEID_OFFSET (offsetof(wisun_addr_t, sin6_scope_id))
#define

Definition of any address for IPv4.

Variable Documentation#

in6addr_any#

const in6_addr_t in6addr_any

Definition of any address for IPv6.


Definition at line 180 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

Function Documentation#

socket#

int32_t socket (sock_domain_t domain, sock_type_t type, proto_type_t protocol)

Create an endpoint for communication and return a socket ID.

Parameters
[in]domain

The communication domain, this selects the protocol family. It can be: AF_WISUN - Wi-SUN FAN AF_INET6 - External IPv6 network socket (future implementation) AF_INET - External IPv4 network address (future implementation)

[in]type

The communication semantics It can be: SOCK_STREAM - Stream Socket type (TCP) SOCK_DGRAM - Datagram Socket type (UDP) SOCK_RAW - Raw Socket type (ICMP)

[in]protocol

Specifies the particular protocol to be used. It can be: IPPROTO_AUTO - Auto selection SOCK_STREAM -> TCP, SOCK_DGRAM -> UDP IPPROTO_IP - Same then IPPROTO_AUTO IPPROTO_ICMP - Ping IPPROTO_TCP - TCP protocol IPPROTO_UDP - UDP protocol

Socket API support Wi-SUN FAN and external IP network communications. External handlers can be implemented, the default implementations are weak functions. Created sockets added to socket handler automatically Returns

  • if any error occurred, return with -1, otherwise return with the socket id


Definition at line 211 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

close#

int32_t close (int32_t sockid)

Close socket.

Parameters
[in]sockid

socket id

Close socket and remove from the socket handler storage Returns

  • if any error occurred, return with -1, otherwise return with the socket id


Definition at line 219 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

bind#

int32_t bind (int32_t sockid, const struct sockaddr * addr, socklen_t addrlen)

Bind a name to a socket.

Parameters
[in]sockid

socket id

[in]addr

address structure ptr

[in]addrlen

address structure size

Assigns the address specified by addr to the socket referred to by the socket id. It is normally necessary to assign a local address using bind() before a SOCK_STREAM socket may receive connections Returns

  • On success, zero is returned. On error, -1 is returned


Definition at line 231 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

listen#

int32_t listen (int32_t sockid, int32_t backlog)

Listen for connections on a socket.

Parameters
[in]sockid

socket id

[in]backlog

Argument defines the maximum length to which the queue of pending connections for sockid may grow. Not implemented for Wi-SUN, the connection queue size is always 1

Marks the socket referred to by sockid as a passive socket, that is, as a socket that will be used to accept incoming connection requests using accept Returns

  • On success, zero is returned. On error, -1 is returned


Definition at line 245 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

accept#

int32_t accept (int32_t sockid, struct sockaddr * addr, socklen_t * addrlen)

Accept a connection on a socket.

Parameters
[in]sockid

socket id

[inout]addr

Is filled in with the address of the peer (remote) socket In Wi-SUN case, it shouldn't be NULL ptr, but for external sockets should be.

[inout]addrlen

It is the byte size of the address. In Wi-SUN, it won't be set to the actual size on return.

It is used with connection-based socket types (TCP). It extracts the first connection request on the queue of pending connections for the listening socket. Returns

  • On success, return with the socket id for accepted socket, on error -1 is returned.


Definition at line 259 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

connect#

int32_t connect (int32_t sockid, const struct sockaddr * addr, socklen_t addrlen)

Initiate a connection on a socket.

Parameters
[in]sockid

socket id

[inout]addr

If the socket sockid is of type SOCK_DGRAM, then addr is the address to which datagrams are sent by default, and the only address from which datagrams are received. If the socket is of type SOCK_STREAM, this call attempts to make a connection to the socket that is bound to the address specified by addr.

[inout]addrlen

It is the byte size of the address. In Wi-SUN, it won't be set to the actual size on return.

Connects the socket referred to by the socketid to the address specified by addr. Returns

  • If the connection or binding succeeds, zero is returned. On error, -1 is returned.


Definition at line 274 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

send#

int32_t send (int32_t sockid, const void * buff, uint32_t len, int32_t flags)

Send a message on a socket,.

Parameters
[in]sockid

socket id

[in]buff

buffer pointer to send

[in]len

length of buffer to send

[in]flags

In Wi-SUN domain, the flags is not used.

It is preferred with connection-oriented sockets (TCP). Returns

  • On success, these calls return the number of bytes sent. On error, -1 is returned


Definition at line 285 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

sendto#

int32_t sendto (int32_t sockid, const void * buff, uint32_t len, int32_t flags, const struct sockaddr * dest_addr, socklen_t addr_len)

Send a message on a socket.

Parameters
[in]sockid

socket id

[in]buff

buffer pointer to send

[in]len

length of buffer to send

[in]flags

In Wi-SUN domain, the flags is not used.

[in]dest_addr

destination address ptr, the structure must be prepared for UDP sockets

[in]addr_len

destination address length

It is preferred in datagram mode (UDP). Returns

  • On success, these calls return the number of bytes sent. On error, -1 is returned


Definition at line 298 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

recv#

int32_t recv (int32_t sockid, void * buf, uint32_t len, int32_t flags)

Receive a message from a socket.

Parameters
[in]sockid

socket id

[out]buf

destination buffer ptr

[in]len

length of data to read

[in]flags

In Wi-SUN domain, the flags is not used.

It should be used for connection-oriented protocol (TCP) Returns

  • return the number of bytes received, or -1 if an error occurred


Definition at line 310 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

recvfrom#

int32_t recvfrom (int32_t sockid, void * buf, uint32_t len, int32_t flags, struct sockaddr * src_addr, socklen_t * addrlen)

Receive messages from a socket.

Parameters
[in]sockid

socket id

[out]buf

destination buffer ptr

[in]len

length of data to read

[in]flags

In Wi-SUN domain, the flags is not used.

[in]src_addr

Source address

[in]addrlen

length of the source address

It can be used to receive data on a socket whether or not it is connection-oriented. Returns

  • return the number of bytes received, or -1 if an error occurred


Definition at line 323 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

setsockopt#

int32_t setsockopt (int32_t sockid, int32_t level, int32_t optname, const void * optval, socklen_t optlen)

Set socket option.

Parameters
[in]sockid

socket id

[in]level

Not used in Wi-SUN domain.

[in]optname

Option name. For Wi-SUN: SL_WISUN_SOCKET_OPTION_EVENT_MODE - Option for socket event mode SL_WISUN_SOCKET_OPTION_MULTICAST_GROUP - Option for multicast group SL_WISUN_SOCKET_OPTION_SEND_BUFFER_LIMIT - Option for send buffer limit

[in]optval

Option value structure pointer. For Wi-SUN it is casted to sl_wisun_socket_option_t

[in]optlen

Must be the size of sl_wisun_socket_option_data_t union

This function can set socket properties. There are some limitation in Wi-SUN domain: optlen and level args are not used. optname and optval are casted to the corresponding Wi-SUN types: sl_wisun_socket_option_t and sl_wisun_socket_option_data_t. sl_wisun_socket_option_data_t is a union type to include all of implemented Wi-SUN socket options Returns

  • Return 0 on succes, other wise -1


Definition at line 344 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

getsockopt#

int32_t getsockopt (int32_t sockid, int32_t level, int32_t optname, void * optval, socklen_t * optlen)

Get socket option.

Parameters
[in]sockid

socket id

[in]level

Not used in Wi-SUN domain.

[in]optname

Option name. For Wi-SUN: SL_WISUN_SOCKET_OPTION_EVENT_MODE - Option for socket event mode SL_WISUN_SOCKET_OPTION_MULTICAST_GROUP - Option for multicast group SL_WISUN_SOCKET_OPTION_SEND_BUFFER_LIMIT - Option for send buffer limit

[out]optval

Destination structure pointer. For Wi-SUN it is casted to sl_wisun_socket_option_t

[in]optlen

Must be the size of sl_wisun_socket_option_data_t union

The function gets socket option by optname, and copy option data to optval ptr Returns

  • Return 0 on success, other wise -1


Definition at line 361 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

htonl#

static uint32_t htonl (uint32_t hostlong)

Long host byte order to network order.

Parameters
[in]hostlong

Long host integer

It converts the unsigned integer hostlong from host byte order to network byte order. For Wi-SUN, conversion is not needed. Dummy implementation Returns

  • Long network integer


Definition at line 371 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

htons#

static uint16_t htons (uint16_t hostshort)

Short host byte order to network order.

Parameters
[in]hostshort

Short host integer

It converts the unsigned short integer hostshort from host byte order to network byte order. For Wi-SUN, conversion is not needed. Dummy implementation Returns

  • Short network integer


Definition at line 383 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

ntohl#

static uint32_t ntohl (uint32_t netlong)

Long network byte order to host byte order.

Parameters
[in]netlong

Long network integer

It converts the unsigned integer netlong from network byte order to host byte order. For Wi-SUN, conversion is not needed. Dummy implementation Returns

  • Long host integer


Definition at line 395 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

ntohs#

static uint16_t ntohs (uint16_t netshort)

Short network byte order to host byte order.

Parameters
[in]netshort

It converts the unsigned short integer netshort from network byte order to host byte order. For Wi-SUN, conversion is not needed. Dummy implementation Returns

  • Short host integer


Definition at line 407 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

inet_pton#

int32_t inet_pton (sock_domain_t af, const char * src, void * dst)

Convert IPv4 and IPv6 addresses from text to binary form.

Parameters
[in]af

Address family. The af argument must be either AF_WISUN, AF_INET6 or AF_INET

[in]src

Source string

[out]dst

Destination address pointer

It converts the character string src into a network address structure in the af address family, then copies the network address structure to dst For AF_WISUN or AF_INET6, stoip6(src, len, dst) called within the implementation AF_INET (IPv4) case not implemented. Returns

  • 1 on succes, -1 on error (POSIX described the 0 value too)


Definition at line 423 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

inet_ntop#

const char* inet_ntop (sock_domain_t af, const void * src, char * dst, socklen_t size)

Convert IPv4 and IPv6 addresses from binary to text form.

Parameters
[in]af

Address family. The af argument must be either AF_WISUN, AF_INET6 or AF_INET

[in]src

Source address in byte form

[out]dst

Destination buffer ptr

[in]size

Size of the destination buffer.

Converts the network address structure src in the af address family into a character string. The resulting string is copied to the buffer pointed to by dst, which must be a non-null pointer. The caller specifies the number of bytes available in this buffer in the argument size. For AF_WISUN or AF_INET6, stoip6(src, len, dst) called within the implementation AF_INET (IPv4) case not implemented. Returns

  • It returns a non-null pointer to dst. NULL is returned if there was an error


Definition at line 439 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

Macro Definition Documentation#

SOCKADDR_WISUN_FAMILY_OFFSET#

#define SOCKADDR_WISUN_FAMILY_OFFSET
Value:
(offsetof(wisun_addr_t, sin6_family))

Wi-SUN address structure offsets.


Definition at line 162 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

SOCKADDR_WISUN_PORT_OFFSET#

#define SOCKADDR_WISUN_PORT_OFFSET
Value:
(offsetof(wisun_addr_t, sin6_port))

Definition at line 163 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

SOCKADDR_WISUN_FLOWINFO_OFFSET#

#define SOCKADDR_WISUN_FLOWINFO_OFFSET
Value:
(offsetof(wisun_addr_t, sin6_flowinfo))

Definition at line 164 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

SOCKADDR_WISUN_ADDRESS_OFFSET#

#define SOCKADDR_WISUN_ADDRESS_OFFSET
Value:
(offsetof(wisun_addr_t, sin6_addr))

Definition at line 165 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

SOCKADDR_WISUN_SCOPEID_OFFSET#

#define SOCKADDR_WISUN_SCOPEID_OFFSET
Value:
(offsetof(wisun_addr_t, sin6_scope_id))

Definition at line 166 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h

INADDR_ANY#

#define INADDR_ANY
Value:
(0UL)

Definition of any address for IPv4.


Definition at line 171 of file C:/silabs/wisun_gsdk/app/wisun/component/socket/socket.h