js-apis-net-ethernet.md 17.1 KB
Newer Older
S
shawn_he 已提交
1
# # @ohos.net.ethernet (Ethernet Connection Management)
G
Gloria 已提交
2

S
shawn_he 已提交
3
The **ethernet** module provides wired network capabilities, which allow users to set the IP address, subnet mask, gateway, and Domain Name System (DNS) server of a wired network.
G
Gloria 已提交
4

S
shawn_he 已提交
5
> **NOTE**
G
Gloria 已提交
6 7 8 9 10 11 12 13 14 15
> 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.

## Modules to Import

```js
import ethernet from '@ohos.net.ethernet'
```

## ethernet.setIfaceConfig

S
shawn_he 已提交
16
setIfaceConfig(iface: string, ic: InterfaceConfiguration, callback: AsyncCallback\<void>): void
G
Gloria 已提交
17 18 19

Sets the network interface configuration. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
20 21
**System API**: This is a system API.

G
Gloria 已提交
22 23
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
24
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
25 26 27 28 29

**Parameters**

| Name  | Type                                             | Mandatory| Description                                      |
| -------- | ------------------------------------------------- | ---- | ------------------------------------------ |
S
shawn_he 已提交
30
| iface    | string                                            | Yes  | Interface name.                                    |
G
Gloria 已提交
31 32 33
| ic       | [InterfaceConfiguration](#interfaceconfiguration) | Yes  | Network interface configuration to set.                  |
| callback | AsyncCallback\<void>                     | Yes  | Callback used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.|

S
shawn_he 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46
**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |
| 2201006 | Device disconnected.                    |
| 2201007 | Failed to write the user configuration.    |

G
Gloria 已提交
47 48 49
**Example**

```js
S
shawn_he 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
ethernet.setIfaceConfig("eth0", {
    mode: 0,
    ipAddr: "192.168.xx.xxx",
    route: "192.168.xx.xxx",
    gateway: "192.168.xx.xxx",
    netMask: "255.255.255.0",
    dnsServers: "1.1.1.1",
    domain: "2.2.2.2"
}, (error) => {
    if (error) {
        console.log("setIfaceConfig callback error = " + JSON.stringify(error));
    } else {
        console.log("setIfaceConfig callback ok ");
    }
});
G
Gloria 已提交
65 66 67 68
```

## ethernet.setIfaceConfig

S
shawn_he 已提交
69
setIfaceConfig(iface: string, ic: InterfaceConfiguration): Promise\<void>
G
Gloria 已提交
70 71 72

Sets the network interface configuration. This API uses a promise to return the result.

S
shawn_he 已提交
73 74
**System API**: This is a system API.

G
Gloria 已提交
75 76
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
77
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
78 79 80 81 82

**Parameters**

| Name| Type                                             | Mandatory| Description                    |
| ------ | ------------------------------------------------- | ---- | ------------------------ |
S
shawn_he 已提交
83
| iface  | string                                            | Yes  | Interface name.                  |
G
Gloria 已提交
84 85 86 87 88 89
| ic     | [InterfaceConfiguration](#interfaceconfiguration) | Yes  | Network interface configuration to set.|

**Return value**

| Type               | Description                                                       |
| ------------------- | ----------------------------------------------------------- |
S
shawn_he 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103
| Promise\<void>       | Promise used to return the result. If the operation is successful, the return result is empty. If the operation fails, an error code is returned.|

**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |
| 2201006 | Device disconnected.                   |
| 2201007 | Failed to write the user configuration.    |
G
Gloria 已提交
104 105 106 107

**Example**

```js
S
shawn_he 已提交
108 109 110 111 112 113 114 115 116
ethernet.setIfaceConfig("eth0", {
    mode: 0,
    ipAddr: "192.168.xx.xxx",
    route: "192.168.xx.xxx",
    gateway: "192.168.xx.xxx",
    netMask: "255.255.255.0",
    dnsServers: "1.1.1.1",
    domain: "2.2.2.2"
}).then(() => {
G
Gloria 已提交
117
    console.log("setIfaceConfig promiss ok ");
S
shawn_he 已提交
118 119
}).catch(error => {
    console.log("setIfaceConfig promiss error = " + JSON.stringify(error));
G
Gloria 已提交
120 121 122 123 124
});
```

## ethernet.getIfaceConfig

S
shawn_he 已提交
125
getIfaceConfig(iface: string, callback: AsyncCallback\<InterfaceConfiguration>): void
G
Gloria 已提交
126 127 128

Obtains the configuration of a network interface. This API uses an asynchronous callback to return the result. 

S
shawn_he 已提交
129 130
**System API**: This is a system API.

G
Gloria 已提交
131 132
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
133
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
134 135 136 137 138

**Parameters**

| Name  | Type                                           | Mandatory | Description        |
| -------- | ----------------------------------------------- | ----- | ------------ |
S
shawn_he 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151
| iface    | string                                          | Yes   | Interface name.|
| callback | AsyncCallback\<[InterfaceConfiguration](#interfaceconfiguration)> | Yes   | Callback used to return the result.  |

**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |
G
Gloria 已提交
152 153 154 155 156 157

**Example**

```js
ethernet.getIfaceConfig("eth0", (error, value) => {
    if (error) {
S
shawn_he 已提交
158
        console.log("getIfaceConfig  callback error = " + JSON.stringify(error));
G
Gloria 已提交
159
    } else {
S
shawn_he 已提交
160 161 162 163 164 165 166
        console.log("getIfaceConfig callback mode = " + JSON.stringify(value.mode));
        console.log("getIfaceConfig callback ipAddr = " + JSON.stringify(value.ipAddr));
        console.log("getIfaceConfig callback route = " + JSON.stringify(value.route));
        console.log("getIfaceConfig callback gateway = " + JSON.stringify(value.gateway));
        console.log("getIfaceConfig callback netMask = " + JSON.stringify(value.netMask));
        console.log("getIfaceConfig callback dnsServers = " + JSON.stringify(value.dnsServers));
        console.log("getIfaceConfig callback domain = " + JSON.stringify(value.domain));
G
Gloria 已提交
167 168 169 170 171 172
    }
});
```

## ethernet.getIfaceConfig

S
shawn_he 已提交
173
getIfaceConfig(iface: string): Promise\<InterfaceConfiguration>
G
Gloria 已提交
174 175 176

Obtains the configuration of a network interface. This API uses a promise to return the result. 

S
shawn_he 已提交
177 178
**System API**: This is a system API.

G
Gloria 已提交
179 180
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
181
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
182 183 184 185 186

**Parameters**

| Name  | Type                                   | Mandatory| Description        |
| -------- | --------------------------------------- | ---- | ------------ |
S
shawn_he 已提交
187
| iface    | string                                  | Yes  | Interface name.|
G
Gloria 已提交
188 189 190 191 192

**Return value**

| Type                             | Description                              |
| --------------------------------- | ---------------------------------- |
S
shawn_he 已提交
193 194 195 196 197 198 199 200 201 202 203 204
| Promise\<[InterfaceConfiguration](#interfaceconfiguration)>   | Promise used to return the result.       |

**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |
G
Gloria 已提交
205 206 207 208 209

**Example**

```js
ethernet.getIfaceConfig("eth0").then((data) => {
S
shawn_he 已提交
210 211 212 213 214 215 216 217 218
    console.log("getIfaceConfig promiss mode = " + JSON.stringify(data.mode));
    console.log("getIfaceConfig promiss ipAddr = " + JSON.stringify(data.ipAddr));
    console.log("getIfaceConfig promiss route = " + JSON.stringify(data.route));
    console.log("getIfaceConfig promiss gateway = " + JSON.stringify(data.gateway));
    console.log("getIfaceConfig promiss netMask = " + JSON.stringify(data.netMask));
    console.log("getIfaceConfig promiss dnsServers = " + JSON.stringify(data.dnsServers));
    console.log("getIfaceConfig promiss domain = " + JSON.stringify(data.domain));
}).catch(error => {
    console.log("getIfaceConfig promiss error = " + JSON.stringify(error));
G
Gloria 已提交
219 220 221 222 223
});
```

## ethernet.isIfaceActive

S
shawn_he 已提交
224
isIfaceActive(iface: string, callback: AsyncCallback\<number>): void
G
Gloria 已提交
225 226 227

Checks whether a network interface is active. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
228 229
**System API**: This is a system API.

G
Gloria 已提交
230 231
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
232
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
233 234 235 236 237

**Parameters**

| Name  | Type                       | Mandatory| Description                                              |
| -------- | --------------------------- | ---- | -------------------------------------------------- |
S
shawn_he 已提交
238
| iface    | string                      | Yes  | Interface name. If this parameter is left empty, the API checks for any active network interface.            |
G
Gloria 已提交
239 240
| callback | AsyncCallback\<number>       | Yes  | Callback used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.|

S
shawn_he 已提交
241 242 243 244 245 246 247 248 249 250 251
**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |

G
Gloria 已提交
252 253 254 255
**Example**

```js
ethernet.isIfaceActive("eth0", (error, value) => {
S
shawn_he 已提交
256 257 258 259 260
    if (error) {
        console.log("whether2Activate callback error = " + JSON.stringify(error));
    } else {
        console.log("whether2Activate callback = " + JSON.stringify(value));
    }
G
Gloria 已提交
261 262 263 264 265
});
```

## ethernet.isIfaceActive

S
shawn_he 已提交
266
isIfaceActive(iface: string): Promise\<number>
G
Gloria 已提交
267 268 269

Checks whether a network interface is active. This API uses a promise to return the result.

S
shawn_he 已提交
270 271
**System API**: This is a system API.

G
Gloria 已提交
272 273
**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
274
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
275 276 277 278 279

**Parameters**

| Name| Type  | Mandatory| Description                                  |
| ------ | ------ | ---- | -------------------------------------- |
S
shawn_he 已提交
280
| iface  | string | Yes  | Interface name. If this parameter is left empty, the API checks for any active network interface.|
G
Gloria 已提交
281 282 283 284 285 286 287

**Return value**

| Type           | Description                                                              |
| ----------------| ------------------------------------------------------------------ |
| Promise\<number> | Promise used to return the result. The value **1** means that the network interface is active, **0** means that the network interface is inactive, and any other value means that an error has occurred.|

S
shawn_he 已提交
288 289 290 291 292 293 294 295 296 297 298
**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 401     | Parameter error.                        |
| 2200001 | Invalid parameter value.                |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
| 2201005 | The device information does not exist.  |

G
Gloria 已提交
299 300 301 302
**Example**

```js
ethernet.isIfaceActive("eth0").then((data) => {
S
shawn_he 已提交
303 304 305
    console.log("isIfaceActive promiss = " + JSON.stringify(data));
}).catch(error => {
    console.log("isIfaceActive promiss error = " + JSON.stringify(error));
G
Gloria 已提交
306 307 308 309 310
});
```

## ethernet.getAllActiveIfaces

S
shawn_he 已提交
311
getAllActiveIfaces(callback: AsyncCallback\<Array\<string>>): void
G
Gloria 已提交
312

S
shawn_he 已提交
313 314 315
Obtains the list of all active network interfaces. This API uses an asynchronous callback to return the result.

**System API**: This is a system API.
G
Gloria 已提交
316 317 318

**Required permission**: ohos.permission.GET_NETWORK_INFO

S
shawn_he 已提交
319
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
320 321 322 323 324

**Parameters**

| Name  | Type                                | Mandatory| Description                          |
| -------- | ------------------------------------ | ---- | ------------------------------ |
S
shawn_he 已提交
325 326 327 328 329 330 331 332 333
| callback | AsyncCallback\<Array\<string>>         | Yes  | Callback used to return the result.|

**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
G
Gloria 已提交
334 335 336 337 338

**Example**

```js
ethernet.getAllActiveIfaces((error, value) => {
S
shawn_he 已提交
339 340 341 342 343 344 345
    if (error) {
        console.log("getAllActiveIfaces callback error = " + JSON.stringify(error));
    } else {
        console.log("getAllActiveIfaces callback value.length = " + JSON.stringify(value.length));
        for (let i = 0; i < value.length; i++) {
            console.log("getAllActiveIfaces callback = " + JSON.stringify(value[i]));
        }
G
Gloria 已提交
346 347 348 349 350 351
    }
});
```

## ethernet.getAllActiveIfaces

S
shawn_he 已提交
352
getAllActiveIfaces(): Promise\<Array\<string>>
G
Gloria 已提交
353

S
shawn_he 已提交
354
Obtains the list of all active network interfaces. This API uses a promise to return the result.
G
Gloria 已提交
355

S
shawn_he 已提交
356
**System API**: This is a system API.
G
Gloria 已提交
357

S
shawn_he 已提交
358
**Required permission**: ohos.permission.GET_NETWORK_INFO
G
Gloria 已提交
359

S
shawn_he 已提交
360
**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
361 362 363 364 365

**Return value**

| Type                          | Description                                           |
| ------------------------------ | ----------------------------------------------- |
S
shawn_he 已提交
366 367 368 369 370 371 372 373 374
| Promise\<Array\<string>>         | Promise used to return the result.  |

**Error codes**

| ID| Error Message                                |
| ------- | ----------------------------------------|
| 201     | Permission denied.                      |
| 2200002 | Operation failed. Cannot connect to service.|
| 2200003 | System internal error.                  |
G
Gloria 已提交
375 376 377 378 379

**Example**

```js
ethernet.getAllActiveIfaces().then((data) => {
S
shawn_he 已提交
380 381 382 383 384 385
    console.log("getAllActiveIfaces promiss data.length = " + JSON.stringify(data.length));
    for (let i = 0; i < data.length; i++) {
        console.log("getAllActiveIfaces promiss  = " + JSON.stringify(data[i]));
    }
}).catch(error => {
    console.log("getAllActiveIfaces promiss error = " + JSON.stringify(error));
G
Gloria 已提交
386 387 388 389 390 391 392
});
```

## InterfaceConfiguration

Defines the network configuration for the Ethernet connection.

S
shawn_he 已提交
393
**System API**: This is a system API.
G
Gloria 已提交
394

S
shawn_he 已提交
395 396 397 398 399 400 401 402 403 404
**System capability**: SystemCapability.Communication.NetManager.Ethernet

| Name         | Type                   | Mandatory| Description                                                        |
| ------------ | ----------------------- | ---|------------------------------------------------------------ |
| mode         | [IPSetMode](#ipsetmode) | Yes| Configuration mode of the Ethernet connection.|
| ipAddr       | string                  | Yes| Static IP address of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in Dynamic Host Configuration Protocol (DHCP) mode.|
| route        | string                  | Yes| Route of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.|
| gateway      | string                  | Yes| Gateway of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.|
| netMask      | string                  | Yes| Subnet mask of the Ethernet connection. The value must be an IPv4 address, which is a 32-bit number displayed in dotted decimal notation and each 8-bit field ranges from 0 to 255. This parameter does not need to be configured in DHCP mode.|
| dnsServers   | string                  | Yes| DNS server addresses of the Ethernet connection. The value must be an IPv4 address. This parameter does not need to be configured in DHCP mode. Multiple addresses are separated by commas (,).|
G
Gloria 已提交
405 406 407 408 409

## IPSetMode

Defines the configuration mode of the Ethernet connection.

S
shawn_he 已提交
410 411 412
**System API**: This is a system API.

**System capability**: SystemCapability.Communication.NetManager.Ethernet
G
Gloria 已提交
413 414 415 416 417

| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| STATIC | 0 | Static configuration.|
| DHCP   | 1 | Dynamic configuration.|