Skip to content

Commit 32060bb

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 9006652 commit 32060bb

File tree

6 files changed

+191
-123
lines changed

6 files changed

+191
-123
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),
@@ -28,7 +28,7 @@ EthernetInterface::EthernetInterface(EMAC &emac, OnboardNetworkStack &stack) :
2828
{
2929
}
3030

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

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

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

51-
nsapi_error_t EthernetInterface::connect()
51+
nsapi_error_t EMACInterface::connect()
5252
{
5353
if (!_interface) {
5454
nsapi_error_t err = _stack.add_ethernet_interface(_emac, true, &_interface);
@@ -65,20 +65,20 @@ nsapi_error_t EthernetInterface::connect()
6565
DEFAULT_STACK);
6666
}
6767

68-
nsapi_error_t EthernetInterface::disconnect()
68+
nsapi_error_t EMACInterface::disconnect()
6969
{
7070
return _interface->bringdown();
7171
}
7272

73-
const char *EthernetInterface::get_mac_address()
73+
const char *EMACInterface::get_mac_address()
7474
{
7575
if (_interface->get_mac_address(_mac_address, sizeof(_mac_address))) {
7676
return _mac_address;
7777
}
7878
return NULL;
7979
}
8080

81-
const char *EthernetInterface::get_ip_address()
81+
const char *EMACInterface::get_ip_address()
8282
{
8383
if (_interface->get_ip_address(_ip_address, sizeof(_ip_address))) {
8484
return _ip_address;
@@ -87,7 +87,7 @@ const char *EthernetInterface::get_ip_address()
8787
return NULL;
8888
}
8989

90-
const char *EthernetInterface::get_netmask()
90+
const char *EMACInterface::get_netmask()
9191
{
9292
if (_interface->get_netmask(_netmask, sizeof(_netmask))) {
9393
return _netmask;
@@ -96,7 +96,7 @@ const char *EthernetInterface::get_netmask()
9696
return 0;
9797
}
9898

99-
const char *EthernetInterface::get_gateway()
99+
const char *EMACInterface::get_gateway()
100100
{
101101
if (_interface->get_gateway(_gateway, sizeof(_gateway))) {
102102
return _gateway;
@@ -105,23 +105,23 @@ const char *EthernetInterface::get_gateway()
105105
return 0;
106106
}
107107

108-
NetworkStack *EthernetInterface::get_stack()
108+
NetworkStack *EMACInterface::get_stack()
109109
{
110110
return &_stack;
111111
}
112112

113-
void EthernetInterface::attach(
113+
void EMACInterface::attach(
114114
Callback<void(nsapi_event_t, intptr_t)> status_cb)
115115
{
116116
_interface->attach(status_cb);
117117
}
118118

119-
nsapi_connection_status_t EthernetInterface::get_connection_status() const
119+
nsapi_connection_status_t EMACInterface::get_connection_status() const
120120
{
121121
return _interface->get_connection_status();
122122
}
123123

124-
nsapi_error_t EthernetInterface::set_blocking(bool blocking)
124+
nsapi_error_t EMACInterface::set_blocking(bool blocking)
125125
{
126126
_interface->set_blocking(blocking);
127127
return NSAPI_ERROR_OK;

features/netsocket/EMACInterface.h

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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+
char _mac_address[NSAPI_MAC_SIZE];
165+
char _ip_address[NSAPI_IPv6_SIZE];
166+
char _netmask[NSAPI_IPv4_SIZE];
167+
char _gateway[NSAPI_IPv4_SIZE];
168+
};
169+
170+
#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)