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

Common interface between Wi-Fi devices. More...

#include <WiFiInterface.h>

Inheritance diagram for WiFiInterface:
NetworkInterface DNS

Public Types

typedef mbed::Callback< void(nsapi_value_or_error_t result, SocketAddress *address)> hostbyname_cb_t
 Hostname translation callback (for use with gethostbyname_async()). More...
 

Public Member Functions

virtual nsapi_error_t set_credentials (const char *ssid, const char *pass, nsapi_security_t security=NSAPI_SECURITY_NONE)=0
 Set the Wi-Fi network credentials. More...
 
virtual nsapi_error_t set_channel (uint8_t channel)=0
 Set the Wi-Fi network channel. More...
 
virtual int8_t get_rssi ()=0
 Get the current radio signal strength for active connection. More...
 
virtual nsapi_error_t connect (const char *ssid, const char *pass, nsapi_security_t security=NSAPI_SECURITY_NONE, uint8_t channel=0)=0
 Attempt to connect to a Wi-Fi network. More...
 
nsapi_error_t connect () override=0
 Attempt to connect to a Wi-Fi network. More...
 
nsapi_error_t disconnect () override=0
 Stop the interface. More...
 
virtual nsapi_size_or_error_t scan (WiFiAccessPoint *res, nsapi_size_t count)=0
 Scan for available networks. More...
 
WiFiInterfacewifiInterface () final
 Return pointer to a WiFiInterface. More...
 
void set_default_parameters () override
 defined(DOXYGEN_ONLY) More...
 
virtual void set_as_default ()
 Set network interface as default one. More...
 
virtual const char * get_hostname ()
 Get hostname. More...
 
virtual nsapi_error_t set_hostname (const char *hostname)
 Set hostname. More...
 
virtual const char * get_mac_address ()
 Get the local MAC address. More...
 
virtual nsapi_error_t set_mac_address (uint8_t *mac_addr, nsapi_size_t addr_len)
 Set the MAC address to the interface. More...
 
virtual nsapi_error_t get_ip_address (SocketAddress *address)
 Get the local IP address. More...
 
virtual nsapi_error_t get_ipv6_link_local_address (SocketAddress *address)
 Get the IPv6 link local address. More...
 
virtual nsapi_error_t get_netmask (SocketAddress *address)
 Get the local network mask. More...
 
virtual nsapi_error_t get_gateway (SocketAddress *address)
 Get the local gateway. More...
 
virtual char * get_interface_name (char *interface_name)
 Get the network interface name. More...
 
virtual nsapi_error_t set_network (const SocketAddress &ip_address, const SocketAddress &netmask, const SocketAddress &gateway)
 Configure this network interface to use a static IP address. More...
 
virtual nsapi_error_t set_dhcp (bool dhcp)
 Enable or disable DHCP on connecting the network. More...
 
virtual nsapi_error_t gethostbyname (const char *host, SocketAddress *address, nsapi_version_t version=NSAPI_UNSPEC, const char *interface_name=NULL)
 Translate a hostname to an IP address with specific version using network interface name. More...
 
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. More...
 
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)
 Translate a hostname to an IP address (asynchronous) using network interface name. More...
 
virtual nsapi_value_or_error_t getaddrinfo_async (const char *hostname, SocketAddress *hints, hostbyname_cb_t callback, const char *interface_name=NULL)
 Translate a hostname to the multiple IP addresses (asynchronous) using network interface name. More...
 
virtual nsapi_error_t gethostbyname_async_cancel (int id)
 Cancel asynchronous hostname translation. More...
 
virtual nsapi_error_t add_dns_server (const SocketAddress &address, const char *interface_name)
 Add a domain name server to list of servers to query. More...
 
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. More...
 
