• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Ethernet Connection
2
3## Overview
4
5The Ethernet Connection module allows a device to access the Internet through a network cable. After a device is connected to the Ethernet through a network cable, the device can obtain a series of network attributes, such as the dynamically allocated IP address, subnet mask, gateway, and DNS. You can manually configure and obtain the network attributes of the device in static mode.
6
7> **NOTE**
8> To maximize the application running efficiency, most API calls are called asynchronously in callback or promise mode. The following code examples use the callback mode. For details about the APIs, see [sms API Reference](../reference/apis/js-apis-net-ethernet.md).
9
10## **Constraints**
11
12- Programming language: JS
13- System: Linux kernel
14- The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.
15
16## Scenario
17
18Typical application scenarios of Ethernet connection are as follows:
19
20- Dynamically assigning a series of network attributes, such as the IP address, subnet mask, gateway, and DNS in DHCP mode to enable network access
21- Configuring a series of network attributes, such as the IP address, subnet mask, gateway, and DNS, in static mode to enable network access.
22
23The following describes the development procedure specific to each application scenario.
24
25## Available APIs
26
27For the complete list of APIs and example code, see [Ethernet Connection](../reference/apis/js-apis-net-ethernet.md).
28
29| Type| API| Description|
30| ---- | ---- | ---- |
31| setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void | Configures the network attributes of the specified Ethernet network. This API uses an asynchronous callback to return the result.|
32| getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void | Obtains the network attributes of the specified Ethernet network. This API uses an asynchronous callback to return the result.|
33| isIfaceActive(iface: string, callback: AsyncCallback\<number>): void | Checks whether the specified network port is active. This API uses an asynchronous callback to return the result.|
34| getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void; | Obtains the list of all active network ports. This API uses an asynchronous callback to return the result.|
35| on(type: 'interfaceStateChange', callback: Callback\<{ iface: string, active: boolean }\>): void; | Subscribes to interface state change events.|
36| off(type: 'interfaceStateChange', callback?: Callback\<{ iface: string, active: boolean }\>): void; | Unsubscribes from interface state change events.|
37
38## Ethernet Connection – DHCP Mode
39
401. Use a network cable to connect the device to a network port.
412. Import the **ethernet** namespace from **@ohos.net.ethernet**.
423. Call **getAllActiveIfaces** to obtain the list of all active network ports, for example, **eth0** and **eth1**.
434. Call **isIfaceActive** in user mode to check whether the **eth0** port is active.
445. Call **getIfaceConfig** in user mode to obtain the static network attributes of the **eth0** port. By default, an unconfigured Ethernet network uses the DHCP mode, in which the Ethernet network obtains the automatically assigned network attributes.
45
46```ts
47// Import the ethernet namespace from @ohos.net.ethernet.
48import ethernet from '@ohos.net.ethernet';
49import { BusinessError } from '@ohos.base';
50
51// Call getAllActiveIfaces to obtain the list of all active network ports.
52ethernet.getAllActiveIfaces((error: BusinessError, data: string[]) => {
53  if (error) {
54    console.log("getAllActiveIfaces callback error = " + error);
55  } else {
56    console.log("getAllActiveIfaces callback data.length = " + data.length);
57    for (let i = 0; i < data.length; i++) {
58      console.log("getAllActiveIfaces callback = " + data[i]);
59    }
60  }
61});
62
63// Call isIfaceActive to check whether the specified network port is active.
64ethernet.isIfaceActive("eth0", (error: BusinessError, data: number) => {
65  if (error) {
66    console.log("isIfaceActive callback error = " + error);
67  } else {
68    console.log("isIfaceActive callback = " + data);
69  }
70});
71
72// Call getIfaceConfig to obtain the network attributes of the specified Ethernet network.
73ethernet.getIfaceConfig("eth0", (error: BusinessError, data: ethernet.InterfaceConfiguration) => {
74  if (error) {
75    console.log("getIfaceConfig  callback error = " + error);
76  } else {
77    console.log("getIfaceConfig callback mode = " + data.mode);
78    console.log("getIfaceConfig callback ipAddr = " + data.ipAddr);
79    console.log("getIfaceConfig callback routeAddr = " + data.route);
80    console.log("getIfaceConfig callback gateAddr = " + data.gateway);
81    console.log("getIfaceConfig callback maskAddr = " + data.netMask);
82    console.log("getIfaceConfig callback dns0Addr = " + data.dnsServers);
83  }
84});
85```
86
87## Ethernet Connection – Static Mode
88
89### How to Develop
90
911. Use a network cable to connect the device to a network port.
922. Import the **ethernet** namespace from **@ohos.net.ethernet**.
933. Call **getAllActiveIfaces** in user mode to obtain the list of all active network ports, for example, **eth0** and **eth1**.
944. Call **isIfaceActive** in user mode to check whether the **eth0** port is active.
955. Call **setIfaceConfig** in user mode to set the **eth0** port to the static mode, in which you need to manually assign the network attributes (including the IP address, subnet mask, gateway, and DNS).
966. Call **getIfaceConfig** in user mode to obtain the static network attributes of the **eth0** port.
97
98```ts
99// Import the ethernet namespace from @ohos.net.ethernet.
100import ethernet from '@ohos.net.ethernet';
101import { BusinessError } from '@ohos.base';
102
103// Call getAllActiveIfaces to obtain the list of all active network ports.
104ethernet.getAllActiveIfaces((error: BusinessError, data: string[]) => {
105  if (error) {
106    console.log("getAllActiveIfaces callback error = " + error);
107  } else {
108    console.log("getAllActiveIfaces callback data.length = " + data.length);
109    for (let i = 0; i < data.length; i++) {
110      console.log("getAllActiveIfaces callback = " + data[i]);
111    }
112  }
113});
114
115// Call isIfaceActive to check whether the specified network port is active.
116ethernet.isIfaceActive("eth0", (error: BusinessError, data: number) => {
117  if (error) {
118    console.log("isIfaceActive callback error = " + error);
119  } else {
120    console.log("isIfaceActive callback = " + data);
121  }
122});
123
124let ethernetParam: ethernet.InterfaceConfiguration = {
125  mode: ethernet.IPSetMode.STATIC,
126  ipAddr: "192.168.xx.xx",
127  route: "192.168.xx.xx",
128  gateway: "192.168.xx.xx",
129  netMask: "255.255.xx.xx",
130  dnsServers: "1.1.xx.xx"
131}
132
133// Call setIfaceConfig to configure the network attributes of the specified Ethernet network.
134ethernet.setIfaceConfig("eth0", ethernetParam, (error: BusinessError) => {
135  if (error) {
136    console.log("setIfaceConfig callback error = " + error);
137  } else {
138    console.log("setIfaceConfig callback ok ");
139  }
140});
141
142// Call getIfaceConfig to obtain the network attributes of the specified Ethernet network.
143ethernet.getIfaceConfig("eth0", (error: BusinessError, data: ethernet.InterfaceConfiguration) => {
144  if (error) {
145    console.log("getIfaceConfig  callback error = " + error);
146  } else {
147    console.log("getIfaceConfig callback mode = " + data.mode);
148    console.log("getIfaceConfig callback ipAddr = " + data.ipAddr);
149    console.log("getIfaceConfig callback routeAddr = " + data.route);
150    console.log("getIfaceConfig callback gateAddr = " + data.gateway);
151    console.log("getIfaceConfig callback maskAddr = " + data.netMask);
152    console.log("getIfaceConfig callback dns0Addr = " + data.dnsServers);
153  }
154});
155```
156
157## Subscribes the status change of network device interfaces.
158
159### How to Develop
160
1611. Import the **ethernet** namespace from **@ohos.net.ethernet**.
1622. Call the **on()** method to subscribe to **interfaceStateChange** events. It is up to you whether to listen for **interfaceStateChange** events.
1633. Check whether an **interfaceStateChange** event is triggered when the interface state changes.
1644. Call the **off()** method to unsubscribe from **interfaceStateChange** events.
165
166```ts
167// Import the ethernet namespace from @ohos.net.ethernet.
168import ethernet from '@ohos.net.ethernet';
169
170// Subscribe to interfaceStateChange events.
171class EthernetData{
172  iface: string = ""
173  active: boolean = false
174}
175
176ethernet.on('interfaceStateChange', (data: EthernetData) => {
177  console.log(JSON.stringify(data));
178});
179
180// Unsubscribe from interfaceStateChange events.
181ethernet.off('interfaceStateChange');
182```
183