From f13a1598407077f4efba67f67c19eb8fedfd0514 Mon Sep 17 00:00:00 2001 From: luo-wei246 Date: Tue, 28 Mar 2023 16:18:48 +0800 Subject: [PATCH] fix:JS file format changes Signed-off-by: luo-wei246 --- .../src/main/ets/MainAbility/MainAbility.ts | 64 +- .../ets/ServiceExtAbility/UsbDialogAbility.ts | 122 +-- interfaces/kits/js/@ohos.usb.d.ts | 890 ---------------- interfaces/kits/js/@ohos.usbManager.d.ts | 980 ++++++++++++++++++ services/native/src/usb_device_manager.cpp | 2 +- 5 files changed, 1074 insertions(+), 984 deletions(-) delete mode 100644 interfaces/kits/js/@ohos.usb.d.ts create mode 100644 interfaces/kits/js/@ohos.usbManager.d.ts diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/MainAbility/MainAbility.ts b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/MainAbility/MainAbility.ts index 26d4de59..cd9b5a85 100644 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/MainAbility/MainAbility.ts +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/MainAbility/MainAbility.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -15,37 +15,37 @@ import Ability from '@ohos.app.ability.UIAbility'; -var TAG = "UsbRightDialog_MainAbility:"; +let TAG = 'UsbRightDialog_MainAbility:'; export default class MainAbility extends Ability { - onCreate(want, launchParam) { - console.log(TAG + "MainAbility onCreate, ability name is " + want.abilityName + "."); - globalThis.context = this.context; - } - - onWindowStageCreate(windowStage) { - // Main window is created, set main page for this ability - console.log(TAG + "MainAbility onWindowStageCreate."); - - windowStage.setUIContent(this.context, "pages/UsbDialog", null); - } - - onForeground() { - // Ability has brought to foreground - console.log(TAG + "MainAbility onForeground."); - } - - onBackground() { - // Ability has back to background - console.log(TAG + "MainAbility onBackground."); - } - - onDestroy() { - console.log(TAG + "MainAbility onDestroy."); - } - - onWindowStageDestroy() { - // Main window is destroyed, release UI related resources - console.log(TAG + "MainAbility onWindowStageDestroy."); - } + onCreate(want, launchParam): void { + console.log(TAG + "MainAbility onCreate, ability name is " + want.abilityName + "."); + globalThis.context = this.context; + } + + onWindowStageCreate(windowStage): void { + // Main window is created, set main page for this ability + console.log(TAG + "MainAbility onWindowStageCreate."); + + windowStage.setUIContent(this.context, 'pages/UsbDialog', null); + } + + onForeground(): void { + // Ability has brought to foreground + console.log(TAG + "MainAbility onForeground."); + } + + onBackground(): void { + // Ability has back to background + console.log(TAG + "MainAbility onBackground."); + } + + onDestroy(): void { + console.log(TAG + "MainAbility onDestroy."); + } + + onWindowStageDestroy(): void { + // Main window is destroyed, release UI related resources + console.log(TAG + "MainAbility onWindowStageDestroy."); + } }; diff --git a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts index 85e204ca..068cb860 100644 --- a/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts +++ b/frameworks/dialog/dialog_ui/usb_right_dialog/src/main/ets/ServiceExtAbility/UsbDialogAbility.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,78 +13,78 @@ * limitations under the License. */ -import extension from '@ohos.app.ability.ServiceExtensionAbility' +import extension from '@ohos.app.ability.ServiceExtensionAbility'; import window from '@ohos.window'; import display from '@ohos.display'; -import rpc from '@ohos.rpc' +import rpc from '@ohos.rpc'; class UsbDialogStub extends rpc.RemoteObject { - constructor(des) { - super(des); - } - onRemoteRequest(code, data, reply, option) { - return true; - } + constructor(des) { + super(des); + } + onRemoteRequest(code, data, reply, option) { + return true; + } } -const BG_COLOR = '#33000000' +const BG_COLOR = '#33000000'; export default class UsbDialogAbility extends extension { - /** - * Lifecycle function, called back when a service extension is started for initialization. - */ - onCreate(want) { - console.log("onCreate want: " + JSON.stringify(want)) - globalThis.extensionContext = this.context; - globalThis.want = want; - globalThis.windowNum = 0 - } + /** + * Lifecycle function, called back when a service extension is started for initialization. + */ + onCreate(want): void { + console.log('onCreate want: ' + JSON.stringify(want)); + globalThis.extensionContext = this.context; + globalThis.want = want; + globalThis.windowNum = 0; + } - onConnect(want) { - console.log("onConnect want: " + JSON.stringify(want)) - display.getDefaultDisplay().then(dis => { - let navigationBarRect = { - left: 0, - top: 0, - width: dis.width, - height: dis.height - } - this.createWindow("UsbDialogAbility", window.WindowType.TYPE_FLOAT, navigationBarRect) - }) - return new UsbDialogStub("UsbRightDialog"); - } + onConnect(want) { + console.log('onConnect want: ' + JSON.stringify(want)); + display.getDefaultDisplay().then(dis => { + let navigationBarRect = { + left: 0, + top: 0, + width: dis.width, + height: dis.height + }; + this.createWindow('UsbDialogAbility', window.WindowType.TYPE_FLOAT, navigationBarRect); + }); + return new UsbDialogStub('UsbRightDialog'); + } - onDisconnect(want) { - console.log("onDisconnect") - } + onDisconnect(want): void { + console.log('onDisconnect'); + } - /** - * Lifecycle function, called back when a service extension is started or recall. - */ - onRequest(want, startId) { - console.log("onRequest") - } - /** - * Lifecycle function, called back before a service extension is destroyed. - */ - onDestroy() { - console.info("UsbDialogAbility onDestroy."); - } + /** + * Lifecycle function, called back when a service extension is started or recall. + */ + onRequest(want, startId): void { + console.log('onRequest'); + } + /** + * Lifecycle function, called back before a service extension is destroyed. + */ + onDestroy(): void { + console.info('UsbDialogAbility onDestroy.'); + } - private async createWindow(name: string, windowType: number, rect) { - console.log("create windows execute") - try { - const usbWin = await window.create(globalThis.extensionContext, name, windowType) - globalThis.window = usbWin - await usbWin.moveTo(rect.left, rect.top) - await usbWin.resetSize(rect.width, rect.height) - await usbWin.loadContent('pages/UsbDialog') - await usbWin.setBackgroundColor(BG_COLOR) - await usbWin.show() - console.log("UsbDialogAbility window create successfully") - } catch { - console.info("UsbDialogAbility window create failed") - } + private async createWindow(name: string, windowType: number, rect): Promise { + console.log('create windows execute'); + try { + const usbWin = await window.create(globalThis.extensionContext, name, windowType); + globalThis.window = usbWin; + await usbWin.moveTo(rect.left, rect.top); + await usbWin.resetSize(rect.width, rect.height); + await usbWin.loadContent('pages/UsbDialog'); + await usbWin.setBackgroundColor(BG_COLOR); + await usbWin.show(); + console.log('UsbDialogAbility window create successfully'); + } catch { + console.info('UsbDialogAbility window create failed'); } + } }; diff --git a/interfaces/kits/js/@ohos.usb.d.ts b/interfaces/kits/js/@ohos.usb.d.ts deleted file mode 100644 index 662ef56d..00000000 --- a/interfaces/kits/js/@ohos.usb.d.ts +++ /dev/null @@ -1,890 +0,0 @@ -/* - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -declare namespace usb { - /** - * Obtains the USB service version for test. - * - * @return Returns the USB service version. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function getVersion(): string; - - /** - * Obtains the USB device list. - * - * @return Returns the {@link USBDevice} list. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function getDevices(): Array>; - - /** - * Connects to the USB device based on the device information returned by {@link getDevices()}. - * - * @param device USB device on the device list returned by {@link getDevices()}. - * @return Returns the {@link USBDevicePipe} object for data transfer. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function connectDevice(device: USBDevice): Readonly; - - /** - * Checks whether the application has the permission to access the device. - * - * @param deviceName Device name defined by {@link USBDevice.name}. - * @return Returns **true** if the user has the permission to access the device; return **false** otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function hasRight(deviceName: string): boolean; - - /** - * Requests the temporary permission for a given application to access the USB device. - * - * @param deviceName Device name defined by {@link USBDevice.name}. - * @return Returns **true** if the temporary device access permissions are granted; return **false** otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function requestRight(deviceName: string): Promise; - - /** - * add device access permission. - * - * @param bundleName refers to application that require access permissions. - * @param deviceName Device name defined by {@link USBDevice.name}. - * @return Returns the boolean value to indicate whether the permission is granted. - * @systemapi - * @syscap SystemCapability.USB.USBManager - * @since 9 - */ - function addRight(bundleName: string, deviceName: string): boolean; - - /** - * Converts the string descriptor of a given USB function list to a numeric mask combination. - * - * @param funcs Descriptor of the supported function list. - * @return Returns the numeric mask combination of the function list. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function usbFunctionsFromString(funcs: string): number; - - /** - * Converts the numeric mask combination of a given USB function list to a string descriptor. - * - * @param funcs Numeric mask combination of the function list. - * @return Returns the string descriptor of the supported function list. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function usbFunctionsToString(funcs: FunctionType): string; - - /** - * Sets the current USB function list in Device mode. - * - * @param funcs Numeric mask combination of the supported function list. - * @return Returns **true** if the setting is successful; returns **false** otherwise. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function setCurrentFunctions(funcs: FunctionType): Promise; - /** - * Obtains the numeric mask combination for the current USB function list in Device mode. - * - * @return Returns the numeric mask combination for the current USB function list in {@link FunctionType}. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function getCurrentFunctions(): FunctionType; - - /* usb port functions begin */ - /** - * Obtains the {@link USBPort} list. - * - * @return Returns the {@link USBPort} list. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function getPorts(): Array; - - /** - * Gets the mask combination for the supported mode list of the specified {@link USBPort}. - * - * @return Returns the mask combination for the supported mode list in {@link PortModeType}. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function getSupportedModes(portId: number): PortModeType; - - /** - * Sets the role types supported by the specified {@link USBPort}, which can be powerRole (for charging) and dataRole (for data transfer). - * - * @param portId Unique ID of the port. - * @param powerRole Charging role defined by {@link PowerRoleType}. - * @param dataRole Data role defined by {@link DataRoleType}. - * @return Returns the supported role type. - * @systemapi - * @SysCap SystemCapability.USB.USBManager - * @since 9 - */ - function setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise; - - /* usb pipe functions begin */ - /** - * Claims a USB interface. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. - * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to claim. - * @param force Optional parameter that determines whether to forcibly claim the USB interface. - * @return Returns **0** if the USB interface is successfully claimed; returns an error code otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number; - /** - * Releases a USB interface. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. - * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to release. - * @return Returns **0** if the USB interface is successfully released; returns an error code otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number; - /** - * Sets the device configuration. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. - * @param config Device configuration defined by {@link USBConfig}. - * @return Returns **0** if the device configuration is successfully set; returns an error code otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function setConfiguration(pipe: USBDevicePipe, config: USBConfig): number; - /** - * Sets a USB interface. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. - * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to set. - * @return Returns **0** if the USB interface is successfully set; return an error code otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function setInterface(pipe: USBDevicePipe, iface: USBInterface): number; - /** - * Obtains the raw USB descriptor. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. - * @return Returns the raw descriptor data. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function getRawDescriptor(pipe: USBDevicePipe): Uint8Array; - /** - * Obtains the file descriptor. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. - * @return Returns the file descriptor of the USB device. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function getFileDescriptor(pipe: USBDevicePipe): number; - /** - * Performs control transfer. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. - * @param contrlparam Control transfer parameters. - * @param timeout Timeout duration. This parameter is optional. The default value is **0**, indicating no timeout. - * @return Returns the size of the transmitted or received data block if the control transfer is successful; return **-1** if an exception occurs. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise; - - /** - * Performs bulk transfer. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. - * @param endpoint USB endpoint defined by {@link USBEndpoint}, which is used to determine the USB port for data transfer. - * @param buffer Buffer for writing or reading data. - * @param timeout Timeout duration. This parameter is optional. The default value is **0**, indicating no timeout. - * @return Returns the size of the transmitted or received data block if the control transfer is successful; return **-1** if an exception occurs. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, - timeout?: number): Promise; - - /** - * Closes a USB device pipe. - * - * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. - * @return Returns **0** if the USB device pipe is closed successfully; return an error code otherwise. - * @SysCap SystemCapability.USB.USBManager - * @since 8 - */ - function closePipe(pipe: USBDevicePipe): number; - - /** - * Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through {@link USBInterface}. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBEndpoint { - /** - * Endpoint address - * - * @since 8 - */ - address: number; - - /** - * Endpoint attributes - * - * @since 8 - */ - attributes: number; - - /** - * Endpoint interval - * - * @since 8 - */ - interval: number; - - /** - * Maximum size of data packets on the endpoint - * - * @since 8 - */ - maxPacketSize: number; - - /** - * Endpoint direction - * - * @since 8 - */ - direction: USBRequestDirection; - - /** - * Endpoint number - * - * @since 8 - */ - number: number; - - /** - * Endpoint type - * - * @since 8 - */ - type: number; - - /** - * Unique ID defined by {@link USBInterface.id}, which indicates the interface to which the endpoint belongs - * - * @since 8 - */ - interfaceId: number; - } - - - /** - * Represents a USB interface. One USBconfig {@link USBConfig} can contain multiple **USBInterface** instances, each providing a specific function. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBInterface { - /** - * Unique ID of the USB interface - * - * @since 8 - */ - id: number; - - /** - * Interface protocol - * - * @since 8 - */ - protocol: number; - - /** - * Device type - * - * @since 8 - */ - clazz: number; - - /** - * Device subclass - * - * @since 8 - */ - subClass: number; - - /** - * Alternating between descriptors of the same USB interface - * - * @since 8 - */ - alternateSetting: number; - - /** - * Interface name - * - * @since 8 - */ - name: string; - - /** - * {@link USBEndpoint} that belongs to the USB interface - * - * @since 8 - */ - endpoints: Array; - } - - /** - * USB configuration. One {@link USBDevice} can contain multiple USBConfig instances. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBConfig { - /** - * Unique ID of the USB configuration - * - * @since 8 - * - * - */ - id: number; - - /** - * Configuration attributes - * - * @since 8 - */ - attributes: number; - - /** - * Maximum power consumption, in mA - * - * @since 8 - */ - maxPower: number; - - /** - * Configuration name, which can be left empty - * - * @since 8 - */ - name: string; - - /** - * Support for remote wakeup - * - * @since 8 - */ - isRemoteWakeup: boolean; - - /** - * Support for independent power supplies - * - * @since 8 - */ - isSelfPowered: boolean; - - /** - * Supported interface attributes defined by {@link USBInterface} - * - * @since 8 - */ - interfaces: Array; - } - - /** - * Represents a USB device. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBDevice { - /** - * Bus address - * - * @since 8 - */ - busNum: number; - /** - * Device address - * - * @since 8 - */ - devAddress: number; - /** - * Device SN - * - * @since 8 - */ - serial: string; - /** - * Device name - * - * @since 8 - */ - name: string; - /** - * Device manufacturer - * - * @since 8 - */ - manufacturerName: string; - /** - * Product information - * - * @since 8 - */ - productName: string; - /** - * Product version - * - * @since 8 - */ - version: string; - /** - * Vendor ID - * - * @since 8 - */ - vendorId: number; - /** - * Product ID - * - * @since 8 - */ - productId: number; - /** - * Device class - * - * @since 8 - */ - clazz: number; - /** - * Device subclass - * - * @since 8 - */ - subClass: number; - /** - * Device protocol code - * - * @since 8 - */ - protocol: number; - /** - * Device configuration descriptor information defined by {@link USBConfig} - * - * @since 8 - */ - configs: Array; - } - - /** - * Represents a USB device pipe, which is used to determine the USB device. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBDevicePipe { - /** - * Bus address. - * - * @since 8 - */ - busNum: number; - /** - * Device address - * - * @since 8 - */ - devAddress: number; - } - - /** - * Enumerates power role types. - * - * @syscap SystemCapability.USB.USBManager - * - * @since 9 - */ - export enum PowerRoleType { - /** - * None - * - * @since 9 - */ - NONE = 0, - /** - * External power supply - * - * @since 9 - */ - SOURCE = 1, - /** - * Internal power supply - * - * @since 9 - */ - SINK = 2 - } - - /** - * Enumerates data role types. - * - * @syscap SystemCapability.USB.USBManager - * - * @since 9 - */ - export enum DataRoleType { - /** - * None - * - * @since 9 - */ - NONE = 0, - /** - * Host mode - * - * @since 9 - */ - HOST = 1, - /** - * Device mode - * - * @since 9 - */ - DEVICE = 2 - } - - /** - * Enumerates port mode types - * - * @syscap SystemCapability.USB.USBManager - * - * @since 9 - */ - export enum PortModeType { - /** - * None - * - * @since 9 - */ - NONE = 0, - /** - * Upstream facing port, which functions as the sink of power supply - * - * @since 9 - */ - UFP = 1, - /** - * Downstream facing port, which functions as the source of power supply - * - * @since 9 - */ - DFP = 2, - /** - * Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently. - * - * @since 9 - */ - DRP = 3, - /** - * Not supported currently - * - * @since 9 - */ - NUM_MODES = 4 - } - - /** - * Enumerates USB device port roles. - * - * @syscap SystemCapability.USB.USBManager - * - * @since 9 - */ - interface USBPortStatus { - /** - * USB mode - * - * @since 9 - */ - currentMode: number; - - /** - * Power role - * - * @since 9 - */ - currentPowerRole: number; - - /** - * Data role - * - * @since 9 - */ - currentDataRole: number; - } - - /** - * Represents a USB device port. - * - * @syscap SystemCapability.USB.USBManager - * - * @since 9 - */ - interface USBPort { - /** - * Unique ID of the USB port - * - * @since 9 - */ - id: number; - - /** - * Mask combination for the supported mode list of the USB port - * - * @since 9 - */ - supportedModes: PortModeType; - - /** - * USB port role defined by {@link USBPortStatus} - * - * @since 9 - */ - status: USBPortStatus; - } - - /** - * Represents control transfer parameters. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - interface USBControlParams { - /** - * Request type - * - * @since 8 - */ - request: number; - /** - * Request target type - * - * @since 8 - */ - target: USBRequestTargetType; - /** - * Control request type - * - * @since 8 - */ - reqType: USBControlRequestType; - /** - * Request parameter value - * - * @since 8 - */ - value: number; - /** - * Index of the parameter value - * - * @since 8 - */ - index: number; - /** - * Data written to or read from the buffer - * @since 8 - */ - data: Uint8Array; - } - - /** - * Enumerates USB request target types. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - export enum USBRequestTargetType { - /** - * USB device - * - * @since 8 - */ - USB_REQUEST_TARGET_DEVICE = 0, - /** - * USB interface - * - * @since 8 - */ - USB_REQUEST_TARGET_INTERFACE = 1, - /** - * Endpoint - * - * @since 8 - */ - USB_REQUEST_TARGET_ENDPOINT = 2, - /** - * Others - * - * @since 8 - */ - USB_REQUEST_TARGET_OTHER = 3 - } - - /** - * Enumerates control request types. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - export enum USBControlRequestType { - /** - * Standard - * - * @since 8 - */ - USB_REQUEST_TYPE_STANDARD = 0, - /** - * Class - * - * @since 8 - */ - USB_REQUEST_TYPE_CLASS = 1, - /** - * Vendor - * - * @since 8 - */ - USB_REQUEST_TYPE_VENDOR = 2 - } - - /** - * Enumerates request directions. - * - * @syscap SystemCapability.USB.USBManager - * @since 8 - */ - export enum USBRequestDirection { - /** - * Request for writing data from the host to the device - * - * @since 8 - */ - USB_REQUEST_DIR_TO_DEVICE = 0, - /** - * Request for reading data from the device to the host - * - * @since 8 - */ - USB_REQUEST_DIR_FROM_DEVICE = 0x80 - } - - /** - * Enumerates function modes. - * - * @syscap SystemCapability.USB.USBManager - * @since 9 - */ - export enum FunctionType { - /** - * None - * - * @since 9 - */ - NONE = 0, - /** - * Serial port device - * - * @since 9 - */ - ACM = 1, - /** - * Ethernet port device - * - * @since 9 - */ - ECM = 2, - /** - * HDC device - * - * @since 9 - */ - HDC = 4, - /** - * MTP device - * - * @since 9 - */ - MTP = 8, - /** - * PTP device - * - * @since 9 - */ - PTP = 16, - /** - * RNDIS device - * - * @since 9 - */ - RNDIS = 32, - /** - * MIDI device - * - * @since 9 - */ - MIDI = 64, - /** - * Audio source device - * - * @since 9 - */ - AUDIO_SOURCE = 128, - /** - * NCM device - * - * @since 9 - */ - NCM = 256 - } - -} - -export default usb; diff --git a/interfaces/kits/js/@ohos.usbManager.d.ts b/interfaces/kits/js/@ohos.usbManager.d.ts new file mode 100644 index 00000000..ac9e7662 --- /dev/null +++ b/interfaces/kits/js/@ohos.usbManager.d.ts @@ -0,0 +1,980 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * This module provides the capability of manage USB device. + * @namespace usbManager + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ +declare namespace usbManager { + /** + * Obtains the USB device list. + * + * @returns Returns the {@link USBDevice} list. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getDevices(): Array>; + + /** + * Connects to the USB device based on the device information returned by {@link getDevices()}. + * + * @param device USB device on the device list returned by {@link getDevices()}. + * @returns Returns the {@link USBDevicePipe} object for data transfer. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @throws {BusinessError} 14400001 - USB Device access denied. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function connectDevice(device: USBDevice): Readonly; + + /** + * Checks whether the application has the permission to access the device. + * + * @param deviceName Device name defined by {@link USBDevice.name}. + * @returns Returns **true** if the user has the permission to access the device; return **false** otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function hasRight(deviceName: string): boolean; + + /** + * Requests the permission for a given application to access the USB device. + * + * @param deviceName Device name defined by {@link USBDevice.name}. + * @returns Returns **true** if the device access permissions are granted; return **false** otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function requestRight(deviceName: string): Promise; + + /** + * Remove the permission for a given application to access the USB device. + * + * @param deviceName Device name defined by {@link USBDevice.name}. + * @returns Returns **true** if the device access permissions are removed; return **false** otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function removeRight(deviceName: string): boolean; + + /** + * Add device access permission. + * + * @param bundleName refers to application that require access permissions. + * @param deviceName Device name defined by {@link USBDevice.name}. + * @returns Returns the boolean value to indicate whether the permission is granted. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function addRight(bundleName: string, deviceName: string): boolean; + + /** + * Converts the string descriptor of a given USB function list to a numeric mask combination. + * + * @param funcs Descriptor of the supported function list. + * @returns Returns the numeric mask combination of the function list. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function usbFunctionsFromString(funcs: string): number; + + /** + * Converts the numeric mask combination of a given USB function list to a string descriptor. + * + * @param funcs Numeric mask combination of the function list. + * @returns Returns the string descriptor of the supported function list. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function usbFunctionsToString(funcs: FunctionType): string; + + /** + * Sets the current USB function list in Device mode. + * + * @param funcs Numeric mask combination of the supported function list. + * @returns Returns **true** if the setting is successful; returns **false** otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function setCurrentFunctions(funcs: FunctionType): Promise; + + /** + * Obtains the numeric mask combination for the current USB function list in Device mode. + * + * @returns Returns the numeric mask combination for the current USB function list in {@link FunctionType}. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getCurrentFunctions(): FunctionType; + + /* usb port functions begin */ + /** + * Obtains the {@link USBPort} list. + * + * @returns Returns the {@link USBPort} list. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getPorts(): Array; + + /** + * Gets the mask combination for the supported mode list of the specified {@link USBPort}. + * + * @returns Returns the mask combination for the supported mode list in {@link PortModeType}. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getSupportedModes(portId: number): PortModeType; + + /** + * Sets the role types supported by the specified {@link USBPort}, which can be powerRole (for charging) and dataRole (for data transfer). + * + * @param portId Unique ID of the port. + * @param powerRole Charging role defined by {@link PowerRoleType}. + * @param dataRole Data role defined by {@link DataRoleType}. + * @returns Returns the supported role type. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @systemapi + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise; + + /* usb pipe functions begin */ + /** + * Claims a USB interface. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. + * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to claim. + * @param force Optional parameter that determines whether to forcibly claim the USB interface. + * @returns Returns **0** if the USB interface is successfully claimed; returns an error code otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number; + + /** + * Releases a USB interface. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. + * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to release. + * @returns Returns **0** if the USB interface is successfully released; returns an error code otherwise. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number; + + /** + * Sets the device configuration. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. + * @param config Device configuration defined by {@link USBConfiguration}. + * @returns Returns **0** if the device configuration is successfully set; returns an error code otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): number; + + /** + * Sets a USB interface. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. + * @param iface USB interface defined by {@link USBInterface}, which is used to determine the interface to set. + * @returns Returns **0** if the USB interface is successfully set; return an error code otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function setInterface(pipe: USBDevicePipe, iface: USBInterface): number; + + /** + * Obtains the raw USB descriptor. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the bus number and device address. + * @returns Returns the raw descriptor data. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getRawDescriptor(pipe: USBDevicePipe): Uint8Array; + + /** + * Obtains the file descriptor. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. + * @returns Returns the file descriptor of the USB device. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function getFileDescriptor(pipe: USBDevicePipe): number; + + /** + * Performs control transfer. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. + * @param controlparam Control transfer parameters. + * @param timeout Timeout duration. This parameter is optional. The default value is **0**, indicating no timeout. + * @returns Returns the size of the transmitted or received data block if the control transfer is successful; return **-1** if an exception occurs. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function controlTransfer(pipe: USBDevicePipe, controlparam: USBControlParams, timeout?: number): Promise; + + /** + * Performs bulk transfer. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. + * @param endpoint USB endpoint defined by {@link USBEndpoint}, which is used to determine the USB port for data transfer. + * @param buffer Buffer for writing or reading data. + * @param timeout Timeout duration. This parameter is optional. The default value is **0**, indicating no timeout. + * @returns Returns the size of the transmitted or received data block if the control transfer is successful; return **-1** if an exception occurs. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, + timeout?: number): Promise; + + /** + * Closes a USB device pipe. + * + * @param pipe Device pipe defined by {@link USBDevicePipe}, which is used to determine the USB device. + * @returns Returns **0** if the USB device pipe is closed successfully; return an error code otherwise. + * @throws {BusinessError} 401 - The parameter types do not match or parameter is not specified. + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + function closePipe(pipe: USBDevicePipe): number; + + /** + * Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through {@link USBInterface}. + * + * @typedef USBEndpoint + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBEndpoint { + /** + * Endpoint address + * + * @since 9 + */ + address: number; + + /** + * Endpoint attributes + * + * @since 9 + */ + attributes: number; + + /** + * Endpoint interval + * + * @since 9 + */ + interval: number; + + /** + * Maximum size of data packets on the endpoint + * + * @since 9 + */ + maxPacketSize: number; + + /** + * Endpoint direction + * + * @since 9 + */ + direction: USBRequestDirection; + + /** + * Endpoint number + * + * @since 9 + */ + number: number; + + /** + * Endpoint type + * + * @since 9 + */ + type: number; + + /** + * Unique ID defined by {@link USBInterface.id}, which indicates the interface to which the endpoint belongs + * + * @since 9 + */ + interfaceId: number; + } + + + /** + * Represents a USB interface. One config {@link USBConfiguration} can contain multiple **USBInterface** instances, each providing a specific function. + * + * @typedef USBInterface + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBInterface { + /** + * Unique ID of the USB interface + * + * @since 9 + */ + id: number; + + /** + * Interface protocol + * + * @since 9 + */ + protocol: number; + + /** + * Device type + * + * @since 9 + */ + clazz: number; + + /** + * Device subclass + * + * @since 9 + */ + subClass: number; + + /** + * Alternation between descriptors of the same USB interface + * + * @since 9 + */ + alternateSetting: number; + + /** + * Interface name + * + * @since 9 + */ + name: string; + + /** + * {@link USBEndpoint} that belongs to the USB interface + * + * @since 9 + */ + endpoints: Array; + } + + /** + * USB configuration. One {@link USBDevice} can contain multiple USBConfiguration instances. + * + * @typedef USBConfiguration + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBConfiguration { + /** + * Unique ID of the USB configuration + * + * @since 9 + * + * + */ + id: number; + + /** + * Configuration attributes + * + * @since 9 + */ + attributes: number; + + /** + * Maximum power consumption, in mA + * + * @since 9 + */ + maxPower: number; + + /** + * Configuration name, which can be left empty + * + * @since 9 + */ + name: string; + + /** + * Support for remote wakeup + * + * @since 9 + */ + isRemoteWakeup: boolean; + + /** + * Support for independent power supplies + * + * @since 9 + */ + isSelfPowered: boolean; + + /** + * Supported interface attributes defined by {@link USBInterface} + * + * @since 9 + */ + interfaces: Array; + } + + /** + * Represents a USB device. + * + * @typedef USBDevice + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBDevice { + /** + * Bus address + * + * @since 9 + */ + busNum: number; + + /** + * Device address + * + * @since 9 + */ + devAddress: number; + + /** + * Device SN + * + * @since 9 + */ + serial: string; + + /** + * Device name + * + * @since 9 + */ + name: string; + + /** + * Device manufacturer + * + * @since 9 + */ + manufacturerName: string; + + /** + * Product information + * + * @since 9 + */ + productName: string; + + /** + * Product version + * + * @since 9 + */ + version: string; + + /** + * Vendor ID + * + * @since 9 + */ + vendorId: number; + + /** + * Product ID + * + * @since 9 + */ + productId: number; + + /** + * Device class + * + * @since 9 + */ + clazz: number; + + /** + * Device subclass + * + * @since 9 + */ + subClass: number; + + /** + * Device protocol code + * + * @since 9 + */ + protocol: number; + + /** + * Device configuration descriptor information defined by {@link USBConfiguration} + * + * @since 9 + */ + configs: Array; + } + + /** + * Represents a USB device pipe, which is used to determine the USB device. + * + * @typedef USBDevicePipe + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBDevicePipe { + /** + * Bus address. + * + * @since 9 + */ + busNum: number; + + /** + * Device address + * + * @since 9 + */ + devAddress: number; + } + + /** + * Enumerates power role types. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + export enum PowerRoleType { + /** + * None + * + * @since 9 + */ + NONE = 0, + + /** + * External power supply + * + * @since 9 + */ + SOURCE = 1, + + /** + * Internal power supply + * + * @since 9 + */ + SINK = 2 + } + + /** + * Enumerates data role types. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + export enum DataRoleType { + /** + * None + * + * @since 9 + */ + NONE = 0, + + /** + * Host mode + * + * @since 9 + */ + HOST = 1, + + /** + * Device mode + * + * @since 9 + */ + DEVICE = 2 + } + + /** + * Enumerates port mode types + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + export enum PortModeType { + /** + * None + * + * @since 9 + */ + NONE = 0, + + /** + * Upstream facing port, which functions as the sink of power supply + * + * @since 9 + */ + UFP = 1, + + /** + * Downstream facing port, which functions as the source of power supply + * + * @since 9 + */ + DFP = 2, + + /** + * Dynamic reconfiguration port (DRP), which can function as the DFP (host) or UFP (device). It is not supported currently. + * + * @since 9 + */ + DRP = 3, + + /** + * Not supported currently + * + * @since 9 + */ + NUM_MODES = 4 + } + + /** + * Enumerates USB device port roles. + * + * @typedef USBPortStatus + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + interface USBPortStatus { + /** + * USB mode + * + * @since 9 + */ + currentMode: number; + + /** + * Power role + * + * @since 9 + */ + currentPowerRole: number; + + /** + * Data role + * + * @since 9 + */ + currentDataRole: number; + } + + /** + * Represents a USB device port. + * + * @typedef USBPort + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + interface USBPort { + /** + * Unique ID of the USB port + * + * @since 9 + */ + id: number; + + /** + * Mask combination for the supported mode list of the USB port + * + * @since 9 + */ + supportedModes: PortModeType; + + /** + * USB port role defined by {@link USBPortStatus} + * + * @since 9 + */ + status: USBPortStatus; + } + + /** + * Represents control transfer parameters. + * + * @typedef USBControlParams + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + interface USBControlParams { + /** + * Request type + * + * @since 9 + */ + request: number; + + /** + * Request target type + * + * @since 9 + */ + target: USBRequestTargetType; + + /** + * Control request type + * + * @since 9 + */ + reqType: USBControlRequestType; + + /** + * Request parameter value + * + * @since 9 + */ + value: number; + + /** + * Index of the parameter value + * + * @since 9 + */ + index: number; + + /** + * Data written to or read from the buffer + * @since 9 + */ + data: Uint8Array; + } + + /** + * Enumerates USB request target types. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + export enum USBRequestTargetType { + /** + * USB device + * + * @since 9 + */ + USB_REQUEST_TARGET_DEVICE = 0, + + /** + * USB interface + * + * @since 9 + */ + USB_REQUEST_TARGET_INTERFACE = 1, + + /** + * Endpoint + * + * @since 9 + */ + USB_REQUEST_TARGET_ENDPOINT = 2, + + /** + * Others + * + * @since 9 + */ + USB_REQUEST_TARGET_OTHER = 3 + } + + /** + * Enumerates control request types. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + export enum USBControlRequestType { + /** + * Standard + * + * @since 9 + */ + USB_REQUEST_TYPE_STANDARD = 0, + + /** + * Class + * + * @since 9 + */ + USB_REQUEST_TYPE_CLASS = 1, + + /** + * Vendor + * + * @since 9 + */ + USB_REQUEST_TYPE_VENDOR = 2 + } + + /** + * Enumerates request directions. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @since 9 + */ + export enum USBRequestDirection { + /** + * Request for writing data from the host to the device + * + * @since 9 + */ + USB_REQUEST_DIR_TO_DEVICE = 0, + + /** + * Request for reading data from the device to the host + * + * @since 9 + */ + USB_REQUEST_DIR_FROM_DEVICE = 0x80 + } + + /** + * Enumerates function modes. + * + * @enum { number } + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 9 + */ + export enum FunctionType { + /** + * None + * + * @since 9 + */ + NONE = 0, + + /** + * Serial port device + * + * @since 9 + */ + ACM = 1, + + /** + * Ethernet port device + * + * @since 9 + */ + ECM = 2, + + /** + * HDC device + * + * @since 9 + */ + HDC = 4, + + /** + * MTP device + * + * @since 9 + */ + MTP = 8, + + /** + * PTP device + * + * @since 9 + */ + PTP = 16, + + /** + * RNDIS device + * + * @since 9 + */ + RNDIS = 32, + + /** + * MIDI device + * + * @since 9 + */ + MIDI = 64, + + /** + * Audio source device + * + * @since 9 + */ + AUDIO_SOURCE = 128, + + /** + * NCM device + * + * @since 9 + */ + NCM = 256 + } + +} + +export default usbManager; diff --git a/services/native/src/usb_device_manager.cpp b/services/native/src/usb_device_manager.cpp index 095ea5ab..a4660c2c 100644 --- a/services/native/src/usb_device_manager.cpp +++ b/services/native/src/usb_device_manager.cpp @@ -211,7 +211,7 @@ void UsbDeviceManager::ProcessFunctionSwitchWindow(int32_t status) if (!window_->DismissFunctionSwitchWindow()) { USB_HILOGE(MODULE_USB_SERVICE, "start dismiss usb service switch window failed"); } - int32_t targetFunction = currentFunctions_; + uint32_t targetFunction = static_cast(currentFunctions_); targetFunction &= ~(UsbSrvSupport::FUNCTION_MTP | UsbSrvSupport::FUNCTION_PTP); int32_t ret = usbd_->SetCurrentFunctions(targetFunction); if (ret != UEC_OK) { -- Gitee