virtual void attach (mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
 Register callback for status reporting. More...
 
void add_event_listener (mbed::Callback< void(nsapi_event_t, intptr_t)> status_cb)
 Add event listener for interface. More...
 
virtual nsapi_connection_status_t get_connection_status () const
 Get the connection status. More...
 
virtual nsapi_error_t set_blocking (bool blocking)
 Set asynchronous operation of connect() and disconnect() calls. More...
 
virtual EthInterfaceethInterface ()
 Return pointer to an EthInterface. More...
 
virtual MeshInterfacemeshInterface ()
 Return pointer to a MeshInterface. More...
 
virtual EMACInterfaceemacInterface ()
 Return pointer to an EMACInterface. More...
 
virtual CellularInterfacecellularInterface ()
 Return pointer to a CellularInterface. More...
 

Static Public Member Functions

static WiFiInterfaceget_default_instance ()
 Get the default Wi-Fi interface. More...
 

Detailed Description

Common interface between Wi-Fi devices.

Definition at line 32 of file WiFiInterface.h.

Member Typedef Documentation

◆ hostbyname_cb_t

typedef mbed::Callback<void (nsapi_value_or_error_t result, SocketAddress *address)> hostbyname_cb_t
inherited

Hostname translation callback (for use with gethostbyname_async()).

Callback will be called after DNS resolution completes or a failure occurs.

Note
Callback should not take more than 10ms to execute, otherwise it might prevent underlying thread processing. A portable user of the callback should not make calls to network operations due to stack size limitations. The callback should not perform expensive operations such as socket recv/send calls or blocking operations.


result : Negative error code on failure, or value that represents the number of DNS records
address : On success, destination for the host SocketAddress.

Definition at line 303 of file NetworkInterface.h.

Member Function Documentation

◆ get_default_instance()

static WiFiInterface * get_default_instance ( )
static

Get the default Wi-Fi interface.

This is provided as a weak method so applications can override it. Default behavior is to get the target's default interface, if any.

Returns
pointer to interface, if any.

◆ set_credentials()

virtual nsapi_error_t set_credentials ( const char *  ssid,
const char *  pass,
nsapi_security_t  security = NSAPI_SECURITY_NONE 
)
pure virtual

Set the Wi-Fi network credentials.

Parameters
ssidName of the network to connect to.
passSecurity passphrase to connect to the network.
securityType of encryption for connection (defaults to NSAPI_SECURITY_NONE).
Returns
NSAPI_ERROR_OK on success, or error code on failure.

◆ set_channel()

virtual nsapi_error_t set_channel ( uint8_t  channel)
pure virtual

Set the Wi-Fi network channel.

Parameters
channelChannel to make the connection, or 0 for any (Default: 0).
Returns
NSAPI_ERROR_OK on success, or error code on failure.

◆ get_rssi()

virtual int8_t get_rssi ( )
pure virtual

Get the current radio signal strength for active connection.

Returns
Connection strength in dBm (negative value), or 0 if measurement impossible.

◆ connect() [1/2]

virtual nsapi_error_t connect ( const char *  ssid,
const char *  pass,
nsapi_security_t  security = NSAPI_SECURITY_NONE,
uint8_t  channel = 0 
)
pure virtual

Attempt to connect to a Wi-Fi network.

Parameters
ssidName of the network to connect to.
passSecurity passphrase to connect to the network.
securityType of encryption for connection (Default: NSAPI_SECURITY_NONE).
channelChannel to make the connection, or 0 for any (Default: 0).
Returns
NSAPI_ERROR_OK on success, or error code on failure.

◆ connect() [2/2]

nsapi_error_t connect ( )
overridepure virtual

Attempt to connect to a Wi-Fi network.

Requires ssid and passphrase to be set. If passphrase is invalid, NSAPI_ERROR_AUTH_ERROR is returned.

Returns
NSAPI_ERROR_OK on success, negative error code on failure.

Implements NetworkInterface.

◆ disconnect()

nsapi_error_t disconnect ( )
overridepure virtual

Stop the interface.

Returns
NSAPI_ERROR_OK on success, or error code on failure.

Implements NetworkInterface.

◆ scan()

virtual nsapi_size_or_error_t scan ( WiFiAccessPoint res,
nsapi_size_t  count 
)
pure virtual

Scan for available networks.

Note
This is a blocking function.

If the count is 0, the function only returns the number of available networks. If the count is greater than 0 and the res is not NULL, the array of discovered APs is populated with discovered networks up to the value of the count.

Parameters
resPointer to allocated array to store discovered APs.
countSize of allocated res array, or 0 to only count available APs.
Returns
Number of entries in res, or if count was 0, number of available networks. Negative on error (
See also
nsapi_types.h for nsapi_error).

◆ wifiInterface()

WiFiInterface * wifiInterface ( )
finalvirtual

Return pointer to a WiFiInterface.

Returns
Pointer to requested interface type or NULL if this class doesn't implement the interface.

Reimplemented from NetworkInterface.

Definition at line 110 of file WiFiInterface.h.

◆ set_default_parameters()

void set_default_parameters ( )
overridevirtual

defined(DOXYGEN_ONLY)

Set default parameters on a Wi-Fi interface.

A Wi-Fi interface instantiated directly or using WiFiInterface::get_default_instance() is initially unconfigured. This call can be used to set the default parameters that would have been set if the interface had been requested using NetworkInterface::get_default_instance() (see nsapi JSON configuration).

Reimplemented from NetworkInterface.

◆ set_as_default()

virtual void set_as_default ( )
virtualinherited

Set network interface as default one.

Reimplemented in EMACInterface, L3IPInterface, and PPPInterface.

◆ get_hostname()

virtual const char * get_hostname ( )
virtualinherited

Get hostname.

Returns
Hostname if configured, null otherwise

Reimplemented in EMACInterface.

◆ set_hostname()

virtual nsapi_error_t set_hostname ( const char *  hostname)
virtualinherited

Set hostname.

Parameters
hostnameHostname string
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this feature is not supported
NSAPI_ERROR_PARAMETERif hostname is not valid
NSAPI_ERROR_BUSYif hostname couldn't be set (e.g. for LwIP stack, hostname can only be set before calling EthernetInterface::connect method)

Reimplemented in EMACInterface.

◆ get_mac_address()

virtual const char * get_mac_address ( )
virtualinherited

Get the local MAC address.

Provided MAC address is intended for info or debug purposes and may be not provided if the underlying network interface does not provide a MAC address.

Returns
Null-terminated representation of the local MAC address or null if no MAC address is available.

Reimplemented in InterfaceNanostack, and EMACInterface.

◆ set_mac_address()

virtual nsapi_error_t set_mac_address ( uint8_t *  mac_addr,
nsapi_size_t  addr_len 
)
virtualinherited

Set the MAC address to the interface.

Set the provided MAC address on the network interface. The address must be unique globally. The address must be set before calling the interface connect() method.

Not all interfaces are supporting MAC address set and an error is not returned for this method call. Verify the changed MAC address by checking packet captures from the used network interface.

6-byte EUI-48 MAC addresses are used for Ethernet while Mesh interface is using 8-byte EUI-64 address.

More information about obtaining MAC address can be found from: https://standards.ieee.org/products-services/regauth/index.html

Parameters
mac_addrBuffer containing the MAC address in hexadecimal format.
addr_lenLength of provided buffer in bytes (6 or 8)
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this feature is not supported
NSAPI_ERROR_PARAMETERif address is not valid
NSAPI_ERROR_BUSYif address can't be set.

Reimplemented in InterfaceNanostack, and EMACInterface.

◆ 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_CellularContext, InterfaceNanostack, EMACInterface, L3IPInterface, PPPInterface, CellularInterface, and CellularContext.

◆ 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

Reimplemented in EMACInterface.

◆ get_netmask()

virtual nsapi_error_t get_netmask ( SocketAddress address)
virtualinherited

Get the local network mask.

Parameters
addressSocketAddress representation of netmask
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 EMACInterface, L3IPInterface, and PPPInterface.

◆ get_gateway()

virtual nsapi_error_t get_gateway ( SocketAddress address)
virtualinherited

Get the local gateway.

Parameters
addressSocketAddress representation of gateway 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 EMACInterface, L3IPInterface, and PPPInterface.

◆ get_interface_name()

virtual char * get_interface_name ( char *  interface_name)
virtualinherited

Get the network interface name.

Returns
Null-terminated representation of the network interface name or null if interface not exists

Reimplemented in AT_CellularContext, EMACInterface, L3IPInterface, and PPPInterface.

◆ set_network()

virtual nsapi_error_t set_network ( const SocketAddress ip_address,
const SocketAddress netmask,
const SocketAddress gateway 
)
virtualinherited

Configure this network interface to use a static IP address.

Implicitly disables DHCP, which can be enabled in set_dhcp. Requires that the network is disconnected.

Parameters
ip_addressSocketAddress object containing the local IP address
netmaskSocketAddress object containing the local network mask
gatewaySocketAddress object containing the local gateway
Return values
NSAPI_ERROR_OKon success
NSAPI_ERROR_UNSUPPORTEDif this function is unsupported

Reimplemented in EMACInterface, L3IPInterface, and PPPInterface.

◆ set_dhcp()

virtual nsapi_error_t set_dhcp ( bool  dhcp)
virtualinherited

Enable or disable DHCP on connecting the network.

Enabled by default unless a static IP address has been assigned. Requires that the network is disconnected.

Parameters
dhcpTrue to enable DHCP.
Return values
NSAPI_ERROR_OKon success.
NSAPI_ERROR_UNSUPPORTEDif operation is not supported.

Reimplemented in EMACInterface, and L3IPInterface.

◆ gethostbyname()

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

Translate a hostname to an IP address 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
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
intNegative error code on failure. See NetworkStack::gethostbyname

Implements DNS.

◆ 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

Translate a hostname to an IP address (asynchronous) using network interface name.

The hostname may be either a domain name or a dotted 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.

◆ 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

Translate a hostname to the multiple IP addresses (asynchronous) using network interface name.

The hostname may be either a domain name or a dotted 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
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

Cancel asynchronous hostname translation.

When translation is cancelled, callback will not be called.

Parameters
idUnique id of the hostname translation operation (returned by gethostbyname_async)
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 
)
virtualinherited

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

