Skip to content

Commit dd8e531

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

File tree

7 files changed

+112
-1
lines changed

7 files changed

+112
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,14 @@ bool LoWPANNDInterface::getRouterIpAddress(char *address, int8_t len)
154154
{
155155
return _interface->get_gateway(address, len);
156156
}
157+
158+
#define MESH 0x2345
159+
#define LOWPAN 0x2345
160+
#if MBED_CONF_NSAPI_DEFAULT_INTERFACE_TYPE == MESH && MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == LOWPAN
161+
MBED_WEAK NetworkInterface &NetworkInterface::get_default_instance()
162+
{
163+
static LoWPANNDInterface lowpan(NanostackRfPhy::get_default_instance());
164+
165+
return lowpan;
166+
}
167+
#endif

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,14 @@ mesh_error_t Nanostack::ThreadInterface::device_pskd_set(const char *pskd)
215215
{
216216
return (mesh_error_t)thread_tasklet_device_pskd_set(pskd);
217217
}
218+
219+
#define MESH 0x2345
220+
#define THREAD 0x2345
221+
#if MBED_CONF_NSAPI_DEFAULT_INTERFACE_TYPE == MESH && MBED_CONF_NSAPI_DEFAULT_MESH_TYPE == THREAD
222+
MBED_WEAK NetworkInterface &NetworkInterface::get_default_instance()
223+
{
224+
static ThreadInterface thread(NanostackRfPhy::get_default_instance());
225+
226+
return thread;
227+
}
228+
#endif
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 NetworkInterface - step up
21+
* if the target has a default EMAC.
22+
*/
23+
24+
#define ETHERNET 0x2345
25+
#if DEVICE_EMAC && MBED_CONF_NSAPI_DEFAULT_INTERFACE_TYPE == ETHERNET
26+
MBED_WEAK NetworkInterface &NetworkInterface::get_default_instance()
27+
{
28+
static EthernetInterface ethernet;
29+
return ethernet;
30+
}
31+
#endif

features/netsocket/NetworkInterface.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,33 @@ class NetworkInterface {
3737
public:
3838
virtual ~NetworkInterface() {};
3939

40+
/** Return the default network interface
41+
*
42+
* Returns the default network interface, as determined by JSON option
43+
* nsapi.default-interface-type and other overrides.
44+
*
45+
* The core code provides default weak implementations of this in a number
46+
* of circumstances:
47+
* * if default-interface-type is ETHERNET and target has DEVICE_EMAC
48+
* * if default-interface-type is MESH and the RF phy has been located
49+
* * if default-interface-type is CELLULAR and OnboardCellularModem is available
50+
*
51+
* Targets can guide this default behaviour by setting nsapi.default-interface-type
52+
* in their targets.json. Or they can completely override by implementing
53+
* NetworkInterface::get_default_instance() themselves - they should do this
54+
* weakly, and not to conflict with the core definition.
55+
*
56+
* For example, a device with both Ethernet and Wi-fi could be set up so that
57+
* * DEVICE_EMAC is set, pointing at the Ethernet MAC
58+
* * The core will automatically provide EthernetInterface if default-interface-type is ETHERNET
59+
* * The target should provide its Wi-Fi driver if default-interface-type is WIFI
60+
* * The target could dynamically provide either depending on Ethernet cable detect if default-interface-type was AUTO
61+
*
62+
* Targets and core both provide weak definitions, so that an application or library
63+
* can provide an overriding normal definition.
64+
*/
65+
static NetworkInterface &get_default_instance();
66+
4067
/** Get the local MAC address
4168
*
4269
* Provided MAC address is intended for info or debug purposes and

features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,22 @@ void OnboardCellularInterface::modem_power_down()
5252
::onboard_modem_power_down();
5353
}
5454
#endif
55+
56+
#define CELLULAR 0x2345
57+
#if ONBOARD_CELLULAR_INTERFACE_AVAILABLE && MBED_CONF_NSAPI_DEFAULT_INTERFACE_TYPE == CELLULAR
58+
MBED_WEAK NetworkInterface &NetworkInterface::get_default_instance()
59+
{
60+
static OnboardCellularInterface cellular;
61+
62+
#ifdef MBED_CONF_DEFAULT_CELLULAR_APN
63+
#ifndef MBED_CONF_DEFAULT_CELLULAR_USERNAME
64+
#define MBED_CONF_DEFAULT_CELLULAR_USERNAME NULL
65+
#endif
66+
#ifndef MBED_CONF_DEFAULT_CELLULAR_PASSWORD
67+
#define MBED_CONF_DEFAULT_CELLULAR_PASSWORD NULL
68+
#endif
69+
cellular.set_credentials(MBED_CONF_DEFAULT_CELLULAR_APN, MBED_CONF_DEFAULT_CELLULAR_USERNAME, MBED_CONF_DEFAULT_CELLULAR_PASSWORD);
70+
#endif
71+
72+
return cellular;
73+
}

features/netsocket/cellular/generic_modem_driver/OnboardCellularInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,7 @@ class OnboardCellularInterface : public UARTCellularInterface {
6969
virtual void modem_power_down();
7070
};
7171

72+
#define ONBOARD_CELLULAR_INTERFACE_AVAILABLE
73+
7274
#endif //MODEM_ON_BOARD && MODEM_ON_BOARD_UART && NSAPI_PPP_AVAILABLE
7375
#endif //ONBOARD_CELLULAR_INTERFACE_

features/netsocket/mbed_lib.json

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
"name": "nsapi",
33
"config": {
44
"present": 1,
5-
"default-stack": "LWIP"
5+
"default-stack": "LWIP",
6+
"default-interface-type": {
7+
"help": "This guides the choice of the default NetworkInterface. Exactly what effect it has, and what settings are available, will depend on the target platform. Suggested values: ETHERNET, WIFI, CELLULAR, MESH.",
8+
"value": "ETHERNET"
9+
},
10+
"default-wifi-ssid": null,
11+
"default-wifi-password": null,
12+
"default-cellular-apn": null,
13+
"default-cellular-username": null,
14+
"default-cellular-password": null,
15+
"default-mesh-type": "THREAD",
616
}
717
}

0 commit comments

Comments
 (0)