| CMSIS-Driver Peripheral Interface for Middleware and Application Code |

Loading...
Searching...
No Matches
Content | Functions
WiFi Socket
WiFi Interface

Socket interface to IP stack running on WiFi module. More...

## Content
| WiFi Socket Address Family definitions
| WiFi Socket Address Family definitions.
| WiFi Socket Type definitions
| WiFi Socket Type definitions.
| WiFi Socket Protocol definitions
| WiFi Socket Protocol definitions.
| WiFi Socket Option definitions
| WiFi Socket Option definitions.
| WiFi Socket Function return codes
| WiFi Socket Function return codes.

## Functions
int32_t | ARM_WIFI_SocketCreate (int32_t af, int32_t type, int32_t protocol)
| Create a communication socket.
int32_t | ARM_WIFI_SocketBind (int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)
| Assign a local address to a socket.
int32_t | ARM_WIFI_SocketListen (int32_t socket, int32_t backlog)
| Listen for socket connections.
int32_t | ARM_WIFI_SocketAccept (int32_t socket, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
| Accept a new connection on a socket.
int32_t | ARM_WIFI_SocketConnect (int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)
| Connect a socket to a remote host.
int32_t | ARM_WIFI_SocketRecv (int32_t socket, void *buf, uint32_t len)
| Receive data or check if data is available on a connected socket.
int32_t | ARM_WIFI_SocketRecvFrom (int32_t socket, void *buf, uint32_t len, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
| Receive data or check if data is available on a socket.
int32_t | ARM_WIFI_SocketSend (int32_t socket, const void *buf, uint32_t len)
| Send data or check if data can be sent on a connected socket.
int32_t | ARM_WIFI_SocketSendTo (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 | ARM_WIFI_SocketGetSockName (int32_t socket, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
| Retrieve local IP address and port of a socket.
int32_t | ARM_WIFI_SocketGetPeerName (int32_t socket, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
| Retrieve remote IP address and port of a socket.
int32_t | ARM_WIFI_SocketGetOpt (int32_t socket, int32_t opt_id, void *opt_val, uint32_t *opt_len)
| Get socket option.
int32_t | ARM_WIFI_SocketSetOpt (int32_t socket, int32_t opt_id, const void *opt_val, uint32_t opt_len)
| Set socket option.
int32_t | ARM_WIFI_SocketClose (int32_t socket)
| Close and release a socket.
int32_t | ARM_WIFI_SocketGetHostByName (const char *name, int32_t af, uint8_t *ip, uint32_t *ip_len)
| Retrieve host IP address from host name.
int32_t | ARM_WIFI_Ping (const uint8_t *ip, uint32_t ip_len)
| Probe remote host with Ping command.

## Description

Socket interface to IP stack running on WiFi module.

The WiFi Socket functions provide the interface to an IP stack that is running on the WiFi module. This IP stack handles data communication with the network and provides the user with a communication endpoint called sockets.

## Function Documentation

## ◆ ARM_WIFI_SocketCreate()

int32_t ARM_WIFI_SocketCreate | ( | int32_t | af,
|  | int32_t | type,
|  | int32_t | protocol
| ) |  |

Create a communication socket.

Parameters

  [in] | af | Address family
  [in] | type | Socket type
  [in] | protocol | Socket protocol

Returns
  status information
  - Socket identification number (>=0)
  - ARM_SOCKET_EINVAL : Invalid argument
  - ARM_SOCKET_ENOTSUP : Operation not supported
  - ARM_SOCKET_ENOMEM : Not enough memory
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketCreate creates a communication endpoint called a socket.

The argument af specifies the address family. The following values are supported:

Family | Description
--- | ---
ARM_SOCKET_AF_INET | Address Family Internet
ARM_SOCKET_AF_INET6 | Address Family Internet version 6

The argument type specifies the communication semantics. The following are the currently supported types:

Type | Description
--- | ---
ARM_SOCKET_SOCK_STREAM | Provides a reliable connection based data stream that is full-duplex
ARM_SOCKET_SOCK_DGRAM | Provides connectionless communication that is unreliable

The argument protocol specifies the protocol that must be used with the socket type:

Protocol | Description
--- | ---
ARM_SOCKET_IPPROTO_TCP | Must be used with ARM_SOCKET_SOCK_STREAM socket type
ARM_SOCKET_IPPROTO_UDP | Must be used with ARM_SOCKET_SOCK_DGRAM socket type
0 | The system selects a matching protocol for the socket type

Example:

- see ARM_WIFI_SocketListen, ARM_WIFI_SocketConnect

## ◆ ARM_WIFI_SocketBind()

int32_t ARM_WIFI_SocketBind | ( | 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 local IP address
  [in] | ip_len | Length of 'ip' address in bytes
  [in] | port | Local port number

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (address or socket already bound)
  - ARM_SOCKET_EADDRINUSE : Address already in use
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketBind assigns a name to an unnamed socket. The name represents the local address and port of the communication endpoint.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument ip is a pointer to the buffer containing the IP address octets of the local IP address.

The argument ip_len specifies the length of the local IP address. The length is 4 bytes for the IPv4 address and 16 bytes for the IPv6 address.

The argument port specifies the local port. If the argument port is 0, the function returns error, because this port is reserved.

Example:

- see ARM_WIFI_SocketListen

## ◆ ARM_WIFI_SocketListen()

int32_t ARM_WIFI_SocketListen | ( | int32_t | socket,
|  | int32_t | backlog
| ) |  |

Listen for socket connections.

Parameters

  [in] | socket | Socket identification number
  [in] | backlog | Number of connection requests that can be queued

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (socket not bound)
  - ARM_SOCKET_ENOTSUP : Operation not supported
  - ARM_SOCKET_EISCONN : Socket is already connected
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketListen sets the specified socket to listening mode, that is to the server mode of operation. Before calling the ARM_WIFI_SocketListen function, the ARM_WIFI_SocketBind function must be called.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument backlog specifies a maximum number of connection requests that can be queued.

Example:

extern ARM_DRIVER_WIFI Driver_WiFi0;
static ARM_DRIVER_WIFI *wifi;
void Echo_Server_Thread (void *arg) {
uint8_t ip[4] = { 0U, 0U, 0U, 0U };
int32_t sock, sd, res;
char dbuf[120];
while (1) {
wifi = &Driver_WiFi0;
sock = wifi->SocketCreate (ARM_SOCKET_AF_INET, ARM_SOCKET_SOCK_STREAM, ARM_SOCKET_IPPROTO_TCP);
wifi->SocketBind (sock, (uint8_t *)ip, sizeof(ip), 7U);
wifi->SocketListen (sock, 1);
sd = wifi->SocketAccept (sock, NULL, NULL, NULL);
wifi->SocketClose (sock);
sock = sd;
while (1) {
res = wifi->SocketRecv (sock, dbuf, sizeof(dbuf));
if (res < 0) {
break; // Error occurred
}
if (res > 0) {
wifi->SocketSend (sock, dbuf, res); // Echo the data
}
}
wifi->SocketClose (sock);
}
}
ARM_SOCKET_AF_INET
#define ARM_SOCKET_AF_INET
IPv4.
ARM_DRIVER_WIFI::SocketRecv
int32_t(* SocketRecv)(int32_t socket, void *buf, uint32_t len)
Definition: Driver_WiFi.h:648
ARM_DRIVER_WIFI::SocketClose
int32_t(* SocketClose)(int32_t socket)
Definition: Driver_WiFi.h:656
ARM_DRIVER_WIFI::SocketAccept
int32_t(* SocketAccept)(int32_t socket, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
Definition: Driver_WiFi.h:646
ARM_DRIVER_WIFI::SocketSend
int32_t(* SocketSend)(int32_t socket, const void *buf, uint32_t len)
Definition: Driver_WiFi.h:650
ARM_DRIVER_WIFI::SocketCreate
int32_t(* SocketCreate)(int32_t af, int32_t type, int32_t protocol)
Definition: Driver_WiFi.h:643
ARM_DRIVER_WIFI::SocketListen
int32_t(* SocketListen)(int32_t socket, int32_t backlog)
Definition: Driver_WiFi.h:645
ARM_DRIVER_WIFI::SocketBind
int32_t(* SocketBind)(int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)
Definition: Driver_WiFi.h:644
ARM_DRIVER_WIFI
Access structure of the WiFi Driver.
Definition: Driver_WiFi.h:625
ARM_SOCKET_IPPROTO_TCP
#define ARM_SOCKET_IPPROTO_TCP
TCP.
ARM_SOCKET_SOCK_STREAM
#define ARM_SOCKET_SOCK_STREAM
Stream socket.

## ◆ ARM_WIFI_SocketAccept()

int32_t ARM_WIFI_SocketAccept | ( | 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 buffer where address of connecting socket shall be returned (NULL for none)
  [in,out] | ip_len | Pointer to length of 'ip' (or NULL if 'ip' is NULL) length of supplied 'ip' on input length of stored 'ip' on output
  [out] | port | Pointer to buffer where port of connecting socket shall be returned (NULL for none)

Returns
  status information
  - socket identification number of accepted socket (>=0)
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (socket not in listen mode)
  - ARM_SOCKET_ENOTSUP : Operation not supported (socket type does not support accepting connections)
  - ARM_SOCKET_ECONNRESET : Connection reset by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EAGAIN : Operation would block or timed out (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketAccept accepts a connection request queued for a listening socket. If a connection request is pending, ARM_WIFI_SocketAccept removes the request from the queue, and creates a new socket for the connection. The original listening socket remains open and continues to queue new connection requests. The socket must be a socket of type ARM_SOCKET_SOCK_STREAM.

In blocking mode, which is enabled by default, this function waits for a connection request. In non blocking mode, you must call the ARM_WIFI_SocketAccept function again if the error code `ARM_SOCKET_EAGAIN` is returned.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument ip is a pointer to the buffer that will receive the IP address of the connection node. If the ip is NULL, the IP address is not returned.

The argument ip_len is a pointer to the IP address length. It should initially contain the amount of space pointed to by ip. On return it contains the actual length of the address returned in bytes.

The argument port is a pointer to the buffer, that will receive the port number of the connection node. If the port is NULL, the port number is not returned.

Example:

- see ARM_WIFI_SocketListen

## ◆ ARM_WIFI_SocketConnect()

int32_t ARM_WIFI_SocketConnect | ( | 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 remote IP address
  [in] | ip_len | Length of 'ip' address in bytes
  [in] | port | Remote port number

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument
  - ARM_SOCKET_EALREADY : Connection already in progress
  - ARM_SOCKET_EINPROGRESS : Operation in progress
  - ARM_SOCKET_EISCONN : Socket is connected
  - ARM_SOCKET_ECONNREFUSED : Connection rejected by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EADDRINUSE : Address already in use
  - ARM_SOCKET_ETIMEDOUT : Operation timed out
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketConnect assigns the address of the peer communication endpoint. The function behaves differently according to the type of socket:

- ARM_SOCKET_SOCK_STREAM: A connection is established between the endpoints.

  In blocking mode, which is enabled by default, this function waits for a connection to be established.

  In non blocking mode, the function returns the error code `ARM_SOCKET_EINPROGRESS` and the connection is established asynchronously. Subsequent calls to ARM_WIFI_SocketConnect for the same socket, before the connection is established, return the error code `ARM_SOCKET_EALREADY`. When the connection is established, the call to ARM_WIFI_SocketConnect returns the error code `ARM_SOCKET_EISCONN`.

- ARM_SOCKET_SOCK_DGRAM: An address filter is established between the endpoints.

  The address filter is changed with another ARM_WIFI_SocketConnect function call. If the socket is not yet bound, the system implicitly binds to a random dynamic port.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument ip is a pointer to the buffer containing the IP address octets of the endpoint node.

The argument ip_len specifies the length of the IP address. The length is 4 bytes for the IPv4 address and 16 bytes for the IPv6 address.

The argument port specifies the port of the endpoint node. If the argument port is 0, the function returns error, because this port is reserved.

Example:

extern ARM_DRIVER_WIFI Driver_WiFi0;
static ARM_DRIVER_WIFI *wifi;
static const char message[] = { "The quick brown fox jumps over the lazy dog." };
void Echo_Client_Thread (void *arg) {
uint8_t ip[4] = { 192U, 168U, 0U, 100U };
int32_t sock, res;
char dbuf[120];
while (1) {
wifi = &Driver_WiFi0;
sock = wifi->SocketCreate (ARM_SOCKET_AF_INET, ARM_SOCKET_SOCK_STREAM, ARM_SOCKET_IPPROTO_TCP);
res = wifi->SocketConnect (sock, (uint8_t *)ip, sizeof(ip), 7U);
if (res == 0) {
wifi->SocketSend (sock, message, sizeof(message));
res = wifi->SocketRecv (sock, dbuf, sizeof(dbuf));
if (res < 0) {
break; // Error occurred
}
if (res > 0) {
if (memcmp (dbuf, message, res) != 0) {
// error handling, message is not the same as sent
}
}
}
wifi->SocketClose (sock);
osDelay (1000U);
}
}
ARM_DRIVER_WIFI::SocketConnect
int32_t(* SocketConnect)(int32_t socket, const uint8_t *ip, uint32_t ip_len, uint16_t port)
Definition: Driver_WiFi.h:647

## ◆ ARM_WIFI_SocketRecv()

int32_t ARM_WIFI_SocketRecv | ( | int32_t | socket,
|  | void * | buf,
|  | uint32_t | len
| ) |  |

Receive data or check if data is available on a connected socket.

Parameters

  [in] | socket | Socket identification number
  [out] | buf | Pointer to buffer where data should be stored
  [in] | len | Length of buffer (in bytes), set len = 0 to check if data is available

Returns
  status information
  - number of bytes received (>=0), if len != 0
  - 0 : Data is available (len = 0)
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ENOTCONN : Socket is not connected
  - ARM_SOCKET_ECONNRESET : Connection reset by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EAGAIN : Operation would block or timed out (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketRecv receives incoming data that has been queued for the socket. You can use this function with both, the stream and the datagram socket. It reads as much information as currently available up to the size of the buffer specified.

In blocking mode, which is enabled by default, this function waits for received data. In non blocking mode, you must call the ARM_WIFI_SocketRecv function again if the error code `ARM_SOCKET_EAGAIN` is returned.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument buf is a pointer to the application data buffer for storing the data to. If the available data is too large to fit in the supplied application buffer buf, excess bytes are discarded in case of a datagram sockets. For stream sockets, the data is buffered internally so the application can retrieve all data by multiple calls of ARM_WIFI_SocketRecv function.

The argument len specifies the size of the application data buffer.

Note
  The function can also be used to check if the socket has data available to read by specifying 0 for argument len (argument buf is ignored). The function returns 0 if data is available or error code otherwise. In blocking mode, the function waits until data is available, in non blocking mode the function returns instantly.

Example:

- see ARM_WIFI_SocketListen

## ◆ ARM_WIFI_SocketRecvFrom()

int32_t ARM_WIFI_SocketRecvFrom | ( | int32_t | socket,
|  | void * | buf,
|  | uint32_t | len,
|  | uint8_t * | ip,
|  | uint32_t * | ip_len,
|  | uint16_t * | port
| ) |  |

Receive data or check if data is available on a socket.

Parameters

  [in] | socket | Socket identification number
  [out] | buf | Pointer to buffer where data should be stored
  [in] | len | Length of buffer (in bytes), set len = 0 to check if data is available
  [out] | ip | Pointer to buffer where remote source address shall be returned (NULL for none)
  [in,out] | ip_len | Pointer to length of 'ip' (or NULL if 'ip' is NULL) length of supplied 'ip' on input length of stored 'ip' on output
  [out] | port | Pointer to buffer where remote source port shall be returned (NULL for none)

Returns
  status information
  - number of bytes received (>=0), if len != 0
  - 0 : Data is available (len = 0)
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ENOTCONN : Socket is not connected
  - ARM_SOCKET_ECONNRESET : Connection reset by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EAGAIN : Operation would block or timed out (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketRecvFrom is used to receive data that has been queued for a socket. It is normally used to receive messages on datagram sockets, but can also be used to receive a reliable, ordered stream of data on a connected stream sockets. It reads as much information as currently available up to the size of the buffer specified.

In blocking mode, which is enabled by default, this function waits for received data. In non blocking mode, you must call the ARM_WIFI_SocketRecv function again if the error code `ARM_SOCKET_EAGAIN` is returned.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument buf is a pointer to the application data buffer for storing the data to. If the available data is too large to fit in the supplied application buffer buf, excess bytes are discarded in case of a datagram sockets. For stream sockets, the data is buffered internally so the application can retrieve all data by multiple calls of ARM_WIFI_SocketRecv function.

The argument len specifies the size of the application data buffer.

The argument ip is a pointer to the buffer that will receive the IP address of the sender. If the ip is NULL, the IP address is not returned.

The argument ip_len is a pointer to the IP address length. It should initially contain the amount of space pointed to by ip. On return it contains the actual length of the address returned in bytes.

The argument port is a pointer to the buffer, that will receive the port number of the sender. If the port is NULL, the port number is not returned.

Note
  The function can also be used to check if the socket has data available to read by specifying 0 for argument len (arguments buf, ip, ip_len and port are ignored). The function returns 0 if data is available or error code otherwise. In blocking mode, the function waits until data is available, in non blocking mode the function returns instantly.

Example:

extern ARM_DRIVER_WIFI Driver_WiFi0;
static ARM_DRIVER_WIFI *wifi;
void Echo_Server_Thread (void *arg) {
uint8_t ip[4];
uint16_t port;
int32_t sock, res;
uint32_t ip_len;
char dbuf[120];
while (1) {
wifi = &Driver_WiFi0;
sock = wifi->SocketCreate (ARM_SOCKET_AF_INET, ARM_SOCKET_SOCK_DGRAM, ARM_SOCKET_IPPROTO_UDP);
ip[0] = 0U; // Unspecified address
ip[1] = 0U;
ip[2] = 0U;
ip[3] = 0U;
port = 7U; // Standard port for Echo service
wifi->SocketBind (sock, (uint8_t *)ip, sizeof(ip), port);
while (1) {
ip_len = sizeof(ip);
res = wifi->SocketRecvFrom (sock, dbuf, sizeof(dbuf), (uint8_t *)ip, &ip_len, &port);
if (res < 0) {
break; // Error occurred
}
if (res > 0) { // Echo the data
wifi->SocketSendTo (sock, dbuf, res, (uint8_t *)ip, ip_len, port);
}
}
wifi->SocketClose (sock);
}
}
ARM_DRIVER_WIFI::SocketSendTo
int32_t(* SocketSendTo)(int32_t socket, const void *buf, uint32_t len, const uint8_t *ip, uint32_t ip_len, uint16_t port)
Definition: Driver_WiFi.h:651
ARM_DRIVER_WIFI::SocketRecvFrom
int32_t(* SocketRecvFrom)(int32_t socket, void *buf, uint32_t len, uint8_t *ip, uint32_t *ip_len, uint16_t *port)
Definition: Driver_WiFi.h:649
ARM_SOCKET_IPPROTO_UDP
#define ARM_SOCKET_IPPROTO_UDP
UDP.
ARM_SOCKET_SOCK_DGRAM
#define ARM_SOCKET_SOCK_DGRAM
Datagram socket.

## ◆ ARM_WIFI_SocketSend()

int32_t ARM_WIFI_SocketSend | ( | 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 buffer containing data to send
  [in] | len | Length of data (in bytes), set len = 0 to check if data can be sent

Returns
  status information
  - number of bytes sent (>=0), if len != 0
  - 0 : Data can be sent (len = 0)
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ENOTCONN : Socket is not connected
  - ARM_SOCKET_ECONNRESET : Connection reset by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EAGAIN : Operation would block or timed out (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketSend is used to send data on an already connected socket. This function is normally used to send a reliable, ordered stream of data bytes on a stream sockets. It can also be used to send messages on datagram sockets.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument buf is a pointer to the application data buffer containing data to transmit. The buffer data length is not limited in size. If the data length is too large for one packet, the ARM_WIFI_SocketSend function will fragment the data and send it in several successive data packets:

- In blocking mode, which is enabled by default, this function returns after the data has been successfully queued for transmission.
- In non blocking mode, the function returns immediately without blocking the system.

The argument len specifies the length of data in bytes.

Return value, when positive, represents the number of bytes sent, which can be less than len.

Note
  The function can also be used to check if the socket is ready to send data by specifying 0 for argument len (argument buf is ignored). The function returns 0 if the socket is writable or error code otherwise.

Example:

- see ARM_WIFI_SocketListen

## ◆ ARM_WIFI_SocketSendTo()

int32_t ARM_WIFI_SocketSendTo | ( | 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 buffer containing data to send
  [in] | len | Length of data (in bytes), set len = 0 to check if data can be sent
  [in] | ip | Pointer to remote destination IP address
  [in] | ip_len | Length of 'ip' address in bytes
  [in] | port | Remote destination port number

Returns
  status information
  - number of bytes sent (>=0), if len != 0
  - 0 : Data can be sent (len = 0)
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ENOTCONN : Socket is not connected
  - ARM_SOCKET_ECONNRESET : Connection reset by the peer
  - ARM_SOCKET_ECONNABORTED : Connection aborted locally
  - ARM_SOCKET_EAGAIN : Operation would block or timed out (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketSendTo is used to send data. It is normally used to send messages on a datagram sockets, but can also be used to send data on a connected stream sockets.

If the datagram socket is not yet bound, the system implicitly binds to a random dynamic port.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument buf is a pointer to the application data buffer containing data to transmit. The buffer data length is not limited in size. If the data length is too large for one packet, the ARM_WIFI_SocketSend function will fragment the data and send it in several successive data packets:

- In blocking mode, which is enabled by default, this function returns after the data has been successfully queued for transmission.
- In non blocking mode, the function returns immediately without blocking the system.

The argument len specifies the length of data in bytes.

The argument ip is a pointer to the buffer containing the IP address octets of the endpoint node.

The argument ip_len specifies the length of the IP address. The length is 4 bytes for the IPv4 address and 16 bytes for the IPv6 address.

The argument port specifies the port of the endpoint node. If the argument port is 0, the function returns error, because this port is reserved.

For the stream sockets, arguments ip, ip_len and port are ignored.

Return value, when positive, represents the number of bytes sent, which can be less than len.

Note
  The function can also be used to check if the socket is ready to send data by specifying 0 for argument len (arguments buf, ip, ip_len and port are ignored). The function returns 0 if the socket is writable or error code otherwise.

Example:

- see ARM_WIFI_SocketRecvFrom

## ◆ ARM_WIFI_SocketGetSockName()

int32_t ARM_WIFI_SocketGetSockName | ( | 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 buffer where local address shall be returned (NULL for none)
  [in,out] | ip_len | Pointer to length of 'ip' (or NULL if 'ip' is NULL) length of supplied 'ip' on input length of stored 'ip' on output
  [out] | port | Pointer to buffer where local port shall be returned (NULL for none)

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketGetSockName retrieves the local IP address and port for a socket.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument ip is a pointer to the buffer that will receive the local IP address. If the ip is NULL, the local IP address is not returned.

The argument ip_len is a pointer to the IP address length. It should initially contain the amount of space pointed to by ip. On return it contains the actual length of the address returned in bytes.

The argument port is a pointer to the buffer, that will receive the local port number. If the port is NULL, the local port number is not returned.

Example:

static uint8_t local_ip[4]; // Socket address and port
static uint16_t local_port;
static void get_socket_local_info (void) {
uint32_t ip_len;
ip_len = sizeof(local_ip);
wifi->SocketGetSockName (sock, (uint8_t *)local_ip, &ip_len, &local_port);
}

## ◆ ARM_WIFI_SocketGetPeerName()

int32_t ARM_WIFI_SocketGetPeerName | ( | 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 buffer where remote address shall be returned (NULL for none)
  [in,out] | ip_len | Pointer to length of 'ip' (or NULL if 'ip' is NULL) length of supplied 'ip' on input length of stored 'ip' on output
  [out] | port | Pointer to buffer where remote port shall be returned (NULL for none)

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument (pointer to buffer or length)
  - ARM_SOCKET_ENOTCONN : Socket is not connected
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketGetPeerName retrieves the IP address and port of the peer to which a socket is connected.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument ip is a pointer to the buffer that will receive the IP address of the peer. If the ip is NULL, the IP address is not returned.

The argument ip_len is a pointer to the IP address length. It should initially contain the amount of space pointed to by ip. On return it contains the actual length of the address returned in bytes.

The argument port is a pointer to the buffer, that will receive the port number of the peer. If the port is NULL, the port number is not returned.

Example:

static uint8_t peer_ip[4]; // Socket address and port
static uint16_t peer_port;
static void get_socket_peer_info (void) {
uint32_t ip_len;
ip_len = sizeof(peer_ip);
wifi->SocketGetPeerName (sock, (uint8_t *)peer_ip, &ip_len, &peer_port);
}

## ◆ ARM_WIFI_SocketGetOpt()

int32_t ARM_WIFI_SocketGetOpt | ( | int32_t | socket,
|  | int32_t | opt_id,
|  | void * | opt_val,
|  | uint32_t * | opt_len
| ) |  |

Get socket option.

Parameters

  [in] | socket | Socket identification number
  [in] | opt_id | Option identifier
  [out] | opt_val | Pointer to the buffer that will receive the option value
  [in,out] | opt_len | Pointer to length of the option value length of buffer on input length of data on output

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument
  - ARM_SOCKET_ENOTSUP : Operation not supported
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketGetOpt retrieves options for a socket.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument opt_id is the socket option for which the value is to be retrieved. The following socket options are supported:

Option | Description
--- | ---
ARM_SOCKET_SO_RCVTIMEO | Timeout for receiving in blocking mode
ARM_SOCKET_SO_SNDTIMEO | Timeout for sending in blocking mode
ARM_SOCKET_SO_KEEPALIVE | Keep-alive mode for the stream socket
ARM_SOCKET_SO_TYPE | Type of the socket (stream or datagram)

The argument opt_val points to the buffer that will receive the value of the opt_id.

The argument opt_len contains the length of the buffer at the input and returns the length of the option information on the output.

Example:

uint32_t type;
wifi->SocketGetOpt (sock, ARM_SOCKET_SO_TYPE, &type, sizeof(type));
if (type == ARM_SOCKET_SOCK_STREAM) {
// Stream socket
}
if (type == ARM_SOCKET_SOCK_DGRAM) {
// Datagram socket
}
ARM_SOCKET_SO_TYPE
#define ARM_SOCKET_SO_TYPE
Socket Type (Get only); opt_val = &socket_type, opt_len = sizeof(socket_type), socket_type (integer):...

## ◆ ARM_WIFI_SocketSetOpt()

int32_t ARM_WIFI_SocketSetOpt | ( | 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
  [in] | opt_val | Pointer to the option value
  [in] | opt_len | Length of the option value in bytes

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EINVAL : Invalid argument
  - ARM_SOCKET_ENOTSUP : Operation not supported
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketSetOpt sets options for a socket.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

The argument opt_id is the socket option for which the value is to be set. The following socket options are supported:

Option | Description
--- | ---
ARM_SOCKET_IO_FIONBIO | Non-blocking mode for the socket
ARM_SOCKET_SO_RCVTIMEO | Timeout for receiving in blocking mode
ARM_SOCKET_SO_SNDTIMEO | Timeout for sending in blocking mode
ARM_SOCKET_SO_KEEPALIVE | Keep-alive mode for the stream socket

The argument opt_val points to the buffer containing the value of the opt_id.

The argument opt_len tells the exact length of the option.

Example:

uint32_t nonblocking = 0U; // Blocking mode
uint32_t timeout = 10000U; // Timeout 10 seconds
wifi->SocketSetOpt (sock, ARM_SOCKET_IO_FIONBIO, &nonblocking, sizeof(nonblocking));
wifi->SocketSetOpt (sock, ARM_SOCKET_SO_RCVTIMEO, &timeout, sizeof(timeout));
wifi->SocketSetOpt (sock, ARM_SOCKET_SO_SNDTIMEO, &timeout, sizeof(timeout));
ARM_SOCKET_SO_RCVTIMEO
#define ARM_SOCKET_SO_RCVTIMEO
Receive timeout in ms (default = 0); opt_val = &timeout, opt_len = sizeof(timeout)
ARM_SOCKET_IO_FIONBIO
#define ARM_SOCKET_IO_FIONBIO
Non-blocking I/O (Set only, default = 0); opt_val = &nbio, opt_len = sizeof(nbio),...
ARM_SOCKET_SO_SNDTIMEO
#define ARM_SOCKET_SO_SNDTIMEO
Send timeout in ms (default = 0); opt_val = &timeout, opt_len = sizeof(timeout)

## ◆ ARM_WIFI_SocketClose()

int32_t ARM_WIFI_SocketClose | ( | int32_t | socket | ) |

Close and release a socket.

Parameters

  [in] | socket | Socket identification number

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_ESOCK : Invalid socket
  - ARM_SOCKET_EAGAIN : Operation would block (may be called again)
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketClose closes an existing socket and releases the socket descriptor. Further references to socket fail with `ARM_SOCKET_EINVAL` error code.

The argument socket specifies a socket identification number returned from a previous call to ARM_WIFI_SocketCreate.

In blocking mode, which is enabled by default, this function will wait until a socket is closed. In non blocking mode, you must call the ARM_WIFI_SocketClose function again if the error code `ARM_SOCKET_EAGAIN` is returned.

Example:

- see ARM_WIFI_SocketListen

## ◆ ARM_WIFI_SocketGetHostByName()

int32_t ARM_WIFI_SocketGetHostByName | ( | 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
  [in] | af | Address family
  [out] | ip | Pointer to buffer where resolved IP address shall be returned
  [in,out] | ip_len | Pointer to length of 'ip' length of supplied 'ip' on input length of stored 'ip' on output

Returns
  status information
  - 0 : Operation successful
  - ARM_SOCKET_EINVAL : Invalid argument
  - ARM_SOCKET_ENOTSUP : Operation not supported
  - ARM_SOCKET_ETIMEDOUT : Operation timed out
  - ARM_SOCKET_EHOSTNOTFOUND : Host not found
  - ARM_SOCKET_ERROR : Unspecified error

The function ARM_WIFI_SocketGetHostByName retrieves host information corresponding to a host name from a host database. It does this by sending DNS requests to the DNS server. The IP address of the DNS server is specified in the network interface configuration or can be obtained from the DHCP server for the local area network.

The argument name is a pointer to the null-terminated name of the host to resolve.

The argument af specifies the address family, that is, which type of IP address you want to resolve. The following values are supported:

Family | Description
--- | ---
ARM_SOCKET_AF_INET | Resolve the IPv4 address
ARM_SOCKET_AF_INET6 | Resolve the IPv6 address

The argument ip is a pointer to the buffer that will receive the resolved IP address of the host. If the argument ip is NULL, the function returns error.

The argument ip_len is a pointer to the IP address length. It should initially contain the amount of space pointed to by ip. On return it contains the actual length of the address returned in bytes.

Example:

extern ARM_DRIVER_WIFI Driver_WiFi0;
static ARM_DRIVER_WIFI *wifi;
void ping_arm_com (void) {
uint8_t ip[4];
uint32_t ip_len;
int32_t res;
wifi = &Driver_WiFi0;
ip_len = sizeof(ip);
res = wifi->SocketGetHostByName ("www.arm.com", ARM_SOCKET_AF_INET, (uint8_t *)ip, &ip_len);
if (res == ARM_DRIVER_OK) {
res = wifi->Ping ((uint8_t *)ip, sizeof(ip));
if (res == ARM_DRIVER_OK) {
// "www.arm.com" responded to ping
}
}
else {
// "www.arm.com" not resolved
}
}
ARM_DRIVER_OK
#define ARM_DRIVER_OK
Operation succeeded.
ARM_DRIVER_WIFI::Ping
int32_t(* Ping)(const uint8_t *ip, uint32_t ip_len)
Definition: Driver_WiFi.h:658
ARM_DRIVER_WIFI::SocketGetHostByName
int32_t(* SocketGetHostByName)(const char *name, int32_t af, uint8_t *ip, uint32_t *ip_len)
Definition: Driver_WiFi.h:657

## ◆ ARM_WIFI_Ping()

int32_t ARM_WIFI_Ping | ( | const uint8_t * | ip,
|  | uint32_t | ip_len
| ) |  |

Probe remote host with Ping command.

Parameters

  [in] | ip | Pointer to remote host IP address
  [in] | ip_len | Length of 'ip' address in bytes

Returns
  execution status
  - ARM_DRIVER_OK : Operation successful
  - ARM_DRIVER_ERROR : Operation failed
  - ARM_DRIVER_ERROR_TIMEOUT : Timeout occurred
  - ARM_DRIVER_ERROR_UNSUPPORTED : Operation not supported
  - ARM_DRIVER_ERROR_PARAMETER : Parameter error (NULL ip pointer or ip_len different than 4 or 16)

The function ARM_WIFI_Ping checks if the remote host is reachable. It does this by sending an echo request and waiting for an echo response. The function then returns the result of the operation. Check the ARM_WIFI_CAPABILITIES of the driver, if this function is supported in the driver implementation.

The argument ip is a pointer to the buffer containing the IP address octets of the host to ping.

The argument ip_len specifies the length of the IP address. The length is 4 bytes for the IPv4 address and 16 bytes for the IPv6 address.

Note
  The host availability check fails, if the remote host does not respond to echo requests, or intermediate routers do not forward the echo requests or echo responses.

Example:

extern ARM_DRIVER_WIFI Driver_WiFi0;
static ARM_DRIVER_WIFI *wifi;
void ping_host (void) {
uint8_t ip[4] = { 192U, 168U, 0U, 100U };
int32_t res;
wifi = &Driver_WiFi0;
res = wifi->Ping ((uint8_t *)ip, sizeof(ip));
if (res == ARM_DRIVER_OK) {
// Host responded
}
}