Skip to content

Commit 1c5bbaf

Browse files
committed
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 0386f73 commit 1c5bbaf

File tree

6 files changed

+193
-125
lines changed

6 files changed

+193
-125
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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
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),
2222
_stack(stack),
2323
_interface(NULL),
@@ -29,7 +29,7 @@ EthernetInterface::EthernetInterface(EMAC &emac, OnboardNetworkStack &stack) :
2929
{
3030
}
3131

32-
nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
32+
nsapi_error_t EMACInterface::set_network(const char *ip_address, const char *netmask, const char *gateway)
3333
{
3434
_dhcp = false;
3535

@@ -43,13 +43,13 @@ nsapi_error_t EthernetInterface::set_network(const char *ip_address, const char
4343
return NSAPI_ERROR_OK;
4444
}
4545

46-
nsapi_error_t EthernetInterface::set_dhcp(bool dhcp)
46+
nsapi_error_t EMACInterface::set_dhcp(bool dhcp)
4747
{
4848
_dhcp = dhcp;
4949
return NSAPI_ERROR_OK;
5050
}
5151

52-
nsapi_error_t EthernetInterface::connect()
52+
nsapi_error_t EMACInterface::connect()
5353
{
5454
if (!_interface) {
5555
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
@@ -68,20 +68,20 @@ nsapi_error_t EthernetInterface::connect()
6868
_blocking);
6969
}
7070

71-
nsapi_error_t EthernetInterface::disconnect()
71+
nsapi_error_t EMACInterface::disconnect()
7272
{
7373
return _interface->bringdown();
7474
}
7575

76-
const char *EthernetInterface::get_mac_address()
76+
const char *EMACInterface::get_mac_address()
7777
{
7878
if (_interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
7979
return _mac_address;
8080
}
8181
return NULL;
8282
}
8383

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

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

102-
const char *EthernetInterface::get_gateway()
102+
const char *EMACInterface::get_gateway()
103103
{
104104
if (_interface->get_gateway(_gateway, sizeof(_gateway))) {
105105
return _gateway;
@@ -108,12 +108,12 @@ const char *EthernetInterface::get_gateway()
108108
return 0;
109109
}
110110

111-
NetworkStack *EthernetInterface::get_stack()
111+
NetworkStack *EMACInterface::get_stack()
112112
{
113113
return &_stack;
114114
}
115115

116-
void EthernetInterface::attach(
116+
void EMACInterface::attach(
117117
Callback<void(nsapi_event_t, intptr_t)> status_cb)
118118
{
119119
_connection_status_cb = status_cb;
@@ -122,7 +122,7 @@ void EthernetInterface::attach(
122122
}
123123
}
124124

125-
nsapi_connection_status_t EthernetInterface::get_connection_status() const
125+
nsapi_connection_status_t EMACInterface::get_connection_status() const
126126
{
127127
if (_interface) {
128128
return _interface->get_connection_status();
@@ -131,7 +131,7 @@ nsapi_connection_status_t EthernetInterface::get_connection_status() const
131131
}
132132
}
133133

134-
nsapi_error_t EthernetInterface::set_blocking(bool blocking)
134+
nsapi_error_t EMACInterface::set_blocking(bool blocking)
135135
{
136136
_blocking = blocking;
137137
return NSAPI_ERROR_OK;

features/netsocket/EMACInterface.h

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
* This class provides the necessary glue logic to create a NetworkInterface
30+
* based on an EMAC and an OnboardNetworkStack. EthernetInterface and
31+
* EMAC-based Wi-Fi drivers derive from it.
32+
*
33+
* Drivers derived from EMACInterface should be constructed so that their
34+
* EMAC is functional without the need to call `connect()`. For example
35+
* a Wi-Fi driver should permit `WiFi::get_emac().power_up()` as soon as
36+
* the credentials have been set. This is necessary to support specialised
37+
* applications such as 6LoWPAN mesh border routers.
38+
*/
39+
class EMACInterface : public virtual NetworkInterface
40+
{
41+
public:
42+
/** Create an EMAC-based network interface.
43+
*
44+
* The default arguments obtain the default EMAC, which will be target-
45+
* dependent (and the target may have some JSON option to choose which
46+
* is the default, if there are multiple). The default stack is configured
47+
* by JSON option nsapi.default-stack.
48+
*
49+
* Due to inability to return errors from the constructor, no real
50+
* work is done until the first call to connect().
51+
*
52+
* @param emac Reference to EMAC to use
53+
* @param stack Reference to onboard-network stack to use
54+
*/
55+
EMACInterface(
56+
EMAC &emac = EMAC::get_default_instance(),
57+
OnboardNetworkStack &stack = OnboardNetworkStack::get_default_instance());
58+
59+
/** Set a static IP address
60+
*
61+
* Configures this network interface to use a static IP address.
62+
* Implicitly disables DHCP, which can be enabled in set_dhcp.
63+
* Requires that the network is disconnected.
64+
*
65+
* @param ip_address Null-terminated representation of the local IP address
66+
* @param netmask Null-terminated representation of the local network mask
67+
* @param gateway Null-terminated representation of the local gateway
68+
* @return 0 on success, negative error code on failure
69+
*/
70+
virtual nsapi_error_t set_network(
71+
const char *ip_address, const char *netmask, const char *gateway);
72+
73+
/** Enable or disable DHCP on the network
74+
*
75+
* Requires that the network is disconnected
76+
*
77+
* @param dhcp False to disable dhcp (defaults to enabled)
78+
* @return 0 on success, negative error code on failure
79+
*/
80+
virtual nsapi_error_t set_dhcp(bool dhcp);
81+
82+
/** Start the interface
83+
* @return 0 on success, negative on failure
84+
*/
85+
virtual nsapi_error_t connect();
86+
87+
/** Stop the interface
88+
* @return 0 on success, negative on failure
89+
*/
90+
virtual nsapi_error_t disconnect();
91+
92+
/** Get the local MAC address
93+
*
94+
* Provided MAC address is intended for info or debug purposes and
95+
* may not be provided if the underlying network interface does not
96+
* provide a MAC address
97+
*
98+
* @return Null-terminated representation of the local MAC address
99+
* or null if no MAC address is available
100+
*/
101+
virtual const char *get_mac_address();
102+
103+
/** Get the local IP address
104+
*
105+
* @return Null-terminated representation of the local IP address
106+
* or null if no IP address has been recieved
107+
*/
108+
virtual const char *get_ip_address();
109+
110+
/** Get the local network mask
111+
*
112+
* @return Null-terminated representation of the local network mask
113+
* or null if no network mask has been recieved
114+
*/
115+
virtual const char *get_netmask();
116+
117+
/** Get the local gateways
118+
*
119+
* @return Null-terminated representation of the local gateway
120+
* or null if no network mask has been recieved
121+
*/
122+
virtual const char *get_gateway();
123+
124+
/** Register callback for status reporting
125+
*
126+
* @param status_cb The callback for status changes
127+
*/
128+
virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb);
129+
130+
/** Get the connection status
131+
*
132+
* @return The connection status according to nsapi_connection_status_t
133+
*/
134+
virtual nsapi_connection_status_t get_connection_status() const;
135+
136+
/** Set blocking status of connect() which by default should be blocking
137+
*
138+
* @param blocking true if connect is blocking
139+
* @return 0 on success, negative error code on failure
140+
*/
141+
virtual nsapi_error_t set_blocking(bool blocking);
142+
143+
/** Provide access to the EMAC
144+
*
145+
* This should be used with care - normally the network stack would
146+
* control the EMAC, so manipulating the EMAC while the stack
147+
* is also using it (ie after connect) will likely cause problems.
148+
*
149+
* @return Reference to the EMAC in use
150+
*/
151+
EMAC &get_emac() const { return _emac; }
152+
153+
protected:
154+
/** Provide access to the underlying stack
155+
*
156+
* @return The underlying network stack
157+
*/
158+
virtual NetworkStack *get_stack();
159+
160+
EMAC &_emac;
161+
OnboardNetworkStack &_stack;
162+
OnboardNetworkStack::Interface *_interface;
163+
bool _dhcp;
164+
bool _blocking;
165+
char _mac_address[NSAPI_MAC_SIZE];
166+
char _ip_address[NSAPI_IPv6_SIZE];
167+
char _netmask[NSAPI_IPv4_SIZE];
168+
char _gateway[NSAPI_IPv4_SIZE];
169+
Callback<void(nsapi_event_t, intptr_t)> _connection_status_cb;
170+
};
171+
172+
#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

0 commit comments

Comments
 (0)