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 and is extremely valuable in the IP world to leverage the various application protocols. 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.

Enumerations#

enum
SOL_SOCKET_HANDLER = 0
SOL_SOCKET = 1
}

Socket option level enum type definition.

enum
SO_HANDLER_OVERWRITE_PREV_RCV_BUFF = -1
}

Socket option level enum type definition.

Typedefs#

typedef enum socket_option_level

Socket option level enum type definition.

typedef enum socket_handler_opt

Socket option level enum type definition.

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 ocket ID communication and return.

int32_t
close(int32_t sockid)

Close a 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.

__STATIC_INLINE uint32_t
htonl(uint32_t hostlong)

Convert the long host byte order to network order.

__STATIC_INLINE uint16_t
htons(uint16_t hostshort)

Convert the short host byte order to network order.

__STATIC_INLINE uint32_t
ntohl(uint32_t netlong)

Convert the long network byte order to host byte order.

__STATIC_INLINE uint16_t
ntohs(uint16_t netshort)

Convert the short network byte order to host byte order.

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

Convert the 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))

Address structure family offset.

#define
SOCKADDR_WISUN_PORT_OFFSET (offsetof(wisun_addr_t, sin6_port))

Address structure port offset.

#define
SOCKADDR_WISUN_FLOWINFO_OFFSET (offsetof(wisun_addr_t, sin6_flowinfo))

Address structure flow info offset.

#define
SOCKADDR_WISUN_ADDRESS_OFFSET (offsetof(wisun_addr_t, sin6_addr))

Address structure address offset.

#define
SOCKADDR_WISUN_SCOPEID_OFFSET (offsetof(wisun_addr_t, sin6_scope_id))

Address structure scope ID offset.

#define
INADDR_ANY (0UL)

Definition of any address for IPv4.

Enumeration Documentation#

socket_option_level#

socket_option_level

Socket option level enum type definition.

Enumerator
SOL_SOCKET_HANDLER

Special Wi-SUN Socket handler options.

SOL_SOCKET

POSIX style.


socket_handler_opt#

socket_handler_opt

Socket option level enum type definition.

Enumerator
SO_HANDLER_OVERWRITE_PREV_RCV_BUFF

Typedef Documentation#

socket_option_level_t#

typedef enum socket_option_level socket_option_level_t

Socket option level enum type definition.


socket_handler_opt_t#

typedef enum socket_handler_opt socket_handler_opt_t

Socket option level enum type definition.


Variable Documentation#

in6addr_any#

const in6_addr_t in6addr_any

Definition of any address for IPv6.


Function Documentation#

socket#

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

Create an endpoint for ocket ID communication and return.

Parameters
TypeDirectionArgument NameDescription
sock_domain_t[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)

sock_type_t[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)

proto_type_t[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 supports Wi-SUN FAN and external IP network communications. External handlers can be implemented; Default implementations are weak functions. Created sockets are added to the socket handler automatically. Returns

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


close#

int32_t close (int32_t sockid)

Close a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

Close a socket and remove from the socket handler storage. Returns

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


bind#

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

Bind a name to a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

const struct sockaddr *[in]addr

address structure ptr

socklen_t[in]addrlen

address structure size

Assigns the address to the socket, referred to by the socket ID, as specified by addr. 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


listen#

int32_t listen (int32_t sockid, int32_t backlog)

Listen for connections on a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

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


accept#

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

Accept a connection on a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket ID

struct sockaddr *[inout]addr

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

socklen_t *[inout]addrlen

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

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.


connect#

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

Initiate a connection on a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

const struct sockaddr *[inout]addr

If the socket sockid is of type SOCK_DGRAM, 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.

socklen_t[inout]addrlen

It is the byte size of the address. For 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.


send#

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

Send a message on a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket ID

const void *[in]buff

buffer pointer to send

uint32_t[in]len

length of buffer to send

int32_t[in]flags

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

Preferred with connection-oriented sockets (TCP). Returns

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


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
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket ID

const void *[in]buff

buffer pointer to send

uint32_t[in]len

length of buffer to send

int32_t[in]flags

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

const struct sockaddr *[in]dest_addr

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

socklen_t[in]addr_len

destination address length

Preferred in datagram mode (UDP). Returns

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


recv#

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

Receive a message from a socket.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

void *[out]buf

destination buffer ptr

uint32_t[in]len

length of data to read

int32_t[in]flags

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

Should be used for connection-oriented protocol (TCP) Returns

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


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
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket id

void *[out]buf

destination buffer ptr

uint32_t[in]len

length of data to read

int32_t[in]flags

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

struct sockaddr *[in]src_addr

Source address

socklen_t *[in]addrlen

length of the source address

Receives data on a socket whether or not it is connection-oriented. Returns

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


setsockopt#

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

Set socket option.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket ID

int32_t[in]level

SO_SOCKET for Wi-SUN socket options, or SO_SOCKET_HANDLER for socket handler options.

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

const void *[in]optval

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

socklen_t[in]optlen

Must be the size of sl_wisun_socket_option_data_t union

This function can set socket properties. Limitation for the Wi-SUN domain are that optlen and level args are not used. optname and optval are cast 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 success, other wise -1


getsockopt#

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

Get socket option.

Parameters
TypeDirectionArgument NameDescription
int32_t[in]sockid

socket ID

int32_t[in]level

SO_SOCKET for Wi-SUN socket options, or SO_SOCKET_HANDLER for socket handler options.

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

void *[out]optval

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

socklen_t *[in]optlen

Must be the size of sl_wisun_socket_option_data_t union

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

  • Return 0 on success, other wise -1


htonl#

__STATIC_INLINE uint32_t htonl (uint32_t hostlong)

Convert the long host byte order to network order.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]hostlong

Long host integer

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

  • Long network integer


htons#

__STATIC_INLINE uint16_t htons (uint16_t hostshort)

Convert the short host byte order to network order.

Parameters
TypeDirectionArgument NameDescription
uint16_t[in]hostshort

Short host integer

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

  • Short network integer


ntohl#

__STATIC_INLINE uint32_t ntohl (uint32_t netlong)

Convert the long network byte order to host byte order.

Parameters
TypeDirectionArgument NameDescription
uint32_t[in]netlong

Long network integer

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

  • Long host integer


ntohs#

__STATIC_INLINE uint16_t ntohs (uint16_t netshort)

Convert the short network byte order to host byte order.

Parameters
TypeDirectionArgument NameDescription
uint16_t[in]netshort

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

  • Short host integer


References inet_ntop , and inet_pton

inet_pton#

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

Convert the IPv4 and IPv6 addresses from text to binary form.

Parameters
TypeDirectionArgument NameDescription
sock_domain_t[in]af

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

const char *[in]src

Source string

void *[out]dst

Destination address pointer

This function 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)


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
TypeDirectionArgument NameDescription
sock_domain_t[in]af

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

const void *[in]src

Source address in byte form

char *[out]dst

Destination buffer ptr

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