Mbed OS Reference
Loading...
Searching...
No Matches
ESP32Stack Class Referenceabstract

ESP32Stack class Implementation of the NetworkStack for the ESP32. More...

#include <ESP32Stack.h>

Inheritance diagram for ESP32Stack:
NetworkStack DNS ESP32Interface ESP32InterfaceAP

Public Types

typedef mbed::Callback< nsapi_error_t(int delay_ms, mbed::Callback< void()> user_cb)> call_in_callback_cb_t
 Type for a call-in callback.
 

Public Member Functions

virtual nsapi_error_t get_ip_address (SocketAddress *address)
 Get the local IP address.
 
virtual nsapi_error_t get_ipv6_link_local_address (SocketAddress *address)
 Get the IPv6 link local address.
 
virtual nsapi_error_t get_ip_address_if (SocketAddress *address, const char *interface_name)
 Get the local IP address on interface name.
 
virtual nsapi_error_t gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
 Translates a hostname to an IP address with specific version.
 
virtual nsapi_value_or_error_t getaddrinfo (const char *hostname, SocketAddress *hints, SocketAddress **res, const char *interface_name=NULL)
 Translate a hostname to the multiple IP addresses with specific version using network interface name.
 
virtual nsapi_value_or_error_t gethostbyname_async (const char *host, hostbyname_cb_t callback, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
 Translates a hostname to multiple IP addresses (asynchronous)
 
virtual nsapi_value_or_error_t getaddrinfo_async (const char *hostname, SocketAddress *hints, hostbyname_cb_t callback, const char *interface_name=NULL)
 Translates a hostname to the multiple IP addresses (asynchronous)
 
virtual nsapi_error_t gethostbyname_async_cancel (int id)
 Cancels asynchronous hostname translation.
 
virtual nsapi_error_t add_dns_server (const SocketAddress &address, const char *interface_name=NULL)
 Add a domain name server to list of servers to query.
 
virtual nsapi_error_t get_dns_server (int index, SocketAddress *address, const char *interface_name=NULL)
 Get a domain name server from a list of servers to query.
 
virtual OnboardNetworkStackonboardNetworkStack ()
 Dynamic downcast to a OnboardNetworkStack.
 

Protected Member Functions

 ESP32Stack (PinName en, PinName io0, PinName tx, PinName rx, bool debug, PinName rts, PinName cts, int baudrate, int is_ap)
 ESP32Stack lifetime.
 
int socket_open (void **handle, nsapi_protocol_t proto) override
 Opens a socket.
 
int socket_close (void *handle) override
 Close the socket.
 
int socket_bind (void *handle, const SocketAddress &address) override
 Bind a specific address to a socket.
 
int socket_listen (void *handle, int backlog) override
 Listen for connections on a TCP socket.
 
int socket_connect (void *handle, const SocketAddress &address) override
 Connects TCP socket to a remote host.
 
int socket_accept (void *handle, void **socket, SocketAddress *address) override
 Accepts a connection on a TCP socket.
 
void socket_attach (void *handle, void(*callback)(void *), void *data) override
 Register a callback on state change of the socket.
 
nsapi_error_t setsockopt (nsapi_socket_t handle, int level, int optname, const void *optval, unsigned optlen) override
 Set stack-specific socket options.
 
nsapi_error_t getsockopt (nsapi_socket_t handle, int level, int optname, void *optval, unsigned *optlen) override
 Get stack-specific socket options.
 
virtual nsapi_size_or_error_t socket_send (nsapi_socket_t handle, const void *data, nsapi_size_t size)=0
 Send data over a TCP socket.
 
virtual nsapi_size_or_error_t socket_recv (nsapi_socket_t handle, void *data, nsapi_size_t size)=0
 Receive data over a TCP socket.
 
virtual nsapi_size_or_error_t socket_sendto (nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size)=0
 Send a packet over a UDP socket.
 
virtual nsapi_size_or_error_t socket_recvfrom (nsapi_socket_t handle, SocketAddress *address, void *buffer, nsapi_size_t size)=0
 Receive a packet over a UDP socket.
 
virtual nsapi_size_or_error_t socket_sendto_control (nsapi_socket_t handle, const SocketAddress &address, const void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size)
 Send a packet with ancillary data over a UDP socket.
 
virtual nsapi_size_or_error_t socket_recvfrom_control (nsapi_socket_t handle, SocketAddress *address, void *data, nsapi_size_t size, nsapi_msghdr_t *control, nsapi_size_t control_size)
 Receive a packet with ancillary data over a UDP socket.
 

Detailed Description

ESP32Stack class Implementation of the NetworkStack for the ESP32.

Definition at line 31 of file ESP32Stack.h.

Member Typedef Documentation

◆ call_in_callback_cb_t

typedef mbed::Callback<nsapi_error_t (int delay_ms, mbed::Callback<void()> user_cb)> call_in_callback_cb_t
inherited

Type for a call-in callback.

This is a pointer to a function that will call the provided callback from the network stack after a given delay, or immediately if delay_ms is 0.

Definition at line 235 of file NetworkStack.h.

Constructor & Destructor Documentation

◆ ESP32Stack()

ESP32Stack ( PinName  en,
PinName  io0,
PinName  tx,
PinName  rx,
bool  debug,
PinName  rts,
PinName  cts,
int  baudrate,
int  is_ap 
)
protected

ESP32Stack lifetime.

Parameters
enEN pin
io0IO0 pin
txTX pin
rxRX pin
debugEnable debugging
rtsRTS pin
ctsCTS pin
baudrateThe baudrate of the serial port.
is_ap0:Terminal 1:Access point.

Member Function Documentation

◆ socket_open()

int socket_open ( void **  handle,
nsapi_protocol_t  proto 
)
overrideprotectedvirtual

Opens a socket.

Creates a network socket and stores it in the specified handle. The handle must be passed to following calls on the socket.

A stack may have a finite number of sockets, in this case NSAPI_ERROR_NO_SOCKET is returned if no socket is available.

Parameters
handleDestination for the handle to a newly created socket
protoProtocol of socket to open, NSAPI_TCP or NSAPI_UDP
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements NetworkStack.

◆ socket_close()

int socket_close ( void *  handle)
overrideprotectedvirtual

Close the socket.

Closes any open connection and deallocates any memory associated with the socket.

Parameters
handleSocket handle
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements NetworkStack.

◆ socket_bind()

int socket_bind ( void *  handle,
const SocketAddress address 
)
overrideprotectedvirtual

Bind a specific address to a socket.

Binding a socket specifies the address and port on which to receive data. If the IP address is zeroed, only the port is bound.

Parameters
handleSocket handle
addressLocal address to bind
Returns
NSAPI_ERROR_OK on success, negative error code on failure.

Implements NetworkStack.

◆ socket_listen()

int socket_listen ( void *  handle,
int  backlog 
)
overrideprotectedvirtual

Listen for connections on a TCP socket.

Marks the socket as a passive socket that can be used to accept incoming connections.

Parameters
handleSocket handle
backlogNumber of pending connections that can be queued simultaneously
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements NetworkStack.

◆ socket_connect()

int socket_connect ( void *  handle,
const SocketAddress address 
)
overrideprotectedvirtual

Connects TCP socket to a remote host.

Initiates a connection to a remote server specified by the indicated address.

Parameters
handleSocket handle
addressThe SocketAddress of the remote host
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements NetworkStack.

◆ socket_accept()

int socket_accept ( void *  server,
void **  handle,
SocketAddress address 
)
overrideprotectedvirtual

Accepts a connection on a TCP socket.

The server socket must be bound and set to listen for connections. On a new connection, creates a network socket and stores it in the specified handle. The handle must be passed to following calls on the socket.

A stack may have a finite number of sockets, in this case NSAPI_ERROR_NO_SOCKET is returned if no socket is available.

This call is non-blocking. If accept would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
serverSocket handle to server to accept from
handleDestination for a handle to the newly created socket
addressDestination for the remote address or NULL
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements NetworkStack.

◆ socket_attach()

void socket_attach ( void *  handle,
void(*)(void *)  callback,
void *  data 
)
overrideprotectedvirtual

Register a callback on state change of the socket.

The specified callback will be called on state changes such as when the socket can recv/send/accept successfully and on when an error occurs. The callback may also be called spuriously without reason.

The callback may be called in an interrupt context and should not perform expensive operations such as recv/send calls.

Parameters
handleSocket handle
callbackFunction to call on state change
dataArgument to pass to callback

Implements NetworkStack.

◆ setsockopt()

nsapi_error_t setsockopt ( nsapi_socket_t  handle,
int  level,
int  optname,
const void *  optval,
unsigned  optlen 
)
overrideprotectedvirtual

Set stack-specific socket options.

The setsockopt allow an application to pass stack-specific hints to the underlying stack. For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned and the socket is unmodified.

Parameters
handleSocket handle.
levelStack-specific protocol level.
optnameStack-specific option identifier.
optvalOption value.
optlenLength of the option value.
Returns
NSAPI_ERROR_OK on success, negative error code on failure.

Reimplemented from NetworkStack.

◆ getsockopt()

nsapi_error_t getsockopt ( nsapi_socket_t  handle,
int  level,
int  optname,
void *  optval,
unsigned *  optlen 
)
overrideprotectedvirtual

Get stack-specific socket options.

The getstackopt allow an application to retrieve stack-specific hints from the underlying stack. For unsupported options, NSAPI_ERROR_UNSUPPORTED is returned and optval is unmodified.

Parameters
handleSocket handle.
levelStack-specific protocol level.
optnameStack-specific option identifier.
optvalDestination for option value.
optlenLength of the option value.
Returns
NSAPI_ERROR_OK on success, negative error code on failure.

Reimplemented from NetworkStack.

◆ get_ip_address()

virtual nsapi_error_t get_ip_address ( SocketAddress address)
virtualinherited

Get the local IP address.

Parameters
addressSocketAddress representation of the local IP address
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this feature is not supported
NSAPI_ERROR_PARAMETERif the provided pointer is invalid
NSAPI_ERROR_NO_ADDRESSif the address cannot be obtained from stack

Reimplemented in AT_CellularStack, ESP32InterfaceAP, Nanostack, and ESP32Interface.

◆ get_ipv6_link_local_address()

virtual nsapi_error_t get_ipv6_link_local_address ( SocketAddress address)
virtualinherited

Get the IPv6 link local address.

Parameters
addressSocketAddress representation of the link local IPv6 address
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this feature is not supported
NSAPI_ERROR_PARAMETERif the provided pointer is invalid

◆ get_ip_address_if()

virtual nsapi_error_t get_ip_address_if ( SocketAddress address,
const char *  interface_name 
)
virtualinherited

Get the local IP address on interface name.

Parameters
addressSocketAddress representation of the link local IPv6 address
interface_nameNetwork interface_name
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this feature is not supported
NSAPI_ERROR_PARAMETERif the provided pointer is invalid
NSAPI_ERROR_NO_ADDRESSif the address cannot be obtained from stack

◆ gethostbyname()

virtual nsapi_error_t gethostbyname ( const char *  host,
SocketAddress address,
nsapi_version_t  version = NSAPI_UNSPEC,
const char *  interface_name = NULL 
)
virtualinherited

Translates a hostname to an IP address with specific version.

The hostname may be either a domain name or an IP address. If the hostname is an IP address, no network transactions will be performed.

If no stack-specific DNS resolution is provided, the hostname will be resolve using a UDP socket on the stack.

Parameters
hostHostname to resolve
addressPointer to a SocketAddress to store the result.
versionIP version of address to resolve, NSAPI_UNSPEC indicates version is chosen by the stack (defaults to NSAPI_UNSPEC)
interface_nameNetwork interface_name
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_PARAMETERif invalid (null) name is provided
NSAPI_ERROR_DNS_FAILUREif DNS resolution fails
intother negative errors, see nsapi_dns_query

Implements DNS.

Reimplemented in Nanostack, RM1000_AT_CellularStack, ESP32Interface, and ESP32InterfaceAP.

◆ getaddrinfo()

virtual nsapi_value_or_error_t getaddrinfo ( const char *  hostname,
SocketAddress hints,
SocketAddress **  res,
const char *  interface_name = NULL 
)
virtualinherited

Translate a hostname to the multiple IP addresses with specific version using network interface name.

The hostname may be either a domain name or an IP address. If the hostname is an IP address, no network transactions will be performed.

If no stack-specific DNS resolution is provided, the hostname will be resolve using a UDP socket on the stack.

Parameters
hostnameHostname to resolve.
hintsPointer to a SocketAddress with query parameters.
resPointer to a SocketAddress array to store the result..
interface_nameNetwork interface name
Returns
number of results on success, negative error code on failure.

Implements DNS.

◆ gethostbyname_async()

virtual nsapi_value_or_error_t gethostbyname_async ( const char *  host,
hostbyname_cb_t  callback,
nsapi_version_t  version = NSAPI_UNSPEC,
const char *  interface_name = NULL 
)
virtualinherited

Translates a hostname to multiple IP addresses (asynchronous)

The hostname may be either a domain name or an IP address. If the hostname is an IP address, no network transactions will be performed.

If no stack-specific DNS resolution is provided, the hostname will be resolve using a UDP socket on the stack.

Call is non-blocking. Result of the DNS operation is returned by the callback. If this function returns failure, callback will not be called. In case result is success (IP address was found from DNS cache), callback will be called before function returns.

Parameters
hostHostname to resolve
callbackCallback that is called for result
versionIP version of address to resolve, NSAPI_UNSPEC indicates version is chosen by the stack (defaults to NSAPI_UNSPEC)
interface_nameNetwork interface_name
Returns
0 on immediate success, negative error code on immediate failure or a positive unique id that represents the hostname translation operation and can be passed to cancel

Implements DNS.

Reimplemented in Nanostack.

◆ getaddrinfo_async()

virtual nsapi_value_or_error_t getaddrinfo_async ( const char *  hostname,
SocketAddress hints,
hostbyname_cb_t  callback,
const char *  interface_name = NULL 
)
virtualinherited

Translates a hostname to the multiple IP addresses (asynchronous)

The hostname may be either a domain name or an IP address. If the hostname is an IP address, no network transactions will be performed.

If no stack-specific DNS resolution is provided, the hostname will be resolve using a UDP socket on the stack.

The call is non-blocking. Result of the DNS operation is returned by the callback. If this function returns failure, callback will not be called. In case that IP addresses are found from DNS cache, callback will be called before function returns.

Parameters
hostnameHostname to resolve
hintsPointer to a SocketAddress with query parameters.
callbackCallback that is called for result
interface_nameNetwork interface_name
Returns
0 on immediate success, negative error code on immediate failure or a positive unique id that represents the hostname translation operation and can be passed to cancel

Implements DNS.

◆ gethostbyname_async_cancel()

virtual nsapi_error_t gethostbyname_async_cancel ( int  id)
virtualinherited

Cancels asynchronous hostname translation.

When translation is cancelled, callback will not be called.

Parameters
idUnique id of the hostname translation operation
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements DNS.

◆ add_dns_server()

virtual nsapi_error_t add_dns_server ( const SocketAddress address,
const char *  interface_name = NULL 
)
virtualinherited

Add a domain name server to list of servers to query.

Parameters
addressDestination for the host address
interface_nameNetwork interface name. Currently unused, the server is added for all interfaces.
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Implements DNS.

Reimplemented in ESP32Interface, and ESP32InterfaceAP.

◆ get_dns_server()

virtual nsapi_error_t get_dns_server ( int  index,
SocketAddress address,
const char *  interface_name = NULL 
)
virtualinherited

Get a domain name server from a list of servers to query.

Returns a DNS server address for a index. If returns error no more DNS servers to read.

Parameters
indexIndex of the DNS server, starts from zero
addressDestination for the host address
interface_nameNetwork interface name
Returns
NSAPI_ERROR_OK on success, negative error code on failure

Reimplemented in Nanostack.

◆ onboardNetworkStack()

virtual OnboardNetworkStack * onboardNetworkStack ( )
virtualinherited

Dynamic downcast to a OnboardNetworkStack.

Reimplemented in OnboardNetworkStack.

Definition at line 225 of file NetworkStack.h.

◆ socket_send()

virtual nsapi_size_or_error_t socket_send ( nsapi_socket_t  handle,
const void *  data,
nsapi_size_t  size 
)
protectedpure virtualinherited

Send data over a TCP socket.

The socket must be connected to a remote host. Returns the number of bytes sent from the buffer.

This call is non-blocking. If send would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
dataBuffer of data to send to the host
sizeSize of the buffer in bytes
Returns
Number of sent bytes on success, negative error code on failure

Implemented in AT_CellularStack, and Nanostack.

◆ socket_recv()

virtual nsapi_size_or_error_t socket_recv ( nsapi_socket_t  handle,
void *  data,
nsapi_size_t  size 
)
protectedpure virtualinherited

Receive data over a TCP socket.

The socket must be connected to a remote host. Returns the number of bytes received into the buffer.

This call is non-blocking. If recv would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
dataDestination buffer for data received from the host
sizeSize of the buffer in bytes
Returns
Number of received bytes on success, negative error code on failure

Implemented in AT_CellularStack, and Nanostack.

◆ socket_sendto()

virtual nsapi_size_or_error_t socket_sendto ( nsapi_socket_t  handle,
const SocketAddress address,
const void *  data,
nsapi_size_t  size 
)
protectedpure virtualinherited

Send a packet over a UDP socket.

Sends data to the specified address. Returns the number of bytes sent from the buffer.

This call is non-blocking. If sendto would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
addressThe SocketAddress of the remote host
dataBuffer of data to send to the host
sizeSize of the buffer in bytes
Returns
Number of sent bytes on success, negative error code on failure

Implemented in AT_CellularStack, and Nanostack.

◆ socket_recvfrom()

virtual nsapi_size_or_error_t socket_recvfrom ( nsapi_socket_t  handle,
SocketAddress address,
void *  buffer,
nsapi_size_t  size 
)
protectedpure virtualinherited

Receive a packet over a UDP socket.

Receives data and stores the source address in address if address is not NULL. Returns the number of bytes received into the buffer.

This call is non-blocking. If recvfrom would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
addressDestination for the source address or NULL
bufferDestination buffer for data received from the host
sizeSize of the buffer in bytes
Returns
Number of received bytes on success, negative error code on failure

Implemented in AT_CellularStack, and Nanostack.

◆ socket_sendto_control()

virtual nsapi_size_or_error_t socket_sendto_control ( nsapi_socket_t  handle,
const SocketAddress address,
const void *  data,
nsapi_size_t  size,
nsapi_msghdr_t control,
nsapi_size_t  control_size 
)
protectedvirtualinherited

Send a packet with ancillary data over a UDP socket.

Sends data to the specified address. Returns the number of bytes sent from the buffer.

This call is non-blocking. If sendto would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
addressThe SocketAddress of the remote host
dataBuffer of data to send to the host
sizeSize of the buffer in bytes
controlStorage for ancillary data
control_sizeSize of ancillary data
Returns
Number of sent bytes on success, negative error code on failure

Definition at line 408 of file NetworkStack.h.

◆ socket_recvfrom_control()

virtual nsapi_size_or_error_t socket_recvfrom_control ( nsapi_socket_t  handle,
SocketAddress address,
void *  data,
nsapi_size_t  size,
nsapi_msghdr_t control,
nsapi_size_t  control_size 
)
protectedvirtualinherited

Receive a packet with ancillary data over a UDP socket.

Receives data and stores the source address in address if address is not NULL. Returns the number of bytes received into the buffer.

Ancillary data is stored into control. The caller needs to allocate a buffer that is large enough to contain the data they want to receive, then pass the pointer in through the control member. The data will be filled into control, beginning with a header specifying what data was received. See MsgHeaderIterator for how to parse this data.

This call is non-blocking. If recvfrom would block, NSAPI_ERROR_WOULD_BLOCK is returned immediately.

Parameters
handleSocket handle
addressDestination for the source address or NULL
dataDestination buffer for data received from the host
sizeSize of the buffer in bytes
controlStorage for ancillary data
control_sizeSize of ancillary data
Returns
Number of received bytes on success, negative error code on failure

Definition at line 441 of file NetworkStack.h.