1/* 2 * Copyright (c) 2021 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 16declare namespace usb { 17 /** 18 * 获取USB服务版本号,测试用 19 * 20 * @return USB服务当前的版本号. 21 * @SysCap SystemCapability.USB.USBManager 22 * @since 8 23 */ 24 function getVersion(): string; 25 26 /* usb core functions begin */ 27 /** 28 * 获取USB设备列表 29 * 30 * @return 设备信息{@link USBDevice}列表. 31 * @SysCap SystemCapability.USB.USBManager 32 * @since 8 33 */ 34 function getDevices(): Array<Readonly<USBDevice>>; 35 36 /** 37 * 根据{@link getDevices()}返回的设备信息打开USB设备 38 * 39 * @param device 设备信息,{@link getDevices()}返回的列表中的一个设备信息 40 * @return 指定的传输通道 {@link USBDevicePipe} 对象. 41 * @SysCap SystemCapability.USB.USBManager 42 * @since 8 43 */ 44 function connectDevice(device: USBDevice): Readonly<USBDevicePipe>; 45 46 /** 47 * 判断是否有权访问设备 48 * 49 * @param deviceName 设备名称,{@link USBDevice.name} 50 * @return true 有权限, false 没有权限 51 * @SysCap SystemCapability.USB.USBManager 52 * @since 8 53 */ 54 function hasRight(deviceName: string): boolean; 55 56 /** 57 * 请求给定软件包的临时权限以访问设备 58 * 59 * @param deviceName 设备名称,{@link USBDevice.name} 60 * @return true 请求权限成功, false 请求权限失败 61 * @SysCap SystemCapability.USB.USBManager 62 * @since 8 63 */ 64 function requestRight(deviceName: string): Promise<boolean>; 65 66 /** 67 * 将给定的功能列表描述字符串转换为功能列表的数字组合掩码 68 * 69 * @param funcs 支持的功能列表描述符 70 * @return 功能列表的数字组合掩码 71 * @systemapi 72 * @SysCap SystemCapability.USB.USBManager 73 * @since 8 74 */ 75 function usbFunctionsFromString(funcs: string): number; 76 77 /** 78 * 将给定的功能列表的数字组合掩码转换为功能列表描述字符串 79 * 80 * @param funcs 支持的功能列表的数字组合掩码 81 * @return 支持的功能列表描述字符串 82 * @systemapi 83 * @SysCap SystemCapability.USB.USBManager 84 * @since 8 85 */ 86 function usbFunctionsToString(funcs: FunctionType): string; 87 88 /** 89 * 在设备模式下设置当前的USB功能列表 90 * 91 * @param funcs 设置支持的功能列表的数字组合掩码{@link FunctionType} 92 * @return true 设置成功, false 设置失败 93 * @systemapi 94 * @SysCap SystemCapability.USB.USBManager 95 * @since 8 96 */ 97 function setCurrentFunctions(funcs: FunctionType): Promise<boolean>; 98 /** 99 * 获取设备模式下的当前USB功能列表的数字组合掩码 100 * 101 * @return 支持的功能列表的数字组合掩码{@link FunctionType} 102 * @systemapi 103 * @SysCap SystemCapability.USB.USBManager 104 * @since 8 105 */ 106 function getCurrentFunctions(): FunctionType; 107 108 /* usb port functions begin */ 109 /** 110 * 获取物理USB端口描述信息{@link USBPort}列表 111 * 112 * @return {@link USBPort}列表 113 * @systemapi 114 * @SysCap SystemCapability.USB.USBManager 115 * @since 8 116 */ 117 function getPorts(): Array<USBPort>; 118 119 /** 120 * 获取指定的端口{@link USBPort}支持的模式列表的组合掩码 121 * 122 * @return 支持的模式列表的组合掩码{@link PortModeType} 123 * @systemapi 124 * @SysCap SystemCapability.USB.USBManager 125 * @since 8 126 */ 127 function getSupportedModes(portId: number): PortModeType; 128 129 /** 130 * 设置指定的端口{@link USBPort}支持的角色模式,包含充电角色、数据传输角色 131 * 132 * @param portId 端口的唯一标识 133 * @param powerRole 充电的角色{@link PowerRoleType} 134 * @param dataRole 数据传输的角色{@link DataRoleType} 135 * @return 支持的模式 136 * @systemapi 137 * @SysCap SystemCapability.USB.USBManager 138 * @since 8 139 */ 140 function setPortRoles(portId: number, powerRole: PowerRoleType, dataRole: DataRoleType): Promise<boolean>; 141 142 /* usb pipe functions begin */ 143 /** 144 * 获取接口 145 * 146 * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. 147 * @param iface 用于确定需要获取接口 {@link USBInterface}. 148 * @param force 是否强制获取 149 * @return 成功:0;失败:错误码 150 * @SysCap SystemCapability.USB.USBManager 151 * @since 8 152 */ 153 function claimInterface(pipe: USBDevicePipe, iface: USBInterface, force?: boolean): number; 154 /** 155 * 释放接口 156 * 157 * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. 158 * @param iface 用于确定需要释放接口 {@link USBInterface}. 159 * @return 成功:0;失败:错误码 160 * @SysCap SystemCapability.USB.USBManager 161 * @since 8 162 */ 163 function releaseInterface(pipe: USBDevicePipe, iface: USBInterface): number; 164 /** 165 * 设置设备配置 166 * 167 * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. 168 * @param config 用于确定需要设置配置 {@link USBConfig}. 169 * @return 成功:0;失败:错误码 170 * @SysCap SystemCapability.USB.USBManager 171 * @since 8 172 */ 173 function setConfiguration(pipe: USBDevicePipe, config: USBConfig): number; 174 /** 175 * 设置设备接口 176 * 177 * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. 178 * @param iface 用于确定需要设置接口 {@link USBInterface}. 179 * @return 成功:0;失败:错误码 180 * @SysCap SystemCapability.USB.USBManager 181 * @since 8 182 */ 183 function setInterface(pipe: USBDevicePipe, iface: USBInterface): number; 184 /** 185 * 获取原始的USB描述符 186 * 187 * @param pipe 用于确定总线号和设备地址 {@link USBDevicePipe}. 188 * @return 返回获取的原始数据 189 * @SysCap SystemCapability.USB.USBManager 190 * @since 8 191 */ 192 function getRawDescriptor(pipe: USBDevicePipe): Uint8Array; 193 /** 194 * 获取文件描述符 195 * 196 * @param pipe 用于确定设备 {@link USBDevicePipe}. 197 * @return 返回设备对应的文件描述符 198 * @SysCap SystemCapability.USB.USBManager 199 * @since 8 200 */ 201 function getFileDescriptor(pipe: USBDevicePipe): number; 202 /** 203 * 控制传输 204 * 205 * @param pipe 用于确定设备 {@link USBDevicePipe}. 206 * @param contrlparam 控制传输参数 207 * @param timeout 超时时间,可选参数,默认为0不超时 208 * @return 传输或接收到的数据块大小,异常返回-1 209 * @SysCap SystemCapability.USB.USBManager 210 * @since 8 211 */ 212 function controlTransfer(pipe: USBDevicePipe, contrlparam: USBControlParams, timeout?: number): Promise<number>; 213 214 /** 215 * 批量传输 216 * 217 * @param pipe 用于确定设备 {@link USBDevicePipe}. 218 * @param endpoint 用于确定传输的端口 {@link USBEndpoint}. 219 * @param buffer 用于写入或读取的缓冲区 220 * @param timeout 超时时间,可选参数,默认为0不超时 221 * @return 传输或接收到的数据块大小,异常返回-1 222 * @SysCap SystemCapability.USB.USBManager 223 * @since 8 224 */ 225 function bulkTransfer(pipe: USBDevicePipe, endpoint: USBEndpoint, buffer: Uint8Array, 226 timeout?: number): Promise<number>; 227 228 /** 229 * 关闭USBDevicePipe 230 * 231 * @param pipe 用于确定pipe {@link USBDevicePipe}. 232 * @return 成功:0;失败:错误码 233 * @SysCap SystemCapability.USB.USBManager 234 * @since 8 235 */ 236 function closePipe(pipe: USBDevicePipe): number; 237 238 /** 239 * 通过USB发送和接收数据的端口。通过{@link USBInterface}获取。 240 * 241 * @since 8 242 */ 243 interface USBEndpoint { 244 /** 245 * 端点地址 246 * 247 * @since 8 248 */ 249 address: number; 250 251 /** 252 * 端点属性 253 * 254 * @since 8 255 */ 256 attributes: number; 257 258 /** 259 * 端点间隔 260 * 261 * @since 8 262 */ 263 interval: number; 264 265 /** 266 * 端点最大数据包大小 267 * 268 * @since 8 269 */ 270 maxPacketSize: number; 271 272 /** 273 * 端点的方向。 274 * 275 * @since 8 276 */ 277 direction: USBRequestDirection; 278 279 /** 280 * 端点号 281 * 282 * @since 8 283 */ 284 number: number; 285 286 /** 287 * 端点类型 288 * 289 * @since 8 290 */ 291 type: number; 292 293 /** 294 * 端点所属的接口的唯一标识{@link USBInterface.id} 295 * 296 * @since 8 297 */ 298 interfaceId: number; 299 } 300 301 302 /** 303 * 一个{@link USBConfig}中可以含有多个interface,每个interface提供一个功能。 304 * 305 * @since 8 306 */ 307 interface USBInterface { 308 /** 309 * 接口的唯一标识 310 * 311 * @since 8 312 */ 313 id: number; 314 315 /** 316 * 接口的协议 317 * 318 * @since 8 319 */ 320 protocol: number; 321 322 /** 323 * 设备类型 324 * 325 * @since 8 326 */ 327 clazz: number; 328 329 /** 330 * 设备子类 331 * 332 * @since 8 333 */ 334 subClass: number; 335 336 /** 337 *在同一个接口中的多个描述符中进行切换设置 338 * 339 * @since 8 340 */ 341 alternateSetting: number; 342 343 /** 344 * 接口名称 345 * 346 * @since 8 347 */ 348 name: string; 349 350 /** 351 * 当前接口所包含的端点{@link USBEndpoint} 352 * 353 * @since 8 354 */ 355 endpoints: Array<USBEndpoint>; 356 } 357 358 /** 359 * USB配置,一个{@link USBDevice}中可以含有多个配置。 360 * 361 * @since 8 362 */ 363 interface USBConfig { 364 /** 365 * 配置的唯一标识 366 * 367 * @since 8 368 * 369 * 370 */ 371 id: number; 372 373 /** 374 * 配置的属性 375 * 376 * @since 8 377 */ 378 attributes: number; 379 380 /** 381 * 最大功耗,以毫安为单位 382 * 383 * @since 8 384 */ 385 maxPower: number; 386 387 /** 388 * 配置的名称,可以为空 389 * 390 * @since 8 391 */ 392 name: string; 393 394 /** 395 * 检查当前配置是否支持远程唤醒 396 * 397 * @since 8 398 */ 399 isRemoteWakeup: boolean; 400 401 /** 402 * 检查当前配置是否支持独立电源 403 * 404 * @since 8 405 */ 406 isSelfPowered: boolean; 407 408 /** 409 * 配置支持的接口属性{@link USBInterface} 410 * 411 * @since 8 412 */ 413 interfaces: Array<USBInterface>; 414 } 415 416 /** 417 * USB设备信息。 418 * 419 * @since 8 420 */ 421 interface USBDevice { 422 /** 423 * 总线地址 424 * 425 * @since 8 426 */ 427 busNum: number; 428 /** 429 * 设备地址 430 * 431 * @since 8 432 */ 433 devAddress: number; 434 /** 435 * 序列号 436 * 437 * @since 8 438 */ 439 serial: string; 440 /** 441 * 设备名字 442 * 443 * @since 8 444 */ 445 name: string; 446 /** 447 * 产商信息 448 * 449 * @since 8 450 */ 451 manufacturerName: string; 452 /** 453 * 产品信息 454 * 455 * @since 8 456 */ 457 productName: string; 458 /** 459 * 版本 460 * 461 * @since 8 462 */ 463 version: string; 464 /** 465 * 厂商ID 466 * 467 * @since 8 468 */ 469 vendorId: number; 470 /** 471 * 产品ID 472 * 473 * @since 8 474 */ 475 productId: number; 476 /** 477 * 设备类 478 * 479 * @since 8 480 */ 481 clazz: number; 482 /** 483 * 设备子类 484 * 485 * @since 8 486 */ 487 subClass: number; 488 /** 489 * 设备协议码 490 * 491 * @since 8 492 */ 493 protocol: number; 494 /** 495 * 设备配置描述符信息 {@link USBConfig}. 496 * 497 * @since 8 498 */ 499 configs: Array<USBConfig>; 500 } 501 502 /** 503 * USB设备消息控制通道,用于确定设备 504 * 505 * @since 8 506 */ 507 interface USBDevicePipe { 508 /** 509 * 总线地址 510 * 511 * @since 8 512 */ 513 busNum: number; 514 /** 515 * 设备地址 516 * 517 * @since 8 518 */ 519 devAddress: number; 520 } 521 522 /** 523 * 电源角色类型 524 * 525 * @since 8 526 */ 527 export enum PowerRoleType { 528 /** 529 * 无 530 * 531 * @since 8 532 */ 533 NONE = 0, 534 /** 535 * 外部供电 536 * 537 * @since 8 538 */ 539 SOURCE = 1, 540 /** 541 * 内部供电 542 * 543 * @since 8 544 */ 545 SINK = 2 546 } 547 548 /** 549 * 数据角色类型 550 * 551 * @since 8 552 */ 553 export enum DataRoleType { 554 /** 555 * 无 556 * 557 * @since 8 558 */ 559 NONE = 0, 560 /** 561 * host模式,主设备模式 562 * 563 * @since 8 564 */ 565 HOST = 1, 566 /** 567 * device模式,从设备模式 568 * 569 * @since 8 570 */ 571 DEVICE = 2 572 } 573 574 /** 575 * port模式类型 576 * 577 * @since 8 578 */ 579 export enum PortModeType { 580 /** 581 * 无 582 * 583 * @since 8 584 */ 585 NONE = 0, 586 /** 587 * device 数据上行,需要外部供电 588 * 589 * @since 8 590 */ 591 UFP = 1, 592 /** 593 * host 数据下行,对外提供电源 594 * 595 * @since 8 596 */ 597 DFP = 2, 598 /** 599 * DRP既可以做DFP(Host),也可以做UFP(Device),当前不支持 600 * 601 * @since 8 602 */ 603 DRP = 3, 604 /** 605 * 当前不支持 606 * 607 * @since 8 608 */ 609 NUM_MODES = 4 610 } 611 612 /** 613 * USB设备端口角色信息 614 * 615 * @since 8 616 */ 617 interface USBPortStatus { 618 /** 619 * 当前的USB模式 620 * 621 * @since 8 622 */ 623 currentMode: number; 624 625 /** 626 * 当前设备充电模式 627 * 628 * @since 8 629 */ 630 currentPowerRole: number; 631 632 /** 633 * 当前设备数据传输模式 634 * 635 * @since 8 636 */ 637 currentDataRole: number; 638 } 639 640 /** 641 * USB设备端口信息 642 * 643 * @since 8 644 */ 645 interface USBPort { 646 /** 647 * USB端口信息唯一标识 648 * 649 * @since 8 650 */ 651 id: number; 652 653 /** 654 * 当前端口支持的模式列表的组合掩码 655 * 656 * @since 8 657 */ 658 supportedModes: PortModeType; 659 660 /** 661 * 当前端口角色信息 {@link USBPortStatus} 662 * 663 * @since 8 664 */ 665 status: USBPortStatus; 666 } 667 668 /** 669 * 控制传输参数 670 * 671 * @since 8 672 */ 673 interface USBControlParams { 674 /** 675 * 请求类型 676 * 677 * @since 8 678 */ 679 request: number; 680 /** 681 * 请求目标类型 682 * 683 * @since 8 684 */ 685 target: USBRequestTargetType; 686 /** 687 * 控制请求类型 688 * 689 * @since 8 690 */ 691 reqType: USBControlRequestType; 692 /** 693 * 请求参数 694 * 695 * @since 8 696 */ 697 value: number; 698 /** 699 * 根据请求参数value的不同有所不同 700 * 701 * @since 8 702 */ 703 index: number; 704 /** 705 * @用于写入或读取的缓冲区 706 * @since 8 707 */ 708 data: Uint8Array; 709 } 710 711 /** 712 * 请求目标类型 713 * 714 * @since 8 715 */ 716 export enum USBRequestTargetType { 717 /** 718 * 设备 719 * 720 * @since 8 721 */ 722 USB_REQUEST_TARGET_DEVICE = 0, 723 /** 724 * 接口 725 * 726 * @since 8 727 */ 728 USB_REQUEST_TARGET_INTERFACE = 1, 729 /** 730 * 端点 731 * 732 * @since 8 733 */ 734 USB_REQUEST_TARGET_ENDPOINT = 2, 735 /** 736 * 其他 737 * 738 * @since 8 739 */ 740 USB_REQUEST_TARGET_OTHER = 3 741 } 742 743 /** 744 * 控制请求类型 745 * @since 8 746 */ 747 export enum USBControlRequestType { 748 /** 749 * 标准 750 * 751 * @since 8 752 */ 753 USB_REQUEST_TYPE_STANDARD = 0, 754 /** 755 * 类 756 * 757 * @since 8 758 */ 759 USB_REQUEST_TYPE_CLASS = 1, 760 /** 761 * 厂商 762 * 763 * @since 8 764 */ 765 USB_REQUEST_TYPE_VENDOR = 2 766 } 767 768 /** 769 * 请求方向 770 * @since 8 771 */ 772 export enum USBRequestDirection { 773 /** 774 * 写数据,主设备往从设备 775 * 776 * @since 8 777 */ 778 USB_REQUEST_DIR_TO_DEVICE = 0, 779 /** 780 * 读数据,从设备往主设备 781 * 782 * @since 8 783 */ 784 USB_REQUEST_DIR_FROM_DEVICE = 0x80 785 } 786 787 /** 788 * function 模式 789 * 790 * @since 8 791 */ 792 export enum FunctionType { 793 /** 794 * 无 795 * 796 * @since 8 797 */ 798 NONE = 0, 799 /** 800 * 串口设备 801 * 802 * @since 8 803 */ 804 ACM = 1, 805 /** 806 * 网口设备 807 * 808 * @since 8 809 */ 810 ECM = 2, 811 /** 812 * HDC设备 813 * 814 * @since 8 815 */ 816 HDC = 4, 817 /** 818 * MTP设备 819 * 820 * @since 8 821 */ 822 MTP = 8, 823 /** 824 * PTP设备 825 * 826 * @since 8 827 */ 828 PTP = 16, 829 /** 830 * RNDIS设备 831 * 832 * @since 8 833 */ 834 RNDIS = 32, 835 /** 836 * MIDI设备 837 * 838 * @since 8 839 */ 840 MIDI = 64, 841 /** 842 * AUDIO SOURCE设备 843 * 844 * @since 8 845 */ 846 AUDIO_SOURCE = 128, 847 /** 848 * NCM设备 849 * 850 * @since 8 851 */ 852 NCM = 256 853 } 854 855} 856 857export default usb; 858