Skip to content

Commit 25a54f8

Browse files
kjbracey0xc0170
authored andcommitted
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
1 parent 20aadc0 commit 25a54f8

File tree

6 files changed

+149
-100
lines changed

6 files changed

+149
-100
lines changed

features/netsocket/EMAC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class EMAC {
148148

149149

150150
/** These need to be defined by targets wishing to provide an Ethernet driver using EMAC interface. It will
151-
* be used by the EthernetInterface class's default constructor to initialise the networking subsystem.
151+
* be used by the EMACInterface class's default constructor to initialise the networking subsystem.
152152
*/
153153
//extern const emac_interface_ops_t mbed_emac_eth_ops_default;
154154
//extern void *mbed_emac_eth_hw_default;

features/netsocket/EthernetInterface.cpp renamed to features/netsocket/EMACInterface.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include "EthernetInterface.h"
17+
#include "EMACInterface.h"
1818

1919
/* Interface implementation */
20-
EthernetInterface::EthernetInterface(EMAC &emac, OnboardNetworkStack &stack)
20+
EMACInterface::EMACInterface(EMAC &emac, OnboardNetworkStack &stack)
2121
: _emac(emac), _stack(stack), _interface(NULL), _dhcp(true), _mac_address(), _ip_address(), _netmask(), _gateway()
2222
{
2323
}
2424

25-
nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
25+
nsapi_error_t EMACInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
2626
{
2727
_dhcp = false;
2828

@@ -36,13 +36,13 @@ nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char
3636
return NSAPI_ERROR_OK;
3737
}
3838

39-
nsapi_error_t EthernetInterface::set_dhcp(bool dhcp)
39+
nsapi_error_t EMACInterface::set_dhcp(bool dhcp)
4040
{
4141
_dhcp = dhcp;
4242
return NSAPI_ERROR_OK;
4343
}
4444

45-
nsapi_error_t EthernetInterface::connect()
45+
nsapi_error_t EMACInterface::connect()
4646
{
4747
if (!_interface) {
4848
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
@@ -59,20 +59,20 @@ nsapi_error_t EthernetInterface::connect()
5959
DEFAULT_STACK);
6060
}
6161

62-
nsapi_error_t EthernetInterface::disconnect()
62+
nsapi_error_t EMACInterface::disconnect()
6363
{
6464
return _interface->bringdown();
6565
}
6666

67-
const char *EthernetInterface::get_mac_address()
67+
const char *EMACInterface::get_mac_address()
6868
{
6969
if (_interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
7070
return _mac_address;
7171
}
7272
return NULL;
7373
}
7474

75-
const char *EthernetInterface::get_ip_address()
75+
const char *EMACInterface::get_ip_address()
7676
{
7777
if (_interface->get_ip_address(_ip_address, sizeof(_ip_address))) {
7878
return _ip_address;
@@ -81,7 +81,7 @@ const char *EthernetInterface::get_ip_address()
8181
return NULL;
8282
}
8383

84-
const char *EthernetInterface::get_netmask()
84+
const char *EMACInterface::get_netmask()
8585
{
8686
if (_interface->get_netmask(_netmask, sizeof(_netmask))) {
8787
return _netmask;
@@ -90,7 +90,7 @@ const char *EthernetInterface::get_netmask()
9090
return 0;
9191
}
9292

93-
const char *EthernetInterface::get_gateway()
93+
const char *EMACInterface::get_gateway()
9494
{
9595
if (_interface->get_gateway(_gateway, sizeof(_gateway))) {
9696
return _gateway;
@@ -99,7 +99,7 @@ const char *EthernetInterface::get_gateway()
9999
return 0;
100100
}
101101

102-
NetworkStack *EthernetInterface::get_stack()
102+
NetworkStack *EMACInterface::get_stack()
103103
{
104104
return &_stack;
105105
}

features/netsocket/EMACInterface.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* LWIP implementation of NetworkInterfaceAPI
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef EMAC_INTERFACE_H
18+
#define EMAC_INTERFACE_H
19+
20+
#include "nsapi.h"
21+
#include "rtos.h"
22+
#include "EMAC.h"
23+
#include "OnboardNetworkStack.h"
24+
25+
26+
/** EMACInterface class
27+
* Implementation of the NetworkInterface for an EMAC-based driver
28+
*/
29+
class EMACInterface : public virtual NetworkInterface
30+
{
31+
public:
32+
/** Create an EMAC-based network interface.
33+
*
34+
* The default arguments obtain the default EMAC, which will be target-
35+
* dependent (and the target may have some JSON option to choose which
36+
* is the default, if there are multiple). The default stack is configured
37+
* by JSON option nsapi.default-stack.
38+
*
39+
* Due to inability to return errors from the constructor, no real
40+
* work is done until the first call to connect().
41+
*
42+
* @param emac Reference to EMAC to use
43+
* @param stack Reference to onboard-network stack to use
44+
*/
45+
EMACInterface(
46+
EMAC &emac = EMAC::get_default_instance(),
47+
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
48+
49+
/** Set a static IP address
50+
*
51+
* Configures this network interface to use a static IP address.
52+
* Implicitly disables DHCP, which can be enabled in set_dhcp.
53+
* Requires that the network is disconnected.
54+
*
55+
* @param ip_address Null-terminated representation of the local IP address
56+
* @param netmask Null-terminated representation of the local network mask
57+
* @param gateway Null-terminated representation of the local gateway
58+
* @return 0 on success, negative error code on failure
59+
*/
60+
virtual nsapi_error_t set_network(
61+
const char *ip_address, const char *netmask, const char *gateway);
62+
63+
/** Enable or disable DHCP on the network
64+
*
65+
* Requires that the network is disconnected
66+
*
67+
* @param dhcp False to disable dhcp (defaults to enabled)
68+
* @return 0 on success, negative error code on failure
69+
*/
70+
virtual nsapi_error_t set_dhcp(bool dhcp);
71+
72+
/** Start the interface
73+
* @return 0 on success, negative on failure
74+
*/
75+
virtual nsapi_error_t connect();
76+
77+
/** Stop the interface
78+
* @return 0 on success, negative on failure
79+
*/
80+
virtual nsapi_error_t disconnect();
81+
82+
/** Get the local MAC address
83+
*
84+
* Provided MAC address is intended for info or debug purposes and
85+
* may not be provided if the underlying network interface does not
86+
* provide a MAC address
87+
*
88+
* @return Null-terminated representation of the local MAC address
89+
* or null if no MAC address is available
90+
*/
91+
virtual const char *get_mac_address();
92+
93+
/** Get the local IP address
94+
*
95+
* @return Null-terminated representation of the local IP address
96+
* or null if no IP address has been recieved
97+
*/
98+
virtual const char *get_ip_address();
99+
100+
/** Get the local network mask
101+
*
102+
* @return Null-terminated representation of the local network mask
103+
* or null if no network mask has been recieved
104+
*/
105+
virtual const char *get_netmask();
106+
107+
/** Get the local gateways
108+
*
109+
* @return Null-terminated representation of the local gateway
110+
* or null if no network mask has been recieved
111+
*/
112+
virtual const char *get_gateway();
113+
114+
protected:
115+
/** Provide access to the underlying stack
116+
*
117+
* @return The underlying network stack
118+
*/
119+
virtual NetworkStack *get_stack();
120+
121+
EMAC &_emac;
122+
OnboardNetworkStack &_stack;
123+
OnboardNetworkStack::Interface *_interface;
124+
bool _dhcp;
125+
char _mac_address[NSAPI_MAC_SIZE];
126+
char _ip_address[NSAPI_IPv6_SIZE];
127+
char _netmask[NSAPI_IPv4_SIZE];
128+
char _gateway[NSAPI_IPv4_SIZE];
129+
};
130+
131+
#endif

features/netsocket/EthInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* Common interface that is shared between ethernet hardware.
2929
*/
30-
class EthInterface : public NetworkInterface
30+
class EthInterface : public virtual NetworkInterface
3131
{
3232
};
3333

features/netsocket/EthernetInterface.h

Lines changed: 4 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
#include "nsapi.h"
2121
#include "rtos.h"
22-
#include "EMAC.h"
23-
#include "OnboardNetworkStack.h"
22+
#include "EMACInterface.h"
2423

2524

2625
/** EthernetInterface class
27-
* Implementation of the NetworkStack for an EMAC-based driver
26+
* Implementation of the NetworkStack for an EMAC-based Ethernet driver
2827
*/
29-
class EthernetInterface : public EthInterface
28+
class EthernetInterface : public EMACInterface, public EthInterface
3029
{
3130
public:
3231
/** Create an EMAC-based ethernet interface.
@@ -44,88 +43,7 @@ class EthernetInterface : public EthInterface
4443
*/
4544
EthernetInterface(
4645
EMAC &emac = EMAC::get_default_instance(),
47-
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
48-
49-
/** Set a static IP address
50-
*
51-
* Configures this network interface to use a static IP address.
52-
* Implicitly disables DHCP, which can be enabled in set_dhcp.
53-
* Requires that the network is disconnected.
54-
*
55-
* @param ip_address Null-terminated representation of the local IP address
56-
* @param netmask Null-terminated representation of the local network mask
57-
* @param gateway Null-terminated representation of the local gateway
58-
* @return 0 on success, negative error code on failure
59-
*/
60-
virtual nsapi_error_t set_network(
61-
const char *ip_address, const char *netmask, const char *gateway);
62-
63-
/** Enable or disable DHCP on the network
64-
*
65-
* Requires that the network is disconnected
66-
*
67-
* @param dhcp False to disable dhcp (defaults to enabled)
68-
* @return 0 on success, negative error code on failure
69-
*/
70-
virtual nsapi_error_t set_dhcp(bool dhcp);
71-
72-
/** Start the interface
73-
* @return 0 on success, negative on failure
74-
*/
75-
virtual nsapi_error_t connect();
76-
77-
/** Stop the interface
78-
* @return 0 on success, negative on failure
79-
*/
80-
virtual nsapi_error_t disconnect();
81-
82-
/** Get the local MAC address
83-
*
84-
* Provided MAC address is intended for info or debug purposes and
85-
* may not be provided if the underlying network interface does not
86-
* provide a MAC address
87-
*
88-
* @return Null-terminated representation of the local MAC address
89-
* or null if no MAC address is available
90-
*/
91-
virtual const char *get_mac_address();
92-
93-
/** Get the local IP address
94-
*
95-
* @return Null-terminated representation of the local IP address
96-
* or null if no IP address has been recieved
97-
*/
98-
virtual const char *get_ip_address();
99-
100-
/** Get the local network mask
101-
*
102-
* @return Null-terminated representation of the local network mask
103-
* or null if no network mask has been recieved
104-
*/
105-
virtual const char *get_netmask();
106-
107-
/** Get the local gateways
108-
*
109-
* @return Null-terminated representation of the local gateway
110-
* or null if no network mask has been recieved
111-
*/
112-
virtual const char *get_gateway();
113-
114-
protected:
115-
/** Provide access to the underlying stack
116-
*
117-
* @return The underlying network stack
118-
*/
119-
virtual NetworkStack *get_stack();
120-
121-
EMAC &_emac;
122-
OnboardNetworkStack &_stack;
123-
OnboardNetworkStack::Interface *_interface;
124-
bool _dhcp;
125-
char _mac_address[NSAPI_MAC_SIZE];
126-
char _ip_address[NSAPI_IPv6_SIZE];
127-
char _netmask[NSAPI_IPv4_SIZE];
128-
char _gateway[NSAPI_IPv4_SIZE];
46+
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance()) : EMACInterface(emac, stack) { }
12947
};
13048

13149
#endif

features/netsocket/WiFiInterface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* Common interface that is shared between WiFi devices
2828
* @addtogroup netsocket
2929
*/
30-
class WiFiInterface: public NetworkInterface
30+
class WiFiInterface: public virtual NetworkInterface
3131
{
3232
public:
3333
/** WiFiInterface lifetime

0 commit comments

Comments
 (0)