Skip to content

Commit f3ec0da

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

File tree

15 files changed

+489
-26
lines changed

15 files changed

+489
-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
@@ -182,3 +182,10 @@ nsapi_error_t InterfaceNanostack::set_blocking(bool blocking)
182182
_blocking = blocking;
183183
return NSAPI_ERROR_OK;
184184
}
185+
186+
#if !DEVICE_802_15_4_PHY
187+
MBED_WEAK MeshInterface *MeshInterface::get_target_default_instance()
188+
{
189+
return NULL;
190+
}
191+
#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.
@@ -104,6 +115,17 @@ class CellularBase: public NetworkInterface {
104115
virtual CellularBase *cellularBase() {
105116
return this;
106117
}
118+
119+
protected:
120+
/** Get the target's default Cellular interface.
121+
*
122+
* This is provided as a weak method so targets can override. The
123+
* default implementation configures and returns the OnBoardModemInterface
124+
* if available.
125+
*
126+
* @return pointer to interface, if any
127+
*/
128+
static CellularBase *get_target_default_instance();
107129
};
108130

109131
#endif //CELLULAR_BASE_H

features/netsocket/EthInterface.h

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

3761

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,9 +29,33 @@
2929
*/
3030
class MeshInterface : public virtual NetworkInterface
3131
{
32+
public:
33+
3234
virtual MeshInterface *meshInterface() {
3335
return this;
3436
}
37+
38+
/** Get the default Mesh interface.
39+
*
40+
* This is provided as a weak method so applications can override.
41+
* Default behaviour is to get the target's default interface, if
42+
* any.
43+
*
44+
* @return pointer to interface, if any
45+
*/
46+
static MeshInterface *get_default_instance();
47+
48+
protected:
49+
50+
/** Get the target's default Mesh interface.
51+
*
52+
* This is provided as a weak method so targets can override. The
53+
* default implementation will invoke LoWPANNDInterface or ThreadInterface
54+
* with the default NanostackRfPhy.
55+
*
56+
* @return pointer to interface, if any
57+
*/
58+
static MeshInterface *get_target_default_instance();
3559
};
3660

3761

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
@@ -247,6 +283,37 @@ class NetworkInterface: public DNS {
247283
* @return The underlying NetworkStack object
248284
*/
249285
virtual NetworkStack *get_stack() = 0;
286+
287+
/** Get the target's default network instance.
288+
*
289+
* This method can be overridden by the target. Default implementations
290+
* are provided weakly by various subsystems as described in
291+
* NetworkInterface::get_default_instance(), so targets should not
292+
* need to override in simple cases.
293+
*
294+
* If a target has more elaborate interface selection, it can completely
295+
* override this behaviour by implementing
296+
* NetworkInterface::get_target_default_instance() themselves, either
297+
* unconditionally, or for a specific network-default-interface-type setting
298+
*
299+
* For example, a device with both Ethernet and Wi-fi could be set up its
300+
* target so that:
301+
* * DEVICE_EMAC is set, and it provides EMAC::get_default_instance(),
302+
* which means EthernetInterface provides EthInterface::get_target_instance()
303+
* based on that EMAC.
304+
* * It provides WifiInterface::get_target_default_instance().
305+
* * The core will route NetworkInterface::get_default_instance() to
306+
* either of those if network-default-interface-type is set to
307+
* ETHERNET or WIFI.
308+
* * The board overrides NetworkInterface::get_target_default_instance()
309+
* if network-default-interface-type is set to AUTO. This returns
310+
* either EthInterface::get_default_instance() or WiFIInterface::get_default_instance()
311+
* depending on a cable detection.
312+
*
313+
*
314+
* performs the search described by get_default_instance.
315+
*/
316+
static NetworkInterface *get_target_default_instance();
250317
};
251318

252319

0 commit comments

Comments
 (0)