Skip to content

Commit 295302e

Browse files
author
Amanda Butler
authored
Merge pull request #660 from SeppoTakalo/default_interface
Add information about default network interface
2 parents da75fed + a08ff94 commit 295302e

File tree

1 file changed

+74
-2
lines changed

1 file changed

+74
-2
lines changed

docs/reference/api/connectivity/networksocket/networkinterface.md

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,85 @@ A socket requires a NetworkInterface instance when opened to indicate which Netw
44

55
Network interface is also the controlling API for application to specify network configuration.
66

7-
Existing network interfaces:
7+
### Existing network interfaces:
88

99
- [Ethernet](ethernet.html): API for connecting to the internet over an Ethernet connection.
1010
- [Wi-Fi](wi-fi.html): API for connecting to the internet with a Wi-Fi device.
1111
- [Cellular](cellular-api.html): API for connecting to the internet using a cellular device.
1212
- [Mesh networking interface](mesh-api.html): Mbed OS provides two kinds of IPv6-based mesh networks - 6LoWPAN_ND and Thread.
1313

14-
Optional functionality:
14+
### Network status updates
15+
16+
An application may choose to register a callback to monitoring status changes from network interfaces. The application can do this by using the following API:
1517

1618
- [Network status](network-status.html): API for monitoring network status changes.
19+
20+
### Default network interface
21+
22+
In Mbed OS, targets that provide network connectivity also provide a default network interface. This can be Ethernet, Wi-Fi, mesh or cellular. Using a default interface allows you to port applications to different targets and connectivity options.
23+
24+
The following example uses a default interface to connect to the network:
25+
26+
```
27+
NetworkInterface *net = NetworkInterface::get_default_instance();
28+
29+
if (!net) {
30+
printf("Error! No network inteface found.\n");
31+
return 0;
32+
}
33+
34+
net->connect();
35+
```
36+
37+
This example works on all IP-based connectivity options that Mbed OS supports. The Mbed OS configuration system provides configuration for the default interface at build time, so at run time, you can just call `connect()` without any parameters.
38+
39+
For example, when providing Wi-Fi SSID and password, you may use the following `mbed_app.json`:
40+
41+
```
42+
{
43+
"target_overrides": {
44+
"*": {
45+
"nsapi.default-wifi-security": "WPA_WPA2",
46+
"nsapi.default-wifi-ssid": "\"ssid\"",
47+
"nsapi.default-wifi-password": "\"password\""
48+
}
49+
}
50+
}
51+
```
52+
53+
Please see [Selecting the default network interface](configuration-connectivity.html#selecting-the-default-network-interface) for information about how to supply required configuration parameters on different connections.
54+
55+
Targets with connectivity set the `target.network-default-interface-type` configuration variable appropriately, either to their only interface or the one most commonly used. For targets that provide more than one type of connectivity, you may choose the default by overriding the `target.network-default-interface-type` configuration variable.
56+
57+
Applications may also ask for a specific type of connection, as the following table shows:
58+
59+
|Method|What connectivity is returned| Requirements |
60+
|------|-----------------------------|--------------|
61+
|`*WiFiInterface::get_default_instance()`| Wi-Fi interface | Requires security parameters (mode, SSID, password) |
62+
|`*EthInterface::get_default_instance()` | Wired Ethernet interface, not Wi-Fi | none |
63+
|`*MeshInterface::get_default_instance()` | Returns either `LoWPANNDInterface` or `ThreadInterface`, depending on which is set to default | Target provides a driver or macro `DEVICE_802_15_4_PHY` is enabled |
64+
| `*CellularBase::get_default_instance()` | Return cellular connectivity | Requires network parameters (pin, APN, username, password) |
65+
| `*NetworkInterface::get_default_instance()` | One of the above, depending on `target.network-default-interface-type` | |
66+
67+
Note that the calls for a specific interface type do not preconfigure credentials such as SSID because an interface-type-specific application is expected to configure these in code. `NULL` is returned if no interface of that type is available.
68+
69+
Calls for a NetworkInterface will request one of the interface types depending on `target.default-network-interface-type`, and preconfigure the credentials. If credentials can't be preconfigured (for example because `nsapi.default-wifi-ssid` isn't set), the call returns `NULL` rather than an unconfigured interface.
70+
71+
An application may check the type of the interface returned by `NetworkInterface::get_default_instance()` by using the "dynamic downcast" methods:
72+
73+
```
74+
// net set from NetworkInterface::get_default_instance() as above
75+
WiFiInterface *wifi = net->wifiInterface();
76+
if (wifi) {
77+
printf("This is a Wi-Fi board.")
78+
// call WiFi-specific methods
79+
}
80+
```
81+
82+
### Related content
83+
84+
- [Configuring the default network interface](configuration-connectivity.html#selecting-the-default-network-interface).
85+
- [Network connectivity](connectivity-tech.html).
86+
- [IP networking](ip-networking.html).
87+
- [Network status API](network-status.html).
88+
- [Network sockets](network-socket.html).

0 commit comments

Comments
 (0)