Insert EMACInterface class

Rather than let "EthernetInterface" be the base EMAC NetworkInterface,
insert an "EMACInterface" class.

EthernetInterface then derives from EMACInterface and EthInterface.

A Wi-Fi driver can derive from EMACInterface and WiFiInterface - this
will be more logical than deriving from EthernetInterface and
WiFiInterface.

This does mean adding a couple of virtual inheritances to avoid
duplicate NetworkInterfaces:

                   NetworkInterface
                     /           \
            virtual /             \ virtual
                   /               \
             EMACInterface     WiFiInterface
                   \               /
                    \             /
                     \           /
                  MyCustomWiFiInterface
pull/6847/head
Kevin Bracey 2017-12-05 12:45:30 +02:00
parent 0386f73719
commit 1c5bbaf4d8
6 changed files with 193 additions and 125 deletions

View File

@ -148,7 +148,7 @@ public:
/** These need to be defined by targets wishing to provide an Ethernet driver using EMAC interface. It will
* be used by the EthernetInterface class's default constructor to initialise the networking subsystem.
* be used by the EMACInterface class's default constructor to initialise the networking subsystem.
*/
//extern const emac_interface_ops_t mbed_emac_eth_ops_default;
//extern void *mbed_emac_eth_hw_default;

View File

@ -14,10 +14,10 @@
* limitations under the License.
*/
#include "EthernetInterface.h"
#include "EMACInterface.h"
/* Interface implementation */
EthernetInterface::EthernetInterface(EMAC &emac, OnboardNetworkStack &stack) :
EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack) :
_emac(emac),
_stack(stack),
_interface(NULL),
@ -29,7 +29,7 @@ EthernetInterface::EthernetInterface(EMAC &emac, OnboardNetworkStack &stack) :
{
}
nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
nsapi_error_t EMACInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
{
_dhcp = false;
@ -43,13 +43,13 @@ nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char
return NSAPI_ERROR_OK;
}
nsapi_error_t EthernetInterface::set_dhcp(bool dhcp)
nsapi_error_t EMACInterface::set_dhcp(bool dhcp)
{
_dhcp = dhcp;
return NSAPI_ERROR_OK;
}
nsapi_error_t EthernetInterface::connect()
nsapi_error_t EMACInterface::connect()
{
if (!_interface) {
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
@ -68,12 +68,12 @@ nsapi_error_t EthernetInterface::connect()
_blocking);
}
nsapi_error_t EthernetInterface::disconnect()
nsapi_error_t EMACInterface::disconnect()
{
return _interface->bringdown();
}
const char *EthernetInterface::get_mac_address()
const char *EMACInterface::get_mac_address()
{
if (_interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
return _mac_address;
@ -81,7 +81,7 @@ const char *EthernetInterface::get_mac_address()
return NULL;
}
const char *EthernetInterface::get_ip_address()
const char *EMACInterface::get_ip_address()
{
if (_interface->get_ip_address(_ip_address, sizeof(_ip_address))) {
return _ip_address;
@ -90,7 +90,7 @@ const char *EthernetInterface::get_ip_address()
return NULL;
}
const char *EthernetInterface::get_netmask()
const char *EMACInterface::get_netmask()
{
if (_interface->get_netmask(_netmask, sizeof(_netmask))) {
return _netmask;
@ -99,7 +99,7 @@ const char *EthernetInterface::get_netmask()
return 0;
}
const char *EthernetInterface::get_gateway()
const char *EMACInterface::get_gateway()
{
if (_interface->get_gateway(_gateway, sizeof(_gateway))) {
return _gateway;
@ -108,12 +108,12 @@ const char *EthernetInterface::get_gateway()
return 0;
}
NetworkStack *EthernetInterface::get_stack()
NetworkStack *EMACInterface::get_stack()
{
return &_stack;
}
void EthernetInterface::attach(
void EMACInterface::attach(
Callback<void(nsapi_event_t, intptr_t)> status_cb)
{
_connection_status_cb = status_cb;
@ -122,7 +122,7 @@ void EthernetInterface::attach(
}
}
nsapi_connection_status_t EthernetInterface::get_connection_status() const
nsapi_connection_status_t EMACInterface::get_connection_status() const
{
if (_interface) {
return _interface->get_connection_status();
@ -131,7 +131,7 @@ nsapi_connection_status_t EthernetInterface::get_connection_status() const
}
}
nsapi_error_t EthernetInterface::set_blocking(bool blocking)
nsapi_error_t EMACInterface::set_blocking(bool blocking)
{
_blocking = blocking;
return NSAPI_ERROR_OK;

View File

@ -0,0 +1,172 @@
/* LWIP implementation of NetworkInterfaceAPI
* 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 EMAC_INTERFACE_H
#define EMAC_INTERFACE_H
#include "nsapi.h"
#include "rtos.h"
#include "EMAC.h"
#include "OnboardNetworkStack.h"
/** EMACInterface class
* Implementation of the NetworkInterface for an EMAC-based driver
*
* This class provides the necessary glue logic to create a NetworkInterface
* based on an EMAC and an OnboardNetworkStack. EthernetInterface and
* EMAC-based Wi-Fi drivers derive from it.
*
* Drivers derived from EMACInterface should be constructed so that their
* EMAC is functional without the need to call `connect()`. For example
* a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
* the credentials have been set. This is necessary to support specialised
* applications such as 6LoWPAN mesh border routers.
*/
class EMACInterface : public virtual NetworkInterface
{
public:
/** Create an EMAC-based network interface.
*
* The default arguments obtain the default EMAC, which will be target-
* dependent (and the target may have some JSON option to choose which
* is the default, if there are multiple). The default stack is configured
* by JSON option nsapi.default-stack.
*
* Due to inability to return errors from the constructor, no real
* work is done until the first call to connect().
*
* @param emac Reference to EMAC to use
* @param stack Reference to onboard-network stack to use
*/
EMACInterface(
EMAC &emac = EMAC::get_default_instance(),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
/** Set a static IP address
*
* Configures 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.
*
* @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
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network
*
* Requires that the network is disconnected
*
* @param dhcp False to disable dhcp (defaults to enabled)
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_dhcp(bool dhcp);
/** Start the interface
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t connect();
/** Stop the interface
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t disconnect();
/** Get the local MAC address
*
* Provided MAC address is intended for info or debug purposes and
* may not be provided if the underlying network interface does not
* provide a MAC address
*
* @return Null-terminated representation of the local MAC address
* or null if no MAC address is available
*/
virtual const char *get_mac_address();
/** Get the local IP address
*
* @return Null-terminated representation of the local IP address
* or null if no IP address has been recieved
*/
virtual const char *get_ip_address();
/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual const char *get_netmask();
/** Get the local gateways
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual const char *get_gateway();
/** Register callback for status reporting
*
* @param status_cb The callback for status changes
*/
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
/** Get the connection status
*
* @return The connection status according to nsapi_connection_status_t
*/
virtual nsapi_connection_status_t get_connection_status() const;
/** Set blocking status of connect() which by default should be blocking
*
* @param blocking true if connect is blocking
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_blocking(bool blocking);
/** Provide access to the EMAC
*
* This should be used with care - normally the network stack would
* control the EMAC, so manipulating the EMAC while the stack
* is also using it (ie after connect) will likely cause problems.
*
* @return Reference to the EMAC in use
*/
EMAC &get_emac() const { return _emac; }
protected:
/** Provide access to the underlying stack
*
* @return The underlying network stack
*/
virtual NetworkStack *get_stack();
EMAC &_emac;
OnboardNetworkStack &_stack;
OnboardNetworkStack::Interface *_interface;
bool _dhcp;
bool _blocking;
char _mac_address[NSAPI_MAC_SIZE];
char _ip_address[NSAPI_IPv6_SIZE];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
};
#endif

View File

@ -27,7 +27,7 @@
*
* Common interface that is shared between ethernet hardware.
*/
class EthInterface : public NetworkInterface
class EthInterface : public virtual NetworkInterface
{
};

View File

@ -19,14 +19,13 @@
#include "nsapi.h"
#include "rtos.h"
#include "EMAC.h"
#include "OnboardNetworkStack.h"
#include "EMACInterface.h"
/** EthernetInterface class
* Implementation of the NetworkStack for an EMAC-based driver
* Implementation of the NetworkStack for an EMAC-based Ethernet driver
*/
class EthernetInterface : public EthInterface
class EthernetInterface : public EMACInterface, public EthInterface
{
public:
/** Create an EMAC-based ethernet interface.
@ -44,110 +43,7 @@ public:
*/
EthernetInterface(
EMAC &emac = EMAC::get_default_instance(),
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
/** Set a static IP address
*
* Configures 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.
*
* @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
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_network(
const char *ip_address, const char *netmask, const char *gateway);
/** Enable or disable DHCP on the network
*
* Requires that the network is disconnected
*
* @param dhcp False to disable dhcp (defaults to enabled)
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_dhcp(bool dhcp);
/** Start the interface
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t connect();
/** Stop the interface
* @return 0 on success, negative on failure
*/
virtual nsapi_error_t disconnect();
/** Get the local MAC address
*
* Provided MAC address is intended for info or debug purposes and
* may not be provided if the underlying network interface does not
* provide a MAC address
*
* @return Null-terminated representation of the local MAC address
* or null if no MAC address is available
*/
virtual const char *get_mac_address();
/** Get the local IP address
*
* @return Null-terminated representation of the local IP address
* or null if no IP address has been recieved
*/
virtual const char *get_ip_address();
/** Get the local network mask
*
* @return Null-terminated representation of the local network mask
* or null if no network mask has been recieved
*/
virtual const char *get_netmask();
/** Get the local gateways
*
* @return Null-terminated representation of the local gateway
* or null if no network mask has been recieved
*/
virtual const char *get_gateway();
/** Register callback for status reporting
*
* @param status_cb The callback for status changes
*/
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
/** Get the connection status
*
* @return The connection status according to nsapi_connection_status_t
*/
virtual nsapi_connection_status_t get_connection_status() const;
/** Set blocking status of connect() which by default should be blocking
*
* @param blocking true if connect is blocking
* @return 0 on success, negative error code on failure
*/
virtual nsapi_error_t set_blocking(bool blocking);
protected:
/** Provide access to the underlying stack
*
* @return The underlying network stack
*/
virtual NetworkStack *get_stack();
EMAC &_emac;
OnboardNetworkStack &_stack;
OnboardNetworkStack::Interface *_interface;
bool _dhcp;
bool _blocking;
char _mac_address[NSAPI_MAC_SIZE];
char _ip_address[NSAPI_IPv6_SIZE];
char _netmask[NSAPI_IPv4_SIZE];
char _gateway[NSAPI_IPv4_SIZE];
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance()) : EMACInterface(emac, stack) { }
};
#endif

View File

@ -27,7 +27,7 @@
* Common interface that is shared between WiFi devices
* @addtogroup netsocket
*/
class WiFiInterface: public NetworkInterface
class WiFiInterface: public virtual NetworkInterface
{
public:
/** WiFiInterface lifetime