Parameters
addressAddress for the dns host.
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.

◆ 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

◆ attach()

virtual void attach ( mbed::Callback< void(nsapi_event_t, intptr_t)>  status_cb)
virtualinherited

Register callback for status reporting.

The specified status callback function will be called on status changes on the network. The parameters on the callback are the event type and event-type dependent reason parameter. Only one callback can be registered at a time.

To unregister a callback call with status_cb parameter as a zero.

NOTE: Any callbacks registered with this function will be overwritten if add_event_listener() API is used.

Parameters
status_cbThe callback for status changes.

Reimplemented in AT_CellularContext, InterfaceNanostack, EMACInterface, L3IPInterface, PPPInterface, and CellularContext.

◆ add_event_listener()

void add_event_listener ( mbed::Callback< void(nsapi_event_t, intptr_t)>  status_cb)
inherited

Add event listener for interface.

This API allows multiple callback to be registered for a single interface. When first called, internal list of event handlers are created and registered to interface through attach() API.

Application may only use attach() or add_event_listener() interface. Mixing usage of both leads to undefined behavior.

Warning
This version of the function does not use the std::nothrow feature. Subsequently, the function may fail to allocate memory and cause a system error. To use the new version with the changes, set "nsapi.add-event-listener-return-change": 1 in the target overrides section in your mbed_app.json file.
Parameters
status_cbThe callback for status changes.

