1/* 2 * Copyright (C) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16@!sts_inject_into_module("import { AsyncCallback } from '@ohos.base';") 17 18@!namespace("@ohos.usbManager", "usbManager") 19@!sts_inject(""" 20static { loadLibrary("usb_manager_taihe_native.z") } 21""") 22 23function getDevices(): Array<USBDevice>; 24 25function connectDevice(device: USBDevice): USBDevicePipe; 26 27function hasRight(deviceName: String): bool; 28 29@gen_promise("requestRight") 30function requestRightSync(deviceName: String): bool; 31 32function removeRight(deviceName: String): bool; 33 34function addDeviceAccessRight(tokenId: String, deviceName: String): bool; 35 36function getFunctionsFromString(funcs: String): i32; 37 38function getStringFromFunctions(funcs: FunctionType): String; 39 40@gen_promise("setDeviceFunctions") 41function setDeviceFunctionsSync(funcs: FunctionType): void; 42 43function getDeviceFunctions(): FunctionType; 44 45function getPortList(): Array<USBPort>; 46 47function getPortSupportModes(portId: i32): PortModeType; 48 49@gen_promise("setPortRoleTypes") 50function setPortRoleTypesSync(portId: i32, powerRole: PowerRoleType, dataRole: DataRoleType): void; 51 52function addAccessoryRight(tokenId: i32, accessory: USBAccessory): void; 53 54function claimInterface(pipe: USBDevicePipe, iface: USBInterface, force: Optional<bool>): i32; 55 56function releaseInterface(pipe: USBDevicePipe, iface: USBInterface): i32; 57 58function setConfiguration(pipe: USBDevicePipe, config: USBConfiguration): i32; 59 60function setInterface(pipe: USBDevicePipe, iface: USBInterface): i32; 61 62function getRawDescriptor(pipe: USBDevicePipe): @typedarray Array<u8>; 63 64function getFileDescriptor(pipe: USBDevicePipe): i32; 65 66@gen_promise("usbControlTransfer") 67function usbControlTransferSync(pipe: USBDevicePipe, requestparam: USBDeviceRequestParams, timeout: Optional<i32>): i32; 68 69@gen_promise("bulkTransfer") 70function bulkTransferSync( 71 pipe: USBDevicePipe, 72 endpoint: USBEndpoint, 73 buffer: @sts_type("Uint8Array") Opaque, 74 timeout: Optional<i32> 75): i32; 76 77function closePipe(pipe: USBDevicePipe): i32; 78 79function hasAccessoryRight(accessory: USBAccessory): bool; 80 81@gen_promise("requestAccessoryRight") 82function requestAccessoryRightSync(accessory: USBAccessory): bool; 83 84function cancelAccessoryRight(accessory: USBAccessory): void; 85 86function getAccessoryList(): Array<USBAccessory>; 87 88function openAccessory(accessory: USBAccessory): USBAccessoryHandle; 89 90function closeAccessory(accessoryHandle: USBAccessoryHandle): void; 91 92struct USBEndpoint { 93 address: i32; 94 95 attributes: i32; 96 97 interval: i32; 98 99 maxPacketSize: i32; 100 101 direction: USBRequestDirection; 102 103 endpointAddr: i32; 104 105 type: i32; 106 107 interfaceId: i32; 108} 109 110struct USBInterface { 111 id: i32; 112 113 protocol: i32; 114 115 clazz: i32; 116 117 subClass: i32; 118 119 alternateSetting: i32; 120 121 name: String; 122 123 endpoints: Array<USBEndpoint>; 124} 125 126struct USBConfiguration { 127 id: i32; 128 129 attributes: i32; 130 131 maxPower: i32; 132 133 name: String; 134 135 isRemoteWakeup: bool; 136 137 isSelfPowered: bool; 138 139 interfaces: Array<USBInterface>; 140} 141 142struct USBDevice { 143 busNum: i32; 144 145 devAddress: i32; 146 147 serial: String; 148 149 name: String; 150 151 manufacturerName: String; 152 153 productName: String; 154 155 version: String; 156 157 vendorId: i32; 158 159 productId: i32; 160 161 clazz: i32; 162 163 subClass: i32; 164 165 protocol: i32; 166 167 configs: Array<USBConfiguration>; 168} 169 170struct USBDevicePipe { 171 busNum: i32; 172 173 devAddress: i32; 174} 175 176enum PowerRoleType : i32 { 177 NONE = 0, 178 179 SOURCE = 1, 180 181 SINK = 2 182} 183 184enum DataRoleType : i32 { 185 NONE = 0, 186 187 HOST = 1, 188 189 DEVICE = 2 190} 191 192enum PortModeType : i32 { 193 NONE = 0, 194 195 UFP = 1, 196 197 DFP = 2, 198 199 DRP = 3, 200 201 NUM_MODES = 4 202} 203 204struct USBPortStatus { 205 currentMode: i32; 206 207 currentPowerRole: i32; 208 209 currentDataRole: i32; 210} 211 212struct USBPort { 213 id: i32; 214 215 supportedModes: PortModeType; 216 217 status: USBPortStatus; 218} 219 220struct USBControlParams { 221 request: i32; 222 223 target: USBRequestTargetType; 224 225 reqType: USBControlRequestType; 226 227 value: i32; 228 229 index: i32; 230 231 data: @typedarray Array<u8>; 232} 233 234struct USBDeviceRequestParams { 235 bmRequestType: i32; 236 237 bRequest: i32; 238 239 wValue: i32; 240 241 wIndex: i32; 242 243 wLength: i32; 244 245 data: @sts_type("Uint8Array") Opaque; 246} 247 248enum USBRequestTargetType : i32 { 249 USB_REQUEST_TARGET_DEVICE = 0, 250 251 USB_REQUEST_TARGET_INTERFACE = 1, 252 253 USB_REQUEST_TARGET_ENDPOINT = 2, 254 255 USB_REQUEST_TARGET_OTHER = 3 256} 257 258enum USBControlRequestType : i32 { 259 USB_REQUEST_TYPE_STANDARD = 0, 260 261 USB_REQUEST_TYPE_CLASS = 1, 262 263 USB_REQUEST_TYPE_VENDOR = 2 264} 265 266enum USBRequestDirection : i32 { 267 USB_REQUEST_DIR_TO_DEVICE = 0, 268 269 USB_REQUEST_DIR_FROM_DEVICE = 0x80 270} 271 272enum FunctionType : i32 { 273 NONE = 0, 274 275 ACM = 1, 276 277 ECM = 2, 278 279 HDC = 4, 280 281 MTP = 8, 282 283 PTP = 16, 284 285 RNDIS = 32, 286 287 MIDI = 64, 288 289 AUDIO_SOURCE = 128, 290 291 NCM = 256 292} 293 294struct USBAccessory { 295 manufacturer: String; 296 297 product: String; 298 299 description: String; 300 301 version: String; 302 303 serialNumber: String; 304} 305 306struct USBAccessoryHandle { 307 accessoryFd: i32; 308} 309 310enum UsbTransferFlags : i32 { 311 USB_TRANSFER_SHORT_NOT_OK = 0, 312 313 USB_TRANSFER_FREE_BUFFER = 1, 314 315 USB_TRANSFER_FREE_TRANSFER = 2, 316 317 USB_TRANSFER_ADD_ZERO_PACKET = 3 318} 319 320enum UsbTransferStatus : i32 { 321 TRANSFER_COMPLETED = 0, 322 323 TRANSFER_ERROR = 1, 324 325 TRANSFER_TIMED_OUT = 2, 326 327 TRANSFER_CANCELED = 3, 328 329 TRANSFER_STALL = 4, 330 331 TRANSFER_NO_DEVICE = 5, 332 333 TRANSFER_OVERFLOW = 6 334} 335 336enum UsbEndpointTransferType : i32 { 337 TRANSFER_TYPE_ISOCHRONOUS = 0x1, 338 339 TRANSFER_TYPE_BULK = 0x2, 340 341 TRANSFER_TYPE_INTERRUPT = 0x3 342} 343 344struct UsbIsoPacketDescriptor { 345 length: i32; 346 347 actualLength: i32; 348 349 status: UsbTransferStatus; 350} 351 352struct SubmitTransferCallback { 353 actualLength: i32; 354 355 status: UsbTransferStatus; 356 357 isoPacketDescs: Array<UsbIsoPacketDescriptor>; 358} 359 360struct UsbDataTransferParams { 361 devPipe: USBDevicePipe; 362 363 flags: UsbTransferFlags; 364 365 endpoint: i32; 366 367 type: UsbEndpointTransferType; 368 369 timeout: i32; 370 371 length: i32; 372 373 callback: @sts_type("AsyncCallback<SubmitTransferCallback>") Opaque; 374 375 userData: @typedarray Array<u8>; 376 377 buffer: @sts_type("Uint8Array") Opaque; 378 379 isoPacketCount: i32; 380} 381 382function usbSubmitTransfer(transfer: UsbDataTransferParams): void; 383 384function usbCancelTransfer(transfer: UsbDataTransferParams): void; 385 386function resetUsbDevice(pipe: USBDevicePipe): bool;