Wi-SUN Sockets#
The Wi-SUN socket API is loosely modeled after the well-known BSD socket API, also known as POSIX socket API. Each socket represents a handle for the local endpoint of a communication circuit.
For a POSIX-compliant socket interface, refer to the POSIX-compliant Socket software component.
API Overview#
BSD Function | Wi-SUN API Function | Description |
---|---|---|
Open a socket. . | ||
Bind a socket to a specific local address and/or a port number. . | ||
Set a TCP socket to listening state. . | ||
Initiate a connection from a socket to a remote peer socket. . | ||
Accept a pending connection request on a TCP socket. . | ||
Write data to a connected socket. . | ||
Read data from a socket. . | ||
Write data to an unconnected socket. . | ||
Not supported | ||
Close a socket. . | ||
gethostbyname() | Not supported | |
gethostbyaddr() | Not supported | |
select() | Not supported | |
poll() | Not supported | |
Get a socket option. . | ||
Set a socket option. . |
Socket Life Cycle#
In general, a socket is created by explicitly calling sl_wisun_open_socket() and must be explicitly closed by calling sl_wisun_close_socket(). This both closes the communication circuit and frees any resources allocated to the socket.
In certain protocols, the communication circuit may be closed by the remote peer. This is indicated with a SL_WISUN_MSG_SOCKET_CLOSING_IND_ID event. Although the socket connection is closed, the system resources will not be freed until the application calls sl_wisun_close_socket().
Exchanging Data#
Certain protocols require a socket in a connected state, meaning the underlying protocol must first establish a session with the remote peer before data can be exchanged. It is also optionally possible on certain connectionless protocols. In either case, connecting a socket creates a direct communication link with a remote peer, allowing the socket to be used only with that particular peer.
To switch a socket into connected state, use sl_wisun_connect_socket(). See the table below for protocol details.
Protocol | Unconnected | Connected |
---|---|---|
MAY | MAY | |
MUST NOT | MUST | |
MUST | MUST NOT |
Transmit#
When a socket is in unconnected state, sl_wisun_sendto_on_socket() is used to transmit data. The function requires the application to specify the remote peer address and port number on each call. When a socket is connected, sl_wisun_send_on_socket() is used instead. Because the socket is "locked" into a single remote peer, address and port number are not needed.
Both functions are asynchronous. A successful return code indicates the socket has buffered the provided data and the application may free the data resources. The application receives a SL_WISUN_MSG_SOCKET_DATA_SENT_IND_ID event when a part or all buffered data has been sent.
Receive#
To manage received socket data, the application has indication mode and polling mode. All sockets default to indication mode. In this mode, the received data is sent as a SL_WISUN_MSG_SOCKET_DATA_IND_ID event as soon as it is received. The application must either handle the data immediately or store it for later processing. Either way, the socket will not keep the data once the event handler returns. In polling mode, the application will receive a SL_WISUN_MSG_SOCKET_DATA_AVAILABLE_IND_ID event on data reception. The received data is stored in the socket buffer until the application reads it using sl_wisun_receive_on_socket().
Client#
TCP Example#
Open a SL_WISUN_SOCKET_PROTOCOL_TCP type socket using sl_wisun_open_socket().
Initiate connection to the remote server using sl_wisun_connect_socket().
Wait for SL_WISUN_MSG_SOCKET_CONNECTED_IND_ID event.
... Exchange data with the remote server ...
Close the socket using sl_wisun_close_socket().
UDP Example#
Open a SL_WISUN_SOCKET_PROTOCOL_UDP type socket using sl_wisun_open_socket().
... Exchange data with the remote server ...
Close the socket using sl_wisun_close_socket().
Server#
TCP Example#
Open a SL_WISUN_SOCKET_PROTOCOL_TCP type server socket using sl_wisun_open_socket().
Set the server socket port number using sl_wisun_bind_socket().
Set the server socket into listening state using sl_wisun_listen_on_socket().
Wait for SL_WISUN_MSG_SOCKET_CONNECTION_AVAILABLE_IND_ID event.
Accept the client connection using sl_wisun_accept_on_socket() on the server socket.
... Exchange data with the remote client using the newly created client socket ...
Close the client socket using sl_wisun_close_socket().
Go to 4.
UDP Example#
Open a SL_WISUN_SOCKET_PROTOCOL_UDP type socket using sl_wisun_open_socket().
Set the socket port number using sl_wisun_bind_socket().
... Exchange data with the remote client ...