◆ get_connection_status()

virtual nsapi_connection_status_t get_connection_status ( ) const
virtualinherited

Get the connection status.

Returns
The connection status (
See also
nsapi_types.h).

Reimplemented in AT_CellularContext, InterfaceNanostack, EMACInterface, L3IPInterface, and PPPInterface.

◆ set_blocking()

virtual nsapi_error_t set_blocking ( bool  blocking)
virtualinherited

Set asynchronous operation of connect() and disconnect() calls.

By default, interfaces are in synchronous mode which means that connect() or disconnect() blocks until it reach the target state or requested operation fails.

Parameters
blockingUse false to set NetworkInterface in asynchronous mode.
Returns
NSAPI_ERROR_OK on success
NSAPI_ERROR_UNSUPPORTED if driver does not support asynchronous mode.
negative error code on failure.

Reimplemented in AT_CellularContext, InterfaceNanostack, EMACInterface, L3IPInterface, PPPInterface, and CellularContext.

◆ ethInterface()

virtual EthInterface * ethInterface ( )
virtualinherited

Return pointer to an EthInterface.

Returns
Pointer to requested interface type or NULL if this class doesn't implement the interface.

Reimplemented in EthInterface.

Definition at line 463 of file NetworkInterface.h.

◆ meshInterface()

virtual MeshInterface * meshInterface ( )
virtualinherited

Return pointer to a MeshInterface.

Returns
Pointer to requested interface type or NULL if this class doesn't implement the interface.

Reimplemented in MeshInterface.

Definition at line 479 of file NetworkInterface.h.

◆ emacInterface()

virtual EMACInterface * emacInterface ( )
virtualinherited

Return pointer to an EMACInterface.

Returns
Pointer to requested interface type or NULL if this class doesn't implement the interface.

Reimplemented in EMACInterface.

Definition at line 487 of file NetworkInterface.h.

◆ cellularInterface()

virtual CellularInterface * cellularInterface ( )
virtualinherited

Return pointer to a CellularInterface.

Returns
Pointer to requested interface type or NULL if this class doesn't implement the interface.

Reimplemented in CellularInterface.

Definition at line 495 of file NetworkInterface.h.