From 00ecd8e64c236b9018141dbef1df06161b769b34 Mon Sep 17 00:00:00 2001 From: liangqi Date: Fri, 18 Oct 2024 11:21:46 +0800 Subject: [PATCH 1/2] add usb accessory mode Signed-off-by: liangqi --- api/@ohos.usbManager.d.ts | 188 +++++++++++++++++++++++++++++++++++++- 1 file changed, 187 insertions(+), 1 deletion(-) diff --git a/api/@ohos.usbManager.d.ts b/api/@ohos.usbManager.d.ts index 0afe6b809b..be6be54a0d 100644 --- a/api/@ohos.usbManager.d.ts +++ b/api/@ohos.usbManager.d.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2024 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 @@ -342,6 +342,26 @@ declare namespace usbManager { */ function setPortRoleTypes(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise; + /** + * Adds USB accessory access right. + * + * @permission ohos.permission.MANAGE_USB_CONFIG + * @param { number } tokenId - refers to application that require access permissions. It cannot be empty. + * @param { USBAccessory } accessory - USB accessory. It cannot be empty. + * @throws { BusinessError } 201 - The permission check failed. + * @throws { BusinessError } 202 - Permission denied. Normal application do not have permission to use system api. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @throws { BusinessError } 14400005 - Database operation exception. + * @syscap SystemCapability.USB.USBManager + * @systemapi + * @since 14 + */ + function addAccessoryRight(tokenId: number, accessory: USBAccessory): void; + /* usb pipe functions begin */ /** * Claims a USB interface. @@ -495,6 +515,100 @@ declare namespace usbManager { */ function closePipe(pipe: USBDevicePipe): number; + /** + * Checks whether the application has the right to access the USB accessory. + * + * @param { USBAccessory } accessory - USB accessory. It cannot be empty. + * @returns { boolean } indicates if the user has the right to access the USB accessory. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @throws { BusinessError } 14400005 - Database operation exception. + * @throws { BusinessError } 14401001 - The target USBAccessory not matched. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function hasAccessoryRight(accessory: USBAccessory): boolean; + + /** + * Requests the right for a given application to access the USB accessory. + * + * @param { USBAccessory } accessory - USB accessory. It cannot be empty. + * @returns { Promise } indicates if the USB accessory access right are granted. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @throws { BusinessError } 14400005 - Database operation exception. + * @throws { BusinessError } 14401001 - The target USBAccessory not matched. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function requestAccessoryRight(accessory: USBAccessory): Promise; + + /** + * Cancels the right for a given application to access the USB accessory. + * + * @param { USBAccessory } accessory - USB accessory. It cannot be empty. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @throws { BusinessError } 14400005 - Database operation exception. + * @throws { BusinessError } 14401001 - The target USBAccessory not matched. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function cancelAccessoryRight(accessory: USBAccessory): void; + + /** + * Obtains the USB Accessory list. + * + * @returns { Array> } USB accessory list. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function getAccessoryList(): Array>; + + /** + * Obtains the accessory handle and opens accessory file descriptor. + * + * @param { USBAccessory } accessory - accessory, which is used to determine the accessory. It cannot be empty. + * @returns { USBAccessoryHandle } returns the handle of the accessory. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400001 - Permission denied. Call requestAccessoryRight to get the right first. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @throws { BusinessError } 14401001 - The target USBAccessory not matched. + * @throws { BusinessError } 14401002 - Failed to open the native accessory node. + * @throws { BusinessError } 14401003 - Cannot reopen the accessory. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function openAccessory(accessory: USBAccessory): USBAccessoryHandle; + + /** + * Closes the accessory file descriptor. + * + * @param { USBAccessoryHandle } accessoryHandle - Accessory handle to be closed. + * @throws { BusinessError } 401 - Parameter error. Possible causes: + *
1. Mandatory parameters are left unspecified. + *
2. Incorrect parameter types. + * @throws { BusinessError } 14400004 - Service exception. Possible causes: + *
1. No accessory is plugged in. + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + function closeAccessory(accessoryHandle: USBAccessoryHandle): void; + /** * Represents the USB endpoint from which data is sent or received. You can obtain the USB endpoint through USBInterface. * @@ -1406,6 +1520,78 @@ declare namespace usbManager { */ NCM = 256 } + + /** + * Represents a USB Accessory. + * + * @typedef USBAccessory + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + interface USBAccessory { + /** + * The manufacturer name of the accessory. + * + * @type { string } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + manufacturer: string; + + /** + * The product of the accessory. + * + * @type { string } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + product: string; + + /** + * The user visible description of the accessory. + * + * @type { string } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + description: string; + + /** + * The version of the accessory. + * + * @type { string } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + version: string; + + /** + * The serial number of the accessory. + * + * @type { string } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + serialNumber: string; + } + + /** + * Handle of accessory. + * + * @typedef USBAccessoryHandle + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + interface USBAccessoryHandle { + /** + * The file descriptor of the accessory. + * + * @type { number } + * @syscap SystemCapability.USB.USBManager + * @since 14 + */ + accessoryFd: number; + } } export default usbManager; From d7f8257bbe7c6980a6285ebe0506ded18c4e0e31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A2=81=E7=90=A6?= Date: Sat, 2 Nov 2024 10:08:11 +0000 Subject: [PATCH 2/2] update api/@ohos.usbManager.d.ts. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 梁琦 --- api/@ohos.usbManager.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/@ohos.usbManager.d.ts b/api/@ohos.usbManager.d.ts index be6be54a0d..b02f89e602 100644 --- a/api/@ohos.usbManager.d.ts +++ b/api/@ohos.usbManager.d.ts @@ -1584,7 +1584,7 @@ declare namespace usbManager { */ interface USBAccessoryHandle { /** - * The file descriptor of the accessory. + * The file descriptor of the accessory.The valid USBAccessoryHandle.accessoryFd is a positive value. * * @type { number } * @syscap SystemCapability.USB.USBManager