USB Driver Development Kit (USB DDK) is a tool kit provided by OpenHarmony to help you develop USB device drivers for your applications based on the user mode. It provides a series of device access APIs, which implement functions such as opening and closing USB interfaces, performing non-isochronous and isochronous data transfer, and implementing control transfer and interrupt transfer over USB pipes, etc.
| OH_Usb_GetConfigDescriptor(uint64_t deviceId, uint8_t configIndex, struct UsbDdkConfigDescriptor **const config) | Obtains the configuration descriptor. To avoid memory leakage, use **OH_Usb_FreeConfigDescriptor()** to release a descriptor after use.|
| OH_Usb_FreeConfigDescriptor(const struct UsbDdkConfigDescriptor *const config) | Releases a configuration descriptor. To avoid memory leakage, release a descriptor after use.|
| OH_Usb_ClaimInterface(uint64_t deviceId, uint8_t interfaceIndex, uint64_t *interfaceHandle) | Declares a USB interface.|
| OH_Usb_ReleaseInterface(uint64_t interfaceHandle) | Releases a USB interface.|
| OH_Usb_SendPipeRequest(const struct UsbRequestPipe *pipe, UsbDeviceMemMap *devMmap) | Sends a pipe request. This API works in a synchronous manner. It applies to interrupt transfer and bulk transfer.|
| OH_Usb_CreateDeviceMemMap(uint64_t deviceId, size_t size, UsbDeviceMemMap **devMmap) | Creates a buffer. To avoid memory leakage, use **OH_Usb_DestroyDeviceMemMap()** to destroy a buffer after use.|
| OH_Usb_DestroyDeviceMemMap(UsbDeviceMemMap *devMmap) | Destroys a buffer. To avoid resource leakage, destroy a buffer in time after use.|
For details about the APIs, see [USB DDK](../reference/native-apis/_usb_ddk.md).
## How to Develop
To develop a USB driver using the USB DDK in OpenHarmony, perform the following steps:
1. Obtain the device descriptor. Initialize the DDK by calling **OH_Usb_Init** of **usb_ddk_api.h**, and obtain the device descriptor by calling **OH_Usb_GetDeviceDescriptor**.
```c++
// Initialize the USB DDK.
OH_Usb_Init();
struct UsbDeviceDescriptor devDesc;
uint64_t deviceId = 0;
// Obtain the device descriptor.
OH_Usb_GetDeviceDescriptor(deviceId, &devDesc);
```
2. Obtain the configuration descriptor, and declare the USB interface. Obtain the configuration descriptor **config** by calling **OH_Usb_GetConfigDescriptor** of **usb_ddk_api.h**, and declare the USB interface by calling **OH_Usb_ClaimInterface**.
```c++
struct UsbDdkConfigDescriptor *config = nullptr;
// Obtain the configuration descriptor.
OH_Usb_GetConfigDescriptor(deviceId, 1, &config);
// Obtain the index of the target USB interface based on the configuration descriptor.
3. Create a device memory map, and send a request. Create the device memory map **devMmap** by calling **OH_Usb_CreateDeviceMemMap** of **usb_ddk_api.h**, and send a request by calling **OH_Usb_SendPipeRequest**.
// Obtain the target endpoint based on the configuration descriptor.
pipe.endpoint = 128;
pipe.timeout = UINT32_MAX;
// Send a request.
OH_Usb_SendPipeRequest(&pipe, devMmap);
```
4. Release resources. After all requests are processed and before the application exits, destroy the buffer by calling **OH_Usb_DestroyDeviceMemMap** of **usb_ddk_api.h** to release resources. Release the USB interface by calling **OH_Usb_ReleaseInterface**. Release the USB DDK by calling **OH_Usb_Release**.
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.
The **ethernet** module provides wired network capabilities, which allow users to set the IP address, subnet mask, gateway, and Domain Name System (DNS) server, and HTTP proxy of a wired network.
> **NOTE**
> **NOTE**
> 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.
> 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.
@@ -476,6 +486,7 @@ Defines the network configuration for the Ethernet connection.
...
@@ -476,6 +486,7 @@ Defines the network configuration for the Ethernet connection.
| 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.|
| 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.|
| 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 (,).|
| 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 (,).|
| httpProxy<sup>10+</sup> | [HttpProxy](js-apis-net-connection.md#httpproxy) | No| HTTP proxy of the Ethernet connection. By default, no proxy is configured.|
The **statistics** module provides APIs to query real-time or historical data traffic by the specified network interface card (NIC) or user ID (UID).
> **NOTE**
>
> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the NIC in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service. |
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the NIC in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service. |
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the cellular network in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
| 2103005 | Failed to read map. |
| 2103011 | Failed to create map. |
| 2103012 | Get iface name failed. |
**Example**
```js
statistics.getCellularRxBytes((error,stats)=>{
console.log(JSON.stringify(error))
console.log(JSON.stringify(stats))
})
```
## statistics.getCellularRxBytes<sup>10+</sup>
getCellularRxBytes(): Promise\<number>;
Obtains the real-time downlink data traffic of a cellular network. This API uses a promise to return the result.
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the cellular network in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
| 2103005 | Failed to read map. |
| 2103011 | Failed to create map. |
| 2103012 | Get iface name failed. |
**Example**
```js
statistics.getCellularTxBytes((error,stats)=>{
console.log(JSON.stringify(error))
console.log(JSON.stringify(stats))
})
```
## statistics.getCellularTxBytes<sup>10+</sup>
getCellularTxBytes(): Promise\<number>;
Obtains the real-time uplink data traffic of a cellular network. This API uses a promise to return the result.
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of all NICs in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
| 2103005 | Failed to read map. |
| 2103011 | Failed to create map. |
**Example**
```js
statistics.getAllRxBytes((error,stats)=>{
console.log(JSON.stringify(error))
console.log(JSON.stringify(stats))
})
```
## statistics.getAllRxBytes<sup>10+</sup>
getAllRxBytes(): Promise\<number>;
Obtains the real-time downlink data traffic of all NICs. This API uses a promise to return the result.
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of all NICs in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 2100002 | Operation failed. Cannot connect to service. |
| 2100003 | System internal error. |
| 2103005 | Failed to read map. |
| 2103011 | Failed to create map. |
**Example**
```js
statistics.getAllTxBytes((error,stats)=>{
console.log(JSON.stringify(error))
console.log(JSON.stringify(stats))
})
```
## statistics.getAllTxBytes<sup>10+</sup>
getAllTxBytes(): Promise\<number>;
Obtains the real-time uplink data traffic of all NICs. This API uses a promise to return the result.
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time downlink data traffic of the application in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service. |
| callback | AsyncCallback\<number> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **stats** is the real-time uplink data traffic of the application in bytes. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 401 | Parameter error. |
| 2100002 | Operation failed. Cannot connect to service. |
| type | string | Yes | Event type. This field has a fixed value of **netStatsChange**.|
| callback | Callback\<{ iface: string, uid?: number }\> | Yes | Callback invoked when the data traffic changes.<br>**iface**: NIC name.<br>**uid**: application UID.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| type | string | Yes | Event type. This field has a fixed value of **netStatsChange**.|
| callback | Callback\<{ iface: string, uid?: number }\> | No | Callback invoked when the data traffic changes.<br>**iface**: NIC name.<br>uid: application UID.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
| ifaceInfo | [IfaceInfo](#ifaceinfo10) | Yes | NIC information. For details, see [IfaceInfo](#ifaceinfo10). |
| callback | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **statsInfo** is the historical data traffic of the NIC. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service. |
| uidInfo | [UidInfo](#uidinfo10) | Yes | Application information. For details, see [UidInfo](#uidinfo10). |
| callback | AsyncCallback\<[NetStatsInfo](#netstatsinfo10)> | Yes | Callback used to return the result. If the operation is successful, **error** is **undefined** and **statsInfo** is the historical data traffic of the application. Otherwise, **error** is an error object.|
**Error codes**
For details about the error codes, see [Traffic Management Error Codes](../errorcodes/errorcode-net-statistics.md).
| ID| Error Message |
| ------- | ----------------------------- |
| 201 | Permission denied. |
| 202 | Non-system applications use system APIs. |
| 401 | Parameter error. |
| 2100001 | Invalid parameter value. |
| 2100002 | Operation failed. Cannot connect to service. |
| data | [CdmaCellInformation](#cdmacellinformation8)\|[GsmCellInformation](#gsmcellinformation8)\|[LteCellInformation](#ltecellinformation8)\|[NrCellInformation](#nrcellinformation8)\|[TdscdmaCellInformation](#tdscdmacellinformation8) | Yes | CDMA cell information\|GSM cell information\|LTE cell information\|NR cell information\|TD-SCDMA cell information<br>**System API**: This is a system API.|
The **resourceManager** module provides APIs to obtain information about application resources based on the current configuration, including the language, region, screen direction, MCC/MNC, as well as device capability and density.
The **resourceManager** module provides APIs to obtain information about application resources based on the current configuration, including the language, region, screen direction, MCC/MNC, as well as device capability and density.
...
@@ -3506,7 +3506,7 @@ For details about the error codes, see [Resource Manager Error Codes](../errorco
...
@@ -3506,7 +3506,7 @@ For details about the error codes, see [Resource Manager Error Codes](../errorco
@@ -34,7 +34,7 @@ let udp = socket.constructUDPSocketInstance();
...
@@ -34,7 +34,7 @@ let udp = socket.constructUDPSocketInstance();
## UDPSocket<sup>7+</sup>
## UDPSocket<sup>7+</sup>
Defines a **UDPSocket** connection. Before invoking UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object.
Defines a UDPSocket connection. Before calling UDPSocket APIs, you need to call [socket.constructUDPSocketInstance](#socketconstructudpsocketinstance) to create a **UDPSocket** object.
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('message',callback);
udp.off('message',callback);
udp.off('message');
udp.off('message');
```
```
...
@@ -541,7 +541,7 @@ Enables listening for data packet message events or close events of the UDPSocke
...
@@ -541,7 +541,7 @@ Enables listening for data packet message events or close events of the UDPSocke
| type | string | Yes | Type of the event to subscribe to.<br>- **listening**: data packet message event<br>- **close**: close event|
| type | string | Yes | Type of the event to subscribe to.<br>- **listening**: data packet message event.<br>- **close**: close event.|
| callback | Callback\<void\> | No | Callback used to return the result. |
| callback | Callback\<void\> | No | Callback used to return the result. |
**Example**
**Example**
...
@@ -582,14 +582,14 @@ let callback1 = () => {
...
@@ -582,14 +582,14 @@ let callback1 = () => {
console.log("on listening, success");
console.log("on listening, success");
}
}
udp.on('listening',callback1);
udp.on('listening',callback1);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('listening',callback1);
udp.off('listening',callback1);
udp.off('listening');
udp.off('listening');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close, success");
console.log("on close, success");
}
}
udp.on('close',callback2);
udp.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('close',callback2);
udp.off('close',callback2);
udp.off('close');
udp.off('close');
```
```
...
@@ -606,7 +606,7 @@ Enables listening for error events of the UDPSocket connection. This API uses an
...
@@ -606,7 +606,7 @@ Enables listening for error events of the UDPSocket connection. This API uses an
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
udp.off('error',callback);
udp.off('error',callback);
udp.off('error');
udp.off('error');
```
```
...
@@ -739,7 +739,7 @@ let tcp = socket.constructTCPSocketInstance();
...
@@ -739,7 +739,7 @@ let tcp = socket.constructTCPSocketInstance();
## TCPSocket<sup>7+</sup>
## TCPSocket<sup>7+</sup>
Defines a TCPSocket connection. Before invoking TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance) to create a **TCPSocket** object.
Defines a TCPSocket connection. Before calling TCPSocket APIs, you need to call [socket.constructTCPSocketInstance](#socketconstructtcpsocketinstance) to create a **TCPSocket** object.
### bind<sup>7+</sup>
### bind<sup>7+</sup>
...
@@ -1358,7 +1358,7 @@ Enables listening for message receiving events of the TCPSocket connection. This
...
@@ -1358,7 +1358,7 @@ Enables listening for message receiving events of the TCPSocket connection. This
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('message',callback);
tcp.off('message',callback);
tcp.off('message');
tcp.off('message');
```
```
...
@@ -1427,7 +1427,7 @@ Enables listening for connection or close events of the TCPSocket connection. Th
...
@@ -1427,7 +1427,7 @@ Enables listening for connection or close events of the TCPSocket connection. Th
| type | string | Yes | Type of the event to subscribe to.<br>- **connect**: connection event<br>- **close**: close event|
| type | string | Yes | Type of the event to subscribe to.<br>- **connect**: connection event.<br>- **close**: close event.|
| callback | Callback\<void\> | No | Callback used to return the result. |
| callback | Callback\<void\> | No | Callback used to return the result. |
**Example**
**Example**
...
@@ -1468,14 +1468,14 @@ let callback1 = () => {
...
@@ -1468,14 +1468,14 @@ let callback1 = () => {
console.log("on connect success");
console.log("on connect success");
}
}
tcp.on('connect',callback1);
tcp.on('connect',callback1);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('connect',callback1);
tcp.off('connect',callback1);
tcp.off('connect');
tcp.off('connect');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close success");
console.log("on close success");
}
}
tcp.on('close',callback2);
tcp.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('close',callback2);
tcp.off('close',callback2);
tcp.off('close');
tcp.off('close');
```
```
...
@@ -1492,7 +1492,7 @@ Enables listening for error events of the TCPSocket connection. This API uses an
...
@@ -1492,7 +1492,7 @@ Enables listening for error events of the TCPSocket connection. This API uses an
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcp.off('error',callback);
tcp.off('error',callback);
tcp.off('error');
tcp.off('error');
```
```
...
@@ -1596,7 +1596,7 @@ let tcpServer = socket.constructTCPSocketServerInstance();
...
@@ -1596,7 +1596,7 @@ let tcpServer = socket.constructTCPSocketServerInstance();
## TCPSocketServer<sup>10+</sup>
## TCPSocketServer<sup>10+</sup>
Defines a TCPSocketServer connection. Before invoking TCPSocketServer APIs, you need to call [socket.constructTCPSocketServerInstance](#socketconstructtcpsocketserverinstance10) to create a **TCPSocketServer** object.
Defines a TCPSocketServer connection. Before calling TCPSocketServer APIs, you need to call [socket.constructTCPSocketServerInstance](#socketconstructtcpsocketserverinstance10) to create a **TCPSocketServer** object.
### listen<sup>10+</sup>
### listen<sup>10+</sup>
...
@@ -1916,7 +1916,7 @@ Enables listening for TCPSocketServer connection events. This API uses an asynch
...
@@ -1916,7 +1916,7 @@ Enables listening for TCPSocketServer connection events. This API uses an asynch
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcpServer.off('connect',callback);
tcpServer.off('connect',callback);
tcpServer.off('connect');
tcpServer.off('connect');
```
```
...
@@ -1983,7 +1983,7 @@ Enables listening for error events of the TCPSocketServer connection. This API u
...
@@ -1983,7 +1983,7 @@ Enables listening for error events of the TCPSocketServer connection. This API u
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tcpServer.off('error',callback);
tcpServer.off('error',callback);
tcpServer.off('error');
tcpServer.off('error');
```
```
## TCPSocketConnection<sup>10+</sup>
## TCPSocketConnection<sup>10+</sup>
Defines a **TCPSocketConnection** object, that is, the connection between the TCPSocket client and the server. Before invoking TCPSocketConnection APIs, you need to obtain a **TCPSocketConnection** object.
Defines a **TCPSocketConnection** object, that is, the connection between the TCPSocket client and the server. Before calling TCPSocketConnection APIs, you need to obtain a **TCPSocketConnection** object.
> **NOTE**
> The TCPSocket client can call related APIs through the **TCPSocketConnection** object only after a connection is successfully established between the TCPSocket client and the server.
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event|
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo7)}> | Yes | Callback used to return the result.<br>**message**: received message.<br>**remoteInfo**: socket connection information. |
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo7)}> | Yes | Callback used to return the result.<br>**message**: received message.<br>**remoteInfo**: socket connection information. |
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event|
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo7)}> | No | Callback used to return the result. **message**: received message.<br>**remoteInfo**: socket connection information. |
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo7)}> | No | Callback used to return the result.<br>**message**: received message.<br>**remoteInfo**: socket connection information. |
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
client.off('message',callback);
client.off('message',callback);
client.off('message');
client.off('message');
});
});
...
@@ -2397,7 +2400,7 @@ Enables listening for **close** events of a **TCPSocketConnection** object. This
...
@@ -2397,7 +2400,7 @@ Enables listening for **close** events of a **TCPSocketConnection** object. This
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
client.off('close',callback);
client.off('close',callback);
client.off('close');
client.off('close');
});
});
...
@@ -2468,7 +2471,7 @@ Enables listening for **error** events of a **TCPSocketConnection** object. This
...
@@ -2468,7 +2471,7 @@ Enables listening for **error** events of a **TCPSocketConnection** object. This
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
client.off('error',callback);
client.off('error',callback);
client.off('error');
client.off('error');
});
});
...
@@ -2555,7 +2558,7 @@ let tls = socket.constructTLSSocketInstance();
...
@@ -2555,7 +2558,7 @@ let tls = socket.constructTLSSocketInstance();
## TLSSocket<sup>9+</sup>
## TLSSocket<sup>9+</sup>
Defines a TLSSocket connection. Before invoking TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object.
Defines a TLSSocket connection. Before calling TLSSocket APIs, you need to call [socket.constructTLSSocketInstance](#socketconstructtlssocketinstance9) to create a **TLSSocket** object.
Sets other properties of the TCPSocket connection after successful binding of the local IP address and port number of the connection. This API uses an asynchronous callback to return the result.
Sets other properties of the TCPSocket connection after **bind** is successfully called. This API uses an asynchronous callback to return the result.
Sets other properties of the TCPSocket connection after successful binding of the local IP address and port number of the connection. This API uses a promise to return the result.
Sets other properties of the TCPSocket connection after **bind** is successfully called. This API uses a promise to return the result.
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event|
| type | string | Yes | Type of the event to subscribe to.<br/>**message**: message receiving event.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo)}> | No | Callback used to return the result. **message**: received message.<br>**remoteInfo**: socket connection information.|
| callback | Callback<{message:ArrayBuffer,remoteInfo:[SocketRemoteInfo](#socketremoteinfo)}> | No | Callback used to return the result.<br/>**message**: received message.<br>**remoteInfo**: socket connection information.|
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for connection or close events of the TLSSocket connection. This API uses an asynchronous callback to return the result.
Disables listening for **connect** or **close** events of the TLSSocket connection. This API uses an asynchronous callback to return the result.
> **NOTE**
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
...
@@ -2931,7 +2934,7 @@ Disables listening for connection or close events of the TLSSocket connection. T
...
@@ -2931,7 +2934,7 @@ Disables listening for connection or close events of the TLSSocket connection. T
| type | string | Yes | Type of the event to subscribe to.<br>- **connect**: connection event<br>- **close**: close event|
| type | string | Yes | Type of the event to subscribe to.<br>- **connect**: connection event.<br>- **close**: close event.|
| callback | Callback\<void\> | No | Callback used to return the result. |
| callback | Callback\<void\> | No | Callback used to return the result. |
**Example**
**Example**
...
@@ -2942,14 +2945,14 @@ let callback1 = () => {
...
@@ -2942,14 +2945,14 @@ let callback1 = () => {
console.log("on connect success");
console.log("on connect success");
}
}
tls.on('connect',callback1);
tls.on('connect',callback1);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tls.off('connect',callback1);
tls.off('connect',callback1);
tls.off('connect');
tls.off('connect');
letcallback2=()=>{
letcallback2=()=>{
console.log("on close success");
console.log("on close success");
}
}
tls.on('close',callback2);
tls.on('close',callback2);
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tls.off('close',callback2);
tls.off('close',callback2);
```
```
...
@@ -2965,7 +2968,7 @@ Enables listening for error events of the TLSSocket connection. This API uses an
...
@@ -2965,7 +2968,7 @@ Enables listening for error events of the TLSSocket connection. This API uses an
// You can pass the callback of the on method to cancel listening for a certain type of callback. If you do not pass the callback, you will cancel listening for all callbacks.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Sets up a TLSSocket connection, and creates and initializes a TLS session after successful binding of the local IP address and port number of the TLSSocket connection. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.
Sets up a TLSSocket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. This API uses an asynchronous callback to return the result.
Sets up a TLSSocket connection, and creates and initializes a TLS session after successful binding of the local IP address and port number of the TLSSocket connection. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. Both two-way and one-way authentication modes are supported. This API uses a promise to return the result.
Sets up a TLSSocket connection, and creates and initializes a TLS session after **bind** is successfully called. During this process, a TLS/SSL handshake is performed between the application and the server to implement data transmission. Both two-way and one-way authentication modes are supported. This API uses a promise to return the result.
Defines a TLSSocketServer connection. Before calling TLSSocketServer APIs, you need to call [socket.constructTLSSocketServerInstance](#socketconstructtlssocketserverinstance10) to create a **TLSSocketServer** object.
Listens to client connections after **bind** is successfully called. This API uses an asynchronous callback to return the result. After a connection is established, a TLS session will be created and initialized and a certificate key will be loaded and verified.
| options | [TLSConnectOptions](#tlsconnectoptions9) | Yes | Parameters required for the connection. |
| callback | AsyncCallback\<void> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
Listens to client connections after **bind** is successfully called. This API uses a promise to return the result. After a connection is established, a TLS session will be created and initialized and a certificate key will be loaded and verified.
| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<[SocketStateBase](#socketstatebase7)> | Yes | Callback used to return the result. If the operation is successful, the status of the TLSSocketServer connection is returned. If the operation fails, an error message is returned.|
| options | [TCPExtraOptions](#tcpextraoptions7) | Yes | Other properties of the TLSSocketServer connection. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes | Callback used to return the result. If the operation is successful, the local certificate is returned. If the operation fails, an error message is returned.|
Obtains the communication protocol version after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
> **NOTE**
> This API can be called only after **listen** is successfully called.
Disables listening for TLSSocketServer connection events. This API uses an asynchronous callback to return the result.
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tlsServer.off('connect',callback);
tlsServer.off('connect');
```
### on('error')
on(type: 'error', callback: ErrorCallback): void
Enables listening for **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> This API can be called only after **listen** is successfully called.
Disables listening for **error** events of a **TLSSocketServer** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
tlsServer.off('error',callback);
tlsServer.off('error');
```
## TLSSocketConnection<sup>10+</sup>
Defines a **TLSSocketConnection** object, that is, the connection between the TLSSocket client and the server. Before calling TLSSocketConnection APIs, you need to obtain a **TLSSocketConnection** object.
> **NOTE**
> The TLSSocket client can call related APIs through the **TLSSocketConnection** object only after a connection is successfully established between the TLSSocket client and the server.
| data | string | Yes | Parameters for sending data over the TLSSocketServer connection. |
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<void\> | Yes | Callback used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned.|
| Promise\<void\> | Promise used to return the result. If the operation is successful, no value is returned. If the operation fails, an error message is returned. If the operation fails, an error message is returned.|
| callback | AsyncCallback\<[NetAddress](#netaddress7)\> | Yes | Callback used to return the result. If the operation is successful, the remote address is returned. If the operation fails, an error message is returned.|
Obtains the digital certificate of the peer end after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result. It applies only to the scenario where the client sends a certificate to the server.
| callback | AsyncCallback\<[X509CertRawData](#x509certrawdata9)\> | Yes | Callback used to return the result. If the operation fails, an error message is returned.|
Obtains the digital certificate of the peer end after a TLSSocketServer connection is established. This API uses a promise to return the result. It applies only to the scenario where the client sends a certificate to the server.
Obtains the cipher suite negotiated by both communication parties after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the cipher suite negotiated by both communication parties after a TLSSocketServer connection is established. This API uses a promise to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocketServer connection is established. This API uses an asynchronous callback to return the result.
Obtains the signing algorithm negotiated by both communication parties after a TLSSocketServer connection is established. This API uses a promise to return the result.
Disables listening for **message** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
Disables listening for **close** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.
client.off('close',callback);
client.off('close');
});
```
### on('error')<sup>10+</sup>
on(type: 'error', callback: ErrorCallback): void
Enables listening for **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
Disables listening for **error** events of a **TLSSocketConnection** object. This API uses an asynchronous callback to return the result.
> **NOTE**
> You can pass the callback of the **on** function if you want to cancel listening for a certain type of event. If you do not pass the callback, you will cancel listening for all events.
// You can pass the callback of the on method to cancel listening for a certain type of events. If you do not pass the callback, you will cancel listening for all events.