2016-05-27 04:54:05 +00:00
|
|
|
/* NetworkStack
|
|
|
|
* Copyright (c) 2015 ARM Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef NETWORK_INTERFACE_H
|
|
|
|
#define NETWORK_INTERFACE_H
|
|
|
|
|
2016-09-30 23:32:06 +00:00
|
|
|
#include "netsocket/nsapi_types.h"
|
|
|
|
#include "netsocket/SocketAddress.h"
|
2017-11-06 12:18:03 +00:00
|
|
|
#include "Callback.h"
|
2018-05-03 13:13:10 +00:00
|
|
|
#include "DNS.h"
|
|
|
|
|
2016-09-08 18:44:11 +00:00
|
|
|
|
2017-12-19 14:05:35 +00:00
|
|
|
// Predeclared classes
|
2016-07-20 20:20:03 +00:00
|
|
|
class NetworkStack;
|
2017-12-19 14:05:35 +00:00
|
|
|
class EthInterface;
|
|
|
|
class WiFiInterface;
|
|
|
|
class MeshInterface;
|
|
|
|
class CellularBase;
|
|
|
|
class EMACInterface;
|
2016-05-27 04:54:05 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Common interface that is shared between network devices.
|
2016-07-20 02:54:51 +00:00
|
|
|
*
|
2017-06-01 21:43:43 +00:00
|
|
|
* @addtogroup netsocket
|
2016-07-20 02:54:51 +00:00
|
|
|
*/
|
2018-05-03 13:13:10 +00:00
|
|
|
class NetworkInterface: public DNS {
|
2016-05-27 04:54:05 +00:00
|
|
|
public:
|
2017-11-06 12:18:03 +00:00
|
|
|
|
2016-05-27 04:54:05 +00:00
|
|
|
virtual ~NetworkInterface() {};
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return the default network interface.
|
2018-02-15 12:46:11 +00:00
|
|
|
*
|
|
|
|
* Returns the default network interface, as determined by JSON option
|
|
|
|
* target.network-default-interface-type or other overrides.
|
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* The type of the interface returned can be tested by calling ethInterface(),
|
|
|
|
* wifiInterface(), meshInterface(), cellularBase(), emacInterface() and checking
|
|
|
|
* for NULL pointers.
|
2018-02-15 12:46:11 +00:00
|
|
|
*
|
|
|
|
* The default behaviour is to return the default interface for the
|
|
|
|
* interface type specified by target.network-default-interface-type. Targets
|
|
|
|
* should set this in their targets.json to guide default selection,
|
|
|
|
* and applications may override.
|
|
|
|
*
|
|
|
|
* The interface returned should be already configured for use such that its
|
|
|
|
* connect() method works with no parameters. For connection types needing
|
|
|
|
* configuration, settings should normally be obtained from JSON - the
|
|
|
|
* settings for the core types are under the "nsapi" JSON config tree.
|
|
|
|
*
|
|
|
|
* The list of possible settings for default interface type is open-ended,
|
|
|
|
* as is the number of possible providers. Core providers are:
|
|
|
|
*
|
|
|
|
* * ETHERNET: EthernetInterface, using default EMAC and OnboardNetworkStack
|
|
|
|
* * MESH: ThreadInterface or LoWPANNDInterface, using default NanostackRfPhy
|
|
|
|
* * CELLULAR: OnboardModemInterface
|
|
|
|
* * WIFI: None - always provided by a specific class
|
|
|
|
*
|
|
|
|
* Specific drivers may be activated by other settings of the
|
|
|
|
* default-network-interface-type configuration. This will depend on the
|
|
|
|
* target and the driver. For example a board may have its default setting
|
|
|
|
* as "AUTO" which causes it to autodetect an Ethernet cable. This should
|
|
|
|
* be described in the target's documentation.
|
|
|
|
*
|
|
|
|
* An application can override all target settings by implementing
|
|
|
|
* NetworkInterface::get_default_instance() themselves - the default
|
|
|
|
* definition is weak, and calls get_target_default_instance().
|
|
|
|
*/
|
|
|
|
static NetworkInterface *get_default_instance();
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Get the local MAC address.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
|
|
|
* Provided MAC address is intended for info or debug purposes and
|
2018-10-24 19:24:33 +00:00
|
|
|
* may be not provided if the underlying network interface does not
|
|
|
|
* provide a MAC address.
|
2018-05-17 20:36:15 +00:00
|
|
|
*
|
2016-09-08 18:44:11 +00:00
|
|
|
* @return Null-terminated representation of the local MAC address
|
2018-10-24 19:24:33 +00:00
|
|
|
* or null if no MAC address is available.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
|
|
|
virtual const char *get_mac_address();
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Get the local IP address.
|
2016-07-20 02:54:51 +00:00
|
|
|
*
|
|
|
|
* @return Null-terminated representation of the local IP address
|
2018-10-24 19:24:33 +00:00
|
|
|
* or null if no IP address has been received.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
|
|
|
virtual const char *get_ip_address();
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Get the local network mask.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
2018-05-17 20:36:15 +00:00
|
|
|
* @return Null-terminated representation of the local network mask
|
2018-10-24 19:24:33 +00:00
|
|
|
* or null if no network mask has been received.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
|
|
|
virtual const char *get_netmask();
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Get the local gateway.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
|
|
|
* @return Null-terminated representation of the local gateway
|
2018-10-24 19:24:33 +00:00
|
|
|
* or null if no network mask has been received.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
|
|
|
virtual const char *get_gateway();
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Configure this network interface to use a static IP address.
|
2016-09-08 18:44:11 +00:00
|
|
|
* Implicitly disables DHCP, which can be enabled in set_dhcp.
|
|
|
|
* Requires that the network is disconnected.
|
|
|
|
*
|
2017-06-01 21:43:43 +00:00
|
|
|
* @param ip_address Null-terminated representation of the local IP address
|
|
|
|
* @param netmask Null-terminated representation of the local network mask
|
|
|
|
* @param gateway Null-terminated representation of the local gateway
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
|
2016-09-08 18:44:11 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Enable or disable DHCP on connecting the network.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* Enabled by default unless a static IP address has been assigned. Requires
|
|
|
|
* that the network is disconnected.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param dhcp True to enable DHCP.
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
2016-10-18 19:48:46 +00:00
|
|
|
virtual nsapi_error_t set_dhcp(bool dhcp);
|
2016-09-08 18:44:11 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Start the interface.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2016-09-08 18:44:11 +00:00
|
|
|
*/
|
2016-10-18 19:48:46 +00:00
|
|
|
virtual nsapi_error_t connect() = 0;
|
2016-09-08 18:44:11 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Stop the interface.
|
2016-09-08 18:44:11 +00:00
|
|
|
*
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2016-07-20 02:54:51 +00:00
|
|
|
*/
|
2016-10-18 19:48:46 +00:00
|
|
|
virtual nsapi_error_t disconnect() = 0;
|
2016-07-20 02:54:51 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Translate a hostname to an IP address with specific version.
|
2016-09-08 14:28:41 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param host Hostname to resolve.
|
2018-10-24 19:35:53 +00:00
|
|
|
* @param address Pointer to a SocketAddress to store the result.
|
2016-10-03 21:57:27 +00:00
|
|
|
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
|
2018-10-24 19:24:33 +00:00
|
|
|
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2016-09-08 14:28:41 +00:00
|
|
|
*/
|
2016-10-18 19:48:46 +00:00
|
|
|
virtual nsapi_error_t gethostbyname(const char *host,
|
2018-08-03 12:32:29 +00:00
|
|
|
SocketAddress *address, nsapi_version_t version = NSAPI_UNSPEC);
|
2016-09-08 14:28:41 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Hostname translation callback (for use with gethostbyname_async()).
|
2018-04-20 12:29:46 +00:00
|
|
|
*
|
2018-05-03 13:13:10 +00:00
|
|
|
* Callback will be called after DNS resolution completes or a failure occurs.
|
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @note Callback should not take more than 10ms to execute, otherwise it might
|
2018-05-03 13:13:10 +00:00
|
|
|
* 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.
|
2018-04-20 12:29:46 +00:00
|
|
|
*
|
2018-10-24 19:35:53 +00:00
|
|
|
* @param result NSAPI_ERROR_OK on success, negative error code on failure.
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param address On success, destination for the host SocketAddress.
|
2018-04-20 12:29:46 +00:00
|
|
|
*/
|
2018-05-03 13:13:10 +00:00
|
|
|
typedef mbed::Callback<void (nsapi_error_t result, SocketAddress *address)> hostbyname_cb_t;
|
2018-04-20 12:29:46 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Translate a hostname to an IP address (asynchronous).
|
2018-04-20 12:29:46 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* The hostname may be either a domain name or a dotted IP address. If the
|
2018-04-20 12:29:46 +00:00
|
|
|
* 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.
|
2018-05-03 13:13:10 +00:00
|
|
|
* 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.
|
2018-04-20 12:29:46 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param host Hostname to resolve.
|
|
|
|
* @param callback Callback that is called for result.
|
2018-04-20 12:29:46 +00:00
|
|
|
* @param version IP version of address to resolve, NSAPI_UNSPEC indicates
|
2018-10-24 19:24:33 +00:00
|
|
|
* version is chosen by the stack (defaults to NSAPI_UNSPEC).
|
2018-05-09 13:07:53 +00:00
|
|
|
* @return 0 on immediate success,
|
|
|
|
* negative error code on immediate failure or
|
|
|
|
* a positive unique id that represents the hostname translation operation
|
2018-10-24 19:24:33 +00:00
|
|
|
* and can be passed to cancel.
|
2018-04-20 12:29:46 +00:00
|
|
|
*/
|
2018-05-09 13:07:53 +00:00
|
|
|
virtual nsapi_value_or_error_t gethostbyname_async(const char *host, hostbyname_cb_t callback,
|
2018-08-03 12:32:29 +00:00
|
|
|
nsapi_version_t version = NSAPI_UNSPEC);
|
2018-04-20 12:29:46 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Cancel asynchronous hostname translation.
|
2018-05-03 13:13:10 +00:00
|
|
|
*
|
|
|
|
* When translation is cancelled, callback will not be called.
|
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param id Unique id of the hostname translation operation (returned
|
|
|
|
* by gethostbyname_async)
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2018-05-03 13:13:10 +00:00
|
|
|
*/
|
2018-05-09 13:07:53 +00:00
|
|
|
virtual nsapi_error_t gethostbyname_async_cancel(int id);
|
2018-05-03 13:13:10 +00:00
|
|
|
|
2016-09-08 14:28:41 +00:00
|
|
|
/** Add a domain name server to list of servers to query
|
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param address Address for the dns host.
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2016-09-08 14:28:41 +00:00
|
|
|
*/
|
2016-10-18 19:48:46 +00:00
|
|
|
virtual nsapi_error_t add_dns_server(const SocketAddress &address);
|
2016-09-08 14:28:41 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Register callback for status reporting.
|
2018-02-12 09:02:07 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2017-11-06 12:18:03 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param status_cb The callback for status changes.
|
2017-11-06 12:18:03 +00:00
|
|
|
*/
|
|
|
|
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Get the connection status.
|
2017-11-06 12:18:03 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @return The connection status (@see nsapi_types.h).
|
2017-11-06 12:18:03 +00:00
|
|
|
*/
|
|
|
|
virtual nsapi_connection_status_t get_connection_status() const;
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Set blocking status of connect() which by default should be blocking.
|
2017-11-06 12:18:03 +00:00
|
|
|
*
|
2018-10-24 19:24:33 +00:00
|
|
|
* @param blocking Use true to make connect() blocking.
|
2018-10-24 19:35:53 +00:00
|
|
|
* @return NSAPI_ERROR_OK on success, negative error code on failure.
|
2017-11-06 12:18:03 +00:00
|
|
|
*/
|
|
|
|
virtual nsapi_error_t set_blocking(bool blocking);
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return pointer to an EthInterface.
|
|
|
|
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
|
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual EthInterface *ethInterface()
|
|
|
|
{
|
2017-12-19 14:05:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return pointer to a WiFiInterface.
|
|
|
|
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
|
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual WiFiInterface *wifiInterface()
|
|
|
|
{
|
2017-12-19 14:05:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return pointer to a MeshInterface.
|
|
|
|
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
|
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual MeshInterface *meshInterface()
|
|
|
|
{
|
2017-12-19 14:05:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return pointer to a CellularBase.
|
|
|
|
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
|
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual CellularBase *cellularBase()
|
|
|
|
{
|
2017-12-19 14:05:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
/** Return pointer to an EMACInterface.
|
|
|
|
* @return Pointer to requested interface type or NULL if this class doesn't implement the interface.
|
|
|
|
*/
|
2018-08-03 12:32:29 +00:00
|
|
|
virtual EMACInterface *emacInterface()
|
|
|
|
{
|
2017-12-19 14:05:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-11-06 12:18:03 +00:00
|
|
|
|
2018-10-24 19:24:33 +00:00
|
|
|
#if !defined(DOXYGEN_ONLY)
|
|
|
|
|
2016-05-27 04:54:05 +00:00
|
|
|
protected:
|
2018-05-17 20:36:15 +00:00
|
|
|
friend class InternetSocket;
|
2016-05-27 04:54:05 +00:00
|
|
|
friend class UDPSocket;
|
|
|
|
friend class TCPSocket;
|
|
|
|
friend class TCPServer;
|
|
|
|
friend class SocketAddress;
|
2016-07-20 20:20:03 +00:00
|
|
|
template <typename IF>
|
|
|
|
friend NetworkStack *nsapi_create_stack(IF *iface);
|
2016-07-20 02:54:51 +00:00
|
|
|
|
|
|
|
/** Provide access to the NetworkStack object
|
|
|
|
*
|
|
|
|
* @return The underlying NetworkStack object
|
|
|
|
*/
|
|
|
|
virtual NetworkStack *get_stack() = 0;
|
2018-02-15 12:46:11 +00:00
|
|
|
|
|
|
|
/** Get the target's default network instance.
|
|
|
|
*
|
|
|
|
* This method can be overridden by the target. Default implementations
|
|
|
|
* are provided weakly by various subsystems as described in
|
|
|
|
* NetworkInterface::get_default_instance(), so targets should not
|
|
|
|
* need to override in simple cases.
|
|
|
|
*
|
|
|
|
* If a target has more elaborate interface selection, it can completely
|
|
|
|
* override this behaviour by implementing
|
|
|
|
* NetworkInterface::get_target_default_instance() themselves, either
|
|
|
|
* unconditionally, or for a specific network-default-interface-type setting
|
|
|
|
*
|
|
|
|
* For example, a device with both Ethernet and Wi-fi could be set up its
|
|
|
|
* target so that:
|
|
|
|
* * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(),
|
|
|
|
* which means EthernetInterface provides EthInterface::get_target_instance()
|
|
|
|
* based on that EMAC.
|
|
|
|
* * It provides WifiInterface::get_target_default_instance().
|
|
|
|
* * The core will route NetworkInterface::get_default_instance() to
|
|
|
|
* either of those if network-default-interface-type is set to
|
|
|
|
* ETHERNET or WIFI.
|
|
|
|
* * The board overrides NetworkInterface::get_target_default_instance()
|
|
|
|
* if network-default-interface-type is set to AUTO. This returns
|
|
|
|
* either EthInterface::get_default_instance() or WiFIInterface::get_default_instance()
|
|
|
|
* depending on a cable detection.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* performs the search described by get_default_instance.
|
|
|
|
*/
|
|
|
|
static NetworkInterface *get_target_default_instance();
|
2018-10-24 19:24:33 +00:00
|
|
|
#endif //!defined(DOXYGEN_ONLY)
|
2016-05-27 04:54:05 +00:00
|
|
|
};
|
|
|
|
|
2016-07-19 22:12:22 +00:00
|
|
|
|
2016-05-27 04:54:05 +00:00
|
|
|
#endif
|