Skip to content

Commit 2f5c66a

Browse files
committed
Add NetworkInterface::get_default_instance()
Provide an initial framework to make it easier to find a default network interface.
1 parent 847a3e6 commit 2f5c66a

File tree

15 files changed

+494
-26
lines changed

15 files changed

+494
-26
lines changed

features/nanostack/mbed-mesh-api/source/LoWPANNDInterface.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,13 @@ bool LoWPANNDInterface::getRouterIpAddress(char *address, int8_t len)
160160
{
161161
return _interface->get_gateway(address, len);
162162
}
163+
164+
#define LOWPAN 0x2345
165+
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == LOWPAN && DEVICE_802_15_4_PHY
166+
MBED_WEAK MeshInterface *MeshInterface::get_default_instance()
167+
{
168+
static LoWPANNDInterface lowpan(NanostackRfPhy::get_default_instance());
169+
170+
return lowpan;
171+
}
172+
#endif

features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,10 @@ nsapi_error_t InterfaceNanostack::set_blocking(bool blocking)
183183
_blocking = blocking;
184184
return NSAPI_ERROR_OK;
185185
}
186+
187+
#if !DEVICE_802_15_4_PHY
188+
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
189+
{
190+
return NULL;
191+
}
192+
#endif

features/nanostack/mbed-mesh-api/source/ThreadInterface.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,13 @@ mesh_error_t Nanostack::ThreadInterface::device_pskd_set(const char *pskd)
231231
{
232232
return (mesh_error_t)thread_tasklet_device_pskd_set(pskd);
233233
}
234+
235+
#define THREAD 0x2345
236+
#if MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == THREAD && DEVICE_802_15_4_PHY
237+
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
238+
{
239+
static ThreadInterface thread(NanostackRfPhy::get_default_instance());
240+
241+
return thread;
242+
}
243+
#endif

features/nanostack/nanostack-interface/NanostackRfPhy.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
class NanostackRfPhy : public NanostackPhy {
2525
public:
2626

27+
/** Return the default on-board NanostackRfPhy
28+
*
29+
* Returns the default on-board NanostackRfPhy - this will be target-specific, and
30+
* may not be available on all targets.
31+
*/
32+
static NanostackRfPhy &get_default_instance();
33+
2734
/** Register this physical interface with Nanostack
2835
*
2936
* @return Device driver ID or a negative error

features/netsocket/CellularBase.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class CellularBase: public NetworkInterface {
2626

2727
public:
2828

29+
/** Get the default Cellular interface.
30+
*
31+
* This is provided as a weak method so applications can override.
32+
* Default behaviour is to get the target's default interface, if
33+
* any.
34+
*
35+
* @return pointer to interface, if any
36+
*/
37+
38+
static CellularBase *get_default_instance();
39+
2940
/** Set the Cellular network credentials
3041
*
3142
* Please check documentation of connect() for default behaviour of APN settings.
@@ -102,6 +113,17 @@ class CellularBase: public NetworkInterface {
102113
virtual const char *get_gateway() = 0;
103114

104115
virtual CellularBase *cellularBase() { return this; }
116+
117+
protected:
118+
/** Get the target's default Cellular interface.
119+
*
120+
* This is provided as a weak method so targets can override. The
121+
* default implementation configures and returns the OnBoardModemInterface
122+
* if available.
123+
*
124+
* @return pointer to interface, if any
125+
*/
126+
static CellularBase *get_target_default_instance();
105127
};
106128

107129
#endif //CELLULAR_BASE_H

features/netsocket/EthInterface.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,31 @@
2929
*/
3030
class EthInterface : public virtual NetworkInterface
3131
{
32+
public:
33+
3234
virtual EthInterface *ethInterface() { return this; }
35+
36+
/** Get the default Ethernet interface.
37+
*
38+
* This is provided as a weak method so applications can override.
39+
* Default behaviour is to get the target's default interface, if
40+
* any.
41+
*
42+
* @return pointer to interface, if any
43+
*/
44+
static EthInterface *get_default_instance();
45+
46+
protected:
47+
48+
/** Get the target's default Ethernet interface.
49+
*
50+
* This is provided as a weak method so targets can override. The
51+
* default implementation will invoke EthernetInterface with the
52+
* default EMAC and default network stack, if DEVICE_EMAC is set.
53+
*
54+
* @return pointer to interface, if any
55+
*/
56+
static EthInterface *get_target_default_instance();
3357
};
3458

3559

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
#include "EthernetInterface.h"
18+
19+
/* No actual interface implementation here, as EthernetInterface is
20+
* just an EMACInterface. But we can be the default EthInterface - step up
21+
* if the target has a default EMAC.
22+
*/
23+
#if DEVICE_EMAC
24+
MBED_WEAK EthInterface *EthInterface::get_target_default_instance()
25+
{
26+
static EthernetInterface ethernet;
27+
return &ethernet;
28+
}
29+
#else
30+
MBED_WEAK EthInterface *EthInterface::get_target_default_instance()
31+
{
32+
return NULL;
33+
}
34+
#endif

features/netsocket/MeshInterface.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,31 @@
2929
*/
3030
class MeshInterface : public virtual NetworkInterface
3131
{
32+
public:
33+
3234
virtual MeshInterface *meshInterface() { return this; }
35+
36+
/** Get the default Mesh interface.
37+
*
38+
* This is provided as a weak method so applications can override.
39+
* Default behaviour is to get the target's default interface, if
40+
* any.
41+
*
42+
* @return pointer to interface, if any
43+
*/
44+
static MeshInterface *get_default_instance();
45+
46+
protected:
47+
48+
/** Get the target's default Mesh interface.
49+
*
50+
* This is provided as a weak method so targets can override. The
51+
* default implementation will invoke LoWPANNDInterface or ThreadInterface
52+
* with the default NanostackRfPhy.
53+
*
54+
* @return pointer to interface, if any
55+
*/
56+
static MeshInterface *get_target_default_instance();
3357
};
3458

3559

features/netsocket/NetworkInterface.h

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,46 @@ class EMACInterface;
3939
class NetworkInterface: public DNS {
4040
public:
4141

42-
43-
4442
virtual ~NetworkInterface() {};
4543

44+
/** Return the default network interface
45+
*
46+
* Returns the default network interface, as determined by JSON option
47+
* target.network-default-interface-type or other overrides.
48+
*
49+
* The type of the interface returned can be tested via the ethInterface()
50+
* etc downcasts.
51+
*
52+
* The default behaviour is to return the default interface for the
53+
* interface type specified by target.network-default-interface-type. Targets
54+
* should set this in their targets.json to guide default selection,
55+
* and applications may override.
56+
*
57+
* The interface returned should be already configured for use such that its
58+
* connect() method works with no parameters. For connection types needing
59+
* configuration, settings should normally be obtained from JSON - the
60+
* settings for the core types are under the "nsapi" JSON config tree.
61+
*
62+
* The list of possible settings for default interface type is open-ended,
63+
* as is the number of possible providers. Core providers are:
64+
*
65+
* * ETHERNET: EthernetInterface, using default EMAC and OnboardNetworkStack
66+
* * MESH: ThreadInterface or LoWPANNDInterface, using default NanostackRfPhy
67+
* * CELLULAR: OnboardModemInterface
68+
* * WIFI: None - always provided by a specific class
69+
*
70+
* Specific drivers may be activated by other settings of the
71+
* default-network-interface-type configuration. This will depend on the
72+
* target and the driver. For example a board may have its default setting
73+
* as "AUTO" which causes it to autodetect an Ethernet cable. This should
74+
* be described in the target's documentation.
75+
*
76+
* An application can override all target settings by implementing
77+
* NetworkInterface::get_default_instance() themselves - the default
78+
* definition is weak, and calls get_target_default_instance().
79+
*/
80+
static NetworkInterface *get_default_instance();
81+
4682
/** Get the local MAC address
4783
*
4884
* Provided MAC address is intended for info or debug purposes and
@@ -237,6 +273,37 @@ class NetworkInterface: public DNS {
237273
* @return The underlying NetworkStack object
238274
*/
239275
virtual NetworkStack *get_stack() = 0;
276+
277+
/** Get the target's default network instance.
278+
*
279+
* This method can be overridden by the target. Default implementations
280+
* are provided weakly by various subsystems as described in
281+
* NetworkInterface::get_default_instance(), so targets should not
282+
* need to override in simple cases.
283+
*
284+
* If a target has more elaborate interface selection, it can completely
285+
* override this behaviour by implementing
286+
* NetworkInterface::get_target_default_instance() themselves, either
287+
* unconditionally, or for a specific network-default-interface-type setting
288+
*
289+
* For example, a device with both Ethernet and Wi-fi could be set up its
290+
* target so that:
291+
* * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(),
292+
* which means EthernetInterface provides EthInterface::get_target_instance()
293+
* based on that EMAC.
294+
* * It provides WifiInterface::get_target_default_instance().
295+
* * The core will route NetworkInterface::get_default_instance() to
296+
* either of those if network-default-interface-type is set to
297+
* ETHERNET or WIFI.
298+
* * The board overrides NetworkInterface::get_target_default_instance()
299+
* if network-default-interface-type is set to AUTO. This returns
300+
* either EthInterface::get_default_instance() or WiFIInterface::get_default_instance()
301+
* depending on a cable detection.
302+
*
303+
*
304+
* performs the search described by get_default_instance.
305+
*/
306+
static NetworkInterface *get_target_default_instance();
240307
};
241308

242309

0 commit comments

Comments
 (0)